bpf.c revision 160787
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 * $FreeBSD: head/usr.bin/netstat/bpf.c 160787 2006-07-28 16:09:19Z yar $
27 */
28#include <sys/types.h>
29#include <sys/protosw.h>
30#include <sys/socket.h>
31#include <sys/sysctl.h>
32#include <sys/param.h>
33#include <sys/user.h>
34
35#include <net/if.h>
36#include <net/if_var.h>
37#include <net/bpfdesc.h>
38#include <arpa/inet.h>
39
40#include <err.h>
41#include <errno.h>
42#include <stdint.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48#include "netstat.h"
49
50/* print bpf stats */
51
52static char *
53bpf_pidname(pid_t pid)
54{
55	struct kinfo_proc newkp;
56	int error, mib[4];
57	size_t size;
58
59	mib[0] = CTL_KERN;
60	mib[1] = KERN_PROC;
61	mib[2] = KERN_PROC_PID;
62	mib[3] = pid;
63	size = sizeof(newkp);
64	error = sysctl(mib, 4, &newkp, &size, NULL, 0);
65	if (error < 0) {
66		warn("kern.proc.pid failed");
67		return (strdup("??????"));
68	}
69	return (strdup(newkp.ki_comm));
70}
71
72static void
73bpf_flags(struct xbpf_d *bd, char *flagbuf)
74{
75
76	*flagbuf++ = bd->bd_promisc ? 'p' : '-';
77	*flagbuf++ = bd->bd_immediate ? 'i' : '-';
78	*flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
79	*flagbuf++ = bd->bd_seesent ? 's' : '-';
80	*flagbuf++ = bd->bd_async ? 'a' : '-';
81	*flagbuf++ = bd->bd_locked ? 'l' : '-';
82	*flagbuf++ = '\0';
83}
84
85void
86bpf_stats(char *interface)
87{
88	struct xbpf_d *d, *bd;
89	char *pname, flagbuf[12];
90	size_t size;
91
92	if (sysctlbyname("net.bpf.stats", NULL, &size,
93	    NULL, 0) < 0) {
94		warn("net.bpf.stats");
95		return;
96	}
97	if (size == 0)
98		return;
99	bd = malloc(size);
100	if (bd == NULL) {
101		warn("malloc failed");
102		return;
103	}
104	if (sysctlbyname("net.bpf.stats", bd, &size,
105	    NULL, 0) < 0) {
106		warn("net.bpf.stats");
107		free(bd);
108		return;
109	}
110	printf("%5s %6s %6s %9s %9s %9s %5s %5s %s\n",
111	    "Pid", "Netif", "Flags", "Recv", "Drop", "Match", "Sblen",
112	    "Hblen", "Command");
113	for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
114		if (interface && strcmp(interface, d->bd_ifname) != 0)
115			continue;
116		bpf_flags(d, flagbuf);
117		pname = bpf_pidname(d->bd_pid);
118		printf("%5d %6s %6s %9lu %9lu %9lu %5d %5d %s\n",
119		    d->bd_pid, d->bd_ifname, flagbuf,
120		    d->bd_rcount, d->bd_dcount, d->bd_fcount,
121		    d->bd_slen, d->bd_hlen, pname);
122		free(pname);
123	}
124	free(bd);
125}
126