bpf.c revision 1.1 1 /* $NetBSD: bpf.c,v 1.1 2005/08/04 19:39:40 rpaulo Exp $ */
2
3 /*
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Rui Paulo.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <kvm.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <net/if.h>
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/sysctl.h>
51 #include <net/bpfdesc.h>
52 #include <net/bpf.h>
53 #include "netstat.h"
54
55 void
56 bpf_stats(void)
57 {
58 struct bpf_stat bpf_s;
59 size_t len = sizeof(bpf_s);
60
61 if (sysctlbyname("net.bpf.stats", &bpf_s, &len, NULL, 0) == -1)
62 err(1, "net.bpf.stats");
63
64 printf("bpf:\n");
65 printf("\t%lld total packets received\n", bpf_s.bs_recv);
66 printf("\t%lld total packets captured\n", bpf_s.bs_capt);
67 printf("\t%lld total packets dropped\n", bpf_s.bs_drop);
68 }
69
70 void
71 bpf_dump(kvm_t *kd, char *interface)
72 {
73 int name[CTL_MAXNAME], rc, i, cnt;
74 size_t sz;
75 u_int namelen;
76 void *v;
77 struct bpf_d_ext *dpe;
78 struct kinfo_proc *kp;
79
80 /* adapted from sockstat.c by Andrew Brown */
81
82 sz = CTL_MAXNAME;
83 if (sysctlnametomib("net.bpf.peers", &name[0], &sz) == -1)
84 err(1, "sysctlnametomib");
85 namelen = sz;
86
87 name[namelen++] = sizeof(*dpe);
88 name[namelen++] = INT_MAX;
89
90 v = NULL;
91 sz = 0;
92 do {
93 rc = sysctl(&name[0], namelen, v, &sz, NULL, 0);
94 if (rc == -1 && errno != ENOMEM)
95 err(1, "sysctl: net.bpf.peers");
96 if (rc == -1 && v != NULL) {
97 free(v);
98 v = NULL;
99 }
100 if (v == NULL) {
101 v = malloc(sz);
102 rc = -1;
103 }
104 if (v == NULL)
105 err(1, "malloc");
106 } while (rc == -1);
107
108 dpe = v;
109
110 printf("Active BPF peers\n");
111 printf("PID\tInt\tRecv Drop Capt Flags Bufsize Comm\n");
112
113 #define BPFEXT(entry) dpe->entry
114
115 for (i = 0; i < (sz / sizeof(*dpe)); i++, dpe++) {
116 if (interface &&
117 strncmp(BPFEXT(bde_ifname), interface, IFNAMSIZ))
118 continue;
119
120 printf("%-7d ", BPFEXT(bde_pid));
121 printf("%-7s ",
122 (BPFEXT(bde_ifname)[0] == '\0') ? "-" :
123 BPFEXT(bde_ifname));
124
125 printf("%-8lld %-8lld %-8lld ",
126 BPFEXT(bde_rcount), BPFEXT(bde_dcount),
127 BPFEXT(bde_ccount));
128
129 switch (BPFEXT(bde_state)) {
130 case BPF_IDLE:
131 printf("I");
132 break;
133 case BPF_WAITING:
134 printf("W");
135 break;
136 case BPF_TIMED_OUT:
137 printf("T");
138 break;
139 default:
140 printf("-");
141 break;
142 }
143
144 printf("%c", BPFEXT(bde_promisc) ? 'P' : '-');
145 printf("%c", BPFEXT(bde_immediate) ? 'R' : '-');
146 printf("%c", BPFEXT(bde_seesent) ? 'S' : '-');
147 printf("%c", BPFEXT(bde_hdrcmplt) ? 'H' : '-');
148 printf(" %-8d ", BPFEXT(bde_bufsize));
149
150 kp = kvm_getprocs(kd, KERN_PROC_PID, BPFEXT(bde_pid), &cnt);
151
152 if (cnt && kd)
153 printf("%s\n", kp->kp_proc.p_comm);
154 else
155 printf("-\n");
156 #undef BPFEXT
157 }
158 }
159