Home | History | Annotate | Line # | Download | only in if_vlan
bpfopen.c revision 1.1
      1  1.1  yamaguch /*	$NetBSD: bpfopen.c,v 1.1 2021/07/09 05:54:11 yamaguchi Exp $	*/
      2  1.1  yamaguch 
      3  1.1  yamaguch /*
      4  1.1  yamaguch  * Copyright (c) 2021 Internet Initiative Japan Inc.
      5  1.1  yamaguch  * All rights reserved.
      6  1.1  yamaguch  *
      7  1.1  yamaguch  * Redistribution and use in source and binary forms, with or without
      8  1.1  yamaguch  * modification, are permitted provided that the following conditions
      9  1.1  yamaguch  * are met:
     10  1.1  yamaguch  * 1. Redistributions of source code must retain the above copyright
     11  1.1  yamaguch  *    notice, this list of conditions and the following disclaimer.
     12  1.1  yamaguch  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  yamaguch  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  yamaguch  *    documentation and/or other materials provided with the distribution.
     15  1.1  yamaguch  *
     16  1.1  yamaguch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  yamaguch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  yamaguch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  yamaguch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  yamaguch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  yamaguch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  yamaguch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  yamaguch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  yamaguch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  yamaguch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  yamaguch  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  yamaguch  */
     28  1.1  yamaguch 
     29  1.1  yamaguch #include <sys/cdefs.h>
     30  1.1  yamaguch __RCSID("$NetBSD: bpfopen.c,v 1.1 2021/07/09 05:54:11 yamaguchi Exp $");
     31  1.1  yamaguch 
     32  1.1  yamaguch #include <sys/param.h>
     33  1.1  yamaguch #include <sys/ioctl.h>
     34  1.1  yamaguch 
     35  1.1  yamaguch #include <net/if.h>
     36  1.1  yamaguch #include <net/bpf.h>
     37  1.1  yamaguch 
     38  1.1  yamaguch #include <err.h>
     39  1.1  yamaguch #include <errno.h>
     40  1.1  yamaguch #include <fcntl.h>
     41  1.1  yamaguch #include <poll.h>
     42  1.1  yamaguch #include <stdio.h>
     43  1.1  yamaguch #include <stdlib.h>
     44  1.1  yamaguch #include <string.h>
     45  1.1  yamaguch #include <signal.h>
     46  1.1  yamaguch #include <unistd.h>
     47  1.1  yamaguch 
     48  1.1  yamaguch enum {
     49  1.1  yamaguch 	ARG_PROG = 0,
     50  1.1  yamaguch 	ARG_HOST,
     51  1.1  yamaguch 	ARG_IFNAME,
     52  1.1  yamaguch 	ARG_NUM
     53  1.1  yamaguch };
     54  1.1  yamaguch 
     55  1.1  yamaguch enum {
     56  1.1  yamaguch 	PFD_BPF = 0,
     57  1.1  yamaguch 	PFD_NUM
     58  1.1  yamaguch };
     59  1.1  yamaguch 
     60  1.1  yamaguch static void	sighandler(int);
     61  1.1  yamaguch 
     62  1.1  yamaguch static sig_atomic_t quit;
     63  1.1  yamaguch 
     64  1.1  yamaguch static void
     65  1.1  yamaguch usage(void)
     66  1.1  yamaguch {
     67  1.1  yamaguch 
     68  1.1  yamaguch 	fprintf(stderr, "%s {-r|-h} <ifname>\n"
     69  1.1  yamaguch 	    "\t-r: rump_server\n"
     70  1.1  yamaguch 	    "\t-h: host\n",
     71  1.1  yamaguch 	    getprogname());
     72  1.1  yamaguch 	exit(1);
     73  1.1  yamaguch }
     74  1.1  yamaguch 
     75  1.1  yamaguch int
     76  1.1  yamaguch main(int argc, char *argv[])
     77  1.1  yamaguch {
     78  1.1  yamaguch 	struct ifreq ifr;
     79  1.1  yamaguch 	struct pollfd pfd[PFD_NUM];
     80  1.1  yamaguch 	const char *bpf_path;
     81  1.1  yamaguch 	int n, bpfd, nfds;
     82  1.1  yamaguch 	size_t bufsiz;
     83  1.1  yamaguch 	char *buf;
     84  1.1  yamaguch 
     85  1.1  yamaguch 	if (argc != ARG_NUM)
     86  1.1  yamaguch 		usage();
     87  1.1  yamaguch 
     88  1.1  yamaguch 	if (strcmp(argv[ARG_HOST], "-h") == 0) {
     89  1.1  yamaguch 		bpf_path = "/dev/bpf";
     90  1.1  yamaguch 	} else if (strcmp(argv[ARG_HOST], "-r") == 0){
     91  1.1  yamaguch 		bpf_path = "/rump/dev/bpf";
     92  1.1  yamaguch 	} else {
     93  1.1  yamaguch 		errx(1, "-r or -h");
     94  1.1  yamaguch 	}
     95  1.1  yamaguch 
     96  1.1  yamaguch 	bpfd = open(bpf_path, O_RDONLY);
     97  1.1  yamaguch 	if (bpfd < 0)
     98  1.1  yamaguch 		err(1, "open %s", bpf_path);
     99  1.1  yamaguch 
    100  1.1  yamaguch 	memset(&ifr, 0, sizeof(ifr));
    101  1.1  yamaguch 	strlcpy(ifr.ifr_name, argv[ARG_IFNAME],
    102  1.1  yamaguch 	    sizeof(ifr.ifr_name));
    103  1.1  yamaguch 	if (ioctl(bpfd, BIOCSETIF, &ifr) != 0)
    104  1.1  yamaguch 		err(1, "BIOCSETIF");
    105  1.1  yamaguch 	if (ioctl(bpfd, BIOCPROMISC, NULL) != 0)
    106  1.1  yamaguch 		err(1, "BIOCPROMISC");
    107  1.1  yamaguch 	if (ioctl(bpfd, BIOCGBLEN, &bufsiz) != 0)
    108  1.1  yamaguch 		err(1, "BIOCGBLEN");
    109  1.1  yamaguch 	bufsiz = MIN(bufsiz, BPF_DFLTBUFSIZE * 4);
    110  1.1  yamaguch 
    111  1.1  yamaguch 	buf = malloc(bufsiz);
    112  1.1  yamaguch 	if (buf == NULL)
    113  1.1  yamaguch 		err(1, "malloc");
    114  1.1  yamaguch 
    115  1.1  yamaguch 	quit = 0;
    116  1.1  yamaguch 	signal(SIGTERM, sighandler);
    117  1.1  yamaguch 	signal(SIGQUIT, sighandler);
    118  1.1  yamaguch 	signal(SIGINT, sighandler);
    119  1.1  yamaguch 	signal(SIGHUP, sighandler);
    120  1.1  yamaguch 
    121  1.1  yamaguch 	fprintf(stderr, "bpf open %s\n", ifr.ifr_name);
    122  1.1  yamaguch 	while (quit == 0) {
    123  1.1  yamaguch 		pfd[PFD_BPF].fd = bpfd;
    124  1.1  yamaguch 		pfd[PFD_BPF].events = POLLIN;
    125  1.1  yamaguch 
    126  1.1  yamaguch 		nfds = poll(pfd, PFD_NUM, 1 * 1000);
    127  1.1  yamaguch 		if (nfds == -1 && errno != EINTR) {
    128  1.1  yamaguch 			warn("poll");
    129  1.1  yamaguch 			quit = 1;
    130  1.1  yamaguch 		}
    131  1.1  yamaguch 
    132  1.1  yamaguch 		if (nfds > 0 && (pfd[PFD_BPF].revents & POLLIN)) {
    133  1.1  yamaguch 			/* read & drop */
    134  1.1  yamaguch 			memset(buf, 0, sizeof(bufsiz));
    135  1.1  yamaguch 			n = read(pfd[PFD_BPF].fd, buf, bufsiz);
    136  1.1  yamaguch 			if (n < 0)
    137  1.1  yamaguch 				quit = 1;
    138  1.1  yamaguch 		}
    139  1.1  yamaguch 	}
    140  1.1  yamaguch 
    141  1.1  yamaguch 	close(bpfd);
    142  1.1  yamaguch 	free(buf);
    143  1.1  yamaguch 	fprintf(stderr, "closed\n");
    144  1.1  yamaguch 
    145  1.1  yamaguch 	return 0;
    146  1.1  yamaguch }
    147  1.1  yamaguch 
    148  1.1  yamaguch static void
    149  1.1  yamaguch sighandler(int signo)
    150  1.1  yamaguch {
    151  1.1  yamaguch 
    152  1.1  yamaguch 	quit = 1;
    153  1.1  yamaguch }
    154