]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/netstat/bpf.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / netstat / bpf.c
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 #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>
35 #include <sys/user.h>
36
37 #include <net/if.h>
38 #include <net/if_var.h>
39 #include <net/bpf.h>
40 #include <net/bpfdesc.h>
41 #include <arpa/inet.h>
42
43 #include <err.h>
44 #include <errno.h>
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include "netstat.h"
52
53 /* print bpf stats */
54
55 static char *
56 bpf_pidname(pid_t pid)
57 {
58         struct kinfo_proc newkp;
59         int error, mib[4];
60         size_t size;
61
62         mib[0] = CTL_KERN;
63         mib[1] = KERN_PROC;
64         mib[2] = KERN_PROC_PID;
65         mib[3] = pid;
66         size = sizeof(newkp);
67         error = sysctl(mib, 4, &newkp, &size, NULL, 0);
68         if (error < 0) {
69                 warn("kern.proc.pid failed");
70                 return (strdup("??????"));
71         }
72         return (strdup(newkp.ki_comm));
73 }
74
75 static void
76 bpf_flags(struct xbpf_d *bd, char *flagbuf)
77 {
78
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' : '-';
87         *flagbuf++ = '\0';
88 }
89
90 void
91 bpf_stats(char *ifname)
92 {
93         struct xbpf_d *d, *bd;
94         char *pname, flagbuf[12];
95         size_t size;
96
97         if (sysctlbyname("net.bpf.stats", NULL, &size,
98             NULL, 0) < 0) {
99                 warn("net.bpf.stats");
100                 return;
101         }
102         if (size == 0)
103                 return;
104         bd = malloc(size);
105         if (bd == NULL) {
106                 warn("malloc failed");
107                 return;
108         }
109         if (sysctlbyname("net.bpf.stats", bd, &size,
110             NULL, 0) < 0) {
111                 warn("net.bpf.stats");
112                 free(bd);
113                 return;
114         }
115         (void) printf("%5s %6s %7s %9s %9s %9s %5s %5s %s\n",
116             "Pid", "Netif", "Flags", "Recv", "Drop", "Match", "Sblen",
117             "Hblen", "Command");
118         for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
119                 if (ifname && strcmp(ifname, d->bd_ifname) != 0)
120                         continue;
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);
128                 free(pname);
129         }
130         free(bd);
131 }