The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2 | * pcap-dag.c: Packet capture interface for Emulex EndaceDAG cards. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 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) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 13 | * Modifications: Jesper Peterson |
| 14 | * Koryn Grant |
| 15 | * Stephen Donnelly <stephen.donnelly@emulex.com> |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 16 | */ |
| 17 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 18 | #ifdef HAVE_CONFIG_H |
| 19 | #include "config.h" |
| 20 | #endif |
| 21 | |
| 22 | #include <sys/param.h> /* optionally get BSD define */ |
| 23 | |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include "pcap-int.h" |
| 29 | |
| 30 | #include <ctype.h> |
| 31 | #include <netinet/in.h> |
| 32 | #include <sys/mman.h> |
| 33 | #include <sys/socket.h> |
| 34 | #include <sys/types.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | struct mbuf; /* Squelch compiler warnings on some platforms for */ |
| 38 | struct rtentry; /* declarations in <net/if.h> */ |
| 39 | #include <net/if.h> |
| 40 | |
| 41 | #include "dagnew.h" |
| 42 | #include "dagapi.h" |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 43 | #include "dagpci.h" |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 44 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 45 | #include "pcap-dag.h" |
| 46 | |
| 47 | /* |
| 48 | * DAG devices have names beginning with "dag", followed by a number |
| 49 | * from 0 to DAG_MAX_BOARDS, then optionally a colon and a stream number |
| 50 | * from 0 to DAG_STREAM_MAX. |
| 51 | */ |
| 52 | #ifndef DAG_MAX_BOARDS |
| 53 | #define DAG_MAX_BOARDS 32 |
| 54 | #endif |
| 55 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 56 | |
| 57 | #ifndef TYPE_AAL5 |
| 58 | #define TYPE_AAL5 4 |
| 59 | #endif |
| 60 | |
| 61 | #ifndef TYPE_MC_HDLC |
| 62 | #define TYPE_MC_HDLC 5 |
| 63 | #endif |
| 64 | |
| 65 | #ifndef TYPE_MC_RAW |
| 66 | #define TYPE_MC_RAW 6 |
| 67 | #endif |
| 68 | |
| 69 | #ifndef TYPE_MC_ATM |
| 70 | #define TYPE_MC_ATM 7 |
| 71 | #endif |
| 72 | |
| 73 | #ifndef TYPE_MC_RAW_CHANNEL |
| 74 | #define TYPE_MC_RAW_CHANNEL 8 |
| 75 | #endif |
| 76 | |
| 77 | #ifndef TYPE_MC_AAL5 |
| 78 | #define TYPE_MC_AAL5 9 |
| 79 | #endif |
| 80 | |
| 81 | #ifndef TYPE_COLOR_HDLC_POS |
| 82 | #define TYPE_COLOR_HDLC_POS 10 |
| 83 | #endif |
| 84 | |
| 85 | #ifndef TYPE_COLOR_ETH |
| 86 | #define TYPE_COLOR_ETH 11 |
| 87 | #endif |
| 88 | |
| 89 | #ifndef TYPE_MC_AAL2 |
| 90 | #define TYPE_MC_AAL2 12 |
| 91 | #endif |
| 92 | |
| 93 | #ifndef TYPE_IP_COUNTER |
| 94 | #define TYPE_IP_COUNTER 13 |
| 95 | #endif |
| 96 | |
| 97 | #ifndef TYPE_TCP_FLOW_COUNTER |
| 98 | #define TYPE_TCP_FLOW_COUNTER 14 |
| 99 | #endif |
| 100 | |
| 101 | #ifndef TYPE_DSM_COLOR_HDLC_POS |
| 102 | #define TYPE_DSM_COLOR_HDLC_POS 15 |
| 103 | #endif |
| 104 | |
| 105 | #ifndef TYPE_DSM_COLOR_ETH |
| 106 | #define TYPE_DSM_COLOR_ETH 16 |
| 107 | #endif |
| 108 | |
| 109 | #ifndef TYPE_COLOR_MC_HDLC_POS |
| 110 | #define TYPE_COLOR_MC_HDLC_POS 17 |
| 111 | #endif |
| 112 | |
| 113 | #ifndef TYPE_AAL2 |
| 114 | #define TYPE_AAL2 18 |
| 115 | #endif |
| 116 | |
| 117 | #ifndef TYPE_COLOR_HASH_POS |
| 118 | #define TYPE_COLOR_HASH_POS 19 |
| 119 | #endif |
| 120 | |
| 121 | #ifndef TYPE_COLOR_HASH_ETH |
| 122 | #define TYPE_COLOR_HASH_ETH 20 |
| 123 | #endif |
| 124 | |
| 125 | #ifndef TYPE_INFINIBAND |
| 126 | #define TYPE_INFINIBAND 21 |
| 127 | #endif |
| 128 | |
| 129 | #ifndef TYPE_IPV4 |
| 130 | #define TYPE_IPV4 22 |
| 131 | #endif |
| 132 | |
| 133 | #ifndef TYPE_IPV6 |
| 134 | #define TYPE_IPV6 23 |
| 135 | #endif |
| 136 | |
| 137 | #ifndef TYPE_RAW_LINK |
| 138 | #define TYPE_RAW_LINK 24 |
| 139 | #endif |
| 140 | |
| 141 | #ifndef TYPE_INFINIBAND_LINK |
| 142 | #define TYPE_INFINIBAND_LINK 25 |
| 143 | #endif |
| 144 | |
| 145 | #ifndef TYPE_PAD |
| 146 | #define TYPE_PAD 48 |
| 147 | #endif |
| 148 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 149 | #define ATM_CELL_SIZE 52 |
| 150 | #define ATM_HDR_SIZE 4 |
| 151 | |
| 152 | /* |
| 153 | * A header containing additional MTP information. |
| 154 | */ |
| 155 | #define MTP2_SENT_OFFSET 0 /* 1 byte */ |
| 156 | #define MTP2_ANNEX_A_USED_OFFSET 1 /* 1 byte */ |
| 157 | #define MTP2_LINK_NUMBER_OFFSET 2 /* 2 bytes */ |
| 158 | #define MTP2_HDR_LEN 4 /* length of the header */ |
| 159 | |
| 160 | #define MTP2_ANNEX_A_NOT_USED 0 |
| 161 | #define MTP2_ANNEX_A_USED 1 |
| 162 | #define MTP2_ANNEX_A_USED_UNKNOWN 2 |
| 163 | |
| 164 | /* SunATM pseudo header */ |
| 165 | struct sunatm_hdr { |
| 166 | unsigned char flags; /* destination and traffic type */ |
| 167 | unsigned char vpi; /* VPI */ |
| 168 | unsigned short vci; /* VCI */ |
| 169 | }; |
| 170 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 171 | /* |
| 172 | * Private data for capturing on DAG devices. |
| 173 | */ |
| 174 | struct pcap_dag { |
| 175 | struct pcap_stat stat; |
| 176 | #ifdef HAVE_DAG_STREAMS_API |
| 177 | u_char *dag_mem_bottom; /* DAG card current memory bottom pointer */ |
| 178 | u_char *dag_mem_top; /* DAG card current memory top pointer */ |
| 179 | #else /* HAVE_DAG_STREAMS_API */ |
| 180 | void *dag_mem_base; /* DAG card memory base address */ |
| 181 | u_int dag_mem_bottom; /* DAG card current memory bottom offset */ |
| 182 | u_int dag_mem_top; /* DAG card current memory top offset */ |
| 183 | #endif /* HAVE_DAG_STREAMS_API */ |
| 184 | int dag_fcs_bits; /* Number of checksum bits from link layer */ |
| 185 | int dag_offset_flags; /* Flags to pass to dag_offset(). */ |
| 186 | int dag_stream; /* DAG stream number */ |
| 187 | int dag_timeout; /* timeout specified to pcap_open_live. |
| 188 | * Same as in linux above, introduce |
| 189 | * generally? */ |
| 190 | }; |
| 191 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 192 | typedef struct pcap_dag_node { |
| 193 | struct pcap_dag_node *next; |
| 194 | pcap_t *p; |
| 195 | pid_t pid; |
| 196 | } pcap_dag_node_t; |
| 197 | |
| 198 | static pcap_dag_node_t *pcap_dags = NULL; |
| 199 | static int atexit_handler_installed = 0; |
| 200 | static const unsigned short endian_test_word = 0x0100; |
| 201 | |
| 202 | #define IS_BIGENDIAN() (*((unsigned char *)&endian_test_word)) |
| 203 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 204 | #define MAX_DAG_PACKET 65536 |
| 205 | |
| 206 | static unsigned char TempPkt[MAX_DAG_PACKET]; |
| 207 | |
| 208 | static int dag_setfilter(pcap_t *p, struct bpf_program *fp); |
| 209 | static int dag_stats(pcap_t *p, struct pcap_stat *ps); |
| 210 | static int dag_set_datalink(pcap_t *p, int dlt); |
| 211 | static int dag_get_datalink(pcap_t *p); |
| 212 | static int dag_setnonblock(pcap_t *p, int nonblock, char *errbuf); |
| 213 | |
| 214 | static void |
| 215 | delete_pcap_dag(pcap_t *p) |
| 216 | { |
| 217 | pcap_dag_node_t *curr = NULL, *prev = NULL; |
| 218 | |
| 219 | for (prev = NULL, curr = pcap_dags; curr != NULL && curr->p != p; prev = curr, curr = curr->next) { |
| 220 | /* empty */ |
| 221 | } |
| 222 | |
| 223 | if (curr != NULL && curr->p == p) { |
| 224 | if (prev != NULL) { |
| 225 | prev->next = curr->next; |
| 226 | } else { |
| 227 | pcap_dags = curr->next; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Performs a graceful shutdown of the DAG card, frees dynamic memory held |
| 234 | * in the pcap_t structure, and closes the file descriptor for the DAG card. |
| 235 | */ |
| 236 | |
| 237 | static void |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 238 | dag_platform_cleanup(pcap_t *p) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 239 | { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 240 | struct pcap_dag *pd = p->pr; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 241 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 242 | #ifdef HAVE_DAG_STREAMS_API |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 243 | if(dag_stop_stream(p->fd, pd->dag_stream) < 0) |
| 244 | fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno)); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 245 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 246 | if(dag_detach_stream(p->fd, pd->dag_stream) < 0) |
| 247 | fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 248 | #else |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 249 | if(dag_stop(p->fd) < 0) |
| 250 | fprintf(stderr,"dag_stop: %s\n", strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 251 | #endif /* HAVE_DAG_STREAMS_API */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 252 | if(p->fd != -1) { |
| 253 | if(dag_close(p->fd) < 0) |
| 254 | fprintf(stderr,"dag_close: %s\n", strerror(errno)); |
| 255 | p->fd = -1; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 256 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 257 | delete_pcap_dag(p); |
| 258 | pcap_cleanup_live_common(p); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 259 | /* Note: don't need to call close(p->fd) here as dag_close(p->fd) does this. */ |
| 260 | } |
| 261 | |
| 262 | static void |
| 263 | atexit_handler(void) |
| 264 | { |
| 265 | while (pcap_dags != NULL) { |
| 266 | if (pcap_dags->pid == getpid()) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 267 | if (pcap_dags->p != NULL) |
| 268 | dag_platform_cleanup(pcap_dags->p); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 269 | } else { |
| 270 | delete_pcap_dag(pcap_dags->p); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static int |
| 276 | new_pcap_dag(pcap_t *p) |
| 277 | { |
| 278 | pcap_dag_node_t *node = NULL; |
| 279 | |
| 280 | if ((node = malloc(sizeof(pcap_dag_node_t))) == NULL) { |
| 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | if (!atexit_handler_installed) { |
| 285 | atexit(atexit_handler); |
| 286 | atexit_handler_installed = 1; |
| 287 | } |
| 288 | |
| 289 | node->next = pcap_dags; |
| 290 | node->p = p; |
| 291 | node->pid = getpid(); |
| 292 | |
| 293 | pcap_dags = node; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 298 | static unsigned int |
| 299 | dag_erf_ext_header_count(uint8_t * erf, size_t len) |
| 300 | { |
| 301 | uint32_t hdr_num = 0; |
| 302 | uint8_t hdr_type; |
| 303 | |
| 304 | /* basic sanity checks */ |
| 305 | if ( erf == NULL ) |
| 306 | return 0; |
| 307 | if ( len < 16 ) |
| 308 | return 0; |
| 309 | |
| 310 | /* check if we have any extension headers */ |
| 311 | if ( (erf[8] & 0x80) == 0x00 ) |
| 312 | return 0; |
| 313 | |
| 314 | /* loop over the extension headers */ |
| 315 | do { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 316 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 317 | /* sanity check we have enough bytes */ |
| 318 | if ( len < (24 + (hdr_num * 8)) ) |
| 319 | return hdr_num; |
| 320 | |
| 321 | /* get the header type */ |
| 322 | hdr_type = erf[(16 + (hdr_num * 8))]; |
| 323 | hdr_num++; |
| 324 | |
| 325 | } while ( hdr_type & 0x80 ); |
| 326 | |
| 327 | return hdr_num; |
| 328 | } |
| 329 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 330 | /* |
| 331 | * Read at most max_packets from the capture stream and call the callback |
| 332 | * for each of them. Returns the number of packets handled, -1 if an |
| 333 | * error occured, or -2 if we were told to break out of the loop. |
| 334 | */ |
| 335 | static int |
| 336 | dag_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user) |
| 337 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 338 | struct pcap_dag *pd = p->priv; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 339 | unsigned int processed = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 340 | int flags = pd->dag_offset_flags; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 341 | unsigned int nonblocking = flags & DAGF_NONBLOCK; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 342 | unsigned int num_ext_hdr = 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 343 | unsigned int ticks_per_second; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 344 | |
| 345 | /* Get the next bufferful of packets (if necessary). */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 346 | while (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 347 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 348 | /* |
| 349 | * Has "pcap_breakloop()" been called? |
| 350 | */ |
| 351 | if (p->break_loop) { |
| 352 | /* |
| 353 | * Yes - clear the flag that indicates that |
| 354 | * it has, and return -2 to indicate that |
| 355 | * we were told to break out of the loop. |
| 356 | */ |
| 357 | p->break_loop = 0; |
| 358 | return -2; |
| 359 | } |
| 360 | |
| 361 | #ifdef HAVE_DAG_STREAMS_API |
| 362 | /* dag_advance_stream() will block (unless nonblock is called) |
| 363 | * until 64kB of data has accumulated. |
| 364 | * If to_ms is set, it will timeout before 64kB has accumulated. |
| 365 | * We wait for 64kB because processing a few packets at a time |
| 366 | * can cause problems at high packet rates (>200kpps) due |
| 367 | * to inefficiencies. |
| 368 | * This does mean if to_ms is not specified the capture may 'hang' |
| 369 | * for long periods if the data rate is extremely slow (<64kB/sec) |
| 370 | * If non-block is specified it will return immediately. The user |
| 371 | * is then responsible for efficiency. |
| 372 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 373 | if ( NULL == (pd->dag_mem_top = dag_advance_stream(p->fd, pd->dag_stream, &(pd->dag_mem_bottom))) ) { |
| 374 | return -1; |
| 375 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 376 | #else |
| 377 | /* dag_offset does not support timeouts */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 378 | pd->dag_mem_top = dag_offset(p->fd, &(pd->dag_mem_bottom), flags); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 379 | #endif /* HAVE_DAG_STREAMS_API */ |
| 380 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 381 | if (nonblocking && (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size)) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 382 | { |
| 383 | /* Pcap is configured to process only available packets, and there aren't any, return immediately. */ |
| 384 | return 0; |
| 385 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 386 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 387 | if(!nonblocking && |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 388 | pd->dag_timeout && |
| 389 | (pd->dag_mem_top - pd->dag_mem_bottom < dag_record_size)) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 390 | { |
| 391 | /* Blocking mode, but timeout set and no data has arrived, return anyway.*/ |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 396 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 397 | /* Process the packets. */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 398 | while (pd->dag_mem_top - pd->dag_mem_bottom >= dag_record_size) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 399 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 400 | unsigned short packet_len = 0; |
| 401 | int caplen = 0; |
| 402 | struct pcap_pkthdr pcap_header; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 403 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 404 | #ifdef HAVE_DAG_STREAMS_API |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 405 | dag_record_t *header = (dag_record_t *)(pd->dag_mem_bottom); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 406 | #else |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 407 | dag_record_t *header = (dag_record_t *)(pd->dag_mem_base + pd->dag_mem_bottom); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 408 | #endif /* HAVE_DAG_STREAMS_API */ |
| 409 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 410 | u_char *dp = ((u_char *)header); /* + dag_record_size; */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 411 | unsigned short rlen; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 412 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 413 | /* |
| 414 | * Has "pcap_breakloop()" been called? |
| 415 | */ |
| 416 | if (p->break_loop) { |
| 417 | /* |
| 418 | * Yes - clear the flag that indicates that |
| 419 | * it has, and return -2 to indicate that |
| 420 | * we were told to break out of the loop. |
| 421 | */ |
| 422 | p->break_loop = 0; |
| 423 | return -2; |
| 424 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 425 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 426 | rlen = ntohs(header->rlen); |
| 427 | if (rlen < dag_record_size) |
| 428 | { |
| 429 | strncpy(p->errbuf, "dag_read: record too small", PCAP_ERRBUF_SIZE); |
| 430 | return -1; |
| 431 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 432 | pd->dag_mem_bottom += rlen; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 433 | |
| 434 | /* Count lost packets. */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 435 | switch((header->type & 0x7f)) { |
| 436 | /* in these types the color value overwrites the lctr */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 437 | case TYPE_COLOR_HDLC_POS: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 438 | case TYPE_COLOR_ETH: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 439 | case TYPE_DSM_COLOR_HDLC_POS: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 440 | case TYPE_DSM_COLOR_ETH: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 441 | case TYPE_COLOR_MC_HDLC_POS: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 442 | case TYPE_COLOR_HASH_ETH: |
| 443 | case TYPE_COLOR_HASH_POS: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 444 | break; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 445 | |
| 446 | default: |
| 447 | if (header->lctr) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 448 | if (pd->stat.ps_drop > (UINT_MAX - ntohs(header->lctr))) { |
| 449 | pd->stat.ps_drop = UINT_MAX; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 450 | } else { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 451 | pd->stat.ps_drop += ntohs(header->lctr); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 455 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 456 | if ((header->type & 0x7f) == TYPE_PAD) { |
| 457 | continue; |
| 458 | } |
| 459 | |
| 460 | num_ext_hdr = dag_erf_ext_header_count(dp, rlen); |
| 461 | |
| 462 | /* ERF encapsulation */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 463 | /* The Extensible Record Format is not dropped for this kind of encapsulation, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 464 | * and will be handled as a pseudo header by the decoding application. |
| 465 | * The information carried in the ERF header and in the optional subheader (if present) |
| 466 | * could be merged with the libpcap information, to offer a better decoding. |
| 467 | * The packet length is |
| 468 | * o the length of the packet on the link (header->wlen), |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 469 | * o plus the length of the ERF header (dag_record_size), as the length of the |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 470 | * pseudo header will be adjusted during the decoding, |
| 471 | * o plus the length of the optional subheader (if present). |
| 472 | * |
| 473 | * The capture length is header.rlen and the byte stuffing for alignment will be dropped |
| 474 | * if the capture length is greater than the packet length. |
| 475 | */ |
| 476 | if (p->linktype == DLT_ERF) { |
| 477 | packet_len = ntohs(header->wlen) + dag_record_size; |
| 478 | caplen = rlen; |
| 479 | switch ((header->type & 0x7f)) { |
| 480 | case TYPE_MC_AAL5: |
| 481 | case TYPE_MC_ATM: |
| 482 | case TYPE_MC_HDLC: |
| 483 | case TYPE_MC_RAW_CHANNEL: |
| 484 | case TYPE_MC_RAW: |
| 485 | case TYPE_MC_AAL2: |
| 486 | case TYPE_COLOR_MC_HDLC_POS: |
| 487 | packet_len += 4; /* MC header */ |
| 488 | break; |
| 489 | |
| 490 | case TYPE_COLOR_HASH_ETH: |
| 491 | case TYPE_DSM_COLOR_ETH: |
| 492 | case TYPE_COLOR_ETH: |
| 493 | case TYPE_ETH: |
| 494 | packet_len += 2; /* ETH header */ |
| 495 | break; |
| 496 | } /* switch type */ |
| 497 | |
| 498 | /* Include ERF extension headers */ |
| 499 | packet_len += (8 * num_ext_hdr); |
| 500 | |
| 501 | if (caplen > packet_len) { |
| 502 | caplen = packet_len; |
| 503 | } |
| 504 | } else { |
| 505 | /* Other kind of encapsulation according to the header Type */ |
| 506 | |
| 507 | /* Skip over generic ERF header */ |
| 508 | dp += dag_record_size; |
| 509 | /* Skip over extension headers */ |
| 510 | dp += 8 * num_ext_hdr; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 511 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 512 | switch((header->type & 0x7f)) { |
| 513 | case TYPE_ATM: |
| 514 | case TYPE_AAL5: |
| 515 | if (header->type == TYPE_AAL5) { |
| 516 | packet_len = ntohs(header->wlen); |
| 517 | caplen = rlen - dag_record_size; |
| 518 | } |
| 519 | case TYPE_MC_ATM: |
| 520 | if (header->type == TYPE_MC_ATM) { |
| 521 | caplen = packet_len = ATM_CELL_SIZE; |
| 522 | dp+=4; |
| 523 | } |
| 524 | case TYPE_MC_AAL5: |
| 525 | if (header->type == TYPE_MC_AAL5) { |
| 526 | packet_len = ntohs(header->wlen); |
| 527 | caplen = rlen - dag_record_size - 4; |
| 528 | dp+=4; |
| 529 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 530 | /* Skip over extension headers */ |
| 531 | caplen -= (8 * num_ext_hdr); |
| 532 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 533 | if (header->type == TYPE_ATM) { |
| 534 | caplen = packet_len = ATM_CELL_SIZE; |
| 535 | } |
| 536 | if (p->linktype == DLT_SUNATM) { |
| 537 | struct sunatm_hdr *sunatm = (struct sunatm_hdr *)dp; |
| 538 | unsigned long rawatm; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 539 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 540 | rawatm = ntohl(*((unsigned long *)dp)); |
| 541 | sunatm->vci = htons((rawatm >> 4) & 0xffff); |
| 542 | sunatm->vpi = (rawatm >> 20) & 0x00ff; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 543 | sunatm->flags = ((header->flags.iface & 1) ? 0x80 : 0x00) | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 544 | ((sunatm->vpi == 0 && sunatm->vci == htons(5)) ? 6 : |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 545 | ((sunatm->vpi == 0 && sunatm->vci == htons(16)) ? 5 : |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 546 | ((dp[ATM_HDR_SIZE] == 0xaa && |
| 547 | dp[ATM_HDR_SIZE+1] == 0xaa && |
| 548 | dp[ATM_HDR_SIZE+2] == 0x03) ? 2 : 1))); |
| 549 | |
| 550 | } else { |
| 551 | packet_len -= ATM_HDR_SIZE; |
| 552 | caplen -= ATM_HDR_SIZE; |
| 553 | dp += ATM_HDR_SIZE; |
| 554 | } |
| 555 | break; |
| 556 | |
| 557 | case TYPE_COLOR_HASH_ETH: |
| 558 | case TYPE_DSM_COLOR_ETH: |
| 559 | case TYPE_COLOR_ETH: |
| 560 | case TYPE_ETH: |
| 561 | packet_len = ntohs(header->wlen); |
| 562 | packet_len -= (pd->dag_fcs_bits >> 3); |
| 563 | caplen = rlen - dag_record_size - 2; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 564 | /* Skip over extension headers */ |
| 565 | caplen -= (8 * num_ext_hdr); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 566 | if (caplen > packet_len) { |
| 567 | caplen = packet_len; |
| 568 | } |
| 569 | dp += 2; |
| 570 | break; |
| 571 | |
| 572 | case TYPE_COLOR_HASH_POS: |
| 573 | case TYPE_DSM_COLOR_HDLC_POS: |
| 574 | case TYPE_COLOR_HDLC_POS: |
| 575 | case TYPE_HDLC_POS: |
| 576 | packet_len = ntohs(header->wlen); |
| 577 | packet_len -= (pd->dag_fcs_bits >> 3); |
| 578 | caplen = rlen - dag_record_size; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 579 | /* Skip over extension headers */ |
| 580 | caplen -= (8 * num_ext_hdr); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 581 | if (caplen > packet_len) { |
| 582 | caplen = packet_len; |
| 583 | } |
| 584 | break; |
| 585 | |
| 586 | case TYPE_COLOR_MC_HDLC_POS: |
| 587 | case TYPE_MC_HDLC: |
| 588 | packet_len = ntohs(header->wlen); |
| 589 | packet_len -= (pd->dag_fcs_bits >> 3); |
| 590 | caplen = rlen - dag_record_size - 4; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 591 | /* Skip over extension headers */ |
| 592 | caplen -= (8 * num_ext_hdr); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 593 | if (caplen > packet_len) { |
| 594 | caplen = packet_len; |
| 595 | } |
| 596 | /* jump the MC_HDLC_HEADER */ |
| 597 | dp += 4; |
| 598 | #ifdef DLT_MTP2_WITH_PHDR |
| 599 | if (p->linktype == DLT_MTP2_WITH_PHDR) { |
| 600 | /* Add the MTP2 Pseudo Header */ |
| 601 | caplen += MTP2_HDR_LEN; |
| 602 | packet_len += MTP2_HDR_LEN; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 603 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 604 | TempPkt[MTP2_SENT_OFFSET] = 0; |
| 605 | TempPkt[MTP2_ANNEX_A_USED_OFFSET] = MTP2_ANNEX_A_USED_UNKNOWN; |
| 606 | *(TempPkt+MTP2_LINK_NUMBER_OFFSET) = ((header->rec.mc_hdlc.mc_header>>16)&0x01); |
| 607 | *(TempPkt+MTP2_LINK_NUMBER_OFFSET+1) = ((header->rec.mc_hdlc.mc_header>>24)&0xff); |
| 608 | memcpy(TempPkt+MTP2_HDR_LEN, dp, caplen); |
| 609 | dp = TempPkt; |
| 610 | } |
| 611 | #endif |
| 612 | break; |
| 613 | |
| 614 | case TYPE_IPV4: |
| 615 | case TYPE_IPV6: |
| 616 | packet_len = ntohs(header->wlen); |
| 617 | caplen = rlen - dag_record_size; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 618 | /* Skip over extension headers */ |
| 619 | caplen -= (8 * num_ext_hdr); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 620 | if (caplen > packet_len) { |
| 621 | caplen = packet_len; |
| 622 | } |
| 623 | break; |
| 624 | |
| 625 | /* These types have no matching 'native' DLT, but can be used with DLT_ERF above */ |
| 626 | case TYPE_MC_RAW: |
| 627 | case TYPE_MC_RAW_CHANNEL: |
| 628 | case TYPE_IP_COUNTER: |
| 629 | case TYPE_TCP_FLOW_COUNTER: |
| 630 | case TYPE_INFINIBAND: |
| 631 | case TYPE_RAW_LINK: |
| 632 | case TYPE_INFINIBAND_LINK: |
| 633 | default: |
| 634 | /* Unhandled ERF type. |
| 635 | * Ignore rather than generating error |
| 636 | */ |
| 637 | continue; |
| 638 | } /* switch type */ |
| 639 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 640 | } /* ERF encapsulation */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 641 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 642 | if (caplen > p->snapshot) |
| 643 | caplen = p->snapshot; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 644 | |
| 645 | /* Run the packet filter if there is one. */ |
| 646 | if ((p->fcode.bf_insns == NULL) || bpf_filter(p->fcode.bf_insns, dp, packet_len, caplen)) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 647 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 648 | /* convert between timestamp formats */ |
| 649 | register unsigned long long ts; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 650 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 651 | if (IS_BIGENDIAN()) { |
| 652 | ts = SWAPLL(header->ts); |
| 653 | } else { |
| 654 | ts = header->ts; |
| 655 | } |
| 656 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 657 | switch (p->opt.tstamp_precision) { |
| 658 | case PCAP_TSTAMP_PRECISION_NANO: |
| 659 | ticks_per_second = 1000000000; |
| 660 | break; |
| 661 | case PCAP_TSTAMP_PRECISION_MICRO: |
| 662 | default: |
| 663 | ticks_per_second = 1000000; |
| 664 | break; |
| 665 | |
| 666 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 667 | pcap_header.ts.tv_sec = ts >> 32; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 668 | ts = (ts & 0xffffffffULL) * ticks_per_second; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 669 | ts += 0x80000000; /* rounding */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 670 | pcap_header.ts.tv_usec = ts >> 32; |
| 671 | if (pcap_header.ts.tv_usec >= ticks_per_second) { |
| 672 | pcap_header.ts.tv_usec -= ticks_per_second; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 673 | pcap_header.ts.tv_sec++; |
| 674 | } |
| 675 | |
| 676 | /* Fill in our own header data */ |
| 677 | pcap_header.caplen = caplen; |
| 678 | pcap_header.len = packet_len; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 679 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 680 | /* Count the packet. */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 681 | pd->stat.ps_recv++; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 682 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 683 | /* Call the user supplied callback function */ |
| 684 | callback(user, &pcap_header, dp); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 685 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 686 | /* Only count packets that pass the filter, for consistency with standard Linux behaviour. */ |
| 687 | processed++; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 688 | if (processed == cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 689 | { |
| 690 | /* Reached the user-specified limit. */ |
| 691 | return cnt; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | return processed; |
| 697 | } |
| 698 | |
| 699 | static int |
| 700 | dag_inject(pcap_t *p, const void *buf _U_, size_t size _U_) |
| 701 | { |
| 702 | strlcpy(p->errbuf, "Sending packets isn't supported on DAG cards", |
| 703 | PCAP_ERRBUF_SIZE); |
| 704 | return (-1); |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * Get a handle for a live capture from the given DAG device. Passing a NULL |
| 709 | * device will result in a failure. The promisc flag is ignored because DAG |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 710 | * cards are always promiscuous. The to_ms parameter is used in setting the |
| 711 | * API polling parameters. |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 712 | * |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 713 | * snaplen is now also ignored, until we get per-stream slen support. Set |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 714 | * slen with approprite DAG tool BEFORE pcap_activate(). |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 715 | * |
| 716 | * See also pcap(3). |
| 717 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 718 | static int dag_activate(pcap_t* handle) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 719 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 720 | struct pcap_dag *handlep = handle->priv; |
| 721 | #if 0 |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 722 | char conf[30]; /* dag configure string */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 723 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 724 | char *s; |
| 725 | int n; |
| 726 | daginf_t* daginf; |
| 727 | char * newDev = NULL; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 728 | char * device = handle->opt.device; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 729 | #ifdef HAVE_DAG_STREAMS_API |
| 730 | uint32_t mindata; |
| 731 | struct timeval maxwait; |
| 732 | struct timeval poll; |
| 733 | #endif |
| 734 | |
| 735 | if (device == NULL) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 736 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "device is NULL: %s", pcap_strerror(errno)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 737 | return -1; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 738 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 739 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 740 | /* Initialize some components of the pcap structure. */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 741 | |
| 742 | #ifdef HAVE_DAG_STREAMS_API |
| 743 | newDev = (char *)malloc(strlen(device) + 16); |
| 744 | if (newDev == NULL) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 745 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 746 | goto fail; |
| 747 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 748 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 749 | /* Parse input name to get dag device and stream number if provided */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 750 | if (dag_parse_name(device, newDev, strlen(device) + 16, &handlep->dag_stream) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 751 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 752 | goto fail; |
| 753 | } |
| 754 | device = newDev; |
| 755 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 756 | if (handlep->dag_stream%2) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 757 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_parse_name: tx (even numbered) streams not supported for capture"); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 758 | goto fail; |
| 759 | } |
| 760 | #else |
| 761 | if (strncmp(device, "/dev/", 5) != 0) { |
| 762 | newDev = (char *)malloc(strlen(device) + 5); |
| 763 | if (newDev == NULL) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 764 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Can't allocate string for device name: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 765 | goto fail; |
| 766 | } |
| 767 | strcpy(newDev, "/dev/"); |
| 768 | strcat(newDev, device); |
| 769 | device = newDev; |
| 770 | } |
| 771 | #endif /* HAVE_DAG_STREAMS_API */ |
| 772 | |
| 773 | /* setup device parameters */ |
| 774 | if((handle->fd = dag_open((char *)device)) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 775 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_open %s: %s", device, pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 776 | goto fail; |
| 777 | } |
| 778 | |
| 779 | #ifdef HAVE_DAG_STREAMS_API |
| 780 | /* Open requested stream. Can fail if already locked or on error */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 781 | if (dag_attach_stream(handle->fd, handlep->dag_stream, 0, 0) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 782 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_attach_stream: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 783 | goto failclose; |
| 784 | } |
| 785 | |
| 786 | /* Set up default poll parameters for stream |
| 787 | * Can be overridden by pcap_set_nonblock() |
| 788 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 789 | if (dag_get_stream_poll(handle->fd, handlep->dag_stream, |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 790 | &mindata, &maxwait, &poll) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 791 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 792 | goto faildetach; |
| 793 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 794 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 795 | if (handle->opt.immediate) { |
| 796 | /* Call callback immediately. |
| 797 | * XXX - is this the right way to handle this? |
| 798 | */ |
| 799 | mindata = 0; |
| 800 | } else { |
| 801 | /* Amount of data to collect in Bytes before calling callbacks. |
| 802 | * Important for efficiency, but can introduce latency |
| 803 | * at low packet rates if to_ms not set! |
| 804 | */ |
| 805 | mindata = 65536; |
| 806 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 807 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 808 | /* Obey opt.timeout (was to_ms) if supplied. This is a good idea! |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 809 | * Recommend 10-100ms. Calls will time out even if no data arrived. |
| 810 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 811 | maxwait.tv_sec = handle->opt.timeout/1000; |
| 812 | maxwait.tv_usec = (handle->opt.timeout%1000) * 1000; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 813 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 814 | if (dag_set_stream_poll(handle->fd, handlep->dag_stream, |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 815 | mindata, &maxwait, &poll) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 816 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 817 | goto faildetach; |
| 818 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 819 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 820 | #else |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 821 | if((handlep->dag_mem_base = dag_mmap(handle->fd)) == MAP_FAILED) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 822 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_mmap %s: %s", device, pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 823 | goto failclose; |
| 824 | } |
| 825 | |
| 826 | #endif /* HAVE_DAG_STREAMS_API */ |
| 827 | |
| 828 | /* XXX Not calling dag_configure() to set slen; this is unsafe in |
| 829 | * multi-stream environments as the gpp config is global. |
| 830 | * Once the firmware provides 'per-stream slen' this can be supported |
| 831 | * again via the Config API without side-effects */ |
| 832 | #if 0 |
| 833 | /* set the card snap length to the specified snaplen parameter */ |
| 834 | /* This is a really bad idea, as different cards have different |
| 835 | * valid slen ranges. Should fix in Config API. */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 836 | if (handle->snapshot == 0 || handle->snapshot > MAX_DAG_SNAPLEN) { |
| 837 | handle->snapshot = MAX_DAG_SNAPLEN; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 838 | } else if (snaplen < MIN_DAG_SNAPLEN) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 839 | handle->snapshot = MIN_DAG_SNAPLEN; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 840 | } |
| 841 | /* snap len has to be a multiple of 4 */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 842 | pcap_snprintf(conf, 30, "varlen slen=%d", (snaplen + 3) & ~3); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 843 | |
| 844 | if(dag_configure(handle->fd, conf) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 845 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE,"dag_configure %s: %s", device, pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 846 | goto faildetach; |
| 847 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 848 | #endif |
| 849 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 850 | #ifdef HAVE_DAG_STREAMS_API |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 851 | if(dag_start_stream(handle->fd, handlep->dag_stream) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 852 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start_stream %s: %s", device, pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 853 | goto faildetach; |
| 854 | } |
| 855 | #else |
| 856 | if(dag_start(handle->fd) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 857 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "dag_start %s: %s", device, pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 858 | goto failclose; |
| 859 | } |
| 860 | #endif /* HAVE_DAG_STREAMS_API */ |
| 861 | |
| 862 | /* |
| 863 | * Important! You have to ensure bottom is properly |
| 864 | * initialized to zero on startup, it won't give you |
| 865 | * a compiler warning if you make this mistake! |
| 866 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 867 | handlep->dag_mem_bottom = 0; |
| 868 | handlep->dag_mem_top = 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 869 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 870 | /* |
| 871 | * Find out how many FCS bits we should strip. |
| 872 | * First, query the card to see if it strips the FCS. |
| 873 | */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 874 | daginf = dag_info(handle->fd); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 875 | if ((0x4200 == daginf->device_code) || (0x4230 == daginf->device_code)) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 876 | /* DAG 4.2S and 4.23S already strip the FCS. Stripping the final word again truncates the packet. */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 877 | handlep->dag_fcs_bits = 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 878 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 879 | /* Note that no FCS will be supplied. */ |
| 880 | handle->linktype_ext = LT_FCS_DATALINK_EXT(0); |
| 881 | } else { |
| 882 | /* |
| 883 | * Start out assuming it's 32 bits. |
| 884 | */ |
| 885 | handlep->dag_fcs_bits = 32; |
| 886 | |
| 887 | /* Allow an environment variable to override. */ |
| 888 | if ((s = getenv("ERF_FCS_BITS")) != NULL) { |
| 889 | if ((n = atoi(s)) == 0 || n == 16 || n == 32) { |
| 890 | handlep->dag_fcs_bits = n; |
| 891 | } else { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 892 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, |
| 893 | "pcap_activate %s: bad ERF_FCS_BITS value (%d) in environment", device, n); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 894 | goto failstop; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | * Did the user request that they not be stripped? |
| 900 | */ |
| 901 | if ((s = getenv("ERF_DONT_STRIP_FCS")) != NULL) { |
| 902 | /* Yes. Note the number of bytes that will be |
| 903 | supplied. */ |
| 904 | handle->linktype_ext = LT_FCS_DATALINK_EXT(handlep->dag_fcs_bits/16); |
| 905 | |
| 906 | /* And don't strip them. */ |
| 907 | handlep->dag_fcs_bits = 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 911 | handlep->dag_timeout = handle->opt.timeout; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 912 | |
| 913 | handle->linktype = -1; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 914 | if (dag_get_datalink(handle) < 0) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 915 | goto failstop; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 916 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 917 | handle->bufsize = 0; |
| 918 | |
| 919 | if (new_pcap_dag(handle) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 920 | pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "new_pcap_dag %s: %s", device, pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 921 | goto failstop; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * "select()" and "poll()" don't work on DAG device descriptors. |
| 926 | */ |
| 927 | handle->selectable_fd = -1; |
| 928 | |
| 929 | if (newDev != NULL) { |
| 930 | free((char *)newDev); |
| 931 | } |
| 932 | |
| 933 | handle->read_op = dag_read; |
| 934 | handle->inject_op = dag_inject; |
| 935 | handle->setfilter_op = dag_setfilter; |
| 936 | handle->setdirection_op = NULL; /* Not implemented.*/ |
| 937 | handle->set_datalink_op = dag_set_datalink; |
| 938 | handle->getnonblock_op = pcap_getnonblock_fd; |
| 939 | handle->setnonblock_op = dag_setnonblock; |
| 940 | handle->stats_op = dag_stats; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 941 | handle->cleanup_op = dag_platform_cleanup; |
| 942 | handlep->stat.ps_drop = 0; |
| 943 | handlep->stat.ps_recv = 0; |
| 944 | handlep->stat.ps_ifdrop = 0; |
| 945 | return 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 946 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 947 | #ifdef HAVE_DAG_STREAMS_API |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 948 | failstop: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 949 | if (dag_stop_stream(handle->fd, handlep->dag_stream) < 0) { |
| 950 | fprintf(stderr,"dag_stop_stream: %s\n", strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 951 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 952 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 953 | faildetach: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 954 | if (dag_detach_stream(handle->fd, handlep->dag_stream) < 0) |
| 955 | fprintf(stderr,"dag_detach_stream: %s\n", strerror(errno)); |
| 956 | #else |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 957 | failstop: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 958 | if (dag_stop(handle->fd) < 0) |
| 959 | fprintf(stderr,"dag_stop: %s\n", strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 960 | #endif /* HAVE_DAG_STREAMS_API */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 961 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 962 | failclose: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 963 | if (dag_close(handle->fd) < 0) |
| 964 | fprintf(stderr,"dag_close: %s\n", strerror(errno)); |
| 965 | delete_pcap_dag(handle); |
| 966 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 967 | fail: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 968 | pcap_cleanup_live_common(handle); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 969 | if (newDev != NULL) { |
| 970 | free((char *)newDev); |
| 971 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 972 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 973 | return PCAP_ERROR; |
| 974 | } |
| 975 | |
| 976 | pcap_t *dag_create(const char *device, char *ebuf, int *is_ours) |
| 977 | { |
| 978 | const char *cp; |
| 979 | char *cpend; |
| 980 | long devnum; |
| 981 | pcap_t *p; |
| 982 | #ifdef HAVE_DAG_STREAMS_API |
| 983 | long stream = 0; |
| 984 | #endif |
| 985 | |
| 986 | /* Does this look like a DAG device? */ |
| 987 | cp = strrchr(device, '/'); |
| 988 | if (cp == NULL) |
| 989 | cp = device; |
| 990 | /* Does it begin with "dag"? */ |
| 991 | if (strncmp(cp, "dag", 3) != 0) { |
| 992 | /* Nope, doesn't begin with "dag" */ |
| 993 | *is_ours = 0; |
| 994 | return NULL; |
| 995 | } |
| 996 | /* Yes - is "dag" followed by a number from 0 to DAG_MAX_BOARDS-1 */ |
| 997 | cp += 3; |
| 998 | devnum = strtol(cp, &cpend, 10); |
| 999 | #ifdef HAVE_DAG_STREAMS_API |
| 1000 | if (*cpend == ':') { |
| 1001 | /* Followed by a stream number. */ |
| 1002 | stream = strtol(++cpend, &cpend, 10); |
| 1003 | } |
| 1004 | #endif |
| 1005 | if (cpend == cp || *cpend != '\0') { |
| 1006 | /* Not followed by a number. */ |
| 1007 | *is_ours = 0; |
| 1008 | return NULL; |
| 1009 | } |
| 1010 | if (devnum < 0 || devnum >= DAG_MAX_BOARDS) { |
| 1011 | /* Followed by a non-valid number. */ |
| 1012 | *is_ours = 0; |
| 1013 | return NULL; |
| 1014 | } |
| 1015 | #ifdef HAVE_DAG_STREAMS_API |
| 1016 | if (stream <0 || stream >= DAG_STREAM_MAX) { |
| 1017 | /* Followed by a non-valid stream number. */ |
| 1018 | *is_ours = 0; |
| 1019 | return NULL; |
| 1020 | } |
| 1021 | #endif |
| 1022 | |
| 1023 | /* OK, it's probably ours. */ |
| 1024 | *is_ours = 1; |
| 1025 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1026 | p = pcap_create_common(ebuf, sizeof (struct pcap_dag)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1027 | if (p == NULL) |
| 1028 | return NULL; |
| 1029 | |
| 1030 | p->activate_op = dag_activate; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1031 | |
| 1032 | /* |
| 1033 | * We claim that we support microsecond and nanosecond time |
| 1034 | * stamps. |
| 1035 | * |
| 1036 | * XXX Our native precision is 2^-32s, but libpcap doesn't support |
| 1037 | * power of two precisions yet. We can convert to either MICRO or NANO. |
| 1038 | */ |
| 1039 | p->tstamp_precision_count = 2; |
| 1040 | p->tstamp_precision_list = malloc(2 * sizeof(u_int)); |
| 1041 | if (p->tstamp_precision_list == NULL) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1042 | pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1043 | pcap_strerror(errno)); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1044 | pcap_close(p); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1045 | return NULL; |
| 1046 | } |
| 1047 | p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; |
| 1048 | p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1049 | return p; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | static int |
| 1053 | dag_stats(pcap_t *p, struct pcap_stat *ps) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1054 | struct pcap_dag *pd = p->priv; |
| 1055 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1056 | /* This needs to be filled out correctly. Hopefully a dagapi call will |
| 1057 | provide all necessary information. |
| 1058 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1059 | /*pd->stat.ps_recv = 0;*/ |
| 1060 | /*pd->stat.ps_drop = 0;*/ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1061 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1062 | *ps = pd->stat; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1063 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1064 | return 0; |
| 1065 | } |
| 1066 | |
| 1067 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1068 | * Previously we just generated a list of all possible names and let |
| 1069 | * pcap_add_if() attempt to open each one, but with streams this adds up |
| 1070 | * to 81 possibilities which is inefficient. |
| 1071 | * |
| 1072 | * Since we know more about the devices we can prune the tree here. |
| 1073 | * pcap_add_if() will still retest each device but the total number of |
| 1074 | * open attempts will still be much less than the naive approach. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1075 | */ |
| 1076 | int |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1077 | dag_findalldevs(pcap_if_t **devlistp, char *errbuf) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1078 | { |
| 1079 | char name[12]; /* XXX - pick a size */ |
| 1080 | int ret = 0; |
| 1081 | int c; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1082 | char dagname[DAGNAME_BUFSIZE]; |
| 1083 | int dagstream; |
| 1084 | int dagfd; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1085 | dag_card_inf_t *inf; |
| 1086 | char *description; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1087 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1088 | /* Try all the DAGs 0-DAG_MAX_BOARDS */ |
| 1089 | for (c = 0; c < DAG_MAX_BOARDS; c++) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1090 | pcap_snprintf(name, 12, "dag%d", c); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1091 | if (-1 == dag_parse_name(name, dagname, DAGNAME_BUFSIZE, &dagstream)) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1092 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1093 | return -1; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1094 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1095 | description = NULL; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1096 | if ( (dagfd = dag_open(dagname)) >= 0 ) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1097 | if ((inf = dag_pciinfo(dagfd))) |
| 1098 | description = dag_device_name(inf->device_code, 1); |
| 1099 | if (pcap_add_if(devlistp, name, 0, description, errbuf) == -1) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1100 | /* |
| 1101 | * Failure. |
| 1102 | */ |
| 1103 | ret = -1; |
| 1104 | } |
| 1105 | #ifdef HAVE_DAG_STREAMS_API |
| 1106 | { |
| 1107 | int stream, rxstreams; |
| 1108 | rxstreams = dag_rx_get_stream_count(dagfd); |
| 1109 | for(stream=0;stream<DAG_STREAM_MAX;stream+=2) { |
| 1110 | if (0 == dag_attach_stream(dagfd, stream, 0, 0)) { |
| 1111 | dag_detach_stream(dagfd, stream); |
| 1112 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1113 | pcap_snprintf(name, 10, "dag%d:%d", c, stream); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1114 | if (pcap_add_if(devlistp, name, 0, description, errbuf) == -1) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1115 | /* |
| 1116 | * Failure. |
| 1117 | */ |
| 1118 | ret = -1; |
| 1119 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1120 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1121 | rxstreams--; |
| 1122 | if(rxstreams <= 0) { |
| 1123 | break; |
| 1124 | } |
| 1125 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1126 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1127 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1128 | #endif /* HAVE_DAG_STREAMS_API */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1129 | dag_close(dagfd); |
| 1130 | } |
| 1131 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1132 | } |
| 1133 | return (ret); |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * Installs the given bpf filter program in the given pcap structure. There is |
| 1138 | * no attempt to store the filter in kernel memory as that is not supported |
| 1139 | * with DAG cards. |
| 1140 | */ |
| 1141 | static int |
| 1142 | dag_setfilter(pcap_t *p, struct bpf_program *fp) |
| 1143 | { |
| 1144 | if (!p) |
| 1145 | return -1; |
| 1146 | if (!fp) { |
| 1147 | strncpy(p->errbuf, "setfilter: No filter specified", |
| 1148 | sizeof(p->errbuf)); |
| 1149 | return -1; |
| 1150 | } |
| 1151 | |
| 1152 | /* Make our private copy of the filter */ |
| 1153 | |
| 1154 | if (install_bpf_program(p, fp) < 0) |
| 1155 | return -1; |
| 1156 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1157 | return (0); |
| 1158 | } |
| 1159 | |
| 1160 | static int |
| 1161 | dag_set_datalink(pcap_t *p, int dlt) |
| 1162 | { |
| 1163 | p->linktype = dlt; |
| 1164 | |
| 1165 | return (0); |
| 1166 | } |
| 1167 | |
| 1168 | static int |
| 1169 | dag_setnonblock(pcap_t *p, int nonblock, char *errbuf) |
| 1170 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1171 | struct pcap_dag *pd = p->priv; |
| 1172 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1173 | /* |
| 1174 | * Set non-blocking mode on the FD. |
| 1175 | * XXX - is that necessary? If not, don't bother calling it, |
| 1176 | * and have a "dag_getnonblock()" function that looks at |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1177 | * "pd->dag_offset_flags". |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1178 | */ |
| 1179 | if (pcap_setnonblock_fd(p, nonblock, errbuf) < 0) |
| 1180 | return (-1); |
| 1181 | #ifdef HAVE_DAG_STREAMS_API |
| 1182 | { |
| 1183 | uint32_t mindata; |
| 1184 | struct timeval maxwait; |
| 1185 | struct timeval poll; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1186 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1187 | if (dag_get_stream_poll(p->fd, pd->dag_stream, |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1188 | &mindata, &maxwait, &poll) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1189 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_get_stream_poll: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1190 | return -1; |
| 1191 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1192 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1193 | /* Amount of data to collect in Bytes before calling callbacks. |
| 1194 | * Important for efficiency, but can introduce latency |
| 1195 | * at low packet rates if to_ms not set! |
| 1196 | */ |
| 1197 | if(nonblock) |
| 1198 | mindata = 0; |
| 1199 | else |
| 1200 | mindata = 65536; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1201 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1202 | if (dag_set_stream_poll(p->fd, pd->dag_stream, |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1203 | mindata, &maxwait, &poll) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1204 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "dag_set_stream_poll: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1205 | return -1; |
| 1206 | } |
| 1207 | } |
| 1208 | #endif /* HAVE_DAG_STREAMS_API */ |
| 1209 | if (nonblock) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1210 | pd->dag_offset_flags |= DAGF_NONBLOCK; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1211 | } else { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1212 | pd->dag_offset_flags &= ~DAGF_NONBLOCK; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1213 | } |
| 1214 | return (0); |
| 1215 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1216 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1217 | static int |
| 1218 | dag_get_datalink(pcap_t *p) |
| 1219 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1220 | struct pcap_dag *pd = p->priv; |
| 1221 | int index=0, dlt_index=0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1222 | uint8_t types[255]; |
| 1223 | |
| 1224 | memset(types, 0, 255); |
| 1225 | |
| 1226 | if (p->dlt_list == NULL && (p->dlt_list = malloc(255*sizeof(*(p->dlt_list)))) == NULL) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1227 | (void)pcap_snprintf(p->errbuf, sizeof(p->errbuf), "malloc: %s", pcap_strerror(errno)); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1228 | return (-1); |
| 1229 | } |
| 1230 | |
| 1231 | p->linktype = 0; |
| 1232 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1233 | #ifdef HAVE_DAG_GET_STREAM_ERF_TYPES |
| 1234 | /* Get list of possible ERF types for this card */ |
| 1235 | if (dag_get_stream_erf_types(p->fd, pd->dag_stream, types, 255) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1236 | pcap_snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_stream_erf_types: %s", pcap_strerror(errno)); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1237 | return (-1); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1238 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1239 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1240 | while (types[index]) { |
| 1241 | |
| 1242 | #elif defined HAVE_DAG_GET_ERF_TYPES |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1243 | /* Get list of possible ERF types for this card */ |
| 1244 | if (dag_get_erf_types(p->fd, types, 255) < 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1245 | pcap_snprintf(p->errbuf, sizeof(p->errbuf), "dag_get_erf_types: %s", pcap_strerror(errno)); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1246 | return (-1); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1247 | } |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1248 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1249 | while (types[index]) { |
| 1250 | #else |
| 1251 | /* Check the type through a dagapi call. */ |
| 1252 | types[index] = dag_linktype(p->fd); |
| 1253 | |
| 1254 | { |
| 1255 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1256 | switch((types[index] & 0x7f)) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1257 | |
| 1258 | case TYPE_HDLC_POS: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1259 | case TYPE_COLOR_HDLC_POS: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1260 | case TYPE_DSM_COLOR_HDLC_POS: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1261 | case TYPE_COLOR_HASH_POS: |
| 1262 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1263 | if (p->dlt_list != NULL) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1264 | p->dlt_list[dlt_index++] = DLT_CHDLC; |
| 1265 | p->dlt_list[dlt_index++] = DLT_PPP_SERIAL; |
| 1266 | p->dlt_list[dlt_index++] = DLT_FRELAY; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1267 | } |
| 1268 | if(!p->linktype) |
| 1269 | p->linktype = DLT_CHDLC; |
| 1270 | break; |
| 1271 | |
| 1272 | case TYPE_ETH: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1273 | case TYPE_COLOR_ETH: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1274 | case TYPE_DSM_COLOR_ETH: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1275 | case TYPE_COLOR_HASH_ETH: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1276 | /* |
| 1277 | * This is (presumably) a real Ethernet capture; give it a |
| 1278 | * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so |
| 1279 | * that an application can let you choose it, in case you're |
| 1280 | * capturing DOCSIS traffic that a Cisco Cable Modem |
| 1281 | * Termination System is putting out onto an Ethernet (it |
| 1282 | * doesn't put an Ethernet header onto the wire, it puts raw |
| 1283 | * DOCSIS frames out on the wire inside the low-level |
| 1284 | * Ethernet framing). |
| 1285 | */ |
| 1286 | if (p->dlt_list != NULL) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1287 | p->dlt_list[dlt_index++] = DLT_EN10MB; |
| 1288 | p->dlt_list[dlt_index++] = DLT_DOCSIS; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1289 | } |
| 1290 | if(!p->linktype) |
| 1291 | p->linktype = DLT_EN10MB; |
| 1292 | break; |
| 1293 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1294 | case TYPE_ATM: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1295 | case TYPE_AAL5: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1296 | case TYPE_MC_ATM: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1297 | case TYPE_MC_AAL5: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1298 | if (p->dlt_list != NULL) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1299 | p->dlt_list[dlt_index++] = DLT_ATM_RFC1483; |
| 1300 | p->dlt_list[dlt_index++] = DLT_SUNATM; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1301 | } |
| 1302 | if(!p->linktype) |
| 1303 | p->linktype = DLT_ATM_RFC1483; |
| 1304 | break; |
| 1305 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1306 | case TYPE_COLOR_MC_HDLC_POS: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1307 | case TYPE_MC_HDLC: |
| 1308 | if (p->dlt_list != NULL) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1309 | p->dlt_list[dlt_index++] = DLT_CHDLC; |
| 1310 | p->dlt_list[dlt_index++] = DLT_PPP_SERIAL; |
| 1311 | p->dlt_list[dlt_index++] = DLT_FRELAY; |
| 1312 | p->dlt_list[dlt_index++] = DLT_MTP2; |
| 1313 | p->dlt_list[dlt_index++] = DLT_MTP2_WITH_PHDR; |
| 1314 | p->dlt_list[dlt_index++] = DLT_LAPD; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1315 | } |
| 1316 | if(!p->linktype) |
| 1317 | p->linktype = DLT_CHDLC; |
| 1318 | break; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1319 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1320 | case TYPE_IPV4: |
| 1321 | case TYPE_IPV6: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1322 | if(!p->linktype) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1323 | p->linktype = DLT_RAW; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1324 | break; |
| 1325 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1326 | case TYPE_LEGACY: |
| 1327 | case TYPE_MC_RAW: |
| 1328 | case TYPE_MC_RAW_CHANNEL: |
| 1329 | case TYPE_IP_COUNTER: |
| 1330 | case TYPE_TCP_FLOW_COUNTER: |
| 1331 | case TYPE_INFINIBAND: |
| 1332 | case TYPE_RAW_LINK: |
| 1333 | case TYPE_INFINIBAND_LINK: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1334 | default: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1335 | /* Libpcap cannot deal with these types yet */ |
| 1336 | /* Add no 'native' DLTs, but still covered by DLT_ERF */ |
| 1337 | break; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1338 | |
| 1339 | } /* switch */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1340 | index++; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1343 | p->dlt_list[dlt_index++] = DLT_ERF; |
| 1344 | |
| 1345 | p->dlt_count = dlt_index; |
| 1346 | |
| 1347 | if(!p->linktype) |
| 1348 | p->linktype = DLT_ERF; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1349 | |
| 1350 | return p->linktype; |
| 1351 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1352 | |
| 1353 | #ifdef DAG_ONLY |
| 1354 | /* |
| 1355 | * This libpcap build supports only DAG cards, not regular network |
| 1356 | * interfaces. |
| 1357 | */ |
| 1358 | |
| 1359 | /* |
| 1360 | * There are no regular interfaces, just DAG interfaces. |
| 1361 | */ |
| 1362 | int |
| 1363 | pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) |
| 1364 | { |
| 1365 | *alldevsp = NULL; |
| 1366 | return (0); |
| 1367 | } |
| 1368 | |
| 1369 | /* |
| 1370 | * Attempts to open a regular interface fail. |
| 1371 | */ |
| 1372 | pcap_t * |
| 1373 | pcap_create_interface(const char *device, char *errbuf) |
| 1374 | { |
| 1375 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
| 1376 | "This version of libpcap only supports DAG cards"); |
| 1377 | return NULL; |
| 1378 | } |
| 1379 | #endif |