Home | History | Annotate | Line # | Download | only in dist
pcap-netmap.c revision 1.1.1.2
      1      1.1  christos /*
      2      1.1  christos  * Copyright (C) 2014 Luigi Rizzo. All rights reserved.
      3      1.1  christos  *
      4      1.1  christos  * Redistribution and use in source and binary forms, with or without
      5      1.1  christos  * modification, are permitted provided that the following conditions
      6      1.1  christos  * are met:
      7      1.1  christos  *
      8      1.1  christos  *   1. Redistributions of source code must retain the above copyright
      9      1.1  christos  *      notice, this list of conditions and the following disclaimer.
     10      1.1  christos  *   2. Redistributions in binary form must reproduce the above copyright
     11      1.1  christos  *      notice, this list of conditions and the following disclaimer in the
     12      1.1  christos  *      documentation and/or other materials provided with the distribution.
     13      1.1  christos  *
     14      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''AND
     15      1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16      1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17      1.1  christos  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18      1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22      1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23      1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24      1.1  christos  * SUCH DAMAGE.
     25      1.1  christos  */
     26      1.1  christos 
     27      1.1  christos #ifdef HAVE_CONFIG_H
     28      1.1  christos #include <config.h>
     29      1.1  christos #endif
     30      1.1  christos 
     31      1.1  christos #include <poll.h>
     32      1.1  christos #include <ctype.h>
     33      1.1  christos #include <errno.h>
     34      1.1  christos #include <netdb.h>
     35      1.1  christos #include <stdio.h>
     36      1.1  christos #include <stdlib.h>
     37      1.1  christos #include <string.h>
     38      1.1  christos #include <unistd.h>
     39      1.1  christos 
     40      1.1  christos #define NETMAP_WITH_LIBS
     41      1.1  christos #include <net/netmap_user.h>
     42      1.1  christos 
     43      1.1  christos #include "pcap-int.h"
     44      1.1  christos #include "pcap-netmap.h"
     45      1.1  christos 
     46      1.1  christos #ifndef __FreeBSD__
     47      1.1  christos   /*
     48      1.1  christos    * On FreeBSD we use IFF_PPROMISC which is in ifr_flagshigh.
     49      1.1  christos    * Remap to IFF_PROMISC on other platforms.
     50      1.1  christos    *
     51      1.1  christos    * XXX - DragonFly BSD?
     52      1.1  christos    */
     53      1.1  christos   #define IFF_PPROMISC	IFF_PROMISC
     54      1.1  christos #endif /* __FreeBSD__ */
     55      1.1  christos 
     56      1.1  christos struct pcap_netmap {
     57      1.1  christos 	struct nm_desc *d;	/* pointer returned by nm_open() */
     58      1.1  christos 	pcap_handler cb;	/* callback and argument */
     59      1.1  christos 	u_char *cb_arg;
     60      1.1  christos 	int must_clear_promisc;	/* flag */
     61      1.1  christos 	uint64_t rx_pkts;	/* # of pkts received before the filter */
     62      1.1  christos };
     63      1.1  christos 
     64      1.1  christos 
     65      1.1  christos static int
     66      1.1  christos pcap_netmap_stats(pcap_t *p, struct pcap_stat *ps)
     67      1.1  christos {
     68      1.1  christos 	struct pcap_netmap *pn = p->priv;
     69      1.1  christos 
     70  1.1.1.2  christos 	ps->ps_recv = (u_int)pn->rx_pkts;
     71      1.1  christos 	ps->ps_drop = 0;
     72      1.1  christos 	ps->ps_ifdrop = 0;
     73      1.1  christos 	return 0;
     74      1.1  christos }
     75      1.1  christos 
     76      1.1  christos 
     77      1.1  christos static void
     78      1.1  christos pcap_netmap_filter(u_char *arg, struct pcap_pkthdr *h, const u_char *buf)
     79      1.1  christos {
     80      1.1  christos 	pcap_t *p = (pcap_t *)arg;
     81      1.1  christos 	struct pcap_netmap *pn = p->priv;
     82      1.1  christos 	const struct bpf_insn *pc = p->fcode.bf_insns;
     83      1.1  christos 
     84      1.1  christos 	++pn->rx_pkts;
     85      1.1  christos 	if (pc == NULL || bpf_filter(pc, buf, h->len, h->caplen))
     86      1.1  christos 		pn->cb(pn->cb_arg, h, buf);
     87      1.1  christos }
     88      1.1  christos 
     89      1.1  christos 
     90      1.1  christos static int
     91      1.1  christos pcap_netmap_dispatch(pcap_t *p, int cnt, pcap_handler cb, u_char *user)
     92      1.1  christos {
     93      1.1  christos 	int ret;
     94      1.1  christos 	struct pcap_netmap *pn = p->priv;
     95      1.1  christos 	struct nm_desc *d = pn->d;
     96      1.1  christos 	struct pollfd pfd = { .fd = p->fd, .events = POLLIN, .revents = 0 };
     97      1.1  christos 
     98      1.1  christos 	pn->cb = cb;
     99      1.1  christos 	pn->cb_arg = user;
    100      1.1  christos 
    101      1.1  christos 	for (;;) {
    102      1.1  christos 		if (p->break_loop) {
    103      1.1  christos 			p->break_loop = 0;
    104      1.1  christos 			return PCAP_ERROR_BREAK;
    105      1.1  christos 		}
    106      1.1  christos 		/* nm_dispatch won't run forever */
    107      1.1  christos 
    108      1.1  christos 		ret = nm_dispatch((void *)d, cnt, (void *)pcap_netmap_filter, (void *)p);
    109      1.1  christos 		if (ret != 0)
    110      1.1  christos 			break;
    111      1.1  christos 		errno = 0;
    112      1.1  christos 		ret = poll(&pfd, 1, p->opt.timeout);
    113      1.1  christos 	}
    114      1.1  christos 	return ret;
    115      1.1  christos }
    116      1.1  christos 
    117      1.1  christos 
    118      1.1  christos /* XXX need to check the NIOCTXSYNC/poll */
    119      1.1  christos static int
    120      1.1  christos pcap_netmap_inject(pcap_t *p, const void *buf, size_t size)
    121      1.1  christos {
    122      1.1  christos 	struct pcap_netmap *pn = p->priv;
    123      1.1  christos 	struct nm_desc *d = pn->d;
    124      1.1  christos 
    125      1.1  christos 	return nm_inject(d, buf, size);
    126      1.1  christos }
    127      1.1  christos 
    128      1.1  christos 
    129      1.1  christos static int
    130      1.1  christos pcap_netmap_ioctl(pcap_t *p, u_long what, uint32_t *if_flags)
    131      1.1  christos {
    132      1.1  christos 	struct pcap_netmap *pn = p->priv;
    133      1.1  christos 	struct nm_desc *d = pn->d;
    134      1.1  christos 	struct ifreq ifr;
    135      1.1  christos 	int error, fd = d->fd;
    136      1.1  christos 
    137      1.1  christos #ifdef linux
    138      1.1  christos 	fd = socket(AF_INET, SOCK_DGRAM, 0);
    139      1.1  christos 	if (fd < 0) {
    140      1.1  christos 		fprintf(stderr, "Error: cannot get device control socket.\n");
    141      1.1  christos 		return -1;
    142      1.1  christos 	}
    143      1.1  christos #endif /* linux */
    144      1.1  christos 	bzero(&ifr, sizeof(ifr));
    145      1.1  christos 	strncpy(ifr.ifr_name, d->req.nr_name, sizeof(ifr.ifr_name));
    146      1.1  christos 	switch (what) {
    147      1.1  christos 	case SIOCSIFFLAGS:
    148      1.1  christos 		/*
    149      1.1  christos 		 * The flags we pass in are 32-bit and unsigned.
    150      1.1  christos 		 *
    151      1.1  christos 		 * On most if not all UN*Xes, ifr_flags is 16-bit and
    152      1.1  christos 		 * signed, and the result of assigning a longer
    153      1.1  christos 		 * unsigned value to a shorter signed value is
    154      1.1  christos 		 * implementation-defined (even if, in practice, it'll
    155      1.1  christos 		 * do what's intended on all platforms we support
    156      1.1  christos 		 * result of assigning a 32-bit unsigned value).
    157      1.1  christos 		 * So we mask out the upper 16 bits.
    158      1.1  christos 		 */
    159      1.1  christos 		ifr.ifr_flags = *if_flags & 0xffff;
    160      1.1  christos #ifdef __FreeBSD__
    161      1.1  christos 		/*
    162      1.1  christos 		 * In FreeBSD, we need to set the high-order flags,
    163      1.1  christos 		 * as we're using IFF_PPROMISC, which is in those bits.
    164      1.1  christos 		 *
    165      1.1  christos 		 * XXX - DragonFly BSD?
    166      1.1  christos 		 */
    167      1.1  christos 		ifr.ifr_flagshigh = *if_flags >> 16;
    168      1.1  christos #endif /* __FreeBSD__ */
    169      1.1  christos 		break;
    170      1.1  christos 	}
    171      1.1  christos 	error = ioctl(fd, what, &ifr);
    172      1.1  christos 	if (!error) {
    173      1.1  christos 		switch (what) {
    174      1.1  christos 		case SIOCGIFFLAGS:
    175      1.1  christos 			/*
    176      1.1  christos 			 * The flags we return are 32-bit.
    177      1.1  christos 			 *
    178      1.1  christos 			 * On most if not all UN*Xes, ifr_flags is
    179      1.1  christos 			 * 16-bit and signed, and will get sign-
    180      1.1  christos 			 * extended, so that the upper 16 bits of
    181      1.1  christos 			 * those flags will be forced on.  So we
    182      1.1  christos 			 * mask out the upper 16 bits of the
    183      1.1  christos 			 * sign-extended value.
    184      1.1  christos 			 */
    185      1.1  christos 			*if_flags = ifr.ifr_flags & 0xffff;
    186      1.1  christos #ifdef __FreeBSD__
    187      1.1  christos 			/*
    188      1.1  christos 			 * In FreeBSD, we need to return the
    189      1.1  christos 			 * high-order flags, as we're using
    190      1.1  christos 			 * IFF_PPROMISC, which is in those bits.
    191      1.1  christos 			 *
    192      1.1  christos 			 * XXX - DragonFly BSD?
    193      1.1  christos 			 */
    194      1.1  christos 			*if_flags |= (ifr.ifr_flagshigh << 16);
    195      1.1  christos #endif /* __FreeBSD__ */
    196      1.1  christos 		}
    197      1.1  christos 	}
    198      1.1  christos #ifdef linux
    199      1.1  christos 	close(fd);
    200      1.1  christos #endif /* linux */
    201      1.1  christos 	return error ? -1 : 0;
    202      1.1  christos }
    203      1.1  christos 
    204      1.1  christos 
    205      1.1  christos static void
    206      1.1  christos pcap_netmap_close(pcap_t *p)
    207      1.1  christos {
    208      1.1  christos 	struct pcap_netmap *pn = p->priv;
    209      1.1  christos 	struct nm_desc *d = pn->d;
    210      1.1  christos 	uint32_t if_flags = 0;
    211      1.1  christos 
    212      1.1  christos 	if (pn->must_clear_promisc) {
    213      1.1  christos 		pcap_netmap_ioctl(p, SIOCGIFFLAGS, &if_flags); /* fetch flags */
    214      1.1  christos 		if (if_flags & IFF_PPROMISC) {
    215      1.1  christos 			if_flags &= ~IFF_PPROMISC;
    216      1.1  christos 			pcap_netmap_ioctl(p, SIOCSIFFLAGS, &if_flags);
    217      1.1  christos 		}
    218      1.1  christos 	}
    219      1.1  christos 	nm_close(d);
    220      1.1  christos 	pcap_cleanup_live_common(p);
    221      1.1  christos }
    222      1.1  christos 
    223      1.1  christos 
    224      1.1  christos static int
    225      1.1  christos pcap_netmap_activate(pcap_t *p)
    226      1.1  christos {
    227      1.1  christos 	struct pcap_netmap *pn = p->priv;
    228      1.1  christos 	struct nm_desc *d;
    229      1.1  christos 	uint32_t if_flags = 0;
    230      1.1  christos 
    231      1.1  christos 	d = nm_open(p->opt.device, NULL, 0, NULL);
    232      1.1  christos 	if (d == NULL) {
    233      1.1  christos 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
    234      1.1  christos 		    errno, "netmap open: cannot access %s",
    235      1.1  christos 		    p->opt.device);
    236      1.1  christos 		pcap_cleanup_live_common(p);
    237      1.1  christos 		return (PCAP_ERROR);
    238      1.1  christos 	}
    239      1.1  christos #if 0
    240      1.1  christos 	fprintf(stderr, "%s device %s priv %p fd %d ports %d..%d\n",
    241      1.1  christos 	    __FUNCTION__, p->opt.device, d, d->fd,
    242      1.1  christos 	    d->first_rx_ring, d->last_rx_ring);
    243      1.1  christos #endif
    244      1.1  christos 	pn->d = d;
    245      1.1  christos 	p->fd = d->fd;
    246      1.1  christos 
    247      1.1  christos 	/*
    248      1.1  christos 	 * Turn a negative snapshot value (invalid), a snapshot value of
    249      1.1  christos 	 * 0 (unspecified), or a value bigger than the normal maximum
    250      1.1  christos 	 * value, into the maximum allowed value.
    251      1.1  christos 	 *
    252      1.1  christos 	 * If some application really *needs* a bigger snapshot
    253      1.1  christos 	 * length, we should just increase MAXIMUM_SNAPLEN.
    254      1.1  christos 	 */
    255      1.1  christos 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
    256      1.1  christos 		p->snapshot = MAXIMUM_SNAPLEN;
    257      1.1  christos 
    258      1.1  christos 	if (p->opt.promisc && !(d->req.nr_ringid & NETMAP_SW_RING)) {
    259      1.1  christos 		pcap_netmap_ioctl(p, SIOCGIFFLAGS, &if_flags); /* fetch flags */
    260      1.1  christos 		if (!(if_flags & IFF_PPROMISC)) {
    261      1.1  christos 			pn->must_clear_promisc = 1;
    262      1.1  christos 			if_flags |= IFF_PPROMISC;
    263      1.1  christos 			pcap_netmap_ioctl(p, SIOCSIFFLAGS, &if_flags);
    264      1.1  christos 		}
    265      1.1  christos 	}
    266      1.1  christos 	p->linktype = DLT_EN10MB;
    267      1.1  christos 	p->selectable_fd = p->fd;
    268      1.1  christos 	p->read_op = pcap_netmap_dispatch;
    269      1.1  christos 	p->inject_op = pcap_netmap_inject;
    270      1.1  christos 	p->setfilter_op = install_bpf_program;
    271      1.1  christos 	p->setdirection_op = NULL;
    272      1.1  christos 	p->set_datalink_op = NULL;
    273      1.1  christos 	p->getnonblock_op = pcap_getnonblock_fd;
    274      1.1  christos 	p->setnonblock_op = pcap_setnonblock_fd;
    275      1.1  christos 	p->stats_op = pcap_netmap_stats;
    276      1.1  christos 	p->cleanup_op = pcap_netmap_close;
    277      1.1  christos 
    278      1.1  christos 	return (0);
    279      1.1  christos }
    280      1.1  christos 
    281      1.1  christos 
    282      1.1  christos pcap_t *
    283      1.1  christos pcap_netmap_create(const char *device, char *ebuf, int *is_ours)
    284      1.1  christos {
    285      1.1  christos 	pcap_t *p;
    286      1.1  christos 
    287      1.1  christos 	*is_ours = (!strncmp(device, "netmap:", 7) || !strncmp(device, "vale", 4));
    288      1.1  christos 	if (! *is_ours)
    289      1.1  christos 		return NULL;
    290      1.1  christos 	p = pcap_create_common(ebuf, sizeof (struct pcap_netmap));
    291      1.1  christos 	if (p == NULL)
    292      1.1  christos 		return (NULL);
    293      1.1  christos 	p->activate_op = pcap_netmap_activate;
    294      1.1  christos 	return (p);
    295      1.1  christos }
    296      1.1  christos 
    297      1.1  christos /*
    298      1.1  christos  * The "device name" for netmap devices isn't a name for a device, it's
    299      1.1  christos  * an expression that indicates how the device should be set up, so
    300      1.1  christos  * there's no way to enumerate them.
    301      1.1  christos  */
    302      1.1  christos int
    303      1.1  christos pcap_netmap_findalldevs(pcap_if_list_t *devlistp _U_, char *err_str _U_)
    304      1.1  christos {
    305      1.1  christos 	return 0;
    306      1.1  christos }
    307