]> The Tcpdump Group git mirrors - libpcap/blob - pcap-dag.c
CI: Call print_so_deps() on rpcapd in remote enabled build
[libpcap] / pcap-dag.c
1 /*
2 * pcap-dag.c: Packet capture interface for Endace DAG cards.
3 *
4 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
5 * Modifications: Jesper Peterson
6 * Koryn Grant
7 * Stephen Donnelly <stephen.donnelly@endace.com>
8 */
9
10 #include <config.h>
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <endian.h>
16 #include <limits.h>
17 #include <unistd.h>
18
19 #include "pcap-int.h"
20
21 #if __BYTE_ORDER == __BIG_ENDIAN
22 // Will need SWAPLL().
23 #include "pcap-util.h"
24 #endif
25
26 #include "dagapi.h"
27 #include "dagpci.h"
28 #include "dag_config_api.h"
29
30 #include "pcap-dag.h"
31
32 /*
33 * DAG devices have names beginning with "dag", followed by a number
34 * from 0 to DAG_MAX_BOARDS, then optionally a colon and a stream number
35 * from 0 to DAG_STREAM_MAX.
36 */
37 #ifndef DAG_MAX_BOARDS
38 #define DAG_MAX_BOARDS 32
39 #endif
40
41 #define ATM_CELL_SIZE 52
42 #define ATM_HDR_SIZE 4
43
44 /*
45 * A header containing additional MTP information.
46 */
47 #define MTP2_SENT_OFFSET 0 /* 1 byte */
48 #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
49 #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
50 #define MTP2_HDR_LEN 4 /* length of the header */
51
52 #define MTP2_ANNEX_A_NOT_USED 0
53 #define MTP2_ANNEX_A_USED 1
54 #define MTP2_ANNEX_A_USED_UNKNOWN 2
55
56 /* SunATM pseudo header */
57 struct sunatm_hdr {
58 unsigned char flags; /* destination and traffic type */
59 unsigned char vpi; /* VPI */
60 unsigned short vci; /* VCI */
61 };
62
63 /*
64 * Private data for capturing on DAG devices.
65 */
66 struct pcap_dag {
67 struct pcap_stat stat;
68 u_char *dag_mem_bottom; /* DAG card current memory bottom pointer */
69 u_char *dag_mem_top; /* DAG card current memory top pointer */
70 int dag_fcs_bits; /* Number of checksum bits from link layer */
71 int dag_flags; /* Flags */
72 int dag_devnum; /* This is the N in "dagN" or "dagN:M". */
73 int dag_stream; /* And this is the M. */
74 int dag_timeout; /* timeout specified to pcap_open_live.
75 * Same as in linux above, introduce
76 * generally? */
77 dag_card_ref_t dag_ref; /* DAG Configuration/Status API card reference */
78 dag_component_t dag_root; /* DAG CSAPI Root component */
79 attr_uuid_t drop_attr; /* DAG Stream Drop Attribute handle, if available */
80 uint64_t drop_base; // Rx stream drop counter initial value.
81 struct timeval required_select_timeout;
82 /* Timeout caller must use in event loops */
83 uint8_t tx_iface; // Tx interface number
84 uint8_t tx_align_bytes; /* If necessary, add trailing padding to an
85 * ERF record to make it a multiple of this
86 * many bytes long. DAG API calls this "ERF
87 * record alignment". */
88 uint8_t terf_fcs_bytes; // How many FCS bytes TERF is expecting.
89 };
90
91 #define ALIGN_BYTES_DEFAULT 8
92 #define ALIGN_BYTES_9_2 16
93 #define ALIGN_BYTES_MAX ALIGN_BYTES_9_2
94
95 typedef struct pcap_dag_node {
96 struct pcap_dag_node *next;
97 pcap_t *p;
98 pid_t pid;
99 } pcap_dag_node_t;
100
101 static pcap_dag_node_t *pcap_dags = NULL;
102 static int atexit_handler_installed = 0;
103
104 #define MAX_DAG_PACKET 65536
105
106 static unsigned char TempPkt[MAX_DAG_PACKET];
107
108 #define TX_ONLY(stream) ((stream) % 2)
109 #define RX_ONLY(stream) (! TX_ONLY(stream))
110 #define RXTX_STR(stream) (TX_ONLY(stream) ? "Tx" : "Rx")
111
112 static int dag_stats(pcap_t *p, struct pcap_stat *ps);
113 static int dag_set_datalink(pcap_t *p, int dlt);
114 static int dag_get_datalink(pcap_t *p);
115 static int dag_setnonblock(pcap_t *p, int nonblock);
116
117 // Environment variables that can control behaviour of this libpcap module.
118 #define ENV_RX_FCS_BITS "ERF_FCS_BITS"
119 #define ENV_RX_FCS_NOSTRIP "ERF_DONT_STRIP_FCS"
120 #define ENV_TX_IFACE "ERF_TX_INTERFACE"
121
122 /*
123 * Convert the return value of getenv() to an integer using matching stricter
124 * than atoi(). If the environment variable is not set, return the default
125 * value. Otherwise return an integer in the interval [0, INT32_MAX] or -1 on
126 * error.
127 */
128 static int32_t
129 strtouint31(const char *str, const int32_t defaultval) {
130 if (! str)
131 return defaultval;
132 if (! str[0])
133 return -1;
134
135 char * endp;
136 unsigned long val = strtoul(str, &endp, 10);
137 if (*endp || val > INT32_MAX)
138 return -1;
139 return (int32_t)val;
140 }
141
142 static void
143 delete_pcap_dag(const pcap_t *p)
144 {
145 pcap_dag_node_t *curr = NULL, *prev = NULL;
146
147 for (prev = NULL, curr = pcap_dags; curr != NULL && curr->p != p; prev = curr, curr = curr->next) {
148 /* empty */
149 }
150
151 if (curr != NULL && curr->p == p) {
152 if (prev != NULL) {
153 prev->next = curr->next;
154 } else {
155 pcap_dags = curr->next;
156 }
157 }
158 }
159
160 /*
161 * Performs a graceful shutdown of the DAG card, frees dynamic memory held
162 * in the pcap_t structure, and closes the file descriptor for the DAG card.
163 */
164
165 static void
166 dag_platform_cleanup(pcap_t *p)
167 {
168 struct pcap_dag *pd = p->priv;
169
170 /*
171 * Before stopping a Tx stream wait until the stream buffer has been
172 * drained, otherwise packets that have been buffered but have not yet
173 * been transmitted will be lost.
174 */
175 if (TX_ONLY(pd->dag_stream))
176 while (dag_get_stream_buffer_level64(p->fd, pd->dag_stream) > 0)
177 usleep (10000);
178
179 if(dag_stop_stream(p->fd, pd->dag_stream) < 0)
180 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
181
182 if(dag_detach_stream(p->fd, pd->dag_stream) < 0)
183 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
184
185 if(pd->dag_ref != NULL) {
186 dag_config_dispose(pd->dag_ref);
187 /*
188 * Note: we don't need to call close(p->fd) or
189 * dag_close(p->fd), as dag_config_dispose(pd->dag_ref)
190 * does this.
191 *
192 * Set p->fd to -1 to make sure that's not done.
193 */
194 p->fd = -1;
195 pd->dag_ref = NULL;
196 }
197 delete_pcap_dag(p);
198 pcapint_cleanup_live_common(p);
199 }
200
201 static void
202 atexit_handler(void)
203 {
204 while (pcap_dags != NULL) {
205 if (pcap_dags->pid == getpid()) {
206 if (pcap_dags->p != NULL)
207 dag_platform_cleanup(pcap_dags->p);
208 } else {
209 delete_pcap_dag(pcap_dags->p);
210 }
211 }
212 }
213
214 static int
215 new_pcap_dag(pcap_t *p)
216 {
217 pcap_dag_node_t *node = NULL;
218
219 if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
220 return -1;
221 }
222
223 if (!atexit_handler_installed) {
224 atexit(atexit_handler);
225 atexit_handler_installed = 1;
226 }
227
228 node->next = pcap_dags;
229 node->p = p;
230 node->pid = getpid();
231
232 pcap_dags = node;
233
234 return 0;
235 }
236
237 static unsigned int
238 dag_erf_ext_header_count(const uint8_t *erf, size_t len)
239 {
240 uint32_t hdr_num = 0;
241 uint8_t hdr_type;
242
243 /* basic sanity checks */
244 if ( erf == NULL )
245 return 0;
246 if ( len < 16 )
247 return 0;
248
249 /* check if we have any extension headers */
250 if (! (erf[8] & ERF_TYPE_MORE_EXT))
251 return 0;
252
253 /* loop over the extension headers */
254 do {
255
256 /* sanity check we have enough bytes */
257 if ( len < (24 + (hdr_num * 8)) )
258 return hdr_num;
259
260 /* get the header type */
261 hdr_type = erf[(16 + (hdr_num * 8))];
262 hdr_num++;
263
264 } while (hdr_type & ERF_TYPE_MORE_EXT);
265
266 return hdr_num;
267 }
268
269 static int
270 dag_rxtx_mismatch(const char *func, pcap_t *p)
271 {
272 const struct pcap_dag *pd = p->priv;
273
274 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: device %s is %s-only",
275 func,
276 p->opt.device,
277 RXTX_STR(pd->dag_stream));
278 return PCAP_ERROR;
279 }
280
281 static int
282 dag_read_notimpl(pcap_t *p, int cnt _U_, pcap_handler callback _U_, u_char *user _U_)
283 {
284 return dag_rxtx_mismatch(__func__, p);
285 }
286
287 static int
288 dag_getnonblock_fd_notimpl(pcap_t *p)
289 {
290 return dag_rxtx_mismatch(__func__, p);
291 }
292
293 static int
294 dag_stats_notimpl(pcap_t *p, struct pcap_stat *ps _U_)
295 {
296 return dag_rxtx_mismatch(__func__, p);
297 }
298
299 static int
300 dag_setnonblock_notimpl(pcap_t *p, int nonblock _U_)
301 {
302 return dag_rxtx_mismatch( __func__, p);
303 }
304
305 static int
306 dag_inject_notimpl(pcap_t *p, const void *buf _U_, int size _U_)
307 {
308 return dag_rxtx_mismatch(__func__, p);
309 }
310
311 static int
312 dag_install_bpf_program_notimpl(pcap_t *p, struct bpf_program *fp _U_)
313 {
314 return dag_rxtx_mismatch(__func__, p);
315 }
316
317 /*
318 * Read at most max_packets from the capture stream and call the callback
319 * for each of them. Returns the number of packets handled, PCAP_ERROR if an
320 * error occurred, or PCAP_ERROR_BREAK if we were told to break out of the loop.
321 */
322 static int
323 dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
324 {
325 struct pcap_dag *pd = p->priv;
326 int processed = 0;
327 unsigned int nonblocking = pd->dag_flags & DAGF_NONBLOCK;
328 unsigned int num_ext_hdr = 0;
329 unsigned int ticks_per_second;
330
331 /* Get the next bufferful of packets (if necessary). */
332 while (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size) {
333
334 /*
335 * Has "pcap_breakloop()" been called?
336 */
337 if (p->break_loop) {
338 /*
339 * Yes - clear the flag that indicates that
340 * it has, and return PCAP_ERROR_BREAK to indicate that
341 * we were told to break out of the loop.
342 */
343 p->break_loop = 0;
344 return PCAP_ERROR_BREAK;
345 }
346
347 /* dag_advance_stream() will block (unless nonblock is called)
348 * until 64kB of data has accumulated.
349 * If to_ms is set, it will timeout before 64kB has accumulated.
350 * We wait for 64kB because processing a few packets at a time
351 * can cause problems at high packet rates (>200kpps) due
352 * to inefficiencies.
353 * This does mean if to_ms is not specified the capture may 'hang'
354 * for long periods if the data rate is extremely slow (<64kB/sec)
355 * If non-block is specified it will return immediately. The user
356 * is then responsible for efficiency.
357 */
358 if ( NULL == (pd->dag_mem_top = dag_advance_stream(p->fd, pd->dag_stream, &(pd->dag_mem_bottom))) ) {
359 return PCAP_ERROR;
360 }
361
362 if (nonblocking && (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size))
363 {
364 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */
365 return 0;
366 }
367
368 if(!nonblocking &&
369 pd->dag_timeout &&
370 (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size))
371 {
372 /* Blocking mode, but timeout set and no data has arrived, return anyway.*/
373 return 0;
374 }
375
376 }
377
378 /*
379 * Process the packets.
380 *
381 * This assumes that a single buffer of packets will have
382 * <= INT_MAX packets, so the packet count doesn't overflow.
383 */
384 while (pd->dag_mem_top - pd->dag_mem_bottom >= dag_record_size) {
385
386 unsigned short packet_len = 0;
387 int caplen = 0;
388 struct pcap_pkthdr pcap_header;
389
390 dag_record_t *header = (dag_record_t *)(pd->dag_mem_bottom);
391
392 u_char *dp = ((u_char *)header); /* + dag_record_size; */
393 unsigned short rlen;
394
395 /*
396 * Has "pcap_breakloop()" been called?
397 */
398 if (p->break_loop) {
399 /*
400 * Yes - clear the flag that indicates that
401 * it has, and return PCAP_ERROR_BREAK to indicate that
402 * we were told to break out of the loop.
403 */
404 p->break_loop = 0;
405 return PCAP_ERROR_BREAK;
406 }
407
408 rlen = ntohs(header->rlen);
409 if (rlen < dag_record_size)
410 {
411 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
412 "%s: record too small", __func__);
413 return PCAP_ERROR;
414 }
415 pd->dag_mem_bottom += rlen;
416
417 uint8_t erf_type = header->type & ERF_TYPE_MASK;
418
419 /* Count lost packets. */
420 switch(erf_type) {
421 /* in these types the color value overwrites the lctr */
422 case ERF_TYPE_COLOR_HDLC_POS:
423 case ERF_TYPE_COLOR_ETH:
424 case ERF_TYPE_DSM_COLOR_HDLC_POS:
425 case ERF_TYPE_DSM_COLOR_ETH:
426 case ERF_TYPE_COLOR_MC_HDLC_POS:
427 case ERF_TYPE_COLOR_HASH_ETH:
428 case ERF_TYPE_COLOR_HASH_POS:
429 break;
430
431 default:
432 if ( (pd->drop_attr == kNullAttributeUuid) && (header->lctr) ) {
433 pd->stat.ps_drop += ntohs(header->lctr);
434 }
435 }
436
437 if (erf_type == ERF_TYPE_PAD) {
438 continue;
439 }
440
441 num_ext_hdr = dag_erf_ext_header_count(dp, rlen);
442
443 /* ERF encapsulation */
444 /* The Extensible Record Format is not dropped for this kind of encapsulation,
445 * and will be handled as a pseudo header by the decoding application.
446 * The information carried in the ERF header and in the optional subheader (if present)
447 * could be merged with the libpcap information, to offer a better decoding.
448 * The packet length is
449 * o the length of the packet on the link (header->wlen),
450 * o plus the length of the ERF header (dag_record_size), as the length of the
451 * pseudo header will be adjusted during the decoding,
452 * o plus the length of the optional subheader (if present).
453 *
454 * The capture length is header.rlen and the byte stuffing for alignment will be dropped
455 * if the capture length is greater than the packet length.
456 */
457 if (p->linktype == DLT_ERF) {
458 packet_len = ntohs(header->wlen) + dag_record_size;
459 caplen = rlen;
460 switch (erf_type) {
461 case ERF_TYPE_MC_AAL5:
462 case ERF_TYPE_MC_ATM:
463 case ERF_TYPE_MC_HDLC:
464 case ERF_TYPE_MC_RAW_CHANNEL:
465 case ERF_TYPE_MC_RAW:
466 case ERF_TYPE_MC_AAL2:
467 case ERF_TYPE_COLOR_MC_HDLC_POS:
468 packet_len += 4; /* MC header */
469 break;
470
471 case ERF_TYPE_COLOR_HASH_ETH:
472 case ERF_TYPE_DSM_COLOR_ETH:
473 case ERF_TYPE_COLOR_ETH:
474 case ERF_TYPE_ETH:
475 packet_len += 2; /* ETH header */
476 break;
477 } /* switch type */
478
479 /* Include ERF extension headers */
480 packet_len += (8 * num_ext_hdr);
481
482 if (caplen > packet_len) {
483 caplen = packet_len;
484 }
485 } else {
486 /* Other kind of encapsulation according to the header Type */
487
488 /* Skip over generic ERF header */
489 dp += dag_record_size;
490 /* Skip over extension headers */
491 dp += 8 * num_ext_hdr;
492
493 switch(erf_type) {
494 case ERF_TYPE_ATM:
495 case ERF_TYPE_AAL5:
496 if (erf_type == ERF_TYPE_AAL5) {
497 packet_len = ntohs(header->wlen);
498 caplen = rlen - dag_record_size;
499 }
500 /* FALLTHROUGH */
501 case ERF_TYPE_MC_ATM:
502 if (erf_type == ERF_TYPE_MC_ATM) {
503 caplen = packet_len = ATM_CELL_SIZE;
504 dp+=4;
505 }
506 /* FALLTHROUGH */
507 case ERF_TYPE_MC_AAL5:
508 if (erf_type == ERF_TYPE_MC_AAL5) {
509 packet_len = ntohs(header->wlen);
510 caplen = rlen - dag_record_size - 4;
511 dp+=4;
512 }
513 /* Skip over extension headers */
514 caplen -= (8 * num_ext_hdr);
515
516 if (erf_type == ERF_TYPE_ATM) {
517 caplen = packet_len = ATM_CELL_SIZE;
518 }
519 if (p->linktype == DLT_SUNATM) {
520 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
521 unsigned long rawatm;
522
523 rawatm = ntohl(*((uint32_t *)dp));
524 sunatm->vci = htons((rawatm >> 4) & 0xffff);
525 sunatm->vpi = (rawatm >> 20) & 0x00ff;
526 sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
527 ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 :
528 ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 :
529 ((dp[ATM_HDR_SIZE] == 0xaa &&
530 dp[ATM_HDR_SIZE+1] == 0xaa &&
531 dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
532
533 } else if (p->linktype == DLT_ATM_RFC1483) {
534 packet_len -= ATM_HDR_SIZE;
535 caplen -= ATM_HDR_SIZE;
536 dp += ATM_HDR_SIZE;
537 } else
538 continue;
539 break;
540
541 case ERF_TYPE_COLOR_HASH_ETH:
542 case ERF_TYPE_DSM_COLOR_ETH:
543 case ERF_TYPE_COLOR_ETH:
544 case ERF_TYPE_ETH:
545 if ((p->linktype != DLT_EN10MB) &&
546 (p->linktype != DLT_DOCSIS))
547 continue;
548 packet_len = ntohs(header->wlen);
549 packet_len -= (pd->dag_fcs_bits >> 3);
550 caplen = rlen - dag_record_size - 2;
551 /* Skip over extension headers */
552 caplen -= (8 * num_ext_hdr);
553 if (caplen > packet_len) {
554 caplen = packet_len;
555 }
556 dp += 2;
557 break;
558
559 case ERF_TYPE_COLOR_HASH_POS:
560 case ERF_TYPE_DSM_COLOR_HDLC_POS:
561 case ERF_TYPE_COLOR_HDLC_POS:
562 case ERF_TYPE_HDLC_POS:
563 if ((p->linktype != DLT_CHDLC) &&
564 (p->linktype != DLT_PPP_SERIAL) &&
565 (p->linktype != DLT_FRELAY))
566 continue;
567 packet_len = ntohs(header->wlen);
568 packet_len -= (pd->dag_fcs_bits >> 3);
569 caplen = rlen - dag_record_size;
570 /* Skip over extension headers */
571 caplen -= (8 * num_ext_hdr);
572 if (caplen > packet_len) {
573 caplen = packet_len;
574 }
575 break;
576
577 case ERF_TYPE_COLOR_MC_HDLC_POS:
578 case ERF_TYPE_MC_HDLC:
579 if ((p->linktype != DLT_CHDLC) &&
580 (p->linktype != DLT_PPP_SERIAL) &&
581 (p->linktype != DLT_FRELAY) &&
582 (p->linktype != DLT_MTP2) &&
583 (p->linktype != DLT_MTP2_WITH_PHDR) &&
584 (p->linktype != DLT_LAPD))
585 continue;
586 packet_len = ntohs(header->wlen);
587 packet_len -= (pd->dag_fcs_bits >> 3);
588 caplen = rlen - dag_record_size - 4;
589 /* Skip over extension headers */
590 caplen -= (8 * num_ext_hdr);
591 if (caplen > packet_len) {
592 caplen = packet_len;
593 }
594 /* jump the MC_HDLC_HEADER */
595 dp += 4;
596 if (p->linktype == DLT_MTP2_WITH_PHDR) {
597 /* Add the MTP2 Pseudo Header */
598 caplen += MTP2_HDR_LEN;
599 packet_len += MTP2_HDR_LEN;
600
601 TempPkt[MTP2_SENT_OFFSET] = 0;
602 TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN;
603 *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01);
604 *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff);
605 memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen);
606 dp = TempPkt;
607 }
608 break;
609
610 case ERF_TYPE_IPV4:
611 if ((p->linktype != DLT_RAW) &&
612 (p->linktype != DLT_IPV4))
613 continue;
614 packet_len = ntohs(header->wlen);
615 caplen = rlen - dag_record_size;
616 /* Skip over extension headers */
617 caplen -= (8 * num_ext_hdr);
618 if (caplen > packet_len) {
619 caplen = packet_len;
620 }
621 break;
622
623 case ERF_TYPE_IPV6:
624 if ((p->linktype != DLT_RAW) &&
625 (p->linktype != DLT_IPV6))
626 continue;
627 packet_len = ntohs(header->wlen);
628 caplen = rlen - dag_record_size;
629 /* Skip over extension headers */
630 caplen -= (8 * num_ext_hdr);
631 if (caplen > packet_len) {
632 caplen = packet_len;
633 }
634 break;
635
636 /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */
637 case ERF_TYPE_MC_RAW:
638 case ERF_TYPE_MC_RAW_CHANNEL:
639 case ERF_TYPE_IP_COUNTER:
640 case ERF_TYPE_TCP_FLOW_COUNTER:
641 case ERF_TYPE_INFINIBAND:
642 case ERF_TYPE_RAW_LINK:
643 case ERF_TYPE_INFINIBAND_LINK:
644 default:
645 /* Unhandled ERF type.
646 * Ignore rather than generating error
647 */
648 continue;
649 } /* switch type */
650
651 } /* ERF encapsulation */
652
653 /*
654 * In this libpcap module the two length arguments of
655 * pcapint_filter() (the wire length and the captured length)
656 * can have different values.
657 *
658 * The wire length of this packet is packet_len, which is
659 * derived from ERF wlen; the captured length of this packet
660 * is caplen, which is derived from ERF rlen, which in turn
661 * depends on the card/stream slen; the snapshot length
662 * configured for this pcap handle is p->snapshot.
663 */
664 if ((p->fcode.bf_insns == NULL) || pcapint_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
665
666 /* convert between timestamp formats */
667 register unsigned long long ts;
668
669 #if __BYTE_ORDER == __BIG_ENDIAN
670 ts = SWAPLL(header->ts);
671 #else
672 ts = header->ts;
673 #endif // __BYTE_ORDER
674
675 switch (p->opt.tstamp_precision) {
676 case PCAP_TSTAMP_PRECISION_NANO:
677 ticks_per_second = 1000000000;
678 break;
679 case PCAP_TSTAMP_PRECISION_MICRO:
680 default:
681 ticks_per_second = 1000000;
682 break;
683
684 }
685 pcap_header.ts.tv_sec = ts >> 32;
686 ts = (ts & 0xffffffffULL) * ticks_per_second;
687 ts += 0x80000000; /* rounding */
688 pcap_header.ts.tv_usec = ts >> 32;
689 if (pcap_header.ts.tv_usec >= ticks_per_second) {
690 pcap_header.ts.tv_usec -= ticks_per_second;
691 pcap_header.ts.tv_sec++;
692 }
693
694 /* Fill in our own header data */
695 pcap_header.caplen = min(caplen, p->snapshot);
696 pcap_header.len = packet_len;
697
698 /* Count the packet. */
699 pd->stat.ps_recv++;
700
701 /* Call the user supplied callback function */
702 callback(user, &pcap_header, dp);
703
704 /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */
705 processed++;
706 if (processed == cnt && !PACKET_COUNT_IS_UNLIMITED(cnt))
707 {
708 /* Reached the user-specified limit. */
709 return cnt;
710 }
711 }
712 }
713
714 return processed;
715 }
716
717 /*
718 * The minimum number of bytes in a valid Ethernet packet from the beginning
719 * of the destination MAC address to the end of the payload, as far as DAG
720 * TERF and IEEE 802.3 are concerned.
721 */
722 #define ETH_MINLEN_NOFCS 60
723
724 // ...and the maximum, as far as DAG TERF is concerned.
725 #define ETH_MAXLEN_NOFCS 9596
726
727 /*
728 * The minimum number of bytes in a valid Ethernet header: the destination and
729 * the source MAC addresses, the EtherType. (This does not take 802.1Q or
730 * Q-in-Q into account.)
731 */
732 #define ETH_MINLEN_HDRONLY 14
733
734 // Zero padding source (a bit oversized for ERF_TYPE_ETH purposes).
735 static const uint8_t tx_pad[ETH_MINLEN_NOFCS + 4 + ALIGN_BYTES_MAX];
736
737 /*
738 * Take an Ethernet frame, build an ERF record around it and feed the record
739 * into the [Tx-only] DAG stream. The frame must not include FCS, which is
740 * usually the case for DLT_EN10MB in libpcap.
741 */
742 static int
743 dag_inject(pcap_t *p, const void *packet, const int plen)
744 {
745 struct pcap_dag *pd = p->priv;
746
747 if (plen <= ETH_MINLEN_HDRONLY || plen > ETH_MAXLEN_NOFCS) {
748 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
749 "invalid packet size %d", plen);
750 return PCAP_ERROR;
751 }
752
753 /*
754 * sizeof(struct dag_record_t) cannot be used because it is much
755 * greater than the specific ERF header in this buffer.
756 */
757 const dag_size_t hlen = dag_record_size + 2;
758 /*
759 * Some DAG hardware (e.g. 7.5G2) automatically pads outgoing Ethernet
760 * frames that are shorter than the minimum length to make them long
761 * enough. Other DAG hardware (e.g. 9.2X2) rejects such frames. Pad
762 * the frame here if necessary and remove this difference from the
763 * problem space.
764 *
765 * If the TERF is expecting Ethernet frames to have a non-zero number
766 * of FCS bytes (typically in order to strip the FCS correctly before
767 * further processing), append a dummy zero FCS of the expected size.
768 */
769 const unsigned eth_pad_len = ETH_MINLEN_NOFCS
770 - min(plen, ETH_MINLEN_NOFCS) + pd->terf_fcs_bytes;
771 const unsigned rlen = hlen + plen + eth_pad_len;
772 const unsigned erf_pad_len = rlen % pd->tx_align_bytes ?
773 pd->tx_align_bytes - rlen % pd->tx_align_bytes :
774 0;
775 dag_record_t header = {
776 .type = ERF_TYPE_ETH,
777 .flags.vlen = 1,
778 .rlen = htons(rlen + erf_pad_len),
779 .wlen = htons(plen + eth_pad_len),
780 // Silence a -Wmissing-field-initializers from old GCC.
781 .rec.eth = {0},
782 };
783 DAG_ERF_SET_IFACE(&header, pd->tx_iface);
784
785 /*
786 * It is fine to feed less data than a complete ERF record at a time so
787 * long as a complete and well-formed ERF record eventually makes it
788 * into the buffer. This simplifies the process when different parts
789 * of the ERF record come from different memory locations.
790 */
791 if (dag_tx_stream_copy_bytes64(p->fd, pd->dag_stream, (uint8_t *)&header, hlen) < 0 ||
792 dag_tx_stream_copy_bytes64(p->fd, pd->dag_stream, (uint8_t *)packet, plen) < 0)
793 goto fail;
794 /*
795 * Possibly pad to the minimum packet length and/or append a dummy FCS
796 * and/or pad to the next multiple of the detected alignment unit.
797 */
798 const unsigned pad_len = eth_pad_len + erf_pad_len;
799 if (pad_len &&
800 dag_tx_stream_copy_bytes64(p->fd, pd->dag_stream, (uint8_t *)tx_pad, pad_len) < 0)
801 goto fail;
802 return plen;
803
804 fail:
805 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
806 errno, "dag_tx_stream_copy_bytes64 %s", p->opt.device);
807 return PCAP_ERROR;
808 }
809
810 static int
811 dag_activate_tx(pcap_t *p)
812 {
813 struct pcap_dag *pd = p->priv;
814
815 const char * env = getenv(ENV_TX_IFACE);
816 int32_t iface = strtouint31(env, 0);
817 if (iface < 0) {
818 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
819 "%s: failed parsing %s value \"%s\"",
820 __func__, ENV_TX_IFACE, env);
821 return PCAP_ERROR;
822 }
823 uint32_t ifcount = dag_config_get_interface_count(pd->dag_ref);
824 if ((uint32_t)iface >= ifcount) {
825 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
826 "%s: invalid %s value %u: this card has %u interface(s)",
827 __func__, ENV_TX_IFACE, iface, ifcount);
828 return PCAP_ERROR;
829 }
830 pd->tx_iface = (uint8_t)iface;
831
832 const dag_card_inf_t *inf = dag_pciinfo(p->fd);
833 if (! inf) {
834 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
835 errno, "dag_pciinfo");
836 return PCAP_ERROR;
837 }
838
839 if (inf->device_code == PCI_DEVICE_ID_VDAG) {
840 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
841 "vDAG Tx streams are not supported");
842 return PCAP_ERROR;
843 }
844
845 // Determine the correct alignment/padding size for the card.
846 switch(inf->device_code) {
847 case PCI_DEVICE_ID_DAG9_2X2:
848 case PCI_DEVICE_ID_DAG9_2SX2:
849 pd->tx_align_bytes = ALIGN_BYTES_9_2;
850 break;
851 default:
852 pd->tx_align_bytes = ALIGN_BYTES_DEFAULT;
853 }
854
855 // Read the TERF FCS size for later use by dag_inject().
856 dag_component_t cfg_comp = dag_component_get_subcomponent(
857 pd->dag_root, kComponentTerf, 0);
858 if (! cfg_comp) {
859 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
860 "%s: dag_component_get_subcomponent",
861 __func__);
862 return PCAP_ERROR;
863 }
864
865 attr_uuid_t cfg_uuid = dag_component_get_config_attribute_uuid(
866 cfg_comp, kUint32AttributeTerfStripCrc);
867 if (cfg_uuid == kNullAttributeUuid) {
868 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
869 "%s: dag_component_get_config_attribute_uuid",
870 __func__);
871 return PCAP_ERROR;
872 }
873
874 uint32_t cfg_uint32;
875 dag_err_t cfg_err = dag_config_get_uint32_attribute_ex(pd->dag_ref,
876 cfg_uuid, &cfg_uint32);
877 if (cfg_err != kDagErrNone) {
878 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
879 "%s: dag_config_get_uint32_attribute_ex",
880 __func__);
881 return PCAP_ERROR;
882 }
883
884 switch (cfg_uint32) {
885 case kTerfNoStrip:
886 pd->terf_fcs_bytes = 0;
887 break;
888 case kTerfStrip16:
889 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
890 "TERF FCS is configured to 16 bits, is this Ethernet?");
891 return PCAP_ERROR;
892 case kTerfStrip32:
893 pd->terf_fcs_bytes = 4;
894 break;
895 default:
896 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
897 "failed reading TERF FCS configuration");
898 return PCAP_ERROR;
899 }
900
901 /*
902 * TODO: It would be nice to verify that the Tx port is
903 * configured for 32-bit Tx FCS, but it is not trivial to
904 * tell the exact subcomponent that has the attribute.
905 */
906
907 return 0;
908 }
909
910 /*
911 * Get a handle for a live capture from the given DAG device. The promisc
912 * flag is ignored because DAG cards are always promiscuous. The to_ms
913 * parameter is used in setting the API polling parameters.
914 *
915 * See also pcap(3).
916 */
917 static int dag_activate(pcap_t* p)
918 {
919 struct pcap_dag *pd = p->priv;
920 char *s;
921 int n;
922 char * device = p->opt.device;
923 int ret;
924 dag_size_t mindata;
925 struct timeval maxwait;
926 struct timeval poll;
927
928 /*
929 * dag_create() has validated the device name syntax and stored the
930 * parsed device and stream numbers to p->priv. Validate these values
931 * semantically.
932 */
933 if (pd->dag_devnum >= DAG_MAX_BOARDS) {
934 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
935 "DAG device number %d is too large", pd->dag_devnum);
936 ret = PCAP_ERROR_NO_SUCH_DEVICE;
937 goto fail;
938 }
939 if (pd->dag_stream >= DAG_STREAM_MAX) {
940 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
941 "DAG stream number %d is too large", pd->dag_stream);
942 ret = PCAP_ERROR_NO_SUCH_DEVICE;
943 goto fail;
944 }
945 #ifndef ENABLE_DAG_TX
946 if (TX_ONLY(pd->dag_stream)) {
947 /*
948 * dag_findalldevs() does not return any Tx streams, so
949 * PCAP_ERROR_NO_SUCH_DEVICE is more consistent than
950 * PCAP_ERROR_CAPTURE_NOTSUP.
951 */
952 ret = PCAP_ERROR_NO_SUCH_DEVICE;
953 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: tx (odd numbered) streams not supported for capture", __func__);
954 goto fail;
955 }
956 #endif // ENABLE_DAG_TX
957
958 /* setup device parameters */
959 if((pd->dag_ref = dag_config_init(device)) == NULL) {
960 /*
961 * XXX - does this reliably set errno?
962 */
963 if (errno == ENOENT) {
964 /*
965 * There's nothing more to say, so clear
966 * the error message.
967 */
968 ret = PCAP_ERROR_NO_SUCH_DEVICE;
969 p->errbuf[0] = '\0';
970 } else if (errno == EPERM || errno == EACCES) {
971 ret = PCAP_ERROR_PERM_DENIED;
972 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
973 "Attempt to open %s failed with %s - additional privileges may be required",
974 device, (errno == EPERM) ? "EPERM" : "EACCES");
975 } else {
976 ret = PCAP_ERROR;
977 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
978 errno, "dag_config_init %s", device);
979 }
980 goto fail;
981 }
982
983 if((p->fd = dag_config_get_card_fd(pd->dag_ref)) < 0) {
984 /*
985 * XXX - does this reliably set errno?
986 */
987 ret = PCAP_ERROR;
988 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
989 errno, "dag_config_get_card_fd %s", device);
990 goto failclose;
991 }
992
993 /* Open requested stream. Can fail if already locked or on error */
994 if (dag_attach_stream64(p->fd, pd->dag_stream, 0, 0) < 0) {
995 if (errno == ENOMEM) {
996 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
997 "dag%u has no memory allocated to %s stream %u",
998 pd->dag_devnum, RXTX_STR(pd->dag_stream), pd->dag_stream);
999 /*
1000 * dag_findalldevs() does not return streams that do
1001 * not have buffer memory, so PCAP_ERROR_NO_SUCH_DEVICE
1002 * is more consistent than PCAP_ERROR_CAPTURE_NOTSUP.
1003 */
1004 ret = PCAP_ERROR_NO_SUCH_DEVICE;
1005 goto failclose;
1006 } else if (errno == EINVAL) {
1007 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1008 "dag%u has no %s stream %u",
1009 pd->dag_devnum, RXTX_STR(pd->dag_stream), pd->dag_stream);
1010 ret = PCAP_ERROR_NO_SUCH_DEVICE;
1011 goto failclose;
1012 }
1013 ret = PCAP_ERROR;
1014 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1015 errno, "dag_attach_stream64");
1016 goto failclose;
1017 }
1018
1019 /* Try to find Stream Drop attribute */
1020 pd->drop_attr = kNullAttributeUuid;
1021 pd->dag_root = dag_config_get_root_component(pd->dag_ref);
1022 if ( dag_component_get_subcomponent(pd->dag_root, kComponentStreamFeatures, 0) )
1023 {
1024 pd->drop_attr = dag_config_get_indexed_attribute_uuid(pd->dag_ref, kUint32AttributeStreamDropCount, pd->dag_stream);
1025 if (pd->drop_attr != kNullAttributeUuid)
1026 pd->drop_base = dag_config_get_uint64_attribute(
1027 pd->dag_ref, pd->drop_attr);
1028 }
1029
1030 /* Set up default poll parameters for stream
1031 * Can be overridden by pcap_set_nonblock()
1032 */
1033 if (dag_get_stream_poll64(p->fd, pd->dag_stream,
1034 &mindata, &maxwait, &poll) < 0) {
1035 ret = PCAP_ERROR;
1036 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1037 errno, "dag_get_stream_poll64");
1038 goto faildetach;
1039 }
1040
1041 /* Use the poll time as the required select timeout for callers
1042 * who are using select()/etc. in an event loop waiting for
1043 * packets to arrive.
1044 */
1045 pd->required_select_timeout = poll;
1046 p->required_select_timeout = &pd->required_select_timeout;
1047
1048 /*
1049 * Turn a negative snapshot value (invalid), a snapshot value of
1050 * 0 (unspecified), or a value bigger than the normal maximum
1051 * value, into the maximum allowed value.
1052 *
1053 * If some application really *needs* a bigger snapshot
1054 * length, we should just increase MAXIMUM_SNAPLEN.
1055 */
1056 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
1057 p->snapshot = MAXIMUM_SNAPLEN;
1058
1059 if (p->opt.immediate) {
1060 /* Call callback immediately.
1061 * XXX - is this the right way to p this?
1062 */
1063 mindata = 0;
1064 } else {
1065 /* Amount of data to collect in Bytes before calling callbacks.
1066 * Important for efficiency, but can introduce latency
1067 * at low packet rates if to_ms not set!
1068 */
1069 mindata = 65536;
1070 }
1071
1072 /* Obey opt.timeout (was to_ms) if supplied. This is a good idea!
1073 * Recommend 10-100ms. Calls will time out even if no data arrived.
1074 */
1075 maxwait.tv_sec = p->opt.timeout/1000;
1076 maxwait.tv_usec = (p->opt.timeout%1000) * 1000;
1077
1078 if (dag_set_stream_poll64(p->fd, pd->dag_stream,
1079 mindata, &maxwait, &poll) < 0) {
1080 ret = PCAP_ERROR;
1081 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1082 errno, "dag_set_stream_poll64");
1083 goto faildetach;
1084 }
1085
1086 if(dag_start_stream(p->fd, pd->dag_stream) < 0) {
1087 ret = PCAP_ERROR;
1088 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1089 errno, "dag_start_stream %s", device);
1090 goto faildetach;
1091 }
1092
1093 /*
1094 * Important! You have to ensure bottom is properly
1095 * initialized to zero on startup, it won't give you
1096 * a compiler warning if you make this mistake!
1097 */
1098 pd->dag_mem_bottom = 0;
1099 pd->dag_mem_top = 0;
1100
1101 /*
1102 * Find out how many FCS bits we should strip.
1103 * Assume Rx FCS length to be 32 bits unless the user has
1104 * requested a different value, in which case validate it well.
1105 */
1106 s = getenv(ENV_RX_FCS_BITS);
1107 switch ((n = strtouint31(s, 32))) {
1108 case 0:
1109 case 16:
1110 case 32:
1111 pd->dag_fcs_bits = n;
1112 break;
1113 default:
1114 ret = PCAP_ERROR;
1115 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1116 "%s %s: invalid %s value (%s) in environment",
1117 __func__, device, ENV_RX_FCS_BITS, s);
1118 goto failstop;
1119 }
1120
1121 /*
1122 * Did the user request that they not be stripped?
1123 */
1124 s = getenv(ENV_RX_FCS_NOSTRIP);
1125 switch ((n = strtouint31(s, 0))) {
1126 case 0:
1127 break;
1128 case 1:
1129 /* Yes. Note the number of 16-bit words that will be
1130 supplied. */
1131 p->linktype_ext = LT_FCS_DATALINK_EXT(pd->dag_fcs_bits/16);
1132
1133 /* And don't strip them. */
1134 pd->dag_fcs_bits = 0;
1135 break;
1136 default:
1137 ret = PCAP_ERROR;
1138 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1139 "%s %s: invalid %s value (%s) in environment",
1140 __func__, device, ENV_RX_FCS_NOSTRIP, s);
1141 goto failstop;
1142 }
1143
1144 if (TX_ONLY(pd->dag_stream) && dag_activate_tx(p) < 0) {
1145 ret = PCAP_ERROR;
1146 goto failstop;
1147 }
1148
1149 pd->dag_timeout = p->opt.timeout;
1150
1151 if (dag_get_datalink(p) < 0) {
1152 ret = PCAP_ERROR;
1153 goto failstop;
1154 }
1155
1156 p->bufsize = 0;
1157
1158 if (new_pcap_dag(p) < 0) {
1159 ret = PCAP_ERROR;
1160 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1161 errno, "new_pcap_dag %s", device);
1162 goto failstop;
1163 }
1164
1165 /*
1166 * "select()" and "poll()" don't work on DAG device descriptors.
1167 */
1168 p->selectable_fd = -1;
1169
1170 p->read_op = RX_ONLY(pd->dag_stream) ?
1171 dag_read :
1172 dag_read_notimpl;
1173 p->inject_op = TX_ONLY(pd->dag_stream) ?
1174 dag_inject :
1175 dag_inject_notimpl;
1176 p->setfilter_op = RX_ONLY(pd->dag_stream) ?
1177 pcapint_install_bpf_program :
1178 dag_install_bpf_program_notimpl;
1179 p->setdirection_op = NULL; /* Not implemented.*/
1180 p->set_datalink_op = dag_set_datalink;
1181 p->getnonblock_op = RX_ONLY(pd->dag_stream) ?
1182 pcapint_getnonblock_fd :
1183 dag_getnonblock_fd_notimpl;
1184 p->setnonblock_op = RX_ONLY(pd->dag_stream) ?
1185 dag_setnonblock :
1186 dag_setnonblock_notimpl;
1187 p->stats_op = RX_ONLY(pd->dag_stream) ?
1188 dag_stats :
1189 dag_stats_notimpl;
1190 p->cleanup_op = dag_platform_cleanup;
1191 pd->stat.ps_drop = 0;
1192 pd->stat.ps_recv = 0;
1193 pd->stat.ps_ifdrop = 0;
1194 return 0;
1195
1196 failstop:
1197 if (dag_stop_stream(p->fd, pd->dag_stream) < 0) {
1198 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
1199 }
1200
1201 faildetach:
1202 if (dag_detach_stream(p->fd, pd->dag_stream) < 0)
1203 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
1204
1205 failclose:
1206 dag_config_dispose(pd->dag_ref);
1207 /*
1208 * Note: we don't need to call close(p->fd) or dag_close(p->fd),
1209 * as dag_config_dispose(pd->dag_ref) does this.
1210 *
1211 * Set p->fd to -1 to make sure that's not done.
1212 */
1213 p->fd = -1;
1214 pd->dag_ref = NULL;
1215 delete_pcap_dag(p);
1216
1217 fail:
1218 pcapint_cleanup_live_common(p);
1219
1220 return ret;
1221 }
1222
1223 pcap_t *dag_create(const char *device, char *ebuf, int *is_ours)
1224 {
1225 const char *cp;
1226 char *cpend;
1227 long devnum;
1228 pcap_t *p;
1229 long stream = 0;
1230
1231 /*
1232 * The nominal libpcap DAG device name format is either "dagN" or
1233 * "dagN:M", as returned from dag_findalldevs().
1234 *
1235 * First attempt the most basic syntax validation. If the device string
1236 * does not look like a potentially valid DAG device name, reject it
1237 * silently to have pcap_create() try another capture source type.
1238 */
1239 *is_ours = 0;
1240
1241 /* Does this look like a DAG device? */
1242 cp = device;
1243 /* Does it begin with "dag"? */
1244 if (strncmp(cp, "dag", 3) != 0) {
1245 /* Nope, doesn't begin with "dag" */
1246 return NULL;
1247 }
1248 /* Yes - is "dag" followed by a number from 0 to DAG_MAX_BOARDS-1 */
1249 cp += 3;
1250 devnum = strtol(cp, &cpend, 10);
1251 if (*cpend == ':') {
1252 /* Followed by a stream number. */
1253 stream = strtol(++cpend, &cpend, 10);
1254 }
1255
1256 if (cpend == cp || *cpend != '\0') {
1257 /* Not followed by a number. */
1258 return NULL;
1259 }
1260
1261 /*
1262 * OK, it's probably ours, validate the syntax further. From now on
1263 * reject the device string authoritatively with an error message to
1264 * have pcap_create() propagate the failure. Validate the device and
1265 * stream number ranges loosely only.
1266 */
1267 *is_ours = 1;
1268 snprintf (ebuf, PCAP_ERRBUF_SIZE,
1269 "DAG device name \"%s\" is invalid", device);
1270
1271 if (devnum < 0 || devnum > INT_MAX) {
1272 /* Followed by a non-valid number. */
1273 return NULL;
1274 }
1275
1276 if (stream < 0 || stream > INT_MAX) {
1277 /* Followed by a non-valid stream number. */
1278 return NULL;
1279 }
1280
1281 /*
1282 * The syntax validation done so far is lax enough to accept some
1283 * device strings that are not actually acceptable in libpcap as
1284 * defined above. The device strings that are acceptable in libpcap
1285 * are a strict subset of the device strings that are acceptable in
1286 * dag_parse_name(), thus using the latter for validation in libpcap
1287 * would not work reliably. Instead from the detected device and
1288 * stream numbers produce the acceptable device string(s) and require
1289 * the input device string to match an acceptable string exactly.
1290 */
1291 char buf[DAGNAME_BUFSIZE];
1292 snprintf(buf, sizeof(buf), "dag%ld:%ld", devnum, stream);
1293 char acceptable = ! strcmp(device, buf);
1294 if (! acceptable && stream == 0) {
1295 snprintf(buf, sizeof(buf), "dag%ld", devnum);
1296 acceptable = ! strcmp(device, buf);
1297 }
1298 if (! acceptable)
1299 return NULL;
1300
1301 /*
1302 * The device string syntax is acceptable, save the device and stream
1303 * numbers for dag_activate(), which will do semantic and run-time
1304 * validation and possibly reject the pcap_t using more specific error
1305 * codes.
1306 */
1307 ebuf[0] = '\0';
1308 p = PCAP_CREATE_COMMON(ebuf, struct pcap_dag);
1309 if (p == NULL)
1310 return NULL;
1311
1312 p->activate_op = dag_activate;
1313
1314 /*
1315 * We claim that we support microsecond and nanosecond time
1316 * stamps.
1317 *
1318 * XXX Our native precision is 2^-32s, but libpcap doesn't support
1319 * power of two precisions yet. We can convert to either MICRO or NANO.
1320 */
1321 p->tstamp_precision_list = malloc(2 * sizeof(u_int));
1322 if (p->tstamp_precision_list == NULL) {
1323 pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1324 errno, "malloc");
1325 pcap_close(p);
1326 return NULL;
1327 }
1328 p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
1329 p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
1330 p->tstamp_precision_count = 2;
1331 struct pcap_dag *pd = p->priv;
1332 pd->dag_devnum = (int)devnum;
1333 pd->dag_stream = (int)stream;
1334 return p;
1335 }
1336
1337 static int
1338 dag_stats(pcap_t *p, struct pcap_stat *ps) {
1339 struct pcap_dag *pd = p->priv;
1340 uint64_t stream_drop;
1341 dag_err_t dag_error;
1342
1343 /*
1344 * Packet records received (ps_recv) are counted in dag_read().
1345 * Packet records dropped (ps_drop) are read from Stream Drop attribute if present,
1346 * otherwise integrate the ERF Header lctr counts (if available) in dag_read().
1347 * We are reporting that no records are dropped by the card/driver (ps_ifdrop).
1348 */
1349
1350 if(pd->drop_attr != kNullAttributeUuid) {
1351 /* Note this counter is cleared at start of capture and will wrap at UINT_MAX.
1352 * The application is responsible for polling ps_drop frequently enough
1353 * to detect each wrap and integrate total drop with a wider counter */
1354 if ((dag_error = dag_config_get_uint64_attribute_ex(pd->dag_ref, pd->drop_attr, &stream_drop)) == kDagErrNone) {
1355 pd->stat.ps_drop = (u_int)(stream_drop - pd->drop_base);
1356 } else {
1357 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "reading stream drop attribute: %s",
1358 dag_config_strerror(dag_error));
1359 return PCAP_ERROR;
1360 }
1361 }
1362
1363 *ps = pd->stat;
1364
1365 return 0;
1366 }
1367
1368 static const char *
1369 dag_device_description(const unsigned dagid)
1370 {
1371 static char buf[128];
1372 snprintf(buf, sizeof(buf), "alias for dag%u:0", dagid);
1373 return buf;
1374 }
1375
1376 static const char *
1377 dag_stream_short_description(const unsigned stream)
1378 {
1379 static char buf[128];
1380 snprintf(buf, sizeof(buf), "%s stream %u", RXTX_STR(stream), stream);
1381 return buf;
1382 }
1383
1384 static const char *
1385 dag_stream_long_description(const unsigned stream, const dag_size_t bufsize,
1386 const dag_card_inf_t * inf)
1387 {
1388 static char buf[256];
1389 int done = snprintf(buf, sizeof(buf),
1390 "%s stream %u, %" PRIu64 " MiB, %s",
1391 RXTX_STR(stream),
1392 stream,
1393 bufsize / 1024 / 1024,
1394 inf ? dag_device_name(inf->device_code, 1) : "N/A");
1395 if (inf->device_code != PCI_DEVICE_ID_VDAG)
1396 snprintf(buf + done, sizeof(buf) - done,
1397 " rev %c at %s",
1398 (inf && inf->brd_rev < 26) ? ('A' + inf->brd_rev) : '?',
1399 inf ? inf->bus_id : "N/A");
1400 return buf;
1401 }
1402
1403 /*
1404 * Add all DAG devices.
1405 */
1406 int
1407 dag_findalldevs(pcap_if_list_t *devlistp, char *errbuf)
1408 {
1409 int c;
1410 int dagfd;
1411 const char * description;
1412 int stream, rxstreams;
1413 // A DAG card associates a link status with each physical port, but not
1414 // with the data streams. The number of ports is a matter of hardware,
1415 // the number of streams and how each stream associates with zero or
1416 // more ports is a matter of how the user configures the card. In this
1417 // context libpcap uses the streams only (i.e. "dag0" is a shorthand
1418 // for "dag0:0"), thus the notion of link status does not apply to the
1419 // resulting libpcap DAG capture devices.
1420 const bpf_u_int32 flags = PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
1421 FILE * sysfsinfo = NULL;
1422
1423 /* Try all the DAGs 0-DAG_MAX_BOARDS */
1424 for (c = 0; c < DAG_MAX_BOARDS; c++) {
1425 char name[DAGNAME_BUFSIZE]; // libpcap device
1426 snprintf(name, sizeof(name), "dag%d", c);
1427 char dagname[DAGNAME_BUFSIZE]; // DAG API device
1428 snprintf(dagname, sizeof(dagname), "/dev/dag%d", c);
1429 if ( (dagfd = dag_open(dagname)) >= 0 ) {
1430 // Do not add a shorthand device for stream 0 (dagN) yet -- the
1431 // user can disable any stream in the card configuration.
1432 const dag_card_inf_t * inf = dag_pciinfo(dagfd); // NULL is fine
1433 // The count includes existing streams that have no buffer memory.
1434 rxstreams = dag_rx_get_stream_count(dagfd);
1435 if (rxstreams < 0) {
1436 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1437 errno, "dag_rx_get_stream_count");
1438 goto failclose;
1439 }
1440 int txstreams = 0;
1441 #ifdef ENABLE_DAG_TX
1442 txstreams = dag_tx_get_stream_count(dagfd);
1443 if (txstreams < 0) {
1444 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1445 errno, "dag_tx_get_stream_count");
1446 goto failclose;
1447 }
1448 #endif // ENABLE_DAG_TX
1449 for(stream = 0;
1450 stream < DAG_STREAM_MAX && (rxstreams > 0 || txstreams > 0);
1451 stream++) {
1452 #ifndef ENABLE_DAG_TX
1453 if (TX_ONLY(stream))
1454 continue;
1455 #endif // ENABLE_DAG_TX
1456 /*
1457 * dag_attach_stream64() was used before to test if the
1458 * stream exists, but it is not the best tool for the
1459 * job because it tries to lock the stream exclusively.
1460 * If the stream is already locked by another process,
1461 * it fails with EBUSY, otherwise it creates a race
1462 * condition for other processes that may be trying to
1463 * lock the same stream at the same time. Therefore
1464 * dag_get_stream_buffer_size64() seems to be a better
1465 * fit.
1466 */
1467 dag_ssize_t bufsize = dag_get_stream_buffer_size64(dagfd, stream);
1468 if (bufsize < 0)
1469 continue; // Does not exist.
1470 // Only streams with buffer memory are usable.
1471 if (bufsize > 0 &&
1472 (RX_ONLY(stream) || inf->device_code != PCI_DEVICE_ID_VDAG)) {
1473 description = dag_device_description (c);
1474 // a conditional shorthand device
1475 if (stream == 0 &&
1476 pcapint_add_dev(devlistp, name, flags, description, errbuf) == NULL)
1477 goto failclose;
1478 // and the stream device
1479 snprintf(name, sizeof(name), "dag%d:%d", c, stream);
1480 description = dag_stream_long_description(stream,
1481 dag_get_stream_buffer_size64(dagfd, stream), inf);
1482 if (pcapint_add_dev(devlistp, name, flags, description, errbuf) == NULL) {
1483 goto failclose;
1484 }
1485 }
1486 if (RX_ONLY(stream))
1487 rxstreams--;
1488 else
1489 txstreams--;
1490 }
1491 dag_close(dagfd);
1492 dagfd = -1;
1493 } else if (errno == EACCES) {
1494 // The device exists, but the current user privileges are not
1495 // sufficient for dag_open().
1496 // Do not add a shorthand device for stream 0 yet -- same as above.
1497 // Try enumerating the streams using sysfs. The file lists
1498 // all streams (Rx and Tx) that have non-zero amount of buffer
1499 // memory.
1500 char sysfspath[PATH_MAX];
1501 snprintf(sysfspath, sizeof(sysfspath), "/sys/devices/virtual/dag/%s/info", name);
1502 if ((sysfsinfo = fopen(sysfspath, "r"))) {
1503 char linebuf[1024];
1504 while (fgets(linebuf, sizeof(linebuf), sysfsinfo))
1505 if (1 == sscanf(linebuf, "Stream %u:", &stream)) {
1506 #ifndef ENABLE_DAG_TX
1507 if (TX_ONLY(stream))
1508 continue;
1509 #endif // ENABLE_DAG_TX
1510 // a conditional shorthand device
1511 description = dag_device_description(c);
1512 if (stream == 0 &&
1513 pcapint_add_dev(devlistp, name, flags, description, errbuf) == NULL)
1514 goto failclose;
1515 // and the stream device
1516 snprintf(name, sizeof(name), "dag%u:%u", c, stream);
1517 // TODO: Parse and describe the buffer size too.
1518 description = dag_stream_short_description(stream);
1519 if (pcapint_add_dev(devlistp, name, flags, description, errbuf) == NULL)
1520 goto failclose;
1521 }
1522 fclose(sysfsinfo);
1523 sysfsinfo = NULL;
1524 }
1525 } // errno == EACCES
1526
1527 }
1528 return (0);
1529
1530 failclose:
1531 if (dagfd >= 0)
1532 dag_close(dagfd);
1533 if (sysfsinfo)
1534 fclose(sysfsinfo);
1535 return PCAP_ERROR;
1536 }
1537
1538 static int
1539 dag_set_datalink(pcap_t *p, int dlt)
1540 {
1541 p->linktype = dlt;
1542
1543 return (0);
1544 }
1545
1546 static int
1547 dag_setnonblock(pcap_t *p, int nonblock)
1548 {
1549 struct pcap_dag *pd = p->priv;
1550 dag_size_t mindata;
1551 struct timeval maxwait;
1552 struct timeval poll;
1553
1554 /*
1555 * Set non-blocking mode on the FD.
1556 * XXX - is that necessary? If not, don't bother calling it,
1557 * and have a "dag_getnonblock()" function that looks at
1558 * "pd->dag_flags".
1559 */
1560 if (pcapint_setnonblock_fd(p, nonblock) < 0)
1561 return PCAP_ERROR;
1562
1563 if (dag_get_stream_poll64(p->fd, pd->dag_stream,
1564 &mindata, &maxwait, &poll) < 0) {
1565 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1566 errno, "dag_get_stream_poll64");
1567 return PCAP_ERROR;
1568 }
1569
1570 /* Amount of data to collect in Bytes before calling callbacks.
1571 * Important for efficiency, but can introduce latency
1572 * at low packet rates if to_ms not set!
1573 */
1574 if(nonblock)
1575 mindata = 0;
1576 else
1577 mindata = 65536;
1578
1579 if (dag_set_stream_poll64(p->fd, pd->dag_stream,
1580 mindata, &maxwait, &poll) < 0) {
1581 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1582 errno, "dag_set_stream_poll64");
1583 return PCAP_ERROR;
1584 }
1585
1586 if (nonblock) {
1587 pd->dag_flags |= DAGF_NONBLOCK;
1588 } else {
1589 pd->dag_flags &= ~DAGF_NONBLOCK;
1590 }
1591 return (0);
1592 }
1593
1594 static int
1595 dag_get_datalink(pcap_t *p)
1596 {
1597 struct pcap_dag *pd = p->priv;
1598
1599 /*
1600 * There seems to be no trivial way to tell which ERF type(s) a Tx
1601 * stream would accept. Let's assume ERF_TYPE_ETH would work, which
1602 * in libpcap terms means using DLT_EN10MB.
1603 */
1604 if (TX_ONLY(pd->dag_stream))
1605 return (p->linktype = DLT_EN10MB);
1606
1607 int index=0, dlt_index=0;
1608 uint8_t types[255];
1609
1610 memset(types, 0, 255);
1611
1612 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
1613 pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
1614 errno, "malloc");
1615 return PCAP_ERROR;
1616 }
1617
1618 p->linktype = 0;
1619
1620 /* Get list of possible ERF types for this card */
1621 if (dag_get_stream_erf_types(p->fd, pd->dag_stream, types, 255) < 0) {
1622 pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
1623 errno, "dag_get_stream_erf_types");
1624 return PCAP_ERROR;
1625 }
1626
1627 while (types[index]) {
1628
1629 switch((types[index] & ERF_TYPE_MASK)) {
1630
1631 case ERF_TYPE_HDLC_POS:
1632 case ERF_TYPE_COLOR_HDLC_POS:
1633 case ERF_TYPE_DSM_COLOR_HDLC_POS:
1634 case ERF_TYPE_COLOR_HASH_POS:
1635 p->dlt_list[dlt_index++] = DLT_CHDLC;
1636 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1637 p->dlt_list[dlt_index++] = DLT_FRELAY;
1638 if(!p->linktype)
1639 p->linktype = DLT_CHDLC;
1640 break;
1641
1642 case ERF_TYPE_ETH:
1643 case ERF_TYPE_COLOR_ETH:
1644 case ERF_TYPE_DSM_COLOR_ETH:
1645 case ERF_TYPE_COLOR_HASH_ETH:
1646 /*
1647 * This is (presumably) a real Ethernet capture; give it a
1648 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
1649 * that an application can let you choose it, in case you're
1650 * capturing DOCSIS traffic that a Cisco Cable Modem
1651 * Termination System is putting out onto an Ethernet (it
1652 * doesn't put an Ethernet header onto the wire, it puts raw
1653 * DOCSIS frames out on the wire inside the low-level
1654 * Ethernet framing).
1655 */
1656 p->dlt_list[dlt_index++] = DLT_EN10MB;
1657 p->dlt_list[dlt_index++] = DLT_DOCSIS;
1658 if(!p->linktype)
1659 p->linktype = DLT_EN10MB;
1660 break;
1661
1662 case ERF_TYPE_ATM:
1663 case ERF_TYPE_AAL5:
1664 case ERF_TYPE_MC_ATM:
1665 case ERF_TYPE_MC_AAL5:
1666 p->dlt_list[dlt_index++] = DLT_ATM_RFC1483;
1667 p->dlt_list[dlt_index++] = DLT_SUNATM;
1668 if(!p->linktype)
1669 p->linktype = DLT_ATM_RFC1483;
1670 break;
1671
1672 case ERF_TYPE_COLOR_MC_HDLC_POS:
1673 case ERF_TYPE_MC_HDLC:
1674 p->dlt_list[dlt_index++] = DLT_CHDLC;
1675 p->dlt_list[dlt_index++] = DLT_PPP_SERIAL;
1676 p->dlt_list[dlt_index++] = DLT_FRELAY;
1677 p->dlt_list[dlt_index++] = DLT_MTP2;
1678 p->dlt_list[dlt_index++] = DLT_MTP2_WITH_PHDR;
1679 p->dlt_list[dlt_index++] = DLT_LAPD;
1680 if(!p->linktype)
1681 p->linktype = DLT_CHDLC;
1682 break;
1683
1684 case ERF_TYPE_IPV4:
1685 p->dlt_list[dlt_index++] = DLT_RAW;
1686 p->dlt_list[dlt_index++] = DLT_IPV4;
1687 if(!p->linktype)
1688 p->linktype = DLT_RAW;
1689 break;
1690
1691 case ERF_TYPE_IPV6:
1692 p->dlt_list[dlt_index++] = DLT_RAW;
1693 p->dlt_list[dlt_index++] = DLT_IPV6;
1694 if(!p->linktype)
1695 p->linktype = DLT_RAW;
1696 break;
1697
1698 case ERF_TYPE_LEGACY:
1699 case ERF_TYPE_MC_RAW:
1700 case ERF_TYPE_MC_RAW_CHANNEL:
1701 case ERF_TYPE_IP_COUNTER:
1702 case ERF_TYPE_TCP_FLOW_COUNTER:
1703 case ERF_TYPE_INFINIBAND:
1704 case ERF_TYPE_RAW_LINK:
1705 case ERF_TYPE_INFINIBAND_LINK:
1706 case ERF_TYPE_META:
1707 default:
1708 /* Libpcap cannot deal with these types yet */
1709 /* Add no 'native' DLTs, but still covered by DLT_ERF */
1710 break;
1711
1712 } /* switch */
1713 index++;
1714 }
1715
1716 p->dlt_list[dlt_index++] = DLT_ERF;
1717
1718 p->dlt_count = dlt_index;
1719
1720 if(!p->linktype)
1721 p->linktype = DLT_ERF;
1722
1723 return p->linktype;
1724 }
1725
1726 #ifdef DAG_ONLY
1727 /*
1728 * This libpcap build supports only DAG cards, not regular network
1729 * interfaces.
1730 */
1731
1732 /*
1733 * There are no regular interfaces, just DAG interfaces.
1734 */
1735 int
1736 pcapint_platform_finddevs(pcap_if_list_t *devlistp _U_, char *errbuf _U_)
1737 {
1738 return (0);
1739 }
1740
1741 /*
1742 * Attempts to open a regular interface fail.
1743 */
1744 pcap_t *
1745 pcapint_create_interface(const char *device _U_, char *errbuf)
1746 {
1747 snprintf(errbuf, PCAP_ERRBUF_SIZE, PCAP_ENODEV_MESSAGE, "DAG");
1748 return NULL;
1749 }
1750
1751 /*
1752 * Libpcap version string.
1753 */
1754 const char *
1755 pcap_lib_version(void)
1756 {
1757 return (PCAP_VERSION_STRING " (DAG-only)");
1758 }
1759 #endif