]> The Tcpdump Group git mirrors - libpcap/blob - pcap-snf.c
Allow a platform to add information to the version string.
[libpcap] / pcap-snf.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #ifdef _WIN32
6 #include <pcap-stdinc.h>
7 #else /* !_WIN32 */
8 #include <sys/param.h>
9 #endif /* !_WIN32 */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14
15 #include <ctype.h>
16 #ifndef _WIN32
17 #include <netinet/in.h>
18 #include <sys/mman.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #endif /* !_WIN32 */
23
24 #include <snf.h>
25 #if SNF_VERSION_API >= 0x0003
26 #define SNF_HAVE_INJECT_API
27 #endif
28
29 #include "pcap-int.h"
30 #include "pcap-snf.h"
31
32 /*
33 * Private data for capturing on SNF devices.
34 */
35 struct pcap_snf {
36 snf_handle_t snf_handle; /* opaque device handle */
37 snf_ring_t snf_ring; /* opaque device ring handle */
38 #ifdef SNF_HAVE_INJECT_API
39 snf_inject_t snf_inj; /* inject handle, if inject is used */
40 #endif
41 int snf_timeout;
42 int snf_boardnum;
43 };
44
45 static int
46 snf_set_datalink(pcap_t *p, int dlt)
47 {
48 p->linktype = dlt;
49 return (0);
50 }
51
52 static int
53 snf_pcap_stats(pcap_t *p, struct pcap_stat *ps)
54 {
55 struct snf_ring_stats stats;
56 struct pcap_snf *snfps = p->priv;
57 int rc;
58
59 if ((rc = snf_ring_getstats(snfps->snf_ring, &stats))) {
60 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_get_stats: %s",
61 pcap_strerror(rc));
62 return -1;
63 }
64 ps->ps_recv = stats.ring_pkt_recv + stats.ring_pkt_overflow;
65 ps->ps_drop = stats.ring_pkt_overflow;
66 ps->ps_ifdrop = stats.nic_pkt_overflow + stats.nic_pkt_bad;
67 return 0;
68 }
69
70 static void
71 snf_platform_cleanup(pcap_t *p)
72 {
73 struct pcap_snf *ps = p->priv;
74
75 #ifdef SNF_HAVE_INJECT_API
76 if (ps->snf_inj)
77 snf_inject_close(ps->snf_inj);
78 #endif
79 snf_ring_close(ps->snf_ring);
80 snf_close(ps->snf_handle);
81 pcap_cleanup_live_common(p);
82 }
83
84 static int
85 snf_getnonblock(pcap_t *p)
86 {
87 struct pcap_snf *ps = p->priv;
88
89 return (ps->snf_timeout == 0);
90 }
91
92 static int
93 snf_setnonblock(pcap_t *p, int nonblock)
94 {
95 struct pcap_snf *ps = p->priv;
96
97 if (nonblock)
98 ps->snf_timeout = 0;
99 else {
100 if (p->opt.timeout <= 0)
101 ps->snf_timeout = -1; /* forever */
102 else
103 ps->snf_timeout = p->opt.timeout;
104 }
105 return (0);
106 }
107
108 #define _NSEC_PER_SEC 1000000000
109
110 static inline
111 struct timeval
112 snf_timestamp_to_timeval(const int64_t ts_nanosec, const int tstamp_precision)
113 {
114 struct timeval tv;
115 long tv_nsec;
116 const static struct timeval zero_timeval;
117
118 if (ts_nanosec == 0)
119 return zero_timeval;
120
121 tv.tv_sec = ts_nanosec / _NSEC_PER_SEC;
122 tv_nsec = (ts_nanosec % _NSEC_PER_SEC);
123
124 /* libpcap expects tv_usec to be nanos if using nanosecond precision. */
125 if (tstamp_precision == PCAP_TSTAMP_PRECISION_NANO)
126 tv.tv_usec = tv_nsec;
127 else
128 tv.tv_usec = tv_nsec / 1000;
129
130 return tv;
131 }
132
133 static int
134 snf_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
135 {
136 struct pcap_snf *ps = p->priv;
137 struct pcap_pkthdr hdr;
138 int i, flags, err, caplen, n;
139 struct snf_recv_req req;
140 int nonblock, timeout;
141
142 if (!p)
143 return -1;
144
145 n = 0;
146 timeout = ps->snf_timeout;
147 while (n < cnt || PACKET_COUNT_IS_UNLIMITED(cnt)) {
148 /*
149 * Has "pcap_breakloop()" been called?
150 */
151 if (p->break_loop) {
152 if (n == 0) {
153 p->break_loop = 0;
154 return (-2);
155 } else {
156 return (n);
157 }
158 }
159
160 err = snf_ring_recv(ps->snf_ring, timeout, &req);
161
162 if (err) {
163 if (err == EBUSY || err == EAGAIN) {
164 return (n);
165 }
166 else if (err == EINTR) {
167 timeout = 0;
168 continue;
169 }
170 else {
171 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_read: %s",
172 pcap_strerror(err));
173 return -1;
174 }
175 }
176
177 caplen = req.length;
178 if (caplen > p->snapshot)
179 caplen = p->snapshot;
180
181 if ((p->fcode.bf_insns == NULL) ||
182 bpf_filter(p->fcode.bf_insns, req.pkt_addr, req.length, caplen)) {
183 hdr.ts = snf_timestamp_to_timeval(req.timestamp, p->opt.tstamp_precision);
184 hdr.caplen = caplen;
185 hdr.len = req.length;
186 callback(user, &hdr, req.pkt_addr);
187 }
188 n++;
189
190 /* After one successful packet is received, we won't block
191 * again for that timeout. */
192 if (timeout != 0)
193 timeout = 0;
194 }
195 return (n);
196 }
197
198 static int
199 snf_setfilter(pcap_t *p, struct bpf_program *fp)
200 {
201 if (!p)
202 return -1;
203 if (!fp) {
204 strncpy(p->errbuf, "setfilter: No filter specified",
205 sizeof(p->errbuf));
206 return -1;
207 }
208
209 /* Make our private copy of the filter */
210
211 if (install_bpf_program(p, fp) < 0)
212 return -1;
213
214 return (0);
215 }
216
217 static int
218 snf_inject(pcap_t *p, const void *buf _U_, size_t size _U_)
219 {
220 #ifdef SNF_HAVE_INJECT_API
221 struct pcap_snf *ps = p->priv;
222 int rc;
223 if (ps->snf_inj == NULL) {
224 rc = snf_inject_open(ps->snf_boardnum, 0, &ps->snf_inj);
225 if (rc) {
226 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
227 "snf_inject_open: %s", pcap_strerror(rc));
228 return (-1);
229 }
230 }
231
232 rc = snf_inject_send(ps->snf_inj, -1, 0, buf, size);
233 if (!rc) {
234 return (size);
235 }
236 else {
237 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snf_inject_send: %s",
238 pcap_strerror(rc));
239 return (-1);
240 }
241 #else
242 strlcpy(p->errbuf, "Sending packets isn't supported with this snf version",
243 PCAP_ERRBUF_SIZE);
244 return (-1);
245 #endif
246 }
247
248 static int
249 snf_activate(pcap_t* p)
250 {
251 struct pcap_snf *ps = p->priv;
252 char *device = p->opt.device;
253 const char *nr = NULL;
254 int err;
255 int flags = -1, ring_id = -1;
256
257 if (device == NULL) {
258 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
259 "device is NULL: %s", pcap_strerror(errno));
260 return -1;
261 }
262
263 /* In Libpcap, we set pshared by default if NUM_RINGS is set to > 1.
264 * Since libpcap isn't thread-safe */
265 if ((nr = getenv("SNF_FLAGS")) && *nr)
266 flags = strtol(nr, NULL, 0);
267 else if ((nr = getenv("SNF_NUM_RINGS")) && *nr && atoi(nr) > 1)
268 flags = SNF_F_PSHARED;
269 else
270 nr = NULL;
271
272
273 /* Allow pcap_set_buffer_size() to set dataring_size.
274 * Default is zero which allows setting from env SNF_DATARING_SIZE.
275 * pcap_set_buffer_size() is in bytes while snf_open() accepts values
276 * between 0 and 1048576 in Megabytes. Values in this range are
277 * mapped to 1MB.
278 */
279 err = snf_open(ps->snf_boardnum,
280 0, /* let SNF API parse SNF_NUM_RINGS, if set */
281 NULL, /* default RSS, or use SNF_RSS_FLAGS env */
282 (p->opt.buffer_size > 0 && p->opt.buffer_size < 1048576) ? 1048576 : p->opt.buffer_size, /* default to SNF_DATARING_SIZE from env */
283 flags, /* may want pshared */
284 &ps->snf_handle);
285 if (err != 0) {
286 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
287 "snf_open failed: %s", pcap_strerror(err));
288 return -1;
289 }
290
291 if ((nr = getenv("SNF_PCAP_RING_ID")) && *nr) {
292 ring_id = (int) strtol(nr, NULL, 0);
293 }
294 err = snf_ring_open_id(ps->snf_handle, ring_id, &ps->snf_ring);
295 if (err != 0) {
296 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
297 "snf_ring_open_id(ring=%d) failed: %s",
298 ring_id, pcap_strerror(err));
299 return -1;
300 }
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_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
310 "snf_start failed: %s", pcap_strerror(err));
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 = snf_setfilter;
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)pcap_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 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
359 "snf_getifaddrs: %s", pcap_strerror(errno));
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)pcap_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)pcap_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 = 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 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
426 "snf_findalldevs strdup: %s", pcap_strerror(errno));
427 return -1;
428 }
429 free(dev->description);
430 dev->description = desc_str;
431 } else {
432 /*
433 * No. Add an entry for it.
434 */
435 dev = add_dev(devlistp, ifa->snf_ifa_name, 0, desc,
436 errbuf);
437 if (dev == NULL)
438 return -1;
439 #ifdef _WIN32
440 /*
441 * On Windows, fill in IP# from device name
442 */
443 ret = inet_pton(AF_INET, dev->name, &addr.sin_addr);
444 if (ret == 1) {
445 /*
446 * Successful conversion of device name
447 * to IPv4 address.
448 */
449 addr.sin_family = AF_INET;
450 if (add_addr_to_dev(dev, &addr, sizeof(addr),
451 NULL, 0, NULL, 0, NULL, 0, errbuf) == -1)
452 return -1;
453 } else if (ret == -1) {
454 /*
455 * Error.
456 */
457 (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,"sinf_findalldevs inet_pton: %s", pcap_strerror(errno));
458 return -1;
459 }
460 #endif _WIN32
461 }
462 }
463 snf_freeifaddrs(ifaddrs);
464 /*
465 * Create a snfX entry if port aggregation is enabled
466 */
467 if (merge) {
468 /*
469 * Add a new entry with all ports bitmask
470 */
471 (void)pcap_snprintf(name,MAX_DESC_LENGTH,"snf%d",allports);
472 (void)pcap_snprintf(desc,MAX_DESC_LENGTH,"Myricom Merge Bitmask All Ports snf%d",
473 allports);
474 if (add_dev(devlistp, name, 0, desc, errbuf) == NULL)
475 return (-1);
476 /*
477 * XXX - should we give it a list of addresses with all
478 * the addresses for all the ports?
479 */
480 }
481
482 return 0;
483 }
484
485 pcap_t *
486 snf_create(const char *device, char *ebuf, int *is_ours)
487 {
488 pcap_t *p;
489 int boardnum = -1;
490 struct snf_ifaddrs *ifaddrs, *ifa;
491 size_t devlen;
492 struct pcap_snf *ps;
493
494 if (snf_init(SNF_VERSION_API)) {
495 /* Can't initialize the API, so no SNF devices */
496 *is_ours = 0;
497 return NULL;
498 }
499
500 /*
501 * Match a given interface name to our list of interface names, from
502 * which we can obtain the intended board number
503 */
504 if (snf_getifaddrs(&ifaddrs) || ifaddrs == NULL) {
505 /* Can't get SNF addresses */
506 *is_ours = 0;
507 return NULL;
508 }
509 devlen = strlen(device) + 1;
510 ifa = ifaddrs;
511 while (ifa) {
512 if (strncmp(device, ifa->snf_ifa_name, devlen) == 0) {
513 boardnum = ifa->snf_ifa_boardnum;
514 break;
515 }
516 ifa = ifa->snf_ifa_next;
517 }
518 snf_freeifaddrs(ifaddrs);
519
520 if (ifa == NULL) {
521 /*
522 * If we can't find the device by name, support the name "snfX"
523 * and "snf10gX" where X is the board number.
524 */
525 if (sscanf(device, "snf10g%d", &boardnum) != 1 &&
526 sscanf(device, "snf%d", &boardnum) != 1) {
527 /* Nope, not a supported name */
528 *is_ours = 0;
529 return NULL;
530 }
531 }
532
533 /* OK, it's probably ours. */
534 *is_ours = 1;
535
536 p = pcap_create_common(ebuf, sizeof (struct pcap_snf));
537 if (p == NULL)
538 return NULL;
539 ps = p->priv;
540
541 /*
542 * We support microsecond and nanosecond time stamps.
543 */
544 p->tstamp_precision_count = 2;
545 p->tstamp_precision_list = malloc(2 * sizeof(u_int));
546 if (p->tstamp_precision_list == NULL) {
547 pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
548 pcap_strerror(errno));
549 pcap_close(p);
550 return NULL;
551 }
552 p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
553 p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
554
555 p->activate_op = snf_activate;
556 ps->snf_boardnum = boardnum;
557 return p;
558 }
559
560 #ifdef SNF_ONLY
561 /*
562 * This libpcap build supports only SNF cards, not regular network
563 * interfaces..
564 */
565
566 /*
567 * There are no regular interfaces, just SNF interfaces.
568 */
569 int
570 pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
571 {
572 return (0);
573 }
574
575 /*
576 * Attempts to open a regular interface fail.
577 */
578 pcap_t *
579 pcap_create_interface(const char *device, char *errbuf)
580 {
581 pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
582 "This version of libpcap only supports SNF cards");
583 return NULL;
584 }
585
586 /*
587 * Platform-specific information.
588 */
589 const char *
590 pcap_platform_lib_version(void)
591 {
592 return ("SNF-only");
593 }
594 #endif