xref: /freebsd/usr.bin/netstat/bpf.c (revision cc16dea626cf2fc80cde667ac4798065108e596c)
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 <string.h>
50 #include <unistd.h>
51 
52 #include "netstat.h"
53 
54 /* print bpf stats */
55 
56 static char *
57 bpf_pidname(pid_t pid)
58 {
59 	struct kinfo_proc newkp;
60 	int error, mib[4];
61 	size_t size;
62 
63 	mib[0] = CTL_KERN;
64 	mib[1] = KERN_PROC;
65 	mib[2] = KERN_PROC_PID;
66 	mib[3] = pid;
67 	size = sizeof(newkp);
68 	error = sysctl(mib, 4, &newkp, &size, NULL, 0);
69 	if (error < 0) {
70 		warn("kern.proc.pid failed");
71 		return (strdup("??????"));
72 	}
73 	return (strdup(newkp.ki_comm));
74 }
75 
76 static void
77 bpf_flags(struct xbpf_d *bd, char *flagbuf)
78 {
79 
80 	*flagbuf++ = bd->bd_promisc ? 'p' : '-';
81 	*flagbuf++ = bd->bd_immediate ? 'i' : '-';
82 	*flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
83 	*flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' :
84 	    ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's');
85 	*flagbuf++ = bd->bd_feedback ? 'b' : '-';
86 	*flagbuf++ = bd->bd_async ? 'a' : '-';
87 	*flagbuf++ = bd->bd_locked ? 'l' : '-';
88 	*flagbuf++ = '\0';
89 }
90 
91 void
92 bpf_stats(char *ifname)
93 {
94 	struct xbpf_d *d, *bd, zerostat;
95 	char *pname, flagbuf[12];
96 	size_t size;
97 
98 	if (zflag) {
99 		bzero(&zerostat, sizeof(zerostat));
100 		if (sysctlbyname("net.bpf.stats", NULL, NULL,
101 		    &zerostat, sizeof(zerostat)) < 0)
102 			warn("failed to zero bpf counters");
103 		return;
104 	}
105 	if (sysctlbyname("net.bpf.stats", NULL, &size,
106 	    NULL, 0) < 0) {
107 		warn("net.bpf.stats");
108 		return;
109 	}
110 	if (size == 0)
111 		return;
112 	bd = malloc(size);
113 	if (bd == NULL) {
114 		warn("malloc failed");
115 		return;
116 	}
117 	if (sysctlbyname("net.bpf.stats", bd, &size,
118 	    NULL, 0) < 0) {
119 		warn("net.bpf.stats");
120 		free(bd);
121 		return;
122 	}
123 	(void) printf("%5s %6s %7s %9s %9s %9s %5s %5s %s\n",
124 	    "Pid", "Netif", "Flags", "Recv", "Drop", "Match", "Sblen",
125 	    "Hblen", "Command");
126 	for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
127 		if (d->bd_structsize != sizeof(*d)) {
128 			warnx("bpf_stats_extended: version mismatch");
129 			return;
130 		}
131 		if (ifname && strcmp(ifname, d->bd_ifname) != 0)
132 			continue;
133 		bpf_flags(d, flagbuf);
134 		pname = bpf_pidname(d->bd_pid);
135 		(void) printf("%5d %6s %7s %9ju %9ju %9ju %5d %5d %s\n",
136 		    d->bd_pid, d->bd_ifname, flagbuf,
137 		    d->bd_rcount, d->bd_dcount, d->bd_fcount,
138 		    d->bd_slen, d->bd_hlen, pname);
139 		free(pname);
140 	}
141 	free(bd);
142 }
143