2 * Copyright (c) 2005 Christian S.J. Peron
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/protosw.h>
31 #include <sys/socket.h>
32 #include <sys/socketvar.h>
33 #include <sys/sysctl.h>
34 #include <sys/param.h>
38 #include <net/if_var.h>
40 #include <net/bpfdesc.h>
41 #include <arpa/inet.h>
56 bpf_pidname(pid_t pid)
58 struct kinfo_proc newkp;
64 mib[2] = KERN_PROC_PID;
67 error = sysctl(mib, 4, &newkp, &size, NULL, 0);
69 warn("kern.proc.pid failed");
70 return (strdup("??????"));
72 return (strdup(newkp.ki_comm));
76 bpf_flags(struct xbpf_d *bd, char *flagbuf)
79 *flagbuf++ = bd->bd_promisc ? 'p' : '-';
80 *flagbuf++ = bd->bd_immediate ? 'i' : '-';
81 *flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
82 *flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' :
83 ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's');
84 *flagbuf++ = bd->bd_feedback ? 'b' : '-';
85 *flagbuf++ = bd->bd_async ? 'a' : '-';
86 *flagbuf++ = bd->bd_locked ? 'l' : '-';
91 bpf_stats(char *ifname)
93 struct xbpf_d *d, *bd;
94 char *pname, flagbuf[12];
97 if (sysctlbyname("net.bpf.stats", NULL, &size,
99 warn("net.bpf.stats");
106 warn("malloc failed");
109 if (sysctlbyname("net.bpf.stats", bd, &size,
111 warn("net.bpf.stats");
115 (void) printf("%5s %6s %7s %9s %9s %9s %5s %5s %s\n",
116 "Pid", "Netif", "Flags", "Recv", "Drop", "Match", "Sblen",
118 for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
119 if (ifname && strcmp(ifname, d->bd_ifname) != 0)
121 bpf_flags(d, flagbuf);
122 pname = bpf_pidname(d->bd_pid);
123 (void) printf("%5d %6s %7s %9ju %9ju %9ju %5d %5d %s\n",
124 d->bd_pid, d->bd_ifname, flagbuf,
125 (intmax_t)d->bd_rcount, (intmax_t)d->bd_dcount,
126 (intmax_t)d->bd_fcount,
127 d->bd_slen, d->bd_hlen, pname);