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