xref: /freebsd/usr.bin/netstat/bpf.c (revision 4ce386ff25d77954b8cfa11534f632172e848244)
1 /*-
2  * Copyright (c) 2005 Christian S.J. Peron
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/protosw.h>
32 #include <sys/socket.h>
33 #include <sys/socketvar.h>
34 #include <sys/sysctl.h>
35 #include <sys/param.h>
36 #include <sys/user.h>
37 
38 #include <net/if.h>
39 #include <net/if_var.h>
40 #include <net/bpf.h>
41 #include <net/bpfdesc.h>
42 #include <arpa/inet.h>
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdbool.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <libxo/xo.h>
53 
54 #include "netstat.h"
55 
56 /* print bpf stats */
57 
58 static char *
59 bpf_pidname(pid_t pid)
60 {
61 	struct kinfo_proc newkp;
62 	int error, mib[4];
63 	size_t size;
64 
65 	mib[0] = CTL_KERN;
66 	mib[1] = KERN_PROC;
67 	mib[2] = KERN_PROC_PID;
68 	mib[3] = pid;
69 	size = sizeof(newkp);
70 	error = sysctl(mib, 4, &newkp, &size, NULL, 0);
71 	if (error < 0) {
72 		xo_warn("kern.proc.pid failed");
73 		return (strdup("??????"));
74 	}
75 	return (strdup(newkp.ki_comm));
76 }
77 
78 static void
79 bpf_flags(struct xbpf_d *bd, char *flagbuf)
80 {
81 
82 	*flagbuf++ = bd->bd_promisc ? 'p' : '-';
83 	*flagbuf++ = bd->bd_immediate ? 'i' : '-';
84 	*flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
85 	*flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' :
86 	    ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's');
87 	*flagbuf++ = bd->bd_feedback ? 'b' : '-';
88 	*flagbuf++ = bd->bd_async ? 'a' : '-';
89 	*flagbuf++ = bd->bd_locked ? 'l' : '-';
90 	*flagbuf++ = '\0';
91 
92 	if (bd->bd_promisc)
93 		xo_emit("{e:promiscuous/}");
94 	if (bd->bd_immediate)
95 		xo_emit("{e:immediate/}");
96 	if (bd->bd_hdrcmplt)
97 		xo_emit("{e:header-complete/}");
98 	xo_emit("{e:direction}", (bd->bd_direction == BPF_D_IN) ? "input" :
99 	    (bd->bd_direction == BPF_D_OUT) ? "output" : "bidirectional");
100 	if (bd->bd_feedback)
101 		xo_emit("{e:feedback/}");
102 	if (bd->bd_async)
103 		xo_emit("{e:async/}");
104 	if (bd->bd_locked)
105 		xo_emit("{e:locked/}");
106 }
107 
108 void
109 bpf_stats(char *ifname)
110 {
111 	struct xbpf_d *d, *bd, zerostat;
112 	char *pname, flagbuf[12];
113 	size_t size;
114 
115 	if (zflag) {
116 		bzero(&zerostat, sizeof(zerostat));
117 		if (sysctlbyname("net.bpf.stats", NULL, NULL,
118 		    &zerostat, sizeof(zerostat)) < 0)
119 			xo_warn("failed to zero bpf counters");
120 		return;
121 	}
122 	if (sysctlbyname("net.bpf.stats", NULL, &size,
123 	    NULL, 0) < 0) {
124 		xo_warn("net.bpf.stats");
125 		return;
126 	}
127 	if (size == 0)
128 		return;
129 	bd = malloc(size);
130 	if (bd == NULL) {
131 		xo_warn("malloc failed");
132 		return;
133 	}
134 	if (sysctlbyname("net.bpf.stats", bd, &size,
135 	    NULL, 0) < 0) {
136 		xo_warn("net.bpf.stats");
137 		free(bd);
138 		return;
139 	}
140 	xo_emit("{T:/%5s} {T:/%6s} {T:/%7s} {T:/%9s} {T:/%9s} {T:/%9s} "
141 	    "{T:/%5s} {T:/%5s} {T:/%s}\n",
142 	    "Pid", "Netif", "Flags", "Recv", "Drop", "Match",
143 	    "Sblen", "Hblen", "Command");
144 	xo_open_container("bpf-statistics");
145 	xo_open_list("bpf-entry");
146 	for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
147 		if (d->bd_structsize != sizeof(*d)) {
148 			xo_warnx("bpf_stats_extended: version mismatch");
149 			return;
150 		}
151 		if (ifname && strcmp(ifname, d->bd_ifname) != 0)
152 			continue;
153 		xo_open_instance("bpf-entry");
154 		pname = bpf_pidname(d->bd_pid);
155 		xo_emit("{k:pid/%5d} {k:interface-name/%6s} ",
156 		    d->bd_pid, d->bd_ifname);
157 		bpf_flags(d, flagbuf);
158 		xo_emit("{d:flags/%7s} {:received-packets/%9ju} "
159 		    "{:dropped-packets/%9ju} {:filter-packets/%9ju} "
160 		    "{:store-buffer-length/%5d} {:hold-buffer-length/%5d} "
161 		    "{:process/%s}\n",
162 		    flagbuf, (uintmax_t)d->bd_rcount, (uintmax_t)d->bd_dcount,
163 		    (uintmax_t)d->bd_fcount, d->bd_slen, d->bd_hlen, pname);
164 		free(pname);
165 		xo_close_instance("bpf-entry");
166 	}
167 	xo_close_list("bpf-entry");
168 	xo_close_container("bpf-statistics");
169 	free(bd);
170 }
171