Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / usr.bin / netstat / bpf.c
blobbcb99275e864484d6dcd075a131c2058c0b3ad35
1 /* $NetBSD: bpf.c,v 1.8 2008/04/28 20:24:14 martin Exp $ */
3 /*
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Rui Paulo.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <err.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <kvm.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <net/if.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/sysctl.h>
44 #include <net/bpfdesc.h>
45 #include <net/bpf.h>
46 #include "netstat.h"
48 void
49 bpf_stats(void)
51 struct bpf_stat bpf_s;
52 size_t len = sizeof(bpf_s);
54 if (use_sysctl) {
55 if (sysctlbyname("net.bpf.stats", &bpf_s, &len, NULL, 0) == -1)
56 err(1, "net.bpf.stats");
58 printf("bpf:\n");
59 printf("\t%" PRIu64 " total packets received\n",
60 bpf_s.bs_recv);
61 printf("\t%" PRIu64 " total packets captured\n",
62 bpf_s.bs_capt);
63 printf("\t%" PRIu64 " total packets dropped\n",
64 bpf_s.bs_drop);
65 } else {
66 warnx("BPF stats not available via KVM.");
70 void
71 bpf_dump(const char *bpfif)
73 struct bpf_d_ext *dpe;
75 if (use_sysctl) {
76 int name[CTL_MAXNAME], rc;
77 size_t i, sz, szproc;
78 u_int namelen;
79 void *v;
80 struct kinfo_proc2 p;
82 /* adapted from sockstat.c by Andrew Brown */
84 sz = CTL_MAXNAME;
85 if (sysctlnametomib("net.bpf.peers", &name[0], &sz) == -1)
86 err(1, "sysctlnametomib: net.bpf.peers");
87 namelen = sz;
89 name[namelen++] = sizeof(*dpe);
90 name[namelen++] = INT_MAX;
92 v = NULL;
93 sz = 0;
94 do {
95 rc = sysctl(&name[0], namelen, v, &sz, NULL, 0);
96 if (rc == -1 && errno != ENOMEM)
97 err(1, "sysctl: net.bpf.peers");
98 if (rc == -1 && v != NULL) {
99 free(v);
100 v = NULL;
102 if (v == NULL) {
103 v = malloc(sz);
104 rc = -1;
106 if (v == NULL)
107 err(1, "malloc");
108 } while (rc == -1);
110 dpe = v;
112 puts("Active BPF peers\nPID\tInt\tRecv Drop Capt" \
113 " Flags Bufsize Comm");
115 #define BPFEXT(entry) dpe->entry
117 for (i = 0; i < (sz / sizeof(*dpe)); i++, dpe++) {
118 if (bpfif &&
119 strncmp(BPFEXT(bde_ifname), bpfif, IFNAMSIZ))
120 continue;
122 printf("%-7d ", BPFEXT(bde_pid));
123 printf("%-7s ",
124 (BPFEXT(bde_ifname)[0] == '\0') ? "-" :
125 BPFEXT(bde_ifname));
127 printf("%-8" PRIu64 " %-8" PRIu64 " %-8" PRIu64 " ",
128 BPFEXT(bde_rcount), BPFEXT(bde_dcount),
129 BPFEXT(bde_ccount));
131 switch (BPFEXT(bde_state)) {
132 case BPF_IDLE:
133 printf("I");
134 break;
135 case BPF_WAITING:
136 printf("W");
137 break;
138 case BPF_TIMED_OUT:
139 printf("T");
140 break;
141 default:
142 printf("-");
143 break;
146 printf("%c", BPFEXT(bde_promisc) ? 'P' : '-');
147 printf("%c", BPFEXT(bde_immediate) ? 'R' : '-');
148 printf("%c", BPFEXT(bde_seesent) ? 'S' : '-');
149 printf("%c", BPFEXT(bde_hdrcmplt) ? 'H' : '-');
150 printf(" %-8d ", BPFEXT(bde_bufsize));
152 szproc = sizeof(p);
153 namelen = 0;
154 name[namelen++] = CTL_KERN;
155 name[namelen++] = KERN_PROC2;
156 name[namelen++] = KERN_PROC_PID;
157 name[namelen++] = BPFEXT(bde_pid);
158 name[namelen++] = szproc;
159 name[namelen++] = 1;
161 if (sysctl(&name[0], namelen, &p, &szproc,
162 NULL, 0) == -1)
163 printf("-\n");
164 else
165 printf("%s\n", p.p_comm);
166 #undef BPFEXT
168 } else {
169 /* XXX */
170 errx(1, "bpf_dump not implemented using kvm");