1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005 Christian S.J. Peron
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/types.h>
33#include <sys/protosw.h>
34#include <sys/socket.h>
35#include <sys/socketvar.h>
36#include <sys/sysctl.h>
37#include <sys/param.h>
38#include <sys/user.h>
39
40#include <net/if.h>
41#include <net/bpf.h>
42#include <net/bpfdesc.h>
43#include <arpa/inet.h>
44
45#include <err.h>
46#include <errno.h>
47#include <stdint.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <stdbool.h>
51#include <string.h>
52#include <unistd.h>
53#include <libxo/xo.h>
54
55#include "netstat.h"
56
57/* print bpf stats */
58
59static char *
60bpf_pidname(pid_t pid)
61{
62	struct kinfo_proc newkp;
63	int error, mib[4];
64	size_t size;
65
66	mib[0] = CTL_KERN;
67	mib[1] = KERN_PROC;
68	mib[2] = KERN_PROC_PID;
69	mib[3] = pid;
70	size = sizeof(newkp);
71	error = sysctl(mib, 4, &newkp, &size, NULL, 0);
72	if (error < 0) {
73		xo_warn("kern.proc.pid failed");
74		return (strdup("??????"));
75	}
76	return (strdup(newkp.ki_comm));
77}
78
79static void
80bpf_flags(struct xbpf_d *bd, char *flagbuf)
81{
82
83	*flagbuf++ = bd->bd_promisc ? 'p' : '-';
84	*flagbuf++ = bd->bd_immediate ? 'i' : '-';
85	*flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
86	*flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' :
87	    ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's');
88	*flagbuf++ = bd->bd_feedback ? 'b' : '-';
89	*flagbuf++ = bd->bd_async ? 'a' : '-';
90	*flagbuf++ = bd->bd_locked ? 'l' : '-';
91	*flagbuf++ = '\0';
92
93	if (bd->bd_promisc)
94		xo_emit("{e:promiscuous/}");
95	if (bd->bd_immediate)
96		xo_emit("{e:immediate/}");
97	if (bd->bd_hdrcmplt)
98		xo_emit("{e:header-complete/}");
99	xo_emit("{e:direction}", (bd->bd_direction == BPF_D_IN) ? "input" :
100	    (bd->bd_direction == BPF_D_OUT) ? "output" : "bidirectional");
101	if (bd->bd_feedback)
102		xo_emit("{e:feedback/}");
103	if (bd->bd_async)
104		xo_emit("{e:async/}");
105	if (bd->bd_locked)
106		xo_emit("{e:locked/}");
107}
108
109void
110bpf_stats(char *ifname)
111{
112	struct xbpf_d *d, *bd, zerostat;
113	char *pname, flagbuf[12];
114	size_t size;
115
116	if (zflag) {
117		bzero(&zerostat, sizeof(zerostat));
118		if (sysctlbyname("net.bpf.stats", NULL, NULL,
119		    &zerostat, sizeof(zerostat)) < 0)
120			xo_warn("failed to zero bpf counters");
121		return;
122	}
123	if (sysctlbyname("net.bpf.stats", NULL, &size,
124	    NULL, 0) < 0) {
125		xo_warn("net.bpf.stats");
126		return;
127	}
128	if (size == 0)
129		return;
130	bd = malloc(size);
131	if (bd == NULL) {
132		xo_warn("malloc failed");
133		return;
134	}
135	if (sysctlbyname("net.bpf.stats", bd, &size,
136	    NULL, 0) < 0) {
137		xo_warn("net.bpf.stats");
138		free(bd);
139		return;
140	}
141	xo_emit("{T:/%5s} {T:/%6s} {T:/%7s} {T:/%9s} {T:/%9s} {T:/%9s} "
142	    "{T:/%5s} {T:/%5s} {T:/%s}\n",
143	    "Pid", "Netif", "Flags", "Recv", "Drop", "Match",
144	    "Sblen", "Hblen", "Command");
145	xo_open_container("bpf-statistics");
146	xo_open_list("bpf-entry");
147	for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
148		if (d->bd_structsize != sizeof(*d)) {
149			xo_warnx("bpf_stats_extended: version mismatch");
150			return;
151		}
152		if (ifname && strcmp(ifname, d->bd_ifname) != 0)
153			continue;
154		xo_open_instance("bpf-entry");
155		pname = bpf_pidname(d->bd_pid);
156		xo_emit("{k:pid/%5d} {k:interface-name/%6s} ",
157		    d->bd_pid, d->bd_ifname);
158		bpf_flags(d, flagbuf);
159		xo_emit("{d:flags/%7s} {:received-packets/%9ju} "
160		    "{:dropped-packets/%9ju} {:filter-packets/%9ju} "
161		    "{:store-buffer-length/%5d} {:hold-buffer-length/%5d} "
162		    "{:process/%s}\n",
163		    flagbuf, (uintmax_t)d->bd_rcount, (uintmax_t)d->bd_dcount,
164		    (uintmax_t)d->bd_fcount, d->bd_slen, d->bd_hlen, pname);
165		free(pname);
166		xo_close_instance("bpf-entry");
167	}
168	xo_close_list("bpf-entry");
169	xo_close_container("bpf-statistics");
170	free(bd);
171}
172