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