Deleted Added
full compact
pcap-dag.c (162012) pcap-dag.c (172677)
1/*
2 * pcap-dag.c: Packet capture interface for Endace DAG card.
3 *
4 * The functionality of this code attempts to mimic that of pcap-linux as much
5 * as possible. This code is compiled in several different ways depending on
6 * whether DAG_ONLY and HAVE_DAG_API are defined. If HAVE_DAG_API is not
7 * defined it should not get compiled in, otherwise if DAG_ONLY is defined then
8 * the 'dag_' function calls are renamed to 'pcap_' equivalents. If DAG_ONLY
9 * is not defined then nothing is altered - the dag_ functions will be
10 * called as required from their pcap-linux/bpf equivalents.
11 *
12 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
13 * Modifications: Jesper Peterson <support@endace.com>
14 * Koryn Grant <support@endace.com>
15 * Stephen Donnelly <support@endace.com>
16 */
17
18#ifndef lint
19static const char rcsid[] _U_ =
1/*
2 * pcap-dag.c: Packet capture interface for Endace DAG card.
3 *
4 * The functionality of this code attempts to mimic that of pcap-linux as much
5 * as possible. This code is compiled in several different ways depending on
6 * whether DAG_ONLY and HAVE_DAG_API are defined. If HAVE_DAG_API is not
7 * defined it should not get compiled in, otherwise if DAG_ONLY is defined then
8 * the 'dag_' function calls are renamed to 'pcap_' equivalents. If DAG_ONLY
9 * is not defined then nothing is altered - the dag_ functions will be
10 * called as required from their pcap-linux/bpf equivalents.
11 *
12 * Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
13 * Modifications: Jesper Peterson <support@endace.com>
14 * Koryn Grant <support@endace.com>
15 * Stephen Donnelly <support@endace.com>
16 */
17
18#ifndef lint
19static const char rcsid[] _U_ =
20 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.21.2.3 2005/07/10 22:09:34 guy Exp $ (LBL)";
20 "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.21.2.7 2007/06/22 06:43:58 guy Exp $ (LBL)";
21#endif
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <sys/param.h> /* optionally get BSD define */
28

--- 12 unchanged lines hidden (view full) ---

41
42struct mbuf; /* Squelch compiler warnings on some platforms for */
43struct rtentry; /* declarations in <net/if.h> */
44#include <net/if.h>
45
46#include "dagnew.h"
47#include "dagapi.h"
48
21#endif
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <sys/param.h> /* optionally get BSD define */
28

--- 12 unchanged lines hidden (view full) ---

41
42struct mbuf; /* Squelch compiler warnings on some platforms for */
43struct rtentry; /* declarations in <net/if.h> */
44#include <net/if.h>
45
46#include "dagnew.h"
47#include "dagapi.h"
48
49#define MIN_DAG_SNAPLEN 12
50#define MAX_DAG_SNAPLEN 2040
51#define ATM_CELL_SIZE 52
52#define ATM_HDR_SIZE 4
53
49#define ATM_CELL_SIZE 52
50#define ATM_HDR_SIZE 4
51
52/*
53 * A header containing additional MTP information.
54 */
55#define MTP2_SENT_OFFSET 0 /* 1 byte */
56#define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */
57#define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */
58#define MTP2_HDR_LEN 4 /* length of the header */
59
60#define MTP2_ANNEX_A_NOT_USED 0
61#define MTP2_ANNEX_A_USED 1
62#define MTP2_ANNEX_A_USED_UNKNOWN 2
63
54/* SunATM pseudo header */
55struct sunatm_hdr {
56 unsigned char flags; /* destination and traffic type */
57 unsigned char vpi; /* VPI */
58 unsigned short vci; /* VCI */
59};
60
61typedef struct pcap_dag_node {
62 struct pcap_dag_node *next;
63 pcap_t *p;
64 pid_t pid;
65} pcap_dag_node_t;
66
67static pcap_dag_node_t *pcap_dags = NULL;
68static int atexit_handler_installed = 0;
69static const unsigned short endian_test_word = 0x0100;
70
71#define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
72
64/* SunATM pseudo header */
65struct sunatm_hdr {
66 unsigned char flags; /* destination and traffic type */
67 unsigned char vpi; /* VPI */
68 unsigned short vci; /* VCI */
69};
70
71typedef struct pcap_dag_node {
72 struct pcap_dag_node *next;
73 pcap_t *p;
74 pid_t pid;
75} pcap_dag_node_t;
76
77static pcap_dag_node_t *pcap_dags = NULL;
78static int atexit_handler_installed = 0;
79static const unsigned short endian_test_word = 0x0100;
80
81#define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word))
82
73/*
74 * Swap byte ordering of unsigned long long timestamp on a big endian
75 * machine.
76 */
77#define SWAP_TS(ull) ((ull & 0xff00000000000000LL) >> 56) | \
78 ((ull & 0x00ff000000000000LL) >> 40) | \
79 ((ull & 0x0000ff0000000000LL) >> 24) | \
80 ((ull & 0x000000ff00000000LL) >> 8) | \
81 ((ull & 0x00000000ff000000LL) << 8) | \
82 ((ull & 0x0000000000ff0000LL) << 24) | \
83 ((ull & 0x000000000000ff00LL) << 40) | \
84 ((ull & 0x00000000000000ffLL) << 56)
85
83
86
87#ifdef DAG_ONLY
88/* This code is required when compiling for a DAG device only. */
89#include "pcap-dag.h"
90
91/* Replace dag function names with pcap equivalent. */
92#define dag_open_live pcap_open_live
93#define dag_platform_finddevs pcap_platform_finddevs
94#endif /* DAG_ONLY */
95
84#ifdef DAG_ONLY
85/* This code is required when compiling for a DAG device only. */
86#include "pcap-dag.h"
87
88/* Replace dag function names with pcap equivalent. */
89#define dag_open_live pcap_open_live
90#define dag_platform_finddevs pcap_platform_finddevs
91#endif /* DAG_ONLY */
92
93#define MAX_DAG_PACKET 65536
94
95static unsigned char TempPkt[MAX_DAG_PACKET];
96
96static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
97static int dag_stats(pcap_t *p, struct pcap_stat *ps);
98static int dag_set_datalink(pcap_t *p, int dlt);
99static int dag_get_datalink(pcap_t *p);
100static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf);
101
102static void
103delete_pcap_dag(pcap_t *p)

--- 30 unchanged lines hidden (view full) ---

134 if(dag_detach_stream(p->fd, p->md.dag_stream) < 0)
135 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
136#else
137 if(dag_stop(p->fd) < 0)
138 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
139#endif /* HAVE_DAG_STREAMS_API */
140 if(dag_close(p->fd) < 0)
141 fprintf(stderr,"dag_close: %s\n", strerror(errno));
97static int dag_setfilter(pcap_t *p, struct bpf_program *fp);
98static int dag_stats(pcap_t *p, struct pcap_stat *ps);
99static int dag_set_datalink(pcap_t *p, int dlt);
100static int dag_get_datalink(pcap_t *p);
101static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf);
102
103static void
104delete_pcap_dag(pcap_t *p)

--- 30 unchanged lines hidden (view full) ---

135 if(dag_detach_stream(p->fd, p->md.dag_stream) < 0)
136 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
137#else
138 if(dag_stop(p->fd) < 0)
139 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
140#endif /* HAVE_DAG_STREAMS_API */
141 if(dag_close(p->fd) < 0)
142 fprintf(stderr,"dag_close: %s\n", strerror(errno));
142#ifdef linux
143 free(p->md.device);
144#endif
145 }
146 delete_pcap_dag(p);
147 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
148}
149
150static void
151atexit_handler(void)
152{

--- 64 unchanged lines hidden (view full) ---

217 * We wait for 64kB because processing a few packets at a time
218 * can cause problems at high packet rates (>200kpps) due
219 * to inefficiencies.
220 * This does mean if to_ms is not specified the capture may 'hang'
221 * for long periods if the data rate is extremely slow (<64kB/sec)
222 * If non-block is specified it will return immediately. The user
223 * is then responsible for efficiency.
224 */
143 }
144 delete_pcap_dag(p);
145 /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
146}
147
148static void
149atexit_handler(void)
150{

--- 64 unchanged lines hidden (view full) ---

215 * We wait for 64kB because processing a few packets at a time
216 * can cause problems at high packet rates (>200kpps) due
217 * to inefficiencies.
218 * This does mean if to_ms is not specified the capture may 'hang'
219 * for long periods if the data rate is extremely slow (<64kB/sec)
220 * If non-block is specified it will return immediately. The user
221 * is then responsible for efficiency.
222 */
225 p->md.dag_mem_top = dag_advance_stream(p->fd, p->md.dag_stream, (void**)&(p->md.dag_mem_bottom));
223 p->md.dag_mem_top = dag_advance_stream(p->fd, p->md.dag_stream, &(p->md.dag_mem_bottom));
226#else
227 /* dag_offset does not support timeouts */
228 p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), flags);
229#endif /* HAVE_DAG_STREAMS_API */
230
231 if (nonblocking && (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size))
232 {
233 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */

--- 43 unchanged lines hidden (view full) ---

277 if (rlen < dag_record_size)
278 {
279 strncpy(p->errbuf, "dag_read: record too small", PCAP_ERRBUF_SIZE);
280 return -1;
281 }
282 p->md.dag_mem_bottom += rlen;
283
284 switch(header->type) {
224#else
225 /* dag_offset does not support timeouts */
226 p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), flags);
227#endif /* HAVE_DAG_STREAMS_API */
228
229 if (nonblocking && (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size))
230 {
231 /* Pcap is configured to process only available packets, and there aren't any, return immediately. */

--- 43 unchanged lines hidden (view full) ---

275 if (rlen < dag_record_size)
276 {
277 strncpy(p->errbuf, "dag_read: record too small", PCAP_ERRBUF_SIZE);
278 return -1;
279 }
280 p->md.dag_mem_bottom += rlen;
281
282 switch(header->type) {
285 case TYPE_AAL5:
286 case TYPE_ATM:
283 case TYPE_ATM:
284#ifdef TYPE_AAL5
285 case TYPE_AAL5:
286 if (header->type == TYPE_AAL5) {
287 packet_len = ntohs(header->wlen);
288 caplen = rlen - dag_record_size;
289 }
290#endif
287#ifdef TYPE_MC_ATM
288 case TYPE_MC_ATM:
289 if (header->type == TYPE_MC_ATM) {
290 caplen = packet_len = ATM_CELL_SIZE;
291 dp+=4;
292 }
293#endif
294#ifdef TYPE_MC_AAL5
295 case TYPE_MC_AAL5:
296 if (header->type == TYPE_MC_AAL5) {
297 packet_len = ntohs(header->wlen);
298 caplen = rlen - dag_record_size - 4;
299 dp+=4;
300 }
301#endif
291#ifdef TYPE_MC_ATM
292 case TYPE_MC_ATM:
293 if (header->type == TYPE_MC_ATM) {
294 caplen = packet_len = ATM_CELL_SIZE;
295 dp+=4;
296 }
297#endif
298#ifdef TYPE_MC_AAL5
299 case TYPE_MC_AAL5:
300 if (header->type == TYPE_MC_AAL5) {
301 packet_len = ntohs(header->wlen);
302 caplen = rlen - dag_record_size - 4;
303 dp+=4;
304 }
305#endif
302 if (header->type == TYPE_AAL5) {
303 packet_len = ntohs(header->wlen);
304 caplen = rlen - dag_record_size;
305 } else if(header->type == TYPE_ATM) {
306 if (header->type == TYPE_ATM) {
306 caplen = packet_len = ATM_CELL_SIZE;
307 }
308 if (p->linktype == DLT_SUNATM) {
309 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
310 unsigned long rawatm;
311
312 rawatm = ntohl(*((unsigned long *)dp));
313 sunatm->vci = htons((rawatm >> 4) & 0xffff);

--- 7 unchanged lines hidden (view full) ---

321
322 } else {
323 packet_len -= ATM_HDR_SIZE;
324 caplen -= ATM_HDR_SIZE;
325 dp += ATM_HDR_SIZE;
326 }
327 break;
328
307 caplen = packet_len = ATM_CELL_SIZE;
308 }
309 if (p->linktype == DLT_SUNATM) {
310 struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
311 unsigned long rawatm;
312
313 rawatm = ntohl(*((unsigned long *)dp));
314 sunatm->vci = htons((rawatm >> 4) & 0xffff);

--- 7 unchanged lines hidden (view full) ---

322
323 } else {
324 packet_len -= ATM_HDR_SIZE;
325 caplen -= ATM_HDR_SIZE;
326 dp += ATM_HDR_SIZE;
327 }
328 break;
329
330#ifdef TYPE_DSM_COLOR_ETH
331 case TYPE_DSM_COLOR_ETH:
332#endif
329#ifdef TYPE_COLOR_ETH
330 case TYPE_COLOR_ETH:
331#endif
332 case TYPE_ETH:
333 packet_len = ntohs(header->wlen);
334 packet_len -= (p->md.dag_fcs_bits >> 3);
335 caplen = rlen - dag_record_size - 2;
336 if (caplen > packet_len) {
337 caplen = packet_len;
338 }
339 dp += 2;
340 break;
333#ifdef TYPE_COLOR_ETH
334 case TYPE_COLOR_ETH:
335#endif
336 case TYPE_ETH:
337 packet_len = ntohs(header->wlen);
338 packet_len -= (p->md.dag_fcs_bits >> 3);
339 caplen = rlen - dag_record_size - 2;
340 if (caplen > packet_len) {
341 caplen = packet_len;
342 }
343 dp += 2;
344 break;
345#ifdef TYPE_DSM_COLOR_HDLC_POS
346 case TYPE_DSM_COLOR_HDLC_POS:
347#endif
341#ifdef TYPE_COLOR_HDLC_POS
342 case TYPE_COLOR_HDLC_POS:
343#endif
344 case TYPE_HDLC_POS:
345 packet_len = ntohs(header->wlen);
346 packet_len -= (p->md.dag_fcs_bits >> 3);
347 caplen = rlen - dag_record_size;
348 if (caplen > packet_len) {
349 caplen = packet_len;
350 }
351 break;
348#ifdef TYPE_COLOR_HDLC_POS
349 case TYPE_COLOR_HDLC_POS:
350#endif
351 case TYPE_HDLC_POS:
352 packet_len = ntohs(header->wlen);
353 packet_len -= (p->md.dag_fcs_bits >> 3);
354 caplen = rlen - dag_record_size;
355 if (caplen > packet_len) {
356 caplen = packet_len;
357 }
358 break;
359#ifdef TYPE_COLOR_MC_HDLC_POS
360 case TYPE_COLOR_MC_HDLC_POS:
361#endif
352#ifdef TYPE_MC_HDLC
353 case TYPE_MC_HDLC:
354 packet_len = ntohs(header->wlen);
355 packet_len -= (p->md.dag_fcs_bits >> 3);
356 caplen = rlen - dag_record_size - 4;
357 if (caplen > packet_len) {
358 caplen = packet_len;
359 }
362#ifdef TYPE_MC_HDLC
363 case TYPE_MC_HDLC:
364 packet_len = ntohs(header->wlen);
365 packet_len -= (p->md.dag_fcs_bits >> 3);
366 caplen = rlen - dag_record_size - 4;
367 if (caplen > packet_len) {
368 caplen = packet_len;
369 }
370 /* jump the MC_HDLC_HEADER */
360 dp += 4;
371 dp += 4;
372 if (p->linktype == DLT_MTP2_WITH_PHDR) {
373 /* Add the MTP2 Pseudo Header */
374 caplen += MTP2_HDR_LEN;
375 packet_len += MTP2_HDR_LEN;
376
377 TempPkt[MTP2_SENT_OFFSET] = 0;
378 TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN;
379 *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01);
380 *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff);
381 memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen);
382 dp = TempPkt;
383 }
361 break;
362#endif
384 break;
385#endif
386 default:
387 /* Unhandled ERF type.
388 * Ignore rather than generating error
389 */
390 continue;
363 }
364
365 if (caplen > p->snapshot)
366 caplen = p->snapshot;
367
368 /* Count lost packets. */
369 switch(header->type) {
370#ifdef TYPE_COLOR_HDLC_POS
371 /* in this type the color value overwrites the lctr */
372 case TYPE_COLOR_HDLC_POS:
373 break;
374#endif
375#ifdef TYPE_COLOR_ETH
376 /* in this type the color value overwrites the lctr */
377 case TYPE_COLOR_ETH:
378 break;
379#endif
391 }
392
393 if (caplen > p->snapshot)
394 caplen = p->snapshot;
395
396 /* Count lost packets. */
397 switch(header->type) {
398#ifdef TYPE_COLOR_HDLC_POS
399 /* in this type the color value overwrites the lctr */
400 case TYPE_COLOR_HDLC_POS:
401 break;
402#endif
403#ifdef TYPE_COLOR_ETH
404 /* in this type the color value overwrites the lctr */
405 case TYPE_COLOR_ETH:
406 break;
407#endif
408#ifdef TYPE_DSM_COLOR_HDLC_POS
409 /* in this type the color value overwrites the lctr */
410 case TYPE_DSM_COLOR_HDLC_POS:
411 break;
412#endif
413#ifdef TYPE_DSM_COLOR_ETH
414 /* in this type the color value overwrites the lctr */
415 case TYPE_DSM_COLOR_ETH:
416 break;
417#endif
418#ifdef TYPE_COLOR_MC_HDLC_POS
419 case TYPE_COLOR_MC_HDLC_POS:
420 break;
421#endif
422
380 default:
381 if (header->lctr) {
382 if (p->md.stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) {
383 p->md.stat.ps_drop = UINT_MAX;
384 } else {
385 p->md.stat.ps_drop += ntohs(header->lctr);
386 }
387 }
388 }
389
390 /* Run the packet filter if there is one. */
391 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
392
393 /* convert between timestamp formats */
394 register unsigned long long ts;
395
396 if (IS_BIGENDIAN()) {
423 default:
424 if (header->lctr) {
425 if (p->md.stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) {
426 p->md.stat.ps_drop = UINT_MAX;
427 } else {
428 p->md.stat.ps_drop += ntohs(header->lctr);
429 }
430 }
431 }
432
433 /* Run the packet filter if there is one. */
434 if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) {
435
436 /* convert between timestamp formats */
437 register unsigned long long ts;
438
439 if (IS_BIGENDIAN()) {
397 ts = SWAP_TS(header->ts);
440 ts = SWAPLL(header->ts);
398 } else {
399 ts = header->ts;
400 }
401
402 pcap_header.ts.tv_sec = ts >> 32;
403 ts = (ts & 0xffffffffULL) * 1000000;
404 ts += 0x80000000; /* rounding */
405 pcap_header.ts.tv_usec = ts >> 32;

--- 34 unchanged lines hidden (view full) ---

440}
441
442/*
443 * Get a handle for a live capture from the given DAG device. Passing a NULL
444 * device will result in a failure. The promisc flag is ignored because DAG
445 * cards are always promiscuous. The to_ms parameter is also ignored as it is
446 * not supported in hardware.
447 *
441 } else {
442 ts = header->ts;
443 }
444
445 pcap_header.ts.tv_sec = ts >> 32;
446 ts = (ts & 0xffffffffULL) * 1000000;
447 ts += 0x80000000; /* rounding */
448 pcap_header.ts.tv_usec = ts >> 32;

--- 34 unchanged lines hidden (view full) ---

483}
484
485/*
486 * Get a handle for a live capture from the given DAG device. Passing a NULL
487 * device will result in a failure. The promisc flag is ignored because DAG
488 * cards are always promiscuous. The to_ms parameter is also ignored as it is
489 * not supported in hardware.
490 *
491 * snaplen is now also ignored, until we get per-stream slen support. Set
492 * slen with approprite DAG tool BEFORE pcap_open_live().
493 *
448 * See also pcap(3).
449 */
450pcap_t *
451dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf)
452{
453 char conf[30]; /* dag configure string */
454 pcap_t *handle;
455 char *s;
456 int n;
457 daginf_t* daginf;
494 * See also pcap(3).
495 */
496pcap_t *
497dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf)
498{
499 char conf[30]; /* dag configure string */
500 pcap_t *handle;
501 char *s;
502 int n;
503 daginf_t* daginf;
458 char * newDev;
504 char * newDev = NULL;
459#ifdef HAVE_DAG_STREAMS_API
460 uint32_t mindata;
461 struct timeval maxwait;
462 struct timeval poll;
463#endif
464
465 if (device == NULL) {
466 snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));

--- 6 unchanged lines hidden (view full) ---

473 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
474 return NULL;
475 }
476
477 /* Initialize some components of the pcap structure. */
478
479 memset(handle, 0, sizeof(*handle));
480
505#ifdef HAVE_DAG_STREAMS_API
506 uint32_t mindata;
507 struct timeval maxwait;
508 struct timeval poll;
509#endif
510
511 if (device == NULL) {
512 snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno));

--- 6 unchanged lines hidden (view full) ---

519 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno));
520 return NULL;
521 }
522
523 /* Initialize some components of the pcap structure. */
524
525 memset(handle, 0, sizeof(*handle));
526
481 newDev = (char *)malloc(strlen(device) + 16);
482
483#ifdef HAVE_DAG_STREAMS_API
527#ifdef HAVE_DAG_STREAMS_API
528 newDev = (char *)malloc(strlen(device) + 16);
529 if (newDev == NULL) {
530 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
531 goto fail;
532 }
484
485 /* Parse input name to get dag device and stream number if provided */
486 if (dag_parse_name(device, newDev, strlen(device) + 16, &handle->md.dag_stream) < 0) {
487 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s\n", pcap_strerror(errno));
488 goto fail;
489 }
490 device = newDev;
491
492 if (handle->md.dag_stream%2) {
493 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
494 goto fail;
495 }
496#else
533
534 /* Parse input name to get dag device and stream number if provided */
535 if (dag_parse_name(device, newDev, strlen(device) + 16, &handle->md.dag_stream) < 0) {
536 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s\n", pcap_strerror(errno));
537 goto fail;
538 }
539 device = newDev;
540
541 if (handle->md.dag_stream%2) {
542 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture\n");
543 goto fail;
544 }
545#else
497 if (strstr(device, "/dev") == NULL) {
498 newDev[0] = '\0';
499 strcat(newDev, "/dev/");
500 strcat(newDev,device);
546 if (strncmp(device, "/dev/", 5) != 0) {
547 newDev = (char *)malloc(strlen(device) + 5);
548 if (newDev == NULL) {
549 snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s\n", pcap_strerror(errno));
550 goto fail;
551 }
552 strcpy(newDev, "/dev/");
553 strcat(newDev, device);
501 device = newDev;
554 device = newDev;
502 } else {
503 device = strdup(device);
504 }
555 }
505
506 if (device == NULL) {
507 snprintf(ebuf, PCAP_ERRBUF_SIZE, "str_dup: %s\n", pcap_strerror(errno));
508 goto fail;
509 }
510#endif /* HAVE_DAG_STREAMS_API */
511
512 /* setup device parameters */
513 if((handle->fd = dag_open((char *)device)) < 0) {
514 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
515 goto fail;
516 }
517
518#ifdef HAVE_DAG_STREAMS_API
519 /* Open requested stream. Can fail if already locked or on error */
520 if (dag_attach_stream(handle->fd, handle->md.dag_stream, 0, 0) < 0) {
521 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s\n", pcap_strerror(errno));
556#endif /* HAVE_DAG_STREAMS_API */
557
558 /* setup device parameters */
559 if((handle->fd = dag_open((char *)device)) < 0) {
560 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno));
561 goto fail;
562 }
563
564#ifdef HAVE_DAG_STREAMS_API
565 /* Open requested stream. Can fail if already locked or on error */
566 if (dag_attach_stream(handle->fd, handle->md.dag_stream, 0, 0) < 0) {
567 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s\n", pcap_strerror(errno));
522 goto fail;
568 goto failclose;
523 }
524
525 /* Set up default poll parameters for stream
526 * Can be overridden by pcap_set_nonblock()
527 */
528 if (dag_get_stream_poll(handle->fd, handle->md.dag_stream,
529 &mindata, &maxwait, &poll) < 0) {
530 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
569 }
570
571 /* Set up default poll parameters for stream
572 * Can be overridden by pcap_set_nonblock()
573 */
574 if (dag_get_stream_poll(handle->fd, handle->md.dag_stream,
575 &mindata, &maxwait, &poll) < 0) {
576 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s\n", pcap_strerror(errno));
531 goto fail;
577 goto faildetach;
532 }
533
534 /* Amount of data to collect in Bytes before calling callbacks.
535 * Important for efficiency, but can introduce latency
536 * at low packet rates if to_ms not set!
537 */
538 mindata = 65536;
539
540 /* Obey to_ms if supplied. This is a good idea!
541 * Recommend 10-100ms. Calls will time out even if no data arrived.
542 */
543 maxwait.tv_sec = to_ms/1000;
544 maxwait.tv_usec = (to_ms%1000) * 1000;
545
546 if (dag_set_stream_poll(handle->fd, handle->md.dag_stream,
547 mindata, &maxwait, &poll) < 0) {
548 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
578 }
579
580 /* Amount of data to collect in Bytes before calling callbacks.
581 * Important for efficiency, but can introduce latency
582 * at low packet rates if to_ms not set!
583 */
584 mindata = 65536;
585
586 /* Obey to_ms if supplied. This is a good idea!
587 * Recommend 10-100ms. Calls will time out even if no data arrived.
588 */
589 maxwait.tv_sec = to_ms/1000;
590 maxwait.tv_usec = (to_ms%1000) * 1000;
591
592 if (dag_set_stream_poll(handle->fd, handle->md.dag_stream,
593 mindata, &maxwait, &poll) < 0) {
594 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s\n", pcap_strerror(errno));
549 goto fail;
595 goto faildetach;
550 }
551
552#else
553 if((handle->md.dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
554 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
596 }
597
598#else
599 if((handle->md.dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) {
600 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno));
555 goto fail;
601 goto failclose;
556 }
557
558#endif /* HAVE_DAG_STREAMS_API */
559
602 }
603
604#endif /* HAVE_DAG_STREAMS_API */
605
606 /* XXX Not calling dag_configure() to set slen; this is unsafe in
607 * multi-stream environments as the gpp config is global.
608 * Once the firmware provides 'per-stream slen' this can be supported
609 * again via the Config API without side-effects */
610#if 0
560 /* set the card snap length to the specified snaplen parameter */
611 /* set the card snap length to the specified snaplen parameter */
612 /* This is a really bad idea, as different cards have different
613 * valid slen ranges. Should fix in Config API. */
561 if (snaplen == 0 || snaplen > MAX_DAG_SNAPLEN) {
562 snaplen = MAX_DAG_SNAPLEN;
563 } else if (snaplen < MIN_DAG_SNAPLEN) {
564 snaplen = MIN_DAG_SNAPLEN;
565 }
566 /* snap len has to be a multiple of 4 */
567 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
568
569 if(dag_configure(handle->fd, conf) < 0) {
570 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
614 if (snaplen == 0 || snaplen > MAX_DAG_SNAPLEN) {
615 snaplen = MAX_DAG_SNAPLEN;
616 } else if (snaplen < MIN_DAG_SNAPLEN) {
617 snaplen = MIN_DAG_SNAPLEN;
618 }
619 /* snap len has to be a multiple of 4 */
620 snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
621
622 if(dag_configure(handle->fd, conf) < 0) {
623 snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
571 goto fail;
624 goto faildetach;
572 }
625 }
573
626#endif
627
574#ifdef HAVE_DAG_STREAMS_API
575 if(dag_start_stream(handle->fd, handle->md.dag_stream) < 0) {
576 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s\n", device, pcap_strerror(errno));
628#ifdef HAVE_DAG_STREAMS_API
629 if(dag_start_stream(handle->fd, handle->md.dag_stream) < 0) {
630 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s\n", device, pcap_strerror(errno));
577 goto fail;
631 goto faildetach;
578 }
579#else
580 if(dag_start(handle->fd) < 0) {
581 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
632 }
633#else
634 if(dag_start(handle->fd) < 0) {
635 snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno));
582 goto fail;
636 goto failclose;
583 }
584#endif /* HAVE_DAG_STREAMS_API */
585
586 /*
587 * Important! You have to ensure bottom is properly
588 * initialized to zero on startup, it won't give you
589 * a compiler warning if you make this mistake!
590 */

--- 11 unchanged lines hidden (view full) ---

602
603 /* Then allow an environment variable to override. */
604 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
605 if ((n = atoi(s)) == 0 || n == 16|| n == 32) {
606 handle->md.dag_fcs_bits = n;
607 } else {
608 snprintf(ebuf, PCAP_ERRBUF_SIZE,
609 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
637 }
638#endif /* HAVE_DAG_STREAMS_API */
639
640 /*
641 * Important! You have to ensure bottom is properly
642 * initialized to zero on startup, it won't give you
643 * a compiler warning if you make this mistake!
644 */

--- 11 unchanged lines hidden (view full) ---

656
657 /* Then allow an environment variable to override. */
658 if ((s = getenv("ERF_FCS_BITS")) != NULL) {
659 if ((n = atoi(s)) == 0 || n == 16|| n == 32) {
660 handle->md.dag_fcs_bits = n;
661 } else {
662 snprintf(ebuf, PCAP_ERRBUF_SIZE,
663 "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n);
610 goto fail;
664 goto failstop;
611 }
612 }
613
614 handle->snapshot = snaplen;
615 handle->md.dag_timeout = to_ms;
616
617 handle->linktype = -1;
618 if (dag_get_datalink(handle) < 0) {
619 strcpy(ebuf, handle->errbuf);
665 }
666 }
667
668 handle->snapshot = snaplen;
669 handle->md.dag_timeout = to_ms;
670
671 handle->linktype = -1;
672 if (dag_get_datalink(handle) < 0) {
673 strcpy(ebuf, handle->errbuf);
620 goto fail;
674 goto failstop;
621 }
622
623 handle->bufsize = 0;
624
625 if (new_pcap_dag(handle) < 0) {
626 snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
675 }
676
677 handle->bufsize = 0;
678
679 if (new_pcap_dag(handle) < 0) {
680 snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno));
627 goto fail;
681 goto failstop;
628 }
629
630 /*
631 * "select()" and "poll()" don't work on DAG device descriptors.
632 */
633 handle->selectable_fd = -1;
634
682 }
683
684 /*
685 * "select()" and "poll()" don't work on DAG device descriptors.
686 */
687 handle->selectable_fd = -1;
688
635#ifdef linux
636 handle->md.device = (char *)device;
637 handle->md.timeout = to_ms;
638#else
639 free((char *)device);
640 device = NULL;
641#endif
689 if (newDev != NULL) {
690 free((char *)newDev);
691 }
642
643 handle->read_op = dag_read;
644 handle->inject_op = dag_inject;
645 handle->setfilter_op = dag_setfilter;
646 handle->setdirection_op = NULL; /* Not implemented.*/
647 handle->set_datalink_op = dag_set_datalink;
648 handle->getnonblock_op = pcap_getnonblock_fd;
649 handle->setnonblock_op = dag_setnonblock;
650 handle->stats_op = dag_stats;
651 handle->close_op = dag_platform_close;
692
693 handle->read_op = dag_read;
694 handle->inject_op = dag_inject;
695 handle->setfilter_op = dag_setfilter;
696 handle->setdirection_op = NULL; /* Not implemented.*/
697 handle->set_datalink_op = dag_set_datalink;
698 handle->getnonblock_op = pcap_getnonblock_fd;
699 handle->setnonblock_op = dag_setnonblock;
700 handle->stats_op = dag_stats;
701 handle->close_op = dag_platform_close;
652
702 handle->md.stat.ps_drop = 0;
703 handle->md.stat.ps_recv = 0;
653 return handle;
654
704 return handle;
705
706#ifdef HAVE_DAG_STREAMS_API
707failstop:
708 if (handle != NULL) {
709 if (dag_stop_stream(handle->fd, handle->md.dag_stream) < 0)
710 fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno));
711 }
712
713faildetach:
714 if (handle != NULL) {
715 if (dag_detach_stream(handle->fd, handle->md.dag_stream) < 0)
716 fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno));
717 }
718#else
719failstop:
720 if (handle != NULL) {
721 if (dag_stop(p->fd) < 0)
722 fprintf(stderr,"dag_stop: %s\n", strerror(errno));
723 }
724#endif /* HAVE_DAG_STREAMS_API */
725
726failclose:
727 if (handle != NULL) {
728 if (dag_close(handle->fd) < 0)
729 fprintf(stderr,"dag_close: %s\n", strerror(errno));
730 }
731 if (handle != NULL)
732 delete_pcap_dag(handle);
733
655fail:
656 if (newDev != NULL) {
657 free((char *)newDev);
658 }
659 if (handle != NULL) {
660 /*
661 * Get rid of any link-layer type list we allocated.
662 */

--- 138 unchanged lines hidden (view full) ---

801 p->md.dag_offset_flags &= ~DAGF_NONBLOCK;
802 }
803 return (0);
804}
805
806static int
807dag_get_datalink(pcap_t *p)
808{
734fail:
735 if (newDev != NULL) {
736 free((char *)newDev);
737 }
738 if (handle != NULL) {
739 /*
740 * Get rid of any link-layer type list we allocated.
741 */

--- 138 unchanged lines hidden (view full) ---

880 p->md.dag_offset_flags &= ~DAGF_NONBLOCK;
881 }
882 return (0);
883}
884
885static int
886dag_get_datalink(pcap_t *p)
887{
809 int daglinktype;
888 int index=0;
889 uint8_t types[255];
810
890
811 if (p->dlt_list == NULL && (p->dlt_list = malloc(2*sizeof(*(p->dlt_list)))) == NULL) {
891 memset(types, 0, 255);
892
893 if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) {
812 (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
813 return (-1);
814 }
815
894 (void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
895 return (-1);
896 }
897
898 p->linktype = 0;
899
900#ifdef HAVE_DAG_GET_ERF_TYPES
901 /* Get list of possible ERF types for this card */
902 if (dag_get_erf_types(p->fd, types, 255) < 0) {
903 snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno));
904 return (-1);
905 }
906
907 while (types[index]) {
908#else
816 /* Check the type through a dagapi call. */
909 /* Check the type through a dagapi call. */
817 daglinktype = dag_linktype(p->fd);
910 types[index] = dag_linktype(p->fd);
818
911
819 switch(daglinktype) {
912 {
913#endif
914 switch(types[index]) {
820
915
821 case TYPE_HDLC_POS:
822 case TYPE_COLOR_HDLC_POS:
823 if (p->dlt_list != NULL) {
824 p->dlt_count = 2;
825 p->dlt_list[0] = DLT_CHDLC;
826 p->dlt_list[1] = DLT_PPP_SERIAL;
827 p->dlt_list[2] = DLT_FRELAY;
828 }
829 p->linktype = DLT_CHDLC;
830 break;
916 case TYPE_HDLC_POS:
917#ifdef TYPE_COLOR_HDLC_POS
918 case TYPE_COLOR_HDLC_POS:
919#endif
920#ifdef TYPE_DSM_COLOR_HDLC_POS
921 case TYPE_DSM_COLOR_HDLC_POS:
922#endif
923 if (p->dlt_list != NULL) {
924 p->dlt_list[index++] = DLT_CHDLC;
925 p->dlt_list[index++] = DLT_PPP_SERIAL;
926 p->dlt_list[index++] = DLT_FRELAY;
927 }
928 if(!p->linktype)
929 p->linktype = DLT_CHDLC;
930 break;
831
931
832 case TYPE_ETH:
833 case TYPE_COLOR_ETH:
834 /*
835 * This is (presumably) a real Ethernet capture; give it a
836 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
837 * that an application can let you choose it, in case you're
838 * capturing DOCSIS traffic that a Cisco Cable Modem
839 * Termination System is putting out onto an Ethernet (it
840 * doesn't put an Ethernet header onto the wire, it puts raw
841 * DOCSIS frames out on the wire inside the low-level
842 * Ethernet framing).
843 */
844 if (p->dlt_list != NULL) {
845 p->dlt_count = 2;
846 p->dlt_list[0] = DLT_EN10MB;
847 p->dlt_list[1] = DLT_DOCSIS;
848 }
849 p->linktype = DLT_EN10MB;
850 break;
932 case TYPE_ETH:
933#ifdef TYPE_COLOR_ETH
934 case TYPE_COLOR_ETH:
935#endif
936#ifdef TYPE_DSM_COLOR_ETH
937 case TYPE_DSM_COLOR_ETH:
938#endif
939 /*
940 * This is (presumably) a real Ethernet capture; give it a
941 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
942 * that an application can let you choose it, in case you're
943 * capturing DOCSIS traffic that a Cisco Cable Modem
944 * Termination System is putting out onto an Ethernet (it
945 * doesn't put an Ethernet header onto the wire, it puts raw
946 * DOCSIS frames out on the wire inside the low-level
947 * Ethernet framing).
948 */
949 if (p->dlt_list != NULL) {
950 p->dlt_list[index++] = DLT_EN10MB;
951 p->dlt_list[index++] = DLT_DOCSIS;
952 }
953 if(!p->linktype)
954 p->linktype = DLT_EN10MB;
955 break;
851
956
852 case TYPE_AAL5:
853 case TYPE_ATM:
854 case TYPE_MC_ATM:
855 case TYPE_MC_AAL5:
856 if (p->dlt_list != NULL) {
857 p->dlt_count = 2;
858 p->dlt_list[0] = DLT_ATM_RFC1483;
859 p->dlt_list[1] = DLT_SUNATM;
860 }
861 p->linktype = DLT_ATM_RFC1483;
862 break;
957 case TYPE_ATM:
958#ifdef TYPE_AAL5
959 case TYPE_AAL5:
960#endif
961#ifdef TYPE_MC_ATM
962 case TYPE_MC_ATM:
963#endif
964#ifdef TYPE_MC_AAL5
965 case TYPE_MC_AAL5:
966#endif
967 if (p->dlt_list != NULL) {
968 p->dlt_list[index++] = DLT_ATM_RFC1483;
969 p->dlt_list[index++] = DLT_SUNATM;
970 }
971 if(!p->linktype)
972 p->linktype = DLT_ATM_RFC1483;
973 break;
863
974
864 case TYPE_MC_HDLC:
865 if (p->dlt_list != NULL) {
866 p->dlt_count = 4;
867 p->dlt_list[0] = DLT_CHDLC;
868 p->dlt_list[1] = DLT_PPP_SERIAL;
869 p->dlt_list[2] = DLT_FRELAY;
870 p->dlt_list[3] = DLT_MTP2;
871 }
872 p->linktype = DLT_CHDLC;
873 break;
975#ifdef TYPE_COLOR_MC_HDLC_POS
976 case TYPE_COLOR_MC_HDLC_POS:
977#endif
978#ifdef TYPE_MC_HDLC
979 case TYPE_MC_HDLC:
980 if (p->dlt_list != NULL) {
981 p->dlt_list[index++] = DLT_CHDLC;
982 p->dlt_list[index++] = DLT_PPP_SERIAL;
983 p->dlt_list[index++] = DLT_FRELAY;
984 p->dlt_list[index++] = DLT_MTP2;
985 p->dlt_list[index++] = DLT_MTP2_WITH_PHDR;
986 }
987 if(!p->linktype)
988 p->linktype = DLT_CHDLC;
989 break;
990#endif
874
991
875 case TYPE_LEGACY:
876 p->linktype = DLT_NULL;
877 break;
992 case TYPE_LEGACY:
993 if(!p->linktype)
994 p->linktype = DLT_NULL;
995 break;
878
996
879 default:
880 snprintf(p->errbuf, sizeof(p->errbuf), "unknown DAG linktype %d\n", daglinktype);
881 return (-1);
997 default:
998 snprintf(p->errbuf, sizeof(p->errbuf), "unknown DAG linktype %d", types[index]);
999 return (-1);
882
1000
1001 } /* switch */
883 }
884
1002 }
1003
1004 p->dlt_count = index;
1005
885 return p->linktype;
886}
1006 return p->linktype;
1007}