117683Spst/* 239291Sfenner * Copyright (c) 1993, 1994, 1995, 1996, 1998 317683Spst * The Regents of the University of California. All rights reserved. 417683Spst * 517683Spst * Redistribution and use in source and binary forms, with or without 617683Spst * modification, are permitted provided that: (1) source code distributions 717683Spst * retain the above copyright notice and this paragraph in its entirety, (2) 817683Spst * distributions including binary code include the above copyright notice and 917683Spst * this paragraph in its entirety in the documentation or other materials 1017683Spst * provided with the distribution, and (3) all advertising materials mentioning 1117683Spst * features or use of this software display the following acknowledgement: 1217683Spst * ``This product includes software developed by the University of California, 1317683Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 1417683Spst * the University nor the names of its contributors may be used to endorse 1517683Spst * or promote products derived from this software without specific prior 1617683Spst * written permission. 1717683Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 1817683Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 1917683Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 20162020Ssam * 21162020Ssam * $FreeBSD$ 2217683Spst */ 2317683Spst#ifndef lint 24127664Sbmsstatic const char rcsid[] _U_ = 25214518Srpaulo "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.116 2008-09-16 18:42:29 guy Exp $ (LBL)"; 2617683Spst#endif 2717683Spst 2875107Sfenner#ifdef HAVE_CONFIG_H 2975107Sfenner#include "config.h" 3075107Sfenner#endif 3175107Sfenner 3217683Spst#include <sys/param.h> /* optionally get BSD define */ 33190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 34183102Scsjp#include <sys/mman.h> 35190225Srpaulo#endif 3617683Spst#include <sys/socket.h> 37235426Sdelphij#include <time.h> 38214518Srpaulo/* 39214518Srpaulo * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>. 40214518Srpaulo * 41214518Srpaulo * We include <sys/ioctl.h> as it might be necessary to declare ioctl(); 42214518Srpaulo * at least on *BSD and Mac OS X, it also defines various SIOC ioctls - 43214518Srpaulo * we could include <sys/sockio.h>, but if we're already including 44214518Srpaulo * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms, 45214518Srpaulo * there's not much point in doing so. 46214518Srpaulo * 47214518Srpaulo * If we have <sys/ioccom.h>, we include it as well, to handle systems 48214518Srpaulo * such as Solaris which don't arrange to include <sys/ioccom.h> if you 49214518Srpaulo * include <sys/ioctl.h> 50214518Srpaulo */ 5117683Spst#include <sys/ioctl.h> 52214518Srpaulo#ifdef HAVE_SYS_IOCCOM_H 53214518Srpaulo#include <sys/ioccom.h> 54214518Srpaulo#endif 55127664Sbms#include <sys/utsname.h> 5617683Spst 57190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 58190225Srpaulo#include <machine/atomic.h> 59190225Srpaulo#endif 60190225Srpaulo 6117683Spst#include <net/if.h> 62127664Sbms 6398530Sfenner#ifdef _AIX 64127664Sbms 6598530Sfenner/* 66190225Srpaulo * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the 67127664Sbms * native OS version, as we need "struct bpf_config" from it. 6898530Sfenner */ 69127664Sbms#define PCAP_DONT_INCLUDE_PCAP_BPF_H 70127664Sbms 71127664Sbms#include <sys/types.h> 72127664Sbms 73127664Sbms/* 74127664Sbms * Prevent bpf.h from redefining the DLT_ values to their 75127664Sbms * IFT_ values, as we're going to return the standard libpcap 76127664Sbms * values, not IBM's non-standard IFT_ values. 77127664Sbms */ 78127664Sbms#undef _AIX 79127664Sbms#include <net/bpf.h> 80127664Sbms#define _AIX 81127664Sbms 8298530Sfenner#include <net/if_types.h> /* for IFT_ values */ 83127664Sbms#include <sys/sysconfig.h> 84127664Sbms#include <sys/device.h> 85147894Ssam#include <sys/cfgodm.h> 86127664Sbms#include <cf.h> 8717683Spst 88127664Sbms#ifdef __64BIT__ 89127664Sbms#define domakedev makedev64 90127664Sbms#define getmajor major64 91127664Sbms#define bpf_hdr bpf_hdr32 92127664Sbms#else /* __64BIT__ */ 93127664Sbms#define domakedev makedev 94127664Sbms#define getmajor major 95127664Sbms#endif /* __64BIT__ */ 96127664Sbms 97127664Sbms#define BPF_NAME "bpf" 98127664Sbms#define BPF_MINORS 4 99127664Sbms#define DRIVER_PATH "/usr/lib/drivers" 100127664Sbms#define BPF_NODE "/dev/bpf" 101127664Sbmsstatic int bpfloadedflag = 0; 102127664Sbmsstatic int odmlockid = 0; 103127664Sbms 104214518Srpaulostatic int bpf_load(char *errbuf); 105214518Srpaulo 106127664Sbms#else /* _AIX */ 107127664Sbms 108127664Sbms#include <net/bpf.h> 109127664Sbms 110127664Sbms#endif /* _AIX */ 111127664Sbms 11217683Spst#include <ctype.h> 113214518Srpaulo#include <fcntl.h> 11417683Spst#include <errno.h> 11517683Spst#include <netdb.h> 11617683Spst#include <stdio.h> 11717683Spst#include <stdlib.h> 11817683Spst#include <string.h> 11917683Spst#include <unistd.h> 12017683Spst 121190225Srpaulo#ifdef HAVE_NET_IF_MEDIA_H 122190225Srpaulo# include <net/if_media.h> 123190225Srpaulo#endif 124190225Srpaulo 12517683Spst#include "pcap-int.h" 12617683Spst 12717683Spst#ifdef HAVE_OS_PROTO_H 12817683Spst#include "os-proto.h" 12917683Spst#endif 13017683Spst 131190225Srpaulo#ifdef BIOCGDLTLIST 132190225Srpaulo# if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__) 133190225Srpaulo#define HAVE_BSD_IEEE80211 134190225Srpaulo# endif 13556889Sfenner 136190225Srpaulo# if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) 137190225Srpaulostatic int find_802_11(struct bpf_dltlist *); 138190225Srpaulo 139190225Srpaulo# ifdef HAVE_BSD_IEEE80211 140190225Srpaulostatic int monitor_mode(pcap_t *, int); 141190225Srpaulo# endif 142190225Srpaulo 143190225Srpaulo# if defined(__APPLE__) 144190225Srpaulostatic void remove_en(pcap_t *); 145190225Srpaulostatic void remove_802_11(pcap_t *); 146190225Srpaulo# endif 147190225Srpaulo 148190225Srpaulo# endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */ 149190225Srpaulo 150190225Srpaulo#endif /* BIOCGDLTLIST */ 151190225Srpaulo 152241231Sdelphij#if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid) 153241231Sdelphij#include <zone.h> 154241231Sdelphij#endif 155241231Sdelphij 156190225Srpaulo/* 157190225Srpaulo * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably 158190225Srpaulo * don't get DLT_DOCSIS defined. 159190225Srpaulo */ 160190225Srpaulo#ifndef DLT_DOCSIS 161190225Srpaulo#define DLT_DOCSIS 143 162190225Srpaulo#endif 163190225Srpaulo 164190225Srpaulo/* 165190225Srpaulo * On OS X, we don't even get any of the 802.11-plus-radio-header DLT_'s 166190225Srpaulo * defined, even though some of them are used by various Airport drivers. 167190225Srpaulo */ 168190225Srpaulo#ifndef DLT_PRISM_HEADER 169190225Srpaulo#define DLT_PRISM_HEADER 119 170190225Srpaulo#endif 171190225Srpaulo#ifndef DLT_AIRONET_HEADER 172190225Srpaulo#define DLT_AIRONET_HEADER 120 173190225Srpaulo#endif 174190225Srpaulo#ifndef DLT_IEEE802_11_RADIO 175190225Srpaulo#define DLT_IEEE802_11_RADIO 127 176190225Srpaulo#endif 177190225Srpaulo#ifndef DLT_IEEE802_11_RADIO_AVS 178190225Srpaulo#define DLT_IEEE802_11_RADIO_AVS 163 179190225Srpaulo#endif 180190225Srpaulo 181190225Srpaulostatic int pcap_can_set_rfmon_bpf(pcap_t *p); 182190225Srpaulostatic int pcap_activate_bpf(pcap_t *p); 183127664Sbmsstatic int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp); 184162012Ssamstatic int pcap_setdirection_bpf(pcap_t *, pcap_direction_t); 185127664Sbmsstatic int pcap_set_datalink_bpf(pcap_t *p, int dlt); 186127664Sbms 187190225Srpaulo/* 188235426Sdelphij * For zerocopy bpf, the setnonblock/getnonblock routines need to modify 189235426Sdelphij * p->md.timeout so we don't call select(2) if the pcap handle is in non- 190235426Sdelphij * blocking mode. We preserve the timeout supplied by pcap_open functions 191235426Sdelphij * to make sure it does not get clobbered if the pcap handle moves between 192235426Sdelphij * blocking and non-blocking mode. 193190225Srpaulo */ 194127664Sbmsstatic int 195235426Sdelphijpcap_getnonblock_bpf(pcap_t *p, char *errbuf) 196190225Srpaulo{ 197235426Sdelphij#ifdef HAVE_ZEROCOPY_BPF 198235426Sdelphij if (p->md.zerocopy) { 199235426Sdelphij /* 200235426Sdelphij * Use a negative value for the timeout to represent that the 201235426Sdelphij * pcap handle is in non-blocking mode. 202235426Sdelphij */ 203235426Sdelphij return (p->md.timeout < 0); 204235426Sdelphij } 205235426Sdelphij#endif 206235426Sdelphij return (pcap_getnonblock_fd(p, errbuf)); 207190225Srpaulo} 208190225Srpaulo 209190225Srpaulostatic int 210235426Sdelphijpcap_setnonblock_bpf(pcap_t *p, int nonblock, char *errbuf) 211190225Srpaulo{ 212235426Sdelphij#ifdef HAVE_ZEROCOPY_BPF 213235426Sdelphij if (p->md.zerocopy) { 214235426Sdelphij /* 215251129Sdelphij * Map each value to their corresponding negation to 216235426Sdelphij * preserve the timeout value provided with pcap_set_timeout. 217235426Sdelphij * (from pcap-linux.c). 218235426Sdelphij */ 219235426Sdelphij if (nonblock) { 220235426Sdelphij if (p->md.timeout >= 0) { 221235426Sdelphij /* 222251129Sdelphij * Indicate that we're switching to 223251129Sdelphij * non-blocking mode. 224235426Sdelphij */ 225251129Sdelphij p->md.timeout = ~p->md.timeout; 226235426Sdelphij } 227235426Sdelphij } else { 228235426Sdelphij if (p->md.timeout < 0) { 229251129Sdelphij p->md.timeout = ~p->md.timeout; 230235426Sdelphij } 231214518Srpaulo } 232235426Sdelphij return (0); 233214518Srpaulo } 234235426Sdelphij#endif 235235426Sdelphij return (pcap_setnonblock_fd(p, nonblock, errbuf)); 236190225Srpaulo} 237190225Srpaulo 238235426Sdelphij#ifdef HAVE_ZEROCOPY_BPF 239190225Srpaulo/* 240190225Srpaulo * Zero-copy BPF buffer routines to check for and acknowledge BPF data in 241190225Srpaulo * shared memory buffers. 242190225Srpaulo * 243190225Srpaulo * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer, 244190225Srpaulo * and set up p->buffer and cc to reflect one if available. Notice that if 245190225Srpaulo * there was no prior buffer, we select zbuf1 as this will be the first 246190225Srpaulo * buffer filled for a fresh BPF session. 247190225Srpaulo */ 248190225Srpaulostatic int 249190225Srpaulopcap_next_zbuf_shm(pcap_t *p, int *cc) 250190225Srpaulo{ 251190225Srpaulo struct bpf_zbuf_header *bzh; 252190225Srpaulo 253190225Srpaulo if (p->md.zbuffer == p->md.zbuf2 || p->md.zbuffer == NULL) { 254190225Srpaulo bzh = (struct bpf_zbuf_header *)p->md.zbuf1; 255190225Srpaulo if (bzh->bzh_user_gen != 256190225Srpaulo atomic_load_acq_int(&bzh->bzh_kernel_gen)) { 257190225Srpaulo p->md.bzh = bzh; 258190225Srpaulo p->md.zbuffer = (u_char *)p->md.zbuf1; 259190225Srpaulo p->buffer = p->md.zbuffer + sizeof(*bzh); 260190225Srpaulo *cc = bzh->bzh_kernel_len; 261190225Srpaulo return (1); 262190225Srpaulo } 263190225Srpaulo } else if (p->md.zbuffer == p->md.zbuf1) { 264190225Srpaulo bzh = (struct bpf_zbuf_header *)p->md.zbuf2; 265190225Srpaulo if (bzh->bzh_user_gen != 266190225Srpaulo atomic_load_acq_int(&bzh->bzh_kernel_gen)) { 267190225Srpaulo p->md.bzh = bzh; 268190225Srpaulo p->md.zbuffer = (u_char *)p->md.zbuf2; 269190225Srpaulo p->buffer = p->md.zbuffer + sizeof(*bzh); 270190225Srpaulo *cc = bzh->bzh_kernel_len; 271190225Srpaulo return (1); 272190225Srpaulo } 273190225Srpaulo } 274190225Srpaulo *cc = 0; 275190225Srpaulo return (0); 276190225Srpaulo} 277190225Srpaulo 278190225Srpaulo/* 279190225Srpaulo * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using 280190225Srpaulo * select() for data or a timeout, and possibly force rotation of the buffer 281190225Srpaulo * in the event we time out or are in immediate mode. Invoke the shared 282190225Srpaulo * memory check before doing system calls in order to avoid doing avoidable 283190225Srpaulo * work. 284190225Srpaulo */ 285190225Srpaulostatic int 286190225Srpaulopcap_next_zbuf(pcap_t *p, int *cc) 287190225Srpaulo{ 288190225Srpaulo struct bpf_zbuf bz; 289190225Srpaulo struct timeval tv; 290190225Srpaulo struct timespec cur; 291190225Srpaulo fd_set r_set; 292190225Srpaulo int data, r; 293190225Srpaulo int expire, tmout; 294190225Srpaulo 295190225Srpaulo#define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000)) 296190225Srpaulo /* 297190225Srpaulo * Start out by seeing whether anything is waiting by checking the 298190225Srpaulo * next shared memory buffer for data. 299190225Srpaulo */ 300190225Srpaulo data = pcap_next_zbuf_shm(p, cc); 301190225Srpaulo if (data) 302190225Srpaulo return (data); 303190225Srpaulo /* 304190225Srpaulo * If a previous sleep was interrupted due to signal delivery, make 305190225Srpaulo * sure that the timeout gets adjusted accordingly. This requires 306190225Srpaulo * that we analyze when the timeout should be been expired, and 307190225Srpaulo * subtract the current time from that. If after this operation, 308190225Srpaulo * our timeout is less then or equal to zero, handle it like a 309190225Srpaulo * regular timeout. 310190225Srpaulo */ 311190225Srpaulo tmout = p->md.timeout; 312190225Srpaulo if (tmout) 313190225Srpaulo (void) clock_gettime(CLOCK_MONOTONIC, &cur); 314190225Srpaulo if (p->md.interrupted && p->md.timeout) { 315190225Srpaulo expire = TSTOMILLI(&p->md.firstsel) + p->md.timeout; 316190225Srpaulo tmout = expire - TSTOMILLI(&cur); 317190225Srpaulo#undef TSTOMILLI 318190225Srpaulo if (tmout <= 0) { 319190225Srpaulo p->md.interrupted = 0; 320190225Srpaulo data = pcap_next_zbuf_shm(p, cc); 321190225Srpaulo if (data) 322190225Srpaulo return (data); 323190225Srpaulo if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { 324190225Srpaulo (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 325190225Srpaulo "BIOCROTZBUF: %s", strerror(errno)); 326190225Srpaulo return (PCAP_ERROR); 327190225Srpaulo } 328190225Srpaulo return (pcap_next_zbuf_shm(p, cc)); 329190225Srpaulo } 330190225Srpaulo } 331190225Srpaulo /* 332190225Srpaulo * No data in the buffer, so must use select() to wait for data or 333190225Srpaulo * the next timeout. Note that we only call select if the handle 334190225Srpaulo * is in blocking mode. 335190225Srpaulo */ 336190225Srpaulo if (p->md.timeout >= 0) { 337190225Srpaulo FD_ZERO(&r_set); 338190225Srpaulo FD_SET(p->fd, &r_set); 339190225Srpaulo if (tmout != 0) { 340190225Srpaulo tv.tv_sec = tmout / 1000; 341190225Srpaulo tv.tv_usec = (tmout * 1000) % 1000000; 342190225Srpaulo } 343190225Srpaulo r = select(p->fd + 1, &r_set, NULL, NULL, 344190225Srpaulo p->md.timeout != 0 ? &tv : NULL); 345190225Srpaulo if (r < 0 && errno == EINTR) { 346190225Srpaulo if (!p->md.interrupted && p->md.timeout) { 347190225Srpaulo p->md.interrupted = 1; 348190225Srpaulo p->md.firstsel = cur; 349190225Srpaulo } 350190225Srpaulo return (0); 351190225Srpaulo } else if (r < 0) { 352190225Srpaulo (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 353190225Srpaulo "select: %s", strerror(errno)); 354190225Srpaulo return (PCAP_ERROR); 355190225Srpaulo } 356190225Srpaulo } 357190225Srpaulo p->md.interrupted = 0; 358190225Srpaulo /* 359190225Srpaulo * Check again for data, which may exist now that we've either been 360190225Srpaulo * woken up as a result of data or timed out. Try the "there's data" 361190225Srpaulo * case first since it doesn't require a system call. 362190225Srpaulo */ 363190225Srpaulo data = pcap_next_zbuf_shm(p, cc); 364190225Srpaulo if (data) 365190225Srpaulo return (data); 366190225Srpaulo /* 367190225Srpaulo * Try forcing a buffer rotation to dislodge timed out or immediate 368190225Srpaulo * data. 369190225Srpaulo */ 370190225Srpaulo if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) { 371190225Srpaulo (void) snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 372190225Srpaulo "BIOCROTZBUF: %s", strerror(errno)); 373190225Srpaulo return (PCAP_ERROR); 374190225Srpaulo } 375190225Srpaulo return (pcap_next_zbuf_shm(p, cc)); 376190225Srpaulo} 377190225Srpaulo 378190225Srpaulo/* 379190225Srpaulo * Notify kernel that we are done with the buffer. We don't reset zbuffer so 380190225Srpaulo * that we know which buffer to use next time around. 381190225Srpaulo */ 382190225Srpaulostatic int 383190225Srpaulopcap_ack_zbuf(pcap_t *p) 384190225Srpaulo{ 385190225Srpaulo 386190225Srpaulo atomic_store_rel_int(&p->md.bzh->bzh_user_gen, 387190225Srpaulo p->md.bzh->bzh_kernel_gen); 388190225Srpaulo p->md.bzh = NULL; 389190225Srpaulo p->buffer = NULL; 390190225Srpaulo return (0); 391190225Srpaulo} 392235426Sdelphij#endif /* HAVE_ZEROCOPY_BPF */ 393190225Srpaulo 394190225Srpaulopcap_t * 395251129Sdelphijpcap_create_interface(const char *device, char *ebuf) 396190225Srpaulo{ 397190225Srpaulo pcap_t *p; 398190225Srpaulo 399190225Srpaulo p = pcap_create_common(device, ebuf); 400190225Srpaulo if (p == NULL) 401190225Srpaulo return (NULL); 402190225Srpaulo 403190225Srpaulo p->activate_op = pcap_activate_bpf; 404190225Srpaulo p->can_set_rfmon_op = pcap_can_set_rfmon_bpf; 405190225Srpaulo return (p); 406190225Srpaulo} 407190225Srpaulo 408235426Sdelphij/* 409235426Sdelphij * On success, returns a file descriptor for a BPF device. 410235426Sdelphij * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf. 411235426Sdelphij */ 412190225Srpaulostatic int 413190225Srpaulobpf_open(pcap_t *p) 414190225Srpaulo{ 415190225Srpaulo int fd; 416190225Srpaulo#ifdef HAVE_CLONING_BPF 417190225Srpaulo static const char device[] = "/dev/bpf"; 418190225Srpaulo#else 419190225Srpaulo int n = 0; 420190225Srpaulo char device[sizeof "/dev/bpf0000000000"]; 421190225Srpaulo#endif 422190225Srpaulo 423190225Srpaulo#ifdef _AIX 424190225Srpaulo /* 425190225Srpaulo * Load the bpf driver, if it isn't already loaded, 426190225Srpaulo * and create the BPF device entries, if they don't 427190225Srpaulo * already exist. 428190225Srpaulo */ 429190225Srpaulo if (bpf_load(p->errbuf) == PCAP_ERROR) 430190225Srpaulo return (PCAP_ERROR); 431190225Srpaulo#endif 432190225Srpaulo 433190225Srpaulo#ifdef HAVE_CLONING_BPF 434190225Srpaulo if ((fd = open(device, O_RDWR)) == -1 && 435190225Srpaulo (errno != EACCES || (fd = open(device, O_RDONLY)) == -1)) { 436190225Srpaulo if (errno == EACCES) 437190225Srpaulo fd = PCAP_ERROR_PERM_DENIED; 438190225Srpaulo else 439190225Srpaulo fd = PCAP_ERROR; 440190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 441190225Srpaulo "(cannot open device) %s: %s", device, pcap_strerror(errno)); 442190225Srpaulo } 443190225Srpaulo#else 444190225Srpaulo /* 445190225Srpaulo * Go through all the minors and find one that isn't in use. 446190225Srpaulo */ 447190225Srpaulo do { 448190225Srpaulo (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++); 449190225Srpaulo /* 450190225Srpaulo * Initially try a read/write open (to allow the inject 451190225Srpaulo * method to work). If that fails due to permission 452190225Srpaulo * issues, fall back to read-only. This allows a 453190225Srpaulo * non-root user to be granted specific access to pcap 454190225Srpaulo * capabilities via file permissions. 455190225Srpaulo * 456190225Srpaulo * XXX - we should have an API that has a flag that 457190225Srpaulo * controls whether to open read-only or read-write, 458190225Srpaulo * so that denial of permission to send (or inability 459190225Srpaulo * to send, if sending packets isn't supported on 460190225Srpaulo * the device in question) can be indicated at open 461190225Srpaulo * time. 462190225Srpaulo */ 463190225Srpaulo fd = open(device, O_RDWR); 464190225Srpaulo if (fd == -1 && errno == EACCES) 465190225Srpaulo fd = open(device, O_RDONLY); 466190225Srpaulo } while (fd < 0 && errno == EBUSY); 467190225Srpaulo 468190225Srpaulo /* 469190225Srpaulo * XXX better message for all minors used 470190225Srpaulo */ 471190225Srpaulo if (fd < 0) { 472235426Sdelphij switch (errno) { 473235426Sdelphij 474235426Sdelphij case ENOENT: 475235426Sdelphij fd = PCAP_ERROR; 476235426Sdelphij if (n == 1) { 477235426Sdelphij /* 478235426Sdelphij * /dev/bpf0 doesn't exist, which 479235426Sdelphij * means we probably have no BPF 480235426Sdelphij * devices. 481235426Sdelphij */ 482235426Sdelphij snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 483235426Sdelphij "(there are no BPF devices)"); 484235426Sdelphij } else { 485235426Sdelphij /* 486235426Sdelphij * We got EBUSY on at least one 487235426Sdelphij * BPF device, so we have BPF 488235426Sdelphij * devices, but all the ones 489235426Sdelphij * that exist are busy. 490235426Sdelphij */ 491235426Sdelphij snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 492235426Sdelphij "(all BPF devices are busy)"); 493235426Sdelphij } 494235426Sdelphij break; 495235426Sdelphij 496235426Sdelphij case EACCES: 497235426Sdelphij /* 498235426Sdelphij * Got EACCES on the last device we tried, 499235426Sdelphij * and EBUSY on all devices before that, 500235426Sdelphij * if any. 501235426Sdelphij */ 502190225Srpaulo fd = PCAP_ERROR_PERM_DENIED; 503235426Sdelphij snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 504235426Sdelphij "(cannot open BPF device) %s: %s", device, 505235426Sdelphij pcap_strerror(errno)); 506235426Sdelphij break; 507235426Sdelphij 508235426Sdelphij default: 509235426Sdelphij /* 510235426Sdelphij * Some other problem. 511235426Sdelphij */ 512190225Srpaulo fd = PCAP_ERROR; 513235426Sdelphij snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 514235426Sdelphij "(cannot open BPF device) %s: %s", device, 515235426Sdelphij pcap_strerror(errno)); 516235426Sdelphij break; 517235426Sdelphij } 518190225Srpaulo } 519190225Srpaulo#endif 520190225Srpaulo 521190225Srpaulo return (fd); 522190225Srpaulo} 523190225Srpaulo 524190225Srpaulo#ifdef BIOCGDLTLIST 525190225Srpaulostatic int 526190225Srpauloget_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf) 527190225Srpaulo{ 528190225Srpaulo memset(bdlp, 0, sizeof(*bdlp)); 529190225Srpaulo if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) { 530190225Srpaulo u_int i; 531190225Srpaulo int is_ethernet; 532190225Srpaulo 533190225Srpaulo bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1)); 534190225Srpaulo if (bdlp->bfl_list == NULL) { 535190225Srpaulo (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s", 536190225Srpaulo pcap_strerror(errno)); 537190225Srpaulo return (PCAP_ERROR); 538190225Srpaulo } 539190225Srpaulo 540190225Srpaulo if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) { 541190225Srpaulo (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, 542190225Srpaulo "BIOCGDLTLIST: %s", pcap_strerror(errno)); 543190225Srpaulo free(bdlp->bfl_list); 544190225Srpaulo return (PCAP_ERROR); 545190225Srpaulo } 546190225Srpaulo 547190225Srpaulo /* 548190225Srpaulo * OK, for real Ethernet devices, add DLT_DOCSIS to the 549190225Srpaulo * list, so that an application can let you choose it, 550190225Srpaulo * in case you're capturing DOCSIS traffic that a Cisco 551190225Srpaulo * Cable Modem Termination System is putting out onto 552190225Srpaulo * an Ethernet (it doesn't put an Ethernet header onto 553190225Srpaulo * the wire, it puts raw DOCSIS frames out on the wire 554190225Srpaulo * inside the low-level Ethernet framing). 555190225Srpaulo * 556190225Srpaulo * A "real Ethernet device" is defined here as a device 557190225Srpaulo * that has a link-layer type of DLT_EN10MB and that has 558190225Srpaulo * no alternate link-layer types; that's done to exclude 559190225Srpaulo * 802.11 interfaces (which might or might not be the 560190225Srpaulo * right thing to do, but I suspect it is - Ethernet <-> 561190225Srpaulo * 802.11 bridges would probably badly mishandle frames 562190225Srpaulo * that don't have Ethernet headers). 563214518Srpaulo * 564214518Srpaulo * On Solaris with BPF, Ethernet devices also offer 565214518Srpaulo * DLT_IPNET, so we, if DLT_IPNET is defined, we don't 566214518Srpaulo * treat it as an indication that the device isn't an 567214518Srpaulo * Ethernet. 568190225Srpaulo */ 569190225Srpaulo if (v == DLT_EN10MB) { 570190225Srpaulo is_ethernet = 1; 571190225Srpaulo for (i = 0; i < bdlp->bfl_len; i++) { 572214518Srpaulo if (bdlp->bfl_list[i] != DLT_EN10MB 573214518Srpaulo#ifdef DLT_IPNET 574214518Srpaulo && bdlp->bfl_list[i] != DLT_IPNET 575214518Srpaulo#endif 576214518Srpaulo ) { 577190225Srpaulo is_ethernet = 0; 578190225Srpaulo break; 579190225Srpaulo } 580190225Srpaulo } 581190225Srpaulo if (is_ethernet) { 582190225Srpaulo /* 583190225Srpaulo * We reserved one more slot at the end of 584190225Srpaulo * the list. 585190225Srpaulo */ 586190225Srpaulo bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS; 587190225Srpaulo bdlp->bfl_len++; 588190225Srpaulo } 589190225Srpaulo } 590190225Srpaulo } else { 591190225Srpaulo /* 592190225Srpaulo * EINVAL just means "we don't support this ioctl on 593190225Srpaulo * this device"; don't treat it as an error. 594190225Srpaulo */ 595190225Srpaulo if (errno != EINVAL) { 596190225Srpaulo (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, 597190225Srpaulo "BIOCGDLTLIST: %s", pcap_strerror(errno)); 598190225Srpaulo return (PCAP_ERROR); 599190225Srpaulo } 600190225Srpaulo } 601190225Srpaulo return (0); 602190225Srpaulo} 603190225Srpaulo#endif 604190225Srpaulo 605190225Srpaulostatic int 606190225Srpaulopcap_can_set_rfmon_bpf(pcap_t *p) 607190225Srpaulo{ 608190225Srpaulo#if defined(__APPLE__) 609190225Srpaulo struct utsname osinfo; 610190225Srpaulo struct ifreq ifr; 611190225Srpaulo int fd; 612190225Srpaulo#ifdef BIOCGDLTLIST 613190225Srpaulo struct bpf_dltlist bdl; 614190225Srpaulo#endif 615190225Srpaulo 616190225Srpaulo /* 617190225Srpaulo * The joys of monitor mode on OS X. 618190225Srpaulo * 619190225Srpaulo * Prior to 10.4, it's not supported at all. 620190225Srpaulo * 621190225Srpaulo * In 10.4, if adapter enN supports monitor mode, there's a 622190225Srpaulo * wltN adapter corresponding to it; you open it, instead of 623190225Srpaulo * enN, to get monitor mode. You get whatever link-layer 624190225Srpaulo * headers it supplies. 625190225Srpaulo * 626190225Srpaulo * In 10.5, and, we assume, later releases, if adapter enN 627190225Srpaulo * supports monitor mode, it offers, among its selectable 628190225Srpaulo * DLT_ values, values that let you get the 802.11 header; 629190225Srpaulo * selecting one of those values puts the adapter into monitor 630190225Srpaulo * mode (i.e., you can't get 802.11 headers except in monitor 631190225Srpaulo * mode, and you can't get Ethernet headers in monitor mode). 632190225Srpaulo */ 633190225Srpaulo if (uname(&osinfo) == -1) { 634190225Srpaulo /* 635190225Srpaulo * Can't get the OS version; just say "no". 636190225Srpaulo */ 637190225Srpaulo return (0); 638190225Srpaulo } 639190225Srpaulo /* 640190225Srpaulo * We assume osinfo.sysname is "Darwin", because 641190225Srpaulo * __APPLE__ is defined. We just check the version. 642190225Srpaulo */ 643190225Srpaulo if (osinfo.release[0] < '8' && osinfo.release[1] == '.') { 644190225Srpaulo /* 645190225Srpaulo * 10.3 (Darwin 7.x) or earlier. 646190225Srpaulo * Monitor mode not supported. 647190225Srpaulo */ 648190225Srpaulo return (0); 649190225Srpaulo } 650190225Srpaulo if (osinfo.release[0] == '8' && osinfo.release[1] == '.') { 651190225Srpaulo /* 652190225Srpaulo * 10.4 (Darwin 8.x). s/en/wlt/, and check 653190225Srpaulo * whether the device exists. 654190225Srpaulo */ 655190225Srpaulo if (strncmp(p->opt.source, "en", 2) != 0) { 656190225Srpaulo /* 657190225Srpaulo * Not an enN device; no monitor mode. 658190225Srpaulo */ 659190225Srpaulo return (0); 660190225Srpaulo } 661190225Srpaulo fd = socket(AF_INET, SOCK_DGRAM, 0); 662190225Srpaulo if (fd == -1) { 663190225Srpaulo (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 664190225Srpaulo "socket: %s", pcap_strerror(errno)); 665190225Srpaulo return (PCAP_ERROR); 666190225Srpaulo } 667190225Srpaulo strlcpy(ifr.ifr_name, "wlt", sizeof(ifr.ifr_name)); 668190225Srpaulo strlcat(ifr.ifr_name, p->opt.source + 2, sizeof(ifr.ifr_name)); 669190225Srpaulo if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { 670190225Srpaulo /* 671190225Srpaulo * No such device? 672190225Srpaulo */ 673190225Srpaulo close(fd); 674190225Srpaulo return (0); 675190225Srpaulo } 676190225Srpaulo close(fd); 677190225Srpaulo return (1); 678190225Srpaulo } 679190225Srpaulo 680190225Srpaulo#ifdef BIOCGDLTLIST 681190225Srpaulo /* 682190225Srpaulo * Everything else is 10.5 or later; for those, 683190225Srpaulo * we just open the enN device, and check whether 684190225Srpaulo * we have any 802.11 devices. 685190225Srpaulo * 686190225Srpaulo * First, open a BPF device. 687190225Srpaulo */ 688190225Srpaulo fd = bpf_open(p); 689190225Srpaulo if (fd < 0) 690235426Sdelphij return (fd); /* fd is the appropriate error code */ 691190225Srpaulo 692190225Srpaulo /* 693190225Srpaulo * Now bind to the device. 694190225Srpaulo */ 695190225Srpaulo (void)strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name)); 696190225Srpaulo if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { 697235426Sdelphij switch (errno) { 698235426Sdelphij 699235426Sdelphij case ENXIO: 700190225Srpaulo /* 701235426Sdelphij * There's no such device. 702235426Sdelphij */ 703235426Sdelphij close(fd); 704235426Sdelphij return (PCAP_ERROR_NO_SUCH_DEVICE); 705235426Sdelphij 706235426Sdelphij case ENETDOWN: 707235426Sdelphij /* 708190225Srpaulo * Return a "network down" indication, so that 709190225Srpaulo * the application can report that rather than 710190225Srpaulo * saying we had a mysterious failure and 711190225Srpaulo * suggest that they report a problem to the 712190225Srpaulo * libpcap developers. 713190225Srpaulo */ 714190225Srpaulo close(fd); 715190225Srpaulo return (PCAP_ERROR_IFACE_NOT_UP); 716235426Sdelphij 717235426Sdelphij default: 718190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 719190225Srpaulo "BIOCSETIF: %s: %s", 720190225Srpaulo p->opt.source, pcap_strerror(errno)); 721190225Srpaulo close(fd); 722190225Srpaulo return (PCAP_ERROR); 723190225Srpaulo } 724190225Srpaulo } 725190225Srpaulo 726190225Srpaulo /* 727190225Srpaulo * We know the default link type -- now determine all the DLTs 728190225Srpaulo * this interface supports. If this fails with EINVAL, it's 729190225Srpaulo * not fatal; we just don't get to use the feature later. 730190225Srpaulo * (We don't care about DLT_DOCSIS, so we pass DLT_NULL 731190225Srpaulo * as the default DLT for this adapter.) 732190225Srpaulo */ 733190225Srpaulo if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) { 734190225Srpaulo close(fd); 735190225Srpaulo return (PCAP_ERROR); 736190225Srpaulo } 737190225Srpaulo if (find_802_11(&bdl) != -1) { 738190225Srpaulo /* 739190225Srpaulo * We have an 802.11 DLT, so we can set monitor mode. 740190225Srpaulo */ 741190225Srpaulo free(bdl.bfl_list); 742190225Srpaulo close(fd); 743190225Srpaulo return (1); 744190225Srpaulo } 745190225Srpaulo free(bdl.bfl_list); 746190225Srpaulo#endif /* BIOCGDLTLIST */ 747190225Srpaulo return (0); 748190225Srpaulo#elif defined(HAVE_BSD_IEEE80211) 749190225Srpaulo int ret; 750190225Srpaulo 751190225Srpaulo ret = monitor_mode(p, 0); 752190225Srpaulo if (ret == PCAP_ERROR_RFMON_NOTSUP) 753190225Srpaulo return (0); /* not an error, just a "can't do" */ 754190225Srpaulo if (ret == 0) 755190225Srpaulo return (1); /* success */ 756190225Srpaulo return (ret); 757190225Srpaulo#else 758190225Srpaulo return (0); 759190225Srpaulo#endif 760190225Srpaulo} 761190225Srpaulo 762190225Srpaulostatic int 763127664Sbmspcap_stats_bpf(pcap_t *p, struct pcap_stat *ps) 76417683Spst{ 76517683Spst struct bpf_stat s; 76617683Spst 76798530Sfenner /* 76898530Sfenner * "ps_recv" counts packets handed to the filter, not packets 76998530Sfenner * that passed the filter. This includes packets later dropped 77098530Sfenner * because we ran out of buffer space. 77198530Sfenner * 77298530Sfenner * "ps_drop" counts packets dropped inside the BPF device 77398530Sfenner * because we ran out of buffer space. It doesn't count 77498530Sfenner * packets dropped by the interface driver. It counts 77598530Sfenner * only packets that passed the filter. 77698530Sfenner * 77798530Sfenner * Both statistics include packets not yet read from the kernel 77898530Sfenner * by libpcap, and thus not yet seen by the application. 77998530Sfenner */ 78017683Spst if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) { 78175107Sfenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s", 78275107Sfenner pcap_strerror(errno)); 783190225Srpaulo return (PCAP_ERROR); 78417683Spst } 78517683Spst 78617683Spst ps->ps_recv = s.bs_recv; 78717683Spst ps->ps_drop = s.bs_drop; 788214518Srpaulo ps->ps_ifdrop = 0; 78917683Spst return (0); 79017683Spst} 79117683Spst 792127664Sbmsstatic int 793127664Sbmspcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 79417683Spst{ 79517683Spst int cc; 79617683Spst int n = 0; 79717683Spst register u_char *bp, *ep; 798146768Ssam u_char *datap; 799146768Ssam#ifdef PCAP_FDDIPAD 800146768Ssam register int pad; 801146768Ssam#endif 802190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 803190225Srpaulo int i; 804190225Srpaulo#endif 80517683Spst 80617683Spst again: 807127664Sbms /* 808127664Sbms * Has "pcap_breakloop()" been called? 809127664Sbms */ 810127664Sbms if (p->break_loop) { 811127664Sbms /* 812127664Sbms * Yes - clear the flag that indicates that it 813190225Srpaulo * has, and return PCAP_ERROR_BREAK to indicate 814190225Srpaulo * that we were told to break out of the loop. 815127664Sbms */ 816127664Sbms p->break_loop = 0; 817190225Srpaulo return (PCAP_ERROR_BREAK); 818127664Sbms } 81917683Spst cc = p->cc; 82017683Spst if (p->cc == 0) { 821183102Scsjp /* 822183102Scsjp * When reading without zero-copy from a file descriptor, we 823183102Scsjp * use a single buffer and return a length of data in the 824183102Scsjp * buffer. With zero-copy, we update the p->buffer pointer 825183102Scsjp * to point at whatever underlying buffer contains the next 826183102Scsjp * data and update cc to reflect the data found in the 827183102Scsjp * buffer. 828183102Scsjp */ 829190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 830190225Srpaulo if (p->md.zerocopy) { 831183102Scsjp if (p->buffer != NULL) 832183102Scsjp pcap_ack_zbuf(p); 833183102Scsjp i = pcap_next_zbuf(p, &cc); 834183102Scsjp if (i == 0) 835183102Scsjp goto again; 836183102Scsjp if (i < 0) 837190225Srpaulo return (PCAP_ERROR); 838183102Scsjp } else 839183102Scsjp#endif 840190225Srpaulo { 841183102Scsjp cc = read(p->fd, (char *)p->buffer, p->bufsize); 842190225Srpaulo } 84317683Spst if (cc < 0) { 84417683Spst /* Don't choke when we get ptraced */ 84517683Spst switch (errno) { 84617683Spst 84717683Spst case EINTR: 84817683Spst goto again; 84917683Spst 850127664Sbms#ifdef _AIX 851127664Sbms case EFAULT: 852127664Sbms /* 853127664Sbms * Sigh. More AIX wonderfulness. 854127664Sbms * 855127664Sbms * For some unknown reason the uiomove() 856127664Sbms * operation in the bpf kernel extension 857190225Srpaulo * used to copy the buffer into user 858127664Sbms * space sometimes returns EFAULT. I have 859127664Sbms * no idea why this is the case given that 860190225Srpaulo * a kernel debugger shows the user buffer 861190225Srpaulo * is correct. This problem appears to 862190225Srpaulo * be mostly mitigated by the memset of 863190225Srpaulo * the buffer before it is first used. 864127664Sbms * Very strange.... Shaun Clowes 865127664Sbms * 866190225Srpaulo * In any case this means that we shouldn't 867127664Sbms * treat EFAULT as a fatal error; as we 868127664Sbms * don't have an API for returning 869127664Sbms * a "some packets were dropped since 870127664Sbms * the last packet you saw" indication, 871127664Sbms * we just ignore EFAULT and keep reading. 872127664Sbms */ 873127664Sbms goto again; 874190225Srpaulo#endif 875190225Srpaulo 87617683Spst case EWOULDBLOCK: 87717683Spst return (0); 878214518Srpaulo 879214518Srpaulo case ENXIO: 880214518Srpaulo /* 881214518Srpaulo * The device on which we're capturing 882214518Srpaulo * went away. 883214518Srpaulo * 884214518Srpaulo * XXX - we should really return 885214518Srpaulo * PCAP_ERROR_IFACE_NOT_UP, but 886214518Srpaulo * pcap_dispatch() etc. aren't 887214518Srpaulo * defined to retur that. 888214518Srpaulo */ 889214518Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 890214518Srpaulo "The interface went down"); 891214518Srpaulo return (PCAP_ERROR); 892214518Srpaulo 893214518Srpaulo#if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4) 89417683Spst /* 89517683Spst * Due to a SunOS bug, after 2^31 bytes, the kernel 89617683Spst * file offset overflows and read fails with EINVAL. 89717683Spst * The lseek() to 0 will fix things. 89817683Spst */ 89917683Spst case EINVAL: 90017683Spst if (lseek(p->fd, 0L, SEEK_CUR) + 90117683Spst p->bufsize < 0) { 90217683Spst (void)lseek(p->fd, 0L, SEEK_SET); 90317683Spst goto again; 90417683Spst } 90517683Spst /* fall through */ 90617683Spst#endif 90717683Spst } 90875107Sfenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s", 90975107Sfenner pcap_strerror(errno)); 910190225Srpaulo return (PCAP_ERROR); 91117683Spst } 91217683Spst bp = p->buffer; 91317683Spst } else 91417683Spst bp = p->bp; 91517683Spst 91617683Spst /* 91717683Spst * Loop through each packet. 91817683Spst */ 91917683Spst#define bhp ((struct bpf_hdr *)bp) 92017683Spst ep = bp + cc; 921146768Ssam#ifdef PCAP_FDDIPAD 922146768Ssam pad = p->fddipad; 923146768Ssam#endif 92417683Spst while (bp < ep) { 92517683Spst register int caplen, hdrlen; 926127664Sbms 927127664Sbms /* 928127664Sbms * Has "pcap_breakloop()" been called? 929127664Sbms * If so, return immediately - if we haven't read any 930190225Srpaulo * packets, clear the flag and return PCAP_ERROR_BREAK 931190225Srpaulo * to indicate that we were told to break out of the loop, 932190225Srpaulo * otherwise leave the flag set, so that the *next* call 933190225Srpaulo * will break out of the loop without having read any 934190225Srpaulo * packets, and return the number of packets we've 935190225Srpaulo * processed so far. 936127664Sbms */ 937127664Sbms if (p->break_loop) { 938235426Sdelphij p->bp = bp; 939235426Sdelphij p->cc = ep - bp; 940235426Sdelphij /* 941235426Sdelphij * ep is set based on the return value of read(), 942235426Sdelphij * but read() from a BPF device doesn't necessarily 943235426Sdelphij * return a value that's a multiple of the alignment 944235426Sdelphij * value for BPF_WORDALIGN(). However, whenever we 945235426Sdelphij * increment bp, we round up the increment value by 946235426Sdelphij * a value rounded up by BPF_WORDALIGN(), so we 947235426Sdelphij * could increment bp past ep after processing the 948235426Sdelphij * last packet in the buffer. 949235426Sdelphij * 950235426Sdelphij * We treat ep < bp as an indication that this 951235426Sdelphij * happened, and just set p->cc to 0. 952235426Sdelphij */ 953235426Sdelphij if (p->cc < 0) 954235426Sdelphij p->cc = 0; 955127664Sbms if (n == 0) { 956127664Sbms p->break_loop = 0; 957190225Srpaulo return (PCAP_ERROR_BREAK); 958235426Sdelphij } else 959127664Sbms return (n); 960127664Sbms } 961127664Sbms 96217683Spst caplen = bhp->bh_caplen; 96317683Spst hdrlen = bhp->bh_hdrlen; 964146768Ssam datap = bp + hdrlen; 96517683Spst /* 966127664Sbms * Short-circuit evaluation: if using BPF filter 967190225Srpaulo * in kernel, no need to do it now - we already know 968190225Srpaulo * the packet passed the filter. 969146768Ssam * 970146768Ssam#ifdef PCAP_FDDIPAD 971146768Ssam * Note: the filter code was generated assuming 972146768Ssam * that p->fddipad was the amount of padding 973146768Ssam * before the header, as that's what's required 974146768Ssam * in the kernel, so we run the filter before 975146768Ssam * skipping that padding. 976146768Ssam#endif 97717683Spst */ 978190225Srpaulo if (p->md.use_bpf || 979190225Srpaulo bpf_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) { 980146768Ssam struct pcap_pkthdr pkthdr; 981146768Ssam 982146768Ssam pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec; 98398530Sfenner#ifdef _AIX 984127664Sbms /* 985127664Sbms * AIX's BPF returns seconds/nanoseconds time 986127664Sbms * stamps, not seconds/microseconds time stamps. 987127664Sbms */ 988146768Ssam pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000; 989146768Ssam#else 990146768Ssam pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec; 99198530Sfenner#endif 992146768Ssam#ifdef PCAP_FDDIPAD 993146768Ssam if (caplen > pad) 994146768Ssam pkthdr.caplen = caplen - pad; 995146768Ssam else 996146768Ssam pkthdr.caplen = 0; 997146768Ssam if (bhp->bh_datalen > pad) 998146768Ssam pkthdr.len = bhp->bh_datalen - pad; 999146768Ssam else 1000146768Ssam pkthdr.len = 0; 1001146768Ssam datap += pad; 1002146768Ssam#else 1003146768Ssam pkthdr.caplen = caplen; 1004146768Ssam pkthdr.len = bhp->bh_datalen; 1005146768Ssam#endif 1006146768Ssam (*callback)(user, &pkthdr, datap); 1007127664Sbms bp += BPF_WORDALIGN(caplen + hdrlen); 1008127664Sbms if (++n >= cnt && cnt > 0) { 1009127664Sbms p->bp = bp; 1010127664Sbms p->cc = ep - bp; 1011235426Sdelphij /* 1012235426Sdelphij * See comment above about p->cc < 0. 1013235426Sdelphij */ 1014235426Sdelphij if (p->cc < 0) 1015235426Sdelphij p->cc = 0; 1016127664Sbms return (n); 1017127664Sbms } 1018127664Sbms } else { 1019127664Sbms /* 1020127664Sbms * Skip this packet. 1021127664Sbms */ 1022127664Sbms bp += BPF_WORDALIGN(caplen + hdrlen); 102317683Spst } 102417683Spst } 102517683Spst#undef bhp 102617683Spst p->cc = 0; 102717683Spst return (n); 102817683Spst} 102917683Spst 1030146768Ssamstatic int 1031146768Ssampcap_inject_bpf(pcap_t *p, const void *buf, size_t size) 1032146768Ssam{ 1033146768Ssam int ret; 1034146768Ssam 1035146768Ssam ret = write(p->fd, buf, size); 1036146768Ssam#ifdef __APPLE__ 1037146768Ssam if (ret == -1 && errno == EAFNOSUPPORT) { 1038146768Ssam /* 1039146768Ssam * In Mac OS X, there's a bug wherein setting the 1040146768Ssam * BIOCSHDRCMPLT flag causes writes to fail; see, 1041146768Ssam * for example: 1042146768Ssam * 1043146768Ssam * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch 1044146768Ssam * 1045146768Ssam * So, if, on OS X, we get EAFNOSUPPORT from the write, we 1046146768Ssam * assume it's due to that bug, and turn off that flag 1047146768Ssam * and try again. If we succeed, it either means that 1048146768Ssam * somebody applied the fix from that URL, or other patches 1049146768Ssam * for that bug from 1050146768Ssam * 1051146768Ssam * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/ 1052146768Ssam * 1053146768Ssam * and are running a Darwin kernel with those fixes, or 1054146768Ssam * that Apple fixed the problem in some OS X release. 1055146768Ssam */ 1056146768Ssam u_int spoof_eth_src = 0; 1057146768Ssam 1058146768Ssam if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { 1059146768Ssam (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1060146768Ssam "send: can't turn off BIOCSHDRCMPLT: %s", 1061146768Ssam pcap_strerror(errno)); 1062190225Srpaulo return (PCAP_ERROR); 1063146768Ssam } 1064146768Ssam 1065146768Ssam /* 1066146768Ssam * Now try the write again. 1067146768Ssam */ 1068146768Ssam ret = write(p->fd, buf, size); 1069146768Ssam } 1070146768Ssam#endif /* __APPLE__ */ 1071146768Ssam if (ret == -1) { 1072146768Ssam snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 1073146768Ssam pcap_strerror(errno)); 1074190225Srpaulo return (PCAP_ERROR); 1075146768Ssam } 1076146768Ssam return (ret); 1077146768Ssam} 1078146768Ssam 1079127664Sbms#ifdef _AIX 1080190225Srpaulostatic int 1081127664Sbmsbpf_odminit(char *errbuf) 1082127664Sbms{ 1083127664Sbms char *errstr; 1084127664Sbms 1085127664Sbms if (odm_initialize() == -1) { 1086127664Sbms if (odm_err_msg(odmerrno, &errstr) == -1) 1087127664Sbms errstr = "Unknown error"; 1088127664Sbms snprintf(errbuf, PCAP_ERRBUF_SIZE, 1089127664Sbms "bpf_load: odm_initialize failed: %s", 1090127664Sbms errstr); 1091190225Srpaulo return (PCAP_ERROR); 1092127664Sbms } 1093127664Sbms 1094127664Sbms if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) { 1095127664Sbms if (odm_err_msg(odmerrno, &errstr) == -1) 1096127664Sbms errstr = "Unknown error"; 1097127664Sbms snprintf(errbuf, PCAP_ERRBUF_SIZE, 1098127664Sbms "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s", 1099127664Sbms errstr); 1100214518Srpaulo (void)odm_terminate(); 1101190225Srpaulo return (PCAP_ERROR); 1102127664Sbms } 1103127664Sbms 1104127664Sbms return (0); 1105127664Sbms} 1106127664Sbms 1107190225Srpaulostatic int 1108127664Sbmsbpf_odmcleanup(char *errbuf) 1109127664Sbms{ 1110127664Sbms char *errstr; 1111127664Sbms 1112127664Sbms if (odm_unlock(odmlockid) == -1) { 1113214518Srpaulo if (errbuf != NULL) { 1114214518Srpaulo if (odm_err_msg(odmerrno, &errstr) == -1) 1115214518Srpaulo errstr = "Unknown error"; 1116214518Srpaulo snprintf(errbuf, PCAP_ERRBUF_SIZE, 1117214518Srpaulo "bpf_load: odm_unlock failed: %s", 1118214518Srpaulo errstr); 1119214518Srpaulo } 1120190225Srpaulo return (PCAP_ERROR); 1121127664Sbms } 1122127664Sbms 1123127664Sbms if (odm_terminate() == -1) { 1124214518Srpaulo if (errbuf != NULL) { 1125214518Srpaulo if (odm_err_msg(odmerrno, &errstr) == -1) 1126214518Srpaulo errstr = "Unknown error"; 1127214518Srpaulo snprintf(errbuf, PCAP_ERRBUF_SIZE, 1128214518Srpaulo "bpf_load: odm_terminate failed: %s", 1129214518Srpaulo errstr); 1130214518Srpaulo } 1131190225Srpaulo return (PCAP_ERROR); 1132127664Sbms } 1133127664Sbms 1134127664Sbms return (0); 1135127664Sbms} 1136127664Sbms 1137127664Sbmsstatic int 1138127664Sbmsbpf_load(char *errbuf) 1139127664Sbms{ 1140127664Sbms long major; 1141127664Sbms int *minors; 1142127664Sbms int numminors, i, rc; 1143127664Sbms char buf[1024]; 1144127664Sbms struct stat sbuf; 1145127664Sbms struct bpf_config cfg_bpf; 1146127664Sbms struct cfg_load cfg_ld; 1147127664Sbms struct cfg_kmod cfg_km; 1148127664Sbms 1149127664Sbms /* 1150127664Sbms * This is very very close to what happens in the real implementation 1151127664Sbms * but I've fixed some (unlikely) bug situations. 1152127664Sbms */ 1153127664Sbms if (bpfloadedflag) 1154127664Sbms return (0); 1155127664Sbms 1156190225Srpaulo if (bpf_odminit(errbuf) == PCAP_ERROR) 1157190225Srpaulo return (PCAP_ERROR); 1158127664Sbms 1159127664Sbms major = genmajor(BPF_NAME); 1160127664Sbms if (major == -1) { 1161127664Sbms snprintf(errbuf, PCAP_ERRBUF_SIZE, 1162127664Sbms "bpf_load: genmajor failed: %s", pcap_strerror(errno)); 1163214518Srpaulo (void)bpf_odmcleanup(NULL); 1164190225Srpaulo return (PCAP_ERROR); 1165127664Sbms } 1166127664Sbms 1167127664Sbms minors = getminor(major, &numminors, BPF_NAME); 1168127664Sbms if (!minors) { 1169127664Sbms minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1); 1170127664Sbms if (!minors) { 1171127664Sbms snprintf(errbuf, PCAP_ERRBUF_SIZE, 1172127664Sbms "bpf_load: genminor failed: %s", 1173127664Sbms pcap_strerror(errno)); 1174214518Srpaulo (void)bpf_odmcleanup(NULL); 1175190225Srpaulo return (PCAP_ERROR); 1176127664Sbms } 1177127664Sbms } 1178127664Sbms 1179190225Srpaulo if (bpf_odmcleanup(errbuf) == PCAP_ERROR) 1180190225Srpaulo return (PCAP_ERROR); 1181127664Sbms 1182127664Sbms rc = stat(BPF_NODE "0", &sbuf); 1183127664Sbms if (rc == -1 && errno != ENOENT) { 1184127664Sbms snprintf(errbuf, PCAP_ERRBUF_SIZE, 1185127664Sbms "bpf_load: can't stat %s: %s", 1186127664Sbms BPF_NODE "0", pcap_strerror(errno)); 1187190225Srpaulo return (PCAP_ERROR); 1188127664Sbms } 1189127664Sbms 1190127664Sbms if (rc == -1 || getmajor(sbuf.st_rdev) != major) { 1191127664Sbms for (i = 0; i < BPF_MINORS; i++) { 1192127664Sbms sprintf(buf, "%s%d", BPF_NODE, i); 1193127664Sbms unlink(buf); 1194127664Sbms if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) { 1195127664Sbms snprintf(errbuf, PCAP_ERRBUF_SIZE, 1196127664Sbms "bpf_load: can't mknod %s: %s", 1197127664Sbms buf, pcap_strerror(errno)); 1198190225Srpaulo return (PCAP_ERROR); 1199127664Sbms } 1200127664Sbms } 1201127664Sbms } 1202127664Sbms 1203127664Sbms /* Check if the driver is loaded */ 1204127664Sbms memset(&cfg_ld, 0x0, sizeof(cfg_ld)); 1205127664Sbms cfg_ld.path = buf; 1206127664Sbms sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME); 1207127664Sbms if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) || 1208127664Sbms (cfg_ld.kmid == 0)) { 1209127664Sbms /* Driver isn't loaded, load it now */ 1210127664Sbms if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) { 1211127664Sbms snprintf(errbuf, PCAP_ERRBUF_SIZE, 1212127664Sbms "bpf_load: could not load driver: %s", 1213127664Sbms strerror(errno)); 1214190225Srpaulo return (PCAP_ERROR); 1215127664Sbms } 1216127664Sbms } 1217127664Sbms 1218127664Sbms /* Configure the driver */ 1219127664Sbms cfg_km.cmd = CFG_INIT; 1220127664Sbms cfg_km.kmid = cfg_ld.kmid; 1221127664Sbms cfg_km.mdilen = sizeof(cfg_bpf); 1222190225Srpaulo cfg_km.mdiptr = (void *)&cfg_bpf; 1223127664Sbms for (i = 0; i < BPF_MINORS; i++) { 1224127664Sbms cfg_bpf.devno = domakedev(major, i); 1225127664Sbms if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) { 1226127664Sbms snprintf(errbuf, PCAP_ERRBUF_SIZE, 1227127664Sbms "bpf_load: could not configure driver: %s", 1228127664Sbms strerror(errno)); 1229190225Srpaulo return (PCAP_ERROR); 1230127664Sbms } 1231127664Sbms } 1232190225Srpaulo 1233127664Sbms bpfloadedflag = 1; 1234127664Sbms 1235127664Sbms return (0); 1236127664Sbms} 1237127664Sbms#endif 1238127664Sbms 1239190225Srpaulo/* 1240190225Srpaulo * Turn off rfmon mode if necessary. 1241190225Srpaulo */ 1242190225Srpaulostatic void 1243190225Srpaulopcap_cleanup_bpf(pcap_t *p) 124417683Spst{ 1245190225Srpaulo#ifdef HAVE_BSD_IEEE80211 1246190225Srpaulo int sock; 1247190225Srpaulo struct ifmediareq req; 1248190225Srpaulo struct ifreq ifr; 1249172680Smlaier#endif 125017683Spst 1251214518Srpaulo if (p->md.must_do_on_close != 0) { 1252190225Srpaulo /* 1253190225Srpaulo * There's something we have to do when closing this 1254190225Srpaulo * pcap_t. 1255190225Srpaulo */ 1256190225Srpaulo#ifdef HAVE_BSD_IEEE80211 1257214518Srpaulo if (p->md.must_do_on_close & MUST_CLEAR_RFMON) { 1258190225Srpaulo /* 1259190225Srpaulo * We put the interface into rfmon mode; 1260190225Srpaulo * take it out of rfmon mode. 1261190225Srpaulo * 1262190225Srpaulo * XXX - if somebody else wants it in rfmon 1263190225Srpaulo * mode, this code cannot know that, so it'll take 1264190225Srpaulo * it out of rfmon mode. 1265190225Srpaulo */ 1266190225Srpaulo sock = socket(AF_INET, SOCK_DGRAM, 0); 1267190225Srpaulo if (sock == -1) { 1268190225Srpaulo fprintf(stderr, 1269190225Srpaulo "Can't restore interface flags (socket() failed: %s).\n" 1270190225Srpaulo "Please adjust manually.\n", 1271190225Srpaulo strerror(errno)); 1272190225Srpaulo } else { 1273190225Srpaulo memset(&req, 0, sizeof(req)); 1274190225Srpaulo strncpy(req.ifm_name, p->md.device, 1275190225Srpaulo sizeof(req.ifm_name)); 1276190225Srpaulo if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { 1277190225Srpaulo fprintf(stderr, 1278190225Srpaulo "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n" 1279190225Srpaulo "Please adjust manually.\n", 1280190225Srpaulo strerror(errno)); 1281190225Srpaulo } else { 1282190225Srpaulo if (req.ifm_current & IFM_IEEE80211_MONITOR) { 1283190225Srpaulo /* 1284190225Srpaulo * Rfmon mode is currently on; 1285190225Srpaulo * turn it off. 1286190225Srpaulo */ 1287190225Srpaulo memset(&ifr, 0, sizeof(ifr)); 1288190225Srpaulo (void)strncpy(ifr.ifr_name, 1289190225Srpaulo p->md.device, 1290190225Srpaulo sizeof(ifr.ifr_name)); 1291190225Srpaulo ifr.ifr_media = 1292190225Srpaulo req.ifm_current & ~IFM_IEEE80211_MONITOR; 1293190225Srpaulo if (ioctl(sock, SIOCSIFMEDIA, 1294190225Srpaulo &ifr) == -1) { 1295190225Srpaulo fprintf(stderr, 1296190225Srpaulo "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n" 1297190225Srpaulo "Please adjust manually.\n", 1298190225Srpaulo strerror(errno)); 1299190225Srpaulo } 1300190225Srpaulo } 1301190225Srpaulo } 1302190225Srpaulo close(sock); 1303190225Srpaulo } 1304190225Srpaulo } 1305190225Srpaulo#endif /* HAVE_BSD_IEEE80211 */ 1306127664Sbms 1307146768Ssam /* 1308190225Srpaulo * Take this pcap out of the list of pcaps for which we 1309190225Srpaulo * have to take the interface out of some mode. 1310146768Ssam */ 1311190225Srpaulo pcap_remove_from_pcaps_to_close(p); 1312214518Srpaulo p->md.must_do_on_close = 0; 1313190225Srpaulo } 131417683Spst 1315190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 1316190225Srpaulo if (p->md.zerocopy) { 1317235426Sdelphij /* 1318235426Sdelphij * Delete the mappings. Note that p->buffer gets 1319235426Sdelphij * initialized to one of the mmapped regions in 1320235426Sdelphij * this case, so do not try and free it directly; 1321235426Sdelphij * null it out so that pcap_cleanup_live_common() 1322235426Sdelphij * doesn't try to free it. 1323235426Sdelphij */ 1324190225Srpaulo if (p->md.zbuf1 != MAP_FAILED && p->md.zbuf1 != NULL) 1325235426Sdelphij (void) munmap(p->md.zbuf1, p->md.zbufsize); 1326190225Srpaulo if (p->md.zbuf2 != MAP_FAILED && p->md.zbuf2 != NULL) 1327235426Sdelphij (void) munmap(p->md.zbuf2, p->md.zbufsize); 1328222654Scsjp p->buffer = NULL; 1329235426Sdelphij p->buffer = NULL; 1330190225Srpaulo } 1331172680Smlaier#endif 1332190225Srpaulo if (p->md.device != NULL) { 1333190225Srpaulo free(p->md.device); 1334190225Srpaulo p->md.device = NULL; 1335190225Srpaulo } 1336190225Srpaulo pcap_cleanup_live_common(p); 133717683Spst} 133817683Spst 1339190225Srpaulostatic int 1340190225Srpaulocheck_setif_failure(pcap_t *p, int error) 1341190225Srpaulo{ 1342190225Srpaulo#ifdef __APPLE__ 1343190225Srpaulo int fd; 1344190225Srpaulo struct ifreq ifr; 1345190225Srpaulo int err; 1346146768Ssam#endif 1347146768Ssam 1348190225Srpaulo if (error == ENXIO) { 1349190225Srpaulo /* 1350190225Srpaulo * No such device exists. 1351190225Srpaulo */ 1352190225Srpaulo#ifdef __APPLE__ 1353190225Srpaulo if (p->opt.rfmon && strncmp(p->opt.source, "wlt", 3) == 0) { 1354190225Srpaulo /* 1355190225Srpaulo * Monitor mode was requested, and we're trying 1356190225Srpaulo * to open a "wltN" device. Assume that this 1357190225Srpaulo * is 10.4 and that we were asked to open an 1358190225Srpaulo * "enN" device; if that device exists, return 1359190225Srpaulo * "monitor mode not supported on the device". 1360190225Srpaulo */ 1361190225Srpaulo fd = socket(AF_INET, SOCK_DGRAM, 0); 1362190225Srpaulo if (fd != -1) { 1363190225Srpaulo strlcpy(ifr.ifr_name, "en", 1364190225Srpaulo sizeof(ifr.ifr_name)); 1365190225Srpaulo strlcat(ifr.ifr_name, p->opt.source + 3, 1366190225Srpaulo sizeof(ifr.ifr_name)); 1367190225Srpaulo if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) { 1368190225Srpaulo /* 1369190225Srpaulo * We assume this failed because 1370190225Srpaulo * the underlying device doesn't 1371190225Srpaulo * exist. 1372190225Srpaulo */ 1373190225Srpaulo err = PCAP_ERROR_NO_SUCH_DEVICE; 1374214518Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1375214518Srpaulo "SIOCGIFFLAGS on %s failed: %s", 1376214518Srpaulo ifr.ifr_name, pcap_strerror(errno)); 1377190225Srpaulo } else { 1378190225Srpaulo /* 1379190225Srpaulo * The underlying "enN" device 1380190225Srpaulo * exists, but there's no 1381190225Srpaulo * corresponding "wltN" device; 1382190225Srpaulo * that means that the "enN" 1383190225Srpaulo * device doesn't support 1384190225Srpaulo * monitor mode, probably because 1385190225Srpaulo * it's an Ethernet device rather 1386190225Srpaulo * than a wireless device. 1387190225Srpaulo */ 1388190225Srpaulo err = PCAP_ERROR_RFMON_NOTSUP; 1389190225Srpaulo } 1390190225Srpaulo close(fd); 1391190225Srpaulo } else { 1392190225Srpaulo /* 1393190225Srpaulo * We can't find out whether there's 1394190225Srpaulo * an underlying "enN" device, so 1395190225Srpaulo * just report "no such device". 1396190225Srpaulo */ 1397190225Srpaulo err = PCAP_ERROR_NO_SUCH_DEVICE; 1398214518Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1399214518Srpaulo "socket() failed: %s", 1400214518Srpaulo pcap_strerror(errno)); 1401190225Srpaulo } 1402190225Srpaulo return (err); 1403190225Srpaulo } 1404190225Srpaulo#endif 1405190225Srpaulo /* 1406190225Srpaulo * No such device. 1407190225Srpaulo */ 1408214518Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF failed: %s", 1409214518Srpaulo pcap_strerror(errno)); 1410190225Srpaulo return (PCAP_ERROR_NO_SUCH_DEVICE); 1411190225Srpaulo } else if (errno == ENETDOWN) { 1412190225Srpaulo /* 1413190225Srpaulo * Return a "network down" indication, so that 1414190225Srpaulo * the application can report that rather than 1415190225Srpaulo * saying we had a mysterious failure and 1416190225Srpaulo * suggest that they report a problem to the 1417190225Srpaulo * libpcap developers. 1418190225Srpaulo */ 1419190225Srpaulo return (PCAP_ERROR_IFACE_NOT_UP); 1420190225Srpaulo } else { 1421190225Srpaulo /* 1422190225Srpaulo * Some other error; fill in the error string, and 1423190225Srpaulo * return PCAP_ERROR. 1424190225Srpaulo */ 1425190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s", 1426190225Srpaulo p->opt.source, pcap_strerror(errno)); 1427190225Srpaulo return (PCAP_ERROR); 1428190225Srpaulo } 1429190225Srpaulo} 1430190225Srpaulo 1431214518Srpaulo/* 1432214518Srpaulo * Default capture buffer size. 1433214518Srpaulo * 32K isn't very much for modern machines with fast networks; we 1434214518Srpaulo * pick .5M, as that's the maximum on at least some systems with BPF. 1435241231Sdelphij * 1436241231Sdelphij * However, on AIX 3.5, the larger buffer sized caused unrecoverable 1437241231Sdelphij * read failures under stress, so we leave it as 32K; yet another 1438241231Sdelphij * place where AIX's BPF is broken. 1439214518Srpaulo */ 1440241231Sdelphij#ifdef _AIX 1441241231Sdelphij#define DEFAULT_BUFSIZE 32768 1442241231Sdelphij#else 1443214518Srpaulo#define DEFAULT_BUFSIZE 524288 1444241231Sdelphij#endif 1445214518Srpaulo 1446190225Srpaulostatic int 1447190225Srpaulopcap_activate_bpf(pcap_t *p) 144817683Spst{ 1449190225Srpaulo int status = 0; 145017683Spst int fd; 1451235426Sdelphij#ifdef LIFNAMSIZ 1452241231Sdelphij char *zonesep; 1453235426Sdelphij struct lifreq ifr; 1454235426Sdelphij char *ifrname = ifr.lifr_name; 1455235426Sdelphij const size_t ifnamsiz = sizeof(ifr.lifr_name); 1456235426Sdelphij#else 145717683Spst struct ifreq ifr; 1458235426Sdelphij char *ifrname = ifr.ifr_name; 1459235426Sdelphij const size_t ifnamsiz = sizeof(ifr.ifr_name); 1460235426Sdelphij#endif 146117683Spst struct bpf_version bv; 1462190225Srpaulo#ifdef __APPLE__ 1463190225Srpaulo int sockfd; 1464190225Srpaulo char *wltdev = NULL; 1465190225Srpaulo#endif 1466127664Sbms#ifdef BIOCGDLTLIST 1467109839Sfenner struct bpf_dltlist bdl; 1468190225Srpaulo#if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) 1469190225Srpaulo int new_dlt; 1470127664Sbms#endif 1471190225Srpaulo#endif /* BIOCGDLTLIST */ 1472146768Ssam#if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT) 1473146768Ssam u_int spoof_eth_src = 1; 1474146768Ssam#endif 147517683Spst u_int v; 1476146768Ssam struct bpf_insn total_insn; 1477146768Ssam struct bpf_program total_prog; 1478127664Sbms struct utsname osinfo; 147917683Spst 1480127664Sbms#ifdef HAVE_DAG_API 1481127664Sbms if (strstr(device, "dag")) { 1482127664Sbms return dag_open_live(device, snaplen, promisc, to_ms, ebuf); 1483127664Sbms } 1484127664Sbms#endif /* HAVE_DAG_API */ 1485109839Sfenner 1486127664Sbms#ifdef BIOCGDLTLIST 1487127664Sbms memset(&bdl, 0, sizeof(bdl)); 1488190225Srpaulo int have_osinfo = 0; 1489190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 1490190225Srpaulo struct bpf_zbuf bz; 1491190225Srpaulo u_int bufmode, zbufmax; 1492127664Sbms#endif 1493127664Sbms 1494190225Srpaulo fd = bpf_open(p); 1495190225Srpaulo if (fd < 0) { 1496190225Srpaulo status = fd; 1497190225Srpaulo goto bad; 149817683Spst } 149917683Spst 150017683Spst p->fd = fd; 150117683Spst 150217683Spst if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) { 1503190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s", 150475107Sfenner pcap_strerror(errno)); 1505190225Srpaulo status = PCAP_ERROR; 150617683Spst goto bad; 150717683Spst } 150817683Spst if (bv.bv_major != BPF_MAJOR_VERSION || 150917683Spst bv.bv_minor < BPF_MINOR_VERSION) { 1510190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 151175107Sfenner "kernel bpf filter out of date"); 1512190225Srpaulo status = PCAP_ERROR; 151317683Spst goto bad; 151417683Spst } 151575107Sfenner 1516241231Sdelphij#if defined(LIFNAMSIZ) && defined(ZONENAME_MAX) && defined(lifr_zoneid) 1517241231Sdelphij /* 1518241231Sdelphij * Check if the given source network device has a '/' separated 1519241231Sdelphij * zonename prefix string. The zonename prefixed source device 1520241231Sdelphij * can be used by libpcap consumers to capture network traffic 1521241231Sdelphij * in non-global zones from the global zone on Solaris 11 and 1522241231Sdelphij * above. If the zonename prefix is present then we strip the 1523241231Sdelphij * prefix and pass the zone ID as part of lifr_zoneid. 1524241231Sdelphij */ 1525241231Sdelphij if ((zonesep = strchr(p->opt.source, '/')) != NULL) { 1526241231Sdelphij char zonename[ZONENAME_MAX]; 1527241231Sdelphij int znamelen; 1528241231Sdelphij char *lnamep; 1529241231Sdelphij 1530241231Sdelphij znamelen = zonesep - p->opt.source; 1531241231Sdelphij (void) strlcpy(zonename, p->opt.source, znamelen + 1); 1532241231Sdelphij lnamep = strdup(zonesep + 1); 1533241231Sdelphij ifr.lifr_zoneid = getzoneidbyname(zonename); 1534241231Sdelphij free(p->opt.source); 1535241231Sdelphij p->opt.source = lnamep; 1536241231Sdelphij } 1537241231Sdelphij#endif 1538241231Sdelphij 1539190225Srpaulo p->md.device = strdup(p->opt.source); 1540190225Srpaulo if (p->md.device == NULL) { 1541190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "strdup: %s", 1542190225Srpaulo pcap_strerror(errno)); 1543190225Srpaulo status = PCAP_ERROR; 1544190225Srpaulo goto bad; 1545190225Srpaulo } 1546190225Srpaulo 154775107Sfenner /* 1548190225Srpaulo * Try finding a good size for the buffer; 32768 may be too 1549190225Srpaulo * big, so keep cutting it in half until we find a size 1550190225Srpaulo * that works, or run out of sizes to try. If the default 1551190225Srpaulo * is larger, don't make it smaller. 1552190225Srpaulo * 1553190225Srpaulo * XXX - there should be a user-accessible hook to set the 1554190225Srpaulo * initial buffer size. 1555190225Srpaulo * Attempt to find out the version of the OS on which we're running. 1556190225Srpaulo */ 1557190225Srpaulo if (uname(&osinfo) == 0) 1558190225Srpaulo have_osinfo = 1; 1559190225Srpaulo 1560190225Srpaulo#ifdef __APPLE__ 1561190225Srpaulo /* 1562190225Srpaulo * See comment in pcap_can_set_rfmon_bpf() for an explanation 1563190225Srpaulo * of why we check the version number. 1564190225Srpaulo */ 1565190225Srpaulo if (p->opt.rfmon) { 1566190225Srpaulo if (have_osinfo) { 1567190225Srpaulo /* 1568190225Srpaulo * We assume osinfo.sysname is "Darwin", because 1569190225Srpaulo * __APPLE__ is defined. We just check the version. 1570190225Srpaulo */ 1571190225Srpaulo if (osinfo.release[0] < '8' && 1572190225Srpaulo osinfo.release[1] == '.') { 1573190225Srpaulo /* 1574190225Srpaulo * 10.3 (Darwin 7.x) or earlier. 1575190225Srpaulo */ 1576190225Srpaulo status = PCAP_ERROR_RFMON_NOTSUP; 1577190225Srpaulo goto bad; 1578190225Srpaulo } 1579190225Srpaulo if (osinfo.release[0] == '8' && 1580190225Srpaulo osinfo.release[1] == '.') { 1581190225Srpaulo /* 1582190225Srpaulo * 10.4 (Darwin 8.x). s/en/wlt/ 1583190225Srpaulo */ 1584190225Srpaulo if (strncmp(p->opt.source, "en", 2) != 0) { 1585190225Srpaulo /* 1586190225Srpaulo * Not an enN device; check 1587190225Srpaulo * whether the device even exists. 1588190225Srpaulo */ 1589190225Srpaulo sockfd = socket(AF_INET, SOCK_DGRAM, 0); 1590190225Srpaulo if (sockfd != -1) { 1591235426Sdelphij strlcpy(ifrname, 1592235426Sdelphij p->opt.source, ifnamsiz); 1593190225Srpaulo if (ioctl(sockfd, SIOCGIFFLAGS, 1594190225Srpaulo (char *)&ifr) < 0) { 1595190225Srpaulo /* 1596190225Srpaulo * We assume this 1597190225Srpaulo * failed because 1598190225Srpaulo * the underlying 1599190225Srpaulo * device doesn't 1600190225Srpaulo * exist. 1601190225Srpaulo */ 1602190225Srpaulo status = PCAP_ERROR_NO_SUCH_DEVICE; 1603214518Srpaulo snprintf(p->errbuf, 1604214518Srpaulo PCAP_ERRBUF_SIZE, 1605214518Srpaulo "SIOCGIFFLAGS failed: %s", 1606214518Srpaulo pcap_strerror(errno)); 1607190225Srpaulo } else 1608190225Srpaulo status = PCAP_ERROR_RFMON_NOTSUP; 1609190225Srpaulo close(sockfd); 1610190225Srpaulo } else { 1611190225Srpaulo /* 1612190225Srpaulo * We can't find out whether 1613190225Srpaulo * the device exists, so just 1614190225Srpaulo * report "no such device". 1615190225Srpaulo */ 1616190225Srpaulo status = PCAP_ERROR_NO_SUCH_DEVICE; 1617214518Srpaulo snprintf(p->errbuf, 1618214518Srpaulo PCAP_ERRBUF_SIZE, 1619214518Srpaulo "socket() failed: %s", 1620214518Srpaulo pcap_strerror(errno)); 1621190225Srpaulo } 1622190225Srpaulo goto bad; 1623190225Srpaulo } 1624190225Srpaulo wltdev = malloc(strlen(p->opt.source) + 2); 1625190225Srpaulo if (wltdev == NULL) { 1626190225Srpaulo (void)snprintf(p->errbuf, 1627190225Srpaulo PCAP_ERRBUF_SIZE, "malloc: %s", 1628190225Srpaulo pcap_strerror(errno)); 1629190225Srpaulo status = PCAP_ERROR; 1630190225Srpaulo goto bad; 1631190225Srpaulo } 1632190225Srpaulo strcpy(wltdev, "wlt"); 1633190225Srpaulo strcat(wltdev, p->opt.source + 2); 1634190225Srpaulo free(p->opt.source); 1635190225Srpaulo p->opt.source = wltdev; 1636190225Srpaulo } 1637190225Srpaulo /* 1638190225Srpaulo * Everything else is 10.5 or later; for those, 1639190225Srpaulo * we just open the enN device, and set the DLT. 1640190225Srpaulo */ 1641190225Srpaulo } 1642190225Srpaulo } 1643190225Srpaulo#endif /* __APPLE__ */ 1644190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 1645190225Srpaulo /* 1646183102Scsjp * If the BPF extension to set buffer mode is present, try setting 1647183102Scsjp * the mode to zero-copy. If that fails, use regular buffering. If 1648183102Scsjp * it succeeds but other setup fails, return an error to the user. 164939291Sfenner */ 1650183102Scsjp bufmode = BPF_BUFMODE_ZBUF; 1651183102Scsjp if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) { 1652190225Srpaulo /* 1653190225Srpaulo * We have zerocopy BPF; use it. 1654190225Srpaulo */ 1655190225Srpaulo p->md.zerocopy = 1; 1656183102Scsjp 1657183102Scsjp /* 1658183102Scsjp * How to pick a buffer size: first, query the maximum buffer 1659183102Scsjp * size supported by zero-copy. This also lets us quickly 1660183102Scsjp * determine whether the kernel generally supports zero-copy. 1661190225Srpaulo * Then, if a buffer size was specified, use that, otherwise 1662190225Srpaulo * query the default buffer size, which reflects kernel 1663183102Scsjp * policy for a desired default. Round to the nearest page 1664183102Scsjp * size. 166575107Sfenner */ 1666183102Scsjp if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) { 1667190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGETZMAX: %s", 1668183102Scsjp pcap_strerror(errno)); 1669183102Scsjp goto bad; 1670183102Scsjp } 1671190225Srpaulo 1672190225Srpaulo if (p->opt.buffer_size != 0) { 1673190225Srpaulo /* 1674190225Srpaulo * A buffer size was explicitly specified; use it. 1675190225Srpaulo */ 1676190225Srpaulo v = p->opt.buffer_size; 1677190225Srpaulo } else { 1678190225Srpaulo if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || 1679214518Srpaulo v < DEFAULT_BUFSIZE) 1680214518Srpaulo v = DEFAULT_BUFSIZE; 1681190225Srpaulo } 1682183102Scsjp#ifndef roundup 1683190225Srpaulo#define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */ 1684183102Scsjp#endif 1685190225Srpaulo p->md.zbufsize = roundup(v, getpagesize()); 1686190225Srpaulo if (p->md.zbufsize > zbufmax) 1687190225Srpaulo p->md.zbufsize = zbufmax; 1688190225Srpaulo p->md.zbuf1 = mmap(NULL, p->md.zbufsize, PROT_READ | PROT_WRITE, 1689183102Scsjp MAP_ANON, -1, 0); 1690190225Srpaulo p->md.zbuf2 = mmap(NULL, p->md.zbufsize, PROT_READ | PROT_WRITE, 1691183102Scsjp MAP_ANON, -1, 0); 1692190225Srpaulo if (p->md.zbuf1 == MAP_FAILED || p->md.zbuf2 == MAP_FAILED) { 1693190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "mmap: %s", 1694183102Scsjp pcap_strerror(errno)); 1695183102Scsjp goto bad; 1696183102Scsjp } 1697251129Sdelphij memset(&bz, 0, sizeof(bz)); /* bzero() deprecated, replaced with memset() */ 1698190225Srpaulo bz.bz_bufa = p->md.zbuf1; 1699190225Srpaulo bz.bz_bufb = p->md.zbuf2; 1700190225Srpaulo bz.bz_buflen = p->md.zbufsize; 1701183102Scsjp if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) { 1702190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETZBUF: %s", 1703183102Scsjp pcap_strerror(errno)); 1704183102Scsjp goto bad; 1705183102Scsjp } 1706235426Sdelphij (void)strncpy(ifrname, p->opt.source, ifnamsiz); 1707183102Scsjp if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { 1708190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s", 1709190225Srpaulo p->opt.source, pcap_strerror(errno)); 171075107Sfenner goto bad; 171175107Sfenner } 1712190225Srpaulo v = p->md.zbufsize - sizeof(struct bpf_zbuf_header); 1713190225Srpaulo } else 1714183102Scsjp#endif 1715190225Srpaulo { 1716183102Scsjp /* 1717190225Srpaulo * We don't have zerocopy BPF. 1718190225Srpaulo * Set the buffer size. 1719183102Scsjp */ 1720190225Srpaulo if (p->opt.buffer_size != 0) { 1721190225Srpaulo /* 1722190225Srpaulo * A buffer size was explicitly specified; use it. 1723183102Scsjp */ 1724190225Srpaulo if (ioctl(fd, BIOCSBLEN, 1725190225Srpaulo (caddr_t)&p->opt.buffer_size) < 0) { 1726190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1727190225Srpaulo "BIOCSBLEN: %s: %s", p->opt.source, 1728190225Srpaulo pcap_strerror(errno)); 1729190225Srpaulo status = PCAP_ERROR; 1730190225Srpaulo goto bad; 1731190225Srpaulo } 1732183102Scsjp 1733190225Srpaulo /* 1734190225Srpaulo * Now bind to the device. 1735190225Srpaulo */ 1736235426Sdelphij (void)strncpy(ifrname, p->opt.source, ifnamsiz); 1737235426Sdelphij#ifdef BIOCSETLIF 1738235426Sdelphij if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) < 0) 1739235426Sdelphij#else 1740235426Sdelphij if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) 1741235426Sdelphij#endif 1742235426Sdelphij { 1743190225Srpaulo status = check_setif_failure(p, errno); 1744190225Srpaulo goto bad; 1745190225Srpaulo } 1746190225Srpaulo } else { 1747190225Srpaulo /* 1748190225Srpaulo * No buffer size was explicitly specified. 1749190225Srpaulo * 1750214518Srpaulo * Try finding a good size for the buffer; 1751214518Srpaulo * DEFAULT_BUFSIZE may be too big, so keep 1752214518Srpaulo * cutting it in half until we find a size 1753214518Srpaulo * that works, or run out of sizes to try. 1754190225Srpaulo * If the default is larger, don't make it smaller. 1755190225Srpaulo */ 1756190225Srpaulo if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || 1757214518Srpaulo v < DEFAULT_BUFSIZE) 1758214518Srpaulo v = DEFAULT_BUFSIZE; 1759190225Srpaulo for ( ; v != 0; v >>= 1) { 1760190225Srpaulo /* 1761190225Srpaulo * Ignore the return value - this is because the 1762190225Srpaulo * call fails on BPF systems that don't have 1763190225Srpaulo * kernel malloc. And if the call fails, it's 1764190225Srpaulo * no big deal, we just continue to use the 1765190225Srpaulo * standard buffer size. 1766190225Srpaulo */ 1767190225Srpaulo (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v); 1768183102Scsjp 1769235426Sdelphij (void)strncpy(ifrname, p->opt.source, ifnamsiz); 1770235426Sdelphij#ifdef BIOCSETLIF 1771235426Sdelphij if (ioctl(fd, BIOCSETLIF, (caddr_t)&ifr) >= 0) 1772235426Sdelphij#else 1773190225Srpaulo if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0) 1774235426Sdelphij#endif 1775190225Srpaulo break; /* that size worked; we're done */ 1776190225Srpaulo 1777190225Srpaulo if (errno != ENOBUFS) { 1778190225Srpaulo status = check_setif_failure(p, errno); 1779190225Srpaulo goto bad; 1780190225Srpaulo } 1781190225Srpaulo } 1782190225Srpaulo 1783190225Srpaulo if (v == 0) { 1784190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 1785190225Srpaulo "BIOCSBLEN: %s: No buffer size worked", 1786190225Srpaulo p->opt.source); 1787190225Srpaulo status = PCAP_ERROR; 1788183102Scsjp goto bad; 1789183102Scsjp } 1790183102Scsjp } 179117683Spst } 1792183102Scsjp#endif 179375107Sfenner 179417683Spst /* Get the data link layer type. */ 179517683Spst if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) { 1796190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s", 179775107Sfenner pcap_strerror(errno)); 1798190225Srpaulo status = PCAP_ERROR; 179917683Spst goto bad; 180017683Spst } 1801190225Srpaulo 180298530Sfenner#ifdef _AIX 180398530Sfenner /* 180498530Sfenner * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT. 180598530Sfenner */ 180656889Sfenner switch (v) { 180798530Sfenner 180898530Sfenner case IFT_ETHER: 180998530Sfenner case IFT_ISO88023: 181098530Sfenner v = DLT_EN10MB; 181198530Sfenner break; 181298530Sfenner 181398530Sfenner case IFT_FDDI: 181498530Sfenner v = DLT_FDDI; 181598530Sfenner break; 181698530Sfenner 181798530Sfenner case IFT_ISO88025: 181898530Sfenner v = DLT_IEEE802; 181998530Sfenner break; 182098530Sfenner 1821127664Sbms case IFT_LOOP: 1822127664Sbms v = DLT_NULL; 1823127664Sbms break; 1824127664Sbms 182598530Sfenner default: 182675107Sfenner /* 182798530Sfenner * We don't know what to map this to yet. 182875107Sfenner */ 1829190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u", 183098530Sfenner v); 1831190225Srpaulo status = PCAP_ERROR; 183298530Sfenner goto bad; 183356889Sfenner } 183456889Sfenner#endif 183539291Sfenner#if _BSDI_VERSION - 0 >= 199510 183639291Sfenner /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */ 183739291Sfenner switch (v) { 183839291Sfenner 183939291Sfenner case DLT_SLIP: 184039291Sfenner v = DLT_SLIP_BSDOS; 184139291Sfenner break; 184239291Sfenner 184339291Sfenner case DLT_PPP: 184439291Sfenner v = DLT_PPP_BSDOS; 184539291Sfenner break; 184656889Sfenner 184756889Sfenner case 11: /*DLT_FR*/ 1848127664Sbms v = DLT_FRELAY; 184956889Sfenner break; 185056889Sfenner 185156889Sfenner case 12: /*DLT_C_HDLC*/ 185256889Sfenner v = DLT_CHDLC; 185356889Sfenner break; 185439291Sfenner } 185539291Sfenner#endif 185617683Spst 1857127664Sbms#ifdef BIOCGDLTLIST 1858109839Sfenner /* 1859127664Sbms * We know the default link type -- now determine all the DLTs 1860127664Sbms * this interface supports. If this fails with EINVAL, it's 1861127664Sbms * not fatal; we just don't get to use the feature later. 1862109839Sfenner */ 1863190225Srpaulo if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) { 1864190225Srpaulo status = PCAP_ERROR; 1865190225Srpaulo goto bad; 1866190225Srpaulo } 1867190225Srpaulo p->dlt_count = bdl.bfl_len; 1868190225Srpaulo p->dlt_list = bdl.bfl_list; 1869146768Ssam 1870190225Srpaulo#ifdef __APPLE__ 1871190225Srpaulo /* 1872190225Srpaulo * Monitor mode fun, continued. 1873190225Srpaulo * 1874190225Srpaulo * For 10.5 and, we're assuming, later releases, as noted above, 1875190225Srpaulo * 802.1 adapters that support monitor mode offer both DLT_EN10MB, 1876190225Srpaulo * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information 1877190225Srpaulo * DLT_ value. Choosing one of the 802.11 DLT_ values will turn 1878190225Srpaulo * monitor mode on. 1879190225Srpaulo * 1880190225Srpaulo * Therefore, if the user asked for monitor mode, we filter out 1881190225Srpaulo * the DLT_EN10MB value, as you can't get that in monitor mode, 1882190225Srpaulo * and, if the user didn't ask for monitor mode, we filter out 1883190225Srpaulo * the 802.11 DLT_ values, because selecting those will turn 1884190225Srpaulo * monitor mode on. Then, for monitor mode, if an 802.11-plus- 1885190225Srpaulo * radio DLT_ value is offered, we try to select that, otherwise 1886190225Srpaulo * we try to select DLT_IEEE802_11. 1887190225Srpaulo */ 1888190225Srpaulo if (have_osinfo) { 1889190225Srpaulo if (isdigit((unsigned)osinfo.release[0]) && 1890190225Srpaulo (osinfo.release[0] == '9' || 1891190225Srpaulo isdigit((unsigned)osinfo.release[1]))) { 1892190225Srpaulo /* 1893190225Srpaulo * 10.5 (Darwin 9.x), or later. 1894190225Srpaulo */ 1895190225Srpaulo new_dlt = find_802_11(&bdl); 1896190225Srpaulo if (new_dlt != -1) { 1897190225Srpaulo /* 1898190225Srpaulo * We have at least one 802.11 DLT_ value, 1899190225Srpaulo * so this is an 802.11 interface. 1900190225Srpaulo * new_dlt is the best of the 802.11 1901190225Srpaulo * DLT_ values in the list. 1902190225Srpaulo */ 1903190225Srpaulo if (p->opt.rfmon) { 1904190225Srpaulo /* 1905190225Srpaulo * Our caller wants monitor mode. 1906190225Srpaulo * Purge DLT_EN10MB from the list 1907190225Srpaulo * of link-layer types, as selecting 1908190225Srpaulo * it will keep monitor mode off. 1909190225Srpaulo */ 1910190225Srpaulo remove_en(p); 1911190225Srpaulo 1912190225Srpaulo /* 1913190225Srpaulo * If the new mode we want isn't 1914190225Srpaulo * the default mode, attempt to 1915190225Srpaulo * select the new mode. 1916190225Srpaulo */ 1917190225Srpaulo if (new_dlt != v) { 1918190225Srpaulo if (ioctl(p->fd, BIOCSDLT, 1919190225Srpaulo &new_dlt) != -1) { 1920190225Srpaulo /* 1921190225Srpaulo * We succeeded; 1922190225Srpaulo * make this the 1923190225Srpaulo * new DLT_ value. 1924190225Srpaulo */ 1925190225Srpaulo v = new_dlt; 1926190225Srpaulo } 1927190225Srpaulo } 1928190225Srpaulo } else { 1929190225Srpaulo /* 1930190225Srpaulo * Our caller doesn't want 1931190225Srpaulo * monitor mode. Unless this 1932190225Srpaulo * is being done by pcap_open_live(), 1933190225Srpaulo * purge the 802.11 link-layer types 1934190225Srpaulo * from the list, as selecting 1935190225Srpaulo * one of them will turn monitor 1936190225Srpaulo * mode on. 1937190225Srpaulo */ 1938190225Srpaulo if (!p->oldstyle) 1939190225Srpaulo remove_802_11(p); 1940190225Srpaulo } 1941190225Srpaulo } else { 1942190225Srpaulo if (p->opt.rfmon) { 1943190225Srpaulo /* 1944190225Srpaulo * The caller requested monitor 1945190225Srpaulo * mode, but we have no 802.11 1946190225Srpaulo * link-layer types, so they 1947190225Srpaulo * can't have it. 1948190225Srpaulo */ 1949190225Srpaulo status = PCAP_ERROR_RFMON_NOTSUP; 1950190225Srpaulo goto bad; 1951190225Srpaulo } 1952190225Srpaulo } 1953109839Sfenner } 1954190225Srpaulo } 1955190225Srpaulo#elif defined(HAVE_BSD_IEEE80211) 1956190225Srpaulo /* 1957190225Srpaulo * *BSD with the new 802.11 ioctls. 1958190225Srpaulo * Do we want monitor mode? 1959190225Srpaulo */ 1960190225Srpaulo if (p->opt.rfmon) { 1961190225Srpaulo /* 1962190225Srpaulo * Try to put the interface into monitor mode. 1963190225Srpaulo */ 1964190225Srpaulo status = monitor_mode(p, 1); 1965190225Srpaulo if (status != 0) { 1966190225Srpaulo /* 1967190225Srpaulo * We failed. 1968190225Srpaulo */ 1969109839Sfenner goto bad; 1970109839Sfenner } 1971109839Sfenner 1972146768Ssam /* 1973190225Srpaulo * We're in monitor mode. 1974190225Srpaulo * Try to find the best 802.11 DLT_ value and, if we 1975190225Srpaulo * succeed, try to switch to that mode if we're not 1976190225Srpaulo * already in that mode. 1977146768Ssam */ 1978190225Srpaulo new_dlt = find_802_11(&bdl); 1979190225Srpaulo if (new_dlt != -1) { 1980190225Srpaulo /* 1981190225Srpaulo * We have at least one 802.11 DLT_ value. 1982190225Srpaulo * new_dlt is the best of the 802.11 1983190225Srpaulo * DLT_ values in the list. 1984190225Srpaulo * 1985190225Srpaulo * If the new mode we want isn't the default mode, 1986190225Srpaulo * attempt to select the new mode. 1987190225Srpaulo */ 1988190225Srpaulo if (new_dlt != v) { 1989190225Srpaulo if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) { 1990190225Srpaulo /* 1991190225Srpaulo * We succeeded; make this the 1992190225Srpaulo * new DLT_ value. 1993190225Srpaulo */ 1994190225Srpaulo v = new_dlt; 1995146768Ssam } 1996146768Ssam } 1997146768Ssam } 1998109839Sfenner } 1999190225Srpaulo#endif /* various platforms */ 2000190225Srpaulo#endif /* BIOCGDLTLIST */ 2001109839Sfenner 2002146768Ssam /* 2003146768Ssam * If this is an Ethernet device, and we don't have a DLT_ list, 2004146768Ssam * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give 2005146768Ssam * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to 2006146768Ssam * do, but there's not much we can do about that without finding 2007146768Ssam * some other way of determining whether it's an Ethernet or 802.11 2008146768Ssam * device.) 2009146768Ssam */ 2010190225Srpaulo if (v == DLT_EN10MB && p->dlt_count == 0) { 2011146768Ssam p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 2012146768Ssam /* 2013146768Ssam * If that fails, just leave the list empty. 2014146768Ssam */ 2015146768Ssam if (p->dlt_list != NULL) { 2016146768Ssam p->dlt_list[0] = DLT_EN10MB; 2017146768Ssam p->dlt_list[1] = DLT_DOCSIS; 2018146768Ssam p->dlt_count = 2; 2019146768Ssam } 2020146768Ssam } 2021190225Srpaulo#ifdef PCAP_FDDIPAD 2022190225Srpaulo if (v == DLT_FDDI) 2023190225Srpaulo p->fddipad = PCAP_FDDIPAD; 2024190225Srpaulo else 2025190225Srpaulo p->fddipad = 0; 2026190225Srpaulo#endif 2027190225Srpaulo p->linktype = v; 2028190225Srpaulo 2029146768Ssam#if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT) 2030146768Ssam /* 2031146768Ssam * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so 2032146768Ssam * the link-layer source address isn't forcibly overwritten. 2033146768Ssam * (Should we ignore errors? Should we do this only if 2034146768Ssam * we're open for writing?) 2035146768Ssam * 2036146768Ssam * XXX - I seem to remember some packet-sending bug in some 2037146768Ssam * BSDs - check CVS log for "bpf.c"? 2038146768Ssam */ 2039146768Ssam if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) { 2040190225Srpaulo (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 2041146768Ssam "BIOCSHDRCMPLT: %s", pcap_strerror(errno)); 2042190225Srpaulo status = PCAP_ERROR; 2043146768Ssam goto bad; 2044146768Ssam } 2045146768Ssam#endif 204617683Spst /* set timeout */ 2047190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 2048190225Srpaulo if (p->md.timeout != 0 && !p->md.zerocopy) { 2049190225Srpaulo#else 2050190225Srpaulo if (p->md.timeout) { 2051190225Srpaulo#endif 2052127664Sbms /* 2053127664Sbms * XXX - is this seconds/nanoseconds in AIX? 2054127664Sbms * (Treating it as such doesn't fix the timeout 2055127664Sbms * problem described below.) 2056214518Srpaulo * 2057214518Srpaulo * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in 2058214518Srpaulo * 64-bit userland - it takes, as an argument, a 2059214518Srpaulo * "struct BPF_TIMEVAL", which has 32-bit tv_sec 2060214518Srpaulo * and tv_usec, rather than a "struct timeval". 2061214518Srpaulo * 2062214518Srpaulo * If this platform defines "struct BPF_TIMEVAL", 2063214518Srpaulo * we check whether the structure size in BIOCSRTIMEOUT 2064214518Srpaulo * is that of a "struct timeval" and, if not, we use 2065214518Srpaulo * a "struct BPF_TIMEVAL" rather than a "struct timeval". 2066214518Srpaulo * (That way, if the bug is fixed in a future release, 2067214518Srpaulo * we will still do the right thing.) 2068127664Sbms */ 206917683Spst struct timeval to; 2070214518Srpaulo#ifdef HAVE_STRUCT_BPF_TIMEVAL 2071214518Srpaulo struct BPF_TIMEVAL bpf_to; 2072214518Srpaulo 2073214518Srpaulo if (IOCPARM_LEN(BIOCSRTIMEOUT) != sizeof(struct timeval)) { 2074214518Srpaulo bpf_to.tv_sec = p->md.timeout / 1000; 2075214518Srpaulo bpf_to.tv_usec = (p->md.timeout * 1000) % 1000000; 2076214518Srpaulo if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) { 2077214518Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 2078214518Srpaulo "BIOCSRTIMEOUT: %s", pcap_strerror(errno)); 2079214518Srpaulo status = PCAP_ERROR; 2080214518Srpaulo goto bad; 2081214518Srpaulo } 2082214518Srpaulo } else { 2083214518Srpaulo#endif 2084214518Srpaulo to.tv_sec = p->md.timeout / 1000; 2085214518Srpaulo to.tv_usec = (p->md.timeout * 1000) % 1000000; 2086214518Srpaulo if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) { 2087214518Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 2088214518Srpaulo "BIOCSRTIMEOUT: %s", pcap_strerror(errno)); 2089214518Srpaulo status = PCAP_ERROR; 2090214518Srpaulo goto bad; 2091214518Srpaulo } 2092214518Srpaulo#ifdef HAVE_STRUCT_BPF_TIMEVAL 209317683Spst } 2094214518Srpaulo#endif 209517683Spst } 209675107Sfenner 209775107Sfenner#ifdef _AIX 209875107Sfenner#ifdef BIOCIMMEDIATE 209975107Sfenner /* 210075107Sfenner * Darren Reed notes that 210175107Sfenner * 210275107Sfenner * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the 210375107Sfenner * timeout appears to be ignored and it waits until the buffer 210475107Sfenner * is filled before returning. The result of not having it 210575107Sfenner * set is almost worse than useless if your BPF filter 210675107Sfenner * is reducing things to only a few packets (i.e. one every 210775107Sfenner * second or so). 210875107Sfenner * 210975107Sfenner * so we turn BIOCIMMEDIATE mode on if this is AIX. 211075107Sfenner * 211175107Sfenner * We don't turn it on for other platforms, as that means we 211275107Sfenner * get woken up for every packet, which may not be what we want; 211375107Sfenner * in the Winter 1993 USENIX paper on BPF, they say: 211475107Sfenner * 211575107Sfenner * Since a process might want to look at every packet on a 211675107Sfenner * network and the time between packets can be only a few 211775107Sfenner * microseconds, it is not possible to do a read system call 211875107Sfenner * per packet and BPF must collect the data from several 211975107Sfenner * packets and return it as a unit when the monitoring 212075107Sfenner * application does a read. 212175107Sfenner * 212275107Sfenner * which I infer is the reason for the timeout - it means we 212375107Sfenner * wait that amount of time, in the hopes that more packets 212475107Sfenner * will arrive and we'll get them all with one read. 212575107Sfenner * 212675107Sfenner * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other 212775107Sfenner * BSDs) causes the timeout to be ignored. 212875107Sfenner * 212975107Sfenner * On the other hand, some platforms (e.g., Linux) don't support 213075107Sfenner * timeouts, they just hand stuff to you as soon as it arrives; 213175107Sfenner * if that doesn't cause a problem on those platforms, it may 213275107Sfenner * be OK to have BIOCIMMEDIATE mode on BSD as well. 213375107Sfenner * 213475107Sfenner * (Note, though, that applications may depend on the read 213575107Sfenner * completing, even if no packets have arrived, when the timeout 213675107Sfenner * expires, e.g. GUI applications that have to check for input 213775107Sfenner * while waiting for packets to arrive; a non-zero timeout 213875107Sfenner * prevents "select()" from working right on FreeBSD and 213975107Sfenner * possibly other BSDs, as the timer doesn't start until a 214075107Sfenner * "read()" is done, so the timer isn't in effect if the 214175107Sfenner * application is blocked on a "select()", and the "select()" 214275107Sfenner * doesn't get woken up for a BPF device until the buffer 214375107Sfenner * fills up.) 214475107Sfenner */ 214575107Sfenner v = 1; 214675107Sfenner if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) { 2147190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCIMMEDIATE: %s", 214875107Sfenner pcap_strerror(errno)); 2149190225Srpaulo status = PCAP_ERROR; 215075107Sfenner goto bad; 215175107Sfenner } 215275107Sfenner#endif /* BIOCIMMEDIATE */ 215375107Sfenner#endif /* _AIX */ 215475107Sfenner 2155190225Srpaulo if (p->opt.promisc) { 2156190225Srpaulo /* set promiscuous mode, just warn if it fails */ 215798530Sfenner if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) { 2158190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s", 215998530Sfenner pcap_strerror(errno)); 2160190225Srpaulo status = PCAP_WARNING_PROMISC_NOTSUP; 216198530Sfenner } 216298530Sfenner } 216317683Spst 216417683Spst if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) { 2165190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s", 216675107Sfenner pcap_strerror(errno)); 2167190225Srpaulo status = PCAP_ERROR; 216817683Spst goto bad; 216917683Spst } 217017683Spst p->bufsize = v; 2171190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 2172190225Srpaulo if (!p->md.zerocopy) { 2173183102Scsjp#endif 2174190225Srpaulo p->buffer = (u_char *)malloc(p->bufsize); 2175190225Srpaulo if (p->buffer == NULL) { 2176190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", 2177190225Srpaulo pcap_strerror(errno)); 2178190225Srpaulo status = PCAP_ERROR; 2179190225Srpaulo goto bad; 2180190225Srpaulo } 2181127664Sbms#ifdef _AIX 2182190225Srpaulo /* For some strange reason this seems to prevent the EFAULT 2183190225Srpaulo * problems we have experienced from AIX BPF. */ 2184190225Srpaulo memset(p->buffer, 0x0, p->bufsize); 2185127664Sbms#endif 2186190225Srpaulo#ifdef HAVE_ZEROCOPY_BPF 2187183102Scsjp } 2188183102Scsjp#endif 218917683Spst 2190127664Sbms /* 2191146768Ssam * If there's no filter program installed, there's 2192146768Ssam * no indication to the kernel of what the snapshot 2193146768Ssam * length should be, so no snapshotting is done. 2194146768Ssam * 2195146768Ssam * Therefore, when we open the device, we install 2196146768Ssam * an "accept everything" filter with the specified 2197146768Ssam * snapshot length. 2198146768Ssam */ 2199146768Ssam total_insn.code = (u_short)(BPF_RET | BPF_K); 2200146768Ssam total_insn.jt = 0; 2201146768Ssam total_insn.jf = 0; 2202190225Srpaulo total_insn.k = p->snapshot; 2203146768Ssam 2204146768Ssam total_prog.bf_len = 1; 2205146768Ssam total_prog.bf_insns = &total_insn; 2206146768Ssam if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) { 2207190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s", 2208146768Ssam pcap_strerror(errno)); 2209190225Srpaulo status = PCAP_ERROR; 2210146768Ssam goto bad; 2211146768Ssam } 2212146768Ssam 2213146768Ssam /* 2214127664Sbms * On most BPF platforms, either you can do a "select()" or 2215127664Sbms * "poll()" on a BPF file descriptor and it works correctly, 2216127664Sbms * or you can do it and it will return "readable" if the 2217127664Sbms * hold buffer is full but not if the timeout expires *and* 2218127664Sbms * a non-blocking read will, if the hold buffer is empty 2219127664Sbms * but the store buffer isn't empty, rotate the buffers 2220127664Sbms * and return what packets are available. 2221127664Sbms * 2222127664Sbms * In the latter case, the fact that a non-blocking read 2223127664Sbms * will give you the available packets means you can work 2224127664Sbms * around the failure of "select()" and "poll()" to wake up 2225127664Sbms * and return "readable" when the timeout expires by using 2226127664Sbms * the timeout as the "select()" or "poll()" timeout, putting 2227127664Sbms * the BPF descriptor into non-blocking mode, and read from 2228127664Sbms * it regardless of whether "select()" reports it as readable 2229127664Sbms * or not. 2230127664Sbms * 2231127664Sbms * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()" 2232127664Sbms * won't wake up and return "readable" if the timer expires 2233127664Sbms * and non-blocking reads return EWOULDBLOCK if the hold 2234127664Sbms * buffer is empty, even if the store buffer is non-empty. 2235127664Sbms * 2236127664Sbms * This means the workaround in question won't work. 2237127664Sbms * 2238127664Sbms * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd" 2239127664Sbms * to -1, which means "sorry, you can't use 'select()' or 'poll()' 2240127664Sbms * here". On all other BPF platforms, we set it to the FD for 2241127664Sbms * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking 2242127664Sbms * read will, if the hold buffer is empty and the store buffer 2243127664Sbms * isn't empty, rotate the buffers and return what packets are 2244127664Sbms * there (and in sufficiently recent versions of OpenBSD 2245127664Sbms * "select()" and "poll()" should work correctly). 2246127664Sbms * 2247127664Sbms * XXX - what about AIX? 2248127664Sbms */ 2249147894Ssam p->selectable_fd = p->fd; /* assume select() works until we know otherwise */ 2250190225Srpaulo if (have_osinfo) { 2251127664Sbms /* 2252127664Sbms * We can check what OS this is. 2253127664Sbms */ 2254147894Ssam if (strcmp(osinfo.sysname, "FreeBSD") == 0) { 2255147894Ssam if (strncmp(osinfo.release, "4.3-", 4) == 0 || 2256147894Ssam strncmp(osinfo.release, "4.4-", 4) == 0) 2257147894Ssam p->selectable_fd = -1; 2258147894Ssam } 2259127664Sbms } 2260127664Sbms 2261127664Sbms p->read_op = pcap_read_bpf; 2262146768Ssam p->inject_op = pcap_inject_bpf; 2263127664Sbms p->setfilter_op = pcap_setfilter_bpf; 2264147894Ssam p->setdirection_op = pcap_setdirection_bpf; 2265127664Sbms p->set_datalink_op = pcap_set_datalink_bpf; 2266235426Sdelphij p->getnonblock_op = pcap_getnonblock_bpf; 2267235426Sdelphij p->setnonblock_op = pcap_setnonblock_bpf; 2268127664Sbms p->stats_op = pcap_stats_bpf; 2269190225Srpaulo p->cleanup_op = pcap_cleanup_bpf; 2270127664Sbms 2271190225Srpaulo return (status); 227217683Spst bad: 2273190225Srpaulo pcap_cleanup_bpf(p); 2274190225Srpaulo return (status); 227517683Spst} 227617683Spst 227717683Spstint 2278127664Sbmspcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 227917683Spst{ 2280127664Sbms return (0); 2281127664Sbms} 2282127664Sbms 2283190225Srpaulo#ifdef HAVE_BSD_IEEE80211 2284127664Sbmsstatic int 2285190225Srpaulomonitor_mode(pcap_t *p, int set) 2286127664Sbms{ 2287190225Srpaulo int sock; 2288190225Srpaulo struct ifmediareq req; 2289190225Srpaulo int *media_list; 2290190225Srpaulo int i; 2291190225Srpaulo int can_do; 2292190225Srpaulo struct ifreq ifr; 2293190225Srpaulo 2294190225Srpaulo sock = socket(AF_INET, SOCK_DGRAM, 0); 2295190225Srpaulo if (sock == -1) { 2296190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "can't open socket: %s", 2297190225Srpaulo pcap_strerror(errno)); 2298190225Srpaulo return (PCAP_ERROR); 2299190225Srpaulo } 2300190225Srpaulo 2301190225Srpaulo memset(&req, 0, sizeof req); 2302190225Srpaulo strncpy(req.ifm_name, p->opt.source, sizeof req.ifm_name); 2303190225Srpaulo 230456889Sfenner /* 2305190225Srpaulo * Find out how many media types we have. 230656889Sfenner */ 2307190225Srpaulo if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { 2308127664Sbms /* 2309190225Srpaulo * Can't get the media types. 2310127664Sbms */ 2311235426Sdelphij switch (errno) { 2312235426Sdelphij 2313235426Sdelphij case ENXIO: 2314190225Srpaulo /* 2315235426Sdelphij * There's no such device. 2316235426Sdelphij */ 2317235426Sdelphij close(sock); 2318235426Sdelphij return (PCAP_ERROR_NO_SUCH_DEVICE); 2319235426Sdelphij 2320235426Sdelphij case EINVAL: 2321235426Sdelphij /* 2322190225Srpaulo * Interface doesn't support SIOC{G,S}IFMEDIA. 2323190225Srpaulo */ 2324190225Srpaulo close(sock); 2325190225Srpaulo return (PCAP_ERROR_RFMON_NOTSUP); 2326235426Sdelphij 2327235426Sdelphij default: 2328235426Sdelphij snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 2329235426Sdelphij "SIOCGIFMEDIA 1: %s", pcap_strerror(errno)); 2330235426Sdelphij close(sock); 2331235426Sdelphij return (PCAP_ERROR); 2332190225Srpaulo } 2333127664Sbms } 2334190225Srpaulo if (req.ifm_count == 0) { 2335190225Srpaulo /* 2336190225Srpaulo * No media types. 2337190225Srpaulo */ 2338190225Srpaulo close(sock); 2339190225Srpaulo return (PCAP_ERROR_RFMON_NOTSUP); 2340190225Srpaulo } 2341127664Sbms 2342127664Sbms /* 2343190225Srpaulo * Allocate a buffer to hold all the media types, and 2344190225Srpaulo * get the media types. 2345190225Srpaulo */ 2346190225Srpaulo media_list = malloc(req.ifm_count * sizeof(int)); 2347190225Srpaulo if (media_list == NULL) { 2348190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s", 2349190225Srpaulo pcap_strerror(errno)); 2350190225Srpaulo close(sock); 2351190225Srpaulo return (PCAP_ERROR); 2352190225Srpaulo } 2353190225Srpaulo req.ifm_ulist = media_list; 2354190225Srpaulo if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) { 2355190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFMEDIA: %s", 2356190225Srpaulo pcap_strerror(errno)); 2357190225Srpaulo free(media_list); 2358190225Srpaulo close(sock); 2359190225Srpaulo return (PCAP_ERROR); 2360190225Srpaulo } 2361190225Srpaulo 2362190225Srpaulo /* 2363190225Srpaulo * Look for an 802.11 "automatic" media type. 2364190225Srpaulo * We assume that all 802.11 adapters have that media type, 2365190225Srpaulo * and that it will carry the monitor mode supported flag. 2366190225Srpaulo */ 2367190225Srpaulo can_do = 0; 2368190225Srpaulo for (i = 0; i < req.ifm_count; i++) { 2369190225Srpaulo if (IFM_TYPE(media_list[i]) == IFM_IEEE80211 2370190225Srpaulo && IFM_SUBTYPE(media_list[i]) == IFM_AUTO) { 2371190225Srpaulo /* OK, does it do monitor mode? */ 2372190225Srpaulo if (media_list[i] & IFM_IEEE80211_MONITOR) { 2373190225Srpaulo can_do = 1; 2374190225Srpaulo break; 2375190225Srpaulo } 2376190225Srpaulo } 2377190225Srpaulo } 2378190225Srpaulo free(media_list); 2379190225Srpaulo if (!can_do) { 2380190225Srpaulo /* 2381190225Srpaulo * This adapter doesn't support monitor mode. 2382190225Srpaulo */ 2383190225Srpaulo close(sock); 2384190225Srpaulo return (PCAP_ERROR_RFMON_NOTSUP); 2385190225Srpaulo } 2386190225Srpaulo 2387190225Srpaulo if (set) { 2388190225Srpaulo /* 2389190225Srpaulo * Don't just check whether we can enable monitor mode, 2390190225Srpaulo * do so, if it's not already enabled. 2391190225Srpaulo */ 2392190225Srpaulo if ((req.ifm_current & IFM_IEEE80211_MONITOR) == 0) { 2393190225Srpaulo /* 2394190225Srpaulo * Monitor mode isn't currently on, so turn it on, 2395190225Srpaulo * and remember that we should turn it off when the 2396190225Srpaulo * pcap_t is closed. 2397190225Srpaulo */ 2398190225Srpaulo 2399190225Srpaulo /* 2400190225Srpaulo * If we haven't already done so, arrange to have 2401190225Srpaulo * "pcap_close_all()" called when we exit. 2402190225Srpaulo */ 2403190225Srpaulo if (!pcap_do_addexit(p)) { 2404190225Srpaulo /* 2405190225Srpaulo * "atexit()" failed; don't put the interface 2406190225Srpaulo * in monitor mode, just give up. 2407190225Srpaulo */ 2408190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 2409190225Srpaulo "atexit failed"); 2410190225Srpaulo close(sock); 2411190225Srpaulo return (PCAP_ERROR); 2412190225Srpaulo } 2413190225Srpaulo memset(&ifr, 0, sizeof(ifr)); 2414190225Srpaulo (void)strncpy(ifr.ifr_name, p->opt.source, 2415190225Srpaulo sizeof(ifr.ifr_name)); 2416190225Srpaulo ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR; 2417190225Srpaulo if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) { 2418190225Srpaulo snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 2419190225Srpaulo "SIOCSIFMEDIA: %s", pcap_strerror(errno)); 2420190225Srpaulo close(sock); 2421190225Srpaulo return (PCAP_ERROR); 2422190225Srpaulo } 2423190225Srpaulo 2424214518Srpaulo p->md.must_do_on_close |= MUST_CLEAR_RFMON; 2425190225Srpaulo 2426190225Srpaulo /* 2427190225Srpaulo * Add this to the list of pcaps to close when we exit. 2428190225Srpaulo */ 2429190225Srpaulo pcap_add_to_pcaps_to_close(p); 2430190225Srpaulo } 2431190225Srpaulo } 2432190225Srpaulo return (0); 2433190225Srpaulo} 2434190225Srpaulo#endif /* HAVE_BSD_IEEE80211 */ 2435190225Srpaulo 2436190225Srpaulo#if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) 2437190225Srpaulo/* 2438190225Srpaulo * Check whether we have any 802.11 link-layer types; return the best 2439190225Srpaulo * of the 802.11 link-layer types if we find one, and return -1 2440190225Srpaulo * otherwise. 2441190225Srpaulo * 2442190225Srpaulo * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the 2443190225Srpaulo * best 802.11 link-layer type; any of the other 802.11-plus-radio 2444190225Srpaulo * headers are second-best; 802.11 with no radio information is 2445190225Srpaulo * the least good. 2446190225Srpaulo */ 2447190225Srpaulostatic int 2448190225Srpaulofind_802_11(struct bpf_dltlist *bdlp) 2449190225Srpaulo{ 2450190225Srpaulo int new_dlt; 2451190225Srpaulo int i; 2452190225Srpaulo 2453190225Srpaulo /* 2454190225Srpaulo * Scan the list of DLT_ values, looking for 802.11 values, 2455190225Srpaulo * and, if we find any, choose the best of them. 2456190225Srpaulo */ 2457190225Srpaulo new_dlt = -1; 2458190225Srpaulo for (i = 0; i < bdlp->bfl_len; i++) { 2459190225Srpaulo switch (bdlp->bfl_list[i]) { 2460190225Srpaulo 2461190225Srpaulo case DLT_IEEE802_11: 2462190225Srpaulo /* 2463190225Srpaulo * 802.11, but no radio. 2464190225Srpaulo * 2465190225Srpaulo * Offer this, and select it as the new mode 2466190225Srpaulo * unless we've already found an 802.11 2467190225Srpaulo * header with radio information. 2468190225Srpaulo */ 2469190225Srpaulo if (new_dlt == -1) 2470190225Srpaulo new_dlt = bdlp->bfl_list[i]; 2471190225Srpaulo break; 2472190225Srpaulo 2473190225Srpaulo case DLT_PRISM_HEADER: 2474190225Srpaulo case DLT_AIRONET_HEADER: 2475190225Srpaulo case DLT_IEEE802_11_RADIO_AVS: 2476190225Srpaulo /* 2477190225Srpaulo * 802.11 with radio, but not radiotap. 2478190225Srpaulo * 2479190225Srpaulo * Offer this, and select it as the new mode 2480190225Srpaulo * unless we've already found the radiotap DLT_. 2481190225Srpaulo */ 2482190225Srpaulo if (new_dlt != DLT_IEEE802_11_RADIO) 2483190225Srpaulo new_dlt = bdlp->bfl_list[i]; 2484190225Srpaulo break; 2485190225Srpaulo 2486190225Srpaulo case DLT_IEEE802_11_RADIO: 2487190225Srpaulo /* 2488190225Srpaulo * 802.11 with radiotap. 2489190225Srpaulo * 2490190225Srpaulo * Offer this, and select it as the new mode. 2491190225Srpaulo */ 2492190225Srpaulo new_dlt = bdlp->bfl_list[i]; 2493190225Srpaulo break; 2494190225Srpaulo 2495190225Srpaulo default: 2496190225Srpaulo /* 2497190225Srpaulo * Not 802.11. 2498190225Srpaulo */ 2499190225Srpaulo break; 2500190225Srpaulo } 2501190225Srpaulo } 2502190225Srpaulo 2503190225Srpaulo return (new_dlt); 2504190225Srpaulo} 2505190225Srpaulo#endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */ 2506190225Srpaulo 2507190225Srpaulo#if defined(__APPLE__) && defined(BIOCGDLTLIST) 2508190225Srpaulo/* 2509214518Srpaulo * Remove DLT_EN10MB from the list of DLT_ values, as we're in monitor mode, 2510214518Srpaulo * and DLT_EN10MB isn't supported in monitor mode. 2511190225Srpaulo */ 2512190225Srpaulostatic void 2513190225Srpauloremove_en(pcap_t *p) 2514190225Srpaulo{ 2515190225Srpaulo int i, j; 2516190225Srpaulo 2517190225Srpaulo /* 2518190225Srpaulo * Scan the list of DLT_ values and discard DLT_EN10MB. 2519190225Srpaulo */ 2520190225Srpaulo j = 0; 2521190225Srpaulo for (i = 0; i < p->dlt_count; i++) { 2522190225Srpaulo switch (p->dlt_list[i]) { 2523190225Srpaulo 2524190225Srpaulo case DLT_EN10MB: 2525190225Srpaulo /* 2526190225Srpaulo * Don't offer this one. 2527190225Srpaulo */ 2528190225Srpaulo continue; 2529190225Srpaulo 2530190225Srpaulo default: 2531190225Srpaulo /* 2532190225Srpaulo * Just copy this mode over. 2533190225Srpaulo */ 2534190225Srpaulo break; 2535190225Srpaulo } 2536190225Srpaulo 2537190225Srpaulo /* 2538190225Srpaulo * Copy this DLT_ value to its new position. 2539190225Srpaulo */ 2540190225Srpaulo p->dlt_list[j] = p->dlt_list[i]; 2541190225Srpaulo j++; 2542190225Srpaulo } 2543190225Srpaulo 2544190225Srpaulo /* 2545190225Srpaulo * Set the DLT_ count to the number of entries we copied. 2546190225Srpaulo */ 2547190225Srpaulo p->dlt_count = j; 2548190225Srpaulo} 2549190225Srpaulo 2550190225Srpaulo/* 2551214518Srpaulo * Remove 802.11 link-layer types from the list of DLT_ values, as 2552214518Srpaulo * we're not in monitor mode, and those DLT_ values will switch us 2553214518Srpaulo * to monitor mode. 2554190225Srpaulo */ 2555190225Srpaulostatic void 2556190225Srpauloremove_802_11(pcap_t *p) 2557190225Srpaulo{ 2558190225Srpaulo int i, j; 2559190225Srpaulo 2560190225Srpaulo /* 2561190225Srpaulo * Scan the list of DLT_ values and discard 802.11 values. 2562190225Srpaulo */ 2563190225Srpaulo j = 0; 2564190225Srpaulo for (i = 0; i < p->dlt_count; i++) { 2565190225Srpaulo switch (p->dlt_list[i]) { 2566190225Srpaulo 2567190225Srpaulo case DLT_IEEE802_11: 2568190225Srpaulo case DLT_PRISM_HEADER: 2569190225Srpaulo case DLT_AIRONET_HEADER: 2570190225Srpaulo case DLT_IEEE802_11_RADIO: 2571190225Srpaulo case DLT_IEEE802_11_RADIO_AVS: 2572190225Srpaulo /* 2573190225Srpaulo * 802.11. Don't offer this one. 2574190225Srpaulo */ 2575190225Srpaulo continue; 2576190225Srpaulo 2577190225Srpaulo default: 2578190225Srpaulo /* 2579190225Srpaulo * Just copy this mode over. 2580190225Srpaulo */ 2581190225Srpaulo break; 2582190225Srpaulo } 2583190225Srpaulo 2584190225Srpaulo /* 2585190225Srpaulo * Copy this DLT_ value to its new position. 2586190225Srpaulo */ 2587190225Srpaulo p->dlt_list[j] = p->dlt_list[i]; 2588190225Srpaulo j++; 2589190225Srpaulo } 2590190225Srpaulo 2591190225Srpaulo /* 2592190225Srpaulo * Set the DLT_ count to the number of entries we copied. 2593190225Srpaulo */ 2594190225Srpaulo p->dlt_count = j; 2595190225Srpaulo} 2596190225Srpaulo#endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */ 2597190225Srpaulo 2598190225Srpaulostatic int 2599190225Srpaulopcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp) 2600190225Srpaulo{ 2601190225Srpaulo /* 2602127664Sbms * Free any user-mode filter we might happen to have installed. 2603127664Sbms */ 2604127664Sbms pcap_freecode(&p->fcode); 2605127664Sbms 2606127664Sbms /* 2607127664Sbms * Try to install the kernel filter. 2608127664Sbms */ 2609190225Srpaulo if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) { 2610190225Srpaulo /* 2611190225Srpaulo * It worked. 2612190225Srpaulo */ 2613190225Srpaulo p->md.use_bpf = 1; /* filtering in the kernel */ 2614190225Srpaulo 2615190225Srpaulo /* 2616190225Srpaulo * Discard any previously-received packets, as they might 2617190225Srpaulo * have passed whatever filter was formerly in effect, but 2618190225Srpaulo * might not pass this filter (BIOCSETF discards packets 2619190225Srpaulo * buffered in the kernel, so you can lose packets in any 2620190225Srpaulo * case). 2621190225Srpaulo */ 2622190225Srpaulo p->cc = 0; 2623190225Srpaulo return (0); 2624190225Srpaulo } 2625190225Srpaulo 2626190225Srpaulo /* 2627190225Srpaulo * We failed. 2628190225Srpaulo * 2629190225Srpaulo * If it failed with EINVAL, that's probably because the program 2630190225Srpaulo * is invalid or too big. Validate it ourselves; if we like it 2631190225Srpaulo * (we currently allow backward branches, to support protochain), 2632190225Srpaulo * run it in userland. (There's no notion of "too big" for 2633190225Srpaulo * userland.) 2634190225Srpaulo * 2635190225Srpaulo * Otherwise, just give up. 2636190225Srpaulo * XXX - if the copy of the program into the kernel failed, 2637190225Srpaulo * we will get EINVAL rather than, say, EFAULT on at least 2638190225Srpaulo * some kernels. 2639190225Srpaulo */ 2640190225Srpaulo if (errno != EINVAL) { 264175107Sfenner snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s", 264275107Sfenner pcap_strerror(errno)); 264317683Spst return (-1); 264417683Spst } 2645146768Ssam 2646146768Ssam /* 2647190225Srpaulo * install_bpf_program() validates the program. 2648190225Srpaulo * 2649190225Srpaulo * XXX - what if we already have a filter in the kernel? 2650146768Ssam */ 2651190225Srpaulo if (install_bpf_program(p, fp) < 0) 2652190225Srpaulo return (-1); 2653190225Srpaulo p->md.use_bpf = 0; /* filtering in userland */ 265417683Spst return (0); 265517683Spst} 2656109839Sfenner 2657147894Ssam/* 2658147894Ssam * Set direction flag: Which packets do we accept on a forwarding 2659147894Ssam * single device? IN, OUT or both? 2660147894Ssam */ 2661127664Sbmsstatic int 2662162012Ssampcap_setdirection_bpf(pcap_t *p, pcap_direction_t d) 2663147894Ssam{ 2664167035Sjkim#if defined(BIOCSDIRECTION) 2665167035Sjkim u_int direction; 2666167035Sjkim 2667167035Sjkim direction = (d == PCAP_D_IN) ? BPF_D_IN : 2668167035Sjkim ((d == PCAP_D_OUT) ? BPF_D_OUT : BPF_D_INOUT); 2669167035Sjkim if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) { 2670167035Sjkim (void) snprintf(p->errbuf, sizeof(p->errbuf), 2671167035Sjkim "Cannot set direction to %s: %s", 2672167035Sjkim (d == PCAP_D_IN) ? "PCAP_D_IN" : 2673167035Sjkim ((d == PCAP_D_OUT) ? "PCAP_D_OUT" : "PCAP_D_INOUT"), 2674167035Sjkim strerror(errno)); 2675167035Sjkim return (-1); 2676167035Sjkim } 2677167035Sjkim return (0); 2678167035Sjkim#elif defined(BIOCSSEESENT) 2679147894Ssam u_int seesent; 2680147894Ssam 2681147894Ssam /* 2682162012Ssam * We don't support PCAP_D_OUT. 2683147894Ssam */ 2684162012Ssam if (d == PCAP_D_OUT) { 2685147894Ssam snprintf(p->errbuf, sizeof(p->errbuf), 2686162012Ssam "Setting direction to PCAP_D_OUT is not supported on BPF"); 2687147894Ssam return -1; 2688147894Ssam } 2689167035Sjkim 2690162012Ssam seesent = (d == PCAP_D_INOUT); 2691147894Ssam if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) { 2692147894Ssam (void) snprintf(p->errbuf, sizeof(p->errbuf), 2693147894Ssam "Cannot set direction to %s: %s", 2694162012Ssam (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN", 2695147894Ssam strerror(errno)); 2696147894Ssam return (-1); 2697147894Ssam } 2698147894Ssam return (0); 2699147894Ssam#else 2700147894Ssam (void) snprintf(p->errbuf, sizeof(p->errbuf), 2701147894Ssam "This system doesn't support BIOCSSEESENT, so the direction can't be set"); 2702147894Ssam return (-1); 2703147894Ssam#endif 2704147894Ssam} 2705147894Ssam 2706147894Ssamstatic int 2707127664Sbmspcap_set_datalink_bpf(pcap_t *p, int dlt) 2708109839Sfenner{ 2709127664Sbms#ifdef BIOCSDLT 2710109839Sfenner if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) { 2711109839Sfenner (void) snprintf(p->errbuf, sizeof(p->errbuf), 2712109839Sfenner "Cannot set DLT %d: %s", dlt, strerror(errno)); 2713127664Sbms return (-1); 2714109839Sfenner } 2715127664Sbms#endif 2716127664Sbms return (0); 2717109839Sfenner} 2718