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