From Jesper Peterson:
- don't auto-detect HDLC DLT on serial links, use "pcap_set_datalink()" to choose the link-layer header - for ATM, allow selecting SUNATM rather than the default RFC1483 with "pcap_set_datalink()" - reformat and otherwise clean up the code.
This commit is contained in:
parent
f2101dbb5d
commit
fab5c982f2
209
pcap-dag.c
209
pcap-dag.c
|
@ -9,27 +9,13 @@
|
|||
* is not defined then nothing is altered - the dag_ functions will be
|
||||
* called as required from their pcap-linux/bpf equivalents.
|
||||
*
|
||||
* Author: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
|
||||
*
|
||||
* Modifications:
|
||||
* 2003 May - Jesper Peterson <support@endace.com>
|
||||
* Code shuffled around to suit fad-xxx.c structure
|
||||
* Added atexit() handler to stop DAG if application is too lazy
|
||||
* 2003 September - Koryn Grant <koryn@endace.com>
|
||||
* Added support for nonblocking operation.
|
||||
* Added support for processing more than a single packet in pcap_dispatch().
|
||||
* Fixed bug in loss counter code.
|
||||
* Improved portability of loss counter code (e.g. use UINT_MAX instead of 0xffff).
|
||||
* Removed unused local variables.
|
||||
* Added required headers (ctype.h, limits.h, unistd.h, netinet/in.h).
|
||||
* 2003 October - Koryn Grant <koryn@endace.com.>
|
||||
* Changed semantics to match those of standard pcap on linux.
|
||||
* - packets rejected by the filter are not counted.
|
||||
* Authors: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com)
|
||||
* Modifications: Jesper Peterson, Koryn Grant <support@endace.com>
|
||||
*/
|
||||
|
||||
#ifndef lint
|
||||
static const char rcsid[] _U_ =
|
||||
"@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.15 2003-12-18 23:32:31 guy Exp $ (LBL)";
|
||||
"@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.16 2004-01-05 18:18:54 guy Exp $ (LBL)";
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
|
@ -60,7 +46,15 @@ struct rtentry; /* declarations in <net/if.h> */
|
|||
|
||||
#define MIN_DAG_SNAPLEN 12
|
||||
#define MAX_DAG_SNAPLEN 2040
|
||||
#define ATM_SNAPLEN 48
|
||||
#define ATM_CELL_SIZE 52
|
||||
#define ATM_HDR_SIZE 4
|
||||
|
||||
/* SunATM pseudo header */
|
||||
struct sunatm_hdr {
|
||||
unsigned char flags; /* destination and traffic type */
|
||||
unsigned char vpi; /* VPI */
|
||||
unsigned short vci; /* VCI */
|
||||
};
|
||||
|
||||
typedef struct pcap_dag_node {
|
||||
struct pcap_dag_node *next;
|
||||
|
@ -103,12 +97,12 @@ static int dag_set_datalink(pcap_t *p, int dlt);
|
|||
static int dag_get_datalink(pcap_t *p);
|
||||
static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf);
|
||||
|
||||
static void delete_pcap_dag(pcap_t *p) {
|
||||
static void
|
||||
delete_pcap_dag(pcap_t *p)
|
||||
{
|
||||
pcap_dag_node_t *curr = NULL, *prev = NULL;
|
||||
|
||||
for (prev = NULL, curr = pcap_dags;
|
||||
curr != NULL && curr->p != p;
|
||||
prev = curr, curr = curr->next) {
|
||||
for (prev = NULL, curr = pcap_dags; curr != NULL && curr->p != p; prev = curr, curr = curr->next) {
|
||||
/* empty */
|
||||
}
|
||||
|
||||
|
@ -126,7 +120,9 @@ static void delete_pcap_dag(pcap_t *p) {
|
|||
* in the pcap_t structure, and closes the file descriptor for the DAG card.
|
||||
*/
|
||||
|
||||
static void dag_platform_close(pcap_t *p) {
|
||||
static void
|
||||
dag_platform_close(pcap_t *p)
|
||||
{
|
||||
|
||||
#ifdef linux
|
||||
if (p != NULL && p->md.device != NULL) {
|
||||
|
@ -149,7 +145,9 @@ static void dag_platform_close(pcap_t *p) {
|
|||
/* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */
|
||||
}
|
||||
|
||||
static void atexit_handler(void) {
|
||||
static void
|
||||
atexit_handler(void)
|
||||
{
|
||||
while (pcap_dags != NULL) {
|
||||
if (pcap_dags->pid == getpid()) {
|
||||
dag_platform_close(pcap_dags->p);
|
||||
|
@ -159,7 +157,9 @@ static void atexit_handler(void) {
|
|||
}
|
||||
}
|
||||
|
||||
static int new_pcap_dag(pcap_t *p) {
|
||||
static int
|
||||
new_pcap_dag(pcap_t *p)
|
||||
{
|
||||
pcap_dag_node_t *node = NULL;
|
||||
|
||||
if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) {
|
||||
|
@ -185,7 +185,9 @@ static int new_pcap_dag(pcap_t *p) {
|
|||
* for each of them. Returns the number of packets handled, -1 if an
|
||||
* error occured, or -2 if we were told to break out of the loop.
|
||||
*/
|
||||
static int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
|
||||
static int
|
||||
dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
|
||||
{
|
||||
unsigned int processed = 0;
|
||||
int flags = p->md.dag_offset_flags;
|
||||
unsigned int nonblocking = flags & DAGF_NONBLOCK;
|
||||
|
@ -240,54 +242,74 @@ static int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
|
|||
return -2;
|
||||
}
|
||||
|
||||
if (IS_BIGENDIAN())
|
||||
{
|
||||
if (IS_BIGENDIAN()) {
|
||||
rlen = header->rlen;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
rlen = ntohs(header->rlen);
|
||||
}
|
||||
p->md.dag_mem_bottom += rlen;
|
||||
|
||||
switch(header->type) {
|
||||
case TYPE_AAL5:
|
||||
case TYPE_ATM:
|
||||
packet_len = ATM_SNAPLEN;
|
||||
caplen = ATM_SNAPLEN;
|
||||
dp += 4;
|
||||
if (header->type == TYPE_AAL5) {
|
||||
if (IS_BIGENDIAN()) {
|
||||
packet_len = header->wlen;
|
||||
} else {
|
||||
packet_len = ntohs(header->wlen);
|
||||
}
|
||||
caplen = rlen - dag_record_size;
|
||||
} else {
|
||||
caplen = packet_len = ATM_CELL_SIZE;
|
||||
}
|
||||
if (p->linktype == DLT_SUNATM) {
|
||||
struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp;
|
||||
unsigned long rawatm;
|
||||
if (IS_BIGENDIAN()) {
|
||||
rawatm = *((unsigned long *)dp);
|
||||
sunatm->vci = (rawatm >> 4) & 0xffff;
|
||||
} else {
|
||||
rawatm = ntohl(*((unsigned long *)dp));
|
||||
sunatm->vci = htons((rawatm >> 4) & 0xffff);
|
||||
}
|
||||
sunatm->vpi = (rawatm >> 20) & 0x00ff;
|
||||
sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) |
|
||||
((sunatm->vpi == 0 && sunatm->vci == 5) ? 6 :
|
||||
((sunatm->vpi == 0 && sunatm->vci == 16) ? 5 :
|
||||
((dp[ATM_HDR_SIZE] == 0xaa &&
|
||||
dp[ATM_HDR_SIZE+1] == 0xaa &&
|
||||
dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1)));
|
||||
|
||||
} else {
|
||||
packet_len -= ATM_HDR_SIZE;
|
||||
caplen -= ATM_HDR_SIZE;
|
||||
dp += ATM_HDR_SIZE;
|
||||
}
|
||||
break;
|
||||
|
||||
case TYPE_ETH:
|
||||
if (IS_BIGENDIAN())
|
||||
{
|
||||
if (IS_BIGENDIAN()) {
|
||||
packet_len = header->wlen;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
packet_len = ntohs(header->wlen);
|
||||
}
|
||||
packet_len -= (p->md.dag_fcs_bits >> 3);
|
||||
caplen = rlen - dag_record_size - 2;
|
||||
if (caplen > packet_len)
|
||||
{
|
||||
if (caplen > packet_len) {
|
||||
caplen = packet_len;
|
||||
}
|
||||
dp += 2;
|
||||
break;
|
||||
|
||||
case TYPE_HDLC_POS:
|
||||
if (IS_BIGENDIAN())
|
||||
{
|
||||
if (IS_BIGENDIAN()) {
|
||||
packet_len = header->wlen;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
packet_len = ntohs(header->wlen);
|
||||
}
|
||||
packet_len -= (p->md.dag_fcs_bits >> 3);
|
||||
caplen = rlen - dag_record_size;
|
||||
if (caplen > packet_len)
|
||||
{
|
||||
if (caplen > packet_len) {
|
||||
caplen = packet_len;
|
||||
}
|
||||
break;
|
||||
|
@ -311,12 +333,9 @@ static int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
|
|||
/* convert between timestamp formats */
|
||||
register unsigned long long ts;
|
||||
|
||||
if (IS_BIGENDIAN())
|
||||
{
|
||||
if (IS_BIGENDIAN()) {
|
||||
ts = SWAP_TS(header->ts);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ts = header->ts;
|
||||
}
|
||||
|
||||
|
@ -366,7 +385,9 @@ static int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) {
|
|||
*
|
||||
* See also pcap(3).
|
||||
*/
|
||||
pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) {
|
||||
pcap_t *
|
||||
dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf)
|
||||
{
|
||||
char conf[30]; /* dag configure string */
|
||||
pcap_t *handle;
|
||||
char *s;
|
||||
|
@ -418,7 +439,6 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c
|
|||
/* snap len has to be a multiple of 4 */
|
||||
snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3);
|
||||
|
||||
fprintf(stderr, "Configuring DAG with '%s'.\n", conf);
|
||||
if(dag_configure(handle->fd, conf) < 0) {
|
||||
snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno));
|
||||
goto fail;
|
||||
|
@ -457,8 +477,9 @@ pcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, c
|
|||
handle->snapshot = snaplen;
|
||||
/*handle->md.timeout = to_ms; */
|
||||
|
||||
handle->linktype = -1;
|
||||
if (dag_get_datalink(handle) < 0) {
|
||||
snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_linktype %s: unknown linktype\n", device);
|
||||
strcpy(ebuf, handle->errbuf);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
|
@ -499,15 +520,17 @@ fail:
|
|||
/*
|
||||
* Get rid of any link-layer type list we allocated.
|
||||
*/
|
||||
if (handle->dlt_list != NULL)
|
||||
if (handle->dlt_list != NULL) {
|
||||
free(handle->dlt_list);
|
||||
}
|
||||
free(handle);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int dag_stats(pcap_t *p, struct pcap_stat *ps) {
|
||||
static int
|
||||
dag_stats(pcap_t *p, struct pcap_stat *ps) {
|
||||
/* This needs to be filled out correctly. Hopefully a dagapi call will
|
||||
provide all necessary information.
|
||||
*/
|
||||
|
@ -560,8 +583,7 @@ dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
|
|||
return (ret);
|
||||
}
|
||||
|
||||
for (linenum = 1;
|
||||
fgets(linebuf, sizeof linebuf, proc_dag_f) != NULL; linenum++) {
|
||||
for (linenum = 1; fgets(linebuf, sizeof linebuf, proc_dag_f) != NULL; linenum++) {
|
||||
|
||||
/*
|
||||
* Skip the first two lines - they're headers.
|
||||
|
@ -620,7 +642,9 @@ dag_platform_finddevs(pcap_if_t **devlistp, char *errbuf)
|
|||
* no attempt to store the filter in kernel memory as that is not supported
|
||||
* with DAG cards.
|
||||
*/
|
||||
static int dag_setfilter(pcap_t *p, struct bpf_program *fp) {
|
||||
static int
|
||||
dag_setfilter(pcap_t *p, struct bpf_program *fp)
|
||||
{
|
||||
if (!p)
|
||||
return -1;
|
||||
if (!fp) {
|
||||
|
@ -645,6 +669,8 @@ static int dag_setfilter(pcap_t *p, struct bpf_program *fp) {
|
|||
static int
|
||||
dag_set_datalink(pcap_t *p, int dlt)
|
||||
{
|
||||
p->linktype = dlt;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
@ -671,29 +697,28 @@ dag_setnonblock(pcap_t *p, int nonblock, char *errbuf)
|
|||
static int
|
||||
dag_get_datalink(pcap_t *p)
|
||||
{
|
||||
/* Check the type through a dagapi call.
|
||||
*/
|
||||
switch(dag_linktype(p->fd)) {
|
||||
case TYPE_HDLC_POS: {
|
||||
dag_record_t *record;
|
||||
int daglinktype;
|
||||
|
||||
/* peek at the first available record to see if it is PPP */
|
||||
while ((p->md.dag_mem_top - p->md.dag_mem_bottom) < (dag_record_size + 4)) {
|
||||
p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), 0);
|
||||
if (p->dlt_list == NULL && (p->dlt_list = malloc(2*sizeof(*(p->dlt_list)))) == NULL) {
|
||||
(void)snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno));
|
||||
return (-1);
|
||||
}
|
||||
record = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom);
|
||||
|
||||
if ((ntohl(record->rec.pos.hdlc) & 0xffff0000) == 0xff030000) {
|
||||
p->linktype = DLT_PPP_SERIAL;
|
||||
fprintf(stderr, "Set DAG linktype to %d (DLT_PPP_SERIAL)\n", linktype);
|
||||
} else {
|
||||
/* Check the type through a dagapi call. */
|
||||
daglinktype = dag_linktype(p->fd);
|
||||
|
||||
switch(daglinktype) {
|
||||
|
||||
case TYPE_HDLC_POS:
|
||||
if (p->dlt_list != NULL) {
|
||||
p->dlt_count = 2;
|
||||
p->dlt_list[0] = DLT_CHDLC;
|
||||
p->dlt_list[1] = DLT_PPP_SERIAL;
|
||||
}
|
||||
p->linktype = DLT_CHDLC;
|
||||
fprintf(stderr, "Set DAG linktype to %d (DLT_CHDLC)\n", linktype);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case TYPE_ETH:
|
||||
p->linktype = DLT_EN10MB;
|
||||
/*
|
||||
* This is (presumably) a real Ethernet capture; give it a
|
||||
* link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
|
||||
|
@ -704,28 +729,34 @@ dag_get_datalink(pcap_t *p)
|
|||
* DOCSIS frames out on the wire inside the low-level
|
||||
* Ethernet framing).
|
||||
*/
|
||||
p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
|
||||
/*
|
||||
* If that fails, just leave the list empty.
|
||||
*/
|
||||
if (p->dlt_list != NULL) {
|
||||
p->dlt_count = 2;
|
||||
p->dlt_list[0] = DLT_EN10MB;
|
||||
p->dlt_list[1] = DLT_DOCSIS;
|
||||
p->dlt_count = 2;
|
||||
}
|
||||
fprintf(stderr, "Set DAG linktype to %d (DLT_EN10MB)\n", linktype);
|
||||
p->linktype = DLT_EN10MB;
|
||||
break;
|
||||
|
||||
case TYPE_AAL5:
|
||||
case TYPE_ATM:
|
||||
if (p->dlt_list != NULL) {
|
||||
p->dlt_count = 2;
|
||||
p->dlt_list[0] = DLT_ATM_RFC1483;
|
||||
p->dlt_list[1] = DLT_SUNATM;
|
||||
}
|
||||
p->linktype = DLT_ATM_RFC1483;
|
||||
fprintf(stderr, "Set DAG linktype to %d (DLT_ATM_RFC1483)\n", linktype);
|
||||
break;
|
||||
|
||||
|
||||
case TYPE_LEGACY:
|
||||
p->linktype = DLT_NULL;
|
||||
fprintf(stderr, "Set DAG linktype to %d (DLT_NULL)\n", linktype);
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "Unknown DAG linktype %d\n", dag_linktype(p->fd));
|
||||
return -1;;
|
||||
snprintf(p->errbuf, sizeof(p->errbuf), "unknown DAG linktype %d\n", daglinktype);
|
||||
return (-1);
|
||||
|
||||
}
|
||||
return 0;
|
||||
|
||||
return p->linktype;
|
||||
}
|
||||
|
|
Reference in New Issue