1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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/types.h> 30 #include <sys/protosw.h> 31 #include <sys/socket.h> 32 #include <sys/socketvar.h> 33 #include <sys/sysctl.h> 34 #include <sys/param.h> 35 #include <sys/user.h> 36 37 #include <net/if.h> 38 #include <net/bpf.h> 39 #include <net/bpfdesc.h> 40 #include <arpa/inet.h> 41 42 #include <err.h> 43 #include <errno.h> 44 #include <stdint.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <stdbool.h> 48 #include <string.h> 49 #include <unistd.h> 50 #include <libxo/xo.h> 51 52 #include "netstat.h" 53 54 /* print bpf stats */ 55 56 static char * 57 bpf_pidname(pid_t pid) 58 { 59 struct kinfo_proc newkp; 60 int error, mib[4]; 61 size_t size; 62 63 mib[0] = CTL_KERN; 64 mib[1] = KERN_PROC; 65 mib[2] = KERN_PROC_PID; 66 mib[3] = pid; 67 size = sizeof(newkp); 68 error = sysctl(mib, 4, &newkp, &size, NULL, 0); 69 if (error < 0) { 70 xo_warn("kern.proc.pid failed"); 71 return (strdup("??????")); 72 } 73 return (strdup(newkp.ki_comm)); 74 } 75 76 static void 77 bpf_flags(struct xbpf_d *bd, char *flagbuf) 78 { 79 80 *flagbuf++ = bd->bd_promisc ? 'p' : '-'; 81 *flagbuf++ = bd->bd_immediate ? 'i' : '-'; 82 *flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f'; 83 *flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' : 84 ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's'); 85 *flagbuf++ = bd->bd_feedback ? 'b' : '-'; 86 *flagbuf++ = bd->bd_async ? 'a' : '-'; 87 *flagbuf++ = bd->bd_locked ? 'l' : '-'; 88 *flagbuf++ = '\0'; 89 90 if (bd->bd_promisc) 91 xo_emit("{e:promiscuous/}"); 92 if (bd->bd_immediate) 93 xo_emit("{e:immediate/}"); 94 if (bd->bd_hdrcmplt) 95 xo_emit("{e:header-complete/}"); 96 xo_emit("{e:direction}", (bd->bd_direction == BPF_D_IN) ? "input" : 97 (bd->bd_direction == BPF_D_OUT) ? "output" : "bidirectional"); 98 if (bd->bd_feedback) 99 xo_emit("{e:feedback/}"); 100 if (bd->bd_async) 101 xo_emit("{e:async/}"); 102 if (bd->bd_locked) 103 xo_emit("{e:locked/}"); 104 } 105 106 void 107 bpf_stats(char *ifname) 108 { 109 struct xbpf_d *d, *bd, zerostat; 110 char *pname, flagbuf[12]; 111 size_t size; 112 113 if (zflag) { 114 bzero(&zerostat, sizeof(zerostat)); 115 if (sysctlbyname("net.bpf.stats", NULL, NULL, 116 &zerostat, sizeof(zerostat)) < 0) 117 xo_warn("failed to zero bpf counters"); 118 return; 119 } 120 if (sysctlbyname("net.bpf.stats", NULL, &size, 121 NULL, 0) < 0) { 122 xo_warn("net.bpf.stats"); 123 return; 124 } 125 if (size == 0) 126 return; 127 bd = malloc(size); 128 if (bd == NULL) { 129 xo_warn("malloc failed"); 130 return; 131 } 132 if (sysctlbyname("net.bpf.stats", bd, &size, 133 NULL, 0) < 0) { 134 xo_warn("net.bpf.stats"); 135 free(bd); 136 return; 137 } 138 xo_emit("{T:/%5s} {T:/%6s} {T:/%7s} {T:/%9s} {T:/%9s} {T:/%9s} " 139 "{T:/%5s} {T:/%5s} {T:/%s}\n", 140 "Pid", "Netif", "Flags", "Recv", "Drop", "Match", 141 "Sblen", "Hblen", "Command"); 142 xo_open_container("bpf-statistics"); 143 xo_open_list("bpf-entry"); 144 for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) { 145 if (d->bd_structsize != sizeof(*d)) { 146 xo_warnx("bpf_stats_extended: version mismatch"); 147 return; 148 } 149 if (ifname && strcmp(ifname, d->bd_ifname) != 0) 150 continue; 151 xo_open_instance("bpf-entry"); 152 pname = bpf_pidname(d->bd_pid); 153 xo_emit("{k:pid/%5d} {k:interface-name/%6s} ", 154 d->bd_pid, d->bd_ifname); 155 bpf_flags(d, flagbuf); 156 xo_emit("{d:flags/%7s} {:received-packets/%9ju} " 157 "{:dropped-packets/%9ju} {:filter-packets/%9ju} " 158 "{:store-buffer-length/%5d} {:hold-buffer-length/%5d} " 159 "{:process/%s}\n", 160 flagbuf, (uintmax_t)d->bd_rcount, (uintmax_t)d->bd_dcount, 161 (uintmax_t)d->bd_fcount, d->bd_slen, d->bd_hlen, pname); 162 free(pname); 163 xo_close_instance("bpf-entry"); 164 } 165 xo_close_list("bpf-entry"); 166 xo_close_container("bpf-statistics"); 167 free(bd); 168 } 169