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