]> The Tcpdump Group git mirrors - libpcap/blob - pcap-snf.c
ade3783b97f0d992f89022d40fc5df2f6f5d92ad
[libpcap] / pcap-snf.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #ifndef _WIN32
6 #include <sys/param.h>
7 #endif /* !_WIN32 */
8
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12
13 #include <ctype.h>
14 #ifndef _WIN32
15 #include <netinet/in.h>
16 #include <sys/mman.h>
17 #include <sys/socket.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #endif /* !_WIN32 */
21
22 #include <snf.h>
23 #if SNF_VERSION_API >= 0x0003
24 #define SNF_HAVE_INJECT_API
25 #endif
26
27 #include "pcap-int.h"
28 #include "pcap-snf.h"
29
30 /*
31 * Private data for capturing on SNF devices.
32 */
33 struct pcap_snf {
34 snf_handle_t snf_handle; /* opaque device handle */
35 snf_ring_t snf_ring; /* opaque device ring handle */
36 #ifdef SNF_HAVE_INJECT_API
37 snf_inject_t snf_inj; /* inject handle, if inject is used */
38 #endif
39 int snf_timeout;
40 int snf_boardnum;
41 };
42
43 static int
44 snf_set_datalink(pcap_t *p, int dlt)
45 {
46 p->linktype = dlt;
47 return (0);
48 }
49
50 static int
51 snf_pcap_stats(pcap_t *p, struct pcap_stat *ps)
52 {
53 struct snf_ring_stats stats;
54 struct pcap_snf *snfps = p->priv;
55 int rc;
56
57 if ((rc = snf_ring_getstats(snfps->snf_ring, &stats))) {
58 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_get_stats: %s",
59 pcap_strerror(rc));
60 return -1;
61 }
62 ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow;
63 ps->ps_drop = stats.ring_pkt_overflow;
64 ps->ps_ifdrop = stats.nic_pkt_overflow + stats.nic_pkt_bad;
65 return 0;
66 }
67
68 static void
69 snf_platform_cleanup(pcap_t *p)
70 {
71 struct pcap_snf *ps = p->priv;
72
73 #ifdef SNF_HAVE_INJECT_API
74 if (ps->snf_inj)
75 snf_inject_close(ps->snf_inj);
76 #endif
77 snf_ring_close(ps->snf_ring);
78 snf_close(ps->snf_handle);
79 pcap_cleanup_live_common(p);
80 }
81
82 static int
83 snf_getnonblock(pcap_t *p)
84 {
85 struct pcap_snf *ps = p->priv;
86
87 return (ps->snf_timeout == 0);
88 }
89
90 static int
91 snf_setnonblock(pcap_t *p, int nonblock)
92 {
93 struct pcap_snf *ps = p->priv;
94
95 if (nonblock)
96 ps->snf_timeout = 0;
97 else {
98 if (p->opt.timeout <= 0)
99 ps->snf_timeout = -1; /* forever */
100 else
101 ps->snf_timeout = p->opt.timeout;
102 }
103 return (0);
104 }
105
106 #define _NSEC_PER_SEC 1000000000
107
108 static inline
109 struct timeval
110 snf_timestamp_to_timeval(const int64_t ts_nanosec, const int tstamp_precision)
111 {
112 struct timeval tv;
113 long tv_nsec;
114 const static struct timeval zero_timeval;
115
116 if (ts_nanosec == 0)
117 return zero_timeval;
118
119 tv.tv_sec = ts_nanosec / _NSEC_PER_SEC;
120 tv_nsec = (ts_nanosec % _NSEC_PER_SEC);
121
122 /* libpcap expects tv_usec to be nanos if using nanosecond precision. */
123 if (tstamp_precision == PCAP_TSTAMP_PRECISION_NANO)
124 tv.tv_usec = tv_nsec;
125 else
126 tv.tv_usec = tv_nsec / 1000;
127
128 return tv;
129 }
130
131 static int
132 snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
133 {
134 struct pcap_snf *ps = p->priv;
135 struct pcap_pkthdr hdr;
136 int i, flags, err, caplen, n;
137 struct snf_recv_req req;
138 int nonblock, timeout;
139
140 if (!p)
141 return -1;
142
143 n = 0;
144 timeout = ps->snf_timeout;
145 while (n < cnt || PACKET_COUNT_IS_UNLIMITED(cnt)) {
146 /*
147 * Has "pcap_breakloop()" been called?
148 */
149 if (p->break_loop) {
150 if (n == 0) {
151 p->break_loop = 0;
152 return (-2);
153 } else {
154 return (n);
155 }
156 }
157
158 err = snf_ring_recv(ps->snf_ring, timeout, &req);
159
160 if (err) {
161 if (err == EBUSY || err == EAGAIN) {
162 return (n);
163 }
164 else if (err == EINTR) {
165 timeout = 0;
166 continue;
167 }
168 else {
169 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_read: %s",
170 pcap_strerror(err));
171 return -1;
172 }
173 }
174
175 caplen = req.length;
176 if (caplen > p->snapshot)
177 caplen = p->snapshot;
178
179 if ((p->fcode.bf_insns == NULL) ||
180 bpf_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) {
181 hdr.ts = snf_timestamp_to_timeval(req.timestamp, p->opt.tstamp_precision);
182 hdr.caplen = caplen;
183 hdr.len = req.length;
184 callback(user, &hdr, req.pkt_addr);
185 }
186 n++;
187
188 /* After one successful packet is received, we won't block
189 * again for that timeout. */
190 if (timeout != 0)
191 timeout = 0;
192 }
193 return (n);
194 }
195
196 static int
197 snf_setfilter(pcap_t *p, struct bpf_program *fp)
198 {
199 if (!p)
200 return -1;
201 if (!fp) {
202 strncpy(p->errbuf, "setfilter: No filter specified",
203 sizeof(p->errbuf));
204 return -1;
205 }
206
207 /* Make our private copy of the filter */
208
209 if (install_bpf_program(p, fp) < 0)
210 return -1;
211
212 return (0);
213 }
214
215 static int
216 snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
217 {
218 #ifdef SNF_HAVE_INJECT_API
219 struct pcap_snf *ps = p->priv;
220 int rc;
221 if (ps->snf_inj == NULL) {
222 rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj);
223 if (rc) {
224 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
225 "snf_inject_open: %s", pcap_strerror(rc));
226 return (-1);
227 }
228 }
229
230 rc = snf_inject_send(ps->snf_inj, -1, 0, buf, size);
231 if (!rc) {
232 return (size);
233 }
234 else {
235 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_inject_send: %s",
236 pcap_strerror(rc));
237 return (-1);
238 }
239 #else
240 strlcpy(p->errbuf, "Sending packets isn't supported with this snf version",
241 PCAP_ERRBUF_SIZE);
242 return (-1);
243 #endif
244 }
245
246 static int
247 snf_activate(pcap_t* p)
248 {
249 struct pcap_snf *ps = p->priv;
250 char *device = p->opt.device;
251 const char *nr = NULL;
252 int err;
253 int flags = -1, ring_id = -1;
254
255 if (device == NULL) {
256 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
257 "device is NULL: %s", pcap_strerror(errno));
258 return -1;
259 }
260
261 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
262 * Since libpcap isn't thread-safe */
263 if ((nr = getenv("SNF_FLAGS")) && *nr)
264 flags = strtol(nr, NULL, 0);
265 else if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1)
266 flags = SNF_F_PSHARED;
267 else
268 nr = NULL;
269
270
271 /* Allow pcap_set_buffer_size() to set dataring_size.
272 * Default is zero which allows setting from env SNF_DATARING_SIZE.
273 * pcap_set_buffer_size() is in bytes while snf_open() accepts values
274 * between 0 and 1048576 in Megabytes. Values in this range are
275 * mapped to 1MB.
276 */
277 err = snf_open(ps->snf_boardnum,
278 0, /* let SNF API parse SNF_NUM_RINGS, if set */
279 NULL, /* default RSS, or use SNF_RSS_FLAGS env */
280 (p->opt.buffer_size > 0 && p->opt.buffer_size < 1048576) ? 1048576 : p->opt.buffer_size, /* default to SNF_DATARING_SIZE from env */
281 flags, /* may want pshared */
282 &ps->snf_handle);
283 if (err != 0) {
284 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
285 "snf_open failed: %s", pcap_strerror(err));
286 return -1;
287 }
288
289 if ((nr = getenv("SNF_PCAP_RING_ID")) && *nr) {
290 ring_id = (int) strtol(nr, NULL, 0);
291 }
292 err = snf_ring_open_id(ps->snf_handle, ring_id, &ps->snf_ring);
293 if (err != 0) {
294 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
295 "snf_ring_open_id(ring=%d) failed: %s",
296 ring_id, pcap_strerror(err));
297 return -1;
298 }
299
300 /*
301 * Turn a negative snapshot value (invalid), a snapshot value of
302 * 0 (unspecified), or a value bigger than the normal maximum
303 * value, into the maximum allowed value.
304 *
305 * If some application really *needs* a bigger snapshot
306 * length, we should just increase MAXIMUM_SNAPLEN.
307 */
308 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
309 p->snapshot = MAXIMUM_SNAPLEN;
310
311 if (p->opt.timeout <= 0)
312 ps->snf_timeout = -1;
313 else
314 ps->snf_timeout = p->opt.timeout;
315
316 err = snf_start(ps->snf_handle);
317 if (err != 0) {
318 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
319 "snf_start failed: %s", pcap_strerror(err));
320 return -1;
321 }
322
323 /*
324 * "select()" and "poll()" don't work on snf descriptors.
325 */
326 #ifndef _WIN32
327 p->selectable_fd = -1;
328 #endif /* !_WIN32 */
329 p->linktype = DLT_EN10MB;
330 p->read_op = snf_read;
331 p->inject_op = snf_inject;
332 p->setfilter_op = snf_setfilter;
333 p->setdirection_op = NULL; /* Not implemented.*/
334 p->set_datalink_op = snf_set_datalink;
335 p->getnonblock_op = snf_getnonblock;
336 p->setnonblock_op = snf_setnonblock;
337 p->stats_op = snf_pcap_stats;
338 p->cleanup_op = snf_platform_cleanup;
339 #ifdef SNF_HAVE_INJECT_API
340 ps->snf_inj = NULL;
341 #endif
342 return 0;
343 }
344
345 #define MAX_DESC_LENGTH 128
346 int
347 snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf)
348 {
349 pcap_if_t *dev;
350 #ifdef _WIN32
351 struct sockaddr_in addr;
352 #endif
353 struct snf_ifaddrs *ifaddrs, *ifa;
354 char name[MAX_DESC_LENGTH];
355 char desc[MAX_DESC_LENGTH];
356 int ret, allports = 0, merge = 0;
357 const char *nr = NULL;
358
359 if (snf_init(SNF_VERSION_API)) {
360 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
361 "snf_getifaddrs: snf_init failed");
362 return (-1);
363 }
364
365 if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL)
366 {
367 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
368 "snf_getifaddrs: %s", pcap_strerror(errno));
369 return (-1);
370 }
371 if ((nr = getenv("SNF_FLAGS")) && *nr) {
372 errno = 0;
373 merge = strtol(nr, NULL, 0);
374 if (errno) {
375 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
376 "snf_getifaddrs: SNF_FLAGS is not a valid number");
377 return (-1);
378 }
379 merge = merge & SNF_F_AGGREGATE_PORTMASK;
380 }
381
382 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->snf_ifa_next) {
383 /*
384 * Myricom SNF adapter ports may appear as regular
385 * network interfaces, which would already have been
386 * added to the list of adapters by pcap_platform_finddevs()
387 * if this isn't an SNF-only version of libpcap.
388 *
389 * Our create routine intercepts pcap_create() calls for
390 * those interfaces and arranges that they will be
391 * opened using the SNF API instead.
392 *
393 * So if we already have an entry for the device, we
394 * don't add an additional entry for it, we just
395 * update the description for it, if any, to indicate
396 * which snfN device it is. Otherwise, we add an entry
397 * for it.
398 *
399 * In either case, if SNF_F_AGGREGATE_PORTMASK is set
400 * in SNF_FLAGS, we add this port to the bitmask
401 * of ports, which we use to generate a device
402 * we can use to capture on all ports.
403 *
404 * Generate the description string. If port aggregation
405 * is set, use 2^{port number} as the unit number,
406 * rather than {port number}.
407 *
408 * XXX - do entries in this list have IP addresses for
409 * the port? If so, should we add them to the
410 * entry for the device, if they're not already in the
411 * list of IP addresses for the device?
412 */
413 (void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom %ssnf%d",
414 merge ? "Merge Bitmask Port " : "",
415 merge ? 1 << ifa->snf_ifa_portnum : ifa->snf_ifa_portnum);
416 /*
417 * Add the port to the bitmask.
418 */
419 if (merge)
420 allports |= 1 << ifa->snf_ifa_portnum;
421 /*
422 * See if there's already an entry for the device
423 * with the name ifa->snf_ifa_name.
424 */
425 dev = find_dev(devlistp, ifa->snf_ifa_name);
426 if (dev != NULL) {
427 /*
428 * Yes. Update its description.
429 */
430 char *desc_str;
431
432 desc_str = strdup(desc);
433 if (desc_str == NULL) {
434 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
435 "snf_findalldevs strdup: %s", pcap_strerror(errno));
436 return -1;
437 }
438 free(dev->description);
439 dev->description = desc_str;
440 } else {
441 /*
442 * No. Add an entry for it.
443 */
444 dev = add_dev(devlistp, ifa->snf_ifa_name, 0, desc,
445 errbuf);
446 if (dev == NULL)
447 return -1;
448 #ifdef _WIN32
449 /*
450 * On Windows, fill in IP# from device name
451 */
452 ret = inet_pton(AF_INET, dev->name, &addr.sin_addr);
453 if (ret == 1) {
454 /*
455 * Successful conversion of device name
456 * to IPv4 address.
457 */
458 addr.sin_family = AF_INET;
459 if (add_addr_to_dev(dev, &addr, sizeof(addr),
460 NULL, 0, NULL, 0, NULL, 0, errbuf) == -1)
461 return -1;
462 } else if (ret == -1) {
463 /*
464 * Error.
465 */
466 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,"sinf_findalldevs inet_pton: %s", pcap_strerror(errno));
467 return -1;
468 }
469 #endif _WIN32
470 }
471 }
472 snf_freeifaddrs(ifaddrs);
473 /*
474 * Create a snfX entry if port aggregation is enabled
475 */
476 if (merge) {
477 /*
478 * Add a new entry with all ports bitmask
479 */
480 (void)pcap_snprintf(name,MAX_DESC_LENGTH,"snf%d",allports);
481 (void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d",
482 allports);
483 if (add_dev(devlistp, name, 0, desc, errbuf) == NULL)
484 return (-1);
485 /*
486 * XXX - should we give it a list of addresses with all
487 * the addresses for all the ports?
488 */
489 }
490
491 return 0;
492 }
493
494 pcap_t *
495 snf_create(const char *device, char *ebuf, int *is_ours)
496 {
497 pcap_t *p;
498 int boardnum = -1;
499 struct snf_ifaddrs *ifaddrs, *ifa;
500 size_t devlen;
501 struct pcap_snf *ps;
502
503 if (snf_init(SNF_VERSION_API)) {
504 /* Can't initialize the API, so no SNF devices */
505 *is_ours = 0;
506 return NULL;
507 }
508
509 /*
510 * Match a given interface name to our list of interface names, from
511 * which we can obtain the intended board number
512 */
513 if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) {
514 /* Can't get SNF addresses */
515 *is_ours = 0;
516 return NULL;
517 }
518 devlen = strlen(device) + 1;
519 ifa = ifaddrs;
520 while (ifa) {
521 if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) {
522 boardnum = ifa->snf_ifa_boardnum;
523 break;
524 }
525 ifa = ifa->snf_ifa_next;
526 }
527 snf_freeifaddrs(ifaddrs);
528
529 if (ifa == NULL) {
530 /*
531 * If we can't find the device by name, support the name "snfX"
532 * and "snf10gX" where X is the board number.
533 */
534 if (sscanf(device, "snf10g%d", &boardnum) != 1 &&
535 sscanf(device, "snf%d", &boardnum) != 1) {
536 /* Nope, not a supported name */
537 *is_ours = 0;
538 return NULL;
539 }
540 }
541
542 /* OK, it's probably ours. */
543 *is_ours = 1;
544
545 p = pcap_create_common(ebuf, sizeof (struct pcap_snf));
546 if (p == NULL)
547 return NULL;
548 ps = p->priv;
549
550 /*
551 * We support microsecond and nanosecond time stamps.
552 */
553 p->tstamp_precision_count = 2;
554 p->tstamp_precision_list = malloc(2 * sizeof(u_int));
555 if (p->tstamp_precision_list == NULL) {
556 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
557 pcap_strerror(errno));
558 pcap_close(p);
559 return NULL;
560 }
561 p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
562 p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
563
564 p->activate_op = snf_activate;
565 ps->snf_boardnum = boardnum;
566 return p;
567 }
568
569 #ifdef SNF_ONLY
570 /*
571 * This libpcap build supports only SNF cards, not regular network
572 * interfaces..
573 */
574
575 /*
576 * There are no regular interfaces, just SNF interfaces.
577 */
578 int
579 pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
580 {
581 return (0);
582 }
583
584 /*
585 * Attempts to open a regular interface fail.
586 */
587 pcap_t *
588 pcap_create_interface(const char *device, char *errbuf)
589 {
590 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
591 "This version of libpcap only supports SNF cards");
592 return NULL;
593 }
594
595 /*
596 * Libpcap version string.
597 */
598 const char *
599 pcap_lib_version(void)
600 {
601 return (PCAP_VERSION_STRING " (SNF-only)");
602 }
603 #endif