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