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