bpf.c revision 1.4 1 /* $NetBSD: bpf.c,v 1.4 2005/09/02 22:52:24 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 (use_sysctl) {
62 if (sysctlbyname("net.bpf.stats", &bpf_s, &len, NULL, 0) == -1)
63 err(1, "net.bpf.stats");
64
65 printf("bpf:\n");
66 printf("\t%" PRIu64 " total packets received\n",
67 bpf_s.bs_recv);
68 printf("\t%" PRIu64 " total packets captured\n",
69 bpf_s.bs_capt);
70 printf("\t%" PRIu64 " total packets dropped\n",
71 bpf_s.bs_drop);
72 } else {
73 /* XXX */
74 errx(1, "bpf_stats not implemented using kvm");
75 }
76 }
77
78 void
79 bpf_dump(char *interface)
80 {
81 struct bpf_d_ext *dpe;
82
83 if (use_sysctl) {
84 int name[CTL_MAXNAME], rc, i;
85 size_t sz, szproc;
86 u_int namelen;
87 void *v;
88 struct kinfo_proc2 p;
89
90 /* adapted from sockstat.c by Andrew Brown */
91
92 sz = CTL_MAXNAME;
93 if (sysctlnametomib("net.bpf.peers", &name[0], &sz) == -1)
94 err(1, "sysctlnametomib");
95 namelen = sz;
96
97 name[namelen++] = sizeof(*dpe);
98 name[namelen++] = INT_MAX;
99
100 v = NULL;
101 sz = 0;
102 do {
103 rc = sysctl(&name[0], namelen, v, &sz, NULL, 0);
104 if (rc == -1 && errno != ENOMEM)
105 err(1, "sysctl: net.bpf.peers");
106 if (rc == -1 && v != NULL) {
107 free(v);
108 v = NULL;
109 }
110 if (v == NULL) {
111 v = malloc(sz);
112 rc = -1;
113 }
114 if (v == NULL)
115 err(1, "malloc");
116 } while (rc == -1);
117
118 dpe = v;
119
120 printf("Active BPF peers\n");
121 printf("PID\tInt\tRecv Drop Capt");
122 printf(" Flags Bufsize Comm\n");
123
124 #define BPFEXT(entry) dpe->entry
125
126 for (i = 0; i < (sz / sizeof(*dpe)); i++, dpe++) {
127 if (interface &&
128 strncmp(BPFEXT(bde_ifname), interface, IFNAMSIZ))
129 continue;
130
131 printf("%-7d ", BPFEXT(bde_pid));
132 printf("%-7s ",
133 (BPFEXT(bde_ifname)[0] == '\0') ? "-" :
134 BPFEXT(bde_ifname));
135
136 printf("%-8" PRIu64 " %-8" PRIu64 " %-8" PRIu64 " ",
137 BPFEXT(bde_rcount), BPFEXT(bde_dcount),
138 BPFEXT(bde_ccount));
139
140 switch (BPFEXT(bde_state)) {
141 case BPF_IDLE:
142 printf("I");
143 break;
144 case BPF_WAITING:
145 printf("W");
146 break;
147 case BPF_TIMED_OUT:
148 printf("T");
149 break;
150 default:
151 printf("-");
152 break;
153 }
154
155 printf("%c", BPFEXT(bde_promisc) ? 'P' : '-');
156 printf("%c", BPFEXT(bde_immediate) ? 'R' : '-');
157 printf("%c", BPFEXT(bde_seesent) ? 'S' : '-');
158 printf("%c", BPFEXT(bde_hdrcmplt) ? 'H' : '-');
159 printf(" %-8d ", BPFEXT(bde_bufsize));
160
161 szproc = sizeof(p);
162 namelen = 0;
163 name[namelen++] = CTL_KERN;
164 name[namelen++] = KERN_PROC2;
165 name[namelen++] = KERN_PROC_PID;
166 name[namelen++] = BPFEXT(bde_pid);
167 name[namelen++] = szproc;
168 name[namelen++] = 1;
169
170 if (sysctl(&name[0], namelen, &p, &szproc,
171 NULL, 0) == -1)
172 printf("-\n");
173 else
174 printf("%s\n", p.p_comm);
175 #undef BPFEXT
176 }
177 } else {
178 /* XXX */
179 errx(1, "bpf_dump not implemented using kvm");
180 }
181 }
182