pcap-bpf.c revision 190229
1/* 2 * Copyright (c) 1993, 1994, 1995, 1996, 1998 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that: (1) source code distributions 7 * retain the above copyright notice and this paragraph in its entirety, (2) 8 * distributions including binary code include the above copyright notice and 9 * this paragraph in its entirety in the documentation or other materials 10 * provided with the distribution, and (3) all advertising materials mentioning 11 * features or use of this software display the following acknowledgement: 12 * ``This product includes software developed by the University of California, 13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 14 * the University nor the names of its contributors may be used to endorse 15 * or promote products derived from this software without specific prior 16 * written permission. 17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20 * 21 * $FreeBSD: head/contrib/libpcap/pcap-bpf.c 190229 2009-03-21 23:08:04Z rpaulo $ 22 */ 23#ifndef lint 24static const char rcsid[] _U_ = 25 "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.99.2.17 2008-09-16 18:43:02 guy Exp $ (LBL)"; 26#endif 27 28#ifdef HAVE_CONFIG_H 29#include "config.h" 30#endif 31 32#include <sys/param.h> /* optionally get BSD define */ 33#ifdef HAVE_ZEROCOPY_BPF 34#include <sys/mman.h> 35#endif 36#include <sys/time.h> 37#include <sys/timeb.h> 38#include <sys/socket.h> 39#include <sys/file.h> 40#include <sys/ioctl.h> 41#include <sys/utsname.h> 42 43#ifdef HAVE_ZEROCOPY_BPF 44#include <machine/atomic.h> 45#endif 46 47#include <net/if.h> 48 49#ifdef _AIX 50 51/* 52 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the 53 * native OS version, as we need "struct bpf_config" from it. 54 */ 55#define PCAP_DONT_INCLUDE_PCAP_BPF_H 56 57#include <sys/types.h> 58 59/* 60 * Prevent bpf.h from redefining the DLT_ values to their 61 * IFT_ values, as we're going to return the standard libpcap 62 * values, not IBM's non-standard IFT_ values. 63 */ 64#undef _AIX 65#include <net/bpf.h> 66#define _AIX 67 68#include <net/if_types.h> /* for IFT_ values */ 69#include <sys/sysconfig.h> 70#include <sys/device.h> 71#include <sys/cfgodm.h> 72#include <cf.h> 73 74#ifdef __64BIT__ 75#define domakedev makedev64 76#define getmajor major64 77#define bpf_hdr bpf_hdr32 78#else /* __64BIT__ */ 79#define domakedev makedev 80#define getmajor major 81#endif /* __64BIT__ */ 82 83#define BPF_NAME "bpf" 84#define BPF_MINORS 4 85#define DRIVER_PATH "/usr/lib/drivers" 86#define BPF_NODE "/dev/bpf" 87static int bpfloadedflag = 0; 88static int odmlockid = 0; 89 90#else /* _AIX */ 91 92#include <net/bpf.h> 93 94#endif /* _AIX */ 95 96#ifdef BIOCSETBUFMODE 97#include <machine/atomic.h> 98#endif 99 100#include <ctype.h> 101#include <errno.h> 102#include <netdb.h> 103#include <stdio.h> 104#include <stdlib.h> 105#include <string.h> 106#include <unistd.h> 107 108#ifdef HAVE_NET_IF_MEDIA_H 109# include <net/if_media.h> 110#endif 111 112#include "pcap-int.h" 113 114#ifdef HAVE_DAG_API 115#include "pcap-dag.h" 116#endif /* HAVE_DAG_API */ 117 118#ifdef HAVE_OS_PROTO_H 119#include "os-proto.h" 120#endif 121 122#ifdef BIOCGDLTLIST 123# if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__) 124#define HAVE_BSD_IEEE80211 125# endif 126 127# if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) 128static int find_802_11(struct bpf_dltlist *); 129 130# ifdef HAVE_BSD_IEEE80211 131static int monitor_mode(pcap_t *, int); 132# endif 133 134# if defined(__APPLE__) 135static void remove_en(pcap_t *); 136static void remove_802_11(pcap_t *); 137# endif 138 139# endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */ 140 141#endif /* BIOCGDLTLIST */ 142 143/* 144 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably 145 * don't get DLT_DOCSIS defined. 146 */ 147#ifndef DLT_DOCSIS 148#define DLT_DOCSIS 143 149#endif 150 151/* 152 * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s 153 * defined, even though some of them are used by various Airport drivers. 154 */ 155#ifndef DLT_PRISM_HEADER 156#define DLT_PRISM_HEADER 119 157#endif 158#ifndef DLT_AIRONET_HEADER 159#define DLT_AIRONET_HEADER 120 160#endif 161#ifndef DLT_IEEE802_11_RADIO 162#define DLT_IEEE802_11_RADIO 127 163#endif 164#ifndef DLT_IEEE802_11_RADIO_AVS 165#define DLT_IEEE802_11_RADIO_AVS 163 166#endif 167 168static int pcap_can_set_rfmon_bpf(pcap_t *p); 169static int pcap_activate_bpf(pcap_t *p); 170static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp); 171static int pcap_setdirection_bpf(pcap_t *, pcap_direction_t); 172static int pcap_set_datalink_bpf(pcap_t *p, int dlt); 173 174#ifdef HAVE_ZEROCOPY_BPF 175/* 176 * For zerocopy bpf, we need to override the setnonblock/getnonblock routines 177 * so we don't call select(2) if the pcap handle is in non-blocking mode. We 178 * preserve the timeout supplied by pcap_open functions to make sure it 179 * does not get clobbered if the pcap handle moves between blocking and non- 180 * blocking mode. 181 */ 182static int 183pcap_getnonblock_zbuf(pcap_t *p, char *errbuf) 184{ 185 /* 186 * Use a negative value for the timeout to represent that the 187 * pcap handle is in non-blocking mode. 188 */ 189 return (p->md.timeout < 0); 190} 191 192static int 193pcap_setnonblock_zbuf(pcap_t *p, int nonblock, char *errbuf) 194{ 195 /* 196 * Map each value to the corresponding 2's complement, to 197 * preserve the timeout value provided with pcap_set_timeout. 198 * (from pcap-linux.c). 199 */ 200 if (nonblock) { 201 if (p->md.timeout > 0) 202 p->md.timeout = p->md.timeout * -1 - 1; 203 } else 204 if (p->md.timeout < 0) 205 p->md.timeout = (p->md.timeout + 1) * -1; 206 return (0); 207} 208 209/* 210 * Zero-copy specific close method. Un-map the shared buffers then call 211 * pcap_cleanup_live_common. 212 */ 213static void 214pcap_cleanup_zbuf(pcap_t *p) 215{ 216 /* 217 * Delete the mappings. Note that p->buffer gets initialized to one 218 * of the mmapped regions in this case, so do not try and free it 219 * directly; null it out so that pcap_cleanup_live_common() doesn't 220 * try to free it. 221 */ 222 if (p->md.zbuf1 != MAP_FAILED && p->md.zbuf1 != NULL) 223 (void) munmap(p->md.zbuf1, p->md.zbufsize); 224 if (p->md.zbuf2 != MAP_FAILED && p->md.zbuf2 != NULL) 225 (void) munmap(p->md.zbuf2, p->md.zbufsize); 226 p->buffer = NULL; 227 pcap_cleanup_live_common(p); 228} 229 230/* 231 * Zero-copy BPF buffer routines to check for and acknowledge BPF data in 232 * shared memory buffers. 233 * 234 * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer, 235 * and set up p->buffer and cc to reflect one if available. Notice that if 236 * there was no prior buffer, we select zbuf1 as this will be the first 237 * buffer filled for a fresh BPF session. 238 */ 239static int 240pcap_next_zbuf_shm(pcap_t *p, int *cc) 241{ 242 struct bpf_zbuf_header *bzh; 243 244 if (p->md.zbuffer == p->md.zbuf2 || p->md.zbuffer == NULL) { 245 bzh = (struct bpf_zbuf_header *)p->md.zbuf1; 246 if (bzh->bzh_user_gen != 247 atomic_load_acq_int(&bzh->bzh_kernel_gen)) { 248 p->md.bzh = bzh; 249 p->md.zbuffer = (u_char *)p->md.zbuf1; 250 p->buffer = p->md.zbuffer + sizeof(*bzh); 251 *cc = bzh->bzh_kernel_len; 252 return (1); 253 } 254 } else if (p->md.zbuffer == p->md.zbuf1) { 255 bzh = (struct bpf_zbuf_header *)p->md.zbuf2; 256 if (bzh->bzh_user_gen != 257 atomic_load_acq_int(&bzh->bzh_kernel_gen)) { 258 p->md.bzh = bzh; 259 p->md.zbuffer = (u_char *)p->md.zbuf2; 260 p->buffer = p->md.zbuffer + sizeof(*bzh); 261 *cc = bzh->bzh_kernel_len; 262 return (1); 263 } 264 } 265 *cc = 0; 266 return (0); 267} 268 269/* 270 * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using 271 * select() for data or a timeout, and possibly force rotation of the buffer 272 * in the event we time out or are in immediate mode. Invoke the shared 273 * memory check before doing system calls in order to avoid doing avoidable 274 * work. 275 */ 276static int 277pcap_next_zbuf(pcap_t *p, int *cc) 278{ 279 struct bpf_zbuf bz; 280 struct timeval tv; 281 struct timespec cur; 282 fd_set r_set; 283 int data, r; 284 int expire, tmout; 285 286#define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000)) 287 /* 288 * Start out by seeing whether anything is waiting by checking the 289 * next shared memory buffer for data. 290 */ 291 data = pcap_next_zbuf_shm(p, cc); 292 if (data) 293 return (data); 294 /* 295 * If a previous sleep was interrupted due to signal delivery, make 296 * sure that the timeout gets adjusted accordingly. This requires 297 * that we analyze when the timeout should be been expired, and 298 * subtract the current time from that. If after this operation, 299 * our timeout is less then or equal to zero, handle it like a 300 * regular timeout. 301 */ 302 tmout = p->md.timeout; 303 if (tmout) 304 (void) clock_gettime(CLOCK_MONOTONIC, &cur); 305 if (p->md.interrupted && p->md.timeout) { 306 expire = TSTOMILLI(&p->md.firstsel) + p->md.timeout; 307 tmout = expire - TSTOMILLI(&cur); 308#undef TSTOMILLI 309 if (tmout <= 0) { 310 p->md.interrupted = 0; 311 data = pcap_next_zbuf_shm(p, cc); 312 if (data) 313 return (data); 314 if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { 315 (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 316 "BIOCROTZBUF: %s", strerror(errno)); 317 return (PCAP_ERROR); 318 } 319 return (pcap_next_zbuf_shm(p, cc)); 320 } 321 } 322 /* 323 * No data in the buffer, so must use select() to wait for data or 324 * the next timeout. Note that we only call select if the handle 325 * is in blocking mode. 326 */ 327 if (p->md.timeout >= 0) { 328 FD_ZERO(&r_set); 329 FD_SET(p->fd, &r_set); 330 if (tmout != 0) { 331 tv.tv_sec = tmout / 1000; 332 tv.tv_usec = (tmout * 1000) % 1000000; 333 } 334 r = select(p->fd + 1, &r_set, NULL, NULL, 335 p->md.timeout != 0 ? &tv : NULL); 336 if (r < 0 && errno == EINTR) { 337 if (!p->md.interrupted && p->md.timeout) { 338 p->md.interrupted = 1; 339 p->md.firstsel = cur; 340 } 341 return (0); 342 } else if (r < 0) { 343 (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 344 "select: %s", strerror(errno)); 345 return (PCAP_ERROR); 346 } 347 } 348 p->md.interrupted = 0; 349 /* 350 * Check again for data, which may exist now that we've either been 351 * woken up as a result of data or timed out. Try the "there's data" 352 * case first since it doesn't require a system call. 353 */ 354 data = pcap_next_zbuf_shm(p, cc); 355 if (data) 356 return (data); 357 /* 358 * Try forcing a buffer rotation to dislodge timed out or immediate 359 * data. 360 */ 361 if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { 362 (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 363 "BIOCROTZBUF: %s", strerror(errno)); 364 return (PCAP_ERROR); 365 } 366 return (pcap_next_zbuf_shm(p, cc)); 367} 368 369/* 370 * Notify kernel that we are done with the buffer. We don't reset zbuffer so 371 * that we know which buffer to use next time around. 372 */ 373static int 374pcap_ack_zbuf(pcap_t *p) 375{ 376 377 atomic_store_rel_int(&p->md.bzh->bzh_user_gen, 378 p->md.bzh->bzh_kernel_gen); 379 p->md.bzh = NULL; 380 p->buffer = NULL; 381 return (0); 382} 383#endif 384 385pcap_t * 386pcap_create(const char *device, char *ebuf) 387{ 388 pcap_t *p; 389 390#ifdef HAVE_DAG_API 391 if (strstr(device, "dag")) 392 return (dag_create(device, ebuf)); 393#endif /* HAVE_DAG_API */ 394 395 p = pcap_create_common(device, ebuf); 396 if (p == NULL) 397 return (NULL); 398 399 p->activate_op = pcap_activate_bpf; 400 p->can_set_rfmon_op = pcap_can_set_rfmon_bpf; 401 return (p); 402} 403 404static int 405bpf_open(pcap_t *p) 406{ 407 int fd; 408#ifdef HAVE_CLONING_BPF 409 static const char device[] = "/dev/bpf"; 410#else 411 int n = 0; 412 char device[sizeof "/dev/bpf0000000000"]; 413#endif 414 415#ifdef _AIX 416 /* 417 * Load the bpf driver, if it isn't already loaded, 418 * and create the BPF device entries, if they don't 419 * already exist. 420 */ 421 if (bpf_load(p->errbuf) == PCAP_ERROR) 422 return (PCAP_ERROR); 423#endif 424 425#ifdef HAVE_CLONING_BPF 426 if ((fd = open(device, O_RDWR)) == -1 && 427 (errno != EACCES || (fd = open(device, O_RDONLY)) == -1)) { 428 if (errno == EACCES) 429 fd = PCAP_ERROR_PERM_DENIED; 430 else 431 fd = PCAP_ERROR; 432 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 433 "(cannot open device) %s: %s", device, pcap_strerror(errno)); 434 } 435#else 436 /* 437 * Go through all the minors and find one that isn't in use. 438 */ 439 do { 440 (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++); 441 /* 442 * Initially try a read/write open (to allow the inject 443 * method to work). If that fails due to permission 444 * issues, fall back to read-only. This allows a 445 * non-root user to be granted specific access to pcap 446 * capabilities via file permissions. 447 * 448 * XXX - we should have an API that has a flag that 449 * controls whether to open read-only or read-write, 450 * so that denial of permission to send (or inability 451 * to send, if sending packets isn't supported on 452 * the device in question) can be indicated at open 453 * time. 454 */ 455 fd = open(device, O_RDWR); 456 if (fd == -1 && errno == EACCES) 457 fd = open(device, O_RDONLY); 458 } while (fd < 0 && errno == EBUSY); 459 460 /* 461 * XXX better message for all minors used 462 */ 463 if (fd < 0) { 464 if (errno == EACCES) 465 fd = PCAP_ERROR_PERM_DENIED; 466 else 467 fd = PCAP_ERROR; 468 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "(no devices found) %s: %s", 469 device, pcap_strerror(errno)); 470 } 471#endif 472 473 return (fd); 474} 475 476#ifdef BIOCGDLTLIST 477static int 478get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf) 479{ 480 memset(bdlp, 0, sizeof(*bdlp)); 481 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) { 482 u_int i; 483 int is_ethernet; 484 485 bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1)); 486 if (bdlp->bfl_list == NULL) { 487 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", 488 pcap_strerror(errno)); 489 return (PCAP_ERROR); 490 } 491 492 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) { 493 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, 494 "BIOCGDLTLIST: %s", pcap_strerror(errno)); 495 free(bdlp->bfl_list); 496 return (PCAP_ERROR); 497 } 498 499 /* 500 * OK, for real Ethernet devices, add DLT_DOCSIS to the 501 * list, so that an application can let you choose it, 502 * in case you're capturing DOCSIS traffic that a Cisco 503 * Cable Modem Termination System is putting out onto 504 * an Ethernet (it doesn't put an Ethernet header onto 505 * the wire, it puts raw DOCSIS frames out on the wire 506 * inside the low-level Ethernet framing). 507 * 508 * A "real Ethernet device" is defined here as a device 509 * that has a link-layer type of DLT_EN10MB and that has 510 * no alternate link-layer types; that's done to exclude 511 * 802.11 interfaces (which might or might not be the 512 * right thing to do, but I suspect it is - Ethernet <-> 513 * 802.11 bridges would probably badly mishandle frames 514 * that don't have Ethernet headers). 515 */ 516 if (v == DLT_EN10MB) { 517 is_ethernet = 1; 518 for (i = 0; i < bdlp->bfl_len; i++) { 519 if (bdlp->bfl_list[i] != DLT_EN10MB) { 520 is_ethernet = 0; 521 break; 522 } 523 } 524 if (is_ethernet) { 525 /* 526 * We reserved one more slot at the end of 527 * the list. 528 */ 529 bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS; 530 bdlp->bfl_len++; 531 } 532 } 533 } else { 534 /* 535 * EINVAL just means "we don't support this ioctl on 536 * this device"; don't treat it as an error. 537 */ 538 if (errno != EINVAL) { 539 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, 540 "BIOCGDLTLIST: %s", pcap_strerror(errno)); 541 return (PCAP_ERROR); 542 } 543 } 544 return (0); 545} 546#endif 547 548static int 549pcap_can_set_rfmon_bpf(pcap_t *p) 550{ 551#if defined(__APPLE__) 552 struct utsname osinfo; 553 struct ifreq ifr; 554 int fd; 555#ifdef BIOCGDLTLIST 556 struct bpf_dltlist bdl; 557#endif 558 559 /* 560 * The joys of monitor mode on OS X. 561 * 562 * Prior to 10.4, it's not supported at all. 563 * 564 * In 10.4, if adapter enN supports monitor mode, there's a 565 * wltN adapter corresponding to it; you open it, instead of 566 * enN, to get monitor mode. You get whatever link-layer 567 * headers it supplies. 568 * 569 * In 10.5, and, we assume, later releases, if adapter enN 570 * supports monitor mode, it offers, among its selectable 571 * DLT_ values, values that let you get the 802.11 header; 572 * selecting one of those values puts the adapter into monitor 573 * mode (i.e., you can't get 802.11 headers except in monitor 574 * mode, and you can't get Ethernet headers in monitor mode). 575 */ 576 if (uname(&osinfo) == -1) { 577 /* 578 * Can't get the OS version; just say "no". 579 */ 580 return (0); 581 } 582 /* 583 * We assume osinfo.sysname is "Darwin", because 584 * __APPLE__ is defined. We just check the version. 585 */ 586 if (osinfo.release[0] < '8' && osinfo.release[1] == '.') { 587 /* 588 * 10.3 (Darwin 7.x) or earlier. 589 * Monitor mode not supported. 590 */ 591 return (0); 592 } 593 if (osinfo.release[0] == '8' && osinfo.release[1] == '.') { 594 /* 595 * 10.4 (Darwin 8.x). s/en/wlt/, and check 596 * whether the device exists. 597 */ 598 if (strncmp(p->opt.source, "en", 2) != 0) { 599 /* 600 * Not an enN device; no monitor mode. 601 */ 602 return (0); 603 } 604 fd = socket(AF_INET, SOCK_DGRAM, 0); 605 if (fd == -1) { 606 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 607 "socket: %s", pcap_strerror(errno)); 608 return (PCAP_ERROR); 609 } 610 strlcpy(ifr.ifr_name, "wlt", sizeof(ifr.ifr_name)); 611 strlcat(ifr.ifr_name, p->opt.source + 2, sizeof(ifr.ifr_name)); 612 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { 613 /* 614 * No such device? 615 */ 616 close(fd); 617 return (0); 618 } 619 close(fd); 620 return (1); 621 } 622 623#ifdef BIOCGDLTLIST 624 /* 625 * Everything else is 10.5 or later; for those, 626 * we just open the enN device, and check whether 627 * we have any 802.11 devices. 628 * 629 * First, open a BPF device. 630 */ 631 fd = bpf_open(p); 632 if (fd < 0) 633 return (fd); 634 635 /* 636 * Now bind to the device. 637 */ 638 (void)strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name)); 639 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { 640 if (errno == ENETDOWN) { 641 /* 642 * Return a "network down" indication, so that 643 * the application can report that rather than 644 * saying we had a mysterious failure and 645 * suggest that they report a problem to the 646 * libpcap developers. 647 */ 648 close(fd); 649 return (PCAP_ERROR_IFACE_NOT_UP); 650 } else { 651 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 652 "BIOCSETIF: %s: %s", 653 p->opt.source, pcap_strerror(errno)); 654 close(fd); 655 return (PCAP_ERROR); 656 } 657 } 658 659 /* 660 * We know the default link type -- now determine all the DLTs 661 * this interface supports. If this fails with EINVAL, it's 662 * not fatal; we just don't get to use the feature later. 663 * (We don't care about DLT_DOCSIS, so we pass DLT_NULL 664 * as the default DLT for this adapter.) 665 */ 666 if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) { 667 close(fd); 668 return (PCAP_ERROR); 669 } 670 if (find_802_11(&bdl) != -1) { 671 /* 672 * We have an 802.11 DLT, so we can set monitor mode. 673 */ 674 free(bdl.bfl_list); 675 close(fd); 676 return (1); 677 } 678 free(bdl.bfl_list); 679#endif /* BIOCGDLTLIST */ 680 return (0); 681#elif defined(HAVE_BSD_IEEE80211) 682 int ret; 683 684 ret = monitor_mode(p, 0); 685 if (ret == PCAP_ERROR_RFMON_NOTSUP) 686 return (0); /* not an error, just a "can't do" */ 687 if (ret == 0) 688 return (1); /* success */ 689 return (ret); 690#else 691 return (0); 692#endif 693} 694 695static int 696pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps) 697{ 698 struct bpf_stat s; 699 700 /* 701 * "ps_recv" counts packets handed to the filter, not packets 702 * that passed the filter. This includes packets later dropped 703 * because we ran out of buffer space. 704 * 705 * "ps_drop" counts packets dropped inside the BPF device 706 * because we ran out of buffer space. It doesn't count 707 * packets dropped by the interface driver. It counts 708 * only packets that passed the filter. 709 * 710 * Both statistics include packets not yet read from the kernel 711 * by libpcap, and thus not yet seen by the application. 712 */ 713 if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) { 714 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s", 715 pcap_strerror(errno)); 716 return (PCAP_ERROR); 717 } 718 719 ps->ps_recv = s.bs_recv; 720 ps->ps_drop = s.bs_drop; 721 return (0); 722} 723 724static int 725pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 726{ 727 int cc; 728 int n = 0; 729 register u_char *bp, *ep; 730 u_char *datap; 731#ifdef PCAP_FDDIPAD 732 register int pad; 733#endif 734#ifdef HAVE_ZEROCOPY_BPF 735 int i; 736#endif 737 738 again: 739 /* 740 * Has "pcap_breakloop()" been called? 741 */ 742 if (p->break_loop) { 743 /* 744 * Yes - clear the flag that indicates that it 745 * has, and return PCAP_ERROR_BREAK to indicate 746 * that we were told to break out of the loop. 747 */ 748 p->break_loop = 0; 749 return (PCAP_ERROR_BREAK); 750 } 751 cc = p->cc; 752 if (p->cc == 0) { 753 /* 754 * When reading without zero-copy from a file descriptor, we 755 * use a single buffer and return a length of data in the 756 * buffer. With zero-copy, we update the p->buffer pointer 757 * to point at whatever underlying buffer contains the next 758 * data and update cc to reflect the data found in the 759 * buffer. 760 */ 761#ifdef HAVE_ZEROCOPY_BPF 762 if (p->md.zerocopy) { 763 if (p->buffer != NULL) 764 pcap_ack_zbuf(p); 765 i = pcap_next_zbuf(p, &cc); 766 if (i == 0) 767 goto again; 768 if (i < 0) 769 return (PCAP_ERROR); 770 } else 771#endif 772 { 773 cc = read(p->fd, (char *)p->buffer, p->bufsize); 774 } 775 if (cc < 0) { 776 /* Don't choke when we get ptraced */ 777 switch (errno) { 778 779 case EINTR: 780 goto again; 781 782#ifdef _AIX 783 case EFAULT: 784 /* 785 * Sigh. More AIX wonderfulness. 786 * 787 * For some unknown reason the uiomove() 788 * operation in the bpf kernel extension 789 * used to copy the buffer into user 790 * space sometimes returns EFAULT. I have 791 * no idea why this is the case given that 792 * a kernel debugger shows the user buffer 793 * is correct. This problem appears to 794 * be mostly mitigated by the memset of 795 * the buffer before it is first used. 796 * Very strange.... Shaun Clowes 797 * 798 * In any case this means that we shouldn't 799 * treat EFAULT as a fatal error; as we 800 * don't have an API for returning 801 * a "some packets were dropped since 802 * the last packet you saw" indication, 803 * we just ignore EFAULT and keep reading. 804 */ 805 goto again; 806#endif 807 808 case EWOULDBLOCK: 809 return (0); 810#if defined(sun) && !defined(BSD) 811 /* 812 * Due to a SunOS bug, after 2^31 bytes, the kernel 813 * file offset overflows and read fails with EINVAL. 814 * The lseek() to 0 will fix things. 815 */ 816 case EINVAL: 817 if (lseek(p->fd, 0L, SEEK_CUR) + 818 p->bufsize < 0) { 819 (void)lseek(p->fd, 0L, SEEK_SET); 820 goto again; 821 } 822 /* fall through */ 823#endif 824 } 825 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s", 826 pcap_strerror(errno)); 827 return (PCAP_ERROR); 828 } 829 bp = p->buffer; 830 } else 831 bp = p->bp; 832 833 /* 834 * Loop through each packet. 835 */ 836#define bhp ((struct bpf_hdr *)bp) 837 ep = bp + cc; 838#ifdef PCAP_FDDIPAD 839 pad = p->fddipad; 840#endif 841 while (bp < ep) { 842 register int caplen, hdrlen; 843 844 /* 845 * Has "pcap_breakloop()" been called? 846 * If so, return immediately - if we haven't read any 847 * packets, clear the flag and return PCAP_ERROR_BREAK 848 * to indicate that we were told to break out of the loop, 849 * otherwise leave the flag set, so that the *next* call 850 * will break out of the loop without having read any 851 * packets, and return the number of packets we've 852 * processed so far. 853 */ 854 if (p->break_loop) { 855 if (n == 0) { 856 p->break_loop = 0; 857 return (PCAP_ERROR_BREAK); 858 } else { 859 p->bp = bp; 860 p->cc = ep - bp; 861 return (n); 862 } 863 } 864 865 caplen = bhp->bh_caplen; 866 hdrlen = bhp->bh_hdrlen; 867 datap = bp + hdrlen; 868 /* 869 * Short-circuit evaluation: if using BPF filter 870 * in kernel, no need to do it now - we already know 871 * the packet passed the filter. 872 * 873#ifdef PCAP_FDDIPAD 874 * Note: the filter code was generated assuming 875 * that p->fddipad was the amount of padding 876 * before the header, as that's what's required 877 * in the kernel, so we run the filter before 878 * skipping that padding. 879#endif 880 */ 881 if (p->md.use_bpf || 882 bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) { 883 struct pcap_pkthdr pkthdr; 884 885 pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec; 886#ifdef _AIX 887 /* 888 * AIX's BPF returns seconds/nanoseconds time 889 * stamps, not seconds/microseconds time stamps. 890 */ 891 pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000; 892#else 893 pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec; 894#endif 895#ifdef PCAP_FDDIPAD 896 if (caplen > pad) 897 pkthdr.caplen = caplen - pad; 898 else 899 pkthdr.caplen = 0; 900 if (bhp->bh_datalen > pad) 901 pkthdr.len = bhp->bh_datalen - pad; 902 else 903 pkthdr.len = 0; 904 datap += pad; 905#else 906 pkthdr.caplen = caplen; 907 pkthdr.len = bhp->bh_datalen; 908#endif 909 (*callback)(user, &pkthdr, datap); 910 bp += BPF_WORDALIGN(caplen + hdrlen); 911 if (++n >= cnt && cnt > 0) { 912 p->bp = bp; 913 p->cc = ep - bp; 914 return (n); 915 } 916 } else { 917 /* 918 * Skip this packet. 919 */ 920 bp += BPF_WORDALIGN(caplen + hdrlen); 921 } 922 } 923#undef bhp 924 p->cc = 0; 925 return (n); 926} 927 928static int 929pcap_inject_bpf(pcap_t *p, const void *buf, size_t size) 930{ 931 int ret; 932 933 ret = write(p->fd, buf, size); 934#ifdef __APPLE__ 935 if (ret == -1 && errno == EAFNOSUPPORT) { 936 /* 937 * In Mac OS X, there's a bug wherein setting the 938 * BIOCSHDRCMPLT flag causes writes to fail; see, 939 * for example: 940 * 941 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch 942 * 943 * So, if, on OS X, we get EAFNOSUPPORT from the write, we 944 * assume it's due to that bug, and turn off that flag 945 * and try again. If we succeed, it either means that 946 * somebody applied the fix from that URL, or other patches 947 * for that bug from 948 * 949 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/ 950 * 951 * and are running a Darwin kernel with those fixes, or 952 * that Apple fixed the problem in some OS X release. 953 */ 954 u_int spoof_eth_src = 0; 955 956 if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { 957 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 958 "send: can't turn off BIOCSHDRCMPLT: %s", 959 pcap_strerror(errno)); 960 return (PCAP_ERROR); 961 } 962 963 /* 964 * Now try the write again. 965 */ 966 ret = write(p->fd, buf, size); 967 } 968#endif /* __APPLE__ */ 969 if (ret == -1) { 970 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 971 pcap_strerror(errno)); 972 return (PCAP_ERROR); 973 } 974 return (ret); 975} 976 977#ifdef _AIX 978static int 979bpf_odminit(char *errbuf) 980{ 981 char *errstr; 982 983 if (odm_initialize() == -1) { 984 if (odm_err_msg(odmerrno, &errstr) == -1) 985 errstr = "Unknown error"; 986 snprintf(errbuf, PCAP_ERRBUF_SIZE, 987 "bpf_load: odm_initialize failed: %s", 988 errstr); 989 return (PCAP_ERROR); 990 } 991 992 if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) { 993 if (odm_err_msg(odmerrno, &errstr) == -1) 994 errstr = "Unknown error"; 995 snprintf(errbuf, PCAP_ERRBUF_SIZE, 996 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s", 997 errstr); 998 return (PCAP_ERROR); 999 } 1000 1001 return (0); 1002} 1003 1004static int 1005bpf_odmcleanup(char *errbuf) 1006{ 1007 char *errstr; 1008 1009 if (odm_unlock(odmlockid) == -1) { 1010 if (odm_err_msg(odmerrno, &errstr) == -1) 1011 errstr = "Unknown error"; 1012 snprintf(errbuf, PCAP_ERRBUF_SIZE, 1013 "bpf_load: odm_unlock failed: %s", 1014 errstr); 1015 return (PCAP_ERROR); 1016 } 1017 1018 if (odm_terminate() == -1) { 1019 if (odm_err_msg(odmerrno, &errstr) == -1) 1020 errstr = "Unknown error"; 1021 snprintf(errbuf, PCAP_ERRBUF_SIZE, 1022 "bpf_load: odm_terminate failed: %s", 1023 errstr); 1024 return (PCAP_ERROR); 1025 } 1026 1027 return (0); 1028} 1029 1030static int 1031bpf_load(char *errbuf) 1032{ 1033 long major; 1034 int *minors; 1035 int numminors, i, rc; 1036 char buf[1024]; 1037 struct stat sbuf; 1038 struct bpf_config cfg_bpf; 1039 struct cfg_load cfg_ld; 1040 struct cfg_kmod cfg_km; 1041 1042 /* 1043 * This is very very close to what happens in the real implementation 1044 * but I've fixed some (unlikely) bug situations. 1045 */ 1046 if (bpfloadedflag) 1047 return (0); 1048 1049 if (bpf_odminit(errbuf) == PCAP_ERROR) 1050 return (PCAP_ERROR); 1051 1052 major = genmajor(BPF_NAME); 1053 if (major == -1) { 1054 snprintf(errbuf, PCAP_ERRBUF_SIZE, 1055 "bpf_load: genmajor failed: %s", pcap_strerror(errno)); 1056 return (PCAP_ERROR); 1057 } 1058 1059 minors = getminor(major, &numminors, BPF_NAME); 1060 if (!minors) { 1061 minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1); 1062 if (!minors) { 1063 snprintf(errbuf, PCAP_ERRBUF_SIZE, 1064 "bpf_load: genminor failed: %s", 1065 pcap_strerror(errno)); 1066 return (PCAP_ERROR); 1067 } 1068 } 1069 1070 if (bpf_odmcleanup(errbuf) == PCAP_ERROR) 1071 return (PCAP_ERROR); 1072 1073 rc = stat(BPF_NODE "0", &sbuf); 1074 if (rc == -1 && errno != ENOENT) { 1075 snprintf(errbuf, PCAP_ERRBUF_SIZE, 1076 "bpf_load: can't stat %s: %s", 1077 BPF_NODE "0", pcap_strerror(errno)); 1078 return (PCAP_ERROR); 1079 } 1080 1081 if (rc == -1 || getmajor(sbuf.st_rdev) != major) { 1082 for (i = 0; i < BPF_MINORS; i++) { 1083 sprintf(buf, "%s%d", BPF_NODE, i); 1084 unlink(buf); 1085 if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) { 1086 snprintf(errbuf, PCAP_ERRBUF_SIZE, 1087 "bpf_load: can't mknod %s: %s", 1088 buf, pcap_strerror(errno)); 1089 return (PCAP_ERROR); 1090 } 1091 } 1092 } 1093 1094 /* Check if the driver is loaded */ 1095 memset(&cfg_ld, 0x0, sizeof(cfg_ld)); 1096 cfg_ld.path = buf; 1097 sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME); 1098 if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) || 1099 (cfg_ld.kmid == 0)) { 1100 /* Driver isn't loaded, load it now */ 1101 if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) { 1102 snprintf(errbuf, PCAP_ERRBUF_SIZE, 1103 "bpf_load: could not load driver: %s", 1104 strerror(errno)); 1105 return (PCAP_ERROR); 1106 } 1107 } 1108 1109 /* Configure the driver */ 1110 cfg_km.cmd = CFG_INIT; 1111 cfg_km.kmid = cfg_ld.kmid; 1112 cfg_km.mdilen = sizeof(cfg_bpf); 1113 cfg_km.mdiptr = (void *)&cfg_bpf; 1114 for (i = 0; i < BPF_MINORS; i++) { 1115 cfg_bpf.devno = domakedev(major, i); 1116 if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) { 1117 snprintf(errbuf, PCAP_ERRBUF_SIZE, 1118 "bpf_load: could not configure driver: %s", 1119 strerror(errno)); 1120 return (PCAP_ERROR); 1121 } 1122 } 1123 1124 bpfloadedflag = 1; 1125 1126 return (0); 1127} 1128#endif 1129 1130/* 1131 * Turn off rfmon mode if necessary. 1132 */ 1133static void 1134pcap_cleanup_bpf(pcap_t *p) 1135{ 1136#ifdef HAVE_BSD_IEEE80211 1137 int sock; 1138 struct ifmediareq req; 1139 struct ifreq ifr; 1140#endif 1141 1142 if (p->md.must_clear != 0) { 1143 /* 1144 * There's something we have to do when closing this 1145 * pcap_t. 1146 */ 1147#ifdef HAVE_BSD_IEEE80211 1148 if (p->md.must_clear & MUST_CLEAR_RFMON) { 1149 /* 1150 * We put the interface into rfmon mode; 1151 * take it out of rfmon mode. 1152 * 1153 * XXX - if somebody else wants it in rfmon 1154 * mode, this code cannot know that, so it'll take 1155 * it out of rfmon mode. 1156 */ 1157 sock = socket(AF_INET, SOCK_DGRAM, 0); 1158 if (sock == -1) { 1159 fprintf(stderr, 1160 "Can't restore interface flags (socket() failed: %s).\n" 1161 "Please adjust manually.\n", 1162 strerror(errno)); 1163 } else { 1164 memset(&req, 0, sizeof(req)); 1165 strncpy(req.ifm_name, p->md.device, 1166 sizeof(req.ifm_name)); 1167 if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { 1168 fprintf(stderr, 1169 "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n" 1170 "Please adjust manually.\n", 1171 strerror(errno)); 1172 } else { 1173 if (req.ifm_current & IFM_IEEE80211_MONITOR) { 1174 /* 1175 * Rfmon mode is currently on; 1176 * turn it off. 1177 */ 1178 memset(&ifr, 0, sizeof(ifr)); 1179 (void)strncpy(ifr.ifr_name, 1180 p->md.device, 1181 sizeof(ifr.ifr_name)); 1182 ifr.ifr_media = 1183 req.ifm_current & ~IFM_IEEE80211_MONITOR; 1184 if (ioctl(sock, SIOCSIFMEDIA, 1185 &ifr) == -1) { 1186 fprintf(stderr, 1187 "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n" 1188 "Please adjust manually.\n", 1189 strerror(errno)); 1190 } 1191 } 1192 } 1193 close(sock); 1194 } 1195 } 1196#endif /* HAVE_BSD_IEEE80211 */ 1197 1198 /* 1199 * Take this pcap out of the list of pcaps for which we 1200 * have to take the interface out of some mode. 1201 */ 1202 pcap_remove_from_pcaps_to_close(p); 1203 p->md.must_clear = 0; 1204 } 1205 1206#ifdef HAVE_ZEROCOPY_BPF 1207 /* 1208 * In zero-copy mode, p->buffer is just a pointer into one of the two 1209 * memory-mapped buffers, so no need to free it. 1210 */ 1211 if (p->md.zerocopy) { 1212 if (p->md.zbuf1 != MAP_FAILED && p->md.zbuf1 != NULL) 1213 munmap(p->md.zbuf1, p->md.zbufsize); 1214 if (p->md.zbuf2 != MAP_FAILED && p->md.zbuf2 != NULL) 1215 munmap(p->md.zbuf2, p->md.zbufsize); 1216 } 1217#endif 1218 if (p->md.device != NULL) { 1219 free(p->md.device); 1220 p->md.device = NULL; 1221 } 1222 pcap_cleanup_live_common(p); 1223} 1224 1225static int 1226check_setif_failure(pcap_t *p, int error) 1227{ 1228#ifdef __APPLE__ 1229 int fd; 1230 struct ifreq ifr; 1231 int err; 1232#endif 1233 1234 if (error == ENXIO) { 1235 /* 1236 * No such device exists. 1237 */ 1238#ifdef __APPLE__ 1239 if (p->opt.rfmon && strncmp(p->opt.source, "wlt", 3) == 0) { 1240 /* 1241 * Monitor mode was requested, and we're trying 1242 * to open a "wltN" device. Assume that this 1243 * is 10.4 and that we were asked to open an 1244 * "enN" device; if that device exists, return 1245 * "monitor mode not supported on the device". 1246 */ 1247 fd = socket(AF_INET, SOCK_DGRAM, 0); 1248 if (fd != -1) { 1249 strlcpy(ifr.ifr_name, "en", 1250 sizeof(ifr.ifr_name)); 1251 strlcat(ifr.ifr_name, p->opt.source + 3, 1252 sizeof(ifr.ifr_name)); 1253 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { 1254 /* 1255 * We assume this failed because 1256 * the underlying device doesn't 1257 * exist. 1258 */ 1259 err = PCAP_ERROR_NO_SUCH_DEVICE; 1260 strcpy(p->errbuf, ""); 1261 } else { 1262 /* 1263 * The underlying "enN" device 1264 * exists, but there's no 1265 * corresponding "wltN" device; 1266 * that means that the "enN" 1267 * device doesn't support 1268 * monitor mode, probably because 1269 * it's an Ethernet device rather 1270 * than a wireless device. 1271 */ 1272 err = PCAP_ERROR_RFMON_NOTSUP; 1273 } 1274 close(fd); 1275 } else { 1276 /* 1277 * We can't find out whether there's 1278 * an underlying "enN" device, so 1279 * just report "no such device". 1280 */ 1281 err = PCAP_ERROR_NO_SUCH_DEVICE; 1282 strcpy(p->errbuf, ""); 1283 } 1284 return (err); 1285 } 1286#endif 1287 /* 1288 * No such device. 1289 */ 1290 strcpy(p->errbuf, ""); 1291 return (PCAP_ERROR_NO_SUCH_DEVICE); 1292 } else if (errno == ENETDOWN) { 1293 /* 1294 * Return a "network down" indication, so that 1295 * the application can report that rather than 1296 * saying we had a mysterious failure and 1297 * suggest that they report a problem to the 1298 * libpcap developers. 1299 */ 1300 return (PCAP_ERROR_IFACE_NOT_UP); 1301 } else { 1302 /* 1303 * Some other error; fill in the error string, and 1304 * return PCAP_ERROR. 1305 */ 1306 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s", 1307 p->opt.source, pcap_strerror(errno)); 1308 return (PCAP_ERROR); 1309 } 1310} 1311 1312static int 1313pcap_activate_bpf(pcap_t *p) 1314{ 1315 int status = 0; 1316 int fd; 1317 struct ifreq ifr; 1318 struct bpf_version bv; 1319#ifdef __APPLE__ 1320 int sockfd; 1321 char *wltdev = NULL; 1322#endif 1323#ifdef BIOCGDLTLIST 1324 struct bpf_dltlist bdl; 1325#if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) 1326 int new_dlt; 1327#endif 1328#endif /* BIOCGDLTLIST */ 1329#if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT) 1330 u_int spoof_eth_src = 1; 1331#endif 1332 u_int v; 1333 struct bpf_insn total_insn; 1334 struct bpf_program total_prog; 1335 struct utsname osinfo; 1336 1337#ifdef HAVE_DAG_API 1338 if (strstr(device, "dag")) { 1339 return dag_open_live(device, snaplen, promisc, to_ms, ebuf); 1340 } 1341#endif /* HAVE_DAG_API */ 1342 1343#ifdef BIOCGDLTLIST 1344 memset(&bdl, 0, sizeof(bdl)); 1345 int have_osinfo = 0; 1346#ifdef HAVE_ZEROCOPY_BPF 1347 struct bpf_zbuf bz; 1348 u_int bufmode, zbufmax; 1349#endif 1350 1351 fd = bpf_open(p); 1352 if (fd < 0) { 1353 status = fd; 1354 goto bad; 1355 } 1356 1357 p->fd = fd; 1358 1359 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) { 1360 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s", 1361 pcap_strerror(errno)); 1362 status = PCAP_ERROR; 1363 goto bad; 1364 } 1365 if (bv.bv_major != BPF_MAJOR_VERSION || 1366 bv.bv_minor < BPF_MINOR_VERSION) { 1367 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1368 "kernel bpf filter out of date"); 1369 status = PCAP_ERROR; 1370 goto bad; 1371 } 1372 1373 p->md.device = strdup(p->opt.source); 1374 if (p->md.device == NULL) { 1375 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s", 1376 pcap_strerror(errno)); 1377 status = PCAP_ERROR; 1378 goto bad; 1379 } 1380 1381 /* 1382 * Try finding a good size for the buffer; 32768 may be too 1383 * big, so keep cutting it in half until we find a size 1384 * that works, or run out of sizes to try. If the default 1385 * is larger, don't make it smaller. 1386 * 1387 * XXX - there should be a user-accessible hook to set the 1388 * initial buffer size. 1389 * Attempt to find out the version of the OS on which we're running. 1390 */ 1391 if (uname(&osinfo) == 0) 1392 have_osinfo = 1; 1393 1394#ifdef __APPLE__ 1395 /* 1396 * See comment in pcap_can_set_rfmon_bpf() for an explanation 1397 * of why we check the version number. 1398 */ 1399 if (p->opt.rfmon) { 1400 if (have_osinfo) { 1401 /* 1402 * We assume osinfo.sysname is "Darwin", because 1403 * __APPLE__ is defined. We just check the version. 1404 */ 1405 if (osinfo.release[0] < '8' && 1406 osinfo.release[1] == '.') { 1407 /* 1408 * 10.3 (Darwin 7.x) or earlier. 1409 */ 1410 status = PCAP_ERROR_RFMON_NOTSUP; 1411 goto bad; 1412 } 1413 if (osinfo.release[0] == '8' && 1414 osinfo.release[1] == '.') { 1415 /* 1416 * 10.4 (Darwin 8.x). s/en/wlt/ 1417 */ 1418 if (strncmp(p->opt.source, "en", 2) != 0) { 1419 /* 1420 * Not an enN device; check 1421 * whether the device even exists. 1422 */ 1423 sockfd = socket(AF_INET, SOCK_DGRAM, 0); 1424 if (sockfd != -1) { 1425 strlcpy(ifr.ifr_name, 1426 p->opt.source, 1427 sizeof(ifr.ifr_name)); 1428 if (ioctl(sockfd, SIOCGIFFLAGS, 1429 (char *)&ifr) < 0) { 1430 /* 1431 * We assume this 1432 * failed because 1433 * the underlying 1434 * device doesn't 1435 * exist. 1436 */ 1437 status = PCAP_ERROR_NO_SUCH_DEVICE; 1438 strcpy(p->errbuf, ""); 1439 } else 1440 status = PCAP_ERROR_RFMON_NOTSUP; 1441 close(sockfd); 1442 } else { 1443 /* 1444 * We can't find out whether 1445 * the device exists, so just 1446 * report "no such device". 1447 */ 1448 status = PCAP_ERROR_NO_SUCH_DEVICE; 1449 strcpy(p->errbuf, ""); 1450 } 1451 goto bad; 1452 } 1453 wltdev = malloc(strlen(p->opt.source) + 2); 1454 if (wltdev == NULL) { 1455 (void)snprintf(p->errbuf, 1456 PCAP_ERRBUF_SIZE, "malloc: %s", 1457 pcap_strerror(errno)); 1458 status = PCAP_ERROR; 1459 goto bad; 1460 } 1461 strcpy(wltdev, "wlt"); 1462 strcat(wltdev, p->opt.source + 2); 1463 free(p->opt.source); 1464 p->opt.source = wltdev; 1465 } 1466 /* 1467 * Everything else is 10.5 or later; for those, 1468 * we just open the enN device, and set the DLT. 1469 */ 1470 } 1471 } 1472#endif /* __APPLE__ */ 1473#ifdef HAVE_ZEROCOPY_BPF 1474 /* 1475 * If the BPF extension to set buffer mode is present, try setting 1476 * the mode to zero-copy. If that fails, use regular buffering. If 1477 * it succeeds but other setup fails, return an error to the user. 1478 */ 1479 bufmode = BPF_BUFMODE_ZBUF; 1480 if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) { 1481 /* 1482 * We have zerocopy BPF; use it. 1483 */ 1484 p->md.zerocopy = 1; 1485 1486 /* 1487 * Set the cleanup and set/get nonblocking mode ops 1488 * as appropriate for zero-copy mode. 1489 */ 1490 p->cleanup_op = pcap_cleanup_zbuf; 1491 p->setnonblock_op = pcap_setnonblock_zbuf; 1492 p->getnonblock_op = pcap_getnonblock_zbuf; 1493 1494 /* 1495 * How to pick a buffer size: first, query the maximum buffer 1496 * size supported by zero-copy. This also lets us quickly 1497 * determine whether the kernel generally supports zero-copy. 1498 * Then, if a buffer size was specified, use that, otherwise 1499 * query the default buffer size, which reflects kernel 1500 * policy for a desired default. Round to the nearest page 1501 * size. 1502 */ 1503 if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) { 1504 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGETZMAX: %s", 1505 pcap_strerror(errno)); 1506 goto bad; 1507 } 1508 1509 if (p->opt.buffer_size != 0) { 1510 /* 1511 * A buffer size was explicitly specified; use it. 1512 */ 1513 v = p->opt.buffer_size; 1514 } else { 1515 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || 1516 v < 32768) 1517 v = 32768; 1518 } 1519#ifndef roundup 1520#define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */ 1521#endif 1522 p->md.zbufsize = roundup(v, getpagesize()); 1523 if (p->md.zbufsize > zbufmax) 1524 p->md.zbufsize = zbufmax; 1525 p->md.zbuf1 = mmap(NULL, p->md.zbufsize, PROT_READ | PROT_WRITE, 1526 MAP_ANON, -1, 0); 1527 p->md.zbuf2 = mmap(NULL, p->md.zbufsize, PROT_READ | PROT_WRITE, 1528 MAP_ANON, -1, 0); 1529 if (p->md.zbuf1 == MAP_FAILED || p->md.zbuf2 == MAP_FAILED) { 1530 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "mmap: %s", 1531 pcap_strerror(errno)); 1532 goto bad; 1533 } 1534 bzero(&bz, sizeof(bz)); 1535 bz.bz_bufa = p->md.zbuf1; 1536 bz.bz_bufb = p->md.zbuf2; 1537 bz.bz_buflen = p->md.zbufsize; 1538 if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) { 1539 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETZBUF: %s", 1540 pcap_strerror(errno)); 1541 goto bad; 1542 } 1543 (void)strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name)); 1544 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { 1545 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s", 1546 p->opt.source, pcap_strerror(errno)); 1547 goto bad; 1548 } 1549 v = p->md.zbufsize - sizeof(struct bpf_zbuf_header); 1550 } else 1551#endif 1552 { 1553 /* 1554 * We don't have zerocopy BPF. 1555 * Set the buffer size. 1556 */ 1557 if (p->opt.buffer_size != 0) { 1558 /* 1559 * A buffer size was explicitly specified; use it. 1560 */ 1561 if (ioctl(fd, BIOCSBLEN, 1562 (caddr_t)&p->opt.buffer_size) < 0) { 1563 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1564 "BIOCSBLEN: %s: %s", p->opt.source, 1565 pcap_strerror(errno)); 1566 status = PCAP_ERROR; 1567 goto bad; 1568 } 1569 1570 /* 1571 * Now bind to the device. 1572 */ 1573 (void)strncpy(ifr.ifr_name, p->opt.source, 1574 sizeof(ifr.ifr_name)); 1575 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { 1576 status = check_setif_failure(p, errno); 1577 goto bad; 1578 } 1579 } else { 1580 /* 1581 * No buffer size was explicitly specified. 1582 * 1583 * Try finding a good size for the buffer; 32768 may 1584 * be too big, so keep cutting it in half until we 1585 * find a size that works, or run out of sizes to try. 1586 * If the default is larger, don't make it smaller. 1587 */ 1588 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || 1589 v < 32768) 1590 v = 32768; 1591 for ( ; v != 0; v >>= 1) { 1592 /* 1593 * Ignore the return value - this is because the 1594 * call fails on BPF systems that don't have 1595 * kernel malloc. And if the call fails, it's 1596 * no big deal, we just continue to use the 1597 * standard buffer size. 1598 */ 1599 (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v); 1600 1601 (void)strncpy(ifr.ifr_name, p->opt.source, 1602 sizeof(ifr.ifr_name)); 1603 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0) 1604 break; /* that size worked; we're done */ 1605 1606 if (errno != ENOBUFS) { 1607 status = check_setif_failure(p, errno); 1608 goto bad; 1609 } 1610 } 1611 1612 if (v == 0) { 1613 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1614 "BIOCSBLEN: %s: No buffer size worked", 1615 p->opt.source); 1616 status = PCAP_ERROR; 1617 goto bad; 1618 } 1619 } 1620 } 1621#endif 1622 1623 /* Get the data link layer type. */ 1624 if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) { 1625 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s", 1626 pcap_strerror(errno)); 1627 status = PCAP_ERROR; 1628 goto bad; 1629 } 1630 1631#ifdef _AIX 1632 /* 1633 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT. 1634 */ 1635 switch (v) { 1636 1637 case IFT_ETHER: 1638 case IFT_ISO88023: 1639 v = DLT_EN10MB; 1640 break; 1641 1642 case IFT_FDDI: 1643 v = DLT_FDDI; 1644 break; 1645 1646 case IFT_ISO88025: 1647 v = DLT_IEEE802; 1648 break; 1649 1650 case IFT_LOOP: 1651 v = DLT_NULL; 1652 break; 1653 1654 default: 1655 /* 1656 * We don't know what to map this to yet. 1657 */ 1658 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u", 1659 v); 1660 status = PCAP_ERROR; 1661 goto bad; 1662 } 1663#endif 1664#if _BSDI_VERSION - 0 >= 199510 1665 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */ 1666 switch (v) { 1667 1668 case DLT_SLIP: 1669 v = DLT_SLIP_BSDOS; 1670 break; 1671 1672 case DLT_PPP: 1673 v = DLT_PPP_BSDOS; 1674 break; 1675 1676 case 11: /*DLT_FR*/ 1677 v = DLT_FRELAY; 1678 break; 1679 1680 case 12: /*DLT_C_HDLC*/ 1681 v = DLT_CHDLC; 1682 break; 1683 } 1684#endif 1685 1686#ifdef BIOCGDLTLIST 1687 /* 1688 * We know the default link type -- now determine all the DLTs 1689 * this interface supports. If this fails with EINVAL, it's 1690 * not fatal; we just don't get to use the feature later. 1691 */ 1692 if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) { 1693 status = PCAP_ERROR; 1694 goto bad; 1695 } 1696 p->dlt_count = bdl.bfl_len; 1697 p->dlt_list = bdl.bfl_list; 1698 1699#ifdef __APPLE__ 1700 /* 1701 * Monitor mode fun, continued. 1702 * 1703 * For 10.5 and, we're assuming, later releases, as noted above, 1704 * 802.1 adapters that support monitor mode offer both DLT_EN10MB, 1705 * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information 1706 * DLT_ value. Choosing one of the 802.11 DLT_ values will turn 1707 * monitor mode on. 1708 * 1709 * Therefore, if the user asked for monitor mode, we filter out 1710 * the DLT_EN10MB value, as you can't get that in monitor mode, 1711 * and, if the user didn't ask for monitor mode, we filter out 1712 * the 802.11 DLT_ values, because selecting those will turn 1713 * monitor mode on. Then, for monitor mode, if an 802.11-plus- 1714 * radio DLT_ value is offered, we try to select that, otherwise 1715 * we try to select DLT_IEEE802_11. 1716 */ 1717 if (have_osinfo) { 1718 if (isdigit((unsigned)osinfo.release[0]) && 1719 (osinfo.release[0] == '9' || 1720 isdigit((unsigned)osinfo.release[1]))) { 1721 /* 1722 * 10.5 (Darwin 9.x), or later. 1723 */ 1724 new_dlt = find_802_11(&bdl); 1725 if (new_dlt != -1) { 1726 /* 1727 * We have at least one 802.11 DLT_ value, 1728 * so this is an 802.11 interface. 1729 * new_dlt is the best of the 802.11 1730 * DLT_ values in the list. 1731 */ 1732 if (p->opt.rfmon) { 1733 /* 1734 * Our caller wants monitor mode. 1735 * Purge DLT_EN10MB from the list 1736 * of link-layer types, as selecting 1737 * it will keep monitor mode off. 1738 */ 1739 remove_en(p); 1740 1741 /* 1742 * If the new mode we want isn't 1743 * the default mode, attempt to 1744 * select the new mode. 1745 */ 1746 if (new_dlt != v) { 1747 if (ioctl(p->fd, BIOCSDLT, 1748 &new_dlt) != -1) { 1749 /* 1750 * We succeeded; 1751 * make this the 1752 * new DLT_ value. 1753 */ 1754 v = new_dlt; 1755 } 1756 } 1757 } else { 1758 /* 1759 * Our caller doesn't want 1760 * monitor mode. Unless this 1761 * is being done by pcap_open_live(), 1762 * purge the 802.11 link-layer types 1763 * from the list, as selecting 1764 * one of them will turn monitor 1765 * mode on. 1766 */ 1767 if (!p->oldstyle) 1768 remove_802_11(p); 1769 } 1770 } else { 1771 if (p->opt.rfmon) { 1772 /* 1773 * The caller requested monitor 1774 * mode, but we have no 802.11 1775 * link-layer types, so they 1776 * can't have it. 1777 */ 1778 status = PCAP_ERROR_RFMON_NOTSUP; 1779 goto bad; 1780 } 1781 } 1782 } 1783 } 1784#elif defined(HAVE_BSD_IEEE80211) 1785 /* 1786 * *BSD with the new 802.11 ioctls. 1787 * Do we want monitor mode? 1788 */ 1789 if (p->opt.rfmon) { 1790 /* 1791 * Try to put the interface into monitor mode. 1792 */ 1793 status = monitor_mode(p, 1); 1794 if (status != 0) { 1795 /* 1796 * We failed. 1797 */ 1798 goto bad; 1799 } 1800 1801 /* 1802 * We're in monitor mode. 1803 * Try to find the best 802.11 DLT_ value and, if we 1804 * succeed, try to switch to that mode if we're not 1805 * already in that mode. 1806 */ 1807 new_dlt = find_802_11(&bdl); 1808 if (new_dlt != -1) { 1809 /* 1810 * We have at least one 802.11 DLT_ value. 1811 * new_dlt is the best of the 802.11 1812 * DLT_ values in the list. 1813 * 1814 * If the new mode we want isn't the default mode, 1815 * attempt to select the new mode. 1816 */ 1817 if (new_dlt != v) { 1818 if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) { 1819 /* 1820 * We succeeded; make this the 1821 * new DLT_ value. 1822 */ 1823 v = new_dlt; 1824 } 1825 } 1826 } 1827 } 1828#endif /* various platforms */ 1829#endif /* BIOCGDLTLIST */ 1830 1831 /* 1832 * If this is an Ethernet device, and we don't have a DLT_ list, 1833 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give 1834 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to 1835 * do, but there's not much we can do about that without finding 1836 * some other way of determining whether it's an Ethernet or 802.11 1837 * device.) 1838 */ 1839 if (v == DLT_EN10MB && p->dlt_count == 0) { 1840 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 1841 /* 1842 * If that fails, just leave the list empty. 1843 */ 1844 if (p->dlt_list != NULL) { 1845 p->dlt_list[0] = DLT_EN10MB; 1846 p->dlt_list[1] = DLT_DOCSIS; 1847 p->dlt_count = 2; 1848 } 1849 } 1850#ifdef PCAP_FDDIPAD 1851 if (v == DLT_FDDI) 1852 p->fddipad = PCAP_FDDIPAD; 1853 else 1854 p->fddipad = 0; 1855#endif 1856 p->linktype = v; 1857 1858#if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT) 1859 /* 1860 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so 1861 * the link-layer source address isn't forcibly overwritten. 1862 * (Should we ignore errors? Should we do this only if 1863 * we're open for writing?) 1864 * 1865 * XXX - I seem to remember some packet-sending bug in some 1866 * BSDs - check CVS log for "bpf.c"? 1867 */ 1868 if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { 1869 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1870 "BIOCSHDRCMPLT: %s", pcap_strerror(errno)); 1871 status = PCAP_ERROR; 1872 goto bad; 1873 } 1874#endif 1875 /* set timeout */ 1876#ifdef HAVE_ZEROCOPY_BPF 1877 if (p->md.timeout != 0 && !p->md.zerocopy) { 1878#else 1879 if (p->md.timeout) { 1880#endif 1881 /* 1882 * XXX - is this seconds/nanoseconds in AIX? 1883 * (Treating it as such doesn't fix the timeout 1884 * problem described below.) 1885 */ 1886 struct timeval to; 1887 to.tv_sec = p->md.timeout / 1000; 1888 to.tv_usec = (p->md.timeout * 1000) % 1000000; 1889 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) { 1890 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT: %s", 1891 pcap_strerror(errno)); 1892 status = PCAP_ERROR; 1893 goto bad; 1894 } 1895 } 1896#ifdef BIOCSETBUFMODE 1897 p->timeout = to_ms; 1898#endif 1899 1900#ifdef _AIX 1901#ifdef BIOCIMMEDIATE 1902 /* 1903 * Darren Reed notes that 1904 * 1905 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the 1906 * timeout appears to be ignored and it waits until the buffer 1907 * is filled before returning. The result of not having it 1908 * set is almost worse than useless if your BPF filter 1909 * is reducing things to only a few packets (i.e. one every 1910 * second or so). 1911 * 1912 * so we turn BIOCIMMEDIATE mode on if this is AIX. 1913 * 1914 * We don't turn it on for other platforms, as that means we 1915 * get woken up for every packet, which may not be what we want; 1916 * in the Winter 1993 USENIX paper on BPF, they say: 1917 * 1918 * Since a process might want to look at every packet on a 1919 * network and the time between packets can be only a few 1920 * microseconds, it is not possible to do a read system call 1921 * per packet and BPF must collect the data from several 1922 * packets and return it as a unit when the monitoring 1923 * application does a read. 1924 * 1925 * which I infer is the reason for the timeout - it means we 1926 * wait that amount of time, in the hopes that more packets 1927 * will arrive and we'll get them all with one read. 1928 * 1929 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other 1930 * BSDs) causes the timeout to be ignored. 1931 * 1932 * On the other hand, some platforms (e.g., Linux) don't support 1933 * timeouts, they just hand stuff to you as soon as it arrives; 1934 * if that doesn't cause a problem on those platforms, it may 1935 * be OK to have BIOCIMMEDIATE mode on BSD as well. 1936 * 1937 * (Note, though, that applications may depend on the read 1938 * completing, even if no packets have arrived, when the timeout 1939 * expires, e.g. GUI applications that have to check for input 1940 * while waiting for packets to arrive; a non-zero timeout 1941 * prevents "select()" from working right on FreeBSD and 1942 * possibly other BSDs, as the timer doesn't start until a 1943 * "read()" is done, so the timer isn't in effect if the 1944 * application is blocked on a "select()", and the "select()" 1945 * doesn't get woken up for a BPF device until the buffer 1946 * fills up.) 1947 */ 1948 v = 1; 1949 if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) { 1950 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCIMMEDIATE: %s", 1951 pcap_strerror(errno)); 1952 status = PCAP_ERROR; 1953 goto bad; 1954 } 1955#endif /* BIOCIMMEDIATE */ 1956#endif /* _AIX */ 1957 1958 if (p->opt.promisc) { 1959 /* set promiscuous mode, just warn if it fails */ 1960 if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) { 1961 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s", 1962 pcap_strerror(errno)); 1963 status = PCAP_WARNING_PROMISC_NOTSUP; 1964 } 1965 } 1966 1967 if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) { 1968 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s", 1969 pcap_strerror(errno)); 1970 status = PCAP_ERROR; 1971 goto bad; 1972 } 1973 p->bufsize = v; 1974#ifdef HAVE_ZEROCOPY_BPF 1975 if (!p->md.zerocopy) { 1976#endif 1977 p->buffer = (u_char *)malloc(p->bufsize); 1978 if (p->buffer == NULL) { 1979 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", 1980 pcap_strerror(errno)); 1981 status = PCAP_ERROR; 1982 goto bad; 1983 } 1984#ifdef _AIX 1985 /* For some strange reason this seems to prevent the EFAULT 1986 * problems we have experienced from AIX BPF. */ 1987 memset(p->buffer, 0x0, p->bufsize); 1988#endif 1989#ifdef HAVE_ZEROCOPY_BPF 1990 } 1991#endif 1992 1993 /* 1994 * If there's no filter program installed, there's 1995 * no indication to the kernel of what the snapshot 1996 * length should be, so no snapshotting is done. 1997 * 1998 * Therefore, when we open the device, we install 1999 * an "accept everything" filter with the specified 2000 * snapshot length. 2001 */ 2002 total_insn.code = (u_short)(BPF_RET | BPF_K); 2003 total_insn.jt = 0; 2004 total_insn.jf = 0; 2005 total_insn.k = p->snapshot; 2006 2007 total_prog.bf_len = 1; 2008 total_prog.bf_insns = &total_insn; 2009 if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) { 2010 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s", 2011 pcap_strerror(errno)); 2012 status = PCAP_ERROR; 2013 goto bad; 2014 } 2015 2016 /* 2017 * On most BPF platforms, either you can do a "select()" or 2018 * "poll()" on a BPF file descriptor and it works correctly, 2019 * or you can do it and it will return "readable" if the 2020 * hold buffer is full but not if the timeout expires *and* 2021 * a non-blocking read will, if the hold buffer is empty 2022 * but the store buffer isn't empty, rotate the buffers 2023 * and return what packets are available. 2024 * 2025 * In the latter case, the fact that a non-blocking read 2026 * will give you the available packets means you can work 2027 * around the failure of "select()" and "poll()" to wake up 2028 * and return "readable" when the timeout expires by using 2029 * the timeout as the "select()" or "poll()" timeout, putting 2030 * the BPF descriptor into non-blocking mode, and read from 2031 * it regardless of whether "select()" reports it as readable 2032 * or not. 2033 * 2034 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()" 2035 * won't wake up and return "readable" if the timer expires 2036 * and non-blocking reads return EWOULDBLOCK if the hold 2037 * buffer is empty, even if the store buffer is non-empty. 2038 * 2039 * This means the workaround in question won't work. 2040 * 2041 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd" 2042 * to -1, which means "sorry, you can't use 'select()' or 'poll()' 2043 * here". On all other BPF platforms, we set it to the FD for 2044 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking 2045 * read will, if the hold buffer is empty and the store buffer 2046 * isn't empty, rotate the buffers and return what packets are 2047 * there (and in sufficiently recent versions of OpenBSD 2048 * "select()" and "poll()" should work correctly). 2049 * 2050 * XXX - what about AIX? 2051 */ 2052 p->selectable_fd = p->fd; /* assume select() works until we know otherwise */ 2053 if (have_osinfo) { 2054 /* 2055 * We can check what OS this is. 2056 */ 2057 if (strcmp(osinfo.sysname, "FreeBSD") == 0) { 2058 if (strncmp(osinfo.release, "4.3-", 4) == 0 || 2059 strncmp(osinfo.release, "4.4-", 4) == 0) 2060 p->selectable_fd = -1; 2061 } 2062 } 2063 2064 p->read_op = pcap_read_bpf; 2065 p->inject_op = pcap_inject_bpf; 2066 p->setfilter_op = pcap_setfilter_bpf; 2067 p->setdirection_op = pcap_setdirection_bpf; 2068 p->set_datalink_op = pcap_set_datalink_bpf; 2069 p->getnonblock_op = pcap_getnonblock_fd; 2070 p->setnonblock_op = pcap_setnonblock_fd; 2071 p->stats_op = pcap_stats_bpf; 2072 p->cleanup_op = pcap_cleanup_bpf; 2073 2074 return (status); 2075 bad: 2076 pcap_cleanup_bpf(p); 2077 return (status); 2078} 2079 2080int 2081pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 2082{ 2083#ifdef HAVE_DAG_API 2084 if (dag_platform_finddevs(alldevsp, errbuf) < 0) 2085 return (-1); 2086#endif /* HAVE_DAG_API */ 2087 2088 return (0); 2089} 2090 2091#ifdef HAVE_BSD_IEEE80211 2092static int 2093monitor_mode(pcap_t *p, int set) 2094{ 2095 int sock; 2096 struct ifmediareq req; 2097 int *media_list; 2098 int i; 2099 int can_do; 2100 struct ifreq ifr; 2101 2102 sock = socket(AF_INET, SOCK_DGRAM, 0); 2103 if (sock == -1) { 2104 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't open socket: %s", 2105 pcap_strerror(errno)); 2106 return (PCAP_ERROR); 2107 } 2108 2109 memset(&req, 0, sizeof req); 2110 strncpy(req.ifm_name, p->opt.source, sizeof req.ifm_name); 2111 2112 /* 2113 * Find out how many media types we have. 2114 */ 2115 if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { 2116 /* 2117 * Can't get the media types. 2118 */ 2119 if (errno == EINVAL) { 2120 /* 2121 * Interface doesn't support SIOC{G,S}IFMEDIA. 2122 */ 2123 close(sock); 2124 return (PCAP_ERROR_RFMON_NOTSUP); 2125 } 2126 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFMEDIA 1: %s", 2127 pcap_strerror(errno)); 2128 close(sock); 2129 return (PCAP_ERROR); 2130 } 2131 if (req.ifm_count == 0) { 2132 /* 2133 * No media types. 2134 */ 2135 close(sock); 2136 return (PCAP_ERROR_RFMON_NOTSUP); 2137 } 2138 2139 /* 2140 * Allocate a buffer to hold all the media types, and 2141 * get the media types. 2142 */ 2143 media_list = malloc(req.ifm_count * sizeof(int)); 2144 if (media_list == NULL) { 2145 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", 2146 pcap_strerror(errno)); 2147 close(sock); 2148 return (PCAP_ERROR); 2149 } 2150 req.ifm_ulist = media_list; 2151 if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { 2152 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFMEDIA: %s", 2153 pcap_strerror(errno)); 2154 free(media_list); 2155 close(sock); 2156 return (PCAP_ERROR); 2157 } 2158 2159 /* 2160 * Look for an 802.11 "automatic" media type. 2161 * We assume that all 802.11 adapters have that media type, 2162 * and that it will carry the monitor mode supported flag. 2163 */ 2164 can_do = 0; 2165 for (i = 0; i < req.ifm_count; i++) { 2166 if (IFM_TYPE(media_list[i]) == IFM_IEEE80211 2167 && IFM_SUBTYPE(media_list[i]) == IFM_AUTO) { 2168 /* OK, does it do monitor mode? */ 2169 if (media_list[i] & IFM_IEEE80211_MONITOR) { 2170 can_do = 1; 2171 break; 2172 } 2173 } 2174 } 2175 free(media_list); 2176 if (!can_do) { 2177 /* 2178 * This adapter doesn't support monitor mode. 2179 */ 2180 close(sock); 2181 return (PCAP_ERROR_RFMON_NOTSUP); 2182 } 2183 2184 if (set) { 2185 /* 2186 * Don't just check whether we can enable monitor mode, 2187 * do so, if it's not already enabled. 2188 */ 2189 if ((req.ifm_current & IFM_IEEE80211_MONITOR) == 0) { 2190 /* 2191 * Monitor mode isn't currently on, so turn it on, 2192 * and remember that we should turn it off when the 2193 * pcap_t is closed. 2194 */ 2195 2196 /* 2197 * If we haven't already done so, arrange to have 2198 * "pcap_close_all()" called when we exit. 2199 */ 2200 if (!pcap_do_addexit(p)) { 2201 /* 2202 * "atexit()" failed; don't put the interface 2203 * in monitor mode, just give up. 2204 */ 2205 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 2206 "atexit failed"); 2207 close(sock); 2208 return (PCAP_ERROR); 2209 } 2210 memset(&ifr, 0, sizeof(ifr)); 2211 (void)strncpy(ifr.ifr_name, p->opt.source, 2212 sizeof(ifr.ifr_name)); 2213 ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR; 2214 if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) { 2215 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 2216 "SIOCSIFMEDIA: %s", pcap_strerror(errno)); 2217 close(sock); 2218 return (PCAP_ERROR); 2219 } 2220 2221 p->md.must_clear |= MUST_CLEAR_RFMON; 2222 2223 /* 2224 * Add this to the list of pcaps to close when we exit. 2225 */ 2226 pcap_add_to_pcaps_to_close(p); 2227 } 2228 } 2229 return (0); 2230} 2231#endif /* HAVE_BSD_IEEE80211 */ 2232 2233#if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) 2234/* 2235 * Check whether we have any 802.11 link-layer types; return the best 2236 * of the 802.11 link-layer types if we find one, and return -1 2237 * otherwise. 2238 * 2239 * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the 2240 * best 802.11 link-layer type; any of the other 802.11-plus-radio 2241 * headers are second-best; 802.11 with no radio information is 2242 * the least good. 2243 */ 2244static int 2245find_802_11(struct bpf_dltlist *bdlp) 2246{ 2247 int new_dlt; 2248 int i; 2249 2250 /* 2251 * Scan the list of DLT_ values, looking for 802.11 values, 2252 * and, if we find any, choose the best of them. 2253 */ 2254 new_dlt = -1; 2255 for (i = 0; i < bdlp->bfl_len; i++) { 2256 switch (bdlp->bfl_list[i]) { 2257 2258 case DLT_IEEE802_11: 2259 /* 2260 * 802.11, but no radio. 2261 * 2262 * Offer this, and select it as the new mode 2263 * unless we've already found an 802.11 2264 * header with radio information. 2265 */ 2266 if (new_dlt == -1) 2267 new_dlt = bdlp->bfl_list[i]; 2268 break; 2269 2270 case DLT_PRISM_HEADER: 2271 case DLT_AIRONET_HEADER: 2272 case DLT_IEEE802_11_RADIO_AVS: 2273 /* 2274 * 802.11 with radio, but not radiotap. 2275 * 2276 * Offer this, and select it as the new mode 2277 * unless we've already found the radiotap DLT_. 2278 */ 2279 if (new_dlt != DLT_IEEE802_11_RADIO) 2280 new_dlt = bdlp->bfl_list[i]; 2281 break; 2282 2283 case DLT_IEEE802_11_RADIO: 2284 /* 2285 * 802.11 with radiotap. 2286 * 2287 * Offer this, and select it as the new mode. 2288 */ 2289 new_dlt = bdlp->bfl_list[i]; 2290 break; 2291 2292 default: 2293 /* 2294 * Not 802.11. 2295 */ 2296 break; 2297 } 2298 } 2299 2300 return (new_dlt); 2301} 2302#endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */ 2303 2304#if defined(__APPLE__) && defined(BIOCGDLTLIST) 2305/* 2306 * Remove DLT_EN10MB from the list of DLT_ values. 2307 */ 2308static void 2309remove_en(pcap_t *p) 2310{ 2311 int i, j; 2312 2313 /* 2314 * Scan the list of DLT_ values and discard DLT_EN10MB. 2315 */ 2316 j = 0; 2317 for (i = 0; i < p->dlt_count; i++) { 2318 switch (p->dlt_list[i]) { 2319 2320 case DLT_EN10MB: 2321 /* 2322 * Don't offer this one. 2323 */ 2324 continue; 2325 2326 default: 2327 /* 2328 * Just copy this mode over. 2329 */ 2330 break; 2331 } 2332 2333 /* 2334 * Copy this DLT_ value to its new position. 2335 */ 2336 p->dlt_list[j] = p->dlt_list[i]; 2337 j++; 2338 } 2339 2340 /* 2341 * Set the DLT_ count to the number of entries we copied. 2342 */ 2343 p->dlt_count = j; 2344} 2345 2346/* 2347 * Remove DLT_EN10MB from the list of DLT_ values, and look for the 2348 * best 802.11 link-layer type in that list and return it. 2349 * Radiotap is better than anything else; 802.11 with any other radio 2350 * header is better than 802.11 with no radio header. 2351 */ 2352static void 2353remove_802_11(pcap_t *p) 2354{ 2355 int i, j; 2356 2357 /* 2358 * Scan the list of DLT_ values and discard 802.11 values. 2359 */ 2360 j = 0; 2361 for (i = 0; i < p->dlt_count; i++) { 2362 switch (p->dlt_list[i]) { 2363 2364 case DLT_IEEE802_11: 2365 case DLT_PRISM_HEADER: 2366 case DLT_AIRONET_HEADER: 2367 case DLT_IEEE802_11_RADIO: 2368 case DLT_IEEE802_11_RADIO_AVS: 2369 /* 2370 * 802.11. Don't offer this one. 2371 */ 2372 continue; 2373 2374 default: 2375 /* 2376 * Just copy this mode over. 2377 */ 2378 break; 2379 } 2380 2381 /* 2382 * Copy this DLT_ value to its new position. 2383 */ 2384 p->dlt_list[j] = p->dlt_list[i]; 2385 j++; 2386 } 2387 2388 /* 2389 * Set the DLT_ count to the number of entries we copied. 2390 */ 2391 p->dlt_count = j; 2392} 2393#endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */ 2394 2395static int 2396pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp) 2397{ 2398 /* 2399 * Free any user-mode filter we might happen to have installed. 2400 */ 2401 pcap_freecode(&p->fcode); 2402 2403 /* 2404 * Try to install the kernel filter. 2405 */ 2406 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) { 2407 /* 2408 * It worked. 2409 */ 2410 p->md.use_bpf = 1; /* filtering in the kernel */ 2411 2412 /* 2413 * Discard any previously-received packets, as they might 2414 * have passed whatever filter was formerly in effect, but 2415 * might not pass this filter (BIOCSETF discards packets 2416 * buffered in the kernel, so you can lose packets in any 2417 * case). 2418 */ 2419 p->cc = 0; 2420 return (0); 2421 } 2422 2423 /* 2424 * We failed. 2425 * 2426 * If it failed with EINVAL, that's probably because the program 2427 * is invalid or too big. Validate it ourselves; if we like it 2428 * (we currently allow backward branches, to support protochain), 2429 * run it in userland. (There's no notion of "too big" for 2430 * userland.) 2431 * 2432 * Otherwise, just give up. 2433 * XXX - if the copy of the program into the kernel failed, 2434 * we will get EINVAL rather than, say, EFAULT on at least 2435 * some kernels. 2436 */ 2437 if (errno != EINVAL) { 2438 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s", 2439 pcap_strerror(errno)); 2440 return (-1); 2441 } 2442 2443 /* 2444 * install_bpf_program() validates the program. 2445 * 2446 * XXX - what if we already have a filter in the kernel? 2447 */ 2448 if (install_bpf_program(p, fp) < 0) 2449 return (-1); 2450 p->md.use_bpf = 0; /* filtering in userland */ 2451 return (0); 2452} 2453 2454/* 2455 * Set direction flag: Which packets do we accept on a forwarding 2456 * single device? IN, OUT or both? 2457 */ 2458static int 2459pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d) 2460{ 2461#if defined(BIOCSDIRECTION) 2462 u_int direction; 2463 2464 direction = (d == PCAP_D_IN) ? BPF_D_IN : 2465 ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT); 2466 if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) { 2467 (void) snprintf(p->errbuf, sizeof(p->errbuf), 2468 "Cannot set direction to %s: %s", 2469 (d == PCAP_D_IN) ? "PCAP_D_IN" : 2470 ((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT"), 2471 strerror(errno)); 2472 return (-1); 2473 } 2474 return (0); 2475#elif defined(BIOCSSEESENT) 2476 u_int seesent; 2477 2478 /* 2479 * We don't support PCAP_D_OUT. 2480 */ 2481 if (d == PCAP_D_OUT) { 2482 snprintf(p->errbuf, sizeof(p->errbuf), 2483 "Setting direction to PCAP_D_OUT is not supported on BPF"); 2484 return -1; 2485 } 2486 2487 seesent = (d == PCAP_D_INOUT); 2488 if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) { 2489 (void) snprintf(p->errbuf, sizeof(p->errbuf), 2490 "Cannot set direction to %s: %s", 2491 (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN", 2492 strerror(errno)); 2493 return (-1); 2494 } 2495 return (0); 2496#else 2497 (void) snprintf(p->errbuf, sizeof(p->errbuf), 2498 "This system doesn't support BIOCSSEESENT, so the direction can't be set"); 2499 return (-1); 2500#endif 2501} 2502 2503static int 2504pcap_set_datalink_bpf(pcap_t *p, int dlt) 2505{ 2506#ifdef BIOCSDLT 2507 if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) { 2508 (void) snprintf(p->errbuf, sizeof(p->errbuf), 2509 "Cannot set DLT %d: %s", dlt, strerror(errno)); 2510 return (-1); 2511 } 2512#endif 2513 return (0); 2514} 2515