pcap-dag.c revision 127664
1127664Sbms/* 2127664Sbms * pcap-dag.c: Packet capture interface for Endace DAG card. 3127664Sbms * 4127664Sbms * The functionality of this code attempts to mimic that of pcap-linux as much 5127664Sbms * as possible. This code is compiled in several different ways depending on 6127664Sbms * whether DAG_ONLY and HAVE_DAG_API are defined. If HAVE_DAG_API is not 7127664Sbms * defined it should not get compiled in, otherwise if DAG_ONLY is defined then 8127664Sbms * the 'dag_' function calls are renamed to 'pcap_' equivalents. If DAG_ONLY 9127664Sbms * is not defined then nothing is altered - the dag_ functions will be 10127664Sbms * called as required from their pcap-linux/bpf equivalents. 11127664Sbms * 12127664Sbms * Author: Richard Littin, Sean Irvine ({richard,sean}@reeltwo.com) 13127664Sbms * 14127664Sbms * Modifications: 15127664Sbms * 2003 May - Jesper Peterson <support@endace.com> 16127664Sbms * Code shuffled around to suit fad-xxx.c structure 17127664Sbms * Added atexit() handler to stop DAG if application is too lazy 18127664Sbms * 2003 September - Koryn Grant <koryn@endace.com> 19127664Sbms * Added support for nonblocking operation. 20127664Sbms * Added support for processing more than a single packet in pcap_dispatch(). 21127664Sbms * Fixed bug in loss counter code. 22127664Sbms * Improved portability of loss counter code (e.g. use UINT_MAX instead of 0xffff). 23127664Sbms * Removed unused local variables. 24127664Sbms * Added required headers (ctype.h, limits.h, unistd.h, netinet/in.h). 25127664Sbms * 2003 October - Koryn Grant <koryn@endace.com.> 26127664Sbms * Changed semantics to match those of standard pcap on linux. 27127664Sbms * - packets rejected by the filter are not counted. 28127664Sbms */ 29127664Sbms 30127664Sbms#ifndef lint 31127664Sbmsstatic const char rcsid[] _U_ = 32127664Sbms "@(#) $Header: /tcpdump/master/libpcap/pcap-dag.c,v 1.10.2.4 2003/11/21 10:20:45 guy Exp $ (LBL)"; 33127664Sbms#endif 34127664Sbms 35127664Sbms#ifdef HAVE_CONFIG_H 36127664Sbms#include "config.h" 37127664Sbms#endif 38127664Sbms 39127664Sbms#include <sys/param.h> /* optionally get BSD define */ 40127664Sbms 41127664Sbms#include <stdlib.h> 42127664Sbms#include <string.h> 43127664Sbms#include <errno.h> 44127664Sbms 45127664Sbms#include "pcap-int.h" 46127664Sbms 47127664Sbms#include <ctype.h> 48127664Sbms#include <netinet/in.h> 49127664Sbms#include <sys/mman.h> 50127664Sbms#include <sys/socket.h> 51127664Sbms#include <sys/types.h> 52127664Sbms#include <unistd.h> 53127664Sbms 54127664Sbmsstruct mbuf; /* Squelch compiler warnings on some platforms for */ 55127664Sbmsstruct rtentry; /* declarations in <net/if.h> */ 56127664Sbms#include <net/if.h> 57127664Sbms 58127664Sbms#include <dagnew.h> 59127664Sbms#include <dagapi.h> 60127664Sbms 61127664Sbms#define MIN_DAG_SNAPLEN 12 62127664Sbms#define MAX_DAG_SNAPLEN 2040 63127664Sbms#define ATM_SNAPLEN 48 64127664Sbms 65127664Sbmstypedef struct pcap_dag_node { 66127664Sbms struct pcap_dag_node *next; 67127664Sbms pcap_t *p; 68127664Sbms pid_t pid; 69127664Sbms} pcap_dag_node_t; 70127664Sbms 71127664Sbmsstatic pcap_dag_node_t *pcap_dags = NULL; 72127664Sbmsstatic int atexit_handler_installed = 0; 73127664Sbmsstatic const unsigned short endian_test_word = 0x0100; 74127664Sbms 75127664Sbms#define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word)) 76127664Sbms 77127664Sbms/* 78127664Sbms * Swap byte ordering of unsigned long long timestamp on a big endian 79127664Sbms * machine. 80127664Sbms */ 81127664Sbms#define SWAP_TS(ull) ((ull & 0xff00000000000000LL) >> 56) | \ 82127664Sbms ((ull & 0x00ff000000000000LL) >> 40) | \ 83127664Sbms ((ull & 0x0000ff0000000000LL) >> 24) | \ 84127664Sbms ((ull & 0x000000ff00000000LL) >> 8) | \ 85127664Sbms ((ull & 0x00000000ff000000LL) << 8) | \ 86127664Sbms ((ull & 0x0000000000ff0000LL) << 24) | \ 87127664Sbms ((ull & 0x000000000000ff00LL) << 40) | \ 88127664Sbms ((ull & 0x00000000000000ffLL) << 56) 89127664Sbms 90127664Sbms 91127664Sbms#ifdef DAG_ONLY 92127664Sbms/* This code is required when compiling for a DAG device only. */ 93127664Sbms#include "pcap-dag.h" 94127664Sbms 95127664Sbms/* Replace dag function names with pcap equivalent. */ 96127664Sbms#define dag_open_live pcap_open_live 97127664Sbms#define dag_platform_finddevs pcap_platform_finddevs 98127664Sbms#endif /* DAG_ONLY */ 99127664Sbms 100127664Sbmsstatic int dag_setfilter(pcap_t *p, struct bpf_program *fp); 101127664Sbmsstatic int dag_stats(pcap_t *p, struct pcap_stat *ps); 102127664Sbmsstatic int dag_set_datalink(pcap_t *p, int dlt); 103127664Sbmsstatic int dag_get_datalink(pcap_t *p); 104127664Sbmsstatic int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf); 105127664Sbms 106127664Sbmsstatic void delete_pcap_dag(pcap_t *p) { 107127664Sbms pcap_dag_node_t *curr = NULL, *prev = NULL; 108127664Sbms 109127664Sbms for (prev = NULL, curr = pcap_dags; 110127664Sbms curr != NULL && curr->p != p; 111127664Sbms prev = curr, curr = curr->next) { 112127664Sbms /* empty */ 113127664Sbms } 114127664Sbms 115127664Sbms if (curr != NULL && curr->p == p) { 116127664Sbms if (prev != NULL) { 117127664Sbms prev->next = curr->next; 118127664Sbms } else { 119127664Sbms pcap_dags = curr->next; 120127664Sbms } 121127664Sbms } 122127664Sbms} 123127664Sbms 124127664Sbms/* 125127664Sbms * Performs a graceful shutdown of the DAG card, frees dynamic memory held 126127664Sbms * in the pcap_t structure, and closes the file descriptor for the DAG card. 127127664Sbms */ 128127664Sbms 129127664Sbmsstatic void dag_platform_close(pcap_t *p) { 130127664Sbms 131127664Sbms#ifdef linux 132127664Sbms if (p != NULL && p->md.device != NULL) { 133127664Sbms if(dag_stop(p->fd) < 0) 134127664Sbms fprintf(stderr,"dag_stop %s: %s\n", p->md.device, strerror(errno)); 135127664Sbms if(dag_close(p->fd) < 0) 136127664Sbms fprintf(stderr,"dag_close %s: %s\n", p->md.device, strerror(errno)); 137127664Sbms 138127664Sbms free(p->md.device); 139127664Sbms } 140127664Sbms#else 141127664Sbms if (p != NULL) { 142127664Sbms if(dag_stop(p->fd) < 0) 143127664Sbms fprintf(stderr,"dag_stop: %s\n", strerror(errno)); 144127664Sbms if(dag_close(p->fd) < 0) 145127664Sbms fprintf(stderr,"dag_close: %s\n", strerror(errno)); 146127664Sbms } 147127664Sbms#endif 148127664Sbms delete_pcap_dag(p); 149127664Sbms /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */ 150127664Sbms} 151127664Sbms 152127664Sbmsstatic void atexit_handler(void) { 153127664Sbms while (pcap_dags != NULL) { 154127664Sbms if (pcap_dags->pid == getpid()) { 155127664Sbms dag_platform_close(pcap_dags->p); 156127664Sbms } else { 157127664Sbms delete_pcap_dag(pcap_dags->p); 158127664Sbms } 159127664Sbms } 160127664Sbms} 161127664Sbms 162127664Sbmsstatic int new_pcap_dag(pcap_t *p) { 163127664Sbms pcap_dag_node_t *node = NULL; 164127664Sbms 165127664Sbms if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) { 166127664Sbms return -1; 167127664Sbms } 168127664Sbms 169127664Sbms if (!atexit_handler_installed) { 170127664Sbms atexit(atexit_handler); 171127664Sbms atexit_handler_installed = 1; 172127664Sbms } 173127664Sbms 174127664Sbms node->next = pcap_dags; 175127664Sbms node->p = p; 176127664Sbms node->pid = getpid(); 177127664Sbms 178127664Sbms pcap_dags = node; 179127664Sbms 180127664Sbms return 0; 181127664Sbms} 182127664Sbms 183127664Sbms/* 184127664Sbms * Read at most max_packets from the capture stream and call the callback 185127664Sbms * for each of them. Returns the number of packets handled, -1 if an 186127664Sbms * error occured, or -2 if we were told to break out of the loop. 187127664Sbms */ 188127664Sbmsstatic int dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) { 189127664Sbms unsigned int processed = 0; 190127664Sbms int flags = p->md.dag_offset_flags; 191127664Sbms unsigned int nonblocking = flags & DAGF_NONBLOCK; 192127664Sbms 193127664Sbms for (;;) 194127664Sbms { 195127664Sbms /* Get the next bufferful of packets (if necessary). */ 196127664Sbms while (p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size) { 197127664Sbms 198127664Sbms /* 199127664Sbms * Has "pcap_breakloop()" been called? 200127664Sbms */ 201127664Sbms if (p->break_loop) { 202127664Sbms /* 203127664Sbms * Yes - clear the flag that indicates that 204127664Sbms * it has, and return -2 to indicate that 205127664Sbms * we were told to break out of the loop. 206127664Sbms */ 207127664Sbms p->break_loop = 0; 208127664Sbms return -2; 209127664Sbms } 210127664Sbms 211127664Sbms p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), flags); 212127664Sbms if ((p->md.dag_mem_top - p->md.dag_mem_bottom < dag_record_size) && nonblocking) 213127664Sbms { 214127664Sbms /* Pcap is configured to process only available packets, and there aren't any. */ 215127664Sbms return 0; 216127664Sbms } 217127664Sbms } 218127664Sbms 219127664Sbms /* Process the packets. */ 220127664Sbms while (p->md.dag_mem_top - p->md.dag_mem_bottom >= dag_record_size) { 221127664Sbms 222127664Sbms unsigned short packet_len = 0; 223127664Sbms int caplen = 0; 224127664Sbms struct pcap_pkthdr pcap_header; 225127664Sbms 226127664Sbms dag_record_t *header = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom); 227127664Sbms u_char *dp = ((u_char *)header) + dag_record_size; 228127664Sbms unsigned short rlen; 229127664Sbms 230127664Sbms /* 231127664Sbms * Has "pcap_breakloop()" been called? 232127664Sbms */ 233127664Sbms if (p->break_loop) { 234127664Sbms /* 235127664Sbms * Yes - clear the flag that indicates that 236127664Sbms * it has, and return -2 to indicate that 237127664Sbms * we were told to break out of the loop. 238127664Sbms */ 239127664Sbms p->break_loop = 0; 240127664Sbms return -2; 241127664Sbms } 242127664Sbms 243127664Sbms if (IS_BIGENDIAN()) 244127664Sbms { 245127664Sbms rlen = header->rlen; 246127664Sbms } 247127664Sbms else 248127664Sbms { 249127664Sbms rlen = ntohs(header->rlen); 250127664Sbms } 251127664Sbms p->md.dag_mem_bottom += rlen; 252127664Sbms 253127664Sbms switch(header->type) { 254127664Sbms case TYPE_ATM: 255127664Sbms packet_len = ATM_SNAPLEN; 256127664Sbms caplen = ATM_SNAPLEN; 257127664Sbms dp += 4; 258127664Sbms break; 259127664Sbms 260127664Sbms case TYPE_ETH: 261127664Sbms if (IS_BIGENDIAN()) 262127664Sbms { 263127664Sbms packet_len = header->wlen; 264127664Sbms } 265127664Sbms else 266127664Sbms { 267127664Sbms packet_len = ntohs(header->wlen); 268127664Sbms } 269127664Sbms packet_len -= (p->md.dag_fcs_bits >> 3); 270127664Sbms caplen = rlen - dag_record_size - 2; 271127664Sbms if (caplen > packet_len) 272127664Sbms { 273127664Sbms caplen = packet_len; 274127664Sbms } 275127664Sbms dp += 2; 276127664Sbms break; 277127664Sbms 278127664Sbms case TYPE_HDLC_POS: 279127664Sbms if (IS_BIGENDIAN()) 280127664Sbms { 281127664Sbms packet_len = header->wlen; 282127664Sbms } 283127664Sbms else 284127664Sbms { 285127664Sbms packet_len = ntohs(header->wlen); 286127664Sbms } 287127664Sbms packet_len -= (p->md.dag_fcs_bits >> 3); 288127664Sbms caplen = rlen - dag_record_size; 289127664Sbms if (caplen > packet_len) 290127664Sbms { 291127664Sbms caplen = packet_len; 292127664Sbms } 293127664Sbms break; 294127664Sbms } 295127664Sbms 296127664Sbms if (caplen > p->snapshot) 297127664Sbms caplen = p->snapshot; 298127664Sbms 299127664Sbms /* Count lost packets. */ 300127664Sbms if (header->lctr) { 301127664Sbms if (p->md.stat.ps_drop > (UINT_MAX - header->lctr)) { 302127664Sbms p->md.stat.ps_drop = UINT_MAX; 303127664Sbms } else { 304127664Sbms p->md.stat.ps_drop += header->lctr; 305127664Sbms } 306127664Sbms } 307127664Sbms 308127664Sbms /* Run the packet filter if there is one. */ 309127664Sbms if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) { 310127664Sbms 311127664Sbms /* convert between timestamp formats */ 312127664Sbms register unsigned long long ts; 313127664Sbms 314127664Sbms if (IS_BIGENDIAN()) 315127664Sbms { 316127664Sbms ts = SWAP_TS(header->ts); 317127664Sbms } 318127664Sbms else 319127664Sbms { 320127664Sbms ts = header->ts; 321127664Sbms } 322127664Sbms 323127664Sbms pcap_header.ts.tv_sec = ts >> 32; 324127664Sbms ts = (ts & 0xffffffffULL) * 1000000; 325127664Sbms ts += 0x80000000; /* rounding */ 326127664Sbms pcap_header.ts.tv_usec = ts >> 32; 327127664Sbms if (pcap_header.ts.tv_usec >= 1000000) { 328127664Sbms pcap_header.ts.tv_usec -= 1000000; 329127664Sbms pcap_header.ts.tv_sec++; 330127664Sbms } 331127664Sbms 332127664Sbms /* Fill in our own header data */ 333127664Sbms pcap_header.caplen = caplen; 334127664Sbms pcap_header.len = packet_len; 335127664Sbms 336127664Sbms /* Count the packet. */ 337127664Sbms p->md.stat.ps_recv++; 338127664Sbms 339127664Sbms /* Call the user supplied callback function */ 340127664Sbms callback(user, &pcap_header, dp); 341127664Sbms 342127664Sbms /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */ 343127664Sbms processed++; 344127664Sbms if (processed == cnt) 345127664Sbms { 346127664Sbms /* Reached the user-specified limit. */ 347127664Sbms return cnt; 348127664Sbms } 349127664Sbms } 350127664Sbms } 351127664Sbms 352127664Sbms if (nonblocking || processed) 353127664Sbms { 354127664Sbms return processed; 355127664Sbms } 356127664Sbms } 357127664Sbms 358127664Sbms return processed; 359127664Sbms} 360127664Sbms 361127664Sbms/* 362127664Sbms * Get a handle for a live capture from the given DAG device. Passing a NULL 363127664Sbms * device will result in a failure. The promisc flag is ignored because DAG 364127664Sbms * cards are always promiscuous. The to_ms parameter is also ignored as it is 365127664Sbms * not supported in hardware. 366127664Sbms * 367127664Sbms * See also pcap(3). 368127664Sbms */ 369127664Sbmspcap_t *dag_open_live(const char *device, int snaplen, int promisc, int to_ms, char *ebuf) { 370127664Sbms char conf[30]; /* dag configure string */ 371127664Sbms pcap_t *handle; 372127664Sbms char *s; 373127664Sbms int n; 374127664Sbms 375127664Sbms if (device == NULL) { 376127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno)); 377127664Sbms return NULL; 378127664Sbms } 379127664Sbms /* Allocate a handle for this session. */ 380127664Sbms 381127664Sbms handle = malloc(sizeof(*handle)); 382127664Sbms if (handle == NULL) { 383127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc %s: %s", device, pcap_strerror(errno)); 384127664Sbms return NULL; 385127664Sbms } 386127664Sbms 387127664Sbms /* Initialize some components of the pcap structure. */ 388127664Sbms 389127664Sbms memset(handle, 0, sizeof(*handle)); 390127664Sbms 391127664Sbms if (strstr(device, "/dev") == NULL) { 392127664Sbms char * newDev = (char *)malloc(strlen(device) + 6); 393127664Sbms newDev[0] = '\0'; 394127664Sbms strcat(newDev, "/dev/"); 395127664Sbms strcat(newDev,device); 396127664Sbms device = newDev; 397127664Sbms } else { 398127664Sbms device = strdup(device); 399127664Sbms } 400127664Sbms 401127664Sbms if (device == NULL) { 402127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE, "str_dup: %s\n", pcap_strerror(errno)); 403127664Sbms goto fail; 404127664Sbms } 405127664Sbms 406127664Sbms /* setup device parameters */ 407127664Sbms if((handle->fd = dag_open((char *)device)) < 0) { 408127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno)); 409127664Sbms goto fail; 410127664Sbms } 411127664Sbms 412127664Sbms /* set the card snap length to the specified snaplen parameter */ 413127664Sbms if (snaplen == 0 || snaplen > MAX_DAG_SNAPLEN) { 414127664Sbms snaplen = MAX_DAG_SNAPLEN; 415127664Sbms } else if (snaplen < MIN_DAG_SNAPLEN) { 416127664Sbms snaplen = MIN_DAG_SNAPLEN; 417127664Sbms } 418127664Sbms /* snap len has to be a multiple of 4 */ 419127664Sbms snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3); 420127664Sbms 421127664Sbms fprintf(stderr, "Configuring DAG with '%s'.\n", conf); 422127664Sbms if(dag_configure(handle->fd, conf) < 0) { 423127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s\n", device, pcap_strerror(errno)); 424127664Sbms goto fail; 425127664Sbms } 426127664Sbms 427127664Sbms if((handle->md.dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) { 428127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s\n", device, pcap_strerror(errno)); 429127664Sbms goto fail; 430127664Sbms } 431127664Sbms 432127664Sbms if(dag_start(handle->fd) < 0) { 433127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s\n", device, pcap_strerror(errno)); 434127664Sbms goto fail; 435127664Sbms } 436127664Sbms 437127664Sbms /* 438127664Sbms * Important! You have to ensure bottom is properly 439127664Sbms * initialized to zero on startup, it won't give you 440127664Sbms * a compiler warning if you make this mistake! 441127664Sbms */ 442127664Sbms handle->md.dag_mem_bottom = 0; 443127664Sbms handle->md.dag_mem_top = 0; 444127664Sbms 445127664Sbms /* TODO: query the card */ 446127664Sbms handle->md.dag_fcs_bits = 32; 447127664Sbms if ((s = getenv("ERF_FCS_BITS")) != NULL) { 448127664Sbms if ((n = atoi(s)) == 0 || n == 16|| n == 32) { 449127664Sbms handle->md.dag_fcs_bits = n; 450127664Sbms } else { 451127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE, 452127664Sbms "pcap_open_live %s: bad ERF_FCS_BITS value (%d) in environment\n", device, n); 453127664Sbms goto fail; 454127664Sbms } 455127664Sbms } 456127664Sbms 457127664Sbms handle->snapshot = snaplen; 458127664Sbms /*handle->md.timeout = to_ms; */ 459127664Sbms 460127664Sbms if ((handle->linktype = dag_get_datalink(handle)) < 0) { 461127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE, "dag_get_linktype %s: unknown linktype\n", device); 462127664Sbms goto fail; 463127664Sbms } 464127664Sbms 465127664Sbms handle->bufsize = 0; 466127664Sbms 467127664Sbms if (new_pcap_dag(handle) < 0) { 468127664Sbms snprintf(ebuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s\n", device, pcap_strerror(errno)); 469127664Sbms goto fail; 470127664Sbms } 471127664Sbms 472127664Sbms /* 473127664Sbms * "select()" and "poll()" don't (yet) work on DAG device descriptors. 474127664Sbms */ 475127664Sbms handle->selectable_fd = -1; 476127664Sbms 477127664Sbms#ifdef linux 478127664Sbms handle->md.device = (char *)device; 479127664Sbms#else 480127664Sbms free((char *)device); 481127664Sbms device = NULL; 482127664Sbms#endif 483127664Sbms 484127664Sbms handle->read_op = dag_read; 485127664Sbms handle->setfilter_op = dag_setfilter; 486127664Sbms handle->set_datalink_op = dag_set_datalink; 487127664Sbms handle->getnonblock_op = pcap_getnonblock_fd; 488127664Sbms handle->setnonblock_op = dag_setnonblock; 489127664Sbms handle->stats_op = dag_stats; 490127664Sbms handle->close_op = dag_platform_close; 491127664Sbms 492127664Sbms return handle; 493127664Sbms 494127664Sbmsfail: 495127664Sbms if (device != NULL) { 496127664Sbms free((char *)device); 497127664Sbms } 498127664Sbms if (handle != NULL) { 499127664Sbms free(handle); 500127664Sbms } 501127664Sbms 502127664Sbms return NULL; 503127664Sbms} 504127664Sbms 505127664Sbmsstatic int dag_stats(pcap_t *p, struct pcap_stat *ps) { 506127664Sbms /* This needs to be filled out correctly. Hopefully a dagapi call will 507127664Sbms provide all necessary information. 508127664Sbms */ 509127664Sbms /*p->md.stat.ps_recv = 0;*/ 510127664Sbms /*p->md.stat.ps_drop = 0;*/ 511127664Sbms 512127664Sbms *ps = p->md.stat; 513127664Sbms 514127664Sbms return 0; 515127664Sbms} 516127664Sbms 517127664Sbms/* 518127664Sbms * Get from "/proc/dag" all interfaces listed there; if they're 519127664Sbms * already in the list of interfaces we have, that won't add another 520127664Sbms * instance, but if they're not, that'll add them. 521127664Sbms * 522127664Sbms * We don't bother getting any addresses for them. 523127664Sbms * 524127664Sbms * We also don't fail if we couldn't open "/proc/dag"; we just leave 525127664Sbms * the list of interfaces as is. 526127664Sbms */ 527127664Sbmsint 528127664Sbmsdag_platform_finddevs(pcap_if_t **devlistp, char *errbuf) 529127664Sbms{ 530127664Sbms FILE *proc_dag_f; 531127664Sbms char linebuf[512]; 532127664Sbms int linenum; 533127664Sbms unsigned char *p; 534127664Sbms char name[512]; /* XXX - pick a size */ 535127664Sbms char *q; 536127664Sbms int ret = 0; 537127664Sbms 538127664Sbms /* Quick exit if /proc/dag not readable */ 539127664Sbms proc_dag_f = fopen("/proc/dag", "r"); 540127664Sbms if (proc_dag_f == NULL) 541127664Sbms { 542127664Sbms int i; 543127664Sbms char dev[16] = "dagx"; 544127664Sbms 545127664Sbms for (i = '0'; ret == 0 && i <= '9'; i++) { 546127664Sbms dev[3] = i; 547127664Sbms if (pcap_add_if(devlistp, dev, 0, NULL, errbuf) == -1) { 548127664Sbms /* 549127664Sbms * Failure. 550127664Sbms */ 551127664Sbms ret = -1; 552127664Sbms } 553127664Sbms } 554127664Sbms 555127664Sbms return (ret); 556127664Sbms } 557127664Sbms 558127664Sbms for (linenum = 1; 559127664Sbms fgets(linebuf, sizeof linebuf, proc_dag_f) != NULL; linenum++) { 560127664Sbms 561127664Sbms /* 562127664Sbms * Skip the first two lines - they're headers. 563127664Sbms */ 564127664Sbms if (linenum <= 2) 565127664Sbms continue; 566127664Sbms 567127664Sbms p = &linebuf[0]; 568127664Sbms 569127664Sbms if (*p == '\0' || *p == '\n' || *p != 'D') 570127664Sbms continue; /* not a Dag line */ 571127664Sbms 572127664Sbms /* 573127664Sbms * Get the interface name. 574127664Sbms */ 575127664Sbms q = &name[0]; 576127664Sbms while (*p != '\0' && *p != ':') { 577127664Sbms if (*p != ' ') 578127664Sbms *q++ = tolower(*p++); 579127664Sbms else 580127664Sbms p++; 581127664Sbms } 582127664Sbms *q = '\0'; 583127664Sbms 584127664Sbms /* 585127664Sbms * Add an entry for this interface, with no addresses. 586127664Sbms */ 587127664Sbms p[strlen(p) - 1] = '\0'; /* get rid of \n */ 588127664Sbms if (pcap_add_if(devlistp, name, 0, strdup(p + 2), errbuf) == -1) { 589127664Sbms /* 590127664Sbms * Failure. 591127664Sbms */ 592127664Sbms ret = -1; 593127664Sbms break; 594127664Sbms } 595127664Sbms } 596127664Sbms if (ret != -1) { 597127664Sbms /* 598127664Sbms * Well, we didn't fail for any other reason; did we 599127664Sbms * fail due to an error reading the file? 600127664Sbms */ 601127664Sbms if (ferror(proc_dag_f)) { 602127664Sbms (void)snprintf(errbuf, PCAP_ERRBUF_SIZE, 603127664Sbms "Error reading /proc/dag: %s", 604127664Sbms pcap_strerror(errno)); 605127664Sbms ret = -1; 606127664Sbms } 607127664Sbms } 608127664Sbms 609127664Sbms (void)fclose(proc_dag_f); 610127664Sbms return (ret); 611127664Sbms} 612127664Sbms 613127664Sbms/* 614127664Sbms * Installs the given bpf filter program in the given pcap structure. There is 615127664Sbms * no attempt to store the filter in kernel memory as that is not supported 616127664Sbms * with DAG cards. 617127664Sbms */ 618127664Sbmsstatic int dag_setfilter(pcap_t *p, struct bpf_program *fp) { 619127664Sbms if (!p) 620127664Sbms return -1; 621127664Sbms if (!fp) { 622127664Sbms strncpy(p->errbuf, "setfilter: No filter specified", 623127664Sbms sizeof(p->errbuf)); 624127664Sbms return -1; 625127664Sbms } 626127664Sbms 627127664Sbms /* Make our private copy of the filter */ 628127664Sbms 629127664Sbms if (install_bpf_program(p, fp) < 0) { 630127664Sbms snprintf(p->errbuf, sizeof(p->errbuf), 631127664Sbms "malloc: %s", pcap_strerror(errno)); 632127664Sbms return -1; 633127664Sbms } 634127664Sbms 635127664Sbms p->md.use_bpf = 0; 636127664Sbms 637127664Sbms return (0); 638127664Sbms} 639127664Sbms 640127664Sbmsstatic int 641127664Sbmsdag_set_datalink(pcap_t *p, int dlt) 642127664Sbms{ 643127664Sbms return (0); 644127664Sbms} 645127664Sbms 646127664Sbmsstatic int 647127664Sbmsdag_setnonblock(pcap_t *p, int nonblock, char *errbuf) 648127664Sbms{ 649127664Sbms /* 650127664Sbms * Set non-blocking mode on the FD. 651127664Sbms * XXX - is that necessary? If not, don't bother calling it, 652127664Sbms * and have a "dag_getnonblock()" function that looks at 653127664Sbms * "p->md.dag_offset_flags". 654127664Sbms */ 655127664Sbms if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0) 656127664Sbms return (-1); 657127664Sbms 658127664Sbms if (nonblock) { 659127664Sbms p->md.dag_offset_flags |= DAGF_NONBLOCK; 660127664Sbms } else { 661127664Sbms p->md.dag_offset_flags &= ~DAGF_NONBLOCK; 662127664Sbms } 663127664Sbms return (0); 664127664Sbms} 665127664Sbms 666127664Sbmsstatic int 667127664Sbmsdag_get_datalink(pcap_t *p) 668127664Sbms{ 669127664Sbms int linktype = -1; 670127664Sbms 671127664Sbms /* Check the type through a dagapi call. 672127664Sbms */ 673127664Sbms switch(dag_linktype(p->fd)) { 674127664Sbms case TYPE_HDLC_POS: { 675127664Sbms dag_record_t *record; 676127664Sbms 677127664Sbms /* peek at the first available record to see if it is PPP */ 678127664Sbms while ((p->md.dag_mem_top - p->md.dag_mem_bottom) < (dag_record_size + 4)) { 679127664Sbms p->md.dag_mem_top = dag_offset(p->fd, &(p->md.dag_mem_bottom), 0); 680127664Sbms } 681127664Sbms record = (dag_record_t *)(p->md.dag_mem_base + p->md.dag_mem_bottom); 682127664Sbms 683127664Sbms if ((ntohl(record->rec.pos.hdlc) & 0xffff0000) == 0xff030000) { 684127664Sbms linktype = DLT_PPP_SERIAL; 685127664Sbms fprintf(stderr, "Set DAG linktype to %d (DLT_PPP_SERIAL)\n", linktype); 686127664Sbms } else { 687127664Sbms linktype = DLT_CHDLC; 688127664Sbms fprintf(stderr, "Set DAG linktype to %d (DLT_CHDLC)\n", linktype); 689127664Sbms } 690127664Sbms break; 691127664Sbms } 692127664Sbms case TYPE_ETH: 693127664Sbms linktype = DLT_EN10MB; 694127664Sbms fprintf(stderr, "Set DAG linktype to %d (DLT_EN10MB)\n", linktype); 695127664Sbms break; 696127664Sbms case TYPE_ATM: 697127664Sbms linktype = DLT_ATM_RFC1483; 698127664Sbms fprintf(stderr, "Set DAG linktype to %d (DLT_ATM_RFC1483)\n", linktype); 699127664Sbms break; 700127664Sbms case TYPE_LEGACY: 701127664Sbms linktype = DLT_NULL; 702127664Sbms fprintf(stderr, "Set DAG linktype to %d (DLT_NULL)\n", linktype); 703127664Sbms break; 704127664Sbms default: 705127664Sbms fprintf(stderr, "Unknown DAG linktype %d\n", dag_linktype(p->fd)); 706127664Sbms break; 707127664Sbms } 708127664Sbms 709127664Sbms return linktype; 710127664Sbms} 711