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