]> The Tcpdump Group git mirrors - libpcap/blob - pcap-snf.c
Prefix routines declared in pcap-int.h with pcap_.
[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 #include <limits.h> /* for INT_MAX */
13
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_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
59 rc, "snf_get_stats");
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 /*
141 * This can conceivably process more than INT_MAX packets,
142 * which would overflow the packet count, causing it either
143 * to look like a negative number, and thus cause us to
144 * return a value that looks like an error, or overflow
145 * back into positive territory, and thus cause us to
146 * return a too-low count.
147 *
148 * Therefore, if the packet count is unlimited, we clip
149 * it at INT_MAX; this routine is not expected to
150 * process packets indefinitely, so that's not an issue.
151 */
152 if (PACKET_COUNT_IS_UNLIMITED(cnt))
153 cnt = INT_MAX;
154
155 n = 0;
156 timeout = ps->snf_timeout;
157 while (n < cnt) {
158 /*
159 * Has "pcap_breakloop()" been called?
160 */
161 if (p->break_loop) {
162 if (n == 0) {
163 p->break_loop = 0;
164 return (-2);
165 } else {
166 return (n);
167 }
168 }
169
170 err = snf_ring_recv(ps->snf_ring, timeout, &req);
171
172 if (err) {
173 if (err == EBUSY || err == EAGAIN) {
174 return (n);
175 }
176 else if (err == EINTR) {
177 timeout = 0;
178 continue;
179 }
180 else {
181 pcap_fmt_errmsg_for_errno(p->errbuf,
182 PCAP_ERRBUF_SIZE, err, "snf_read");
183 return -1;
184 }
185 }
186
187 caplen = req.length;
188 if (caplen > p->snapshot)
189 caplen = p->snapshot;
190
191 if ((p->fcode.bf_insns == NULL) ||
192 pcap_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) {
193 hdr.ts = snf_timestamp_to_timeval(req.timestamp, p->opt.tstamp_precision);
194 hdr.caplen = caplen;
195 hdr.len = req.length;
196 callback(user, &hdr, req.pkt_addr);
197 n++;
198 }
199
200 /* After one successful packet is received, we won't block
201 * again for that timeout. */
202 if (timeout != 0)
203 timeout = 0;
204 }
205 return (n);
206 }
207
208 static int
209 snf_inject(pcap_t *p, const void *buf _U_, int size _U_)
210 {
211 #ifdef SNF_HAVE_INJECT_API
212 struct pcap_snf *ps = p->priv;
213 int rc;
214 if (ps->snf_inj == NULL) {
215 rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj);
216 if (rc) {
217 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
218 rc, "snf_inject_open");
219 return (-1);
220 }
221 }
222
223 rc = snf_inject_send(ps->snf_inj, -1, 0, buf, size);
224 if (!rc) {
225 return (size);
226 }
227 else {
228 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
229 rc, "snf_inject_send");
230 return (-1);
231 }
232 #else
233 pcap_strlcpy(p->errbuf, "Sending packets isn't supported with this snf version",
234 PCAP_ERRBUF_SIZE);
235 return (-1);
236 #endif
237 }
238
239 static int
240 snf_activate(pcap_t* p)
241 {
242 struct pcap_snf *ps = p->priv;
243 char *device = p->opt.device;
244 const char *nr = NULL;
245 int err;
246 int flags = -1, ring_id = -1;
247
248 if (device == NULL) {
249 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "device is NULL");
250 return -1;
251 }
252
253 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
254 * Since libpcap isn't thread-safe */
255 if ((nr = getenv("SNF_FLAGS")) && *nr)
256 flags = strtol(nr, NULL, 0);
257 else if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1)
258 flags = SNF_F_PSHARED;
259 else
260 nr = NULL;
261
262
263 /* Allow pcap_set_buffer_size() to set dataring_size.
264 * Default is zero which allows setting from env SNF_DATARING_SIZE.
265 * pcap_set_buffer_size() is in bytes while snf_open() accepts values
266 * between 0 and 1048576 in Megabytes. Values in this range are
267 * mapped to 1MB.
268 */
269 err = snf_open(ps->snf_boardnum,
270 0, /* let SNF API parse SNF_NUM_RINGS, if set */
271 NULL, /* default RSS, or use SNF_RSS_FLAGS env */
272 (p->opt.buffer_size > 0 && p->opt.buffer_size < 1048576) ? 1048576 : p->opt.buffer_size, /* default to SNF_DATARING_SIZE from env */
273 flags, /* may want pshared */
274 &ps->snf_handle);
275 if (err != 0) {
276 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
277 err, "snf_open failed");
278 return -1;
279 }
280
281 if ((nr = getenv("SNF_PCAP_RING_ID")) && *nr) {
282 ring_id = (int) strtol(nr, NULL, 0);
283 }
284 err = snf_ring_open_id(ps->snf_handle, ring_id, &ps->snf_ring);
285 if (err != 0) {
286 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
287 err, "snf_ring_open_id(ring=%d) failed", ring_id);
288 return -1;
289 }
290
291 /*
292 * Turn a negative snapshot value (invalid), a snapshot value of
293 * 0 (unspecified), or a value bigger than the normal maximum
294 * value, into the maximum allowed value.
295 *
296 * If some application really *needs* a bigger snapshot
297 * length, we should just increase MAXIMUM_SNAPLEN.
298 */
299 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
300 p->snapshot = MAXIMUM_SNAPLEN;
301
302 if (p->opt.timeout <= 0)
303 ps->snf_timeout = -1;
304 else
305 ps->snf_timeout = p->opt.timeout;
306
307 err = snf_start(ps->snf_handle);
308 if (err != 0) {
309 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
310 err, "snf_start failed");
311 return -1;
312 }
313
314 /*
315 * "select()" and "poll()" don't work on snf descriptors.
316 */
317 #ifndef _WIN32
318 p->selectable_fd = -1;
319 #endif /* !_WIN32 */
320 p->linktype = DLT_EN10MB;
321 p->read_op = snf_read;
322 p->inject_op = snf_inject;
323 p->setfilter_op = pcap_install_bpf_program;
324 p->setdirection_op = NULL; /* Not implemented.*/
325 p->set_datalink_op = snf_set_datalink;
326 p->getnonblock_op = snf_getnonblock;
327 p->setnonblock_op = snf_setnonblock;
328 p->stats_op = snf_pcap_stats;
329 p->cleanup_op = snf_platform_cleanup;
330 #ifdef SNF_HAVE_INJECT_API
331 ps->snf_inj = NULL;
332 #endif
333 return 0;
334 }
335
336 #define MAX_DESC_LENGTH 128
337 int
338 snf_findalldevs(pcap_if_list_t *devlistp, char *errbuf)
339 {
340 pcap_if_t *dev;
341 #ifdef _WIN32
342 struct sockaddr_in addr;
343 #endif
344 struct snf_ifaddrs *ifaddrs, *ifa;
345 char name[MAX_DESC_LENGTH];
346 char desc[MAX_DESC_LENGTH];
347 int ret, allports = 0, merge = 0;
348 const char *nr = NULL;
349
350 if (snf_init(SNF_VERSION_API)) {
351 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
352 "snf_getifaddrs: snf_init failed");
353 return (-1);
354 }
355
356 if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL)
357 {
358 pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
359 errno, "snf_getifaddrs");
360 return (-1);
361 }
362 if ((nr = getenv("SNF_FLAGS")) && *nr) {
363 errno = 0;
364 merge = strtol(nr, NULL, 0);
365 if (errno) {
366 (void)snprintf(errbuf, PCAP_ERRBUF_SIZE,
367 "snf_getifaddrs: SNF_FLAGS is not a valid number");
368 return (-1);
369 }
370 merge = merge & SNF_F_AGGREGATE_PORTMASK;
371 }
372
373 for (ifa = ifaddrs; ifa != NULL; ifa = ifa->snf_ifa_next) {
374 /*
375 * Myricom SNF adapter ports may appear as regular
376 * network interfaces, which would already have been
377 * added to the list of adapters by pcap_platform_finddevs()
378 * if this isn't an SNF-only version of libpcap.
379 *
380 * Our create routine intercepts pcap_create() calls for
381 * those interfaces and arranges that they will be
382 * opened using the SNF API instead.
383 *
384 * So if we already have an entry for the device, we
385 * don't add an additional entry for it, we just
386 * update the description for it, if any, to indicate
387 * which snfN device it is. Otherwise, we add an entry
388 * for it.
389 *
390 * In either case, if SNF_F_AGGREGATE_PORTMASK is set
391 * in SNF_FLAGS, we add this port to the bitmask
392 * of ports, which we use to generate a device
393 * we can use to capture on all ports.
394 *
395 * Generate the description string. If port aggregation
396 * is set, use 2^{port number} as the unit number,
397 * rather than {port number}.
398 *
399 * XXX - do entries in this list have IP addresses for
400 * the port? If so, should we add them to the
401 * entry for the device, if they're not already in the
402 * list of IP addresses for the device?
403 */
404 (void)snprintf(desc,MAX_DESC_LENGTH,"Myricom %ssnf%d",
405 merge ? "Merge Bitmask Port " : "",
406 merge ? 1 << ifa->snf_ifa_portnum : ifa->snf_ifa_portnum);
407 /*
408 * Add the port to the bitmask.
409 */
410 if (merge)
411 allports |= 1 << ifa->snf_ifa_portnum;
412 /*
413 * See if there's already an entry for the device
414 * with the name ifa->snf_ifa_name.
415 */
416 dev = pcap_find_dev(devlistp, ifa->snf_ifa_name);
417 if (dev != NULL) {
418 /*
419 * Yes. Update its description.
420 */
421 char *desc_str;
422
423 desc_str = strdup(desc);
424 if (desc_str == NULL) {
425 pcap_fmt_errmsg_for_errno(errbuf,
426 PCAP_ERRBUF_SIZE, errno,
427 "snf_findalldevs strdup");
428 return -1;
429 }
430 free(dev->description);
431 dev->description = desc_str;
432 } else {
433 /*
434 * No. Add an entry for it.
435 *
436 * XXX - is there a notion of "up" or "running",
437 * and can we determine whether something's
438 * plugged into the adapter and set
439 * PCAP_IF_CONNECTION_STATUS_CONNECTED or
440 * PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
441 */
442 dev = pcap_add_dev(devlistp, ifa->snf_ifa_name, 0, desc,
443 errbuf);
444 if (dev == NULL)
445 return -1;
446 #ifdef _WIN32
447 /*
448 * On Windows, fill in IP# from device name
449 */
450 ret = inet_pton(AF_INET, dev->name, &addr.sin_addr);
451 if (ret == 1) {
452 /*
453 * Successful conversion of device name
454 * to IPv4 address.
455 */
456 addr.sin_family = AF_INET;
457 if (pcap_add_addr_to_dev(dev, &addr, sizeof(addr),
458 NULL, 0, NULL, 0, NULL, 0, errbuf) == -1)
459 return -1;
460 } else if (ret == -1) {
461 /*
462 * Error.
463 */
464 pcap_fmt_errmsg_for_errno(errbuf,
465 PCAP_ERRBUF_SIZE, errno,
466 "sinf_findalldevs inet_pton");
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)snprintf(name,MAX_DESC_LENGTH,"snf%d",allports);
481 (void)snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d",
482 allports);
483 /*
484 * XXX - is there any notion of "up" and "running" that
485 * would apply to this device, given that it handles
486 * multiple ports?
487 *
488 * Presumably, there's no notion of "connected" vs.
489 * "disconnected", as "is this plugged into a network?"
490 * would be a per-port property.
491 */
492 if (pcap_add_dev(devlistp, name,
493 PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE, desc,
494 errbuf) == NULL)
495 return (-1);
496 /*
497 * XXX - should we give it a list of addresses with all
498 * the addresses for all the ports?
499 */
500 }
501
502 return 0;
503 }
504
505 pcap_t *
506 snf_create(const char *device, char *ebuf, int *is_ours)
507 {
508 pcap_t *p;
509 int boardnum = -1;
510 struct snf_ifaddrs *ifaddrs, *ifa;
511 size_t devlen;
512 struct pcap_snf *ps;
513
514 if (snf_init(SNF_VERSION_API)) {
515 /* Can't initialize the API, so no SNF devices */
516 *is_ours = 0;
517 return NULL;
518 }
519
520 /*
521 * Match a given interface name to our list of interface names, from
522 * which we can obtain the intended board number
523 */
524 if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) {
525 /* Can't get SNF addresses */
526 *is_ours = 0;
527 return NULL;
528 }
529 devlen = strlen(device) + 1;
530 ifa = ifaddrs;
531 while (ifa) {
532 if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) {
533 boardnum = ifa->snf_ifa_boardnum;
534 break;
535 }
536 ifa = ifa->snf_ifa_next;
537 }
538 snf_freeifaddrs(ifaddrs);
539
540 if (ifa == NULL) {
541 /*
542 * If we can't find the device by name, support the name "snfX"
543 * and "snf10gX" where X is the board number.
544 */
545 if (sscanf(device, "snf10g%d", &boardnum) != 1 &&
546 sscanf(device, "snf%d", &boardnum) != 1) {
547 /* Nope, not a supported name */
548 *is_ours = 0;
549 return NULL;
550 }
551 }
552
553 /* OK, it's probably ours. */
554 *is_ours = 1;
555
556 p = PCAP_CREATE_COMMON(ebuf, struct pcap_snf);
557 if (p == NULL)
558 return NULL;
559 ps = p->priv;
560
561 /*
562 * We support microsecond and nanosecond time stamps.
563 */
564 p->tstamp_precision_list = malloc(2 * sizeof(u_int));
565 if (p->tstamp_precision_list == NULL) {
566 pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
567 "malloc");
568 pcap_close(p);
569 return NULL;
570 }
571 p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
572 p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
573 p->tstamp_precision_count = 2;
574
575 p->activate_op = snf_activate;
576 ps->snf_boardnum = boardnum;
577 return p;
578 }
579
580 #ifdef SNF_ONLY
581 /*
582 * This libpcap build supports only SNF cards, not regular network
583 * interfaces..
584 */
585
586 /*
587 * There are no regular interfaces, just SNF interfaces.
588 */
589 int
590 pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
591 {
592 return (0);
593 }
594
595 /*
596 * Attempts to open a regular interface fail.
597 */
598 pcap_t *
599 pcap_create_interface(const char *device, char *errbuf)
600 {
601 snprintf(errbuf, PCAP_ERRBUF_SIZE,
602 "This version of libpcap only supports SNF cards");
603 return NULL;
604 }
605
606 /*
607 * Libpcap version string.
608 */
609 const char *
610 pcap_lib_version(void)
611 {
612 return (PCAP_VERSION_STRING " (SNF-only)");
613 }
614 #endif