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