source: rtems-libbsd/freebsd/usr.bin/netstat/bpf.c @ 0a57e1d

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 0a57e1d was 0a57e1d, checked in by Sebastian Huber <sebastian.huber@…>, on 11/06/13 at 08:35:05

Reduce divergence from FreeBSD sources

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
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/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <rtems/bsd/sys/types.h>
33#include <sys/protosw.h>
34#include <sys/socket.h>
35#include <sys/socketvar.h>
36#include <sys/sysctl.h>
37#include <rtems/bsd/sys/param.h>
38#include <sys/user.h>
39
40#include <net/if.h>
41#include <net/if_var.h>
42#include <net/bpf.h>
43#include <net/bpfdesc.h>
44#include <arpa/inet.h>
45
46#include <err.h>
47#include <errno.h>
48#include <stdint.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54#include "netstat.h"
55
56/* print bpf stats */
57
58static char *
59bpf_pidname(pid_t pid)
60{
61#ifdef __rtems__
62        return "rtems";
63#else /* __rtems__ */
64        struct kinfo_proc newkp;
65        int error, mib[4];
66        size_t size;
67
68        mib[0] = CTL_KERN;
69        mib[1] = KERN_PROC;
70        mib[2] = KERN_PROC_PID;
71        mib[3] = pid;
72        size = sizeof(newkp);
73        error = sysctl(mib, 4, &newkp, &size, NULL, 0);
74        if (error < 0) {
75                warn("kern.proc.pid failed");
76                return (strdup("??????"));
77        }
78        return (strdup(newkp.ki_comm));
79#endif /* __rtems__ */
80}
81
82static void
83bpf_flags(struct xbpf_d *bd, char *flagbuf)
84{
85
86        *flagbuf++ = bd->bd_promisc ? 'p' : '-';
87        *flagbuf++ = bd->bd_immediate ? 'i' : '-';
88        *flagbuf++ = bd->bd_hdrcmplt ? '-' : 'f';
89        *flagbuf++ = (bd->bd_direction == BPF_D_IN) ? '-' :
90            ((bd->bd_direction == BPF_D_OUT) ? 'o' : 's');
91        *flagbuf++ = bd->bd_feedback ? 'b' : '-';
92        *flagbuf++ = bd->bd_async ? 'a' : '-';
93        *flagbuf++ = bd->bd_locked ? 'l' : '-';
94        *flagbuf++ = '\0';
95}
96
97void
98bpf_stats(char *ifname)
99{
100        struct xbpf_d *d, *bd, zerostat;
101        char *pname, flagbuf[12];
102        size_t size;
103
104        if (zflag) {
105                bzero(&zerostat, sizeof(zerostat));
106                if (sysctlbyname("net.bpf.stats", NULL, NULL,
107                    &zerostat, sizeof(zerostat)) < 0)
108                        warn("failed to zero bpf counters");
109                return;
110        }
111        if (sysctlbyname("net.bpf.stats", NULL, &size,
112            NULL, 0) < 0) {
113                warn("net.bpf.stats");
114                return;
115        }
116        if (size == 0)
117                return;
118        bd = malloc(size);
119        if (bd == NULL) {
120                warn("malloc failed");
121                return;
122        }
123        if (sysctlbyname("net.bpf.stats", bd, &size,
124            NULL, 0) < 0) {
125                warn("net.bpf.stats");
126                free(bd);
127                return;
128        }
129        (void) printf("%5s %6s %7s %9s %9s %9s %5s %5s %s\n",
130            "Pid", "Netif", "Flags", "Recv", "Drop", "Match", "Sblen",
131            "Hblen", "Command");
132        for (d = &bd[0]; d < &bd[size / sizeof(*d)]; d++) {
133                if (d->bd_structsize != sizeof(*d)) {
134                        warnx("bpf_stats_extended: version mismatch");
135                        return;
136                }
137                if (ifname && strcmp(ifname, d->bd_ifname) != 0)
138                        continue;
139                bpf_flags(d, flagbuf);
140                pname = bpf_pidname(d->bd_pid);
141                (void) printf("%5d %6s %7s %9ju %9ju %9ju %5d %5d %s\n",
142                    d->bd_pid, d->bd_ifname, flagbuf,
143                    d->bd_rcount, d->bd_dcount, d->bd_fcount,
144                    d->bd_slen, d->bd_hlen, pname);
145                free(pname);
146        }
147        free(bd);
148}
Note: See TracBrowser for help on using the repository browser.