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