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