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