Home | History | Annotate | Line # | Download | only in netstat
bpf.c revision 1.6
      1 /*	$NetBSD: bpf.c,v 1.6 2006/09/22 23:21:52 elad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Rui Paulo.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <err.h>
     40 #include <errno.h>
     41 #include <fcntl.h>
     42 #include <kvm.h>
     43 #include <stdio.h>
     44 #include <stdlib.h>
     45 #include <string.h>
     46 #include <unistd.h>
     47 #include <net/if.h>
     48 #include <sys/types.h>
     49 #include <sys/param.h>
     50 #include <sys/sysctl.h>
     51 #include <net/bpfdesc.h>
     52 #include <net/bpf.h>
     53 #include "netstat.h"
     54 
     55 void
     56 bpf_stats(void)
     57 {
     58 	struct bpf_stat bpf_s;
     59 	size_t len = sizeof(bpf_s);
     60 
     61 	if (use_sysctl) {
     62 		if (sysctlbyname("net.bpf.stats", &bpf_s, &len, NULL, 0) == -1)
     63 			err(1, "net.bpf.stats");
     64 
     65 		printf("bpf:\n");
     66 		printf("\t%" PRIu64 " total packets received\n",
     67 		    bpf_s.bs_recv);
     68 		printf("\t%" PRIu64 " total packets captured\n",
     69 		    bpf_s.bs_capt);
     70 		printf("\t%" PRIu64 " total packets dropped\n",
     71 		    bpf_s.bs_drop);
     72 	} else {
     73 		/* XXX */
     74 		errx(1, "bpf_stats not implemented using kvm");
     75 	}
     76 }
     77 
     78 void
     79 bpf_dump(char *interface)
     80 {
     81 	struct bpf_d_ext *dpe;
     82 
     83 	if (use_sysctl) {
     84 		int	name[CTL_MAXNAME], rc, i;
     85 		size_t	sz, szproc;
     86 		u_int	namelen;
     87 		void	*v;
     88 		struct kinfo_proc2 p;
     89 
     90 		/* adapted from sockstat.c by Andrew Brown */
     91 
     92 		sz = CTL_MAXNAME;
     93 		if (sysctlnametomib("net.bpf.peers", &name[0], &sz) == -1)
     94 			err(1, "sysctlnametomib: net.bpf.peers");
     95 		namelen = sz;
     96 
     97 		name[namelen++] = sizeof(*dpe);
     98 		name[namelen++] = INT_MAX;
     99 
    100 		v = NULL;
    101 		sz = 0;
    102 		do {
    103 			rc = sysctl(&name[0], namelen, v, &sz, NULL, 0);
    104 			if (rc == -1 && errno != ENOMEM)
    105 				err(1, "sysctl: net.bpf.peers");
    106 			if (rc == -1 && v != NULL) {
    107 				free(v);
    108 				v = NULL;
    109 			}
    110 			if (v == NULL) {
    111 				v = malloc(sz);
    112 				rc = -1;
    113 			}
    114 			if (v == NULL)
    115 				err(1, "malloc");
    116 		} while (rc == -1);
    117 
    118 		dpe = v;
    119 
    120 		puts("Active BPF peers\nPID\tInt\tRecv     Drop     Capt" \
    121 		    "     Flags  Bufsize  Comm");
    122 
    123 #define BPFEXT(entry) dpe->entry
    124 
    125 		for (i = 0; i < (sz / sizeof(*dpe)); i++, dpe++) {
    126 			if (interface &&
    127 			    strncmp(BPFEXT(bde_ifname), interface, IFNAMSIZ))
    128 				continue;
    129 
    130 			printf("%-7d ", BPFEXT(bde_pid));
    131 			printf("%-7s ",
    132 			       (BPFEXT(bde_ifname)[0] == '\0') ? "-" :
    133 			       BPFEXT(bde_ifname));
    134 
    135 			printf("%-8" PRIu64 " %-8" PRIu64 " %-8" PRIu64 " ",
    136 				BPFEXT(bde_rcount), BPFEXT(bde_dcount),
    137 				BPFEXT(bde_ccount));
    138 
    139 			switch (BPFEXT(bde_state)) {
    140 			case BPF_IDLE:
    141 				printf("I");
    142 				break;
    143 			case BPF_WAITING:
    144 				printf("W");
    145 				break;
    146 			case BPF_TIMED_OUT:
    147 				printf("T");
    148 				break;
    149 			default:
    150 				printf("-");
    151 				break;
    152 			}
    153 
    154 			printf("%c", BPFEXT(bde_promisc) ? 'P' : '-');
    155 			printf("%c", BPFEXT(bde_immediate) ? 'R' : '-');
    156 			printf("%c", BPFEXT(bde_seesent) ? 'S' : '-');
    157 			printf("%c", BPFEXT(bde_hdrcmplt) ? 'H' : '-');
    158 			printf("  %-8d ", BPFEXT(bde_bufsize));
    159 
    160 			szproc = sizeof(p);
    161 			namelen = 0;
    162 			name[namelen++] = CTL_KERN;
    163 			name[namelen++] = KERN_PROC2;
    164 			name[namelen++] = KERN_PROC_PID;
    165 			name[namelen++] = BPFEXT(bde_pid);
    166 			name[namelen++] = szproc;
    167 			name[namelen++] = 1;
    168 
    169 			if (sysctl(&name[0], namelen, &p, &szproc,
    170 			    NULL, 0) == -1)
    171 				printf("-\n");
    172 			else
    173 				printf("%s\n", p.p_comm);
    174 #undef BPFEXT
    175 		}
    176 	} else {
    177                 /* XXX */
    178                 errx(1, "bpf_dump not implemented using kvm");
    179         }
    180 }
    181