The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1993, 1994, 1995, 1996, 1998 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that: (1) source code distributions |
| 7 | * retain the above copyright notice and this paragraph in its entirety, (2) |
| 8 | * distributions including binary code include the above copyright notice and |
| 9 | * this paragraph in its entirety in the documentation or other materials |
| 10 | * provided with the distribution, and (3) all advertising materials mentioning |
| 11 | * features or use of this software display the following acknowledgement: |
| 12 | * ``This product includes software developed by the University of California, |
| 13 | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of |
| 14 | * the University nor the names of its contributors may be used to endorse |
| 15 | * or promote products derived from this software without specific prior |
| 16 | * written permission. |
| 17 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 20 | */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 23 | #include <config.h> |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 24 | #endif |
| 25 | |
| 26 | #include <sys/param.h> /* optionally get BSD define */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 27 | #include <sys/socket.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 28 | #include <time.h> |
| 29 | /* |
| 30 | * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>. |
| 31 | * |
| 32 | * We include <sys/ioctl.h> as it might be necessary to declare ioctl(); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 33 | * at least on *BSD and macOS, it also defines various SIOC ioctls - |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 34 | * we could include <sys/sockio.h>, but if we're already including |
| 35 | * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms, |
| 36 | * there's not much point in doing so. |
| 37 | * |
| 38 | * If we have <sys/ioccom.h>, we include it as well, to handle systems |
| 39 | * such as Solaris which don't arrange to include <sys/ioccom.h> if you |
| 40 | * include <sys/ioctl.h> |
| 41 | */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 42 | #include <sys/ioctl.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 43 | #ifdef HAVE_SYS_IOCCOM_H |
| 44 | #include <sys/ioccom.h> |
| 45 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 46 | #include <sys/utsname.h> |
| 47 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 48 | #if defined(__FreeBSD__) && defined(SIOCIFCREATE2) |
| 49 | /* |
| 50 | * Add support for capturing on FreeBSD usbusN interfaces. |
| 51 | */ |
| 52 | static const char usbus_prefix[] = "usbus"; |
| 53 | #define USBUS_PREFIX_LEN (sizeof(usbus_prefix) - 1) |
| 54 | #include <dirent.h> |
| 55 | #endif |
| 56 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 57 | #include <net/if.h> |
| 58 | |
| 59 | #ifdef _AIX |
| 60 | |
| 61 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 62 | * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 63 | * native OS version, as we need "struct bpf_config" from it. |
| 64 | */ |
| 65 | #define PCAP_DONT_INCLUDE_PCAP_BPF_H |
| 66 | |
| 67 | #include <sys/types.h> |
| 68 | |
| 69 | /* |
| 70 | * Prevent bpf.h from redefining the DLT_ values to their |
| 71 | * IFT_ values, as we're going to return the standard libpcap |
| 72 | * values, not IBM's non-standard IFT_ values. |
| 73 | */ |
| 74 | #undef _AIX |
| 75 | #include <net/bpf.h> |
| 76 | #define _AIX |
| 77 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 78 | /* |
| 79 | * If both BIOCROTZBUF and BPF_BUFMODE_ZBUF are defined, we have |
| 80 | * zero-copy BPF. |
| 81 | */ |
| 82 | #if defined(BIOCROTZBUF) && defined(BPF_BUFMODE_ZBUF) |
| 83 | #define HAVE_ZEROCOPY_BPF |
| 84 | #include <sys/mman.h> |
| 85 | #include <machine/atomic.h> |
| 86 | #endif |
| 87 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 88 | #include <net/if_types.h> /* for IFT_ values */ |
| 89 | #include <sys/sysconfig.h> |
| 90 | #include <sys/device.h> |
| 91 | #include <sys/cfgodm.h> |
| 92 | #include <cf.h> |
| 93 | |
| 94 | #ifdef __64BIT__ |
| 95 | #define domakedev makedev64 |
| 96 | #define getmajor major64 |
| 97 | #define bpf_hdr bpf_hdr32 |
| 98 | #else /* __64BIT__ */ |
| 99 | #define domakedev makedev |
| 100 | #define getmajor major |
| 101 | #endif /* __64BIT__ */ |
| 102 | |
| 103 | #define BPF_NAME "bpf" |
| 104 | #define BPF_MINORS 4 |
| 105 | #define DRIVER_PATH "/usr/lib/drivers" |
| 106 | #define BPF_NODE "/dev/bpf" |
| 107 | static int bpfloadedflag = 0; |
| 108 | static int odmlockid = 0; |
| 109 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 110 | static int bpf_load(char *errbuf); |
| 111 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 112 | #else /* _AIX */ |
| 113 | |
| 114 | #include <net/bpf.h> |
| 115 | |
| 116 | #endif /* _AIX */ |
| 117 | |
| 118 | #include <ctype.h> |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 119 | #include <fcntl.h> |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 120 | #include <errno.h> |
| 121 | #include <netdb.h> |
| 122 | #include <stdio.h> |
| 123 | #include <stdlib.h> |
| 124 | #include <string.h> |
| 125 | #include <unistd.h> |
| 126 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 127 | #ifdef SIOCGIFMEDIA |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 128 | # include <net/if_media.h> |
| 129 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 130 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 131 | #include "pcap-int.h" |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 132 | |
| 133 | #ifdef HAVE_OS_PROTO_H |
| 134 | #include "os-proto.h" |
| 135 | #endif |
| 136 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 137 | /* |
| 138 | * Later versions of NetBSD stick padding in front of FDDI frames |
| 139 | * to align the IP header on a 4-byte boundary. |
| 140 | */ |
| 141 | #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000 |
| 142 | #define PCAP_FDDIPAD 3 |
| 143 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 144 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 145 | /* |
| 146 | * Private data for capturing on BPF devices. |
| 147 | */ |
| 148 | struct pcap_bpf { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 149 | #ifdef HAVE_ZEROCOPY_BPF |
| 150 | /* |
| 151 | * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will |
| 152 | * alternative between these two actual mmap'd buffers as required. |
| 153 | * As there is a header on the front size of the mmap'd buffer, only |
| 154 | * some of the buffer is exposed to libpcap as a whole via bufsize; |
| 155 | * zbufsize is the true size. zbuffer tracks the current zbuf |
| 156 | * assocated with buffer so that it can be used to decide which the |
| 157 | * next buffer to read will be. |
| 158 | */ |
| 159 | u_char *zbuf1, *zbuf2, *zbuffer; |
| 160 | u_int zbufsize; |
| 161 | u_int zerocopy; |
| 162 | u_int interrupted; |
| 163 | struct timespec firstsel; |
| 164 | /* |
| 165 | * If there's currently a buffer being actively processed, then it is |
| 166 | * referenced here; 'buffer' is also pointed at it, but offset by the |
| 167 | * size of the header. |
| 168 | */ |
| 169 | struct bpf_zbuf_header *bzh; |
| 170 | int nonblock; /* true if in nonblocking mode */ |
| 171 | #endif /* HAVE_ZEROCOPY_BPF */ |
| 172 | |
| 173 | char *device; /* device name */ |
| 174 | int filtering_in_kernel; /* using kernel filter */ |
| 175 | int must_do_on_close; /* stuff we must do when we close */ |
| 176 | }; |
| 177 | |
| 178 | /* |
| 179 | * Stuff to do when we close. |
| 180 | */ |
| 181 | #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 182 | #define MUST_DESTROY_USBUS 0x00000002 /* destroy usbusN interface */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 183 | |
| 184 | #ifdef BIOCGDLTLIST |
| 185 | # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__) |
| 186 | #define HAVE_BSD_IEEE80211 |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * The ifm_ulist member of a struct ifmediareq is an int * on most systems, |
| 190 | * but it's a uint64_t on newer versions of OpenBSD. |
| 191 | * |
| 192 | * We check this by checking whether IFM_GMASK is defined and > 2^32-1. |
| 193 | */ |
| 194 | # if defined(IFM_GMASK) && IFM_GMASK > 0xFFFFFFFF |
| 195 | # define IFM_ULIST_TYPE uint64_t |
| 196 | # else |
| 197 | # define IFM_ULIST_TYPE int |
| 198 | # endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 199 | # endif |
| 200 | |
| 201 | # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) |
| 202 | static int find_802_11(struct bpf_dltlist *); |
| 203 | |
| 204 | # ifdef HAVE_BSD_IEEE80211 |
| 205 | static int monitor_mode(pcap_t *, int); |
| 206 | # endif |
| 207 | |
| 208 | # if defined(__APPLE__) |
| 209 | static void remove_en(pcap_t *); |
| 210 | static void remove_802_11(pcap_t *); |
| 211 | # endif |
| 212 | |
| 213 | # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */ |
| 214 | |
| 215 | #endif /* BIOCGDLTLIST */ |
| 216 | |
| 217 | #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid) |
| 218 | #include <zone.h> |
| 219 | #endif |
| 220 | |
| 221 | /* |
| 222 | * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably |
| 223 | * don't get DLT_DOCSIS defined. |
| 224 | */ |
| 225 | #ifndef DLT_DOCSIS |
| 226 | #define DLT_DOCSIS 143 |
| 227 | #endif |
| 228 | |
| 229 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 230 | * In some versions of macOS, we might not even get any of the |
| 231 | * 802.11-plus-radio-header DLT_'s defined, even though some |
| 232 | * of them are used by various Airport drivers in those versions. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 233 | */ |
| 234 | #ifndef DLT_PRISM_HEADER |
| 235 | #define DLT_PRISM_HEADER 119 |
| 236 | #endif |
| 237 | #ifndef DLT_AIRONET_HEADER |
| 238 | #define DLT_AIRONET_HEADER 120 |
| 239 | #endif |
| 240 | #ifndef DLT_IEEE802_11_RADIO |
| 241 | #define DLT_IEEE802_11_RADIO 127 |
| 242 | #endif |
| 243 | #ifndef DLT_IEEE802_11_RADIO_AVS |
| 244 | #define DLT_IEEE802_11_RADIO_AVS 163 |
| 245 | #endif |
| 246 | |
| 247 | static int pcap_can_set_rfmon_bpf(pcap_t *p); |
| 248 | static int pcap_activate_bpf(pcap_t *p); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 249 | static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp); |
| 250 | static int pcap_setdirection_bpf(pcap_t *, pcap_direction_t); |
| 251 | static int pcap_set_datalink_bpf(pcap_t *p, int dlt); |
| 252 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 253 | /* |
| 254 | * For zerocopy bpf, the setnonblock/getnonblock routines need to modify |
| 255 | * pb->nonblock so we don't call select(2) if the pcap handle is in non- |
| 256 | * blocking mode. |
| 257 | */ |
| 258 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 259 | pcap_getnonblock_bpf(pcap_t *p) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 260 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 261 | #ifdef HAVE_ZEROCOPY_BPF |
| 262 | struct pcap_bpf *pb = p->priv; |
| 263 | |
| 264 | if (pb->zerocopy) |
| 265 | return (pb->nonblock); |
| 266 | #endif |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 267 | return (pcap_getnonblock_fd(p)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 271 | pcap_setnonblock_bpf(pcap_t *p, int nonblock) |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 272 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 273 | #ifdef HAVE_ZEROCOPY_BPF |
| 274 | struct pcap_bpf *pb = p->priv; |
| 275 | |
| 276 | if (pb->zerocopy) { |
| 277 | pb->nonblock = nonblock; |
| 278 | return (0); |
| 279 | } |
| 280 | #endif |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 281 | return (pcap_setnonblock_fd(p, nonblock)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | #ifdef HAVE_ZEROCOPY_BPF |
| 285 | /* |
| 286 | * Zero-copy BPF buffer routines to check for and acknowledge BPF data in |
| 287 | * shared memory buffers. |
| 288 | * |
| 289 | * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer, |
| 290 | * and set up p->buffer and cc to reflect one if available. Notice that if |
| 291 | * there was no prior buffer, we select zbuf1 as this will be the first |
| 292 | * buffer filled for a fresh BPF session. |
| 293 | */ |
| 294 | static int |
| 295 | pcap_next_zbuf_shm(pcap_t *p, int *cc) |
| 296 | { |
| 297 | struct pcap_bpf *pb = p->priv; |
| 298 | struct bpf_zbuf_header *bzh; |
| 299 | |
| 300 | if (pb->zbuffer == pb->zbuf2 || pb->zbuffer == NULL) { |
| 301 | bzh = (struct bpf_zbuf_header *)pb->zbuf1; |
| 302 | if (bzh->bzh_user_gen != |
| 303 | atomic_load_acq_int(&bzh->bzh_kernel_gen)) { |
| 304 | pb->bzh = bzh; |
| 305 | pb->zbuffer = (u_char *)pb->zbuf1; |
| 306 | p->buffer = pb->zbuffer + sizeof(*bzh); |
| 307 | *cc = bzh->bzh_kernel_len; |
| 308 | return (1); |
| 309 | } |
| 310 | } else if (pb->zbuffer == pb->zbuf1) { |
| 311 | bzh = (struct bpf_zbuf_header *)pb->zbuf2; |
| 312 | if (bzh->bzh_user_gen != |
| 313 | atomic_load_acq_int(&bzh->bzh_kernel_gen)) { |
| 314 | pb->bzh = bzh; |
| 315 | pb->zbuffer = (u_char *)pb->zbuf2; |
| 316 | p->buffer = pb->zbuffer + sizeof(*bzh); |
| 317 | *cc = bzh->bzh_kernel_len; |
| 318 | return (1); |
| 319 | } |
| 320 | } |
| 321 | *cc = 0; |
| 322 | return (0); |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using |
| 327 | * select() for data or a timeout, and possibly force rotation of the buffer |
| 328 | * in the event we time out or are in immediate mode. Invoke the shared |
| 329 | * memory check before doing system calls in order to avoid doing avoidable |
| 330 | * work. |
| 331 | */ |
| 332 | static int |
| 333 | pcap_next_zbuf(pcap_t *p, int *cc) |
| 334 | { |
| 335 | struct pcap_bpf *pb = p->priv; |
| 336 | struct bpf_zbuf bz; |
| 337 | struct timeval tv; |
| 338 | struct timespec cur; |
| 339 | fd_set r_set; |
| 340 | int data, r; |
| 341 | int expire, tmout; |
| 342 | |
| 343 | #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000)) |
| 344 | /* |
| 345 | * Start out by seeing whether anything is waiting by checking the |
| 346 | * next shared memory buffer for data. |
| 347 | */ |
| 348 | data = pcap_next_zbuf_shm(p, cc); |
| 349 | if (data) |
| 350 | return (data); |
| 351 | /* |
| 352 | * If a previous sleep was interrupted due to signal delivery, make |
| 353 | * sure that the timeout gets adjusted accordingly. This requires |
| 354 | * that we analyze when the timeout should be been expired, and |
| 355 | * subtract the current time from that. If after this operation, |
| 356 | * our timeout is less then or equal to zero, handle it like a |
| 357 | * regular timeout. |
| 358 | */ |
| 359 | tmout = p->opt.timeout; |
| 360 | if (tmout) |
| 361 | (void) clock_gettime(CLOCK_MONOTONIC, &cur); |
| 362 | if (pb->interrupted && p->opt.timeout) { |
| 363 | expire = TSTOMILLI(&pb->firstsel) + p->opt.timeout; |
| 364 | tmout = expire - TSTOMILLI(&cur); |
| 365 | #undef TSTOMILLI |
| 366 | if (tmout <= 0) { |
| 367 | pb->interrupted = 0; |
| 368 | data = pcap_next_zbuf_shm(p, cc); |
| 369 | if (data) |
| 370 | return (data); |
| 371 | if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 372 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 373 | PCAP_ERRBUF_SIZE, errno, "BIOCROTZBUF"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 374 | return (PCAP_ERROR); |
| 375 | } |
| 376 | return (pcap_next_zbuf_shm(p, cc)); |
| 377 | } |
| 378 | } |
| 379 | /* |
| 380 | * No data in the buffer, so must use select() to wait for data or |
| 381 | * the next timeout. Note that we only call select if the handle |
| 382 | * is in blocking mode. |
| 383 | */ |
| 384 | if (!pb->nonblock) { |
| 385 | FD_ZERO(&r_set); |
| 386 | FD_SET(p->fd, &r_set); |
| 387 | if (tmout != 0) { |
| 388 | tv.tv_sec = tmout / 1000; |
| 389 | tv.tv_usec = (tmout * 1000) % 1000000; |
| 390 | } |
| 391 | r = select(p->fd + 1, &r_set, NULL, NULL, |
| 392 | p->opt.timeout != 0 ? &tv : NULL); |
| 393 | if (r < 0 && errno == EINTR) { |
| 394 | if (!pb->interrupted && p->opt.timeout) { |
| 395 | pb->interrupted = 1; |
| 396 | pb->firstsel = cur; |
| 397 | } |
| 398 | return (0); |
| 399 | } else if (r < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 400 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 401 | errno, "select"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 402 | return (PCAP_ERROR); |
| 403 | } |
| 404 | } |
| 405 | pb->interrupted = 0; |
| 406 | /* |
| 407 | * Check again for data, which may exist now that we've either been |
| 408 | * woken up as a result of data or timed out. Try the "there's data" |
| 409 | * case first since it doesn't require a system call. |
| 410 | */ |
| 411 | data = pcap_next_zbuf_shm(p, cc); |
| 412 | if (data) |
| 413 | return (data); |
| 414 | /* |
| 415 | * Try forcing a buffer rotation to dislodge timed out or immediate |
| 416 | * data. |
| 417 | */ |
| 418 | if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 419 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 420 | errno, "BIOCROTZBUF"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 421 | return (PCAP_ERROR); |
| 422 | } |
| 423 | return (pcap_next_zbuf_shm(p, cc)); |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Notify kernel that we are done with the buffer. We don't reset zbuffer so |
| 428 | * that we know which buffer to use next time around. |
| 429 | */ |
| 430 | static int |
| 431 | pcap_ack_zbuf(pcap_t *p) |
| 432 | { |
| 433 | struct pcap_bpf *pb = p->priv; |
| 434 | |
| 435 | atomic_store_rel_int(&pb->bzh->bzh_user_gen, |
| 436 | pb->bzh->bzh_kernel_gen); |
| 437 | pb->bzh = NULL; |
| 438 | p->buffer = NULL; |
| 439 | return (0); |
| 440 | } |
| 441 | #endif /* HAVE_ZEROCOPY_BPF */ |
| 442 | |
| 443 | pcap_t * |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 444 | pcap_create_interface(const char *device _U_, char *ebuf) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 445 | { |
| 446 | pcap_t *p; |
| 447 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 448 | p = pcap_create_common(ebuf, sizeof (struct pcap_bpf)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 449 | if (p == NULL) |
| 450 | return (NULL); |
| 451 | |
| 452 | p->activate_op = pcap_activate_bpf; |
| 453 | p->can_set_rfmon_op = pcap_can_set_rfmon_bpf; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 454 | #ifdef BIOCSTSTAMP |
| 455 | /* |
| 456 | * We claim that we support microsecond and nanosecond time |
| 457 | * stamps. |
| 458 | */ |
| 459 | p->tstamp_precision_count = 2; |
| 460 | p->tstamp_precision_list = malloc(2 * sizeof(u_int)); |
| 461 | if (p->tstamp_precision_list == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 462 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, |
| 463 | "malloc"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 464 | free(p); |
| 465 | return (NULL); |
| 466 | } |
| 467 | p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; |
| 468 | p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; |
| 469 | #endif /* BIOCSTSTAMP */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 470 | return (p); |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * On success, returns a file descriptor for a BPF device. |
| 475 | * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf. |
| 476 | */ |
| 477 | static int |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 478 | bpf_open(char *errbuf) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 479 | { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 480 | int fd = -1; |
| 481 | static const char cloning_device[] = "/dev/bpf"; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 482 | int n = 0; |
| 483 | char device[sizeof "/dev/bpf0000000000"]; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 484 | static int no_cloning_bpf = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 485 | |
| 486 | #ifdef _AIX |
| 487 | /* |
| 488 | * Load the bpf driver, if it isn't already loaded, |
| 489 | * and create the BPF device entries, if they don't |
| 490 | * already exist. |
| 491 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 492 | if (bpf_load(errbuf) == PCAP_ERROR) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 493 | return (PCAP_ERROR); |
| 494 | #endif |
| 495 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 496 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 497 | * First, unless we've already tried opening /dev/bpf and |
| 498 | * gotten ENOENT, try opening /dev/bpf. |
| 499 | * If it fails with ENOENT, remember that, so we don't try |
| 500 | * again, and try /dev/bpfN. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 501 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 502 | if (!no_cloning_bpf && |
| 503 | (fd = open(cloning_device, O_RDWR)) == -1 && |
| 504 | ((errno != EACCES && errno != ENOENT) || |
| 505 | (fd = open(cloning_device, O_RDONLY)) == -1)) { |
| 506 | if (errno != ENOENT) { |
| 507 | if (errno == EACCES) |
| 508 | fd = PCAP_ERROR_PERM_DENIED; |
| 509 | else |
| 510 | fd = PCAP_ERROR; |
| 511 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 512 | errno, "(cannot open device) %s", cloning_device); |
| 513 | return (fd); |
| 514 | } |
| 515 | no_cloning_bpf = 1; |
| 516 | } |
| 517 | |
| 518 | if (no_cloning_bpf) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 519 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 520 | * We don't have /dev/bpf. |
| 521 | * Go through all the /dev/bpfN minors and find one |
| 522 | * that isn't in use. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 523 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 524 | do { |
| 525 | (void)pcap_snprintf(device, sizeof(device), "/dev/bpf%d", n++); |
| 526 | /* |
| 527 | * Initially try a read/write open (to allow the inject |
| 528 | * method to work). If that fails due to permission |
| 529 | * issues, fall back to read-only. This allows a |
| 530 | * non-root user to be granted specific access to pcap |
| 531 | * capabilities via file permissions. |
| 532 | * |
| 533 | * XXX - we should have an API that has a flag that |
| 534 | * controls whether to open read-only or read-write, |
| 535 | * so that denial of permission to send (or inability |
| 536 | * to send, if sending packets isn't supported on |
| 537 | * the device in question) can be indicated at open |
| 538 | * time. |
| 539 | */ |
| 540 | fd = open(device, O_RDWR); |
| 541 | if (fd == -1 && errno == EACCES) |
| 542 | fd = open(device, O_RDONLY); |
| 543 | } while (fd < 0 && errno == EBUSY); |
| 544 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 545 | |
| 546 | /* |
| 547 | * XXX better message for all minors used |
| 548 | */ |
| 549 | if (fd < 0) { |
| 550 | switch (errno) { |
| 551 | |
| 552 | case ENOENT: |
| 553 | fd = PCAP_ERROR; |
| 554 | if (n == 1) { |
| 555 | /* |
| 556 | * /dev/bpf0 doesn't exist, which |
| 557 | * means we probably have no BPF |
| 558 | * devices. |
| 559 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 560 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 561 | "(there are no BPF devices)"); |
| 562 | } else { |
| 563 | /* |
| 564 | * We got EBUSY on at least one |
| 565 | * BPF device, so we have BPF |
| 566 | * devices, but all the ones |
| 567 | * that exist are busy. |
| 568 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 569 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 570 | "(all BPF devices are busy)"); |
| 571 | } |
| 572 | break; |
| 573 | |
| 574 | case EACCES: |
| 575 | /* |
| 576 | * Got EACCES on the last device we tried, |
| 577 | * and EBUSY on all devices before that, |
| 578 | * if any. |
| 579 | */ |
| 580 | fd = PCAP_ERROR_PERM_DENIED; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 581 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 582 | errno, "(cannot open BPF device) %s", device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 583 | break; |
| 584 | |
| 585 | default: |
| 586 | /* |
| 587 | * Some other problem. |
| 588 | */ |
| 589 | fd = PCAP_ERROR; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 590 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 591 | errno, "(cannot open BPF device) %s", device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 592 | break; |
| 593 | } |
| 594 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 595 | |
| 596 | return (fd); |
| 597 | } |
| 598 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 599 | /* |
| 600 | * Open and bind to a device; used if we're not actually going to use |
| 601 | * the device, but are just testing whether it can be opened, or opening |
| 602 | * it to get information about it. |
| 603 | * |
| 604 | * Returns an error code on failure (always negative), and an FD for |
| 605 | * the now-bound BPF device on success (always non-negative). |
| 606 | */ |
| 607 | static int |
| 608 | bpf_open_and_bind(const char *name, char *errbuf) |
| 609 | { |
| 610 | int fd; |
| 611 | struct ifreq ifr; |
| 612 | |
| 613 | /* |
| 614 | * First, open a BPF device. |
| 615 | */ |
| 616 | fd = bpf_open(errbuf); |
| 617 | if (fd < 0) |
| 618 | return (fd); /* fd is the appropriate error code */ |
| 619 | |
| 620 | /* |
| 621 | * Now bind to the device. |
| 622 | */ |
| 623 | (void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
| 624 | if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { |
| 625 | switch (errno) { |
| 626 | |
| 627 | case ENXIO: |
| 628 | /* |
| 629 | * There's no such device. |
| 630 | */ |
| 631 | close(fd); |
| 632 | return (PCAP_ERROR_NO_SUCH_DEVICE); |
| 633 | |
| 634 | case ENETDOWN: |
| 635 | /* |
| 636 | * Return a "network down" indication, so that |
| 637 | * the application can report that rather than |
| 638 | * saying we had a mysterious failure and |
| 639 | * suggest that they report a problem to the |
| 640 | * libpcap developers. |
| 641 | */ |
| 642 | close(fd); |
| 643 | return (PCAP_ERROR_IFACE_NOT_UP); |
| 644 | |
| 645 | default: |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 646 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 647 | errno, "BIOCSETIF: %s", name); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 648 | close(fd); |
| 649 | return (PCAP_ERROR); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Success. |
| 655 | */ |
| 656 | return (fd); |
| 657 | } |
| 658 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 659 | #ifdef BIOCGDLTLIST |
| 660 | static int |
| 661 | get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf) |
| 662 | { |
| 663 | memset(bdlp, 0, sizeof(*bdlp)); |
| 664 | if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) { |
| 665 | u_int i; |
| 666 | int is_ethernet; |
| 667 | |
| 668 | bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1)); |
| 669 | if (bdlp->bfl_list == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 670 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 671 | errno, "malloc"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 672 | return (PCAP_ERROR); |
| 673 | } |
| 674 | |
| 675 | if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 676 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 677 | errno, "BIOCGDLTLIST"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 678 | free(bdlp->bfl_list); |
| 679 | return (PCAP_ERROR); |
| 680 | } |
| 681 | |
| 682 | /* |
| 683 | * OK, for real Ethernet devices, add DLT_DOCSIS to the |
| 684 | * list, so that an application can let you choose it, |
| 685 | * in case you're capturing DOCSIS traffic that a Cisco |
| 686 | * Cable Modem Termination System is putting out onto |
| 687 | * an Ethernet (it doesn't put an Ethernet header onto |
| 688 | * the wire, it puts raw DOCSIS frames out on the wire |
| 689 | * inside the low-level Ethernet framing). |
| 690 | * |
| 691 | * A "real Ethernet device" is defined here as a device |
| 692 | * that has a link-layer type of DLT_EN10MB and that has |
| 693 | * no alternate link-layer types; that's done to exclude |
| 694 | * 802.11 interfaces (which might or might not be the |
| 695 | * right thing to do, but I suspect it is - Ethernet <-> |
| 696 | * 802.11 bridges would probably badly mishandle frames |
| 697 | * that don't have Ethernet headers). |
| 698 | * |
| 699 | * On Solaris with BPF, Ethernet devices also offer |
| 700 | * DLT_IPNET, so we, if DLT_IPNET is defined, we don't |
| 701 | * treat it as an indication that the device isn't an |
| 702 | * Ethernet. |
| 703 | */ |
| 704 | if (v == DLT_EN10MB) { |
| 705 | is_ethernet = 1; |
| 706 | for (i = 0; i < bdlp->bfl_len; i++) { |
| 707 | if (bdlp->bfl_list[i] != DLT_EN10MB |
| 708 | #ifdef DLT_IPNET |
| 709 | && bdlp->bfl_list[i] != DLT_IPNET |
| 710 | #endif |
| 711 | ) { |
| 712 | is_ethernet = 0; |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | if (is_ethernet) { |
| 717 | /* |
| 718 | * We reserved one more slot at the end of |
| 719 | * the list. |
| 720 | */ |
| 721 | bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS; |
| 722 | bdlp->bfl_len++; |
| 723 | } |
| 724 | } |
| 725 | } else { |
| 726 | /* |
| 727 | * EINVAL just means "we don't support this ioctl on |
| 728 | * this device"; don't treat it as an error. |
| 729 | */ |
| 730 | if (errno != EINVAL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 731 | pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, |
| 732 | errno, "BIOCGDLTLIST"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 733 | return (PCAP_ERROR); |
| 734 | } |
| 735 | } |
| 736 | return (0); |
| 737 | } |
| 738 | #endif |
| 739 | |
| 740 | static int |
| 741 | pcap_can_set_rfmon_bpf(pcap_t *p) |
| 742 | { |
| 743 | #if defined(__APPLE__) |
| 744 | struct utsname osinfo; |
| 745 | struct ifreq ifr; |
| 746 | int fd; |
| 747 | #ifdef BIOCGDLTLIST |
| 748 | struct bpf_dltlist bdl; |
| 749 | #endif |
| 750 | |
| 751 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 752 | * The joys of monitor mode on Mac OS X/OS X/macOS. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 753 | * |
| 754 | * Prior to 10.4, it's not supported at all. |
| 755 | * |
| 756 | * In 10.4, if adapter enN supports monitor mode, there's a |
| 757 | * wltN adapter corresponding to it; you open it, instead of |
| 758 | * enN, to get monitor mode. You get whatever link-layer |
| 759 | * headers it supplies. |
| 760 | * |
| 761 | * In 10.5, and, we assume, later releases, if adapter enN |
| 762 | * supports monitor mode, it offers, among its selectable |
| 763 | * DLT_ values, values that let you get the 802.11 header; |
| 764 | * selecting one of those values puts the adapter into monitor |
| 765 | * mode (i.e., you can't get 802.11 headers except in monitor |
| 766 | * mode, and you can't get Ethernet headers in monitor mode). |
| 767 | */ |
| 768 | if (uname(&osinfo) == -1) { |
| 769 | /* |
| 770 | * Can't get the OS version; just say "no". |
| 771 | */ |
| 772 | return (0); |
| 773 | } |
| 774 | /* |
| 775 | * We assume osinfo.sysname is "Darwin", because |
| 776 | * __APPLE__ is defined. We just check the version. |
| 777 | */ |
| 778 | if (osinfo.release[0] < '8' && osinfo.release[1] == '.') { |
| 779 | /* |
| 780 | * 10.3 (Darwin 7.x) or earlier. |
| 781 | * Monitor mode not supported. |
| 782 | */ |
| 783 | return (0); |
| 784 | } |
| 785 | if (osinfo.release[0] == '8' && osinfo.release[1] == '.') { |
| 786 | /* |
| 787 | * 10.4 (Darwin 8.x). s/en/wlt/, and check |
| 788 | * whether the device exists. |
| 789 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 790 | if (strncmp(p->opt.device, "en", 2) != 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 791 | /* |
| 792 | * Not an enN device; no monitor mode. |
| 793 | */ |
| 794 | return (0); |
| 795 | } |
| 796 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 797 | if (fd == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 798 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 799 | errno, "socket"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 800 | return (PCAP_ERROR); |
| 801 | } |
| 802 | strlcpy(ifr.ifr_name, "wlt", sizeof(ifr.ifr_name)); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 803 | strlcat(ifr.ifr_name, p->opt.device + 2, sizeof(ifr.ifr_name)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 804 | if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { |
| 805 | /* |
| 806 | * No such device? |
| 807 | */ |
| 808 | close(fd); |
| 809 | return (0); |
| 810 | } |
| 811 | close(fd); |
| 812 | return (1); |
| 813 | } |
| 814 | |
| 815 | #ifdef BIOCGDLTLIST |
| 816 | /* |
| 817 | * Everything else is 10.5 or later; for those, |
| 818 | * we just open the enN device, and check whether |
| 819 | * we have any 802.11 devices. |
| 820 | * |
| 821 | * First, open a BPF device. |
| 822 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 823 | fd = bpf_open(p->errbuf); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 824 | if (fd < 0) |
| 825 | return (fd); /* fd is the appropriate error code */ |
| 826 | |
| 827 | /* |
| 828 | * Now bind to the device. |
| 829 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 830 | (void)strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 831 | if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { |
| 832 | switch (errno) { |
| 833 | |
| 834 | case ENXIO: |
| 835 | /* |
| 836 | * There's no such device. |
| 837 | */ |
| 838 | close(fd); |
| 839 | return (PCAP_ERROR_NO_SUCH_DEVICE); |
| 840 | |
| 841 | case ENETDOWN: |
| 842 | /* |
| 843 | * Return a "network down" indication, so that |
| 844 | * the application can report that rather than |
| 845 | * saying we had a mysterious failure and |
| 846 | * suggest that they report a problem to the |
| 847 | * libpcap developers. |
| 848 | */ |
| 849 | close(fd); |
| 850 | return (PCAP_ERROR_IFACE_NOT_UP); |
| 851 | |
| 852 | default: |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 853 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 854 | errno, "BIOCSETIF: %s", p->opt.device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 855 | close(fd); |
| 856 | return (PCAP_ERROR); |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * We know the default link type -- now determine all the DLTs |
| 862 | * this interface supports. If this fails with EINVAL, it's |
| 863 | * not fatal; we just don't get to use the feature later. |
| 864 | * (We don't care about DLT_DOCSIS, so we pass DLT_NULL |
| 865 | * as the default DLT for this adapter.) |
| 866 | */ |
| 867 | if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) { |
| 868 | close(fd); |
| 869 | return (PCAP_ERROR); |
| 870 | } |
| 871 | if (find_802_11(&bdl) != -1) { |
| 872 | /* |
| 873 | * We have an 802.11 DLT, so we can set monitor mode. |
| 874 | */ |
| 875 | free(bdl.bfl_list); |
| 876 | close(fd); |
| 877 | return (1); |
| 878 | } |
| 879 | free(bdl.bfl_list); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 880 | close(fd); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 881 | #endif /* BIOCGDLTLIST */ |
| 882 | return (0); |
| 883 | #elif defined(HAVE_BSD_IEEE80211) |
| 884 | int ret; |
| 885 | |
| 886 | ret = monitor_mode(p, 0); |
| 887 | if (ret == PCAP_ERROR_RFMON_NOTSUP) |
| 888 | return (0); /* not an error, just a "can't do" */ |
| 889 | if (ret == 0) |
| 890 | return (1); /* success */ |
| 891 | return (ret); |
| 892 | #else |
| 893 | return (0); |
| 894 | #endif |
| 895 | } |
| 896 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 897 | static int |
| 898 | pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps) |
| 899 | { |
| 900 | struct bpf_stat s; |
| 901 | |
| 902 | /* |
| 903 | * "ps_recv" counts packets handed to the filter, not packets |
| 904 | * that passed the filter. This includes packets later dropped |
| 905 | * because we ran out of buffer space. |
| 906 | * |
| 907 | * "ps_drop" counts packets dropped inside the BPF device |
| 908 | * because we ran out of buffer space. It doesn't count |
| 909 | * packets dropped by the interface driver. It counts |
| 910 | * only packets that passed the filter. |
| 911 | * |
| 912 | * Both statistics include packets not yet read from the kernel |
| 913 | * by libpcap, and thus not yet seen by the application. |
| 914 | */ |
| 915 | if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 916 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 917 | errno, "BIOCGSTATS"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 918 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | ps->ps_recv = s.bs_recv; |
| 922 | ps->ps_drop = s.bs_drop; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 923 | ps->ps_ifdrop = 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 924 | return (0); |
| 925 | } |
| 926 | |
| 927 | static int |
| 928 | pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) |
| 929 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 930 | struct pcap_bpf *pb = p->priv; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 931 | int cc; |
| 932 | int n = 0; |
| 933 | register u_char *bp, *ep; |
| 934 | u_char *datap; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 935 | #ifdef PCAP_FDDIPAD |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 936 | register u_int pad; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 937 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 938 | #ifdef HAVE_ZEROCOPY_BPF |
| 939 | int i; |
| 940 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 941 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 942 | again: |
| 943 | /* |
| 944 | * Has "pcap_breakloop()" been called? |
| 945 | */ |
| 946 | if (p->break_loop) { |
| 947 | /* |
| 948 | * Yes - clear the flag that indicates that it |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 949 | * has, and return PCAP_ERROR_BREAK to indicate |
| 950 | * that we were told to break out of the loop. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 951 | */ |
| 952 | p->break_loop = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 953 | return (PCAP_ERROR_BREAK); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 954 | } |
| 955 | cc = p->cc; |
| 956 | if (p->cc == 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 957 | /* |
| 958 | * When reading without zero-copy from a file descriptor, we |
| 959 | * use a single buffer and return a length of data in the |
| 960 | * buffer. With zero-copy, we update the p->buffer pointer |
| 961 | * to point at whatever underlying buffer contains the next |
| 962 | * data and update cc to reflect the data found in the |
| 963 | * buffer. |
| 964 | */ |
| 965 | #ifdef HAVE_ZEROCOPY_BPF |
| 966 | if (pb->zerocopy) { |
| 967 | if (p->buffer != NULL) |
| 968 | pcap_ack_zbuf(p); |
| 969 | i = pcap_next_zbuf(p, &cc); |
| 970 | if (i == 0) |
| 971 | goto again; |
| 972 | if (i < 0) |
| 973 | return (PCAP_ERROR); |
| 974 | } else |
| 975 | #endif |
| 976 | { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 977 | cc = read(p->fd, p->buffer, p->bufsize); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 978 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 979 | if (cc < 0) { |
| 980 | /* Don't choke when we get ptraced */ |
| 981 | switch (errno) { |
| 982 | |
| 983 | case EINTR: |
| 984 | goto again; |
| 985 | |
| 986 | #ifdef _AIX |
| 987 | case EFAULT: |
| 988 | /* |
| 989 | * Sigh. More AIX wonderfulness. |
| 990 | * |
| 991 | * For some unknown reason the uiomove() |
| 992 | * operation in the bpf kernel extension |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 993 | * used to copy the buffer into user |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 994 | * space sometimes returns EFAULT. I have |
| 995 | * no idea why this is the case given that |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 996 | * a kernel debugger shows the user buffer |
| 997 | * is correct. This problem appears to |
| 998 | * be mostly mitigated by the memset of |
| 999 | * the buffer before it is first used. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1000 | * Very strange.... Shaun Clowes |
| 1001 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1002 | * In any case this means that we shouldn't |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1003 | * treat EFAULT as a fatal error; as we |
| 1004 | * don't have an API for returning |
| 1005 | * a "some packets were dropped since |
| 1006 | * the last packet you saw" indication, |
| 1007 | * we just ignore EFAULT and keep reading. |
| 1008 | */ |
| 1009 | goto again; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1010 | #endif |
| 1011 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1012 | case EWOULDBLOCK: |
| 1013 | return (0); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1014 | |
| 1015 | case ENXIO: |
| 1016 | /* |
| 1017 | * The device on which we're capturing |
| 1018 | * went away. |
| 1019 | * |
| 1020 | * XXX - we should really return |
| 1021 | * PCAP_ERROR_IFACE_NOT_UP, but |
| 1022 | * pcap_dispatch() etc. aren't |
| 1023 | * defined to retur that. |
| 1024 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1025 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1026 | "The interface went down"); |
| 1027 | return (PCAP_ERROR); |
| 1028 | |
| 1029 | #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1030 | /* |
| 1031 | * Due to a SunOS bug, after 2^31 bytes, the kernel |
| 1032 | * file offset overflows and read fails with EINVAL. |
| 1033 | * The lseek() to 0 will fix things. |
| 1034 | */ |
| 1035 | case EINVAL: |
| 1036 | if (lseek(p->fd, 0L, SEEK_CUR) + |
| 1037 | p->bufsize < 0) { |
| 1038 | (void)lseek(p->fd, 0L, SEEK_SET); |
| 1039 | goto again; |
| 1040 | } |
| 1041 | /* fall through */ |
| 1042 | #endif |
| 1043 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1044 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1045 | errno, "read"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1046 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1047 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1048 | bp = (u_char *)p->buffer; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1049 | } else |
| 1050 | bp = p->bp; |
| 1051 | |
| 1052 | /* |
| 1053 | * Loop through each packet. |
| 1054 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1055 | #ifdef BIOCSTSTAMP |
| 1056 | #define bhp ((struct bpf_xhdr *)bp) |
| 1057 | #else |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1058 | #define bhp ((struct bpf_hdr *)bp) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1059 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1060 | ep = bp + cc; |
| 1061 | #ifdef PCAP_FDDIPAD |
| 1062 | pad = p->fddipad; |
| 1063 | #endif |
| 1064 | while (bp < ep) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1065 | register u_int caplen, hdrlen; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1066 | |
| 1067 | /* |
| 1068 | * Has "pcap_breakloop()" been called? |
| 1069 | * If so, return immediately - if we haven't read any |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1070 | * packets, clear the flag and return PCAP_ERROR_BREAK |
| 1071 | * to indicate that we were told to break out of the loop, |
| 1072 | * otherwise leave the flag set, so that the *next* call |
| 1073 | * will break out of the loop without having read any |
| 1074 | * packets, and return the number of packets we've |
| 1075 | * processed so far. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1076 | */ |
| 1077 | if (p->break_loop) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1078 | p->bp = bp; |
| 1079 | p->cc = ep - bp; |
| 1080 | /* |
| 1081 | * ep is set based on the return value of read(), |
| 1082 | * but read() from a BPF device doesn't necessarily |
| 1083 | * return a value that's a multiple of the alignment |
| 1084 | * value for BPF_WORDALIGN(). However, whenever we |
| 1085 | * increment bp, we round up the increment value by |
| 1086 | * a value rounded up by BPF_WORDALIGN(), so we |
| 1087 | * could increment bp past ep after processing the |
| 1088 | * last packet in the buffer. |
| 1089 | * |
| 1090 | * We treat ep < bp as an indication that this |
| 1091 | * happened, and just set p->cc to 0. |
| 1092 | */ |
| 1093 | if (p->cc < 0) |
| 1094 | p->cc = 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1095 | if (n == 0) { |
| 1096 | p->break_loop = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1097 | return (PCAP_ERROR_BREAK); |
| 1098 | } else |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1099 | return (n); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | caplen = bhp->bh_caplen; |
| 1103 | hdrlen = bhp->bh_hdrlen; |
| 1104 | datap = bp + hdrlen; |
| 1105 | /* |
| 1106 | * Short-circuit evaluation: if using BPF filter |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1107 | * in kernel, no need to do it now - we already know |
| 1108 | * the packet passed the filter. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1109 | * |
| 1110 | #ifdef PCAP_FDDIPAD |
| 1111 | * Note: the filter code was generated assuming |
| 1112 | * that p->fddipad was the amount of padding |
| 1113 | * before the header, as that's what's required |
| 1114 | * in the kernel, so we run the filter before |
| 1115 | * skipping that padding. |
| 1116 | #endif |
| 1117 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1118 | if (pb->filtering_in_kernel || |
| 1119 | bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1120 | struct pcap_pkthdr pkthdr; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1121 | #ifdef BIOCSTSTAMP |
| 1122 | struct bintime bt; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1123 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1124 | bt.sec = bhp->bh_tstamp.bt_sec; |
| 1125 | bt.frac = bhp->bh_tstamp.bt_frac; |
| 1126 | if (p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) { |
| 1127 | struct timespec ts; |
| 1128 | |
| 1129 | bintime2timespec(&bt, &ts); |
| 1130 | pkthdr.ts.tv_sec = ts.tv_sec; |
| 1131 | pkthdr.ts.tv_usec = ts.tv_nsec; |
| 1132 | } else { |
| 1133 | struct timeval tv; |
| 1134 | |
| 1135 | bintime2timeval(&bt, &tv); |
| 1136 | pkthdr.ts.tv_sec = tv.tv_sec; |
| 1137 | pkthdr.ts.tv_usec = tv.tv_usec; |
| 1138 | } |
| 1139 | #else |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1140 | pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec; |
| 1141 | #ifdef _AIX |
| 1142 | /* |
| 1143 | * AIX's BPF returns seconds/nanoseconds time |
| 1144 | * stamps, not seconds/microseconds time stamps. |
| 1145 | */ |
| 1146 | pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000; |
| 1147 | #else |
| 1148 | pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec; |
| 1149 | #endif |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1150 | #endif /* BIOCSTSTAMP */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1151 | #ifdef PCAP_FDDIPAD |
| 1152 | if (caplen > pad) |
| 1153 | pkthdr.caplen = caplen - pad; |
| 1154 | else |
| 1155 | pkthdr.caplen = 0; |
| 1156 | if (bhp->bh_datalen > pad) |
| 1157 | pkthdr.len = bhp->bh_datalen - pad; |
| 1158 | else |
| 1159 | pkthdr.len = 0; |
| 1160 | datap += pad; |
| 1161 | #else |
| 1162 | pkthdr.caplen = caplen; |
| 1163 | pkthdr.len = bhp->bh_datalen; |
| 1164 | #endif |
| 1165 | (*callback)(user, &pkthdr, datap); |
| 1166 | bp += BPF_WORDALIGN(caplen + hdrlen); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1167 | if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1168 | p->bp = bp; |
| 1169 | p->cc = ep - bp; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1170 | /* |
| 1171 | * See comment above about p->cc < 0. |
| 1172 | */ |
| 1173 | if (p->cc < 0) |
| 1174 | p->cc = 0; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1175 | return (n); |
| 1176 | } |
| 1177 | } else { |
| 1178 | /* |
| 1179 | * Skip this packet. |
| 1180 | */ |
| 1181 | bp += BPF_WORDALIGN(caplen + hdrlen); |
| 1182 | } |
| 1183 | } |
| 1184 | #undef bhp |
| 1185 | p->cc = 0; |
| 1186 | return (n); |
| 1187 | } |
| 1188 | |
| 1189 | static int |
| 1190 | pcap_inject_bpf(pcap_t *p, const void *buf, size_t size) |
| 1191 | { |
| 1192 | int ret; |
| 1193 | |
| 1194 | ret = write(p->fd, buf, size); |
| 1195 | #ifdef __APPLE__ |
| 1196 | if (ret == -1 && errno == EAFNOSUPPORT) { |
| 1197 | /* |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1198 | * In some versions of macOS, there's a bug wherein setting |
| 1199 | * the BIOCSHDRCMPLT flag causes writes to fail; see, for |
| 1200 | * example: |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1201 | * |
| 1202 | * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch |
| 1203 | * |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1204 | * So, if, on macOS, we get EAFNOSUPPORT from the write, we |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1205 | * assume it's due to that bug, and turn off that flag |
| 1206 | * and try again. If we succeed, it either means that |
| 1207 | * somebody applied the fix from that URL, or other patches |
| 1208 | * for that bug from |
| 1209 | * |
| 1210 | * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/ |
| 1211 | * |
| 1212 | * and are running a Darwin kernel with those fixes, or |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1213 | * that Apple fixed the problem in some macOS release. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1214 | */ |
| 1215 | u_int spoof_eth_src = 0; |
| 1216 | |
| 1217 | if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1218 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1219 | errno, "send: can't turn off BIOCSHDRCMPLT"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1220 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * Now try the write again. |
| 1225 | */ |
| 1226 | ret = write(p->fd, buf, size); |
| 1227 | } |
| 1228 | #endif /* __APPLE__ */ |
| 1229 | if (ret == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1230 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1231 | errno, "send"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1232 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1233 | } |
| 1234 | return (ret); |
| 1235 | } |
| 1236 | |
| 1237 | #ifdef _AIX |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1238 | static int |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1239 | bpf_odminit(char *errbuf) |
| 1240 | { |
| 1241 | char *errstr; |
| 1242 | |
| 1243 | if (odm_initialize() == -1) { |
| 1244 | if (odm_err_msg(odmerrno, &errstr) == -1) |
| 1245 | errstr = "Unknown error"; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1246 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1247 | "bpf_load: odm_initialize failed: %s", |
| 1248 | errstr); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1249 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) { |
| 1253 | if (odm_err_msg(odmerrno, &errstr) == -1) |
| 1254 | errstr = "Unknown error"; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1255 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1256 | "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s", |
| 1257 | errstr); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1258 | (void)odm_terminate(); |
| 1259 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | return (0); |
| 1263 | } |
| 1264 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1265 | static int |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1266 | bpf_odmcleanup(char *errbuf) |
| 1267 | { |
| 1268 | char *errstr; |
| 1269 | |
| 1270 | if (odm_unlock(odmlockid) == -1) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1271 | if (errbuf != NULL) { |
| 1272 | if (odm_err_msg(odmerrno, &errstr) == -1) |
| 1273 | errstr = "Unknown error"; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1274 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1275 | "bpf_load: odm_unlock failed: %s", |
| 1276 | errstr); |
| 1277 | } |
| 1278 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | if (odm_terminate() == -1) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1282 | if (errbuf != NULL) { |
| 1283 | if (odm_err_msg(odmerrno, &errstr) == -1) |
| 1284 | errstr = "Unknown error"; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1285 | pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1286 | "bpf_load: odm_terminate failed: %s", |
| 1287 | errstr); |
| 1288 | } |
| 1289 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | return (0); |
| 1293 | } |
| 1294 | |
| 1295 | static int |
| 1296 | bpf_load(char *errbuf) |
| 1297 | { |
| 1298 | long major; |
| 1299 | int *minors; |
| 1300 | int numminors, i, rc; |
| 1301 | char buf[1024]; |
| 1302 | struct stat sbuf; |
| 1303 | struct bpf_config cfg_bpf; |
| 1304 | struct cfg_load cfg_ld; |
| 1305 | struct cfg_kmod cfg_km; |
| 1306 | |
| 1307 | /* |
| 1308 | * This is very very close to what happens in the real implementation |
| 1309 | * but I've fixed some (unlikely) bug situations. |
| 1310 | */ |
| 1311 | if (bpfloadedflag) |
| 1312 | return (0); |
| 1313 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1314 | if (bpf_odminit(errbuf) == PCAP_ERROR) |
| 1315 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1316 | |
| 1317 | major = genmajor(BPF_NAME); |
| 1318 | if (major == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1319 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 1320 | errno, "bpf_load: genmajor failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1321 | (void)bpf_odmcleanup(NULL); |
| 1322 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | minors = getminor(major, &numminors, BPF_NAME); |
| 1326 | if (!minors) { |
| 1327 | minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1); |
| 1328 | if (!minors) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1329 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 1330 | errno, "bpf_load: genminor failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1331 | (void)bpf_odmcleanup(NULL); |
| 1332 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1333 | } |
| 1334 | } |
| 1335 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1336 | if (bpf_odmcleanup(errbuf) == PCAP_ERROR) |
| 1337 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1338 | |
| 1339 | rc = stat(BPF_NODE "0", &sbuf); |
| 1340 | if (rc == -1 && errno != ENOENT) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1341 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 1342 | errno, "bpf_load: can't stat %s", BPF_NODE "0"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1343 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | if (rc == -1 || getmajor(sbuf.st_rdev) != major) { |
| 1347 | for (i = 0; i < BPF_MINORS; i++) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1348 | pcap_snprintf(buf, sizeof(buf), "%s%d", BPF_NODE, i); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1349 | unlink(buf); |
| 1350 | if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1351 | pcap_fmt_errmsg_for_errno(errbuf, |
| 1352 | PCAP_ERRBUF_SIZE, errno, |
| 1353 | "bpf_load: can't mknod %s", buf); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1354 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /* Check if the driver is loaded */ |
| 1360 | memset(&cfg_ld, 0x0, sizeof(cfg_ld)); |
| 1361 | cfg_ld.path = buf; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1362 | pcap_snprintf(cfg_ld.path, sizeof(cfg_ld.path), "%s/%s", DRIVER_PATH, BPF_NAME); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1363 | if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) || |
| 1364 | (cfg_ld.kmid == 0)) { |
| 1365 | /* Driver isn't loaded, load it now */ |
| 1366 | if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1367 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 1368 | errno, "bpf_load: could not load driver"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1369 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | /* Configure the driver */ |
| 1374 | cfg_km.cmd = CFG_INIT; |
| 1375 | cfg_km.kmid = cfg_ld.kmid; |
| 1376 | cfg_km.mdilen = sizeof(cfg_bpf); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1377 | cfg_km.mdiptr = (void *)&cfg_bpf; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1378 | for (i = 0; i < BPF_MINORS; i++) { |
| 1379 | cfg_bpf.devno = domakedev(major, i); |
| 1380 | if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1381 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 1382 | errno, "bpf_load: could not configure driver"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1383 | return (PCAP_ERROR); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1384 | } |
| 1385 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1386 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1387 | bpfloadedflag = 1; |
| 1388 | |
| 1389 | return (0); |
| 1390 | } |
| 1391 | #endif |
| 1392 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1393 | /* |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1394 | * Undo any operations done when opening the device when necessary. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1395 | */ |
| 1396 | static void |
| 1397 | pcap_cleanup_bpf(pcap_t *p) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1398 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1399 | struct pcap_bpf *pb = p->priv; |
| 1400 | #ifdef HAVE_BSD_IEEE80211 |
| 1401 | int sock; |
| 1402 | struct ifmediareq req; |
| 1403 | struct ifreq ifr; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1404 | #endif |
| 1405 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1406 | if (pb->must_do_on_close != 0) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1407 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1408 | * There's something we have to do when closing this |
| 1409 | * pcap_t. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1410 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1411 | #ifdef HAVE_BSD_IEEE80211 |
| 1412 | if (pb->must_do_on_close & MUST_CLEAR_RFMON) { |
| 1413 | /* |
| 1414 | * We put the interface into rfmon mode; |
| 1415 | * take it out of rfmon mode. |
| 1416 | * |
| 1417 | * XXX - if somebody else wants it in rfmon |
| 1418 | * mode, this code cannot know that, so it'll take |
| 1419 | * it out of rfmon mode. |
| 1420 | */ |
| 1421 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 1422 | if (sock == -1) { |
| 1423 | fprintf(stderr, |
| 1424 | "Can't restore interface flags (socket() failed: %s).\n" |
| 1425 | "Please adjust manually.\n", |
| 1426 | strerror(errno)); |
| 1427 | } else { |
| 1428 | memset(&req, 0, sizeof(req)); |
| 1429 | strncpy(req.ifm_name, pb->device, |
| 1430 | sizeof(req.ifm_name)); |
| 1431 | if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { |
| 1432 | fprintf(stderr, |
| 1433 | "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n" |
| 1434 | "Please adjust manually.\n", |
| 1435 | strerror(errno)); |
| 1436 | } else { |
| 1437 | if (req.ifm_current & IFM_IEEE80211_MONITOR) { |
| 1438 | /* |
| 1439 | * Rfmon mode is currently on; |
| 1440 | * turn it off. |
| 1441 | */ |
| 1442 | memset(&ifr, 0, sizeof(ifr)); |
| 1443 | (void)strncpy(ifr.ifr_name, |
| 1444 | pb->device, |
| 1445 | sizeof(ifr.ifr_name)); |
| 1446 | ifr.ifr_media = |
| 1447 | req.ifm_current & ~IFM_IEEE80211_MONITOR; |
| 1448 | if (ioctl(sock, SIOCSIFMEDIA, |
| 1449 | &ifr) == -1) { |
| 1450 | fprintf(stderr, |
| 1451 | "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n" |
| 1452 | "Please adjust manually.\n", |
| 1453 | strerror(errno)); |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | close(sock); |
| 1458 | } |
| 1459 | } |
| 1460 | #endif /* HAVE_BSD_IEEE80211 */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1461 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1462 | #if defined(__FreeBSD__) && defined(SIOCIFCREATE2) |
| 1463 | /* |
| 1464 | * Attempt to destroy the usbusN interface that we created. |
| 1465 | */ |
| 1466 | if (pb->must_do_on_close & MUST_DESTROY_USBUS) { |
| 1467 | if (if_nametoindex(pb->device) > 0) { |
| 1468 | int s; |
| 1469 | |
| 1470 | s = socket(AF_LOCAL, SOCK_DGRAM, 0); |
| 1471 | if (s >= 0) { |
| 1472 | strlcpy(ifr.ifr_name, pb->device, |
| 1473 | sizeof(ifr.ifr_name)); |
| 1474 | ioctl(s, SIOCIFDESTROY, &ifr); |
| 1475 | close(s); |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1480 | /* |
| 1481 | * Take this pcap out of the list of pcaps for which we |
| 1482 | * have to take the interface out of some mode. |
| 1483 | */ |
| 1484 | pcap_remove_from_pcaps_to_close(p); |
| 1485 | pb->must_do_on_close = 0; |
| 1486 | } |
| 1487 | |
| 1488 | #ifdef HAVE_ZEROCOPY_BPF |
| 1489 | if (pb->zerocopy) { |
| 1490 | /* |
| 1491 | * Delete the mappings. Note that p->buffer gets |
| 1492 | * initialized to one of the mmapped regions in |
| 1493 | * this case, so do not try and free it directly; |
| 1494 | * null it out so that pcap_cleanup_live_common() |
| 1495 | * doesn't try to free it. |
| 1496 | */ |
| 1497 | if (pb->zbuf1 != MAP_FAILED && pb->zbuf1 != NULL) |
| 1498 | (void) munmap(pb->zbuf1, pb->zbufsize); |
| 1499 | if (pb->zbuf2 != MAP_FAILED && pb->zbuf2 != NULL) |
| 1500 | (void) munmap(pb->zbuf2, pb->zbufsize); |
| 1501 | p->buffer = NULL; |
| 1502 | } |
| 1503 | #endif |
| 1504 | if (pb->device != NULL) { |
| 1505 | free(pb->device); |
| 1506 | pb->device = NULL; |
| 1507 | } |
| 1508 | pcap_cleanup_live_common(p); |
| 1509 | } |
| 1510 | |
| 1511 | static int |
| 1512 | check_setif_failure(pcap_t *p, int error) |
| 1513 | { |
| 1514 | #ifdef __APPLE__ |
| 1515 | int fd; |
| 1516 | struct ifreq ifr; |
| 1517 | int err; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1518 | #endif |
| 1519 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1520 | if (error == ENXIO) { |
| 1521 | /* |
| 1522 | * No such device exists. |
| 1523 | */ |
| 1524 | #ifdef __APPLE__ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1525 | if (p->opt.rfmon && strncmp(p->opt.device, "wlt", 3) == 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1526 | /* |
| 1527 | * Monitor mode was requested, and we're trying |
| 1528 | * to open a "wltN" device. Assume that this |
| 1529 | * is 10.4 and that we were asked to open an |
| 1530 | * "enN" device; if that device exists, return |
| 1531 | * "monitor mode not supported on the device". |
| 1532 | */ |
| 1533 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 1534 | if (fd != -1) { |
| 1535 | strlcpy(ifr.ifr_name, "en", |
| 1536 | sizeof(ifr.ifr_name)); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1537 | strlcat(ifr.ifr_name, p->opt.device + 3, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1538 | sizeof(ifr.ifr_name)); |
| 1539 | if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { |
| 1540 | /* |
| 1541 | * We assume this failed because |
| 1542 | * the underlying device doesn't |
| 1543 | * exist. |
| 1544 | */ |
| 1545 | err = PCAP_ERROR_NO_SUCH_DEVICE; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1546 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 1547 | PCAP_ERRBUF_SIZE, errno, |
| 1548 | "SIOCGIFFLAGS on %s failed", |
| 1549 | ifr.ifr_name); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1550 | } else { |
| 1551 | /* |
| 1552 | * The underlying "enN" device |
| 1553 | * exists, but there's no |
| 1554 | * corresponding "wltN" device; |
| 1555 | * that means that the "enN" |
| 1556 | * device doesn't support |
| 1557 | * monitor mode, probably because |
| 1558 | * it's an Ethernet device rather |
| 1559 | * than a wireless device. |
| 1560 | */ |
| 1561 | err = PCAP_ERROR_RFMON_NOTSUP; |
| 1562 | } |
| 1563 | close(fd); |
| 1564 | } else { |
| 1565 | /* |
| 1566 | * We can't find out whether there's |
| 1567 | * an underlying "enN" device, so |
| 1568 | * just report "no such device". |
| 1569 | */ |
| 1570 | err = PCAP_ERROR_NO_SUCH_DEVICE; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1571 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 1572 | errno, PCAP_ERRBUF_SIZE, |
| 1573 | "socket() failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1574 | } |
| 1575 | return (err); |
| 1576 | } |
| 1577 | #endif |
| 1578 | /* |
| 1579 | * No such device. |
| 1580 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1581 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1582 | errno, "BIOCSETIF failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1583 | return (PCAP_ERROR_NO_SUCH_DEVICE); |
| 1584 | } else if (errno == ENETDOWN) { |
| 1585 | /* |
| 1586 | * Return a "network down" indication, so that |
| 1587 | * the application can report that rather than |
| 1588 | * saying we had a mysterious failure and |
| 1589 | * suggest that they report a problem to the |
| 1590 | * libpcap developers. |
| 1591 | */ |
| 1592 | return (PCAP_ERROR_IFACE_NOT_UP); |
| 1593 | } else { |
| 1594 | /* |
| 1595 | * Some other error; fill in the error string, and |
| 1596 | * return PCAP_ERROR. |
| 1597 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1598 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1599 | errno, "BIOCSETIF: %s", p->opt.device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1600 | return (PCAP_ERROR); |
| 1601 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1605 | * Default capture buffer size. |
| 1606 | * 32K isn't very much for modern machines with fast networks; we |
| 1607 | * pick .5M, as that's the maximum on at least some systems with BPF. |
| 1608 | * |
| 1609 | * However, on AIX 3.5, the larger buffer sized caused unrecoverable |
| 1610 | * read failures under stress, so we leave it as 32K; yet another |
| 1611 | * place where AIX's BPF is broken. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1612 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1613 | #ifdef _AIX |
| 1614 | #define DEFAULT_BUFSIZE 32768 |
| 1615 | #else |
| 1616 | #define DEFAULT_BUFSIZE 524288 |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1617 | #endif |
| 1618 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1619 | static int |
| 1620 | pcap_activate_bpf(pcap_t *p) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1621 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1622 | struct pcap_bpf *pb = p->priv; |
| 1623 | int status = 0; |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1624 | #ifdef HAVE_BSD_IEEE80211 |
| 1625 | int retv; |
| 1626 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1627 | int fd; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1628 | #ifdef LIFNAMSIZ |
| 1629 | char *zonesep; |
| 1630 | struct lifreq ifr; |
| 1631 | char *ifrname = ifr.lifr_name; |
| 1632 | const size_t ifnamsiz = sizeof(ifr.lifr_name); |
| 1633 | #else |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1634 | struct ifreq ifr; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1635 | char *ifrname = ifr.ifr_name; |
| 1636 | const size_t ifnamsiz = sizeof(ifr.ifr_name); |
| 1637 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1638 | struct bpf_version bv; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1639 | #ifdef __APPLE__ |
| 1640 | int sockfd; |
| 1641 | char *wltdev = NULL; |
| 1642 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1643 | #ifdef BIOCGDLTLIST |
| 1644 | struct bpf_dltlist bdl; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1645 | #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) |
| 1646 | int new_dlt; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1647 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1648 | #endif /* BIOCGDLTLIST */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1649 | #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT) |
| 1650 | u_int spoof_eth_src = 1; |
| 1651 | #endif |
| 1652 | u_int v; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1653 | struct bpf_insn total_insn; |
| 1654 | struct bpf_program total_prog; |
| 1655 | struct utsname osinfo; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1656 | int have_osinfo = 0; |
| 1657 | #ifdef HAVE_ZEROCOPY_BPF |
| 1658 | struct bpf_zbuf bz; |
| 1659 | u_int bufmode, zbufmax; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1660 | #endif |
| 1661 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1662 | fd = bpf_open(p->errbuf); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1663 | if (fd < 0) { |
| 1664 | status = fd; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1665 | goto bad; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1666 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1667 | |
| 1668 | p->fd = fd; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1669 | |
| 1670 | if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1671 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1672 | errno, "BIOCVERSION"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1673 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1674 | goto bad; |
| 1675 | } |
| 1676 | if (bv.bv_major != BPF_MAJOR_VERSION || |
| 1677 | bv.bv_minor < BPF_MINOR_VERSION) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1678 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1679 | "kernel bpf filter out of date"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1680 | status = PCAP_ERROR; |
| 1681 | goto bad; |
| 1682 | } |
| 1683 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1684 | /* |
| 1685 | * Turn a negative snapshot value (invalid), a snapshot value of |
| 1686 | * 0 (unspecified), or a value bigger than the normal maximum |
| 1687 | * value, into the maximum allowed value. |
| 1688 | * |
| 1689 | * If some application really *needs* a bigger snapshot |
| 1690 | * length, we should just increase MAXIMUM_SNAPLEN. |
| 1691 | */ |
| 1692 | if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN) |
| 1693 | p->snapshot = MAXIMUM_SNAPLEN; |
| 1694 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1695 | #if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid) |
| 1696 | /* |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1697 | * Retrieve the zoneid of the zone we are currently executing in. |
| 1698 | */ |
| 1699 | if ((ifr.lifr_zoneid = getzoneid()) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1700 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1701 | errno, "getzoneid()"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1702 | status = PCAP_ERROR; |
| 1703 | goto bad; |
| 1704 | } |
| 1705 | /* |
| 1706 | * Check if the given source datalink name has a '/' separated |
| 1707 | * zonename prefix string. The zonename prefixed source datalink can |
| 1708 | * be used by pcap consumers in the Solaris global zone to capture |
| 1709 | * traffic on datalinks in non-global zones. Non-global zones |
| 1710 | * do not have access to datalinks outside of their own namespace. |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1711 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1712 | if ((zonesep = strchr(p->opt.device, '/')) != NULL) { |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1713 | char path_zname[ZONENAME_MAX]; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1714 | int znamelen; |
| 1715 | char *lnamep; |
| 1716 | |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1717 | if (ifr.lifr_zoneid != GLOBAL_ZONEID) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1718 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1719 | "zonename/linkname only valid in global zone."); |
| 1720 | status = PCAP_ERROR; |
| 1721 | goto bad; |
| 1722 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1723 | znamelen = zonesep - p->opt.device; |
| 1724 | (void) strlcpy(path_zname, p->opt.device, znamelen + 1); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1725 | ifr.lifr_zoneid = getzoneidbyname(path_zname); |
| 1726 | if (ifr.lifr_zoneid == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1727 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1728 | errno, "getzoneidbyname(%s)", path_zname); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1729 | status = PCAP_ERROR; |
| 1730 | goto bad; |
| 1731 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1732 | lnamep = strdup(zonesep + 1); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1733 | if (lnamep == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1734 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1735 | errno, "strdup"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1736 | status = PCAP_ERROR; |
| 1737 | goto bad; |
| 1738 | } |
| 1739 | free(p->opt.device); |
| 1740 | p->opt.device = lnamep; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1741 | } |
| 1742 | #endif |
| 1743 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1744 | pb->device = strdup(p->opt.device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1745 | if (pb->device == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1746 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1747 | errno, "strdup"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1748 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1749 | goto bad; |
| 1750 | } |
| 1751 | |
| 1752 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1753 | * Attempt to find out the version of the OS on which we're running. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1754 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1755 | if (uname(&osinfo) == 0) |
| 1756 | have_osinfo = 1; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1757 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1758 | #ifdef __APPLE__ |
| 1759 | /* |
| 1760 | * See comment in pcap_can_set_rfmon_bpf() for an explanation |
| 1761 | * of why we check the version number. |
| 1762 | */ |
| 1763 | if (p->opt.rfmon) { |
| 1764 | if (have_osinfo) { |
| 1765 | /* |
| 1766 | * We assume osinfo.sysname is "Darwin", because |
| 1767 | * __APPLE__ is defined. We just check the version. |
| 1768 | */ |
| 1769 | if (osinfo.release[0] < '8' && |
| 1770 | osinfo.release[1] == '.') { |
| 1771 | /* |
| 1772 | * 10.3 (Darwin 7.x) or earlier. |
| 1773 | */ |
| 1774 | status = PCAP_ERROR_RFMON_NOTSUP; |
| 1775 | goto bad; |
| 1776 | } |
| 1777 | if (osinfo.release[0] == '8' && |
| 1778 | osinfo.release[1] == '.') { |
| 1779 | /* |
| 1780 | * 10.4 (Darwin 8.x). s/en/wlt/ |
| 1781 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1782 | if (strncmp(p->opt.device, "en", 2) != 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1783 | /* |
| 1784 | * Not an enN device; check |
| 1785 | * whether the device even exists. |
| 1786 | */ |
| 1787 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 1788 | if (sockfd != -1) { |
| 1789 | strlcpy(ifrname, |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1790 | p->opt.device, ifnamsiz); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1791 | if (ioctl(sockfd, SIOCGIFFLAGS, |
| 1792 | (char *)&ifr) < 0) { |
| 1793 | /* |
| 1794 | * We assume this |
| 1795 | * failed because |
| 1796 | * the underlying |
| 1797 | * device doesn't |
| 1798 | * exist. |
| 1799 | */ |
| 1800 | status = PCAP_ERROR_NO_SUCH_DEVICE; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1801 | pcap_fmt_errmsg_for_errno(p->errbuf, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1802 | PCAP_ERRBUF_SIZE, |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1803 | errno, |
| 1804 | "SIOCGIFFLAGS failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1805 | } else |
| 1806 | status = PCAP_ERROR_RFMON_NOTSUP; |
| 1807 | close(sockfd); |
| 1808 | } else { |
| 1809 | /* |
| 1810 | * We can't find out whether |
| 1811 | * the device exists, so just |
| 1812 | * report "no such device". |
| 1813 | */ |
| 1814 | status = PCAP_ERROR_NO_SUCH_DEVICE; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1815 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 1816 | PCAP_ERRBUF_SIZE, errno, |
| 1817 | "socket() failed"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1818 | } |
| 1819 | goto bad; |
| 1820 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1821 | wltdev = malloc(strlen(p->opt.device) + 2); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1822 | if (wltdev == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1823 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 1824 | PCAP_ERRBUF_SIZE, errno, |
| 1825 | "malloc"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1826 | status = PCAP_ERROR; |
| 1827 | goto bad; |
| 1828 | } |
| 1829 | strcpy(wltdev, "wlt"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1830 | strcat(wltdev, p->opt.device + 2); |
| 1831 | free(p->opt.device); |
| 1832 | p->opt.device = wltdev; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1833 | } |
| 1834 | /* |
| 1835 | * Everything else is 10.5 or later; for those, |
| 1836 | * we just open the enN device, and set the DLT. |
| 1837 | */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1838 | } |
| 1839 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1840 | #endif /* __APPLE__ */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1841 | |
| 1842 | /* |
| 1843 | * If this is FreeBSD, and the device name begins with "usbus", |
| 1844 | * try to create the interface if it's not available. |
| 1845 | */ |
| 1846 | #if defined(__FreeBSD__) && defined(SIOCIFCREATE2) |
| 1847 | if (strncmp(p->opt.device, usbus_prefix, USBUS_PREFIX_LEN) == 0) { |
| 1848 | /* |
| 1849 | * Do we already have an interface with that name? |
| 1850 | */ |
| 1851 | if (if_nametoindex(p->opt.device) == 0) { |
| 1852 | /* |
| 1853 | * No. We need to create it, and, if we |
| 1854 | * succeed, remember that we should destroy |
| 1855 | * it when the pcap_t is closed. |
| 1856 | */ |
| 1857 | int s; |
| 1858 | |
| 1859 | /* |
| 1860 | * Open a socket to use for ioctls to |
| 1861 | * create the interface. |
| 1862 | */ |
| 1863 | s = socket(AF_LOCAL, SOCK_DGRAM, 0); |
| 1864 | if (s < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1865 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 1866 | PCAP_ERRBUF_SIZE, errno, |
| 1867 | "Can't open socket"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1868 | status = PCAP_ERROR; |
| 1869 | goto bad; |
| 1870 | } |
| 1871 | |
| 1872 | /* |
| 1873 | * If we haven't already done so, arrange to have |
| 1874 | * "pcap_close_all()" called when we exit. |
| 1875 | */ |
| 1876 | if (!pcap_do_addexit(p)) { |
| 1877 | /* |
| 1878 | * "atexit()" failed; don't create the |
| 1879 | * interface, just give up. |
| 1880 | */ |
| 1881 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1882 | "atexit failed"); |
| 1883 | close(s); |
| 1884 | status = PCAP_ERROR; |
| 1885 | goto bad; |
| 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | * Create the interface. |
| 1890 | */ |
| 1891 | strlcpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name)); |
| 1892 | if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) { |
| 1893 | if (errno == EINVAL) { |
| 1894 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1895 | "Invalid USB bus interface %s", |
| 1896 | p->opt.device); |
| 1897 | } else { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1898 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 1899 | PCAP_ERRBUF_SIZE, errno, |
| 1900 | "Can't create interface for %s", |
| 1901 | p->opt.device); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1902 | } |
| 1903 | close(s); |
| 1904 | status = PCAP_ERROR; |
| 1905 | goto bad; |
| 1906 | } |
| 1907 | |
| 1908 | /* |
| 1909 | * Make sure we clean this up when we close. |
| 1910 | */ |
| 1911 | pb->must_do_on_close |= MUST_DESTROY_USBUS; |
| 1912 | |
| 1913 | /* |
| 1914 | * Add this to the list of pcaps to close when we exit. |
| 1915 | */ |
| 1916 | pcap_add_to_pcaps_to_close(p); |
| 1917 | } |
| 1918 | } |
| 1919 | #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */ |
| 1920 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1921 | #ifdef HAVE_ZEROCOPY_BPF |
| 1922 | /* |
| 1923 | * If the BPF extension to set buffer mode is present, try setting |
| 1924 | * the mode to zero-copy. If that fails, use regular buffering. If |
| 1925 | * it succeeds but other setup fails, return an error to the user. |
| 1926 | */ |
| 1927 | bufmode = BPF_BUFMODE_ZBUF; |
| 1928 | if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) { |
| 1929 | /* |
| 1930 | * We have zerocopy BPF; use it. |
| 1931 | */ |
| 1932 | pb->zerocopy = 1; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 1933 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1934 | /* |
| 1935 | * How to pick a buffer size: first, query the maximum buffer |
| 1936 | * size supported by zero-copy. This also lets us quickly |
| 1937 | * determine whether the kernel generally supports zero-copy. |
| 1938 | * Then, if a buffer size was specified, use that, otherwise |
| 1939 | * query the default buffer size, which reflects kernel |
| 1940 | * policy for a desired default. Round to the nearest page |
| 1941 | * size. |
| 1942 | */ |
| 1943 | if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1944 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1945 | errno, "BIOCGETZMAX"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1946 | status = PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1947 | goto bad; |
| 1948 | } |
| 1949 | |
| 1950 | if (p->opt.buffer_size != 0) { |
| 1951 | /* |
| 1952 | * A buffer size was explicitly specified; use it. |
| 1953 | */ |
| 1954 | v = p->opt.buffer_size; |
| 1955 | } else { |
| 1956 | if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || |
| 1957 | v < DEFAULT_BUFSIZE) |
| 1958 | v = DEFAULT_BUFSIZE; |
| 1959 | } |
| 1960 | #ifndef roundup |
| 1961 | #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */ |
| 1962 | #endif |
| 1963 | pb->zbufsize = roundup(v, getpagesize()); |
| 1964 | if (pb->zbufsize > zbufmax) |
| 1965 | pb->zbufsize = zbufmax; |
| 1966 | pb->zbuf1 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE, |
| 1967 | MAP_ANON, -1, 0); |
| 1968 | pb->zbuf2 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE, |
| 1969 | MAP_ANON, -1, 0); |
| 1970 | if (pb->zbuf1 == MAP_FAILED || pb->zbuf2 == MAP_FAILED) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1971 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1972 | errno, "mmap"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1973 | status = PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1974 | goto bad; |
| 1975 | } |
| 1976 | memset(&bz, 0, sizeof(bz)); /* bzero() deprecated, replaced with memset() */ |
| 1977 | bz.bz_bufa = pb->zbuf1; |
| 1978 | bz.bz_bufb = pb->zbuf2; |
| 1979 | bz.bz_buflen = pb->zbufsize; |
| 1980 | if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1981 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1982 | errno, "BIOCSETZBUF"); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1983 | status = PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1984 | goto bad; |
| 1985 | } |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 1986 | (void)strncpy(ifrname, p->opt.device, ifnamsiz); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1987 | if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 1988 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 1989 | errno, "BIOCSETIF: %s", p->opt.device); |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 1990 | status = PCAP_ERROR; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 1991 | goto bad; |
| 1992 | } |
| 1993 | v = pb->zbufsize - sizeof(struct bpf_zbuf_header); |
| 1994 | } else |
| 1995 | #endif |
| 1996 | { |
| 1997 | /* |
| 1998 | * We don't have zerocopy BPF. |
| 1999 | * Set the buffer size. |
| 2000 | */ |
| 2001 | if (p->opt.buffer_size != 0) { |
| 2002 | /* |
| 2003 | * A buffer size was explicitly specified; use it. |
| 2004 | */ |
| 2005 | if (ioctl(fd, BIOCSBLEN, |
| 2006 | (caddr_t)&p->opt.buffer_size) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2007 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 2008 | PCAP_ERRBUF_SIZE, errno, |
| 2009 | "BIOCSBLEN: %s", p->opt.device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2010 | status = PCAP_ERROR; |
| 2011 | goto bad; |
| 2012 | } |
| 2013 | |
| 2014 | /* |
| 2015 | * Now bind to the device. |
| 2016 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2017 | (void)strncpy(ifrname, p->opt.device, ifnamsiz); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2018 | #ifdef BIOCSETLIF |
| 2019 | if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) < 0) |
| 2020 | #else |
| 2021 | if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) |
| 2022 | #endif |
| 2023 | { |
| 2024 | status = check_setif_failure(p, errno); |
| 2025 | goto bad; |
| 2026 | } |
| 2027 | } else { |
| 2028 | /* |
| 2029 | * No buffer size was explicitly specified. |
| 2030 | * |
| 2031 | * Try finding a good size for the buffer; |
| 2032 | * DEFAULT_BUFSIZE may be too big, so keep |
| 2033 | * cutting it in half until we find a size |
| 2034 | * that works, or run out of sizes to try. |
| 2035 | * If the default is larger, don't make it smaller. |
| 2036 | */ |
| 2037 | if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || |
| 2038 | v < DEFAULT_BUFSIZE) |
| 2039 | v = DEFAULT_BUFSIZE; |
| 2040 | for ( ; v != 0; v >>= 1) { |
| 2041 | /* |
| 2042 | * Ignore the return value - this is because the |
| 2043 | * call fails on BPF systems that don't have |
| 2044 | * kernel malloc. And if the call fails, it's |
| 2045 | * no big deal, we just continue to use the |
| 2046 | * standard buffer size. |
| 2047 | */ |
| 2048 | (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v); |
| 2049 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2050 | (void)strncpy(ifrname, p->opt.device, ifnamsiz); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2051 | #ifdef BIOCSETLIF |
| 2052 | if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) >= 0) |
| 2053 | #else |
| 2054 | if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0) |
| 2055 | #endif |
| 2056 | break; /* that size worked; we're done */ |
| 2057 | |
| 2058 | if (errno != ENOBUFS) { |
| 2059 | status = check_setif_failure(p, errno); |
| 2060 | goto bad; |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | if (v == 0) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2065 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2066 | "BIOCSBLEN: %s: No buffer size worked", |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2067 | p->opt.device); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2068 | status = PCAP_ERROR; |
| 2069 | goto bad; |
| 2070 | } |
| 2071 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | /* Get the data link layer type. */ |
| 2075 | if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2076 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2077 | errno, "BIOCGDLT"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2078 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2079 | goto bad; |
| 2080 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2081 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2082 | #ifdef _AIX |
| 2083 | /* |
| 2084 | * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT. |
| 2085 | */ |
| 2086 | switch (v) { |
| 2087 | |
| 2088 | case IFT_ETHER: |
| 2089 | case IFT_ISO88023: |
| 2090 | v = DLT_EN10MB; |
| 2091 | break; |
| 2092 | |
| 2093 | case IFT_FDDI: |
| 2094 | v = DLT_FDDI; |
| 2095 | break; |
| 2096 | |
| 2097 | case IFT_ISO88025: |
| 2098 | v = DLT_IEEE802; |
| 2099 | break; |
| 2100 | |
| 2101 | case IFT_LOOP: |
| 2102 | v = DLT_NULL; |
| 2103 | break; |
| 2104 | |
| 2105 | default: |
| 2106 | /* |
| 2107 | * We don't know what to map this to yet. |
| 2108 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2109 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u", |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2110 | v); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2111 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2112 | goto bad; |
| 2113 | } |
| 2114 | #endif |
| 2115 | #if _BSDI_VERSION - 0 >= 199510 |
| 2116 | /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */ |
| 2117 | switch (v) { |
| 2118 | |
| 2119 | case DLT_SLIP: |
| 2120 | v = DLT_SLIP_BSDOS; |
| 2121 | break; |
| 2122 | |
| 2123 | case DLT_PPP: |
| 2124 | v = DLT_PPP_BSDOS; |
| 2125 | break; |
| 2126 | |
| 2127 | case 11: /*DLT_FR*/ |
| 2128 | v = DLT_FRELAY; |
| 2129 | break; |
| 2130 | |
| 2131 | case 12: /*DLT_C_HDLC*/ |
| 2132 | v = DLT_CHDLC; |
| 2133 | break; |
| 2134 | } |
| 2135 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2136 | |
| 2137 | #ifdef BIOCGDLTLIST |
| 2138 | /* |
| 2139 | * We know the default link type -- now determine all the DLTs |
| 2140 | * this interface supports. If this fails with EINVAL, it's |
| 2141 | * not fatal; we just don't get to use the feature later. |
| 2142 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2143 | if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) { |
| 2144 | status = PCAP_ERROR; |
| 2145 | goto bad; |
| 2146 | } |
| 2147 | p->dlt_count = bdl.bfl_len; |
| 2148 | p->dlt_list = bdl.bfl_list; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2149 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2150 | #ifdef __APPLE__ |
| 2151 | /* |
| 2152 | * Monitor mode fun, continued. |
| 2153 | * |
| 2154 | * For 10.5 and, we're assuming, later releases, as noted above, |
| 2155 | * 802.1 adapters that support monitor mode offer both DLT_EN10MB, |
| 2156 | * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information |
| 2157 | * DLT_ value. Choosing one of the 802.11 DLT_ values will turn |
| 2158 | * monitor mode on. |
| 2159 | * |
| 2160 | * Therefore, if the user asked for monitor mode, we filter out |
| 2161 | * the DLT_EN10MB value, as you can't get that in monitor mode, |
| 2162 | * and, if the user didn't ask for monitor mode, we filter out |
| 2163 | * the 802.11 DLT_ values, because selecting those will turn |
| 2164 | * monitor mode on. Then, for monitor mode, if an 802.11-plus- |
| 2165 | * radio DLT_ value is offered, we try to select that, otherwise |
| 2166 | * we try to select DLT_IEEE802_11. |
| 2167 | */ |
| 2168 | if (have_osinfo) { |
| 2169 | if (isdigit((unsigned)osinfo.release[0]) && |
| 2170 | (osinfo.release[0] == '9' || |
| 2171 | isdigit((unsigned)osinfo.release[1]))) { |
| 2172 | /* |
| 2173 | * 10.5 (Darwin 9.x), or later. |
| 2174 | */ |
| 2175 | new_dlt = find_802_11(&bdl); |
| 2176 | if (new_dlt != -1) { |
| 2177 | /* |
| 2178 | * We have at least one 802.11 DLT_ value, |
| 2179 | * so this is an 802.11 interface. |
| 2180 | * new_dlt is the best of the 802.11 |
| 2181 | * DLT_ values in the list. |
| 2182 | */ |
| 2183 | if (p->opt.rfmon) { |
| 2184 | /* |
| 2185 | * Our caller wants monitor mode. |
| 2186 | * Purge DLT_EN10MB from the list |
| 2187 | * of link-layer types, as selecting |
| 2188 | * it will keep monitor mode off. |
| 2189 | */ |
| 2190 | remove_en(p); |
| 2191 | |
| 2192 | /* |
| 2193 | * If the new mode we want isn't |
| 2194 | * the default mode, attempt to |
| 2195 | * select the new mode. |
| 2196 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2197 | if ((u_int)new_dlt != v) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2198 | if (ioctl(p->fd, BIOCSDLT, |
| 2199 | &new_dlt) != -1) { |
| 2200 | /* |
| 2201 | * We succeeded; |
| 2202 | * make this the |
| 2203 | * new DLT_ value. |
| 2204 | */ |
| 2205 | v = new_dlt; |
| 2206 | } |
| 2207 | } |
| 2208 | } else { |
| 2209 | /* |
| 2210 | * Our caller doesn't want |
| 2211 | * monitor mode. Unless this |
| 2212 | * is being done by pcap_open_live(), |
| 2213 | * purge the 802.11 link-layer types |
| 2214 | * from the list, as selecting |
| 2215 | * one of them will turn monitor |
| 2216 | * mode on. |
| 2217 | */ |
| 2218 | if (!p->oldstyle) |
| 2219 | remove_802_11(p); |
| 2220 | } |
| 2221 | } else { |
| 2222 | if (p->opt.rfmon) { |
| 2223 | /* |
| 2224 | * The caller requested monitor |
| 2225 | * mode, but we have no 802.11 |
| 2226 | * link-layer types, so they |
| 2227 | * can't have it. |
| 2228 | */ |
| 2229 | status = PCAP_ERROR_RFMON_NOTSUP; |
| 2230 | goto bad; |
| 2231 | } |
| 2232 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2233 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2234 | } |
| 2235 | #elif defined(HAVE_BSD_IEEE80211) |
| 2236 | /* |
| 2237 | * *BSD with the new 802.11 ioctls. |
| 2238 | * Do we want monitor mode? |
| 2239 | */ |
| 2240 | if (p->opt.rfmon) { |
| 2241 | /* |
| 2242 | * Try to put the interface into monitor mode. |
| 2243 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2244 | retv = monitor_mode(p, 1); |
| 2245 | if (retv != 0) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2246 | /* |
| 2247 | * We failed. |
| 2248 | */ |
Elliott Hughes | d8845d7 | 2015-10-19 18:07:04 -0700 | [diff] [blame] | 2249 | status = retv; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2250 | goto bad; |
| 2251 | } |
| 2252 | |
| 2253 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2254 | * We're in monitor mode. |
| 2255 | * Try to find the best 802.11 DLT_ value and, if we |
| 2256 | * succeed, try to switch to that mode if we're not |
| 2257 | * already in that mode. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2258 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2259 | new_dlt = find_802_11(&bdl); |
| 2260 | if (new_dlt != -1) { |
| 2261 | /* |
| 2262 | * We have at least one 802.11 DLT_ value. |
| 2263 | * new_dlt is the best of the 802.11 |
| 2264 | * DLT_ values in the list. |
| 2265 | * |
| 2266 | * If the new mode we want isn't the default mode, |
| 2267 | * attempt to select the new mode. |
| 2268 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2269 | if ((u_int)new_dlt != v) { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2270 | if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) { |
| 2271 | /* |
| 2272 | * We succeeded; make this the |
| 2273 | * new DLT_ value. |
| 2274 | */ |
| 2275 | v = new_dlt; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2276 | } |
| 2277 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2278 | } |
| 2279 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2280 | #endif /* various platforms */ |
| 2281 | #endif /* BIOCGDLTLIST */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2282 | |
| 2283 | /* |
| 2284 | * If this is an Ethernet device, and we don't have a DLT_ list, |
| 2285 | * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give |
| 2286 | * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to |
| 2287 | * do, but there's not much we can do about that without finding |
| 2288 | * some other way of determining whether it's an Ethernet or 802.11 |
| 2289 | * device.) |
| 2290 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2291 | if (v == DLT_EN10MB && p->dlt_count == 0) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2292 | p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); |
| 2293 | /* |
| 2294 | * If that fails, just leave the list empty. |
| 2295 | */ |
| 2296 | if (p->dlt_list != NULL) { |
| 2297 | p->dlt_list[0] = DLT_EN10MB; |
| 2298 | p->dlt_list[1] = DLT_DOCSIS; |
| 2299 | p->dlt_count = 2; |
| 2300 | } |
| 2301 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2302 | #ifdef PCAP_FDDIPAD |
| 2303 | if (v == DLT_FDDI) |
| 2304 | p->fddipad = PCAP_FDDIPAD; |
| 2305 | else |
| 2306 | #endif |
| 2307 | p->fddipad = 0; |
| 2308 | p->linktype = v; |
| 2309 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2310 | #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT) |
| 2311 | /* |
| 2312 | * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so |
| 2313 | * the link-layer source address isn't forcibly overwritten. |
| 2314 | * (Should we ignore errors? Should we do this only if |
| 2315 | * we're open for writing?) |
| 2316 | * |
| 2317 | * XXX - I seem to remember some packet-sending bug in some |
| 2318 | * BSDs - check CVS log for "bpf.c"? |
| 2319 | */ |
| 2320 | if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2321 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2322 | errno, "BIOCSHDRCMPLT"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2323 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2324 | goto bad; |
| 2325 | } |
| 2326 | #endif |
| 2327 | /* set timeout */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2328 | #ifdef HAVE_ZEROCOPY_BPF |
| 2329 | /* |
| 2330 | * In zero-copy mode, we just use the timeout in select(). |
| 2331 | * XXX - what if we're in non-blocking mode and the *application* |
| 2332 | * is using select() or poll() or kqueues or....? |
| 2333 | */ |
| 2334 | if (p->opt.timeout && !pb->zerocopy) { |
| 2335 | #else |
| 2336 | if (p->opt.timeout) { |
| 2337 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2338 | /* |
| 2339 | * XXX - is this seconds/nanoseconds in AIX? |
| 2340 | * (Treating it as such doesn't fix the timeout |
| 2341 | * problem described below.) |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2342 | * |
| 2343 | * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in |
| 2344 | * 64-bit userland - it takes, as an argument, a |
| 2345 | * "struct BPF_TIMEVAL", which has 32-bit tv_sec |
| 2346 | * and tv_usec, rather than a "struct timeval". |
| 2347 | * |
| 2348 | * If this platform defines "struct BPF_TIMEVAL", |
| 2349 | * we check whether the structure size in BIOCSRTIMEOUT |
| 2350 | * is that of a "struct timeval" and, if not, we use |
| 2351 | * a "struct BPF_TIMEVAL" rather than a "struct timeval". |
| 2352 | * (That way, if the bug is fixed in a future release, |
| 2353 | * we will still do the right thing.) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2354 | */ |
| 2355 | struct timeval to; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2356 | #ifdef HAVE_STRUCT_BPF_TIMEVAL |
| 2357 | struct BPF_TIMEVAL bpf_to; |
| 2358 | |
| 2359 | if (IOCPARM_LEN(BIOCSRTIMEOUT) != sizeof(struct timeval)) { |
| 2360 | bpf_to.tv_sec = p->opt.timeout / 1000; |
| 2361 | bpf_to.tv_usec = (p->opt.timeout * 1000) % 1000000; |
| 2362 | if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2363 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 2364 | errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2365 | status = PCAP_ERROR; |
| 2366 | goto bad; |
| 2367 | } |
| 2368 | } else { |
| 2369 | #endif |
| 2370 | to.tv_sec = p->opt.timeout / 1000; |
| 2371 | to.tv_usec = (p->opt.timeout * 1000) % 1000000; |
| 2372 | if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2373 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 2374 | errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2375 | status = PCAP_ERROR; |
| 2376 | goto bad; |
| 2377 | } |
| 2378 | #ifdef HAVE_STRUCT_BPF_TIMEVAL |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2379 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2380 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2381 | } |
| 2382 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2383 | #ifdef BIOCIMMEDIATE |
| 2384 | /* |
| 2385 | * Darren Reed notes that |
| 2386 | * |
| 2387 | * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the |
| 2388 | * timeout appears to be ignored and it waits until the buffer |
| 2389 | * is filled before returning. The result of not having it |
| 2390 | * set is almost worse than useless if your BPF filter |
| 2391 | * is reducing things to only a few packets (i.e. one every |
| 2392 | * second or so). |
| 2393 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2394 | * so we always turn BIOCIMMEDIATE mode on if this is AIX. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2395 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2396 | * For other platforms, we don't turn immediate mode on by default, |
| 2397 | * as that would mean we get woken up for every packet, which |
| 2398 | * probably isn't what you want for a packet sniffer. |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2399 | * |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2400 | * We set immediate mode if the caller requested it by calling |
| 2401 | * pcap_set_immediate() before calling pcap_activate(). |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2402 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2403 | #ifndef _AIX |
| 2404 | if (p->opt.immediate) { |
| 2405 | #endif /* _AIX */ |
| 2406 | v = 1; |
| 2407 | if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2408 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2409 | errno, "BIOCIMMEDIATE"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2410 | status = PCAP_ERROR; |
| 2411 | goto bad; |
| 2412 | } |
| 2413 | #ifndef _AIX |
| 2414 | } |
| 2415 | #endif /* _AIX */ |
| 2416 | #else /* BIOCIMMEDIATE */ |
| 2417 | if (p->opt.immediate) { |
| 2418 | /* |
| 2419 | * We don't support immediate mode. Fail. |
| 2420 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2421 | pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Immediate mode not supported"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2422 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2423 | goto bad; |
| 2424 | } |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2425 | #endif /* BIOCIMMEDIATE */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2426 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2427 | if (p->opt.promisc) { |
| 2428 | /* set promiscuous mode, just warn if it fails */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2429 | if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2430 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2431 | errno, "BIOCPROMISC"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2432 | status = PCAP_WARNING_PROMISC_NOTSUP; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2433 | } |
| 2434 | } |
| 2435 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2436 | #ifdef BIOCSTSTAMP |
| 2437 | v = BPF_T_BINTIME; |
| 2438 | if (ioctl(p->fd, BIOCSTSTAMP, &v) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2439 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2440 | errno, "BIOCSTSTAMP"); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2441 | status = PCAP_ERROR; |
| 2442 | goto bad; |
| 2443 | } |
| 2444 | #endif /* BIOCSTSTAMP */ |
| 2445 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2446 | if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2447 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2448 | errno, "BIOCGBLEN"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2449 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2450 | goto bad; |
| 2451 | } |
| 2452 | p->bufsize = v; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2453 | #ifdef HAVE_ZEROCOPY_BPF |
| 2454 | if (!pb->zerocopy) { |
| 2455 | #endif |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2456 | p->buffer = malloc(p->bufsize); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2457 | if (p->buffer == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2458 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2459 | errno, "malloc"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2460 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2461 | goto bad; |
| 2462 | } |
| 2463 | #ifdef _AIX |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2464 | /* For some strange reason this seems to prevent the EFAULT |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2465 | * problems we have experienced from AIX BPF. */ |
| 2466 | memset(p->buffer, 0x0, p->bufsize); |
| 2467 | #endif |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2468 | #ifdef HAVE_ZEROCOPY_BPF |
| 2469 | } |
| 2470 | #endif |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2471 | |
| 2472 | /* |
| 2473 | * If there's no filter program installed, there's |
| 2474 | * no indication to the kernel of what the snapshot |
| 2475 | * length should be, so no snapshotting is done. |
| 2476 | * |
| 2477 | * Therefore, when we open the device, we install |
| 2478 | * an "accept everything" filter with the specified |
| 2479 | * snapshot length. |
| 2480 | */ |
| 2481 | total_insn.code = (u_short)(BPF_RET | BPF_K); |
| 2482 | total_insn.jt = 0; |
| 2483 | total_insn.jf = 0; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2484 | total_insn.k = p->snapshot; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2485 | |
| 2486 | total_prog.bf_len = 1; |
| 2487 | total_prog.bf_insns = &total_insn; |
| 2488 | if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2489 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2490 | errno, "BIOCSETF"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2491 | status = PCAP_ERROR; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2492 | goto bad; |
| 2493 | } |
| 2494 | |
| 2495 | /* |
| 2496 | * On most BPF platforms, either you can do a "select()" or |
| 2497 | * "poll()" on a BPF file descriptor and it works correctly, |
| 2498 | * or you can do it and it will return "readable" if the |
| 2499 | * hold buffer is full but not if the timeout expires *and* |
| 2500 | * a non-blocking read will, if the hold buffer is empty |
| 2501 | * but the store buffer isn't empty, rotate the buffers |
| 2502 | * and return what packets are available. |
| 2503 | * |
| 2504 | * In the latter case, the fact that a non-blocking read |
| 2505 | * will give you the available packets means you can work |
| 2506 | * around the failure of "select()" and "poll()" to wake up |
| 2507 | * and return "readable" when the timeout expires by using |
| 2508 | * the timeout as the "select()" or "poll()" timeout, putting |
| 2509 | * the BPF descriptor into non-blocking mode, and read from |
| 2510 | * it regardless of whether "select()" reports it as readable |
| 2511 | * or not. |
| 2512 | * |
| 2513 | * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()" |
| 2514 | * won't wake up and return "readable" if the timer expires |
| 2515 | * and non-blocking reads return EWOULDBLOCK if the hold |
| 2516 | * buffer is empty, even if the store buffer is non-empty. |
| 2517 | * |
| 2518 | * This means the workaround in question won't work. |
| 2519 | * |
| 2520 | * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd" |
| 2521 | * to -1, which means "sorry, you can't use 'select()' or 'poll()' |
| 2522 | * here". On all other BPF platforms, we set it to the FD for |
| 2523 | * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking |
| 2524 | * read will, if the hold buffer is empty and the store buffer |
| 2525 | * isn't empty, rotate the buffers and return what packets are |
| 2526 | * there (and in sufficiently recent versions of OpenBSD |
| 2527 | * "select()" and "poll()" should work correctly). |
| 2528 | * |
| 2529 | * XXX - what about AIX? |
| 2530 | */ |
| 2531 | p->selectable_fd = p->fd; /* assume select() works until we know otherwise */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2532 | if (have_osinfo) { |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2533 | /* |
| 2534 | * We can check what OS this is. |
| 2535 | */ |
| 2536 | if (strcmp(osinfo.sysname, "FreeBSD") == 0) { |
| 2537 | if (strncmp(osinfo.release, "4.3-", 4) == 0 || |
| 2538 | strncmp(osinfo.release, "4.4-", 4) == 0) |
| 2539 | p->selectable_fd = -1; |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | p->read_op = pcap_read_bpf; |
| 2544 | p->inject_op = pcap_inject_bpf; |
| 2545 | p->setfilter_op = pcap_setfilter_bpf; |
| 2546 | p->setdirection_op = pcap_setdirection_bpf; |
| 2547 | p->set_datalink_op = pcap_set_datalink_bpf; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2548 | p->getnonblock_op = pcap_getnonblock_bpf; |
| 2549 | p->setnonblock_op = pcap_setnonblock_bpf; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2550 | p->stats_op = pcap_stats_bpf; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2551 | p->cleanup_op = pcap_cleanup_bpf; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2552 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2553 | return (status); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2554 | bad: |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2555 | pcap_cleanup_bpf(p); |
| 2556 | return (status); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2557 | } |
| 2558 | |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2559 | /* |
| 2560 | * Not all interfaces can be bound to by BPF, so try to bind to |
| 2561 | * the specified interface; return 0 if we fail with |
| 2562 | * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried |
| 2563 | * to bind, which means this interface isn't in the list of interfaces |
| 2564 | * attached to BPF) and 1 otherwise. |
| 2565 | */ |
| 2566 | static int |
| 2567 | check_bpf_bindable(const char *name) |
| 2568 | { |
| 2569 | int fd; |
| 2570 | char errbuf[PCAP_ERRBUF_SIZE]; |
| 2571 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2572 | /* |
| 2573 | * On macOS, we don't do this check if the device name begins |
| 2574 | * with "wlt"; at least some versions of macOS (actually, it |
| 2575 | * was called "Mac OS X" then...) offer monitor mode capturing |
| 2576 | * by having a separate "monitor mode" device for each wireless |
| 2577 | * adapter, rather than by implementing the ioctls that |
| 2578 | * {Free,Net,Open,DragonFly}BSD provide. Opening that device |
| 2579 | * puts the adapter into monitor mode, which, at least for |
| 2580 | * some adapters, causes them to deassociate from the network |
| 2581 | * with which they're associated. |
| 2582 | * |
| 2583 | * Instead, we try to open the corresponding "en" device (so |
| 2584 | * that we don't end up with, for users without sufficient |
| 2585 | * privilege to open capture devices, a list of adapters that |
| 2586 | * only includes the wlt devices). |
| 2587 | */ |
| 2588 | #ifdef __APPLE__ |
| 2589 | if (strncmp(name, "wlt", 3) == 0) { |
| 2590 | char *en_name; |
| 2591 | size_t en_name_len; |
| 2592 | |
| 2593 | /* |
| 2594 | * Try to allocate a buffer for the "en" |
| 2595 | * device's name. |
| 2596 | */ |
| 2597 | en_name_len = strlen(name) - 1; |
| 2598 | en_name = malloc(en_name_len + 1); |
| 2599 | if (en_name == NULL) { |
| 2600 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 2601 | errno, "malloc"); |
| 2602 | return (-1); |
| 2603 | } |
| 2604 | strcpy(en_name, "en"); |
| 2605 | strcat(en_name, name + 3); |
| 2606 | fd = bpf_open_and_bind(en_name, errbuf); |
| 2607 | free(en_name); |
| 2608 | } else |
| 2609 | #endif /* __APPLE */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2610 | fd = bpf_open_and_bind(name, errbuf); |
| 2611 | if (fd < 0) { |
| 2612 | /* |
| 2613 | * Error - was it PCAP_ERROR_NO_SUCH_DEVICE? |
| 2614 | */ |
| 2615 | if (fd == PCAP_ERROR_NO_SUCH_DEVICE) { |
| 2616 | /* |
| 2617 | * Yes, so we can't bind to this because it's |
| 2618 | * not something supported by BPF. |
| 2619 | */ |
| 2620 | return (0); |
| 2621 | } |
| 2622 | /* |
| 2623 | * No, so we don't know whether it's supported or not; |
| 2624 | * say it is, so that the user can at least try to |
| 2625 | * open it and report the error (which is probably |
| 2626 | * "you don't have permission to open BPF devices"; |
| 2627 | * reporting those interfaces means users will ask |
| 2628 | * "why am I getting a permissions error when I try |
| 2629 | * to capture" rather than "why am I not seeing any |
| 2630 | * interfaces", making the underlying problem clearer). |
| 2631 | */ |
| 2632 | return (1); |
| 2633 | } |
| 2634 | |
| 2635 | /* |
| 2636 | * Success. |
| 2637 | */ |
| 2638 | close(fd); |
| 2639 | return (1); |
| 2640 | } |
| 2641 | |
| 2642 | #if defined(__FreeBSD__) && defined(SIOCIFCREATE2) |
| 2643 | static int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2644 | get_usb_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_) |
| 2645 | { |
| 2646 | /* |
| 2647 | * XXX - if there's a way to determine whether there's something |
| 2648 | * plugged into a given USB bus, use that to determine whether |
| 2649 | * this device is "connected" or not. |
| 2650 | */ |
| 2651 | return (0); |
| 2652 | } |
| 2653 | |
| 2654 | static int |
| 2655 | finddevs_usb(pcap_if_list_t *devlistp, char *errbuf) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2656 | { |
| 2657 | DIR *usbdir; |
| 2658 | struct dirent *usbitem; |
| 2659 | size_t name_max; |
| 2660 | char *name; |
| 2661 | |
| 2662 | /* |
| 2663 | * We might have USB sniffing support, so try looking for USB |
| 2664 | * interfaces. |
| 2665 | * |
| 2666 | * We want to report a usbusN device for each USB bus, but |
| 2667 | * usbusN interfaces might, or might not, exist for them - |
| 2668 | * we create one if there isn't already one. |
| 2669 | * |
| 2670 | * So, instead, we look in /dev/usb for all buses and create |
| 2671 | * a "usbusN" device for each one. |
| 2672 | */ |
| 2673 | usbdir = opendir("/dev/usb"); |
| 2674 | if (usbdir == NULL) { |
| 2675 | /* |
| 2676 | * Just punt. |
| 2677 | */ |
| 2678 | return (0); |
| 2679 | } |
| 2680 | |
| 2681 | /* |
| 2682 | * Leave enough room for a 32-bit (10-digit) bus number. |
| 2683 | * Yes, that's overkill, but we won't be using |
| 2684 | * the buffer very long. |
| 2685 | */ |
| 2686 | name_max = USBUS_PREFIX_LEN + 10 + 1; |
| 2687 | name = malloc(name_max); |
| 2688 | if (name == NULL) { |
| 2689 | closedir(usbdir); |
| 2690 | return (0); |
| 2691 | } |
| 2692 | while ((usbitem = readdir(usbdir)) != NULL) { |
| 2693 | char *p; |
| 2694 | size_t busnumlen; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2695 | |
| 2696 | if (strcmp(usbitem->d_name, ".") == 0 || |
| 2697 | strcmp(usbitem->d_name, "..") == 0) { |
| 2698 | /* |
| 2699 | * Ignore these. |
| 2700 | */ |
| 2701 | continue; |
| 2702 | } |
| 2703 | p = strchr(usbitem->d_name, '.'); |
| 2704 | if (p == NULL) |
| 2705 | continue; |
| 2706 | busnumlen = p - usbitem->d_name; |
| 2707 | memcpy(name, usbus_prefix, USBUS_PREFIX_LEN); |
| 2708 | memcpy(name + USBUS_PREFIX_LEN, usbitem->d_name, busnumlen); |
| 2709 | *(name + USBUS_PREFIX_LEN + busnumlen) = '\0'; |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2710 | /* |
| 2711 | * There's an entry in this directory for every USB device, |
| 2712 | * not for every bus; if there's more than one device on |
| 2713 | * the bus, there'll be more than one entry for that bus, |
| 2714 | * so we need to avoid adding multiple capture devices |
| 2715 | * for each bus. |
| 2716 | */ |
| 2717 | if (find_or_add_dev(devlistp, name, PCAP_IF_UP, |
| 2718 | get_usb_if_flags, NULL, errbuf) == NULL) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2719 | free(name); |
| 2720 | closedir(usbdir); |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2721 | return (PCAP_ERROR); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2722 | } |
| 2723 | } |
| 2724 | free(name); |
| 2725 | closedir(usbdir); |
| 2726 | return (0); |
| 2727 | } |
| 2728 | #endif |
| 2729 | |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2730 | /* |
| 2731 | * Get additional flags for a device, using SIOCGIFMEDIA. |
| 2732 | */ |
| 2733 | #ifdef SIOCGIFMEDIA |
| 2734 | static int |
| 2735 | get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf) |
| 2736 | { |
| 2737 | int sock; |
| 2738 | struct ifmediareq req; |
| 2739 | |
| 2740 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 2741 | if (sock == -1) { |
| 2742 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno, |
| 2743 | "Can't create socket to get media information for %s", |
| 2744 | name); |
| 2745 | return (-1); |
| 2746 | } |
| 2747 | memset(&req, 0, sizeof(req)); |
| 2748 | strncpy(req.ifm_name, name, sizeof(req.ifm_name)); |
| 2749 | if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { |
| 2750 | if (errno == EOPNOTSUPP || errno == EINVAL || errno == ENOTTY || |
| 2751 | errno == ENODEV) { |
| 2752 | /* |
| 2753 | * Not supported, so we can't provide any |
| 2754 | * additional information. Assume that |
| 2755 | * this means that "connected" vs. |
| 2756 | * "disconnected" doesn't apply. |
| 2757 | */ |
| 2758 | *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE; |
| 2759 | close(sock); |
| 2760 | return (0); |
| 2761 | } |
| 2762 | pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno, |
| 2763 | "SIOCGIFMEDIA on %s failed", name); |
| 2764 | close(sock); |
| 2765 | return (-1); |
| 2766 | } |
| 2767 | close(sock); |
| 2768 | |
| 2769 | /* |
| 2770 | * OK, what type of network is this? |
| 2771 | */ |
| 2772 | switch (IFM_TYPE(req.ifm_active)) { |
| 2773 | |
| 2774 | case IFM_IEEE80211: |
| 2775 | /* |
| 2776 | * Wireless. |
| 2777 | */ |
| 2778 | *flags |= PCAP_IF_WIRELESS; |
| 2779 | break; |
| 2780 | } |
| 2781 | |
| 2782 | /* |
| 2783 | * Do we know whether it's connected? |
| 2784 | */ |
| 2785 | if (req.ifm_status & IFM_AVALID) { |
| 2786 | /* |
| 2787 | * Yes. |
| 2788 | */ |
| 2789 | if (req.ifm_status & IFM_ACTIVE) { |
| 2790 | /* |
| 2791 | * It's connected. |
| 2792 | */ |
| 2793 | *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED; |
| 2794 | } else { |
| 2795 | /* |
| 2796 | * It's disconnected. |
| 2797 | */ |
| 2798 | *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED; |
| 2799 | } |
| 2800 | } |
| 2801 | return (0); |
| 2802 | } |
| 2803 | #else |
| 2804 | static int |
| 2805 | get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_) |
| 2806 | { |
| 2807 | /* |
| 2808 | * Nothing we can do other than mark loopback devices as "the |
| 2809 | * connected/disconnected status doesn't apply". |
| 2810 | * |
| 2811 | * XXX - on Solaris, can we do what the dladm command does, |
| 2812 | * i.e. get a connected/disconnected indication from a kstat? |
| 2813 | * (Note that you can also get the link speed, and possibly |
| 2814 | * other information, from a kstat as well.) |
| 2815 | */ |
| 2816 | if (*flags & PCAP_IF_LOOPBACK) { |
| 2817 | /* |
| 2818 | * Loopback devices aren't wireless, and "connected"/ |
| 2819 | * "disconnected" doesn't apply to them. |
| 2820 | */ |
| 2821 | *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE; |
| 2822 | return (0); |
| 2823 | } |
| 2824 | return (0); |
| 2825 | } |
| 2826 | #endif |
| 2827 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2828 | int |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2829 | pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2830 | { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2831 | /* |
| 2832 | * Get the list of regular interfaces first. |
| 2833 | */ |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2834 | if (pcap_findalldevs_interfaces(devlistp, errbuf, check_bpf_bindable, |
| 2835 | get_if_flags) == -1) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2836 | return (-1); /* failure */ |
| 2837 | |
| 2838 | #if defined(__FreeBSD__) && defined(SIOCIFCREATE2) |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2839 | if (finddevs_usb(devlistp, errbuf) == -1) |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2840 | return (-1); |
| 2841 | #endif |
| 2842 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 2843 | return (0); |
| 2844 | } |
| 2845 | |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2846 | #ifdef HAVE_BSD_IEEE80211 |
| 2847 | static int |
| 2848 | monitor_mode(pcap_t *p, int set) |
| 2849 | { |
| 2850 | struct pcap_bpf *pb = p->priv; |
| 2851 | int sock; |
| 2852 | struct ifmediareq req; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2853 | IFM_ULIST_TYPE *media_list; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2854 | int i; |
| 2855 | int can_do; |
| 2856 | struct ifreq ifr; |
| 2857 | |
| 2858 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 2859 | if (sock == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2860 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2861 | errno, "can't open socket"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2862 | return (PCAP_ERROR); |
| 2863 | } |
| 2864 | |
| 2865 | memset(&req, 0, sizeof req); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2866 | strncpy(req.ifm_name, p->opt.device, sizeof req.ifm_name); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2867 | |
| 2868 | /* |
| 2869 | * Find out how many media types we have. |
| 2870 | */ |
| 2871 | if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { |
| 2872 | /* |
| 2873 | * Can't get the media types. |
| 2874 | */ |
| 2875 | switch (errno) { |
| 2876 | |
| 2877 | case ENXIO: |
| 2878 | /* |
| 2879 | * There's no such device. |
| 2880 | */ |
| 2881 | close(sock); |
| 2882 | return (PCAP_ERROR_NO_SUCH_DEVICE); |
| 2883 | |
| 2884 | case EINVAL: |
| 2885 | /* |
| 2886 | * Interface doesn't support SIOC{G,S}IFMEDIA. |
| 2887 | */ |
| 2888 | close(sock); |
| 2889 | return (PCAP_ERROR_RFMON_NOTSUP); |
| 2890 | |
| 2891 | default: |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2892 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2893 | errno, "SIOCGIFMEDIA 1"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2894 | close(sock); |
| 2895 | return (PCAP_ERROR); |
| 2896 | } |
| 2897 | } |
| 2898 | if (req.ifm_count == 0) { |
| 2899 | /* |
| 2900 | * No media types. |
| 2901 | */ |
| 2902 | close(sock); |
| 2903 | return (PCAP_ERROR_RFMON_NOTSUP); |
| 2904 | } |
| 2905 | |
| 2906 | /* |
| 2907 | * Allocate a buffer to hold all the media types, and |
| 2908 | * get the media types. |
| 2909 | */ |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2910 | media_list = malloc(req.ifm_count * sizeof(*media_list)); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2911 | if (media_list == NULL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2912 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2913 | errno, "malloc"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2914 | close(sock); |
| 2915 | return (PCAP_ERROR); |
| 2916 | } |
| 2917 | req.ifm_ulist = media_list; |
| 2918 | if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2919 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 2920 | errno, "SIOCGIFMEDIA"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2921 | free(media_list); |
| 2922 | close(sock); |
| 2923 | return (PCAP_ERROR); |
| 2924 | } |
| 2925 | |
| 2926 | /* |
| 2927 | * Look for an 802.11 "automatic" media type. |
| 2928 | * We assume that all 802.11 adapters have that media type, |
| 2929 | * and that it will carry the monitor mode supported flag. |
| 2930 | */ |
| 2931 | can_do = 0; |
| 2932 | for (i = 0; i < req.ifm_count; i++) { |
| 2933 | if (IFM_TYPE(media_list[i]) == IFM_IEEE80211 |
| 2934 | && IFM_SUBTYPE(media_list[i]) == IFM_AUTO) { |
| 2935 | /* OK, does it do monitor mode? */ |
| 2936 | if (media_list[i] & IFM_IEEE80211_MONITOR) { |
| 2937 | can_do = 1; |
| 2938 | break; |
| 2939 | } |
| 2940 | } |
| 2941 | } |
| 2942 | free(media_list); |
| 2943 | if (!can_do) { |
| 2944 | /* |
| 2945 | * This adapter doesn't support monitor mode. |
| 2946 | */ |
| 2947 | close(sock); |
| 2948 | return (PCAP_ERROR_RFMON_NOTSUP); |
| 2949 | } |
| 2950 | |
| 2951 | if (set) { |
| 2952 | /* |
| 2953 | * Don't just check whether we can enable monitor mode, |
| 2954 | * do so, if it's not already enabled. |
| 2955 | */ |
| 2956 | if ((req.ifm_current & IFM_IEEE80211_MONITOR) == 0) { |
| 2957 | /* |
| 2958 | * Monitor mode isn't currently on, so turn it on, |
| 2959 | * and remember that we should turn it off when the |
| 2960 | * pcap_t is closed. |
| 2961 | */ |
| 2962 | |
| 2963 | /* |
| 2964 | * If we haven't already done so, arrange to have |
| 2965 | * "pcap_close_all()" called when we exit. |
| 2966 | */ |
| 2967 | if (!pcap_do_addexit(p)) { |
| 2968 | /* |
| 2969 | * "atexit()" failed; don't put the interface |
| 2970 | * in monitor mode, just give up. |
| 2971 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2972 | close(sock); |
| 2973 | return (PCAP_ERROR); |
| 2974 | } |
| 2975 | memset(&ifr, 0, sizeof(ifr)); |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 2976 | (void)strncpy(ifr.ifr_name, p->opt.device, |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2977 | sizeof(ifr.ifr_name)); |
| 2978 | ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR; |
| 2979 | if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 2980 | pcap_fmt_errmsg_for_errno(p->errbuf, |
| 2981 | PCAP_ERRBUF_SIZE, errno, "SIOCSIFMEDIA"); |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 2982 | close(sock); |
| 2983 | return (PCAP_ERROR); |
| 2984 | } |
| 2985 | |
| 2986 | pb->must_do_on_close |= MUST_CLEAR_RFMON; |
| 2987 | |
| 2988 | /* |
| 2989 | * Add this to the list of pcaps to close when we exit. |
| 2990 | */ |
| 2991 | pcap_add_to_pcaps_to_close(p); |
| 2992 | } |
| 2993 | } |
| 2994 | return (0); |
| 2995 | } |
| 2996 | #endif /* HAVE_BSD_IEEE80211 */ |
| 2997 | |
| 2998 | #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) |
| 2999 | /* |
| 3000 | * Check whether we have any 802.11 link-layer types; return the best |
| 3001 | * of the 802.11 link-layer types if we find one, and return -1 |
| 3002 | * otherwise. |
| 3003 | * |
| 3004 | * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the |
| 3005 | * best 802.11 link-layer type; any of the other 802.11-plus-radio |
| 3006 | * headers are second-best; 802.11 with no radio information is |
| 3007 | * the least good. |
| 3008 | */ |
| 3009 | static int |
| 3010 | find_802_11(struct bpf_dltlist *bdlp) |
| 3011 | { |
| 3012 | int new_dlt; |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3013 | u_int i; |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3014 | |
| 3015 | /* |
| 3016 | * Scan the list of DLT_ values, looking for 802.11 values, |
| 3017 | * and, if we find any, choose the best of them. |
| 3018 | */ |
| 3019 | new_dlt = -1; |
| 3020 | for (i = 0; i < bdlp->bfl_len; i++) { |
| 3021 | switch (bdlp->bfl_list[i]) { |
| 3022 | |
| 3023 | case DLT_IEEE802_11: |
| 3024 | /* |
| 3025 | * 802.11, but no radio. |
| 3026 | * |
| 3027 | * Offer this, and select it as the new mode |
| 3028 | * unless we've already found an 802.11 |
| 3029 | * header with radio information. |
| 3030 | */ |
| 3031 | if (new_dlt == -1) |
| 3032 | new_dlt = bdlp->bfl_list[i]; |
| 3033 | break; |
| 3034 | |
| 3035 | case DLT_PRISM_HEADER: |
| 3036 | case DLT_AIRONET_HEADER: |
| 3037 | case DLT_IEEE802_11_RADIO_AVS: |
| 3038 | /* |
| 3039 | * 802.11 with radio, but not radiotap. |
| 3040 | * |
| 3041 | * Offer this, and select it as the new mode |
| 3042 | * unless we've already found the radiotap DLT_. |
| 3043 | */ |
| 3044 | if (new_dlt != DLT_IEEE802_11_RADIO) |
| 3045 | new_dlt = bdlp->bfl_list[i]; |
| 3046 | break; |
| 3047 | |
| 3048 | case DLT_IEEE802_11_RADIO: |
| 3049 | /* |
| 3050 | * 802.11 with radiotap. |
| 3051 | * |
| 3052 | * Offer this, and select it as the new mode. |
| 3053 | */ |
| 3054 | new_dlt = bdlp->bfl_list[i]; |
| 3055 | break; |
| 3056 | |
| 3057 | default: |
| 3058 | /* |
| 3059 | * Not 802.11. |
| 3060 | */ |
| 3061 | break; |
| 3062 | } |
| 3063 | } |
| 3064 | |
| 3065 | return (new_dlt); |
| 3066 | } |
| 3067 | #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */ |
| 3068 | |
| 3069 | #if defined(__APPLE__) && defined(BIOCGDLTLIST) |
| 3070 | /* |
| 3071 | * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode, |
| 3072 | * and DLT_EN10MB isn't supported in monitor mode. |
| 3073 | */ |
| 3074 | static void |
| 3075 | remove_en(pcap_t *p) |
| 3076 | { |
| 3077 | int i, j; |
| 3078 | |
| 3079 | /* |
| 3080 | * Scan the list of DLT_ values and discard DLT_EN10MB. |
| 3081 | */ |
| 3082 | j = 0; |
| 3083 | for (i = 0; i < p->dlt_count; i++) { |
| 3084 | switch (p->dlt_list[i]) { |
| 3085 | |
| 3086 | case DLT_EN10MB: |
| 3087 | /* |
| 3088 | * Don't offer this one. |
| 3089 | */ |
| 3090 | continue; |
| 3091 | |
| 3092 | default: |
| 3093 | /* |
| 3094 | * Just copy this mode over. |
| 3095 | */ |
| 3096 | break; |
| 3097 | } |
| 3098 | |
| 3099 | /* |
| 3100 | * Copy this DLT_ value to its new position. |
| 3101 | */ |
| 3102 | p->dlt_list[j] = p->dlt_list[i]; |
| 3103 | j++; |
| 3104 | } |
| 3105 | |
| 3106 | /* |
| 3107 | * Set the DLT_ count to the number of entries we copied. |
| 3108 | */ |
| 3109 | p->dlt_count = j; |
| 3110 | } |
| 3111 | |
| 3112 | /* |
| 3113 | * Remove 802.11 link-layer types from the list of DLT_ values, as |
| 3114 | * we're not in monitor mode, and those DLT_ values will switch us |
| 3115 | * to monitor mode. |
| 3116 | */ |
| 3117 | static void |
| 3118 | remove_802_11(pcap_t *p) |
| 3119 | { |
| 3120 | int i, j; |
| 3121 | |
| 3122 | /* |
| 3123 | * Scan the list of DLT_ values and discard 802.11 values. |
| 3124 | */ |
| 3125 | j = 0; |
| 3126 | for (i = 0; i < p->dlt_count; i++) { |
| 3127 | switch (p->dlt_list[i]) { |
| 3128 | |
| 3129 | case DLT_IEEE802_11: |
| 3130 | case DLT_PRISM_HEADER: |
| 3131 | case DLT_AIRONET_HEADER: |
| 3132 | case DLT_IEEE802_11_RADIO: |
| 3133 | case DLT_IEEE802_11_RADIO_AVS: |
| 3134 | /* |
| 3135 | * 802.11. Don't offer this one. |
| 3136 | */ |
| 3137 | continue; |
| 3138 | |
| 3139 | default: |
| 3140 | /* |
| 3141 | * Just copy this mode over. |
| 3142 | */ |
| 3143 | break; |
| 3144 | } |
| 3145 | |
| 3146 | /* |
| 3147 | * Copy this DLT_ value to its new position. |
| 3148 | */ |
| 3149 | p->dlt_list[j] = p->dlt_list[i]; |
| 3150 | j++; |
| 3151 | } |
| 3152 | |
| 3153 | /* |
| 3154 | * Set the DLT_ count to the number of entries we copied. |
| 3155 | */ |
| 3156 | p->dlt_count = j; |
| 3157 | } |
| 3158 | #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */ |
| 3159 | |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3160 | static int |
| 3161 | pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp) |
| 3162 | { |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3163 | struct pcap_bpf *pb = p->priv; |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3164 | |
| 3165 | /* |
| 3166 | * Free any user-mode filter we might happen to have installed. |
| 3167 | */ |
| 3168 | pcap_freecode(&p->fcode); |
| 3169 | |
| 3170 | /* |
| 3171 | * Try to install the kernel filter. |
| 3172 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3173 | if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) { |
| 3174 | /* |
| 3175 | * It worked. |
| 3176 | */ |
| 3177 | pb->filtering_in_kernel = 1; /* filtering in the kernel */ |
| 3178 | |
| 3179 | /* |
| 3180 | * Discard any previously-received packets, as they might |
| 3181 | * have passed whatever filter was formerly in effect, but |
| 3182 | * might not pass this filter (BIOCSETF discards packets |
| 3183 | * buffered in the kernel, so you can lose packets in any |
| 3184 | * case). |
| 3185 | */ |
| 3186 | p->cc = 0; |
| 3187 | return (0); |
| 3188 | } |
| 3189 | |
| 3190 | /* |
| 3191 | * We failed. |
| 3192 | * |
| 3193 | * If it failed with EINVAL, that's probably because the program |
| 3194 | * is invalid or too big. Validate it ourselves; if we like it |
| 3195 | * (we currently allow backward branches, to support protochain), |
| 3196 | * run it in userland. (There's no notion of "too big" for |
| 3197 | * userland.) |
| 3198 | * |
| 3199 | * Otherwise, just give up. |
| 3200 | * XXX - if the copy of the program into the kernel failed, |
| 3201 | * we will get EINVAL rather than, say, EFAULT on at least |
| 3202 | * some kernels. |
| 3203 | */ |
| 3204 | if (errno != EINVAL) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3205 | pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, |
| 3206 | errno, "BIOCSETF"); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3207 | return (-1); |
| 3208 | } |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3209 | |
| 3210 | /* |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3211 | * install_bpf_program() validates the program. |
| 3212 | * |
| 3213 | * XXX - what if we already have a filter in the kernel? |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3214 | */ |
JP Abgrall | 511eca3 | 2014-02-12 13:46:45 -0800 | [diff] [blame] | 3215 | if (install_bpf_program(p, fp) < 0) |
| 3216 | return (-1); |
| 3217 | pb->filtering_in_kernel = 0; /* filtering in userland */ |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3218 | return (0); |
| 3219 | } |
| 3220 | |
| 3221 | /* |
| 3222 | * Set direction flag: Which packets do we accept on a forwarding |
| 3223 | * single device? IN, OUT or both? |
| 3224 | */ |
| 3225 | static int |
| 3226 | pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d) |
| 3227 | { |
| 3228 | #if defined(BIOCSDIRECTION) |
| 3229 | u_int direction; |
| 3230 | |
| 3231 | direction = (d == PCAP_D_IN) ? BPF_D_IN : |
| 3232 | ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT); |
| 3233 | if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3234 | pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), |
| 3235 | errno, "Cannot set direction to %s", |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3236 | (d == PCAP_D_IN) ? "PCAP_D_IN" : |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3237 | ((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT")); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3238 | return (-1); |
| 3239 | } |
| 3240 | return (0); |
| 3241 | #elif defined(BIOCSSEESENT) |
| 3242 | u_int seesent; |
| 3243 | |
| 3244 | /* |
| 3245 | * We don't support PCAP_D_OUT. |
| 3246 | */ |
| 3247 | if (d == PCAP_D_OUT) { |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3248 | pcap_snprintf(p->errbuf, sizeof(p->errbuf), |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3249 | "Setting direction to PCAP_D_OUT is not supported on BPF"); |
| 3250 | return -1; |
| 3251 | } |
| 3252 | |
| 3253 | seesent = (d == PCAP_D_INOUT); |
| 3254 | if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3255 | pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), |
| 3256 | errno, "Cannot set direction to %s", |
| 3257 | (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN"); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3258 | return (-1); |
| 3259 | } |
| 3260 | return (0); |
| 3261 | #else |
Elliott Hughes | 965a4b5 | 2017-05-15 10:37:39 -0700 | [diff] [blame] | 3262 | (void) pcap_snprintf(p->errbuf, sizeof(p->errbuf), |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3263 | "This system doesn't support BIOCSSEESENT, so the direction can't be set"); |
| 3264 | return (-1); |
| 3265 | #endif |
| 3266 | } |
| 3267 | |
| 3268 | static int |
| 3269 | pcap_set_datalink_bpf(pcap_t *p, int dlt) |
| 3270 | { |
| 3271 | #ifdef BIOCSDLT |
| 3272 | if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) { |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3273 | pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), |
| 3274 | errno, "Cannot set DLT %d", dlt); |
The Android Open Source Project | 478ab6c | 2009-03-03 19:30:05 -0800 | [diff] [blame] | 3275 | return (-1); |
| 3276 | } |
| 3277 | #endif |
| 3278 | return (0); |
| 3279 | } |
Haibo Huang | 165065a | 2018-07-23 17:26:52 -0700 | [diff] [blame] | 3280 | |
| 3281 | /* |
| 3282 | * Platform-specific information. |
| 3283 | */ |
| 3284 | const char * |
| 3285 | pcap_lib_version(void) |
| 3286 | { |
| 3287 | #ifdef HAVE_ZEROCOPY_BPF |
| 3288 | return (PCAP_VERSION_STRING " (with zerocopy support)"); |
| 3289 | #else |
| 3290 | return (PCAP_VERSION_STRING); |
| 3291 | #endif |
| 3292 | } |