18cf6c252SPaul Traina /*
2a4b5b39fSBill Fenner * Copyright (c) 1993, 1994, 1995, 1996, 1998
38cf6c252SPaul Traina * The Regents of the University of California. All rights reserved.
48cf6c252SPaul Traina *
58cf6c252SPaul Traina * Redistribution and use in source and binary forms, with or without
68cf6c252SPaul Traina * modification, are permitted provided that: (1) source code distributions
78cf6c252SPaul Traina * retain the above copyright notice and this paragraph in its entirety, (2)
88cf6c252SPaul Traina * distributions including binary code include the above copyright notice and
98cf6c252SPaul Traina * this paragraph in its entirety in the documentation or other materials
108cf6c252SPaul Traina * provided with the distribution, and (3) all advertising materials mentioning
118cf6c252SPaul Traina * features or use of this software display the following acknowledgement:
128cf6c252SPaul Traina * ``This product includes software developed by the University of California,
138cf6c252SPaul Traina * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
148cf6c252SPaul Traina * the University nor the names of its contributors may be used to endorse
158cf6c252SPaul Traina * or promote products derived from this software without specific prior
168cf6c252SPaul Traina * written permission.
178cf6c252SPaul Traina * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
188cf6c252SPaul Traina * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
198cf6c252SPaul Traina * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
208cf6c252SPaul Traina */
21dc2c7305SBill Fenner
22b00ab754SHans Petter Selasky #include <config.h>
238cf6c252SPaul Traina
248cf6c252SPaul Traina #include <sys/param.h> /* optionally get BSD define */
258cf6c252SPaul Traina #include <sys/socket.h>
26d1e87331SXin LI #include <time.h>
27a0ee43a1SRui Paulo /*
28a0ee43a1SRui Paulo * <net/bpf.h> defines ioctls, but doesn't include <sys/ioccom.h>.
29a0ee43a1SRui Paulo *
30a0ee43a1SRui Paulo * We include <sys/ioctl.h> as it might be necessary to declare ioctl();
31b00ab754SHans Petter Selasky * at least on *BSD and macOS, it also defines various SIOC ioctls -
32a0ee43a1SRui Paulo * we could include <sys/sockio.h>, but if we're already including
33a0ee43a1SRui Paulo * <sys/ioctl.h>, which includes <sys/sockio.h> on those platforms,
34a0ee43a1SRui Paulo * there's not much point in doing so.
35a0ee43a1SRui Paulo *
36a0ee43a1SRui Paulo * If we have <sys/ioccom.h>, we include it as well, to handle systems
37a0ee43a1SRui Paulo * such as Solaris which don't arrange to include <sys/ioccom.h> if you
38a0ee43a1SRui Paulo * include <sys/ioctl.h>
39a0ee43a1SRui Paulo */
408cf6c252SPaul Traina #include <sys/ioctl.h>
41a0ee43a1SRui Paulo #ifdef HAVE_SYS_IOCCOM_H
42a0ee43a1SRui Paulo #include <sys/ioccom.h>
43a0ee43a1SRui Paulo #endif
44feb4ecdbSBruce M Simpson #include <sys/utsname.h>
458cf6c252SPaul Traina
46ada6f083SXin LI #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
47ada6f083SXin LI /*
48ada6f083SXin LI * Add support for capturing on FreeBSD usbusN interfaces.
49ada6f083SXin LI */
50ada6f083SXin LI static const char usbus_prefix[] = "usbus";
51ada6f083SXin LI #define USBUS_PREFIX_LEN (sizeof(usbus_prefix) - 1)
52ada6f083SXin LI #include <dirent.h>
53ada6f083SXin LI #endif
54ada6f083SXin LI
558cf6c252SPaul Traina #include <net/if.h>
56feb4ecdbSBruce M Simpson
570a94d38fSBill Fenner #ifdef _AIX
58feb4ecdbSBruce M Simpson
590a94d38fSBill Fenner /*
60a8e07101SRui Paulo * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
61feb4ecdbSBruce M Simpson * native OS version, as we need "struct bpf_config" from it.
620a94d38fSBill Fenner */
63feb4ecdbSBruce M Simpson #define PCAP_DONT_INCLUDE_PCAP_BPF_H
64feb4ecdbSBruce M Simpson
65feb4ecdbSBruce M Simpson #include <sys/types.h>
66feb4ecdbSBruce M Simpson
67feb4ecdbSBruce M Simpson /*
68feb4ecdbSBruce M Simpson * Prevent bpf.h from redefining the DLT_ values to their
69feb4ecdbSBruce M Simpson * IFT_ values, as we're going to return the standard libpcap
70feb4ecdbSBruce M Simpson * values, not IBM's non-standard IFT_ values.
71feb4ecdbSBruce M Simpson */
72feb4ecdbSBruce M Simpson #undef _AIX
73feb4ecdbSBruce M Simpson #include <net/bpf.h>
74feb4ecdbSBruce M Simpson #define _AIX
75feb4ecdbSBruce M Simpson
76b00ab754SHans Petter Selasky /*
77b00ab754SHans Petter Selasky * If both BIOCROTZBUF and BPF_BUFMODE_ZBUF are defined, we have
78b00ab754SHans Petter Selasky * zero-copy BPF.
79b00ab754SHans Petter Selasky */
80b00ab754SHans Petter Selasky #if defined(BIOCROTZBUF) && defined(BPF_BUFMODE_ZBUF)
81b00ab754SHans Petter Selasky #define HAVE_ZEROCOPY_BPF
82b00ab754SHans Petter Selasky #include <sys/mman.h>
83b00ab754SHans Petter Selasky #include <machine/atomic.h>
84b00ab754SHans Petter Selasky #endif
85b00ab754SHans Petter Selasky
860a94d38fSBill Fenner #include <net/if_types.h> /* for IFT_ values */
87feb4ecdbSBruce M Simpson #include <sys/sysconfig.h>
88feb4ecdbSBruce M Simpson #include <sys/device.h>
89ee2dd488SSam Leffler #include <sys/cfgodm.h>
90feb4ecdbSBruce M Simpson #include <cf.h>
91feb4ecdbSBruce M Simpson
92feb4ecdbSBruce M Simpson #ifdef __64BIT__
93feb4ecdbSBruce M Simpson #define domakedev makedev64
94feb4ecdbSBruce M Simpson #define getmajor major64
95feb4ecdbSBruce M Simpson #define bpf_hdr bpf_hdr32
96feb4ecdbSBruce M Simpson #else /* __64BIT__ */
97feb4ecdbSBruce M Simpson #define domakedev makedev
98feb4ecdbSBruce M Simpson #define getmajor major
99feb4ecdbSBruce M Simpson #endif /* __64BIT__ */
100feb4ecdbSBruce M Simpson
101feb4ecdbSBruce M Simpson #define BPF_NAME "bpf"
102feb4ecdbSBruce M Simpson #define BPF_MINORS 4
103feb4ecdbSBruce M Simpson #define DRIVER_PATH "/usr/lib/drivers"
104feb4ecdbSBruce M Simpson #define BPF_NODE "/dev/bpf"
105feb4ecdbSBruce M Simpson static int bpfloadedflag = 0;
106feb4ecdbSBruce M Simpson static int odmlockid = 0;
107feb4ecdbSBruce M Simpson
108a0ee43a1SRui Paulo static int bpf_load(char *errbuf);
109a0ee43a1SRui Paulo
110feb4ecdbSBruce M Simpson #else /* _AIX */
111feb4ecdbSBruce M Simpson
112feb4ecdbSBruce M Simpson #include <net/bpf.h>
113feb4ecdbSBruce M Simpson
114feb4ecdbSBruce M Simpson #endif /* _AIX */
1158cf6c252SPaul Traina
116a0ee43a1SRui Paulo #include <fcntl.h>
1178cf6c252SPaul Traina #include <errno.h>
1188cf6c252SPaul Traina #include <netdb.h>
1198cf6c252SPaul Traina #include <stdio.h>
1208cf6c252SPaul Traina #include <stdlib.h>
1218cf6c252SPaul Traina #include <string.h>
1228cf6c252SPaul Traina #include <unistd.h>
123*afdbf109SJoseph Mingrone #include <stddef.h>
1248cf6c252SPaul Traina
125b00ab754SHans Petter Selasky #ifdef SIOCGIFMEDIA
126a8e07101SRui Paulo # include <net/if_media.h>
127a8e07101SRui Paulo #endif
128a8e07101SRui Paulo
1298cf6c252SPaul Traina #include "pcap-int.h"
1308cf6c252SPaul Traina
1318cf6c252SPaul Traina #ifdef HAVE_OS_PROTO_H
1328cf6c252SPaul Traina #include "os-proto.h"
1338cf6c252SPaul Traina #endif
1348cf6c252SPaul Traina
135681ed54cSXin LI /*
136681ed54cSXin LI * Later versions of NetBSD stick padding in front of FDDI frames
137681ed54cSXin LI * to align the IP header on a 4-byte boundary.
138681ed54cSXin LI */
139681ed54cSXin LI #if defined(__NetBSD__) && __NetBSD_Version__ > 106000000
140681ed54cSXin LI #define PCAP_FDDIPAD 3
141681ed54cSXin LI #endif
142681ed54cSXin LI
143681ed54cSXin LI /*
144681ed54cSXin LI * Private data for capturing on BPF devices.
145681ed54cSXin LI */
146681ed54cSXin LI struct pcap_bpf {
147681ed54cSXin LI #ifdef HAVE_ZEROCOPY_BPF
148681ed54cSXin LI /*
149681ed54cSXin LI * Zero-copy read buffer -- for zero-copy BPF. 'buffer' above will
150681ed54cSXin LI * alternative between these two actual mmap'd buffers as required.
151681ed54cSXin LI * As there is a header on the front size of the mmap'd buffer, only
152681ed54cSXin LI * some of the buffer is exposed to libpcap as a whole via bufsize;
153681ed54cSXin LI * zbufsize is the true size. zbuffer tracks the current zbuf
1546f9cba8fSJoseph Mingrone * associated with buffer so that it can be used to decide which the
155681ed54cSXin LI * next buffer to read will be.
156681ed54cSXin LI */
157681ed54cSXin LI u_char *zbuf1, *zbuf2, *zbuffer;
158681ed54cSXin LI u_int zbufsize;
159681ed54cSXin LI u_int zerocopy;
160681ed54cSXin LI u_int interrupted;
161681ed54cSXin LI struct timespec firstsel;
162681ed54cSXin LI /*
163681ed54cSXin LI * If there's currently a buffer being actively processed, then it is
164681ed54cSXin LI * referenced here; 'buffer' is also pointed at it, but offset by the
165681ed54cSXin LI * size of the header.
166681ed54cSXin LI */
167681ed54cSXin LI struct bpf_zbuf_header *bzh;
168681ed54cSXin LI int nonblock; /* true if in nonblocking mode */
169681ed54cSXin LI #endif /* HAVE_ZEROCOPY_BPF */
170681ed54cSXin LI
171681ed54cSXin LI char *device; /* device name */
172681ed54cSXin LI int filtering_in_kernel; /* using kernel filter */
173681ed54cSXin LI int must_do_on_close; /* stuff we must do when we close */
174681ed54cSXin LI };
175681ed54cSXin LI
176681ed54cSXin LI /*
177681ed54cSXin LI * Stuff to do when we close.
178681ed54cSXin LI */
179681ed54cSXin LI #define MUST_CLEAR_RFMON 0x00000001 /* clear rfmon (monitor) mode */
180ada6f083SXin LI #define MUST_DESTROY_USBUS 0x00000002 /* destroy usbusN interface */
181681ed54cSXin LI
182a8e07101SRui Paulo #ifdef BIOCGDLTLIST
183a8e07101SRui Paulo # if (defined(HAVE_NET_IF_MEDIA_H) && defined(IFM_IEEE80211)) && !defined(__APPLE__)
184a8e07101SRui Paulo #define HAVE_BSD_IEEE80211
185ada6f083SXin LI
186ada6f083SXin LI /*
187ada6f083SXin LI * The ifm_ulist member of a struct ifmediareq is an int * on most systems,
188ada6f083SXin LI * but it's a uint64_t on newer versions of OpenBSD.
189ada6f083SXin LI *
190ada6f083SXin LI * We check this by checking whether IFM_GMASK is defined and > 2^32-1.
191ada6f083SXin LI */
192ada6f083SXin LI # if defined(IFM_GMASK) && IFM_GMASK > 0xFFFFFFFF
193ada6f083SXin LI # define IFM_ULIST_TYPE uint64_t
194ada6f083SXin LI # else
195ada6f083SXin LI # define IFM_ULIST_TYPE int
196ada6f083SXin LI # endif
197a8e07101SRui Paulo # endif
1988751327cSBill Fenner
199a8e07101SRui Paulo # if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
200a8e07101SRui Paulo static int find_802_11(struct bpf_dltlist *);
201a8e07101SRui Paulo
202a8e07101SRui Paulo # ifdef HAVE_BSD_IEEE80211
203a8e07101SRui Paulo static int monitor_mode(pcap_t *, int);
204a8e07101SRui Paulo # endif
205a8e07101SRui Paulo
206a8e07101SRui Paulo # if defined(__APPLE__)
20757e22627SCy Schubert static void remove_non_802_11(pcap_t *);
208a8e07101SRui Paulo static void remove_802_11(pcap_t *);
209a8e07101SRui Paulo # endif
210a8e07101SRui Paulo
211a8e07101SRui Paulo # endif /* defined(__APPLE__) || defined(HAVE_BSD_IEEE80211) */
212a8e07101SRui Paulo
213a8e07101SRui Paulo #endif /* BIOCGDLTLIST */
214a8e07101SRui Paulo
21515752fa8SXin LI #if defined(sun) && defined(LIFNAMSIZ) && defined(lifr_zoneid)
21615752fa8SXin LI #include <zone.h>
21715752fa8SXin LI #endif
21815752fa8SXin LI
219a8e07101SRui Paulo /*
220a8e07101SRui Paulo * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
221a8e07101SRui Paulo * don't get DLT_DOCSIS defined.
222a8e07101SRui Paulo */
223a8e07101SRui Paulo #ifndef DLT_DOCSIS
224a8e07101SRui Paulo #define DLT_DOCSIS 143
225a8e07101SRui Paulo #endif
226a8e07101SRui Paulo
227a8e07101SRui Paulo /*
228b00ab754SHans Petter Selasky * In some versions of macOS, we might not even get any of the
229b00ab754SHans Petter Selasky * 802.11-plus-radio-header DLT_'s defined, even though some
230b00ab754SHans Petter Selasky * of them are used by various Airport drivers in those versions.
231a8e07101SRui Paulo */
232a8e07101SRui Paulo #ifndef DLT_PRISM_HEADER
233a8e07101SRui Paulo #define DLT_PRISM_HEADER 119
234a8e07101SRui Paulo #endif
235a8e07101SRui Paulo #ifndef DLT_AIRONET_HEADER
236a8e07101SRui Paulo #define DLT_AIRONET_HEADER 120
237a8e07101SRui Paulo #endif
238a8e07101SRui Paulo #ifndef DLT_IEEE802_11_RADIO
239a8e07101SRui Paulo #define DLT_IEEE802_11_RADIO 127
240a8e07101SRui Paulo #endif
241a8e07101SRui Paulo #ifndef DLT_IEEE802_11_RADIO_AVS
242a8e07101SRui Paulo #define DLT_IEEE802_11_RADIO_AVS 163
243a8e07101SRui Paulo #endif
244a8e07101SRui Paulo
245a8e07101SRui Paulo static int pcap_can_set_rfmon_bpf(pcap_t *p);
246a8e07101SRui Paulo static int pcap_activate_bpf(pcap_t *p);
247feb4ecdbSBruce M Simpson static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
2485d18909fSSam Leffler static int pcap_setdirection_bpf(pcap_t *, pcap_direction_t);
249feb4ecdbSBruce M Simpson static int pcap_set_datalink_bpf(pcap_t *p, int dlt);
250feb4ecdbSBruce M Simpson
251a8e07101SRui Paulo /*
252d1e87331SXin LI * For zerocopy bpf, the setnonblock/getnonblock routines need to modify
253681ed54cSXin LI * pb->nonblock so we don't call select(2) if the pcap handle is in non-
254681ed54cSXin LI * blocking mode.
255a8e07101SRui Paulo */
256a8e07101SRui Paulo static int
pcap_getnonblock_bpf(pcap_t * p)257b00ab754SHans Petter Selasky pcap_getnonblock_bpf(pcap_t *p)
258a8e07101SRui Paulo {
259d1e87331SXin LI #ifdef HAVE_ZEROCOPY_BPF
260681ed54cSXin LI struct pcap_bpf *pb = p->priv;
261681ed54cSXin LI
262681ed54cSXin LI if (pb->zerocopy)
263681ed54cSXin LI return (pb->nonblock);
264d1e87331SXin LI #endif
265*afdbf109SJoseph Mingrone return (pcapint_getnonblock_fd(p));
266d1e87331SXin LI }
267a8e07101SRui Paulo
268a8e07101SRui Paulo static int
pcap_setnonblock_bpf(pcap_t * p,int nonblock)269b00ab754SHans Petter Selasky pcap_setnonblock_bpf(pcap_t *p, int nonblock)
270a8e07101SRui Paulo {
271d1e87331SXin LI #ifdef HAVE_ZEROCOPY_BPF
272681ed54cSXin LI struct pcap_bpf *pb = p->priv;
273681ed54cSXin LI
274681ed54cSXin LI if (pb->zerocopy) {
275681ed54cSXin LI pb->nonblock = nonblock;
276a8e07101SRui Paulo return (0);
277a8e07101SRui Paulo }
278d1e87331SXin LI #endif
279*afdbf109SJoseph Mingrone return (pcapint_setnonblock_fd(p, nonblock));
280a8e07101SRui Paulo }
281a8e07101SRui Paulo
282d1e87331SXin LI #ifdef HAVE_ZEROCOPY_BPF
283a8e07101SRui Paulo /*
284a8e07101SRui Paulo * Zero-copy BPF buffer routines to check for and acknowledge BPF data in
285a8e07101SRui Paulo * shared memory buffers.
286a8e07101SRui Paulo *
287a8e07101SRui Paulo * pcap_next_zbuf_shm(): Check for a newly available shared memory buffer,
288a8e07101SRui Paulo * and set up p->buffer and cc to reflect one if available. Notice that if
289a8e07101SRui Paulo * there was no prior buffer, we select zbuf1 as this will be the first
290a8e07101SRui Paulo * buffer filled for a fresh BPF session.
291a8e07101SRui Paulo */
292a8e07101SRui Paulo static int
pcap_next_zbuf_shm(pcap_t * p,int * cc)293a8e07101SRui Paulo pcap_next_zbuf_shm(pcap_t *p, int *cc)
294a8e07101SRui Paulo {
295681ed54cSXin LI struct pcap_bpf *pb = p->priv;
296a8e07101SRui Paulo struct bpf_zbuf_header *bzh;
297a8e07101SRui Paulo
298681ed54cSXin LI if (pb->zbuffer == pb->zbuf2 || pb->zbuffer == NULL) {
299681ed54cSXin LI bzh = (struct bpf_zbuf_header *)pb->zbuf1;
300a8e07101SRui Paulo if (bzh->bzh_user_gen !=
301a8e07101SRui Paulo atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
302681ed54cSXin LI pb->bzh = bzh;
303681ed54cSXin LI pb->zbuffer = (u_char *)pb->zbuf1;
304681ed54cSXin LI p->buffer = pb->zbuffer + sizeof(*bzh);
305a8e07101SRui Paulo *cc = bzh->bzh_kernel_len;
306a8e07101SRui Paulo return (1);
307a8e07101SRui Paulo }
308681ed54cSXin LI } else if (pb->zbuffer == pb->zbuf1) {
309681ed54cSXin LI bzh = (struct bpf_zbuf_header *)pb->zbuf2;
310a8e07101SRui Paulo if (bzh->bzh_user_gen !=
311a8e07101SRui Paulo atomic_load_acq_int(&bzh->bzh_kernel_gen)) {
312681ed54cSXin LI pb->bzh = bzh;
313681ed54cSXin LI pb->zbuffer = (u_char *)pb->zbuf2;
314681ed54cSXin LI p->buffer = pb->zbuffer + sizeof(*bzh);
315a8e07101SRui Paulo *cc = bzh->bzh_kernel_len;
316a8e07101SRui Paulo return (1);
317a8e07101SRui Paulo }
318a8e07101SRui Paulo }
319a8e07101SRui Paulo *cc = 0;
320a8e07101SRui Paulo return (0);
321a8e07101SRui Paulo }
322a8e07101SRui Paulo
323a8e07101SRui Paulo /*
324a8e07101SRui Paulo * pcap_next_zbuf() -- Similar to pcap_next_zbuf_shm(), except wait using
325a8e07101SRui Paulo * select() for data or a timeout, and possibly force rotation of the buffer
326a8e07101SRui Paulo * in the event we time out or are in immediate mode. Invoke the shared
327a8e07101SRui Paulo * memory check before doing system calls in order to avoid doing avoidable
328a8e07101SRui Paulo * work.
329a8e07101SRui Paulo */
330a8e07101SRui Paulo static int
pcap_next_zbuf(pcap_t * p,int * cc)331a8e07101SRui Paulo pcap_next_zbuf(pcap_t *p, int *cc)
332a8e07101SRui Paulo {
333681ed54cSXin LI struct pcap_bpf *pb = p->priv;
334a8e07101SRui Paulo struct bpf_zbuf bz;
335a8e07101SRui Paulo struct timeval tv;
336a8e07101SRui Paulo struct timespec cur;
337a8e07101SRui Paulo fd_set r_set;
338a8e07101SRui Paulo int data, r;
339a8e07101SRui Paulo int expire, tmout;
340a8e07101SRui Paulo
341a8e07101SRui Paulo #define TSTOMILLI(ts) (((ts)->tv_sec * 1000) + ((ts)->tv_nsec / 1000000))
342a8e07101SRui Paulo /*
343a8e07101SRui Paulo * Start out by seeing whether anything is waiting by checking the
344a8e07101SRui Paulo * next shared memory buffer for data.
345a8e07101SRui Paulo */
346a8e07101SRui Paulo data = pcap_next_zbuf_shm(p, cc);
347a8e07101SRui Paulo if (data)
348a8e07101SRui Paulo return (data);
349a8e07101SRui Paulo /*
350a8e07101SRui Paulo * If a previous sleep was interrupted due to signal delivery, make
351a8e07101SRui Paulo * sure that the timeout gets adjusted accordingly. This requires
352a8e07101SRui Paulo * that we analyze when the timeout should be been expired, and
353a8e07101SRui Paulo * subtract the current time from that. If after this operation,
354*afdbf109SJoseph Mingrone * our timeout is less than or equal to zero, handle it like a
355a8e07101SRui Paulo * regular timeout.
356a8e07101SRui Paulo */
357681ed54cSXin LI tmout = p->opt.timeout;
358a8e07101SRui Paulo if (tmout)
359a8e07101SRui Paulo (void) clock_gettime(CLOCK_MONOTONIC, &cur);
360681ed54cSXin LI if (pb->interrupted && p->opt.timeout) {
361681ed54cSXin LI expire = TSTOMILLI(&pb->firstsel) + p->opt.timeout;
362a8e07101SRui Paulo tmout = expire - TSTOMILLI(&cur);
363a8e07101SRui Paulo #undef TSTOMILLI
364a8e07101SRui Paulo if (tmout <= 0) {
365681ed54cSXin LI pb->interrupted = 0;
366a8e07101SRui Paulo data = pcap_next_zbuf_shm(p, cc);
367a8e07101SRui Paulo if (data)
368a8e07101SRui Paulo return (data);
369a8e07101SRui Paulo if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
370*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
371b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno, "BIOCROTZBUF");
372a8e07101SRui Paulo return (PCAP_ERROR);
373a8e07101SRui Paulo }
374a8e07101SRui Paulo return (pcap_next_zbuf_shm(p, cc));
375a8e07101SRui Paulo }
376a8e07101SRui Paulo }
377a8e07101SRui Paulo /*
378a8e07101SRui Paulo * No data in the buffer, so must use select() to wait for data or
379a8e07101SRui Paulo * the next timeout. Note that we only call select if the handle
380a8e07101SRui Paulo * is in blocking mode.
381a8e07101SRui Paulo */
382681ed54cSXin LI if (!pb->nonblock) {
383a8e07101SRui Paulo FD_ZERO(&r_set);
384a8e07101SRui Paulo FD_SET(p->fd, &r_set);
385a8e07101SRui Paulo if (tmout != 0) {
386a8e07101SRui Paulo tv.tv_sec = tmout / 1000;
387a8e07101SRui Paulo tv.tv_usec = (tmout * 1000) % 1000000;
388a8e07101SRui Paulo }
389a8e07101SRui Paulo r = select(p->fd + 1, &r_set, NULL, NULL,
390681ed54cSXin LI p->opt.timeout != 0 ? &tv : NULL);
391a8e07101SRui Paulo if (r < 0 && errno == EINTR) {
392681ed54cSXin LI if (!pb->interrupted && p->opt.timeout) {
393681ed54cSXin LI pb->interrupted = 1;
394681ed54cSXin LI pb->firstsel = cur;
395a8e07101SRui Paulo }
396a8e07101SRui Paulo return (0);
397a8e07101SRui Paulo } else if (r < 0) {
398*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
399b00ab754SHans Petter Selasky errno, "select");
400a8e07101SRui Paulo return (PCAP_ERROR);
401a8e07101SRui Paulo }
402a8e07101SRui Paulo }
403681ed54cSXin LI pb->interrupted = 0;
404a8e07101SRui Paulo /*
405a8e07101SRui Paulo * Check again for data, which may exist now that we've either been
406a8e07101SRui Paulo * woken up as a result of data or timed out. Try the "there's data"
407a8e07101SRui Paulo * case first since it doesn't require a system call.
408a8e07101SRui Paulo */
409a8e07101SRui Paulo data = pcap_next_zbuf_shm(p, cc);
410a8e07101SRui Paulo if (data)
411a8e07101SRui Paulo return (data);
412a8e07101SRui Paulo /*
413a8e07101SRui Paulo * Try forcing a buffer rotation to dislodge timed out or immediate
414a8e07101SRui Paulo * data.
415a8e07101SRui Paulo */
416a8e07101SRui Paulo if (ioctl(p->fd, BIOCROTZBUF, &bz) < 0) {
417*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
418b00ab754SHans Petter Selasky errno, "BIOCROTZBUF");
419a8e07101SRui Paulo return (PCAP_ERROR);
420a8e07101SRui Paulo }
421a8e07101SRui Paulo return (pcap_next_zbuf_shm(p, cc));
422a8e07101SRui Paulo }
423a8e07101SRui Paulo
424a8e07101SRui Paulo /*
425a8e07101SRui Paulo * Notify kernel that we are done with the buffer. We don't reset zbuffer so
426a8e07101SRui Paulo * that we know which buffer to use next time around.
427a8e07101SRui Paulo */
428a8e07101SRui Paulo static int
pcap_ack_zbuf(pcap_t * p)429a8e07101SRui Paulo pcap_ack_zbuf(pcap_t *p)
430a8e07101SRui Paulo {
431681ed54cSXin LI struct pcap_bpf *pb = p->priv;
432a8e07101SRui Paulo
433681ed54cSXin LI atomic_store_rel_int(&pb->bzh->bzh_user_gen,
434681ed54cSXin LI pb->bzh->bzh_kernel_gen);
435681ed54cSXin LI pb->bzh = NULL;
436a8e07101SRui Paulo p->buffer = NULL;
437a8e07101SRui Paulo return (0);
438a8e07101SRui Paulo }
439d1e87331SXin LI #endif /* HAVE_ZEROCOPY_BPF */
440a8e07101SRui Paulo
441a8e07101SRui Paulo pcap_t *
pcapint_create_interface(const char * device _U_,char * ebuf)442*afdbf109SJoseph Mingrone pcapint_create_interface(const char *device _U_, char *ebuf)
443a8e07101SRui Paulo {
444a8e07101SRui Paulo pcap_t *p;
445a8e07101SRui Paulo
4466f9cba8fSJoseph Mingrone p = PCAP_CREATE_COMMON(ebuf, struct pcap_bpf);
447a8e07101SRui Paulo if (p == NULL)
448a8e07101SRui Paulo return (NULL);
449a8e07101SRui Paulo
450a8e07101SRui Paulo p->activate_op = pcap_activate_bpf;
451a8e07101SRui Paulo p->can_set_rfmon_op = pcap_can_set_rfmon_bpf;
452a4d10fa9SJung-uk Kim #ifdef BIOCSTSTAMP
453a4d10fa9SJung-uk Kim /*
454a4d10fa9SJung-uk Kim * We claim that we support microsecond and nanosecond time
455a4d10fa9SJung-uk Kim * stamps.
456a4d10fa9SJung-uk Kim */
457a4d10fa9SJung-uk Kim p->tstamp_precision_list = malloc(2 * sizeof(u_int));
458a4d10fa9SJung-uk Kim if (p->tstamp_precision_list == NULL) {
459*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno,
460b00ab754SHans Petter Selasky "malloc");
461a4d10fa9SJung-uk Kim free(p);
4629c46493eSJung-uk Kim return (NULL);
463a4d10fa9SJung-uk Kim }
464a4d10fa9SJung-uk Kim p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO;
465a4d10fa9SJung-uk Kim p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO;
4666f9cba8fSJoseph Mingrone p->tstamp_precision_count = 2;
467a4d10fa9SJung-uk Kim #endif /* BIOCSTSTAMP */
468a8e07101SRui Paulo return (p);
469a8e07101SRui Paulo }
470a8e07101SRui Paulo
471d1e87331SXin LI /*
472d1e87331SXin LI * On success, returns a file descriptor for a BPF device.
473d1e87331SXin LI * On failure, returns a PCAP_ERROR_ value, and sets p->errbuf.
474d1e87331SXin LI */
475a8e07101SRui Paulo static int
bpf_open(char * errbuf)476ada6f083SXin LI bpf_open(char *errbuf)
477a8e07101SRui Paulo {
478b00ab754SHans Petter Selasky int fd = -1;
479b00ab754SHans Petter Selasky static const char cloning_device[] = "/dev/bpf";
4806f9cba8fSJoseph Mingrone u_int n = 0;
481a8e07101SRui Paulo char device[sizeof "/dev/bpf0000000000"];
482b00ab754SHans Petter Selasky static int no_cloning_bpf = 0;
483a8e07101SRui Paulo
484a8e07101SRui Paulo #ifdef _AIX
485a8e07101SRui Paulo /*
486a8e07101SRui Paulo * Load the bpf driver, if it isn't already loaded,
487a8e07101SRui Paulo * and create the BPF device entries, if they don't
488a8e07101SRui Paulo * already exist.
489a8e07101SRui Paulo */
490ada6f083SXin LI if (bpf_load(errbuf) == PCAP_ERROR)
491a8e07101SRui Paulo return (PCAP_ERROR);
492a8e07101SRui Paulo #endif
493a8e07101SRui Paulo
494b00ab754SHans Petter Selasky /*
495b00ab754SHans Petter Selasky * First, unless we've already tried opening /dev/bpf and
496b00ab754SHans Petter Selasky * gotten ENOENT, try opening /dev/bpf.
497b00ab754SHans Petter Selasky * If it fails with ENOENT, remember that, so we don't try
498b00ab754SHans Petter Selasky * again, and try /dev/bpfN.
499b00ab754SHans Petter Selasky */
500b00ab754SHans Petter Selasky if (!no_cloning_bpf &&
501b00ab754SHans Petter Selasky (fd = open(cloning_device, O_RDWR)) == -1 &&
502b00ab754SHans Petter Selasky ((errno != EACCES && errno != ENOENT) ||
503b00ab754SHans Petter Selasky (fd = open(cloning_device, O_RDONLY)) == -1)) {
504b00ab754SHans Petter Selasky if (errno != ENOENT) {
5056f9cba8fSJoseph Mingrone if (errno == EACCES) {
506a8e07101SRui Paulo fd = PCAP_ERROR_PERM_DENIED;
5076f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
5086f9cba8fSJoseph Mingrone "Attempt to open %s failed - root privileges may be required",
5096f9cba8fSJoseph Mingrone cloning_device);
5106f9cba8fSJoseph Mingrone } else {
511a8e07101SRui Paulo fd = PCAP_ERROR;
512*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf,
5136f9cba8fSJoseph Mingrone PCAP_ERRBUF_SIZE, errno,
5146f9cba8fSJoseph Mingrone "(cannot open device) %s", cloning_device);
5156f9cba8fSJoseph Mingrone }
516b00ab754SHans Petter Selasky return (fd);
517a8e07101SRui Paulo }
518b00ab754SHans Petter Selasky no_cloning_bpf = 1;
519b00ab754SHans Petter Selasky }
520b00ab754SHans Petter Selasky
521b00ab754SHans Petter Selasky if (no_cloning_bpf) {
522a8e07101SRui Paulo /*
523b00ab754SHans Petter Selasky * We don't have /dev/bpf.
524b00ab754SHans Petter Selasky * Go through all the /dev/bpfN minors and find one
525b00ab754SHans Petter Selasky * that isn't in use.
526a8e07101SRui Paulo */
527a8e07101SRui Paulo do {
5286f9cba8fSJoseph Mingrone (void)snprintf(device, sizeof(device), "/dev/bpf%u", n++);
529a8e07101SRui Paulo /*
530a8e07101SRui Paulo * Initially try a read/write open (to allow the inject
531a8e07101SRui Paulo * method to work). If that fails due to permission
532a8e07101SRui Paulo * issues, fall back to read-only. This allows a
533a8e07101SRui Paulo * non-root user to be granted specific access to pcap
534a8e07101SRui Paulo * capabilities via file permissions.
535a8e07101SRui Paulo *
536a8e07101SRui Paulo * XXX - we should have an API that has a flag that
537a8e07101SRui Paulo * controls whether to open read-only or read-write,
538a8e07101SRui Paulo * so that denial of permission to send (or inability
539a8e07101SRui Paulo * to send, if sending packets isn't supported on
540a8e07101SRui Paulo * the device in question) can be indicated at open
541a8e07101SRui Paulo * time.
542a8e07101SRui Paulo */
543a8e07101SRui Paulo fd = open(device, O_RDWR);
544a8e07101SRui Paulo if (fd == -1 && errno == EACCES)
545a8e07101SRui Paulo fd = open(device, O_RDONLY);
546a8e07101SRui Paulo } while (fd < 0 && errno == EBUSY);
547b00ab754SHans Petter Selasky }
548a8e07101SRui Paulo
549a8e07101SRui Paulo /*
550a8e07101SRui Paulo * XXX better message for all minors used
551a8e07101SRui Paulo */
552a8e07101SRui Paulo if (fd < 0) {
553d1e87331SXin LI switch (errno) {
554d1e87331SXin LI
555d1e87331SXin LI case ENOENT:
556d1e87331SXin LI if (n == 1) {
557d1e87331SXin LI /*
558d1e87331SXin LI * /dev/bpf0 doesn't exist, which
559d1e87331SXin LI * means we probably have no BPF
560d1e87331SXin LI * devices.
561d1e87331SXin LI */
562*afdbf109SJoseph Mingrone fd = PCAP_ERROR_CAPTURE_NOTSUP;
5636f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
564d1e87331SXin LI "(there are no BPF devices)");
565d1e87331SXin LI } else {
566d1e87331SXin LI /*
567d1e87331SXin LI * We got EBUSY on at least one
568d1e87331SXin LI * BPF device, so we have BPF
569d1e87331SXin LI * devices, but all the ones
570d1e87331SXin LI * that exist are busy.
571d1e87331SXin LI */
572*afdbf109SJoseph Mingrone fd = PCAP_ERROR;
5736f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
574d1e87331SXin LI "(all BPF devices are busy)");
575d1e87331SXin LI }
576d1e87331SXin LI break;
577d1e87331SXin LI
578d1e87331SXin LI case EACCES:
579d1e87331SXin LI /*
580d1e87331SXin LI * Got EACCES on the last device we tried,
581d1e87331SXin LI * and EBUSY on all devices before that,
582d1e87331SXin LI * if any.
583d1e87331SXin LI */
584d1e87331SXin LI fd = PCAP_ERROR_PERM_DENIED;
5856f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
5866f9cba8fSJoseph Mingrone "Attempt to open %s failed - root privileges may be required",
5876f9cba8fSJoseph Mingrone device);
588d1e87331SXin LI break;
589d1e87331SXin LI
590d1e87331SXin LI default:
591d1e87331SXin LI /*
592d1e87331SXin LI * Some other problem.
593d1e87331SXin LI */
594d1e87331SXin LI fd = PCAP_ERROR;
595*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
596b00ab754SHans Petter Selasky errno, "(cannot open BPF device) %s", device);
597d1e87331SXin LI break;
598d1e87331SXin LI }
599a8e07101SRui Paulo }
600a8e07101SRui Paulo
601a8e07101SRui Paulo return (fd);
602a8e07101SRui Paulo }
603a8e07101SRui Paulo
604ada6f083SXin LI /*
6056f9cba8fSJoseph Mingrone * Bind a network adapter to a BPF device, given a descriptor for the
6066f9cba8fSJoseph Mingrone * BPF device and the name of the network adapter.
6076f9cba8fSJoseph Mingrone *
6086f9cba8fSJoseph Mingrone * Use BIOCSETLIF if available (meaning "on Solaris"), as it supports
609*afdbf109SJoseph Mingrone * longer device names and binding to devices in other zones.
6106f9cba8fSJoseph Mingrone *
6116f9cba8fSJoseph Mingrone * If the name is longer than will fit, return PCAP_ERROR_NO_SUCH_DEVICE
6126f9cba8fSJoseph Mingrone * before trying to bind the interface, as there cannot be such a device.
6136f9cba8fSJoseph Mingrone *
6146f9cba8fSJoseph Mingrone * If the attempt succeeds, return BPF_BIND_SUCCEEDED.
6156f9cba8fSJoseph Mingrone *
6166f9cba8fSJoseph Mingrone * If the attempt fails:
6176f9cba8fSJoseph Mingrone *
6186f9cba8fSJoseph Mingrone * if it fails with ENOBUFS, return BPF_BIND_BUFFER_TOO_BIG, and
6196f9cba8fSJoseph Mingrone * fill in an error message, as the buffer being requested is too
620*afdbf109SJoseph Mingrone * large - our caller may try a smaller buffer if no buffer size
621*afdbf109SJoseph Mingrone * was explicitly specified.
6226f9cba8fSJoseph Mingrone *
623*afdbf109SJoseph Mingrone * otherwise, return the appropriate PCAP_ERROR_ code and
624*afdbf109SJoseph Mingrone * fill in an error message.
6256f9cba8fSJoseph Mingrone */
6266f9cba8fSJoseph Mingrone #define BPF_BIND_SUCCEEDED 0
6276f9cba8fSJoseph Mingrone #define BPF_BIND_BUFFER_TOO_BIG 1
6286f9cba8fSJoseph Mingrone
6296f9cba8fSJoseph Mingrone static int
bpf_bind(int fd,const char * name,char * errbuf)6306f9cba8fSJoseph Mingrone bpf_bind(int fd, const char *name, char *errbuf)
6316f9cba8fSJoseph Mingrone {
6326f9cba8fSJoseph Mingrone int status;
6336f9cba8fSJoseph Mingrone #ifdef LIFNAMSIZ
6346f9cba8fSJoseph Mingrone struct lifreq ifr;
635*afdbf109SJoseph Mingrone const char *ifname = name;
6366f9cba8fSJoseph Mingrone
637*afdbf109SJoseph Mingrone #if defined(ZONENAME_MAX) && defined(lifr_zoneid)
638*afdbf109SJoseph Mingrone char *zonesep;
639*afdbf109SJoseph Mingrone
640*afdbf109SJoseph Mingrone /*
641*afdbf109SJoseph Mingrone * We have support for zones.
642*afdbf109SJoseph Mingrone * Retrieve the zoneid of the zone we are currently executing in.
643*afdbf109SJoseph Mingrone */
644*afdbf109SJoseph Mingrone if ((ifr.lifr_zoneid = getzoneid()) == -1) {
645*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
646*afdbf109SJoseph Mingrone errno, "getzoneid()");
647*afdbf109SJoseph Mingrone return (PCAP_ERROR);
648*afdbf109SJoseph Mingrone }
649*afdbf109SJoseph Mingrone
650*afdbf109SJoseph Mingrone /*
651*afdbf109SJoseph Mingrone * Check if the given source datalink name has a '/' separated
652*afdbf109SJoseph Mingrone * zonename prefix string. The zonename prefixed source datalink can
653*afdbf109SJoseph Mingrone * be used by pcap consumers in the Solaris global zone to capture
654*afdbf109SJoseph Mingrone * traffic on datalinks in non-global zones. Non-global zones
655*afdbf109SJoseph Mingrone * do not have access to datalinks outside of their own namespace.
656*afdbf109SJoseph Mingrone */
657*afdbf109SJoseph Mingrone if ((zonesep = strchr(name, '/')) != NULL) {
658*afdbf109SJoseph Mingrone char *zname;
659*afdbf109SJoseph Mingrone ptrdiff_t znamelen;
660*afdbf109SJoseph Mingrone
661*afdbf109SJoseph Mingrone if (ifr.lifr_zoneid != GLOBAL_ZONEID) {
662*afdbf109SJoseph Mingrone /*
663*afdbf109SJoseph Mingrone * We treat this as a generic error rather
664*afdbf109SJoseph Mingrone * than as "permission denied" because
665*afdbf109SJoseph Mingrone * this isn't a case of "you don't have
666*afdbf109SJoseph Mingrone * enough permission to capture on this
667*afdbf109SJoseph Mingrone * device, so you'll have to do something
668*afdbf109SJoseph Mingrone * to get that permission" (such as
669*afdbf109SJoseph Mingrone * configuring the system to allow non-root
670*afdbf109SJoseph Mingrone * users to capture traffic), it's a case
671*afdbf109SJoseph Mingrone * of "nobody has permission to do this,
672*afdbf109SJoseph Mingrone * so there's nothing to do to fix it
673*afdbf109SJoseph Mingrone * other than running the capture program
674*afdbf109SJoseph Mingrone * in the global zone or the zone containing
675*afdbf109SJoseph Mingrone * the adapter".
676*afdbf109SJoseph Mingrone *
677*afdbf109SJoseph Mingrone * (And, yes, this is a real issue; for example,
678*afdbf109SJoseph Mingrone * Wireshark might make platform-specific suggestions
679*afdbf109SJoseph Mingrone * on how to fix a PCAP_ERROR_PERM_DENIED problem,
680*afdbf109SJoseph Mingrone * none of which will help here.)
681*afdbf109SJoseph Mingrone */
682*afdbf109SJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
683*afdbf109SJoseph Mingrone "zonename/linkname only valid in global zone.");
684*afdbf109SJoseph Mingrone return (PCAP_ERROR);
685*afdbf109SJoseph Mingrone }
686*afdbf109SJoseph Mingrone znamelen = zonesep - name;
687*afdbf109SJoseph Mingrone zname = malloc(znamelen + 1);
688*afdbf109SJoseph Mingrone if (zname == NULL) {
689*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
690*afdbf109SJoseph Mingrone errno, "malloc");
691*afdbf109SJoseph Mingrone return (PCAP_ERROR);
692*afdbf109SJoseph Mingrone }
693*afdbf109SJoseph Mingrone memcpy(zname, name, znamelen + 1);
694*afdbf109SJoseph Mingrone zname[znamelen] = '\0';
695*afdbf109SJoseph Mingrone ifr.lifr_zoneid = getzoneidbyname(zname);
696*afdbf109SJoseph Mingrone if (ifr.lifr_zoneid == -1) {
697*afdbf109SJoseph Mingrone switch (errno) {
698*afdbf109SJoseph Mingrone
699*afdbf109SJoseph Mingrone case EINVAL:
700*afdbf109SJoseph Mingrone case ENAMETOOLONG:
701*afdbf109SJoseph Mingrone /*
702*afdbf109SJoseph Mingrone * If the name's length exceeds
703*afdbf109SJoseph Mingrone * ZONENAMEMAX, clearly there cannot
704*afdbf109SJoseph Mingrone * be such a zone; it's not clear that
705*afdbf109SJoseph Mingrone * "that name's too long for a zone"
706*afdbf109SJoseph Mingrone * is more informative than "there's
707*afdbf109SJoseph Mingrone * no such zone".
708*afdbf109SJoseph Mingrone */
709*afdbf109SJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
710*afdbf109SJoseph Mingrone "There is no zone named \"%s\"",
711*afdbf109SJoseph Mingrone zname);
712*afdbf109SJoseph Mingrone
713*afdbf109SJoseph Mingrone /*
714*afdbf109SJoseph Mingrone * No such zone means the name
715*afdbf109SJoseph Mingrone * refers to a non-existent interface.
716*afdbf109SJoseph Mingrone */
717*afdbf109SJoseph Mingrone status = PCAP_ERROR_NO_SUCH_DEVICE;
718*afdbf109SJoseph Mingrone break;
719*afdbf109SJoseph Mingrone
720*afdbf109SJoseph Mingrone default:
721*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf,
722*afdbf109SJoseph Mingrone PCAP_ERRBUF_SIZE, errno,
723*afdbf109SJoseph Mingrone "getzoneidbyname(%s)", zname);
724*afdbf109SJoseph Mingrone status = PCAP_ERROR;
725*afdbf109SJoseph Mingrone break;
726*afdbf109SJoseph Mingrone }
727*afdbf109SJoseph Mingrone free(zname);
728*afdbf109SJoseph Mingrone return (status);
729*afdbf109SJoseph Mingrone }
730*afdbf109SJoseph Mingrone free(zname);
731*afdbf109SJoseph Mingrone
732*afdbf109SJoseph Mingrone /*
733*afdbf109SJoseph Mingrone * To bind to this interface, we set the ifr.lifr_zoneid
734*afdbf109SJoseph Mingrone * to the zone ID of its zone (done above), and we set
735*afdbf109SJoseph Mingrone * ifr.lifr_name to the name of the interface within that
736*afdbf109SJoseph Mingrone * zone (done below, using ifname).
737*afdbf109SJoseph Mingrone */
738*afdbf109SJoseph Mingrone ifname = zonesep + 1;
739*afdbf109SJoseph Mingrone }
740*afdbf109SJoseph Mingrone #endif
741*afdbf109SJoseph Mingrone
742*afdbf109SJoseph Mingrone if (strlen(ifname) >= sizeof(ifr.lifr_name)) {
7436f9cba8fSJoseph Mingrone /* The name is too long, so it can't possibly exist. */
7446f9cba8fSJoseph Mingrone return (PCAP_ERROR_NO_SUCH_DEVICE);
7456f9cba8fSJoseph Mingrone }
746*afdbf109SJoseph Mingrone (void)pcapint_strlcpy(ifr.lifr_name, ifname, sizeof(ifr.lifr_name));
7476f9cba8fSJoseph Mingrone status = ioctl(fd, BIOCSETLIF, (caddr_t)&ifr);
7486f9cba8fSJoseph Mingrone #else
7496f9cba8fSJoseph Mingrone struct ifreq ifr;
7506f9cba8fSJoseph Mingrone
7516f9cba8fSJoseph Mingrone if (strlen(name) >= sizeof(ifr.ifr_name)) {
7526f9cba8fSJoseph Mingrone /* The name is too long, so it can't possibly exist. */
7536f9cba8fSJoseph Mingrone return (PCAP_ERROR_NO_SUCH_DEVICE);
7546f9cba8fSJoseph Mingrone }
755*afdbf109SJoseph Mingrone (void)pcapint_strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
7566f9cba8fSJoseph Mingrone status = ioctl(fd, BIOCSETIF, (caddr_t)&ifr);
7576f9cba8fSJoseph Mingrone #endif
7586f9cba8fSJoseph Mingrone
7596f9cba8fSJoseph Mingrone if (status < 0) {
7606f9cba8fSJoseph Mingrone switch (errno) {
7616f9cba8fSJoseph Mingrone
762*afdbf109SJoseph Mingrone #if defined(HAVE_SOLARIS)
763*afdbf109SJoseph Mingrone /*
764*afdbf109SJoseph Mingrone * For some reason, Solaris 11 appears to return ESRCH
765*afdbf109SJoseph Mingrone * for unknown devices.
766*afdbf109SJoseph Mingrone */
767*afdbf109SJoseph Mingrone case ESRCH:
768*afdbf109SJoseph Mingrone #else
769*afdbf109SJoseph Mingrone /*
770*afdbf109SJoseph Mingrone * The *BSDs (including CupertinoBSD a/k/a Darwin)
771*afdbf109SJoseph Mingrone * return ENXIO for unknown devices.
772*afdbf109SJoseph Mingrone */
7736f9cba8fSJoseph Mingrone case ENXIO:
774*afdbf109SJoseph Mingrone #endif
7756f9cba8fSJoseph Mingrone /*
7766f9cba8fSJoseph Mingrone * There's no such device.
7776f9cba8fSJoseph Mingrone *
7786f9cba8fSJoseph Mingrone * There's nothing more to say, so clear out the
7796f9cba8fSJoseph Mingrone * error message.
7806f9cba8fSJoseph Mingrone */
7816f9cba8fSJoseph Mingrone errbuf[0] = '\0';
7826f9cba8fSJoseph Mingrone return (PCAP_ERROR_NO_SUCH_DEVICE);
7836f9cba8fSJoseph Mingrone
7846f9cba8fSJoseph Mingrone case ENETDOWN:
7856f9cba8fSJoseph Mingrone /*
7866f9cba8fSJoseph Mingrone * Return a "network down" indication, so that
7876f9cba8fSJoseph Mingrone * the application can report that rather than
7886f9cba8fSJoseph Mingrone * saying we had a mysterious failure and
7896f9cba8fSJoseph Mingrone * suggest that they report a problem to the
7906f9cba8fSJoseph Mingrone * libpcap developers.
7916f9cba8fSJoseph Mingrone */
7926f9cba8fSJoseph Mingrone return (PCAP_ERROR_IFACE_NOT_UP);
7936f9cba8fSJoseph Mingrone
7946f9cba8fSJoseph Mingrone case ENOBUFS:
7956f9cba8fSJoseph Mingrone /*
7966f9cba8fSJoseph Mingrone * The buffer size is too big.
7976f9cba8fSJoseph Mingrone * Return a special indication so that, if we're
7986f9cba8fSJoseph Mingrone * trying to crank the buffer size down, we know
7996f9cba8fSJoseph Mingrone * we have to continue; add an error message that
8006f9cba8fSJoseph Mingrone * tells the user what needs to be fixed.
8016f9cba8fSJoseph Mingrone */
802*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
8036f9cba8fSJoseph Mingrone errno, "The requested buffer size for %s is too large",
8046f9cba8fSJoseph Mingrone name);
8056f9cba8fSJoseph Mingrone return (BPF_BIND_BUFFER_TOO_BIG);
8066f9cba8fSJoseph Mingrone
8076f9cba8fSJoseph Mingrone default:
808*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
8096f9cba8fSJoseph Mingrone errno, "Binding interface %s to BPF device failed",
8106f9cba8fSJoseph Mingrone name);
8116f9cba8fSJoseph Mingrone return (PCAP_ERROR);
8126f9cba8fSJoseph Mingrone }
8136f9cba8fSJoseph Mingrone }
8146f9cba8fSJoseph Mingrone return (BPF_BIND_SUCCEEDED);
8156f9cba8fSJoseph Mingrone }
8166f9cba8fSJoseph Mingrone
8176f9cba8fSJoseph Mingrone /*
818ada6f083SXin LI * Open and bind to a device; used if we're not actually going to use
819ada6f083SXin LI * the device, but are just testing whether it can be opened, or opening
820ada6f083SXin LI * it to get information about it.
821ada6f083SXin LI *
822ada6f083SXin LI * Returns an error code on failure (always negative), and an FD for
823ada6f083SXin LI * the now-bound BPF device on success (always non-negative).
824ada6f083SXin LI */
825ada6f083SXin LI static int
bpf_open_and_bind(const char * name,char * errbuf)826ada6f083SXin LI bpf_open_and_bind(const char *name, char *errbuf)
827ada6f083SXin LI {
828ada6f083SXin LI int fd;
8296f9cba8fSJoseph Mingrone int status;
830ada6f083SXin LI
831ada6f083SXin LI /*
832ada6f083SXin LI * First, open a BPF device.
833ada6f083SXin LI */
834ada6f083SXin LI fd = bpf_open(errbuf);
835ada6f083SXin LI if (fd < 0)
836ada6f083SXin LI return (fd); /* fd is the appropriate error code */
837ada6f083SXin LI
838ada6f083SXin LI /*
839ada6f083SXin LI * Now bind to the device.
840ada6f083SXin LI */
8416f9cba8fSJoseph Mingrone status = bpf_bind(fd, name, errbuf);
8426f9cba8fSJoseph Mingrone if (status != BPF_BIND_SUCCEEDED) {
8436f9cba8fSJoseph Mingrone close(fd);
8446f9cba8fSJoseph Mingrone if (status == BPF_BIND_BUFFER_TOO_BIG) {
845ada6f083SXin LI /*
8466f9cba8fSJoseph Mingrone * We didn't specify a buffer size, so
8476f9cba8fSJoseph Mingrone * this *really* shouldn't fail because
8486f9cba8fSJoseph Mingrone * there's no buffer space. Fail.
849ada6f083SXin LI */
850ada6f083SXin LI return (PCAP_ERROR);
851ada6f083SXin LI }
8526f9cba8fSJoseph Mingrone return (status);
853ada6f083SXin LI }
854ada6f083SXin LI
855ada6f083SXin LI /*
856ada6f083SXin LI * Success.
857ada6f083SXin LI */
858ada6f083SXin LI return (fd);
859ada6f083SXin LI }
860ada6f083SXin LI
8616f9cba8fSJoseph Mingrone #ifdef __APPLE__
8626f9cba8fSJoseph Mingrone static int
device_exists(int fd,const char * name,char * errbuf)8636f9cba8fSJoseph Mingrone device_exists(int fd, const char *name, char *errbuf)
8646f9cba8fSJoseph Mingrone {
8656f9cba8fSJoseph Mingrone int status;
8666f9cba8fSJoseph Mingrone struct ifreq ifr;
8676f9cba8fSJoseph Mingrone
8686f9cba8fSJoseph Mingrone if (strlen(name) >= sizeof(ifr.ifr_name)) {
8696f9cba8fSJoseph Mingrone /* The name is too long, so it can't possibly exist. */
8706f9cba8fSJoseph Mingrone return (PCAP_ERROR_NO_SUCH_DEVICE);
8716f9cba8fSJoseph Mingrone }
872*afdbf109SJoseph Mingrone (void)pcapint_strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
8736f9cba8fSJoseph Mingrone status = ioctl(fd, SIOCGIFFLAGS, (caddr_t)&ifr);
8746f9cba8fSJoseph Mingrone
8756f9cba8fSJoseph Mingrone if (status < 0) {
8766f9cba8fSJoseph Mingrone if (errno == ENXIO || errno == EINVAL) {
8776f9cba8fSJoseph Mingrone /*
8786f9cba8fSJoseph Mingrone * macOS and *BSD return one of those two
8796f9cba8fSJoseph Mingrone * errors if the device doesn't exist.
8806f9cba8fSJoseph Mingrone * Don't fill in an error, as this is
8816f9cba8fSJoseph Mingrone * an "expected" condition.
8826f9cba8fSJoseph Mingrone */
8836f9cba8fSJoseph Mingrone return (PCAP_ERROR_NO_SUCH_DEVICE);
8846f9cba8fSJoseph Mingrone }
8856f9cba8fSJoseph Mingrone
8866f9cba8fSJoseph Mingrone /*
8876f9cba8fSJoseph Mingrone * Some other error - provide a message for it, as
8886f9cba8fSJoseph Mingrone * it's "unexpected".
8896f9cba8fSJoseph Mingrone */
890*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
8916f9cba8fSJoseph Mingrone "Can't get interface flags on %s", name);
8926f9cba8fSJoseph Mingrone return (PCAP_ERROR);
8936f9cba8fSJoseph Mingrone }
8946f9cba8fSJoseph Mingrone
8956f9cba8fSJoseph Mingrone /*
8966f9cba8fSJoseph Mingrone * The device exists.
8976f9cba8fSJoseph Mingrone */
8986f9cba8fSJoseph Mingrone return (0);
8996f9cba8fSJoseph Mingrone }
9006f9cba8fSJoseph Mingrone #endif
9016f9cba8fSJoseph Mingrone
902a8e07101SRui Paulo #ifdef BIOCGDLTLIST
903a8e07101SRui Paulo static int
get_dlt_list(int fd,int v,struct bpf_dltlist * bdlp,char * ebuf)904a8e07101SRui Paulo get_dlt_list(int fd, int v, struct bpf_dltlist *bdlp, char *ebuf)
905a8e07101SRui Paulo {
906a8e07101SRui Paulo memset(bdlp, 0, sizeof(*bdlp));
907a8e07101SRui Paulo if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) == 0) {
908a8e07101SRui Paulo u_int i;
909a8e07101SRui Paulo int is_ethernet;
910a8e07101SRui Paulo
911a8e07101SRui Paulo bdlp->bfl_list = (u_int *) malloc(sizeof(u_int) * (bdlp->bfl_len + 1));
912a8e07101SRui Paulo if (bdlp->bfl_list == NULL) {
913*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
914b00ab754SHans Petter Selasky errno, "malloc");
915a8e07101SRui Paulo return (PCAP_ERROR);
916a8e07101SRui Paulo }
917a8e07101SRui Paulo
918a8e07101SRui Paulo if (ioctl(fd, BIOCGDLTLIST, (caddr_t)bdlp) < 0) {
919*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
920b00ab754SHans Petter Selasky errno, "BIOCGDLTLIST");
921a8e07101SRui Paulo free(bdlp->bfl_list);
922a8e07101SRui Paulo return (PCAP_ERROR);
923a8e07101SRui Paulo }
924a8e07101SRui Paulo
925a8e07101SRui Paulo /*
926a8e07101SRui Paulo * OK, for real Ethernet devices, add DLT_DOCSIS to the
927a8e07101SRui Paulo * list, so that an application can let you choose it,
928a8e07101SRui Paulo * in case you're capturing DOCSIS traffic that a Cisco
929a8e07101SRui Paulo * Cable Modem Termination System is putting out onto
930a8e07101SRui Paulo * an Ethernet (it doesn't put an Ethernet header onto
931a8e07101SRui Paulo * the wire, it puts raw DOCSIS frames out on the wire
932a8e07101SRui Paulo * inside the low-level Ethernet framing).
933a8e07101SRui Paulo *
934a8e07101SRui Paulo * A "real Ethernet device" is defined here as a device
935a8e07101SRui Paulo * that has a link-layer type of DLT_EN10MB and that has
936a8e07101SRui Paulo * no alternate link-layer types; that's done to exclude
937a8e07101SRui Paulo * 802.11 interfaces (which might or might not be the
938a8e07101SRui Paulo * right thing to do, but I suspect it is - Ethernet <->
939a8e07101SRui Paulo * 802.11 bridges would probably badly mishandle frames
940a8e07101SRui Paulo * that don't have Ethernet headers).
941a0ee43a1SRui Paulo *
942a0ee43a1SRui Paulo * On Solaris with BPF, Ethernet devices also offer
943a0ee43a1SRui Paulo * DLT_IPNET, so we, if DLT_IPNET is defined, we don't
944a0ee43a1SRui Paulo * treat it as an indication that the device isn't an
945a0ee43a1SRui Paulo * Ethernet.
946a8e07101SRui Paulo */
947a8e07101SRui Paulo if (v == DLT_EN10MB) {
948a8e07101SRui Paulo is_ethernet = 1;
949a8e07101SRui Paulo for (i = 0; i < bdlp->bfl_len; i++) {
950a0ee43a1SRui Paulo if (bdlp->bfl_list[i] != DLT_EN10MB
951a0ee43a1SRui Paulo #ifdef DLT_IPNET
952a0ee43a1SRui Paulo && bdlp->bfl_list[i] != DLT_IPNET
953a0ee43a1SRui Paulo #endif
954a0ee43a1SRui Paulo ) {
955a8e07101SRui Paulo is_ethernet = 0;
956a8e07101SRui Paulo break;
957a8e07101SRui Paulo }
958a8e07101SRui Paulo }
959a8e07101SRui Paulo if (is_ethernet) {
960a8e07101SRui Paulo /*
961a8e07101SRui Paulo * We reserved one more slot at the end of
962a8e07101SRui Paulo * the list.
963a8e07101SRui Paulo */
964a8e07101SRui Paulo bdlp->bfl_list[bdlp->bfl_len] = DLT_DOCSIS;
965a8e07101SRui Paulo bdlp->bfl_len++;
966a8e07101SRui Paulo }
967a8e07101SRui Paulo }
968a8e07101SRui Paulo } else {
969a8e07101SRui Paulo /*
970a8e07101SRui Paulo * EINVAL just means "we don't support this ioctl on
971a8e07101SRui Paulo * this device"; don't treat it as an error.
972a8e07101SRui Paulo */
973a8e07101SRui Paulo if (errno != EINVAL) {
974*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
975b00ab754SHans Petter Selasky errno, "BIOCGDLTLIST");
976a8e07101SRui Paulo return (PCAP_ERROR);
977a8e07101SRui Paulo }
978a8e07101SRui Paulo }
979a8e07101SRui Paulo return (0);
980a8e07101SRui Paulo }
981a8e07101SRui Paulo #endif
982a8e07101SRui Paulo
98357e22627SCy Schubert #if defined(__APPLE__)
984a8e07101SRui Paulo static int
pcap_can_set_rfmon_bpf(pcap_t * p)985a8e07101SRui Paulo pcap_can_set_rfmon_bpf(pcap_t *p)
986a8e07101SRui Paulo {
987a8e07101SRui Paulo struct utsname osinfo;
988a8e07101SRui Paulo int fd;
989a8e07101SRui Paulo #ifdef BIOCGDLTLIST
990a8e07101SRui Paulo struct bpf_dltlist bdl;
9916f9cba8fSJoseph Mingrone int err;
992a8e07101SRui Paulo #endif
993a8e07101SRui Paulo
994a8e07101SRui Paulo /*
995b00ab754SHans Petter Selasky * The joys of monitor mode on Mac OS X/OS X/macOS.
996a8e07101SRui Paulo *
997a8e07101SRui Paulo * Prior to 10.4, it's not supported at all.
998a8e07101SRui Paulo *
999a8e07101SRui Paulo * In 10.4, if adapter enN supports monitor mode, there's a
1000a8e07101SRui Paulo * wltN adapter corresponding to it; you open it, instead of
1001a8e07101SRui Paulo * enN, to get monitor mode. You get whatever link-layer
1002a8e07101SRui Paulo * headers it supplies.
1003a8e07101SRui Paulo *
1004a8e07101SRui Paulo * In 10.5, and, we assume, later releases, if adapter enN
1005a8e07101SRui Paulo * supports monitor mode, it offers, among its selectable
1006a8e07101SRui Paulo * DLT_ values, values that let you get the 802.11 header;
1007a8e07101SRui Paulo * selecting one of those values puts the adapter into monitor
1008a8e07101SRui Paulo * mode (i.e., you can't get 802.11 headers except in monitor
1009a8e07101SRui Paulo * mode, and you can't get Ethernet headers in monitor mode).
1010a8e07101SRui Paulo */
1011a8e07101SRui Paulo if (uname(&osinfo) == -1) {
1012a8e07101SRui Paulo /*
1013a8e07101SRui Paulo * Can't get the OS version; just say "no".
1014a8e07101SRui Paulo */
1015a8e07101SRui Paulo return (0);
1016a8e07101SRui Paulo }
1017a8e07101SRui Paulo /*
1018a8e07101SRui Paulo * We assume osinfo.sysname is "Darwin", because
1019a8e07101SRui Paulo * __APPLE__ is defined. We just check the version.
1020a8e07101SRui Paulo */
1021a8e07101SRui Paulo if (osinfo.release[0] < '8' && osinfo.release[1] == '.') {
1022a8e07101SRui Paulo /*
1023a8e07101SRui Paulo * 10.3 (Darwin 7.x) or earlier.
1024a8e07101SRui Paulo * Monitor mode not supported.
1025a8e07101SRui Paulo */
1026a8e07101SRui Paulo return (0);
1027a8e07101SRui Paulo }
1028a8e07101SRui Paulo if (osinfo.release[0] == '8' && osinfo.release[1] == '.') {
10296f9cba8fSJoseph Mingrone char *wlt_name;
10306f9cba8fSJoseph Mingrone int status;
10316f9cba8fSJoseph Mingrone
1032a8e07101SRui Paulo /*
1033a8e07101SRui Paulo * 10.4 (Darwin 8.x). s/en/wlt/, and check
1034a8e07101SRui Paulo * whether the device exists.
1035a8e07101SRui Paulo */
1036ada6f083SXin LI if (strncmp(p->opt.device, "en", 2) != 0) {
1037a8e07101SRui Paulo /*
1038a8e07101SRui Paulo * Not an enN device; no monitor mode.
1039a8e07101SRui Paulo */
1040a8e07101SRui Paulo return (0);
1041a8e07101SRui Paulo }
1042a8e07101SRui Paulo fd = socket(AF_INET, SOCK_DGRAM, 0);
1043a8e07101SRui Paulo if (fd == -1) {
1044*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1045b00ab754SHans Petter Selasky errno, "socket");
1046a8e07101SRui Paulo return (PCAP_ERROR);
1047a8e07101SRui Paulo }
1048*afdbf109SJoseph Mingrone if (pcapint_asprintf(&wlt_name, "wlt%s", p->opt.device + 2) == -1) {
1049*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
10506f9cba8fSJoseph Mingrone errno, "malloc");
1051a8e07101SRui Paulo close(fd);
10526f9cba8fSJoseph Mingrone return (PCAP_ERROR);
1053a8e07101SRui Paulo }
10546f9cba8fSJoseph Mingrone status = device_exists(fd, wlt_name, p->errbuf);
10556f9cba8fSJoseph Mingrone free(wlt_name);
1056a8e07101SRui Paulo close(fd);
10576f9cba8fSJoseph Mingrone if (status != 0) {
10586f9cba8fSJoseph Mingrone if (status == PCAP_ERROR_NO_SUCH_DEVICE)
10596f9cba8fSJoseph Mingrone return (0);
10606f9cba8fSJoseph Mingrone
10616f9cba8fSJoseph Mingrone /*
10626f9cba8fSJoseph Mingrone * Error.
10636f9cba8fSJoseph Mingrone */
10646f9cba8fSJoseph Mingrone return (status);
10656f9cba8fSJoseph Mingrone }
1066a8e07101SRui Paulo return (1);
1067a8e07101SRui Paulo }
1068a8e07101SRui Paulo
1069a8e07101SRui Paulo #ifdef BIOCGDLTLIST
1070a8e07101SRui Paulo /*
1071a8e07101SRui Paulo * Everything else is 10.5 or later; for those,
1072a8e07101SRui Paulo * we just open the enN device, and check whether
1073a8e07101SRui Paulo * we have any 802.11 devices.
1074a8e07101SRui Paulo *
1075a8e07101SRui Paulo * First, open a BPF device.
1076a8e07101SRui Paulo */
1077ada6f083SXin LI fd = bpf_open(p->errbuf);
1078a8e07101SRui Paulo if (fd < 0)
1079d1e87331SXin LI return (fd); /* fd is the appropriate error code */
1080a8e07101SRui Paulo
1081a8e07101SRui Paulo /*
1082a8e07101SRui Paulo * Now bind to the device.
1083a8e07101SRui Paulo */
10846f9cba8fSJoseph Mingrone err = bpf_bind(fd, p->opt.device, p->errbuf);
10856f9cba8fSJoseph Mingrone if (err != BPF_BIND_SUCCEEDED) {
10866f9cba8fSJoseph Mingrone close(fd);
10876f9cba8fSJoseph Mingrone if (err == BPF_BIND_BUFFER_TOO_BIG) {
1088d1e87331SXin LI /*
10896f9cba8fSJoseph Mingrone * We didn't specify a buffer size, so
10906f9cba8fSJoseph Mingrone * this *really* shouldn't fail because
10916f9cba8fSJoseph Mingrone * there's no buffer space. Fail.
1092d1e87331SXin LI */
1093a8e07101SRui Paulo return (PCAP_ERROR);
1094a8e07101SRui Paulo }
10956f9cba8fSJoseph Mingrone return (err);
1096a8e07101SRui Paulo }
1097a8e07101SRui Paulo
1098a8e07101SRui Paulo /*
1099a8e07101SRui Paulo * We know the default link type -- now determine all the DLTs
1100a8e07101SRui Paulo * this interface supports. If this fails with EINVAL, it's
1101a8e07101SRui Paulo * not fatal; we just don't get to use the feature later.
1102a8e07101SRui Paulo * (We don't care about DLT_DOCSIS, so we pass DLT_NULL
1103a8e07101SRui Paulo * as the default DLT for this adapter.)
1104a8e07101SRui Paulo */
1105a8e07101SRui Paulo if (get_dlt_list(fd, DLT_NULL, &bdl, p->errbuf) == PCAP_ERROR) {
1106a8e07101SRui Paulo close(fd);
1107a8e07101SRui Paulo return (PCAP_ERROR);
1108a8e07101SRui Paulo }
1109a8e07101SRui Paulo if (find_802_11(&bdl) != -1) {
1110a8e07101SRui Paulo /*
1111a8e07101SRui Paulo * We have an 802.11 DLT, so we can set monitor mode.
1112a8e07101SRui Paulo */
1113a8e07101SRui Paulo free(bdl.bfl_list);
1114a8e07101SRui Paulo close(fd);
1115a8e07101SRui Paulo return (1);
1116a8e07101SRui Paulo }
1117a8e07101SRui Paulo free(bdl.bfl_list);
1118ada6f083SXin LI close(fd);
1119a8e07101SRui Paulo #endif /* BIOCGDLTLIST */
1120a8e07101SRui Paulo return (0);
112157e22627SCy Schubert }
1122a8e07101SRui Paulo #elif defined(HAVE_BSD_IEEE80211)
112357e22627SCy Schubert static int
pcap_can_set_rfmon_bpf(pcap_t * p)112457e22627SCy Schubert pcap_can_set_rfmon_bpf(pcap_t *p)
112557e22627SCy Schubert {
1126a8e07101SRui Paulo int ret;
1127a8e07101SRui Paulo
1128a8e07101SRui Paulo ret = monitor_mode(p, 0);
1129a8e07101SRui Paulo if (ret == PCAP_ERROR_RFMON_NOTSUP)
1130a8e07101SRui Paulo return (0); /* not an error, just a "can't do" */
1131a8e07101SRui Paulo if (ret == 0)
1132a8e07101SRui Paulo return (1); /* success */
1133a8e07101SRui Paulo return (ret);
1134a8e07101SRui Paulo }
113557e22627SCy Schubert #else
113657e22627SCy Schubert static int
pcap_can_set_rfmon_bpf(pcap_t * p _U_)113757e22627SCy Schubert pcap_can_set_rfmon_bpf(pcap_t *p _U_)
113857e22627SCy Schubert {
113957e22627SCy Schubert return (0);
114057e22627SCy Schubert }
114157e22627SCy Schubert #endif
1142a8e07101SRui Paulo
1143feb4ecdbSBruce M Simpson static int
pcap_stats_bpf(pcap_t * p,struct pcap_stat * ps)1144feb4ecdbSBruce M Simpson pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
11458cf6c252SPaul Traina {
11468cf6c252SPaul Traina struct bpf_stat s;
11478cf6c252SPaul Traina
11480a94d38fSBill Fenner /*
11490a94d38fSBill Fenner * "ps_recv" counts packets handed to the filter, not packets
11500a94d38fSBill Fenner * that passed the filter. This includes packets later dropped
11510a94d38fSBill Fenner * because we ran out of buffer space.
11520a94d38fSBill Fenner *
11530a94d38fSBill Fenner * "ps_drop" counts packets dropped inside the BPF device
11540a94d38fSBill Fenner * because we ran out of buffer space. It doesn't count
11550a94d38fSBill Fenner * packets dropped by the interface driver. It counts
11560a94d38fSBill Fenner * only packets that passed the filter.
11570a94d38fSBill Fenner *
11580a94d38fSBill Fenner * Both statistics include packets not yet read from the kernel
11590a94d38fSBill Fenner * by libpcap, and thus not yet seen by the application.
11600a94d38fSBill Fenner */
11618cf6c252SPaul Traina if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
1162*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1163b00ab754SHans Petter Selasky errno, "BIOCGSTATS");
1164a8e07101SRui Paulo return (PCAP_ERROR);
11658cf6c252SPaul Traina }
11668cf6c252SPaul Traina
1167*afdbf109SJoseph Mingrone /*
1168*afdbf109SJoseph Mingrone * On illumos, NetBSD and Solaris these values are 64-bit, but struct
1169*afdbf109SJoseph Mingrone * pcap_stat is what it is, so the integer precision loss is expected.
1170*afdbf109SJoseph Mingrone */
1171*afdbf109SJoseph Mingrone ps->ps_recv = (u_int)s.bs_recv;
1172*afdbf109SJoseph Mingrone ps->ps_drop = (u_int)s.bs_drop;
1173a0ee43a1SRui Paulo ps->ps_ifdrop = 0;
11748cf6c252SPaul Traina return (0);
11758cf6c252SPaul Traina }
11768cf6c252SPaul Traina
1177feb4ecdbSBruce M Simpson static int
pcap_read_bpf(pcap_t * p,int cnt,pcap_handler callback,u_char * user)1178feb4ecdbSBruce M Simpson pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
11798cf6c252SPaul Traina {
1180681ed54cSXin LI struct pcap_bpf *pb = p->priv;
1181*afdbf109SJoseph Mingrone ssize_t cc;
11828cf6c252SPaul Traina int n = 0;
11838cf6c252SPaul Traina register u_char *bp, *ep;
118404fb2745SSam Leffler u_char *datap;
118504fb2745SSam Leffler #ifdef PCAP_FDDIPAD
1186ada6f083SXin LI register u_int pad;
118704fb2745SSam Leffler #endif
1188a8e07101SRui Paulo #ifdef HAVE_ZEROCOPY_BPF
1189a8e07101SRui Paulo int i;
1190a8e07101SRui Paulo #endif
11918cf6c252SPaul Traina
11928cf6c252SPaul Traina again:
1193feb4ecdbSBruce M Simpson /*
1194feb4ecdbSBruce M Simpson * Has "pcap_breakloop()" been called?
1195feb4ecdbSBruce M Simpson */
1196feb4ecdbSBruce M Simpson if (p->break_loop) {
1197feb4ecdbSBruce M Simpson /*
1198feb4ecdbSBruce M Simpson * Yes - clear the flag that indicates that it
1199a8e07101SRui Paulo * has, and return PCAP_ERROR_BREAK to indicate
1200a8e07101SRui Paulo * that we were told to break out of the loop.
1201feb4ecdbSBruce M Simpson */
1202feb4ecdbSBruce M Simpson p->break_loop = 0;
1203a8e07101SRui Paulo return (PCAP_ERROR_BREAK);
1204feb4ecdbSBruce M Simpson }
12058cf6c252SPaul Traina cc = p->cc;
12068cf6c252SPaul Traina if (p->cc == 0) {
1207154bbe41SChristian S.J. Peron /*
1208154bbe41SChristian S.J. Peron * When reading without zero-copy from a file descriptor, we
1209154bbe41SChristian S.J. Peron * use a single buffer and return a length of data in the
1210154bbe41SChristian S.J. Peron * buffer. With zero-copy, we update the p->buffer pointer
1211154bbe41SChristian S.J. Peron * to point at whatever underlying buffer contains the next
1212154bbe41SChristian S.J. Peron * data and update cc to reflect the data found in the
1213154bbe41SChristian S.J. Peron * buffer.
1214154bbe41SChristian S.J. Peron */
1215a8e07101SRui Paulo #ifdef HAVE_ZEROCOPY_BPF
1216681ed54cSXin LI if (pb->zerocopy) {
1217154bbe41SChristian S.J. Peron if (p->buffer != NULL)
1218154bbe41SChristian S.J. Peron pcap_ack_zbuf(p);
1219154bbe41SChristian S.J. Peron i = pcap_next_zbuf(p, &cc);
1220154bbe41SChristian S.J. Peron if (i == 0)
1221154bbe41SChristian S.J. Peron goto again;
1222154bbe41SChristian S.J. Peron if (i < 0)
1223a8e07101SRui Paulo return (PCAP_ERROR);
1224154bbe41SChristian S.J. Peron } else
1225154bbe41SChristian S.J. Peron #endif
1226a8e07101SRui Paulo {
1227*afdbf109SJoseph Mingrone cc = read(p->fd, p->buffer, p->bufsize);
1228a8e07101SRui Paulo }
12298cf6c252SPaul Traina if (cc < 0) {
12308cf6c252SPaul Traina /* Don't choke when we get ptraced */
12318cf6c252SPaul Traina switch (errno) {
12328cf6c252SPaul Traina
12338cf6c252SPaul Traina case EINTR:
12348cf6c252SPaul Traina goto again;
12358cf6c252SPaul Traina
1236feb4ecdbSBruce M Simpson #ifdef _AIX
1237feb4ecdbSBruce M Simpson case EFAULT:
1238feb4ecdbSBruce M Simpson /*
1239feb4ecdbSBruce M Simpson * Sigh. More AIX wonderfulness.
1240feb4ecdbSBruce M Simpson *
1241feb4ecdbSBruce M Simpson * For some unknown reason the uiomove()
1242feb4ecdbSBruce M Simpson * operation in the bpf kernel extension
1243feb4ecdbSBruce M Simpson * used to copy the buffer into user
1244feb4ecdbSBruce M Simpson * space sometimes returns EFAULT. I have
1245feb4ecdbSBruce M Simpson * no idea why this is the case given that
1246feb4ecdbSBruce M Simpson * a kernel debugger shows the user buffer
1247feb4ecdbSBruce M Simpson * is correct. This problem appears to
1248feb4ecdbSBruce M Simpson * be mostly mitigated by the memset of
1249feb4ecdbSBruce M Simpson * the buffer before it is first used.
1250feb4ecdbSBruce M Simpson * Very strange.... Shaun Clowes
1251feb4ecdbSBruce M Simpson *
1252feb4ecdbSBruce M Simpson * In any case this means that we shouldn't
1253feb4ecdbSBruce M Simpson * treat EFAULT as a fatal error; as we
1254feb4ecdbSBruce M Simpson * don't have an API for returning
1255feb4ecdbSBruce M Simpson * a "some packets were dropped since
1256feb4ecdbSBruce M Simpson * the last packet you saw" indication,
1257feb4ecdbSBruce M Simpson * we just ignore EFAULT and keep reading.
1258feb4ecdbSBruce M Simpson */
1259feb4ecdbSBruce M Simpson goto again;
1260feb4ecdbSBruce M Simpson #endif
1261feb4ecdbSBruce M Simpson
12628cf6c252SPaul Traina case EWOULDBLOCK:
12638cf6c252SPaul Traina return (0);
1264a0ee43a1SRui Paulo
126557e22627SCy Schubert case ENXIO: /* FreeBSD, DragonFly BSD, and Darwin */
126657e22627SCy Schubert case EIO: /* OpenBSD */
126757e22627SCy Schubert /* NetBSD appears not to return an error in this case */
1268a0ee43a1SRui Paulo /*
1269a0ee43a1SRui Paulo * The device on which we're capturing
1270a0ee43a1SRui Paulo * went away.
1271a0ee43a1SRui Paulo *
1272a0ee43a1SRui Paulo * XXX - we should really return
127357e22627SCy Schubert * an appropriate error for that,
127457e22627SCy Schubert * but pcap_dispatch() etc. aren't
127557e22627SCy Schubert * documented as having error returns
127657e22627SCy Schubert * other than PCAP_ERROR or PCAP_ERROR_BREAK.
1277a0ee43a1SRui Paulo */
12786f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
127957e22627SCy Schubert "The interface disappeared");
1280a0ee43a1SRui Paulo return (PCAP_ERROR);
1281a0ee43a1SRui Paulo
1282a0ee43a1SRui Paulo #if defined(sun) && !defined(BSD) && !defined(__svr4__) && !defined(__SVR4)
12838cf6c252SPaul Traina /*
12848cf6c252SPaul Traina * Due to a SunOS bug, after 2^31 bytes, the kernel
12858cf6c252SPaul Traina * file offset overflows and read fails with EINVAL.
12868cf6c252SPaul Traina * The lseek() to 0 will fix things.
12878cf6c252SPaul Traina */
12888cf6c252SPaul Traina case EINVAL:
12898cf6c252SPaul Traina if (lseek(p->fd, 0L, SEEK_CUR) +
12908cf6c252SPaul Traina p->bufsize < 0) {
12918cf6c252SPaul Traina (void)lseek(p->fd, 0L, SEEK_SET);
12928cf6c252SPaul Traina goto again;
12938cf6c252SPaul Traina }
12948cf6c252SPaul Traina /* fall through */
12958cf6c252SPaul Traina #endif
12968cf6c252SPaul Traina }
1297*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1298b00ab754SHans Petter Selasky errno, "read");
1299a8e07101SRui Paulo return (PCAP_ERROR);
13008cf6c252SPaul Traina }
1301ada6f083SXin LI bp = (u_char *)p->buffer;
13028cf6c252SPaul Traina } else
13038cf6c252SPaul Traina bp = p->bp;
13048cf6c252SPaul Traina
13058cf6c252SPaul Traina /*
13068cf6c252SPaul Traina * Loop through each packet.
13076f9cba8fSJoseph Mingrone *
13086f9cba8fSJoseph Mingrone * This assumes that a single buffer of packets will have
13096f9cba8fSJoseph Mingrone * <= INT_MAX packets, so the packet count doesn't overflow.
13108cf6c252SPaul Traina */
1311a4d10fa9SJung-uk Kim #ifdef BIOCSTSTAMP
1312a4d10fa9SJung-uk Kim #define bhp ((struct bpf_xhdr *)bp)
1313a4d10fa9SJung-uk Kim #else
13148cf6c252SPaul Traina #define bhp ((struct bpf_hdr *)bp)
1315a4d10fa9SJung-uk Kim #endif
13168cf6c252SPaul Traina ep = bp + cc;
131704fb2745SSam Leffler #ifdef PCAP_FDDIPAD
131804fb2745SSam Leffler pad = p->fddipad;
131904fb2745SSam Leffler #endif
13208cf6c252SPaul Traina while (bp < ep) {
1321ada6f083SXin LI register u_int caplen, hdrlen;
1322feb4ecdbSBruce M Simpson
1323feb4ecdbSBruce M Simpson /*
1324feb4ecdbSBruce M Simpson * Has "pcap_breakloop()" been called?
1325feb4ecdbSBruce M Simpson * If so, return immediately - if we haven't read any
1326a8e07101SRui Paulo * packets, clear the flag and return PCAP_ERROR_BREAK
1327a8e07101SRui Paulo * to indicate that we were told to break out of the loop,
1328a8e07101SRui Paulo * otherwise leave the flag set, so that the *next* call
1329a8e07101SRui Paulo * will break out of the loop without having read any
1330a8e07101SRui Paulo * packets, and return the number of packets we've
1331a8e07101SRui Paulo * processed so far.
1332feb4ecdbSBruce M Simpson */
1333feb4ecdbSBruce M Simpson if (p->break_loop) {
1334d1e87331SXin LI p->bp = bp;
13356f9cba8fSJoseph Mingrone p->cc = (int)(ep - bp);
1336d1e87331SXin LI /*
1337d1e87331SXin LI * ep is set based on the return value of read(),
1338d1e87331SXin LI * but read() from a BPF device doesn't necessarily
1339d1e87331SXin LI * return a value that's a multiple of the alignment
1340d1e87331SXin LI * value for BPF_WORDALIGN(). However, whenever we
1341d1e87331SXin LI * increment bp, we round up the increment value by
1342d1e87331SXin LI * a value rounded up by BPF_WORDALIGN(), so we
1343d1e87331SXin LI * could increment bp past ep after processing the
1344d1e87331SXin LI * last packet in the buffer.
1345d1e87331SXin LI *
1346d1e87331SXin LI * We treat ep < bp as an indication that this
1347d1e87331SXin LI * happened, and just set p->cc to 0.
1348d1e87331SXin LI */
1349d1e87331SXin LI if (p->cc < 0)
1350d1e87331SXin LI p->cc = 0;
1351feb4ecdbSBruce M Simpson if (n == 0) {
1352feb4ecdbSBruce M Simpson p->break_loop = 0;
1353a8e07101SRui Paulo return (PCAP_ERROR_BREAK);
1354d1e87331SXin LI } else
1355feb4ecdbSBruce M Simpson return (n);
1356feb4ecdbSBruce M Simpson }
1357feb4ecdbSBruce M Simpson
13588cf6c252SPaul Traina caplen = bhp->bh_caplen;
13598cf6c252SPaul Traina hdrlen = bhp->bh_hdrlen;
136004fb2745SSam Leffler datap = bp + hdrlen;
13618cf6c252SPaul Traina /*
1362feb4ecdbSBruce M Simpson * Short-circuit evaluation: if using BPF filter
1363a8e07101SRui Paulo * in kernel, no need to do it now - we already know
1364a8e07101SRui Paulo * the packet passed the filter.
136504fb2745SSam Leffler *
136604fb2745SSam Leffler #ifdef PCAP_FDDIPAD
136704fb2745SSam Leffler * Note: the filter code was generated assuming
136804fb2745SSam Leffler * that p->fddipad was the amount of padding
136904fb2745SSam Leffler * before the header, as that's what's required
137004fb2745SSam Leffler * in the kernel, so we run the filter before
137104fb2745SSam Leffler * skipping that padding.
137204fb2745SSam Leffler #endif
13738cf6c252SPaul Traina */
1374681ed54cSXin LI if (pb->filtering_in_kernel ||
1375*afdbf109SJoseph Mingrone pcapint_filter(p->fcode.bf_insns, datap, bhp->bh_datalen, caplen)) {
137604fb2745SSam Leffler struct pcap_pkthdr pkthdr;
1377a4d10fa9SJung-uk Kim #ifdef BIOCSTSTAMP
1378a4d10fa9SJung-uk Kim struct bintime bt;
137904fb2745SSam Leffler
1380a4d10fa9SJung-uk Kim bt.sec = bhp->bh_tstamp.bt_sec;
1381a4d10fa9SJung-uk Kim bt.frac = bhp->bh_tstamp.bt_frac;
1382a4d10fa9SJung-uk Kim if (p->opt.tstamp_precision == PCAP_TSTAMP_PRECISION_NANO) {
1383a4d10fa9SJung-uk Kim struct timespec ts;
1384a4d10fa9SJung-uk Kim
1385a4d10fa9SJung-uk Kim bintime2timespec(&bt, &ts);
1386a4d10fa9SJung-uk Kim pkthdr.ts.tv_sec = ts.tv_sec;
1387a4d10fa9SJung-uk Kim pkthdr.ts.tv_usec = ts.tv_nsec;
1388a4d10fa9SJung-uk Kim } else {
1389a4d10fa9SJung-uk Kim struct timeval tv;
1390a4d10fa9SJung-uk Kim
1391a4d10fa9SJung-uk Kim bintime2timeval(&bt, &tv);
1392a4d10fa9SJung-uk Kim pkthdr.ts.tv_sec = tv.tv_sec;
1393a4d10fa9SJung-uk Kim pkthdr.ts.tv_usec = tv.tv_usec;
1394a4d10fa9SJung-uk Kim }
1395a4d10fa9SJung-uk Kim #else
139604fb2745SSam Leffler pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec;
13970a94d38fSBill Fenner #ifdef _AIX
13980a94d38fSBill Fenner /*
1399feb4ecdbSBruce M Simpson * AIX's BPF returns seconds/nanoseconds time
1400feb4ecdbSBruce M Simpson * stamps, not seconds/microseconds time stamps.
14010a94d38fSBill Fenner */
140204fb2745SSam Leffler pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000;
140304fb2745SSam Leffler #else
1404*afdbf109SJoseph Mingrone /*
1405*afdbf109SJoseph Mingrone * On NetBSD the former (timeval.tv_usec) is an int via
1406*afdbf109SJoseph Mingrone * suseconds_t and the latter (bpf_timeval.tv_usec) is
1407*afdbf109SJoseph Mingrone * a long. In any case, the value is supposed to be
1408*afdbf109SJoseph Mingrone * within the [0 .. 999999] interval.
1409*afdbf109SJoseph Mingrone */
1410*afdbf109SJoseph Mingrone pkthdr.ts.tv_usec = (suseconds_t)bhp->bh_tstamp.tv_usec;
14110a94d38fSBill Fenner #endif
1412a4d10fa9SJung-uk Kim #endif /* BIOCSTSTAMP */
141304fb2745SSam Leffler #ifdef PCAP_FDDIPAD
141404fb2745SSam Leffler if (caplen > pad)
141504fb2745SSam Leffler pkthdr.caplen = caplen - pad;
141604fb2745SSam Leffler else
141704fb2745SSam Leffler pkthdr.caplen = 0;
141804fb2745SSam Leffler if (bhp->bh_datalen > pad)
141904fb2745SSam Leffler pkthdr.len = bhp->bh_datalen - pad;
142004fb2745SSam Leffler else
142104fb2745SSam Leffler pkthdr.len = 0;
142204fb2745SSam Leffler datap += pad;
142304fb2745SSam Leffler #else
142404fb2745SSam Leffler pkthdr.caplen = caplen;
142504fb2745SSam Leffler pkthdr.len = bhp->bh_datalen;
142604fb2745SSam Leffler #endif
142704fb2745SSam Leffler (*callback)(user, &pkthdr, datap);
14288cf6c252SPaul Traina bp += BPF_WORDALIGN(caplen + hdrlen);
1429681ed54cSXin LI if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
14308cf6c252SPaul Traina p->bp = bp;
14316f9cba8fSJoseph Mingrone p->cc = (int)(ep - bp);
1432d1e87331SXin LI /*
1433d1e87331SXin LI * See comment above about p->cc < 0.
1434d1e87331SXin LI */
1435d1e87331SXin LI if (p->cc < 0)
1436d1e87331SXin LI p->cc = 0;
14378cf6c252SPaul Traina return (n);
14388cf6c252SPaul Traina }
1439feb4ecdbSBruce M Simpson } else {
1440feb4ecdbSBruce M Simpson /*
1441feb4ecdbSBruce M Simpson * Skip this packet.
1442feb4ecdbSBruce M Simpson */
1443feb4ecdbSBruce M Simpson bp += BPF_WORDALIGN(caplen + hdrlen);
1444feb4ecdbSBruce M Simpson }
14458cf6c252SPaul Traina }
14468cf6c252SPaul Traina #undef bhp
14478cf6c252SPaul Traina p->cc = 0;
14488cf6c252SPaul Traina return (n);
14498cf6c252SPaul Traina }
14508cf6c252SPaul Traina
145104fb2745SSam Leffler static int
pcap_inject_bpf(pcap_t * p,const void * buf,int size)14526f9cba8fSJoseph Mingrone pcap_inject_bpf(pcap_t *p, const void *buf, int size)
145304fb2745SSam Leffler {
145404fb2745SSam Leffler int ret;
145504fb2745SSam Leffler
14566f9cba8fSJoseph Mingrone ret = (int)write(p->fd, buf, size);
145704fb2745SSam Leffler #ifdef __APPLE__
145804fb2745SSam Leffler if (ret == -1 && errno == EAFNOSUPPORT) {
145904fb2745SSam Leffler /*
1460b00ab754SHans Petter Selasky * In some versions of macOS, there's a bug wherein setting
1461b00ab754SHans Petter Selasky * the BIOCSHDRCMPLT flag causes writes to fail; see, for
1462b00ab754SHans Petter Selasky * example:
146304fb2745SSam Leffler *
146404fb2745SSam Leffler * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
146504fb2745SSam Leffler *
1466b00ab754SHans Petter Selasky * So, if, on macOS, we get EAFNOSUPPORT from the write, we
146704fb2745SSam Leffler * assume it's due to that bug, and turn off that flag
146804fb2745SSam Leffler * and try again. If we succeed, it either means that
146904fb2745SSam Leffler * somebody applied the fix from that URL, or other patches
147004fb2745SSam Leffler * for that bug from
147104fb2745SSam Leffler *
147204fb2745SSam Leffler * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
147304fb2745SSam Leffler *
147404fb2745SSam Leffler * and are running a Darwin kernel with those fixes, or
1475b00ab754SHans Petter Selasky * that Apple fixed the problem in some macOS release.
147604fb2745SSam Leffler */
147704fb2745SSam Leffler u_int spoof_eth_src = 0;
147804fb2745SSam Leffler
147904fb2745SSam Leffler if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
1480*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1481b00ab754SHans Petter Selasky errno, "send: can't turn off BIOCSHDRCMPLT");
1482a8e07101SRui Paulo return (PCAP_ERROR);
148304fb2745SSam Leffler }
148404fb2745SSam Leffler
148504fb2745SSam Leffler /*
148604fb2745SSam Leffler * Now try the write again.
148704fb2745SSam Leffler */
14886f9cba8fSJoseph Mingrone ret = (int)write(p->fd, buf, size);
148904fb2745SSam Leffler }
149004fb2745SSam Leffler #endif /* __APPLE__ */
149104fb2745SSam Leffler if (ret == -1) {
1492*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1493b00ab754SHans Petter Selasky errno, "send");
1494a8e07101SRui Paulo return (PCAP_ERROR);
149504fb2745SSam Leffler }
149604fb2745SSam Leffler return (ret);
149704fb2745SSam Leffler }
149804fb2745SSam Leffler
1499feb4ecdbSBruce M Simpson #ifdef _AIX
1500feb4ecdbSBruce M Simpson static int
bpf_odminit(char * errbuf)1501feb4ecdbSBruce M Simpson bpf_odminit(char *errbuf)
1502feb4ecdbSBruce M Simpson {
1503feb4ecdbSBruce M Simpson char *errstr;
1504feb4ecdbSBruce M Simpson
1505feb4ecdbSBruce M Simpson if (odm_initialize() == -1) {
1506feb4ecdbSBruce M Simpson if (odm_err_msg(odmerrno, &errstr) == -1)
1507feb4ecdbSBruce M Simpson errstr = "Unknown error";
15086f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
1509feb4ecdbSBruce M Simpson "bpf_load: odm_initialize failed: %s",
1510feb4ecdbSBruce M Simpson errstr);
1511a8e07101SRui Paulo return (PCAP_ERROR);
1512feb4ecdbSBruce M Simpson }
1513feb4ecdbSBruce M Simpson
1514feb4ecdbSBruce M Simpson if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
1515feb4ecdbSBruce M Simpson if (odm_err_msg(odmerrno, &errstr) == -1)
1516feb4ecdbSBruce M Simpson errstr = "Unknown error";
15176f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
1518feb4ecdbSBruce M Simpson "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
1519feb4ecdbSBruce M Simpson errstr);
1520a0ee43a1SRui Paulo (void)odm_terminate();
1521a8e07101SRui Paulo return (PCAP_ERROR);
1522feb4ecdbSBruce M Simpson }
1523feb4ecdbSBruce M Simpson
1524feb4ecdbSBruce M Simpson return (0);
1525feb4ecdbSBruce M Simpson }
1526feb4ecdbSBruce M Simpson
1527feb4ecdbSBruce M Simpson static int
bpf_odmcleanup(char * errbuf)1528feb4ecdbSBruce M Simpson bpf_odmcleanup(char *errbuf)
1529feb4ecdbSBruce M Simpson {
1530feb4ecdbSBruce M Simpson char *errstr;
1531feb4ecdbSBruce M Simpson
1532feb4ecdbSBruce M Simpson if (odm_unlock(odmlockid) == -1) {
1533a0ee43a1SRui Paulo if (errbuf != NULL) {
1534feb4ecdbSBruce M Simpson if (odm_err_msg(odmerrno, &errstr) == -1)
1535feb4ecdbSBruce M Simpson errstr = "Unknown error";
15366f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
1537feb4ecdbSBruce M Simpson "bpf_load: odm_unlock failed: %s",
1538feb4ecdbSBruce M Simpson errstr);
1539a0ee43a1SRui Paulo }
1540a8e07101SRui Paulo return (PCAP_ERROR);
1541feb4ecdbSBruce M Simpson }
1542feb4ecdbSBruce M Simpson
1543feb4ecdbSBruce M Simpson if (odm_terminate() == -1) {
1544a0ee43a1SRui Paulo if (errbuf != NULL) {
1545feb4ecdbSBruce M Simpson if (odm_err_msg(odmerrno, &errstr) == -1)
1546feb4ecdbSBruce M Simpson errstr = "Unknown error";
15476f9cba8fSJoseph Mingrone snprintf(errbuf, PCAP_ERRBUF_SIZE,
1548feb4ecdbSBruce M Simpson "bpf_load: odm_terminate failed: %s",
1549feb4ecdbSBruce M Simpson errstr);
1550a0ee43a1SRui Paulo }
1551a8e07101SRui Paulo return (PCAP_ERROR);
1552feb4ecdbSBruce M Simpson }
1553feb4ecdbSBruce M Simpson
1554feb4ecdbSBruce M Simpson return (0);
1555feb4ecdbSBruce M Simpson }
1556feb4ecdbSBruce M Simpson
1557feb4ecdbSBruce M Simpson static int
bpf_load(char * errbuf)1558feb4ecdbSBruce M Simpson bpf_load(char *errbuf)
1559feb4ecdbSBruce M Simpson {
1560feb4ecdbSBruce M Simpson long major;
1561feb4ecdbSBruce M Simpson int *minors;
1562feb4ecdbSBruce M Simpson int numminors, i, rc;
1563feb4ecdbSBruce M Simpson char buf[1024];
1564feb4ecdbSBruce M Simpson struct stat sbuf;
1565feb4ecdbSBruce M Simpson struct bpf_config cfg_bpf;
1566feb4ecdbSBruce M Simpson struct cfg_load cfg_ld;
1567feb4ecdbSBruce M Simpson struct cfg_kmod cfg_km;
1568feb4ecdbSBruce M Simpson
1569feb4ecdbSBruce M Simpson /*
1570feb4ecdbSBruce M Simpson * This is very very close to what happens in the real implementation
1571feb4ecdbSBruce M Simpson * but I've fixed some (unlikely) bug situations.
1572feb4ecdbSBruce M Simpson */
1573feb4ecdbSBruce M Simpson if (bpfloadedflag)
1574feb4ecdbSBruce M Simpson return (0);
1575feb4ecdbSBruce M Simpson
1576a8e07101SRui Paulo if (bpf_odminit(errbuf) == PCAP_ERROR)
1577a8e07101SRui Paulo return (PCAP_ERROR);
1578feb4ecdbSBruce M Simpson
1579feb4ecdbSBruce M Simpson major = genmajor(BPF_NAME);
1580feb4ecdbSBruce M Simpson if (major == -1) {
1581*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1582b00ab754SHans Petter Selasky errno, "bpf_load: genmajor failed");
1583a0ee43a1SRui Paulo (void)bpf_odmcleanup(NULL);
1584a8e07101SRui Paulo return (PCAP_ERROR);
1585feb4ecdbSBruce M Simpson }
1586feb4ecdbSBruce M Simpson
1587feb4ecdbSBruce M Simpson minors = getminor(major, &numminors, BPF_NAME);
1588feb4ecdbSBruce M Simpson if (!minors) {
1589feb4ecdbSBruce M Simpson minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
1590feb4ecdbSBruce M Simpson if (!minors) {
1591*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1592b00ab754SHans Petter Selasky errno, "bpf_load: genminor failed");
1593a0ee43a1SRui Paulo (void)bpf_odmcleanup(NULL);
1594a8e07101SRui Paulo return (PCAP_ERROR);
1595feb4ecdbSBruce M Simpson }
1596feb4ecdbSBruce M Simpson }
1597feb4ecdbSBruce M Simpson
1598a8e07101SRui Paulo if (bpf_odmcleanup(errbuf) == PCAP_ERROR)
1599a8e07101SRui Paulo return (PCAP_ERROR);
1600feb4ecdbSBruce M Simpson
1601feb4ecdbSBruce M Simpson rc = stat(BPF_NODE "0", &sbuf);
1602feb4ecdbSBruce M Simpson if (rc == -1 && errno != ENOENT) {
1603*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1604b00ab754SHans Petter Selasky errno, "bpf_load: can't stat %s", BPF_NODE "0");
1605a8e07101SRui Paulo return (PCAP_ERROR);
1606feb4ecdbSBruce M Simpson }
1607feb4ecdbSBruce M Simpson
1608feb4ecdbSBruce M Simpson if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
1609feb4ecdbSBruce M Simpson for (i = 0; i < BPF_MINORS; i++) {
16106f9cba8fSJoseph Mingrone snprintf(buf, sizeof(buf), "%s%d", BPF_NODE, i);
1611feb4ecdbSBruce M Simpson unlink(buf);
1612feb4ecdbSBruce M Simpson if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
1613*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf,
1614b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
1615b00ab754SHans Petter Selasky "bpf_load: can't mknod %s", buf);
1616a8e07101SRui Paulo return (PCAP_ERROR);
1617feb4ecdbSBruce M Simpson }
1618feb4ecdbSBruce M Simpson }
1619feb4ecdbSBruce M Simpson }
1620feb4ecdbSBruce M Simpson
1621feb4ecdbSBruce M Simpson /* Check if the driver is loaded */
1622feb4ecdbSBruce M Simpson memset(&cfg_ld, 0x0, sizeof(cfg_ld));
16236f9cba8fSJoseph Mingrone snprintf(buf, sizeof(buf), "%s/%s", DRIVER_PATH, BPF_NAME);
1624feb4ecdbSBruce M Simpson cfg_ld.path = buf;
1625feb4ecdbSBruce M Simpson if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
1626feb4ecdbSBruce M Simpson (cfg_ld.kmid == 0)) {
1627feb4ecdbSBruce M Simpson /* Driver isn't loaded, load it now */
1628feb4ecdbSBruce M Simpson if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
1629*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1630b00ab754SHans Petter Selasky errno, "bpf_load: could not load driver");
1631a8e07101SRui Paulo return (PCAP_ERROR);
1632feb4ecdbSBruce M Simpson }
1633feb4ecdbSBruce M Simpson }
1634feb4ecdbSBruce M Simpson
1635feb4ecdbSBruce M Simpson /* Configure the driver */
1636feb4ecdbSBruce M Simpson cfg_km.cmd = CFG_INIT;
1637feb4ecdbSBruce M Simpson cfg_km.kmid = cfg_ld.kmid;
1638feb4ecdbSBruce M Simpson cfg_km.mdilen = sizeof(cfg_bpf);
1639feb4ecdbSBruce M Simpson cfg_km.mdiptr = (void *)&cfg_bpf;
1640feb4ecdbSBruce M Simpson for (i = 0; i < BPF_MINORS; i++) {
1641feb4ecdbSBruce M Simpson cfg_bpf.devno = domakedev(major, i);
1642feb4ecdbSBruce M Simpson if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
1643*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1644b00ab754SHans Petter Selasky errno, "bpf_load: could not configure driver");
1645a8e07101SRui Paulo return (PCAP_ERROR);
1646feb4ecdbSBruce M Simpson }
1647feb4ecdbSBruce M Simpson }
1648feb4ecdbSBruce M Simpson
1649feb4ecdbSBruce M Simpson bpfloadedflag = 1;
1650feb4ecdbSBruce M Simpson
1651feb4ecdbSBruce M Simpson return (0);
1652feb4ecdbSBruce M Simpson }
1653feb4ecdbSBruce M Simpson #endif
1654feb4ecdbSBruce M Simpson
1655a8e07101SRui Paulo /*
1656ada6f083SXin LI * Undo any operations done when opening the device when necessary.
1657a8e07101SRui Paulo */
1658a8e07101SRui Paulo static void
pcap_cleanup_bpf(pcap_t * p)1659a8e07101SRui Paulo pcap_cleanup_bpf(pcap_t *p)
16608cf6c252SPaul Traina {
1661681ed54cSXin LI struct pcap_bpf *pb = p->priv;
1662a8e07101SRui Paulo #ifdef HAVE_BSD_IEEE80211
1663a8e07101SRui Paulo int sock;
1664a8e07101SRui Paulo struct ifmediareq req;
1665a8e07101SRui Paulo struct ifreq ifr;
16665357e0feSMax Laier #endif
16678cf6c252SPaul Traina
1668681ed54cSXin LI if (pb->must_do_on_close != 0) {
1669feb4ecdbSBruce M Simpson /*
1670a8e07101SRui Paulo * There's something we have to do when closing this
1671a8e07101SRui Paulo * pcap_t.
1672feb4ecdbSBruce M Simpson */
1673a8e07101SRui Paulo #ifdef HAVE_BSD_IEEE80211
1674681ed54cSXin LI if (pb->must_do_on_close & MUST_CLEAR_RFMON) {
16758cf6c252SPaul Traina /*
1676a8e07101SRui Paulo * We put the interface into rfmon mode;
1677a8e07101SRui Paulo * take it out of rfmon mode.
167804fb2745SSam Leffler *
1679a8e07101SRui Paulo * XXX - if somebody else wants it in rfmon
1680a8e07101SRui Paulo * mode, this code cannot know that, so it'll take
1681a8e07101SRui Paulo * it out of rfmon mode.
168204fb2745SSam Leffler */
1683a8e07101SRui Paulo sock = socket(AF_INET, SOCK_DGRAM, 0);
1684a8e07101SRui Paulo if (sock == -1) {
1685a8e07101SRui Paulo fprintf(stderr,
1686a8e07101SRui Paulo "Can't restore interface flags (socket() failed: %s).\n"
1687a8e07101SRui Paulo "Please adjust manually.\n",
1688a8e07101SRui Paulo strerror(errno));
1689a8e07101SRui Paulo } else {
1690a8e07101SRui Paulo memset(&req, 0, sizeof(req));
1691*afdbf109SJoseph Mingrone pcapint_strlcpy(req.ifm_name, pb->device,
1692a8e07101SRui Paulo sizeof(req.ifm_name));
1693a8e07101SRui Paulo if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
1694a8e07101SRui Paulo fprintf(stderr,
1695a8e07101SRui Paulo "Can't restore interface flags (SIOCGIFMEDIA failed: %s).\n"
1696a8e07101SRui Paulo "Please adjust manually.\n",
1697a8e07101SRui Paulo strerror(errno));
1698a8e07101SRui Paulo } else {
1699a8e07101SRui Paulo if (req.ifm_current & IFM_IEEE80211_MONITOR) {
1700a8e07101SRui Paulo /*
1701a8e07101SRui Paulo * Rfmon mode is currently on;
1702a8e07101SRui Paulo * turn it off.
1703a8e07101SRui Paulo */
1704a8e07101SRui Paulo memset(&ifr, 0, sizeof(ifr));
1705*afdbf109SJoseph Mingrone (void)pcapint_strlcpy(ifr.ifr_name,
1706681ed54cSXin LI pb->device,
1707a8e07101SRui Paulo sizeof(ifr.ifr_name));
1708a8e07101SRui Paulo ifr.ifr_media =
1709a8e07101SRui Paulo req.ifm_current & ~IFM_IEEE80211_MONITOR;
1710a8e07101SRui Paulo if (ioctl(sock, SIOCSIFMEDIA,
1711a8e07101SRui Paulo &ifr) == -1) {
1712a8e07101SRui Paulo fprintf(stderr,
1713a8e07101SRui Paulo "Can't restore interface flags (SIOCSIFMEDIA failed: %s).\n"
1714a8e07101SRui Paulo "Please adjust manually.\n",
1715a8e07101SRui Paulo strerror(errno));
1716a8e07101SRui Paulo }
1717a8e07101SRui Paulo }
1718a8e07101SRui Paulo }
1719a8e07101SRui Paulo close(sock);
1720a8e07101SRui Paulo }
1721a8e07101SRui Paulo }
1722a8e07101SRui Paulo #endif /* HAVE_BSD_IEEE80211 */
17238cf6c252SPaul Traina
1724ada6f083SXin LI #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
1725ada6f083SXin LI /*
1726ada6f083SXin LI * Attempt to destroy the usbusN interface that we created.
1727ada6f083SXin LI */
1728ada6f083SXin LI if (pb->must_do_on_close & MUST_DESTROY_USBUS) {
1729ada6f083SXin LI if (if_nametoindex(pb->device) > 0) {
1730ada6f083SXin LI int s;
1731ada6f083SXin LI
1732ada6f083SXin LI s = socket(AF_LOCAL, SOCK_DGRAM, 0);
1733ada6f083SXin LI if (s >= 0) {
1734*afdbf109SJoseph Mingrone pcapint_strlcpy(ifr.ifr_name, pb->device,
1735ada6f083SXin LI sizeof(ifr.ifr_name));
1736ada6f083SXin LI ioctl(s, SIOCIFDESTROY, &ifr);
1737ada6f083SXin LI close(s);
1738ada6f083SXin LI }
1739ada6f083SXin LI }
1740ada6f083SXin LI }
1741ada6f083SXin LI #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
17428cf6c252SPaul Traina /*
1743a8e07101SRui Paulo * Take this pcap out of the list of pcaps for which we
1744a8e07101SRui Paulo * have to take the interface out of some mode.
17458cf6c252SPaul Traina */
1746*afdbf109SJoseph Mingrone pcapint_remove_from_pcaps_to_close(p);
1747681ed54cSXin LI pb->must_do_on_close = 0;
17488cf6c252SPaul Traina }
17498cf6c252SPaul Traina
1750a8e07101SRui Paulo #ifdef HAVE_ZEROCOPY_BPF
1751681ed54cSXin LI if (pb->zerocopy) {
1752d1e87331SXin LI /*
1753d1e87331SXin LI * Delete the mappings. Note that p->buffer gets
1754d1e87331SXin LI * initialized to one of the mmapped regions in
1755d1e87331SXin LI * this case, so do not try and free it directly;
1756*afdbf109SJoseph Mingrone * null it out so that pcapint_cleanup_live_common()
1757d1e87331SXin LI * doesn't try to free it.
1758d1e87331SXin LI */
1759681ed54cSXin LI if (pb->zbuf1 != MAP_FAILED && pb->zbuf1 != NULL)
1760681ed54cSXin LI (void) munmap(pb->zbuf1, pb->zbufsize);
1761681ed54cSXin LI if (pb->zbuf2 != MAP_FAILED && pb->zbuf2 != NULL)
1762681ed54cSXin LI (void) munmap(pb->zbuf2, pb->zbufsize);
1763d1e87331SXin LI p->buffer = NULL;
1764a8e07101SRui Paulo }
1765a8e07101SRui Paulo #endif
1766681ed54cSXin LI if (pb->device != NULL) {
1767681ed54cSXin LI free(pb->device);
1768681ed54cSXin LI pb->device = NULL;
1769a8e07101SRui Paulo }
1770*afdbf109SJoseph Mingrone pcapint_cleanup_live_common(p);
1771a8e07101SRui Paulo }
1772a8e07101SRui Paulo
17736f9cba8fSJoseph Mingrone #ifdef __APPLE__
1774a8e07101SRui Paulo static int
check_setif_failure(pcap_t * p,int error)1775a8e07101SRui Paulo check_setif_failure(pcap_t *p, int error)
1776a8e07101SRui Paulo {
1777a8e07101SRui Paulo int fd;
1778a8e07101SRui Paulo int err;
177904fb2745SSam Leffler
17806f9cba8fSJoseph Mingrone if (error == PCAP_ERROR_NO_SUCH_DEVICE) {
1781a8e07101SRui Paulo /*
1782a8e07101SRui Paulo * No such device exists.
1783a8e07101SRui Paulo */
1784ada6f083SXin LI if (p->opt.rfmon && strncmp(p->opt.device, "wlt", 3) == 0) {
1785a8e07101SRui Paulo /*
1786a8e07101SRui Paulo * Monitor mode was requested, and we're trying
1787a8e07101SRui Paulo * to open a "wltN" device. Assume that this
1788a8e07101SRui Paulo * is 10.4 and that we were asked to open an
1789a8e07101SRui Paulo * "enN" device; if that device exists, return
1790a8e07101SRui Paulo * "monitor mode not supported on the device".
1791a8e07101SRui Paulo */
1792a8e07101SRui Paulo fd = socket(AF_INET, SOCK_DGRAM, 0);
1793a8e07101SRui Paulo if (fd != -1) {
17946f9cba8fSJoseph Mingrone char *en_name;
17956f9cba8fSJoseph Mingrone
1796*afdbf109SJoseph Mingrone if (pcapint_asprintf(&en_name, "en%s",
17976f9cba8fSJoseph Mingrone p->opt.device + 3) == -1) {
1798a8e07101SRui Paulo /*
17996f9cba8fSJoseph Mingrone * We can't find out whether there's
18006f9cba8fSJoseph Mingrone * an underlying "enN" device, so
18016f9cba8fSJoseph Mingrone * just report "no such device".
1802a8e07101SRui Paulo */
1803*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
1804b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
18056f9cba8fSJoseph Mingrone "malloc");
18066f9cba8fSJoseph Mingrone close(fd);
18076f9cba8fSJoseph Mingrone return (PCAP_ERROR_NO_SUCH_DEVICE);
18086f9cba8fSJoseph Mingrone }
18096f9cba8fSJoseph Mingrone err = device_exists(fd, en_name, p->errbuf);
18106f9cba8fSJoseph Mingrone free(en_name);
18116f9cba8fSJoseph Mingrone if (err != 0) {
18126f9cba8fSJoseph Mingrone if (err == PCAP_ERROR_NO_SUCH_DEVICE) {
1813a8e07101SRui Paulo /*
1814a8e07101SRui Paulo * The underlying "enN" device
1815a8e07101SRui Paulo * exists, but there's no
1816a8e07101SRui Paulo * corresponding "wltN" device;
1817a8e07101SRui Paulo * that means that the "enN"
1818a8e07101SRui Paulo * device doesn't support
18196f9cba8fSJoseph Mingrone * monitor mode, probably
18206f9cba8fSJoseph Mingrone * because it's an Ethernet
18216f9cba8fSJoseph Mingrone * device rather than a
18226f9cba8fSJoseph Mingrone * wireless device.
1823a8e07101SRui Paulo */
1824a8e07101SRui Paulo err = PCAP_ERROR_RFMON_NOTSUP;
1825a8e07101SRui Paulo }
18266f9cba8fSJoseph Mingrone }
1827a8e07101SRui Paulo close(fd);
1828a8e07101SRui Paulo } else {
1829a8e07101SRui Paulo /*
1830a8e07101SRui Paulo * We can't find out whether there's
1831a8e07101SRui Paulo * an underlying "enN" device, so
1832a8e07101SRui Paulo * just report "no such device".
1833a8e07101SRui Paulo */
1834a8e07101SRui Paulo err = PCAP_ERROR_NO_SUCH_DEVICE;
1835*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
1836b00ab754SHans Petter Selasky errno, PCAP_ERRBUF_SIZE,
1837b00ab754SHans Petter Selasky "socket() failed");
1838a8e07101SRui Paulo }
1839a8e07101SRui Paulo return (err);
1840a8e07101SRui Paulo }
18416f9cba8fSJoseph Mingrone
1842a8e07101SRui Paulo /*
1843a8e07101SRui Paulo * No such device.
1844a8e07101SRui Paulo */
1845a8e07101SRui Paulo return (PCAP_ERROR_NO_SUCH_DEVICE);
1846a8e07101SRui Paulo }
18476f9cba8fSJoseph Mingrone
18486f9cba8fSJoseph Mingrone /*
18496f9cba8fSJoseph Mingrone * Just return the error status; it's what we want, and, if it's
18506f9cba8fSJoseph Mingrone * PCAP_ERROR, the error string has been filled in.
18516f9cba8fSJoseph Mingrone */
18526f9cba8fSJoseph Mingrone return (error);
1853a8e07101SRui Paulo }
18546f9cba8fSJoseph Mingrone #else
18556f9cba8fSJoseph Mingrone static int
check_setif_failure(pcap_t * p _U_,int error)18566f9cba8fSJoseph Mingrone check_setif_failure(pcap_t *p _U_, int error)
18576f9cba8fSJoseph Mingrone {
18586f9cba8fSJoseph Mingrone /*
18596f9cba8fSJoseph Mingrone * Just return the error status; it's what we want, and, if it's
18606f9cba8fSJoseph Mingrone * PCAP_ERROR, the error string has been filled in.
18616f9cba8fSJoseph Mingrone */
18626f9cba8fSJoseph Mingrone return (error);
18636f9cba8fSJoseph Mingrone }
18646f9cba8fSJoseph Mingrone #endif
1865a8e07101SRui Paulo
1866a0ee43a1SRui Paulo /*
1867a0ee43a1SRui Paulo * Default capture buffer size.
1868a0ee43a1SRui Paulo * 32K isn't very much for modern machines with fast networks; we
1869a0ee43a1SRui Paulo * pick .5M, as that's the maximum on at least some systems with BPF.
187015752fa8SXin LI *
187115752fa8SXin LI * However, on AIX 3.5, the larger buffer sized caused unrecoverable
187215752fa8SXin LI * read failures under stress, so we leave it as 32K; yet another
187315752fa8SXin LI * place where AIX's BPF is broken.
1874a0ee43a1SRui Paulo */
187515752fa8SXin LI #ifdef _AIX
187615752fa8SXin LI #define DEFAULT_BUFSIZE 32768
187715752fa8SXin LI #else
1878a0ee43a1SRui Paulo #define DEFAULT_BUFSIZE 524288
187915752fa8SXin LI #endif
1880a0ee43a1SRui Paulo
1881a8e07101SRui Paulo static int
pcap_activate_bpf(pcap_t * p)1882a8e07101SRui Paulo pcap_activate_bpf(pcap_t *p)
18838cf6c252SPaul Traina {
1884681ed54cSXin LI struct pcap_bpf *pb = p->priv;
1885a8e07101SRui Paulo int status = 0;
1886681ed54cSXin LI #ifdef HAVE_BSD_IEEE80211
1887681ed54cSXin LI int retv;
1888681ed54cSXin LI #endif
18898cf6c252SPaul Traina int fd;
18908cf6c252SPaul Traina struct bpf_version bv;
1891a8e07101SRui Paulo #ifdef __APPLE__
1892a8e07101SRui Paulo int sockfd;
1893a8e07101SRui Paulo char *wltdev = NULL;
1894a8e07101SRui Paulo #endif
1895feb4ecdbSBruce M Simpson #ifdef BIOCGDLTLIST
189609f33d61SBill Fenner struct bpf_dltlist bdl;
1897a8e07101SRui Paulo #if defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)
1898a8e07101SRui Paulo int new_dlt;
1899feb4ecdbSBruce M Simpson #endif
1900a8e07101SRui Paulo #endif /* BIOCGDLTLIST */
190104fb2745SSam Leffler #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
190204fb2745SSam Leffler u_int spoof_eth_src = 1;
190304fb2745SSam Leffler #endif
19048cf6c252SPaul Traina u_int v;
190504fb2745SSam Leffler struct bpf_insn total_insn;
190604fb2745SSam Leffler struct bpf_program total_prog;
1907feb4ecdbSBruce M Simpson struct utsname osinfo;
1908a8e07101SRui Paulo int have_osinfo = 0;
1909a8e07101SRui Paulo #ifdef HAVE_ZEROCOPY_BPF
1910a8e07101SRui Paulo struct bpf_zbuf bz;
1911a8e07101SRui Paulo u_int bufmode, zbufmax;
1912feb4ecdbSBruce M Simpson #endif
191309f33d61SBill Fenner
1914ada6f083SXin LI fd = bpf_open(p->errbuf);
1915a8e07101SRui Paulo if (fd < 0) {
1916a8e07101SRui Paulo status = fd;
19178cf6c252SPaul Traina goto bad;
1918a8e07101SRui Paulo }
19198cf6c252SPaul Traina
19208cf6c252SPaul Traina p->fd = fd;
19218cf6c252SPaul Traina
19228cf6c252SPaul Traina if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
1923*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1924b00ab754SHans Petter Selasky errno, "BIOCVERSION");
1925a8e07101SRui Paulo status = PCAP_ERROR;
19268cf6c252SPaul Traina goto bad;
19278cf6c252SPaul Traina }
19288cf6c252SPaul Traina if (bv.bv_major != BPF_MAJOR_VERSION ||
19298cf6c252SPaul Traina bv.bv_minor < BPF_MINOR_VERSION) {
19306f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1931dc2c7305SBill Fenner "kernel bpf filter out of date");
1932a8e07101SRui Paulo status = PCAP_ERROR;
19338cf6c252SPaul Traina goto bad;
19348cf6c252SPaul Traina }
1935dc2c7305SBill Fenner
1936b00ab754SHans Petter Selasky /*
1937b00ab754SHans Petter Selasky * Turn a negative snapshot value (invalid), a snapshot value of
1938b00ab754SHans Petter Selasky * 0 (unspecified), or a value bigger than the normal maximum
1939b00ab754SHans Petter Selasky * value, into the maximum allowed value.
1940b00ab754SHans Petter Selasky *
1941b00ab754SHans Petter Selasky * If some application really *needs* a bigger snapshot
1942b00ab754SHans Petter Selasky * length, we should just increase MAXIMUM_SNAPLEN.
1943b00ab754SHans Petter Selasky */
1944b00ab754SHans Petter Selasky if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
1945b00ab754SHans Petter Selasky p->snapshot = MAXIMUM_SNAPLEN;
1946b00ab754SHans Petter Selasky
1947ada6f083SXin LI pb->device = strdup(p->opt.device);
1948681ed54cSXin LI if (pb->device == NULL) {
1949*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
1950b00ab754SHans Petter Selasky errno, "strdup");
1951a8e07101SRui Paulo status = PCAP_ERROR;
1952154bbe41SChristian S.J. Peron goto bad;
1953154bbe41SChristian S.J. Peron }
1954154bbe41SChristian S.J. Peron
1955dc2c7305SBill Fenner /*
1956a8e07101SRui Paulo * Attempt to find out the version of the OS on which we're running.
1957dc2c7305SBill Fenner */
1958a8e07101SRui Paulo if (uname(&osinfo) == 0)
1959a8e07101SRui Paulo have_osinfo = 1;
1960a8e07101SRui Paulo
1961a8e07101SRui Paulo #ifdef __APPLE__
1962a8e07101SRui Paulo /*
1963a8e07101SRui Paulo * See comment in pcap_can_set_rfmon_bpf() for an explanation
1964a8e07101SRui Paulo * of why we check the version number.
1965a8e07101SRui Paulo */
1966a8e07101SRui Paulo if (p->opt.rfmon) {
1967a8e07101SRui Paulo if (have_osinfo) {
1968a8e07101SRui Paulo /*
1969a8e07101SRui Paulo * We assume osinfo.sysname is "Darwin", because
1970a8e07101SRui Paulo * __APPLE__ is defined. We just check the version.
1971a8e07101SRui Paulo */
1972a8e07101SRui Paulo if (osinfo.release[0] < '8' &&
1973a8e07101SRui Paulo osinfo.release[1] == '.') {
1974a8e07101SRui Paulo /*
1975a8e07101SRui Paulo * 10.3 (Darwin 7.x) or earlier.
1976a8e07101SRui Paulo */
1977a8e07101SRui Paulo status = PCAP_ERROR_RFMON_NOTSUP;
1978a8e07101SRui Paulo goto bad;
1979a8e07101SRui Paulo }
1980a8e07101SRui Paulo if (osinfo.release[0] == '8' &&
1981a8e07101SRui Paulo osinfo.release[1] == '.') {
1982a8e07101SRui Paulo /*
1983a8e07101SRui Paulo * 10.4 (Darwin 8.x). s/en/wlt/
1984a8e07101SRui Paulo */
1985ada6f083SXin LI if (strncmp(p->opt.device, "en", 2) != 0) {
1986a8e07101SRui Paulo /*
1987a8e07101SRui Paulo * Not an enN device; check
1988a8e07101SRui Paulo * whether the device even exists.
1989a8e07101SRui Paulo */
1990a8e07101SRui Paulo sockfd = socket(AF_INET, SOCK_DGRAM, 0);
1991a8e07101SRui Paulo if (sockfd != -1) {
19926f9cba8fSJoseph Mingrone status = device_exists(sockfd,
19936f9cba8fSJoseph Mingrone p->opt.device, p->errbuf);
19946f9cba8fSJoseph Mingrone if (status == 0) {
1995a8e07101SRui Paulo /*
19966f9cba8fSJoseph Mingrone * The device exists,
19976f9cba8fSJoseph Mingrone * but it's not an
19986f9cba8fSJoseph Mingrone * enN device; that
19996f9cba8fSJoseph Mingrone * means it doesn't
20006f9cba8fSJoseph Mingrone * support monitor
20016f9cba8fSJoseph Mingrone * mode.
2002a8e07101SRui Paulo */
2003a8e07101SRui Paulo status = PCAP_ERROR_RFMON_NOTSUP;
20046f9cba8fSJoseph Mingrone }
2005a8e07101SRui Paulo close(sockfd);
2006a8e07101SRui Paulo } else {
2007a8e07101SRui Paulo /*
2008a8e07101SRui Paulo * We can't find out whether
2009a8e07101SRui Paulo * the device exists, so just
2010a8e07101SRui Paulo * report "no such device".
2011a8e07101SRui Paulo */
2012a8e07101SRui Paulo status = PCAP_ERROR_NO_SUCH_DEVICE;
2013*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
2014b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
2015b00ab754SHans Petter Selasky "socket() failed");
2016a8e07101SRui Paulo }
2017a8e07101SRui Paulo goto bad;
2018a8e07101SRui Paulo }
2019ada6f083SXin LI wltdev = malloc(strlen(p->opt.device) + 2);
2020a8e07101SRui Paulo if (wltdev == NULL) {
2021*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
2022b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
2023b00ab754SHans Petter Selasky "malloc");
2024a8e07101SRui Paulo status = PCAP_ERROR;
2025a8e07101SRui Paulo goto bad;
2026a8e07101SRui Paulo }
2027a8e07101SRui Paulo strcpy(wltdev, "wlt");
2028ada6f083SXin LI strcat(wltdev, p->opt.device + 2);
2029ada6f083SXin LI free(p->opt.device);
2030ada6f083SXin LI p->opt.device = wltdev;
2031a8e07101SRui Paulo }
2032a8e07101SRui Paulo /*
2033a8e07101SRui Paulo * Everything else is 10.5 or later; for those,
2034a8e07101SRui Paulo * we just open the enN device, and set the DLT.
2035a8e07101SRui Paulo */
2036a8e07101SRui Paulo }
2037a8e07101SRui Paulo }
2038a8e07101SRui Paulo #endif /* __APPLE__ */
2039ada6f083SXin LI
2040ada6f083SXin LI /*
2041ada6f083SXin LI * If this is FreeBSD, and the device name begins with "usbus",
2042ada6f083SXin LI * try to create the interface if it's not available.
2043ada6f083SXin LI */
2044ada6f083SXin LI #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2045ada6f083SXin LI if (strncmp(p->opt.device, usbus_prefix, USBUS_PREFIX_LEN) == 0) {
2046ada6f083SXin LI /*
2047ada6f083SXin LI * Do we already have an interface with that name?
2048ada6f083SXin LI */
2049ada6f083SXin LI if (if_nametoindex(p->opt.device) == 0) {
2050ada6f083SXin LI /*
2051ada6f083SXin LI * No. We need to create it, and, if we
2052ada6f083SXin LI * succeed, remember that we should destroy
2053ada6f083SXin LI * it when the pcap_t is closed.
2054ada6f083SXin LI */
2055ada6f083SXin LI int s;
20566f9cba8fSJoseph Mingrone struct ifreq ifr;
2057ada6f083SXin LI
2058ada6f083SXin LI /*
2059ada6f083SXin LI * Open a socket to use for ioctls to
2060ada6f083SXin LI * create the interface.
2061ada6f083SXin LI */
2062ada6f083SXin LI s = socket(AF_LOCAL, SOCK_DGRAM, 0);
2063ada6f083SXin LI if (s < 0) {
2064*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
2065b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
2066b00ab754SHans Petter Selasky "Can't open socket");
2067ada6f083SXin LI status = PCAP_ERROR;
2068ada6f083SXin LI goto bad;
2069ada6f083SXin LI }
2070ada6f083SXin LI
2071ada6f083SXin LI /*
2072ada6f083SXin LI * If we haven't already done so, arrange to have
2073ada6f083SXin LI * "pcap_close_all()" called when we exit.
2074ada6f083SXin LI */
2075*afdbf109SJoseph Mingrone if (!pcapint_do_addexit(p)) {
2076ada6f083SXin LI /*
2077ada6f083SXin LI * "atexit()" failed; don't create the
2078ada6f083SXin LI * interface, just give up.
2079ada6f083SXin LI */
20806f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2081ada6f083SXin LI "atexit failed");
2082ada6f083SXin LI close(s);
2083ada6f083SXin LI status = PCAP_ERROR;
2084ada6f083SXin LI goto bad;
2085ada6f083SXin LI }
2086ada6f083SXin LI
2087ada6f083SXin LI /*
2088ada6f083SXin LI * Create the interface.
2089ada6f083SXin LI */
2090*afdbf109SJoseph Mingrone pcapint_strlcpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
2091ada6f083SXin LI if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) {
2092ada6f083SXin LI if (errno == EINVAL) {
20936f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2094ada6f083SXin LI "Invalid USB bus interface %s",
2095ada6f083SXin LI p->opt.device);
2096ada6f083SXin LI } else {
2097*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
2098b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
2099b00ab754SHans Petter Selasky "Can't create interface for %s",
2100b00ab754SHans Petter Selasky p->opt.device);
2101ada6f083SXin LI }
2102ada6f083SXin LI close(s);
2103ada6f083SXin LI status = PCAP_ERROR;
2104ada6f083SXin LI goto bad;
2105ada6f083SXin LI }
2106ada6f083SXin LI
2107ada6f083SXin LI /*
2108ada6f083SXin LI * Make sure we clean this up when we close.
2109ada6f083SXin LI */
2110ada6f083SXin LI pb->must_do_on_close |= MUST_DESTROY_USBUS;
2111ada6f083SXin LI
2112ada6f083SXin LI /*
2113ada6f083SXin LI * Add this to the list of pcaps to close when we exit.
2114ada6f083SXin LI */
2115*afdbf109SJoseph Mingrone pcapint_add_to_pcaps_to_close(p);
2116ada6f083SXin LI }
2117ada6f083SXin LI }
2118ada6f083SXin LI #endif /* defined(__FreeBSD__) && defined(SIOCIFCREATE2) */
2119ada6f083SXin LI
2120a8e07101SRui Paulo #ifdef HAVE_ZEROCOPY_BPF
2121a8e07101SRui Paulo /*
2122a8e07101SRui Paulo * If the BPF extension to set buffer mode is present, try setting
2123a8e07101SRui Paulo * the mode to zero-copy. If that fails, use regular buffering. If
2124a8e07101SRui Paulo * it succeeds but other setup fails, return an error to the user.
2125a8e07101SRui Paulo */
2126a8e07101SRui Paulo bufmode = BPF_BUFMODE_ZBUF;
2127a8e07101SRui Paulo if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) {
2128a8e07101SRui Paulo /*
2129a8e07101SRui Paulo * We have zerocopy BPF; use it.
2130a8e07101SRui Paulo */
2131681ed54cSXin LI pb->zerocopy = 1;
2132a8e07101SRui Paulo
2133a8e07101SRui Paulo /*
2134a8e07101SRui Paulo * How to pick a buffer size: first, query the maximum buffer
2135a8e07101SRui Paulo * size supported by zero-copy. This also lets us quickly
2136a8e07101SRui Paulo * determine whether the kernel generally supports zero-copy.
2137a8e07101SRui Paulo * Then, if a buffer size was specified, use that, otherwise
2138a8e07101SRui Paulo * query the default buffer size, which reflects kernel
2139a8e07101SRui Paulo * policy for a desired default. Round to the nearest page
2140a8e07101SRui Paulo * size.
2141a8e07101SRui Paulo */
2142a8e07101SRui Paulo if (ioctl(fd, BIOCGETZMAX, (caddr_t)&zbufmax) < 0) {
2143*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2144b00ab754SHans Petter Selasky errno, "BIOCGETZMAX");
2145681ed54cSXin LI status = PCAP_ERROR;
2146a8e07101SRui Paulo goto bad;
2147a8e07101SRui Paulo }
2148a8e07101SRui Paulo
2149a8e07101SRui Paulo if (p->opt.buffer_size != 0) {
2150a8e07101SRui Paulo /*
2151a8e07101SRui Paulo * A buffer size was explicitly specified; use it.
2152a8e07101SRui Paulo */
2153a8e07101SRui Paulo v = p->opt.buffer_size;
2154a8e07101SRui Paulo } else {
2155a8e07101SRui Paulo if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
2156a0ee43a1SRui Paulo v < DEFAULT_BUFSIZE)
2157a0ee43a1SRui Paulo v = DEFAULT_BUFSIZE;
2158a8e07101SRui Paulo }
2159a8e07101SRui Paulo #ifndef roundup
2160a8e07101SRui Paulo #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
2161a8e07101SRui Paulo #endif
2162681ed54cSXin LI pb->zbufsize = roundup(v, getpagesize());
2163681ed54cSXin LI if (pb->zbufsize > zbufmax)
2164681ed54cSXin LI pb->zbufsize = zbufmax;
2165681ed54cSXin LI pb->zbuf1 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
2166a8e07101SRui Paulo MAP_ANON, -1, 0);
2167681ed54cSXin LI pb->zbuf2 = mmap(NULL, pb->zbufsize, PROT_READ | PROT_WRITE,
2168a8e07101SRui Paulo MAP_ANON, -1, 0);
2169681ed54cSXin LI if (pb->zbuf1 == MAP_FAILED || pb->zbuf2 == MAP_FAILED) {
2170*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2171b00ab754SHans Petter Selasky errno, "mmap");
2172681ed54cSXin LI status = PCAP_ERROR;
2173a8e07101SRui Paulo goto bad;
2174a8e07101SRui Paulo }
2175edc89b24SXin LI memset(&bz, 0, sizeof(bz)); /* bzero() deprecated, replaced with memset() */
2176681ed54cSXin LI bz.bz_bufa = pb->zbuf1;
2177681ed54cSXin LI bz.bz_bufb = pb->zbuf2;
2178681ed54cSXin LI bz.bz_buflen = pb->zbufsize;
2179a8e07101SRui Paulo if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) {
2180*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2181b00ab754SHans Petter Selasky errno, "BIOCSETZBUF");
2182681ed54cSXin LI status = PCAP_ERROR;
2183a8e07101SRui Paulo goto bad;
2184a8e07101SRui Paulo }
21856f9cba8fSJoseph Mingrone status = bpf_bind(fd, p->opt.device, ifnamsiz, p->errbuf);
21866f9cba8fSJoseph Mingrone if (status != BPF_BIND_SUCCEEDED) {
21876f9cba8fSJoseph Mingrone if (status == BPF_BIND_BUFFER_TOO_BIG) {
21886f9cba8fSJoseph Mingrone /*
21896f9cba8fSJoseph Mingrone * The requested buffer size
21906f9cba8fSJoseph Mingrone * is too big. Fail.
21916f9cba8fSJoseph Mingrone *
21926f9cba8fSJoseph Mingrone * XXX - should we do the "keep cutting
21936f9cba8fSJoseph Mingrone * the buffer size in half" loop here if
21946f9cba8fSJoseph Mingrone * we're using the default buffer size?
21956f9cba8fSJoseph Mingrone */
2196681ed54cSXin LI status = PCAP_ERROR;
21976f9cba8fSJoseph Mingrone }
2198a8e07101SRui Paulo goto bad;
2199a8e07101SRui Paulo }
2200681ed54cSXin LI v = pb->zbufsize - sizeof(struct bpf_zbuf_header);
2201a8e07101SRui Paulo } else
2202a8e07101SRui Paulo #endif
2203a8e07101SRui Paulo {
2204a8e07101SRui Paulo /*
2205a8e07101SRui Paulo * We don't have zerocopy BPF.
2206a8e07101SRui Paulo * Set the buffer size.
2207a8e07101SRui Paulo */
2208a8e07101SRui Paulo if (p->opt.buffer_size != 0) {
2209a8e07101SRui Paulo /*
2210a8e07101SRui Paulo * A buffer size was explicitly specified; use it.
2211a8e07101SRui Paulo */
2212a8e07101SRui Paulo if (ioctl(fd, BIOCSBLEN,
2213a8e07101SRui Paulo (caddr_t)&p->opt.buffer_size) < 0) {
2214*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
2215b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno,
2216b00ab754SHans Petter Selasky "BIOCSBLEN: %s", p->opt.device);
2217a8e07101SRui Paulo status = PCAP_ERROR;
2218a8e07101SRui Paulo goto bad;
2219a8e07101SRui Paulo }
2220a8e07101SRui Paulo
2221a8e07101SRui Paulo /*
2222a8e07101SRui Paulo * Now bind to the device.
2223a8e07101SRui Paulo */
22246f9cba8fSJoseph Mingrone status = bpf_bind(fd, p->opt.device, p->errbuf);
22256f9cba8fSJoseph Mingrone if (status != BPF_BIND_SUCCEEDED) {
22266f9cba8fSJoseph Mingrone if (status == BPF_BIND_BUFFER_TOO_BIG) {
22276f9cba8fSJoseph Mingrone /*
22286f9cba8fSJoseph Mingrone * The requested buffer size
22296f9cba8fSJoseph Mingrone * is too big. Fail.
22306f9cba8fSJoseph Mingrone */
22316f9cba8fSJoseph Mingrone status = PCAP_ERROR;
22326f9cba8fSJoseph Mingrone goto bad;
22336f9cba8fSJoseph Mingrone }
22346f9cba8fSJoseph Mingrone
22356f9cba8fSJoseph Mingrone /*
22366f9cba8fSJoseph Mingrone * Special checks on macOS to deal with
22376f9cba8fSJoseph Mingrone * the way monitor mode was done on
22386f9cba8fSJoseph Mingrone * 10.4 Tiger.
22396f9cba8fSJoseph Mingrone */
22406f9cba8fSJoseph Mingrone status = check_setif_failure(p, status);
2241a8e07101SRui Paulo goto bad;
2242a8e07101SRui Paulo }
2243a8e07101SRui Paulo } else {
2244a8e07101SRui Paulo /*
2245a8e07101SRui Paulo * No buffer size was explicitly specified.
2246a8e07101SRui Paulo *
2247a0ee43a1SRui Paulo * Try finding a good size for the buffer;
2248a0ee43a1SRui Paulo * DEFAULT_BUFSIZE may be too big, so keep
2249a0ee43a1SRui Paulo * cutting it in half until we find a size
2250a0ee43a1SRui Paulo * that works, or run out of sizes to try.
2251a8e07101SRui Paulo * If the default is larger, don't make it smaller.
2252a8e07101SRui Paulo */
2253a8e07101SRui Paulo if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) ||
2254a0ee43a1SRui Paulo v < DEFAULT_BUFSIZE)
2255a0ee43a1SRui Paulo v = DEFAULT_BUFSIZE;
2256feb4ecdbSBruce M Simpson for ( ; v != 0; v >>= 1) {
2257a8e07101SRui Paulo /*
2258a8e07101SRui Paulo * Ignore the return value - this is because the
2259a8e07101SRui Paulo * call fails on BPF systems that don't have
2260a8e07101SRui Paulo * kernel malloc. And if the call fails, it's
2261a8e07101SRui Paulo * no big deal, we just continue to use the
2262a8e07101SRui Paulo * standard buffer size.
2263a4b5b39fSBill Fenner */
2264a4b5b39fSBill Fenner (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
2265a4b5b39fSBill Fenner
22666f9cba8fSJoseph Mingrone status = bpf_bind(fd, p->opt.device, p->errbuf);
22676f9cba8fSJoseph Mingrone if (status == BPF_BIND_SUCCEEDED)
2268dc2c7305SBill Fenner break; /* that size worked; we're done */
2269dc2c7305SBill Fenner
22706f9cba8fSJoseph Mingrone /*
22716f9cba8fSJoseph Mingrone * If the attempt failed because the
22726f9cba8fSJoseph Mingrone * buffer was too big, cut the buffer
22736f9cba8fSJoseph Mingrone * size in half and try again.
22746f9cba8fSJoseph Mingrone *
22756f9cba8fSJoseph Mingrone * Otherwise, fail.
22766f9cba8fSJoseph Mingrone */
22776f9cba8fSJoseph Mingrone if (status != BPF_BIND_BUFFER_TOO_BIG) {
22786f9cba8fSJoseph Mingrone /*
22796f9cba8fSJoseph Mingrone * Special checks on macOS to deal
22806f9cba8fSJoseph Mingrone * with the way monitor mode was
22816f9cba8fSJoseph Mingrone * done on 10.4 Tiger.
22826f9cba8fSJoseph Mingrone */
22836f9cba8fSJoseph Mingrone status = check_setif_failure(p, status);
22848cf6c252SPaul Traina goto bad;
22858cf6c252SPaul Traina }
2286dc2c7305SBill Fenner }
2287dc2c7305SBill Fenner
2288dc2c7305SBill Fenner if (v == 0) {
22896f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2290a8e07101SRui Paulo "BIOCSBLEN: %s: No buffer size worked",
2291ada6f083SXin LI p->opt.device);
2292a8e07101SRui Paulo status = PCAP_ERROR;
2293dc2c7305SBill Fenner goto bad;
2294dc2c7305SBill Fenner }
2295a8e07101SRui Paulo }
2296154bbe41SChristian S.J. Peron }
2297dc2c7305SBill Fenner
22988cf6c252SPaul Traina /* Get the data link layer type. */
22998cf6c252SPaul Traina if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
2300*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2301b00ab754SHans Petter Selasky errno, "BIOCGDLT");
2302a8e07101SRui Paulo status = PCAP_ERROR;
23038cf6c252SPaul Traina goto bad;
23048cf6c252SPaul Traina }
2305a8e07101SRui Paulo
23060a94d38fSBill Fenner #ifdef _AIX
2307dc2c7305SBill Fenner /*
23080a94d38fSBill Fenner * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
2309dc2c7305SBill Fenner */
23100a94d38fSBill Fenner switch (v) {
23110a94d38fSBill Fenner
23120a94d38fSBill Fenner case IFT_ETHER:
23130a94d38fSBill Fenner case IFT_ISO88023:
23140a94d38fSBill Fenner v = DLT_EN10MB;
23158751327cSBill Fenner break;
23160a94d38fSBill Fenner
23170a94d38fSBill Fenner case IFT_FDDI:
23180a94d38fSBill Fenner v = DLT_FDDI;
23190a94d38fSBill Fenner break;
23200a94d38fSBill Fenner
23210a94d38fSBill Fenner case IFT_ISO88025:
23220a94d38fSBill Fenner v = DLT_IEEE802;
23230a94d38fSBill Fenner break;
23240a94d38fSBill Fenner
2325feb4ecdbSBruce M Simpson case IFT_LOOP:
2326feb4ecdbSBruce M Simpson v = DLT_NULL;
2327feb4ecdbSBruce M Simpson break;
2328feb4ecdbSBruce M Simpson
23290a94d38fSBill Fenner default:
23300a94d38fSBill Fenner /*
23310a94d38fSBill Fenner * We don't know what to map this to yet.
23320a94d38fSBill Fenner */
23336f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
23340a94d38fSBill Fenner v);
2335a8e07101SRui Paulo status = PCAP_ERROR;
23360a94d38fSBill Fenner goto bad;
23378751327cSBill Fenner }
23388751327cSBill Fenner #endif
2339*afdbf109SJoseph Mingrone #if defined(_BSDI_VERSION) && _BSDI_VERSION >= 199510
2340a4b5b39fSBill Fenner /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
2341a4b5b39fSBill Fenner switch (v) {
2342a4b5b39fSBill Fenner
2343a4b5b39fSBill Fenner case DLT_SLIP:
2344a4b5b39fSBill Fenner v = DLT_SLIP_BSDOS;
2345a4b5b39fSBill Fenner break;
2346a4b5b39fSBill Fenner
2347a4b5b39fSBill Fenner case DLT_PPP:
2348a4b5b39fSBill Fenner v = DLT_PPP_BSDOS;
2349a4b5b39fSBill Fenner break;
23508751327cSBill Fenner
23518751327cSBill Fenner case 11: /*DLT_FR*/
2352feb4ecdbSBruce M Simpson v = DLT_FRELAY;
23538751327cSBill Fenner break;
23548751327cSBill Fenner
23558751327cSBill Fenner case 12: /*DLT_C_HDLC*/
23568751327cSBill Fenner v = DLT_CHDLC;
23578751327cSBill Fenner break;
2358a4b5b39fSBill Fenner }
2359a4b5b39fSBill Fenner #endif
23608cf6c252SPaul Traina
2361feb4ecdbSBruce M Simpson #ifdef BIOCGDLTLIST
236209f33d61SBill Fenner /*
2363feb4ecdbSBruce M Simpson * We know the default link type -- now determine all the DLTs
2364feb4ecdbSBruce M Simpson * this interface supports. If this fails with EINVAL, it's
2365feb4ecdbSBruce M Simpson * not fatal; we just don't get to use the feature later.
236609f33d61SBill Fenner */
2367a8e07101SRui Paulo if (get_dlt_list(fd, v, &bdl, p->errbuf) == -1) {
2368a8e07101SRui Paulo status = PCAP_ERROR;
236909f33d61SBill Fenner goto bad;
237009f33d61SBill Fenner }
237109f33d61SBill Fenner p->dlt_count = bdl.bfl_len;
237209f33d61SBill Fenner p->dlt_list = bdl.bfl_list;
2373a8e07101SRui Paulo
2374a8e07101SRui Paulo #ifdef __APPLE__
2375a8e07101SRui Paulo /*
2376a8e07101SRui Paulo * Monitor mode fun, continued.
2377a8e07101SRui Paulo *
2378a8e07101SRui Paulo * For 10.5 and, we're assuming, later releases, as noted above,
2379a8e07101SRui Paulo * 802.1 adapters that support monitor mode offer both DLT_EN10MB,
2380a8e07101SRui Paulo * DLT_IEEE802_11, and possibly some 802.11-plus-radio-information
2381a8e07101SRui Paulo * DLT_ value. Choosing one of the 802.11 DLT_ values will turn
2382a8e07101SRui Paulo * monitor mode on.
2383a8e07101SRui Paulo *
2384a8e07101SRui Paulo * Therefore, if the user asked for monitor mode, we filter out
2385a8e07101SRui Paulo * the DLT_EN10MB value, as you can't get that in monitor mode,
2386a8e07101SRui Paulo * and, if the user didn't ask for monitor mode, we filter out
2387a8e07101SRui Paulo * the 802.11 DLT_ values, because selecting those will turn
2388a8e07101SRui Paulo * monitor mode on. Then, for monitor mode, if an 802.11-plus-
2389a8e07101SRui Paulo * radio DLT_ value is offered, we try to select that, otherwise
2390a8e07101SRui Paulo * we try to select DLT_IEEE802_11.
2391a8e07101SRui Paulo */
2392a8e07101SRui Paulo if (have_osinfo) {
23936f9cba8fSJoseph Mingrone if (PCAP_ISDIGIT((unsigned)osinfo.release[0]) &&
2394a8e07101SRui Paulo (osinfo.release[0] == '9' ||
23956f9cba8fSJoseph Mingrone PCAP_ISDIGIT((unsigned)osinfo.release[1]))) {
2396a8e07101SRui Paulo /*
2397a8e07101SRui Paulo * 10.5 (Darwin 9.x), or later.
2398a8e07101SRui Paulo */
2399a8e07101SRui Paulo new_dlt = find_802_11(&bdl);
2400a8e07101SRui Paulo if (new_dlt != -1) {
2401a8e07101SRui Paulo /*
2402a8e07101SRui Paulo * We have at least one 802.11 DLT_ value,
2403a8e07101SRui Paulo * so this is an 802.11 interface.
2404a8e07101SRui Paulo * new_dlt is the best of the 802.11
2405a8e07101SRui Paulo * DLT_ values in the list.
2406a8e07101SRui Paulo */
2407a8e07101SRui Paulo if (p->opt.rfmon) {
2408a8e07101SRui Paulo /*
2409a8e07101SRui Paulo * Our caller wants monitor mode.
2410a8e07101SRui Paulo * Purge DLT_EN10MB from the list
2411a8e07101SRui Paulo * of link-layer types, as selecting
2412a8e07101SRui Paulo * it will keep monitor mode off.
2413a8e07101SRui Paulo */
241457e22627SCy Schubert remove_non_802_11(p);
2415a8e07101SRui Paulo
2416a8e07101SRui Paulo /*
2417a8e07101SRui Paulo * If the new mode we want isn't
2418a8e07101SRui Paulo * the default mode, attempt to
2419a8e07101SRui Paulo * select the new mode.
2420a8e07101SRui Paulo */
2421ada6f083SXin LI if ((u_int)new_dlt != v) {
2422a8e07101SRui Paulo if (ioctl(p->fd, BIOCSDLT,
2423a8e07101SRui Paulo &new_dlt) != -1) {
2424a8e07101SRui Paulo /*
2425a8e07101SRui Paulo * We succeeded;
2426a8e07101SRui Paulo * make this the
2427a8e07101SRui Paulo * new DLT_ value.
2428a8e07101SRui Paulo */
2429a8e07101SRui Paulo v = new_dlt;
2430a8e07101SRui Paulo }
2431a8e07101SRui Paulo }
2432feb4ecdbSBruce M Simpson } else {
2433a8e07101SRui Paulo /*
2434a8e07101SRui Paulo * Our caller doesn't want
2435a8e07101SRui Paulo * monitor mode. Unless this
2436a8e07101SRui Paulo * is being done by pcap_open_live(),
2437a8e07101SRui Paulo * purge the 802.11 link-layer types
2438a8e07101SRui Paulo * from the list, as selecting
2439a8e07101SRui Paulo * one of them will turn monitor
2440a8e07101SRui Paulo * mode on.
2441a8e07101SRui Paulo */
2442a8e07101SRui Paulo if (!p->oldstyle)
2443a8e07101SRui Paulo remove_802_11(p);
2444a8e07101SRui Paulo }
2445a8e07101SRui Paulo } else {
2446a8e07101SRui Paulo if (p->opt.rfmon) {
2447a8e07101SRui Paulo /*
2448a8e07101SRui Paulo * The caller requested monitor
2449a8e07101SRui Paulo * mode, but we have no 802.11
2450a8e07101SRui Paulo * link-layer types, so they
2451a8e07101SRui Paulo * can't have it.
2452a8e07101SRui Paulo */
2453a8e07101SRui Paulo status = PCAP_ERROR_RFMON_NOTSUP;
2454feb4ecdbSBruce M Simpson goto bad;
245509f33d61SBill Fenner }
2456feb4ecdbSBruce M Simpson }
2457a8e07101SRui Paulo }
2458a8e07101SRui Paulo }
2459a8e07101SRui Paulo #elif defined(HAVE_BSD_IEEE80211)
2460a8e07101SRui Paulo /*
2461a8e07101SRui Paulo * *BSD with the new 802.11 ioctls.
2462a8e07101SRui Paulo * Do we want monitor mode?
2463a8e07101SRui Paulo */
2464a8e07101SRui Paulo if (p->opt.rfmon) {
2465a8e07101SRui Paulo /*
2466a8e07101SRui Paulo * Try to put the interface into monitor mode.
2467a8e07101SRui Paulo */
2468681ed54cSXin LI retv = monitor_mode(p, 1);
2469681ed54cSXin LI if (retv != 0) {
2470a8e07101SRui Paulo /*
2471a8e07101SRui Paulo * We failed.
2472a8e07101SRui Paulo */
2473681ed54cSXin LI status = retv;
2474a8e07101SRui Paulo goto bad;
2475a8e07101SRui Paulo }
2476a8e07101SRui Paulo
2477a8e07101SRui Paulo /*
2478a8e07101SRui Paulo * We're in monitor mode.
2479a8e07101SRui Paulo * Try to find the best 802.11 DLT_ value and, if we
2480a8e07101SRui Paulo * succeed, try to switch to that mode if we're not
2481a8e07101SRui Paulo * already in that mode.
2482a8e07101SRui Paulo */
2483a8e07101SRui Paulo new_dlt = find_802_11(&bdl);
2484a8e07101SRui Paulo if (new_dlt != -1) {
2485a8e07101SRui Paulo /*
2486a8e07101SRui Paulo * We have at least one 802.11 DLT_ value.
2487a8e07101SRui Paulo * new_dlt is the best of the 802.11
2488a8e07101SRui Paulo * DLT_ values in the list.
2489a8e07101SRui Paulo *
2490a8e07101SRui Paulo * If the new mode we want isn't the default mode,
2491a8e07101SRui Paulo * attempt to select the new mode.
2492a8e07101SRui Paulo */
2493ada6f083SXin LI if ((u_int)new_dlt != v) {
2494a8e07101SRui Paulo if (ioctl(p->fd, BIOCSDLT, &new_dlt) != -1) {
2495a8e07101SRui Paulo /*
2496a8e07101SRui Paulo * We succeeded; make this the
2497a8e07101SRui Paulo * new DLT_ value.
2498a8e07101SRui Paulo */
2499a8e07101SRui Paulo v = new_dlt;
2500a8e07101SRui Paulo }
2501a8e07101SRui Paulo }
2502a8e07101SRui Paulo }
2503a8e07101SRui Paulo }
2504a8e07101SRui Paulo #endif /* various platforms */
2505a8e07101SRui Paulo #endif /* BIOCGDLTLIST */
250609f33d61SBill Fenner
250704fb2745SSam Leffler /*
250804fb2745SSam Leffler * If this is an Ethernet device, and we don't have a DLT_ list,
250904fb2745SSam Leffler * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
251004fb2745SSam Leffler * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
251104fb2745SSam Leffler * do, but there's not much we can do about that without finding
251204fb2745SSam Leffler * some other way of determining whether it's an Ethernet or 802.11
251304fb2745SSam Leffler * device.)
251404fb2745SSam Leffler */
2515a8e07101SRui Paulo if (v == DLT_EN10MB && p->dlt_count == 0) {
251604fb2745SSam Leffler p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
2517*afdbf109SJoseph Mingrone if (p->dlt_list == NULL) {
2518*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2519*afdbf109SJoseph Mingrone errno, "malloc");
2520*afdbf109SJoseph Mingrone status = PCAP_ERROR;
2521*afdbf109SJoseph Mingrone goto bad;
2522*afdbf109SJoseph Mingrone }
252304fb2745SSam Leffler p->dlt_list[0] = DLT_EN10MB;
252404fb2745SSam Leffler p->dlt_list[1] = DLT_DOCSIS;
252504fb2745SSam Leffler p->dlt_count = 2;
252604fb2745SSam Leffler }
2527a8e07101SRui Paulo #ifdef PCAP_FDDIPAD
2528a8e07101SRui Paulo if (v == DLT_FDDI)
2529a8e07101SRui Paulo p->fddipad = PCAP_FDDIPAD;
2530a8e07101SRui Paulo else
2531a8e07101SRui Paulo #endif
2532681ed54cSXin LI p->fddipad = 0;
2533a8e07101SRui Paulo p->linktype = v;
253404fb2745SSam Leffler
253504fb2745SSam Leffler #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
253604fb2745SSam Leffler /*
253704fb2745SSam Leffler * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
253804fb2745SSam Leffler * the link-layer source address isn't forcibly overwritten.
253904fb2745SSam Leffler * (Should we ignore errors? Should we do this only if
254004fb2745SSam Leffler * we're open for writing?)
254104fb2745SSam Leffler *
254204fb2745SSam Leffler * XXX - I seem to remember some packet-sending bug in some
254304fb2745SSam Leffler * BSDs - check CVS log for "bpf.c"?
254404fb2745SSam Leffler */
254504fb2745SSam Leffler if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
2546*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2547b00ab754SHans Petter Selasky errno, "BIOCSHDRCMPLT");
2548a8e07101SRui Paulo status = PCAP_ERROR;
254904fb2745SSam Leffler goto bad;
255004fb2745SSam Leffler }
255104fb2745SSam Leffler #endif
25528cf6c252SPaul Traina /* set timeout */
2553a8e07101SRui Paulo #ifdef HAVE_ZEROCOPY_BPF
2554681ed54cSXin LI /*
2555681ed54cSXin LI * In zero-copy mode, we just use the timeout in select().
2556681ed54cSXin LI * XXX - what if we're in non-blocking mode and the *application*
2557681ed54cSXin LI * is using select() or poll() or kqueues or....?
2558681ed54cSXin LI */
2559681ed54cSXin LI if (p->opt.timeout && !pb->zerocopy) {
2560a8e07101SRui Paulo #else
2561681ed54cSXin LI if (p->opt.timeout) {
2562a8e07101SRui Paulo #endif
2563feb4ecdbSBruce M Simpson /*
2564feb4ecdbSBruce M Simpson * XXX - is this seconds/nanoseconds in AIX?
2565feb4ecdbSBruce M Simpson * (Treating it as such doesn't fix the timeout
2566feb4ecdbSBruce M Simpson * problem described below.)
2567a0ee43a1SRui Paulo *
2568a0ee43a1SRui Paulo * XXX - Mac OS X 10.6 mishandles BIOCSRTIMEOUT in
2569a0ee43a1SRui Paulo * 64-bit userland - it takes, as an argument, a
2570a0ee43a1SRui Paulo * "struct BPF_TIMEVAL", which has 32-bit tv_sec
2571a0ee43a1SRui Paulo * and tv_usec, rather than a "struct timeval".
2572a0ee43a1SRui Paulo *
2573a0ee43a1SRui Paulo * If this platform defines "struct BPF_TIMEVAL",
2574a0ee43a1SRui Paulo * we check whether the structure size in BIOCSRTIMEOUT
2575a0ee43a1SRui Paulo * is that of a "struct timeval" and, if not, we use
2576a0ee43a1SRui Paulo * a "struct BPF_TIMEVAL" rather than a "struct timeval".
2577a0ee43a1SRui Paulo * (That way, if the bug is fixed in a future release,
2578a0ee43a1SRui Paulo * we will still do the right thing.)
2579feb4ecdbSBruce M Simpson */
25808cf6c252SPaul Traina struct timeval to;
2581a0ee43a1SRui Paulo #ifdef HAVE_STRUCT_BPF_TIMEVAL
2582a0ee43a1SRui Paulo struct BPF_TIMEVAL bpf_to;
2583a0ee43a1SRui Paulo
2584a0ee43a1SRui Paulo if (IOCPARM_LEN(BIOCSRTIMEOUT) != sizeof(struct timeval)) {
2585681ed54cSXin LI bpf_to.tv_sec = p->opt.timeout / 1000;
2586681ed54cSXin LI bpf_to.tv_usec = (p->opt.timeout * 1000) % 1000000;
2587a0ee43a1SRui Paulo if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&bpf_to) < 0) {
2588*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
2589b00ab754SHans Petter Selasky errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT");
2590a8e07101SRui Paulo status = PCAP_ERROR;
25918cf6c252SPaul Traina goto bad;
25928cf6c252SPaul Traina }
2593a0ee43a1SRui Paulo } else {
2594a0ee43a1SRui Paulo #endif
2595681ed54cSXin LI to.tv_sec = p->opt.timeout / 1000;
2596681ed54cSXin LI to.tv_usec = (p->opt.timeout * 1000) % 1000000;
2597a0ee43a1SRui Paulo if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
2598*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
2599b00ab754SHans Petter Selasky errno, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT");
2600a0ee43a1SRui Paulo status = PCAP_ERROR;
2601a0ee43a1SRui Paulo goto bad;
2602a0ee43a1SRui Paulo }
2603a0ee43a1SRui Paulo #ifdef HAVE_STRUCT_BPF_TIMEVAL
2604a0ee43a1SRui Paulo }
2605a0ee43a1SRui Paulo #endif
26068cf6c252SPaul Traina }
2607dc2c7305SBill Fenner
2608dc2c7305SBill Fenner #ifdef BIOCIMMEDIATE
2609dc2c7305SBill Fenner /*
2610dc2c7305SBill Fenner * Darren Reed notes that
2611dc2c7305SBill Fenner *
2612dc2c7305SBill Fenner * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
2613dc2c7305SBill Fenner * timeout appears to be ignored and it waits until the buffer
2614dc2c7305SBill Fenner * is filled before returning. The result of not having it
2615dc2c7305SBill Fenner * set is almost worse than useless if your BPF filter
2616dc2c7305SBill Fenner * is reducing things to only a few packets (i.e. one every
2617dc2c7305SBill Fenner * second or so).
2618dc2c7305SBill Fenner *
2619681ed54cSXin LI * so we always turn BIOCIMMEDIATE mode on if this is AIX.
2620dc2c7305SBill Fenner *
2621681ed54cSXin LI * For other platforms, we don't turn immediate mode on by default,
2622681ed54cSXin LI * as that would mean we get woken up for every packet, which
2623681ed54cSXin LI * probably isn't what you want for a packet sniffer.
2624dc2c7305SBill Fenner *
2625681ed54cSXin LI * We set immediate mode if the caller requested it by calling
2626681ed54cSXin LI * pcap_set_immediate() before calling pcap_activate().
2627dc2c7305SBill Fenner */
2628681ed54cSXin LI #ifndef _AIX
2629681ed54cSXin LI if (p->opt.immediate) {
2630681ed54cSXin LI #endif /* _AIX */
2631dc2c7305SBill Fenner v = 1;
2632dc2c7305SBill Fenner if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
2633*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2634b00ab754SHans Petter Selasky errno, "BIOCIMMEDIATE");
2635681ed54cSXin LI status = PCAP_ERROR;
2636681ed54cSXin LI goto bad;
2637681ed54cSXin LI }
2638681ed54cSXin LI #ifndef _AIX
2639681ed54cSXin LI }
2640681ed54cSXin LI #endif /* _AIX */
2641681ed54cSXin LI #else /* BIOCIMMEDIATE */
2642681ed54cSXin LI if (p->opt.immediate) {
2643681ed54cSXin LI /*
2644681ed54cSXin LI * We don't support immediate mode. Fail.
2645681ed54cSXin LI */
26466f9cba8fSJoseph Mingrone snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "Immediate mode not supported");
2647a8e07101SRui Paulo status = PCAP_ERROR;
2648dc2c7305SBill Fenner goto bad;
2649dc2c7305SBill Fenner }
2650dc2c7305SBill Fenner #endif /* BIOCIMMEDIATE */
2651dc2c7305SBill Fenner
2652a8e07101SRui Paulo if (p->opt.promisc) {
2653a8e07101SRui Paulo /* set promiscuous mode, just warn if it fails */
26540a94d38fSBill Fenner if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
2655*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2656b00ab754SHans Petter Selasky errno, "BIOCPROMISC");
2657a8e07101SRui Paulo status = PCAP_WARNING_PROMISC_NOTSUP;
26580a94d38fSBill Fenner }
26590a94d38fSBill Fenner }
26608cf6c252SPaul Traina
2661a4d10fa9SJung-uk Kim #ifdef BIOCSTSTAMP
2662a4d10fa9SJung-uk Kim v = BPF_T_BINTIME;
2663a4d10fa9SJung-uk Kim if (ioctl(p->fd, BIOCSTSTAMP, &v) < 0) {
2664*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2665b00ab754SHans Petter Selasky errno, "BIOCSTSTAMP");
2666a4d10fa9SJung-uk Kim status = PCAP_ERROR;
2667a4d10fa9SJung-uk Kim goto bad;
2668a4d10fa9SJung-uk Kim }
2669a4d10fa9SJung-uk Kim #endif /* BIOCSTSTAMP */
2670a4d10fa9SJung-uk Kim
26718cf6c252SPaul Traina if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
2672*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2673b00ab754SHans Petter Selasky errno, "BIOCGBLEN");
2674a8e07101SRui Paulo status = PCAP_ERROR;
26758cf6c252SPaul Traina goto bad;
26768cf6c252SPaul Traina }
26778cf6c252SPaul Traina p->bufsize = v;
2678a8e07101SRui Paulo #ifdef HAVE_ZEROCOPY_BPF
2679681ed54cSXin LI if (!pb->zerocopy) {
2680154bbe41SChristian S.J. Peron #endif
2681ada6f083SXin LI p->buffer = malloc(p->bufsize);
26828cf6c252SPaul Traina if (p->buffer == NULL) {
2683*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2684b00ab754SHans Petter Selasky errno, "malloc");
2685a8e07101SRui Paulo status = PCAP_ERROR;
26868cf6c252SPaul Traina goto bad;
26878cf6c252SPaul Traina }
2688feb4ecdbSBruce M Simpson #ifdef _AIX
2689feb4ecdbSBruce M Simpson /* For some strange reason this seems to prevent the EFAULT
2690feb4ecdbSBruce M Simpson * problems we have experienced from AIX BPF. */
2691feb4ecdbSBruce M Simpson memset(p->buffer, 0x0, p->bufsize);
2692feb4ecdbSBruce M Simpson #endif
2693a8e07101SRui Paulo #ifdef HAVE_ZEROCOPY_BPF
2694154bbe41SChristian S.J. Peron }
2695154bbe41SChristian S.J. Peron #endif
2696feb4ecdbSBruce M Simpson
2697feb4ecdbSBruce M Simpson /*
269804fb2745SSam Leffler * If there's no filter program installed, there's
269904fb2745SSam Leffler * no indication to the kernel of what the snapshot
270004fb2745SSam Leffler * length should be, so no snapshotting is done.
270104fb2745SSam Leffler *
270204fb2745SSam Leffler * Therefore, when we open the device, we install
270304fb2745SSam Leffler * an "accept everything" filter with the specified
270404fb2745SSam Leffler * snapshot length.
270504fb2745SSam Leffler */
270604fb2745SSam Leffler total_insn.code = (u_short)(BPF_RET | BPF_K);
270704fb2745SSam Leffler total_insn.jt = 0;
270804fb2745SSam Leffler total_insn.jf = 0;
2709a8e07101SRui Paulo total_insn.k = p->snapshot;
271004fb2745SSam Leffler
271104fb2745SSam Leffler total_prog.bf_len = 1;
271204fb2745SSam Leffler total_prog.bf_insns = &total_insn;
271304fb2745SSam Leffler if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
2714*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
2715b00ab754SHans Petter Selasky errno, "BIOCSETF");
2716a8e07101SRui Paulo status = PCAP_ERROR;
271704fb2745SSam Leffler goto bad;
271804fb2745SSam Leffler }
271904fb2745SSam Leffler
272004fb2745SSam Leffler /*
2721feb4ecdbSBruce M Simpson * On most BPF platforms, either you can do a "select()" or
2722feb4ecdbSBruce M Simpson * "poll()" on a BPF file descriptor and it works correctly,
2723feb4ecdbSBruce M Simpson * or you can do it and it will return "readable" if the
2724feb4ecdbSBruce M Simpson * hold buffer is full but not if the timeout expires *and*
2725feb4ecdbSBruce M Simpson * a non-blocking read will, if the hold buffer is empty
2726feb4ecdbSBruce M Simpson * but the store buffer isn't empty, rotate the buffers
2727feb4ecdbSBruce M Simpson * and return what packets are available.
2728feb4ecdbSBruce M Simpson *
2729feb4ecdbSBruce M Simpson * In the latter case, the fact that a non-blocking read
2730feb4ecdbSBruce M Simpson * will give you the available packets means you can work
2731feb4ecdbSBruce M Simpson * around the failure of "select()" and "poll()" to wake up
2732feb4ecdbSBruce M Simpson * and return "readable" when the timeout expires by using
2733feb4ecdbSBruce M Simpson * the timeout as the "select()" or "poll()" timeout, putting
2734feb4ecdbSBruce M Simpson * the BPF descriptor into non-blocking mode, and read from
2735feb4ecdbSBruce M Simpson * it regardless of whether "select()" reports it as readable
2736feb4ecdbSBruce M Simpson * or not.
2737feb4ecdbSBruce M Simpson *
2738feb4ecdbSBruce M Simpson * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
2739feb4ecdbSBruce M Simpson * won't wake up and return "readable" if the timer expires
2740feb4ecdbSBruce M Simpson * and non-blocking reads return EWOULDBLOCK if the hold
2741feb4ecdbSBruce M Simpson * buffer is empty, even if the store buffer is non-empty.
2742feb4ecdbSBruce M Simpson *
2743feb4ecdbSBruce M Simpson * This means the workaround in question won't work.
2744feb4ecdbSBruce M Simpson *
2745feb4ecdbSBruce M Simpson * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
2746feb4ecdbSBruce M Simpson * to -1, which means "sorry, you can't use 'select()' or 'poll()'
2747feb4ecdbSBruce M Simpson * here". On all other BPF platforms, we set it to the FD for
2748feb4ecdbSBruce M Simpson * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
2749feb4ecdbSBruce M Simpson * read will, if the hold buffer is empty and the store buffer
2750feb4ecdbSBruce M Simpson * isn't empty, rotate the buffers and return what packets are
2751feb4ecdbSBruce M Simpson * there (and in sufficiently recent versions of OpenBSD
2752feb4ecdbSBruce M Simpson * "select()" and "poll()" should work correctly).
2753feb4ecdbSBruce M Simpson *
2754feb4ecdbSBruce M Simpson * XXX - what about AIX?
2755feb4ecdbSBruce M Simpson */
2756ee2dd488SSam Leffler p->selectable_fd = p->fd; /* assume select() works until we know otherwise */
2757a8e07101SRui Paulo if (have_osinfo) {
2758feb4ecdbSBruce M Simpson /*
2759feb4ecdbSBruce M Simpson * We can check what OS this is.
2760feb4ecdbSBruce M Simpson */
2761ee2dd488SSam Leffler if (strcmp(osinfo.sysname, "FreeBSD") == 0) {
2762ee2dd488SSam Leffler if (strncmp(osinfo.release, "4.3-", 4) == 0 ||
2763ee2dd488SSam Leffler strncmp(osinfo.release, "4.4-", 4) == 0)
2764feb4ecdbSBruce M Simpson p->selectable_fd = -1;
2765ee2dd488SSam Leffler }
2766feb4ecdbSBruce M Simpson }
2767feb4ecdbSBruce M Simpson
2768feb4ecdbSBruce M Simpson p->read_op = pcap_read_bpf;
276904fb2745SSam Leffler p->inject_op = pcap_inject_bpf;
2770feb4ecdbSBruce M Simpson p->setfilter_op = pcap_setfilter_bpf;
2771ee2dd488SSam Leffler p->setdirection_op = pcap_setdirection_bpf;
2772feb4ecdbSBruce M Simpson p->set_datalink_op = pcap_set_datalink_bpf;
2773d1e87331SXin LI p->getnonblock_op = pcap_getnonblock_bpf;
2774d1e87331SXin LI p->setnonblock_op = pcap_setnonblock_bpf;
2775feb4ecdbSBruce M Simpson p->stats_op = pcap_stats_bpf;
2776a8e07101SRui Paulo p->cleanup_op = pcap_cleanup_bpf;
27778cf6c252SPaul Traina
2778a8e07101SRui Paulo return (status);
27798cf6c252SPaul Traina bad:
2780a8e07101SRui Paulo pcap_cleanup_bpf(p);
2781a8e07101SRui Paulo return (status);
27828cf6c252SPaul Traina }
27838cf6c252SPaul Traina
2784ada6f083SXin LI /*
2785ada6f083SXin LI * Not all interfaces can be bound to by BPF, so try to bind to
2786ada6f083SXin LI * the specified interface; return 0 if we fail with
2787ada6f083SXin LI * PCAP_ERROR_NO_SUCH_DEVICE (which means we got an ENXIO when we tried
2788ada6f083SXin LI * to bind, which means this interface isn't in the list of interfaces
2789ada6f083SXin LI * attached to BPF) and 1 otherwise.
2790ada6f083SXin LI */
2791ada6f083SXin LI static int
2792ada6f083SXin LI check_bpf_bindable(const char *name)
2793ada6f083SXin LI {
2794ada6f083SXin LI int fd;
2795ada6f083SXin LI char errbuf[PCAP_ERRBUF_SIZE];
2796ada6f083SXin LI
2797b00ab754SHans Petter Selasky /*
2798b00ab754SHans Petter Selasky * On macOS, we don't do this check if the device name begins
2799b00ab754SHans Petter Selasky * with "wlt"; at least some versions of macOS (actually, it
2800b00ab754SHans Petter Selasky * was called "Mac OS X" then...) offer monitor mode capturing
2801b00ab754SHans Petter Selasky * by having a separate "monitor mode" device for each wireless
2802b00ab754SHans Petter Selasky * adapter, rather than by implementing the ioctls that
2803b00ab754SHans Petter Selasky * {Free,Net,Open,DragonFly}BSD provide. Opening that device
2804b00ab754SHans Petter Selasky * puts the adapter into monitor mode, which, at least for
2805*afdbf109SJoseph Mingrone * some adapters, causes them to disassociate from the network
2806b00ab754SHans Petter Selasky * with which they're associated.
2807b00ab754SHans Petter Selasky *
2808b00ab754SHans Petter Selasky * Instead, we try to open the corresponding "en" device (so
2809b00ab754SHans Petter Selasky * that we don't end up with, for users without sufficient
2810b00ab754SHans Petter Selasky * privilege to open capture devices, a list of adapters that
2811b00ab754SHans Petter Selasky * only includes the wlt devices).
2812b00ab754SHans Petter Selasky */
2813b00ab754SHans Petter Selasky #ifdef __APPLE__
2814b00ab754SHans Petter Selasky if (strncmp(name, "wlt", 3) == 0) {
2815b00ab754SHans Petter Selasky char *en_name;
2816b00ab754SHans Petter Selasky size_t en_name_len;
2817b00ab754SHans Petter Selasky
2818b00ab754SHans Petter Selasky /*
2819b00ab754SHans Petter Selasky * Try to allocate a buffer for the "en"
2820b00ab754SHans Petter Selasky * device's name.
2821b00ab754SHans Petter Selasky */
2822b00ab754SHans Petter Selasky en_name_len = strlen(name) - 1;
2823b00ab754SHans Petter Selasky en_name = malloc(en_name_len + 1);
2824b00ab754SHans Petter Selasky if (en_name == NULL) {
2825*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
2826b00ab754SHans Petter Selasky errno, "malloc");
2827b00ab754SHans Petter Selasky return (-1);
2828b00ab754SHans Petter Selasky }
2829b00ab754SHans Petter Selasky strcpy(en_name, "en");
2830b00ab754SHans Petter Selasky strcat(en_name, name + 3);
2831b00ab754SHans Petter Selasky fd = bpf_open_and_bind(en_name, errbuf);
2832b00ab754SHans Petter Selasky free(en_name);
2833b00ab754SHans Petter Selasky } else
2834b00ab754SHans Petter Selasky #endif /* __APPLE */
2835ada6f083SXin LI fd = bpf_open_and_bind(name, errbuf);
2836ada6f083SXin LI if (fd < 0) {
2837ada6f083SXin LI /*
2838ada6f083SXin LI * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
2839ada6f083SXin LI */
2840ada6f083SXin LI if (fd == PCAP_ERROR_NO_SUCH_DEVICE) {
2841ada6f083SXin LI /*
2842ada6f083SXin LI * Yes, so we can't bind to this because it's
2843ada6f083SXin LI * not something supported by BPF.
2844ada6f083SXin LI */
2845ada6f083SXin LI return (0);
2846ada6f083SXin LI }
2847ada6f083SXin LI /*
2848ada6f083SXin LI * No, so we don't know whether it's supported or not;
2849ada6f083SXin LI * say it is, so that the user can at least try to
2850ada6f083SXin LI * open it and report the error (which is probably
2851ada6f083SXin LI * "you don't have permission to open BPF devices";
2852ada6f083SXin LI * reporting those interfaces means users will ask
2853ada6f083SXin LI * "why am I getting a permissions error when I try
2854ada6f083SXin LI * to capture" rather than "why am I not seeing any
2855ada6f083SXin LI * interfaces", making the underlying problem clearer).
2856ada6f083SXin LI */
2857ada6f083SXin LI return (1);
2858ada6f083SXin LI }
2859ada6f083SXin LI
2860ada6f083SXin LI /*
2861ada6f083SXin LI * Success.
2862ada6f083SXin LI */
2863ada6f083SXin LI close(fd);
2864ada6f083SXin LI return (1);
2865ada6f083SXin LI }
2866ada6f083SXin LI
2867ada6f083SXin LI #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
2868ada6f083SXin LI static int
2869b00ab754SHans Petter Selasky get_usb_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
2870b00ab754SHans Petter Selasky {
2871b00ab754SHans Petter Selasky /*
2872b00ab754SHans Petter Selasky * XXX - if there's a way to determine whether there's something
2873b00ab754SHans Petter Selasky * plugged into a given USB bus, use that to determine whether
2874b00ab754SHans Petter Selasky * this device is "connected" or not.
2875b00ab754SHans Petter Selasky */
2876b00ab754SHans Petter Selasky return (0);
2877b00ab754SHans Petter Selasky }
2878b00ab754SHans Petter Selasky
2879b00ab754SHans Petter Selasky static int
2880b00ab754SHans Petter Selasky finddevs_usb(pcap_if_list_t *devlistp, char *errbuf)
2881ada6f083SXin LI {
2882ada6f083SXin LI DIR *usbdir;
2883ada6f083SXin LI struct dirent *usbitem;
2884ada6f083SXin LI size_t name_max;
2885ada6f083SXin LI char *name;
2886ada6f083SXin LI
2887ada6f083SXin LI /*
2888ada6f083SXin LI * We might have USB sniffing support, so try looking for USB
2889ada6f083SXin LI * interfaces.
2890ada6f083SXin LI *
2891ada6f083SXin LI * We want to report a usbusN device for each USB bus, but
2892ada6f083SXin LI * usbusN interfaces might, or might not, exist for them -
2893ada6f083SXin LI * we create one if there isn't already one.
2894ada6f083SXin LI *
2895ada6f083SXin LI * So, instead, we look in /dev/usb for all buses and create
2896ada6f083SXin LI * a "usbusN" device for each one.
2897ada6f083SXin LI */
2898ada6f083SXin LI usbdir = opendir("/dev/usb");
2899ada6f083SXin LI if (usbdir == NULL) {
2900ada6f083SXin LI /*
2901ada6f083SXin LI * Just punt.
2902ada6f083SXin LI */
2903ada6f083SXin LI return (0);
2904ada6f083SXin LI }
2905ada6f083SXin LI
2906ada6f083SXin LI /*
2907ada6f083SXin LI * Leave enough room for a 32-bit (10-digit) bus number.
2908ada6f083SXin LI * Yes, that's overkill, but we won't be using
2909ada6f083SXin LI * the buffer very long.
2910ada6f083SXin LI */
2911ada6f083SXin LI name_max = USBUS_PREFIX_LEN + 10 + 1;
2912ada6f083SXin LI name = malloc(name_max);
2913ada6f083SXin LI if (name == NULL) {
2914ada6f083SXin LI closedir(usbdir);
2915ada6f083SXin LI return (0);
2916ada6f083SXin LI }
2917ada6f083SXin LI while ((usbitem = readdir(usbdir)) != NULL) {
2918ada6f083SXin LI char *p;
2919ada6f083SXin LI size_t busnumlen;
2920ada6f083SXin LI
2921ada6f083SXin LI if (strcmp(usbitem->d_name, ".") == 0 ||
2922ada6f083SXin LI strcmp(usbitem->d_name, "..") == 0) {
2923ada6f083SXin LI /*
2924ada6f083SXin LI * Ignore these.
2925ada6f083SXin LI */
2926ada6f083SXin LI continue;
2927ada6f083SXin LI }
2928ada6f083SXin LI p = strchr(usbitem->d_name, '.');
2929ada6f083SXin LI if (p == NULL)
2930ada6f083SXin LI continue;
2931ada6f083SXin LI busnumlen = p - usbitem->d_name;
2932ada6f083SXin LI memcpy(name, usbus_prefix, USBUS_PREFIX_LEN);
2933ada6f083SXin LI memcpy(name + USBUS_PREFIX_LEN, usbitem->d_name, busnumlen);
2934ada6f083SXin LI *(name + USBUS_PREFIX_LEN + busnumlen) = '\0';
2935b00ab754SHans Petter Selasky /*
2936b00ab754SHans Petter Selasky * There's an entry in this directory for every USB device,
2937b00ab754SHans Petter Selasky * not for every bus; if there's more than one device on
2938b00ab754SHans Petter Selasky * the bus, there'll be more than one entry for that bus,
2939b00ab754SHans Petter Selasky * so we need to avoid adding multiple capture devices
2940b00ab754SHans Petter Selasky * for each bus.
2941b00ab754SHans Petter Selasky */
2942*afdbf109SJoseph Mingrone if (pcapint_find_or_add_dev(devlistp, name, PCAP_IF_UP,
2943b00ab754SHans Petter Selasky get_usb_if_flags, NULL, errbuf) == NULL) {
2944ada6f083SXin LI free(name);
2945ada6f083SXin LI closedir(usbdir);
2946b00ab754SHans Petter Selasky return (PCAP_ERROR);
2947ada6f083SXin LI }
2948ada6f083SXin LI }
2949ada6f083SXin LI free(name);
2950ada6f083SXin LI closedir(usbdir);
2951ada6f083SXin LI return (0);
2952ada6f083SXin LI }
2953ada6f083SXin LI #endif
2954ada6f083SXin LI
2955b00ab754SHans Petter Selasky /*
2956b00ab754SHans Petter Selasky * Get additional flags for a device, using SIOCGIFMEDIA.
2957b00ab754SHans Petter Selasky */
2958b00ab754SHans Petter Selasky #ifdef SIOCGIFMEDIA
2959b00ab754SHans Petter Selasky static int
2960b00ab754SHans Petter Selasky get_if_flags(const char *name, bpf_u_int32 *flags, char *errbuf)
2961b00ab754SHans Petter Selasky {
2962b00ab754SHans Petter Selasky int sock;
2963b00ab754SHans Petter Selasky struct ifmediareq req;
2964b00ab754SHans Petter Selasky
2965b00ab754SHans Petter Selasky sock = socket(AF_INET, SOCK_DGRAM, 0);
2966b00ab754SHans Petter Selasky if (sock == -1) {
2967*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
2968b00ab754SHans Petter Selasky "Can't create socket to get media information for %s",
2969b00ab754SHans Petter Selasky name);
2970b00ab754SHans Petter Selasky return (-1);
2971b00ab754SHans Petter Selasky }
2972b00ab754SHans Petter Selasky memset(&req, 0, sizeof(req));
2973*afdbf109SJoseph Mingrone pcapint_strlcpy(req.ifm_name, name, sizeof(req.ifm_name));
2974b00ab754SHans Petter Selasky if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
2975b00ab754SHans Petter Selasky if (errno == EOPNOTSUPP || errno == EINVAL || errno == ENOTTY ||
29766f9cba8fSJoseph Mingrone errno == ENODEV || errno == EPERM
29776f9cba8fSJoseph Mingrone #ifdef EPWROFF
29786f9cba8fSJoseph Mingrone || errno == EPWROFF
29796f9cba8fSJoseph Mingrone #endif
29806f9cba8fSJoseph Mingrone ) {
2981b00ab754SHans Petter Selasky /*
2982b00ab754SHans Petter Selasky * Not supported, so we can't provide any
2983b00ab754SHans Petter Selasky * additional information. Assume that
2984b00ab754SHans Petter Selasky * this means that "connected" vs.
2985b00ab754SHans Petter Selasky * "disconnected" doesn't apply.
298657e22627SCy Schubert *
298757e22627SCy Schubert * The ioctl routine for Apple's pktap devices,
298857e22627SCy Schubert * annoyingly, checks for "are you root?" before
298957e22627SCy Schubert * checking whether the ioctl is valid, so it
299057e22627SCy Schubert * returns EPERM, rather than ENOTSUP, for the
299157e22627SCy Schubert * invalid SIOCGIFMEDIA, unless you're root.
299257e22627SCy Schubert * So, just as we do for some ethtool ioctls
299357e22627SCy Schubert * on Linux, which makes the same mistake, we
299457e22627SCy Schubert * also treat EPERM as meaning "not supported".
29956f9cba8fSJoseph Mingrone *
29966f9cba8fSJoseph Mingrone * And it appears that Apple's llw0 device, which
29976f9cba8fSJoseph Mingrone * appears to be part of the Skywalk subsystem:
29986f9cba8fSJoseph Mingrone *
29996f9cba8fSJoseph Mingrone * http://newosxbook.com/bonus/vol1ch16.html
30006f9cba8fSJoseph Mingrone *
30016f9cba8fSJoseph Mingrone * can sometimes return EPWROFF ("Device power
30026f9cba8fSJoseph Mingrone * is off") for that ioctl, so we treat *that*
30036f9cba8fSJoseph Mingrone * as another indication that we can't get a
30046f9cba8fSJoseph Mingrone * connection status. (If it *isn't* "powered
30056f9cba8fSJoseph Mingrone * off", it's reported as a wireless device,
30066f9cba8fSJoseph Mingrone * complete with an active/inactive state.)
3007b00ab754SHans Petter Selasky */
3008b00ab754SHans Petter Selasky *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
3009b00ab754SHans Petter Selasky close(sock);
3010b00ab754SHans Petter Selasky return (0);
3011b00ab754SHans Petter Selasky }
3012*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno,
3013b00ab754SHans Petter Selasky "SIOCGIFMEDIA on %s failed", name);
3014b00ab754SHans Petter Selasky close(sock);
3015b00ab754SHans Petter Selasky return (-1);
3016b00ab754SHans Petter Selasky }
3017b00ab754SHans Petter Selasky close(sock);
3018b00ab754SHans Petter Selasky
3019b00ab754SHans Petter Selasky /*
3020b00ab754SHans Petter Selasky * OK, what type of network is this?
3021b00ab754SHans Petter Selasky */
3022b00ab754SHans Petter Selasky switch (IFM_TYPE(req.ifm_active)) {
3023b00ab754SHans Petter Selasky
3024b00ab754SHans Petter Selasky case IFM_IEEE80211:
3025b00ab754SHans Petter Selasky /*
3026b00ab754SHans Petter Selasky * Wireless.
3027b00ab754SHans Petter Selasky */
3028b00ab754SHans Petter Selasky *flags |= PCAP_IF_WIRELESS;
3029b00ab754SHans Petter Selasky break;
3030b00ab754SHans Petter Selasky }
3031b00ab754SHans Petter Selasky
3032b00ab754SHans Petter Selasky /*
3033b00ab754SHans Petter Selasky * Do we know whether it's connected?
3034b00ab754SHans Petter Selasky */
3035b00ab754SHans Petter Selasky if (req.ifm_status & IFM_AVALID) {
3036b00ab754SHans Petter Selasky /*
3037b00ab754SHans Petter Selasky * Yes.
3038b00ab754SHans Petter Selasky */
3039b00ab754SHans Petter Selasky if (req.ifm_status & IFM_ACTIVE) {
3040b00ab754SHans Petter Selasky /*
3041b00ab754SHans Petter Selasky * It's connected.
3042b00ab754SHans Petter Selasky */
3043b00ab754SHans Petter Selasky *flags |= PCAP_IF_CONNECTION_STATUS_CONNECTED;
3044b00ab754SHans Petter Selasky } else {
3045b00ab754SHans Petter Selasky /*
3046b00ab754SHans Petter Selasky * It's disconnected.
3047b00ab754SHans Petter Selasky */
3048b00ab754SHans Petter Selasky *flags |= PCAP_IF_CONNECTION_STATUS_DISCONNECTED;
3049b00ab754SHans Petter Selasky }
3050b00ab754SHans Petter Selasky }
3051b00ab754SHans Petter Selasky return (0);
3052b00ab754SHans Petter Selasky }
3053b00ab754SHans Petter Selasky #else
3054b00ab754SHans Petter Selasky static int
30556f9cba8fSJoseph Mingrone get_if_flags(const char *name _U_, bpf_u_int32 *flags, char *errbuf _U_)
3056b00ab754SHans Petter Selasky {
3057b00ab754SHans Petter Selasky /*
3058b00ab754SHans Petter Selasky * Nothing we can do other than mark loopback devices as "the
3059b00ab754SHans Petter Selasky * connected/disconnected status doesn't apply".
3060b00ab754SHans Petter Selasky *
3061b00ab754SHans Petter Selasky * XXX - on Solaris, can we do what the dladm command does,
3062b00ab754SHans Petter Selasky * i.e. get a connected/disconnected indication from a kstat?
3063b00ab754SHans Petter Selasky * (Note that you can also get the link speed, and possibly
3064b00ab754SHans Petter Selasky * other information, from a kstat as well.)
3065b00ab754SHans Petter Selasky */
3066b00ab754SHans Petter Selasky if (*flags & PCAP_IF_LOOPBACK) {
3067b00ab754SHans Petter Selasky /*
3068b00ab754SHans Petter Selasky * Loopback devices aren't wireless, and "connected"/
3069b00ab754SHans Petter Selasky * "disconnected" doesn't apply to them.
3070b00ab754SHans Petter Selasky */
3071b00ab754SHans Petter Selasky *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
3072b00ab754SHans Petter Selasky return (0);
3073b00ab754SHans Petter Selasky }
3074b00ab754SHans Petter Selasky return (0);
3075b00ab754SHans Petter Selasky }
3076b00ab754SHans Petter Selasky #endif
3077b00ab754SHans Petter Selasky
30788cf6c252SPaul Traina int
3079*afdbf109SJoseph Mingrone pcapint_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
3080feb4ecdbSBruce M Simpson {
3081ada6f083SXin LI /*
3082ada6f083SXin LI * Get the list of regular interfaces first.
3083ada6f083SXin LI */
3084*afdbf109SJoseph Mingrone if (pcapint_findalldevs_interfaces(devlistp, errbuf, check_bpf_bindable,
3085b00ab754SHans Petter Selasky get_if_flags) == -1)
3086ada6f083SXin LI return (-1); /* failure */
3087ada6f083SXin LI
3088*afdbf109SJoseph Mingrone #if defined(HAVE_SOLARIS_ANY_DEVICE)
3089*afdbf109SJoseph Mingrone /*
3090*afdbf109SJoseph Mingrone * Add the "any" device.
3091*afdbf109SJoseph Mingrone */
3092*afdbf109SJoseph Mingrone if (pcap_add_any_dev(devlistp, errbuf) == NULL)
3093*afdbf109SJoseph Mingrone return (-1);
3094*afdbf109SJoseph Mingrone #endif
3095*afdbf109SJoseph Mingrone
3096ada6f083SXin LI #if defined(__FreeBSD__) && defined(SIOCIFCREATE2)
3097b00ab754SHans Petter Selasky if (finddevs_usb(devlistp, errbuf) == -1)
3098ada6f083SXin LI return (-1);
3099ada6f083SXin LI #endif
3100ada6f083SXin LI
3101feb4ecdbSBruce M Simpson return (0);
3102feb4ecdbSBruce M Simpson }
3103feb4ecdbSBruce M Simpson
3104a8e07101SRui Paulo #ifdef HAVE_BSD_IEEE80211
3105a8e07101SRui Paulo static int
3106a8e07101SRui Paulo monitor_mode(pcap_t *p, int set)
3107a8e07101SRui Paulo {
3108681ed54cSXin LI struct pcap_bpf *pb = p->priv;
3109a8e07101SRui Paulo int sock;
3110a8e07101SRui Paulo struct ifmediareq req;
3111ada6f083SXin LI IFM_ULIST_TYPE *media_list;
3112a8e07101SRui Paulo int i;
3113a8e07101SRui Paulo int can_do;
3114a8e07101SRui Paulo struct ifreq ifr;
3115a8e07101SRui Paulo
3116a8e07101SRui Paulo sock = socket(AF_INET, SOCK_DGRAM, 0);
3117a8e07101SRui Paulo if (sock == -1) {
3118*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3119b00ab754SHans Petter Selasky errno, "can't open socket");
3120a8e07101SRui Paulo return (PCAP_ERROR);
3121a8e07101SRui Paulo }
3122a8e07101SRui Paulo
3123a8e07101SRui Paulo memset(&req, 0, sizeof req);
3124*afdbf109SJoseph Mingrone pcapint_strlcpy(req.ifm_name, p->opt.device, sizeof req.ifm_name);
3125a8e07101SRui Paulo
3126a8e07101SRui Paulo /*
3127a8e07101SRui Paulo * Find out how many media types we have.
3128a8e07101SRui Paulo */
3129a8e07101SRui Paulo if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
3130a8e07101SRui Paulo /*
3131a8e07101SRui Paulo * Can't get the media types.
3132a8e07101SRui Paulo */
3133d1e87331SXin LI switch (errno) {
3134d1e87331SXin LI
3135d1e87331SXin LI case ENXIO:
3136d1e87331SXin LI /*
3137d1e87331SXin LI * There's no such device.
31386f9cba8fSJoseph Mingrone *
31396f9cba8fSJoseph Mingrone * There's nothing more to say, so clear the
31406f9cba8fSJoseph Mingrone * error message.
3141d1e87331SXin LI */
31426f9cba8fSJoseph Mingrone p->errbuf[0] = '\0';
3143d1e87331SXin LI close(sock);
3144d1e87331SXin LI return (PCAP_ERROR_NO_SUCH_DEVICE);
3145d1e87331SXin LI
3146d1e87331SXin LI case EINVAL:
3147a8e07101SRui Paulo /*
3148a8e07101SRui Paulo * Interface doesn't support SIOC{G,S}IFMEDIA.
3149a8e07101SRui Paulo */
3150a8e07101SRui Paulo close(sock);
3151a8e07101SRui Paulo return (PCAP_ERROR_RFMON_NOTSUP);
3152d1e87331SXin LI
3153d1e87331SXin LI default:
3154*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
315557e22627SCy Schubert errno, "SIOCGIFMEDIA");
3156a8e07101SRui Paulo close(sock);
3157a8e07101SRui Paulo return (PCAP_ERROR);
3158a8e07101SRui Paulo }
3159d1e87331SXin LI }
3160a8e07101SRui Paulo if (req.ifm_count == 0) {
3161a8e07101SRui Paulo /*
3162a8e07101SRui Paulo * No media types.
3163a8e07101SRui Paulo */
3164a8e07101SRui Paulo close(sock);
3165a8e07101SRui Paulo return (PCAP_ERROR_RFMON_NOTSUP);
3166a8e07101SRui Paulo }
3167a8e07101SRui Paulo
3168a8e07101SRui Paulo /*
3169a8e07101SRui Paulo * Allocate a buffer to hold all the media types, and
3170a8e07101SRui Paulo * get the media types.
3171a8e07101SRui Paulo */
3172ada6f083SXin LI media_list = malloc(req.ifm_count * sizeof(*media_list));
3173a8e07101SRui Paulo if (media_list == NULL) {
3174*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3175b00ab754SHans Petter Selasky errno, "malloc");
3176a8e07101SRui Paulo close(sock);
3177a8e07101SRui Paulo return (PCAP_ERROR);
3178a8e07101SRui Paulo }
3179a8e07101SRui Paulo req.ifm_ulist = media_list;
3180a8e07101SRui Paulo if (ioctl(sock, SIOCGIFMEDIA, &req) < 0) {
3181*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3182b00ab754SHans Petter Selasky errno, "SIOCGIFMEDIA");
3183a8e07101SRui Paulo free(media_list);
3184a8e07101SRui Paulo close(sock);
3185a8e07101SRui Paulo return (PCAP_ERROR);
3186a8e07101SRui Paulo }
3187a8e07101SRui Paulo
3188a8e07101SRui Paulo /*
3189a8e07101SRui Paulo * Look for an 802.11 "automatic" media type.
3190a8e07101SRui Paulo * We assume that all 802.11 adapters have that media type,
3191a8e07101SRui Paulo * and that it will carry the monitor mode supported flag.
3192a8e07101SRui Paulo */
3193a8e07101SRui Paulo can_do = 0;
3194a8e07101SRui Paulo for (i = 0; i < req.ifm_count; i++) {
3195a8e07101SRui Paulo if (IFM_TYPE(media_list[i]) == IFM_IEEE80211
3196a8e07101SRui Paulo && IFM_SUBTYPE(media_list[i]) == IFM_AUTO) {
3197a8e07101SRui Paulo /* OK, does it do monitor mode? */
3198a8e07101SRui Paulo if (media_list[i] & IFM_IEEE80211_MONITOR) {
3199a8e07101SRui Paulo can_do = 1;
3200a8e07101SRui Paulo break;
3201a8e07101SRui Paulo }
3202a8e07101SRui Paulo }
3203a8e07101SRui Paulo }
3204a8e07101SRui Paulo free(media_list);
3205a8e07101SRui Paulo if (!can_do) {
3206a8e07101SRui Paulo /*
3207a8e07101SRui Paulo * This adapter doesn't support monitor mode.
3208a8e07101SRui Paulo */
3209a8e07101SRui Paulo close(sock);
3210a8e07101SRui Paulo return (PCAP_ERROR_RFMON_NOTSUP);
3211a8e07101SRui Paulo }
3212a8e07101SRui Paulo
3213a8e07101SRui Paulo if (set) {
3214a8e07101SRui Paulo /*
3215a8e07101SRui Paulo * Don't just check whether we can enable monitor mode,
3216a8e07101SRui Paulo * do so, if it's not already enabled.
3217a8e07101SRui Paulo */
3218a8e07101SRui Paulo if ((req.ifm_current & IFM_IEEE80211_MONITOR) == 0) {
3219a8e07101SRui Paulo /*
3220a8e07101SRui Paulo * Monitor mode isn't currently on, so turn it on,
3221a8e07101SRui Paulo * and remember that we should turn it off when the
3222a8e07101SRui Paulo * pcap_t is closed.
3223a8e07101SRui Paulo */
3224a8e07101SRui Paulo
3225a8e07101SRui Paulo /*
3226a8e07101SRui Paulo * If we haven't already done so, arrange to have
3227a8e07101SRui Paulo * "pcap_close_all()" called when we exit.
3228a8e07101SRui Paulo */
3229*afdbf109SJoseph Mingrone if (!pcapint_do_addexit(p)) {
3230a8e07101SRui Paulo /*
3231a8e07101SRui Paulo * "atexit()" failed; don't put the interface
3232a8e07101SRui Paulo * in monitor mode, just give up.
3233a8e07101SRui Paulo */
3234a8e07101SRui Paulo close(sock);
3235a8e07101SRui Paulo return (PCAP_ERROR);
3236a8e07101SRui Paulo }
3237a8e07101SRui Paulo memset(&ifr, 0, sizeof(ifr));
3238*afdbf109SJoseph Mingrone (void)pcapint_strlcpy(ifr.ifr_name, p->opt.device,
3239a8e07101SRui Paulo sizeof(ifr.ifr_name));
3240a8e07101SRui Paulo ifr.ifr_media = req.ifm_current | IFM_IEEE80211_MONITOR;
3241a8e07101SRui Paulo if (ioctl(sock, SIOCSIFMEDIA, &ifr) == -1) {
3242*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf,
3243b00ab754SHans Petter Selasky PCAP_ERRBUF_SIZE, errno, "SIOCSIFMEDIA");
3244a8e07101SRui Paulo close(sock);
3245a8e07101SRui Paulo return (PCAP_ERROR);
3246a8e07101SRui Paulo }
3247a8e07101SRui Paulo
3248681ed54cSXin LI pb->must_do_on_close |= MUST_CLEAR_RFMON;
3249a8e07101SRui Paulo
3250a8e07101SRui Paulo /*
3251a8e07101SRui Paulo * Add this to the list of pcaps to close when we exit.
3252a8e07101SRui Paulo */
3253*afdbf109SJoseph Mingrone pcapint_add_to_pcaps_to_close(p);
3254a8e07101SRui Paulo }
3255a8e07101SRui Paulo }
3256a8e07101SRui Paulo return (0);
3257a8e07101SRui Paulo }
3258a8e07101SRui Paulo #endif /* HAVE_BSD_IEEE80211 */
3259a8e07101SRui Paulo
3260a8e07101SRui Paulo #if defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211))
3261a8e07101SRui Paulo /*
3262a8e07101SRui Paulo * Check whether we have any 802.11 link-layer types; return the best
3263a8e07101SRui Paulo * of the 802.11 link-layer types if we find one, and return -1
3264a8e07101SRui Paulo * otherwise.
3265a8e07101SRui Paulo *
3266a8e07101SRui Paulo * DLT_IEEE802_11_RADIO, with the radiotap header, is considered the
3267a8e07101SRui Paulo * best 802.11 link-layer type; any of the other 802.11-plus-radio
3268a8e07101SRui Paulo * headers are second-best; 802.11 with no radio information is
3269a8e07101SRui Paulo * the least good.
3270a8e07101SRui Paulo */
3271a8e07101SRui Paulo static int
3272a8e07101SRui Paulo find_802_11(struct bpf_dltlist *bdlp)
3273a8e07101SRui Paulo {
3274a8e07101SRui Paulo int new_dlt;
3275ada6f083SXin LI u_int i;
3276a8e07101SRui Paulo
3277a8e07101SRui Paulo /*
3278a8e07101SRui Paulo * Scan the list of DLT_ values, looking for 802.11 values,
3279a8e07101SRui Paulo * and, if we find any, choose the best of them.
3280a8e07101SRui Paulo */
3281a8e07101SRui Paulo new_dlt = -1;
3282a8e07101SRui Paulo for (i = 0; i < bdlp->bfl_len; i++) {
3283a8e07101SRui Paulo switch (bdlp->bfl_list[i]) {
3284a8e07101SRui Paulo
3285a8e07101SRui Paulo case DLT_IEEE802_11:
3286a8e07101SRui Paulo /*
3287a8e07101SRui Paulo * 802.11, but no radio.
3288a8e07101SRui Paulo *
3289a8e07101SRui Paulo * Offer this, and select it as the new mode
3290a8e07101SRui Paulo * unless we've already found an 802.11
3291a8e07101SRui Paulo * header with radio information.
3292a8e07101SRui Paulo */
3293a8e07101SRui Paulo if (new_dlt == -1)
3294a8e07101SRui Paulo new_dlt = bdlp->bfl_list[i];
3295a8e07101SRui Paulo break;
3296a8e07101SRui Paulo
329757e22627SCy Schubert #ifdef DLT_PRISM_HEADER
3298a8e07101SRui Paulo case DLT_PRISM_HEADER:
329957e22627SCy Schubert #endif
330057e22627SCy Schubert #ifdef DLT_AIRONET_HEADER
3301a8e07101SRui Paulo case DLT_AIRONET_HEADER:
330257e22627SCy Schubert #endif
3303a8e07101SRui Paulo case DLT_IEEE802_11_RADIO_AVS:
3304a8e07101SRui Paulo /*
3305a8e07101SRui Paulo * 802.11 with radio, but not radiotap.
3306a8e07101SRui Paulo *
3307a8e07101SRui Paulo * Offer this, and select it as the new mode
3308a8e07101SRui Paulo * unless we've already found the radiotap DLT_.
3309a8e07101SRui Paulo */
3310a8e07101SRui Paulo if (new_dlt != DLT_IEEE802_11_RADIO)
3311a8e07101SRui Paulo new_dlt = bdlp->bfl_list[i];
3312a8e07101SRui Paulo break;
3313a8e07101SRui Paulo
3314a8e07101SRui Paulo case DLT_IEEE802_11_RADIO:
3315a8e07101SRui Paulo /*
3316a8e07101SRui Paulo * 802.11 with radiotap.
3317a8e07101SRui Paulo *
3318a8e07101SRui Paulo * Offer this, and select it as the new mode.
3319a8e07101SRui Paulo */
3320a8e07101SRui Paulo new_dlt = bdlp->bfl_list[i];
3321a8e07101SRui Paulo break;
3322a8e07101SRui Paulo
3323a8e07101SRui Paulo default:
3324a8e07101SRui Paulo /*
3325a8e07101SRui Paulo * Not 802.11.
3326a8e07101SRui Paulo */
3327a8e07101SRui Paulo break;
3328a8e07101SRui Paulo }
3329a8e07101SRui Paulo }
3330a8e07101SRui Paulo
3331a8e07101SRui Paulo return (new_dlt);
3332a8e07101SRui Paulo }
3333a8e07101SRui Paulo #endif /* defined(BIOCGDLTLIST) && (defined(__APPLE__) || defined(HAVE_BSD_IEEE80211)) */
3334a8e07101SRui Paulo
3335a8e07101SRui Paulo #if defined(__APPLE__) && defined(BIOCGDLTLIST)
3336a8e07101SRui Paulo /*
333757e22627SCy Schubert * Remove non-802.11 header types from the list of DLT_ values, as we're in
333857e22627SCy Schubert * monitor mode, and those header types aren't supported in monitor mode.
3339a8e07101SRui Paulo */
3340a8e07101SRui Paulo static void
334157e22627SCy Schubert remove_non_802_11(pcap_t *p)
3342a8e07101SRui Paulo {
3343a8e07101SRui Paulo int i, j;
3344a8e07101SRui Paulo
3345a8e07101SRui Paulo /*
334657e22627SCy Schubert * Scan the list of DLT_ values and discard non-802.11 ones.
3347a8e07101SRui Paulo */
3348a8e07101SRui Paulo j = 0;
3349a8e07101SRui Paulo for (i = 0; i < p->dlt_count; i++) {
3350a8e07101SRui Paulo switch (p->dlt_list[i]) {
3351a8e07101SRui Paulo
3352a8e07101SRui Paulo case DLT_EN10MB:
335357e22627SCy Schubert case DLT_RAW:
3354a8e07101SRui Paulo /*
335557e22627SCy Schubert * Not 802.11. Don't offer this one.
3356a8e07101SRui Paulo */
3357a8e07101SRui Paulo continue;
3358a8e07101SRui Paulo
3359a8e07101SRui Paulo default:
3360a8e07101SRui Paulo /*
3361a8e07101SRui Paulo * Just copy this mode over.
3362a8e07101SRui Paulo */
3363a8e07101SRui Paulo break;
3364a8e07101SRui Paulo }
3365a8e07101SRui Paulo
3366a8e07101SRui Paulo /*
3367a8e07101SRui Paulo * Copy this DLT_ value to its new position.
3368a8e07101SRui Paulo */
3369a8e07101SRui Paulo p->dlt_list[j] = p->dlt_list[i];
3370a8e07101SRui Paulo j++;
3371a8e07101SRui Paulo }
3372a8e07101SRui Paulo
3373a8e07101SRui Paulo /*
3374a8e07101SRui Paulo * Set the DLT_ count to the number of entries we copied.
3375a8e07101SRui Paulo */
3376a8e07101SRui Paulo p->dlt_count = j;
3377a8e07101SRui Paulo }
3378a8e07101SRui Paulo
3379a8e07101SRui Paulo /*
3380a0ee43a1SRui Paulo * Remove 802.11 link-layer types from the list of DLT_ values, as
3381a0ee43a1SRui Paulo * we're not in monitor mode, and those DLT_ values will switch us
3382a0ee43a1SRui Paulo * to monitor mode.
3383a8e07101SRui Paulo */
3384a8e07101SRui Paulo static void
3385a8e07101SRui Paulo remove_802_11(pcap_t *p)
3386a8e07101SRui Paulo {
3387a8e07101SRui Paulo int i, j;
3388a8e07101SRui Paulo
3389a8e07101SRui Paulo /*
3390a8e07101SRui Paulo * Scan the list of DLT_ values and discard 802.11 values.
3391a8e07101SRui Paulo */
3392a8e07101SRui Paulo j = 0;
3393a8e07101SRui Paulo for (i = 0; i < p->dlt_count; i++) {
3394a8e07101SRui Paulo switch (p->dlt_list[i]) {
3395a8e07101SRui Paulo
3396a8e07101SRui Paulo case DLT_IEEE802_11:
339757e22627SCy Schubert #ifdef DLT_PRISM_HEADER
3398a8e07101SRui Paulo case DLT_PRISM_HEADER:
339957e22627SCy Schubert #endif
340057e22627SCy Schubert #ifdef DLT_AIRONET_HEADER
3401a8e07101SRui Paulo case DLT_AIRONET_HEADER:
340257e22627SCy Schubert #endif
3403a8e07101SRui Paulo case DLT_IEEE802_11_RADIO:
3404a8e07101SRui Paulo case DLT_IEEE802_11_RADIO_AVS:
340557e22627SCy Schubert #ifdef DLT_PPI
340657e22627SCy Schubert case DLT_PPI:
340757e22627SCy Schubert #endif
3408a8e07101SRui Paulo /*
3409a8e07101SRui Paulo * 802.11. Don't offer this one.
3410a8e07101SRui Paulo */
3411a8e07101SRui Paulo continue;
3412a8e07101SRui Paulo
3413a8e07101SRui Paulo default:
3414a8e07101SRui Paulo /*
3415a8e07101SRui Paulo * Just copy this mode over.
3416a8e07101SRui Paulo */
3417a8e07101SRui Paulo break;
3418a8e07101SRui Paulo }
3419a8e07101SRui Paulo
3420a8e07101SRui Paulo /*
3421a8e07101SRui Paulo * Copy this DLT_ value to its new position.
3422a8e07101SRui Paulo */
3423a8e07101SRui Paulo p->dlt_list[j] = p->dlt_list[i];
3424a8e07101SRui Paulo j++;
3425a8e07101SRui Paulo }
3426a8e07101SRui Paulo
3427a8e07101SRui Paulo /*
3428a8e07101SRui Paulo * Set the DLT_ count to the number of entries we copied.
3429a8e07101SRui Paulo */
3430a8e07101SRui Paulo p->dlt_count = j;
3431a8e07101SRui Paulo }
3432a8e07101SRui Paulo #endif /* defined(__APPLE__) && defined(BIOCGDLTLIST) */
3433a8e07101SRui Paulo
3434feb4ecdbSBruce M Simpson static int
3435feb4ecdbSBruce M Simpson pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
34368cf6c252SPaul Traina {
3437681ed54cSXin LI struct pcap_bpf *pb = p->priv;
3438681ed54cSXin LI
34398751327cSBill Fenner /*
3440feb4ecdbSBruce M Simpson * Free any user-mode filter we might happen to have installed.
3441feb4ecdbSBruce M Simpson */
3442feb4ecdbSBruce M Simpson pcap_freecode(&p->fcode);
3443feb4ecdbSBruce M Simpson
3444feb4ecdbSBruce M Simpson /*
3445feb4ecdbSBruce M Simpson * Try to install the kernel filter.
3446feb4ecdbSBruce M Simpson */
3447a8e07101SRui Paulo if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) == 0) {
3448a8e07101SRui Paulo /*
3449a8e07101SRui Paulo * It worked.
3450a8e07101SRui Paulo */
3451681ed54cSXin LI pb->filtering_in_kernel = 1; /* filtering in the kernel */
3452a8e07101SRui Paulo
3453a8e07101SRui Paulo /*
3454a8e07101SRui Paulo * Discard any previously-received packets, as they might
3455a8e07101SRui Paulo * have passed whatever filter was formerly in effect, but
3456a8e07101SRui Paulo * might not pass this filter (BIOCSETF discards packets
3457a8e07101SRui Paulo * buffered in the kernel, so you can lose packets in any
3458a8e07101SRui Paulo * case).
3459a8e07101SRui Paulo */
3460a8e07101SRui Paulo p->cc = 0;
3461a8e07101SRui Paulo return (0);
3462a8e07101SRui Paulo }
3463a8e07101SRui Paulo
3464a8e07101SRui Paulo /*
3465a8e07101SRui Paulo * We failed.
3466a8e07101SRui Paulo *
3467a8e07101SRui Paulo * If it failed with EINVAL, that's probably because the program
3468a8e07101SRui Paulo * is invalid or too big. Validate it ourselves; if we like it
3469a8e07101SRui Paulo * (we currently allow backward branches, to support protochain),
3470a8e07101SRui Paulo * run it in userland. (There's no notion of "too big" for
3471a8e07101SRui Paulo * userland.)
3472a8e07101SRui Paulo *
3473a8e07101SRui Paulo * Otherwise, just give up.
3474a8e07101SRui Paulo * XXX - if the copy of the program into the kernel failed,
3475a8e07101SRui Paulo * we will get EINVAL rather than, say, EFAULT on at least
3476a8e07101SRui Paulo * some kernels.
3477a8e07101SRui Paulo */
3478a8e07101SRui Paulo if (errno != EINVAL) {
3479*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
3480b00ab754SHans Petter Selasky errno, "BIOCSETF");
34818cf6c252SPaul Traina return (-1);
34828cf6c252SPaul Traina }
348304fb2745SSam Leffler
348404fb2745SSam Leffler /*
3485*afdbf109SJoseph Mingrone * pcapint_install_bpf_program() validates the program.
3486a8e07101SRui Paulo *
3487a8e07101SRui Paulo * XXX - what if we already have a filter in the kernel?
348804fb2745SSam Leffler */
3489*afdbf109SJoseph Mingrone if (pcapint_install_bpf_program(p, fp) < 0)
3490a8e07101SRui Paulo return (-1);
3491681ed54cSXin LI pb->filtering_in_kernel = 0; /* filtering in userland */
34928cf6c252SPaul Traina return (0);
34938cf6c252SPaul Traina }
349409f33d61SBill Fenner
3495ee2dd488SSam Leffler /*
3496ee2dd488SSam Leffler * Set direction flag: Which packets do we accept on a forwarding
3497ee2dd488SSam Leffler * single device? IN, OUT or both?
3498ee2dd488SSam Leffler */
349957e22627SCy Schubert #if defined(BIOCSDIRECTION)
3500ee2dd488SSam Leffler static int
35015d18909fSSam Leffler pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
3502ee2dd488SSam Leffler {
3503560a54e1SJung-uk Kim u_int direction;
35046f9cba8fSJoseph Mingrone const char *direction_name;
3505560a54e1SJung-uk Kim
35066f9cba8fSJoseph Mingrone /*
35076f9cba8fSJoseph Mingrone * FreeBSD and NetBSD.
35086f9cba8fSJoseph Mingrone */
35096f9cba8fSJoseph Mingrone switch (d) {
35106f9cba8fSJoseph Mingrone
35116f9cba8fSJoseph Mingrone case PCAP_D_IN:
35126f9cba8fSJoseph Mingrone /*
35136f9cba8fSJoseph Mingrone * Incoming, but not outgoing, so accept only
35146f9cba8fSJoseph Mingrone * incoming packets.
35156f9cba8fSJoseph Mingrone */
35166f9cba8fSJoseph Mingrone direction = BPF_D_IN;
35176f9cba8fSJoseph Mingrone direction_name = "\"incoming only\"";
35186f9cba8fSJoseph Mingrone break;
35196f9cba8fSJoseph Mingrone
35206f9cba8fSJoseph Mingrone case PCAP_D_OUT:
35216f9cba8fSJoseph Mingrone /*
35226f9cba8fSJoseph Mingrone * Outgoing, but not incoming, so accept only
35236f9cba8fSJoseph Mingrone * outgoing packets.
35246f9cba8fSJoseph Mingrone */
35256f9cba8fSJoseph Mingrone direction = BPF_D_OUT;
35266f9cba8fSJoseph Mingrone direction_name = "\"outgoing only\"";
35276f9cba8fSJoseph Mingrone break;
35286f9cba8fSJoseph Mingrone
35296f9cba8fSJoseph Mingrone default:
35306f9cba8fSJoseph Mingrone /*
35316f9cba8fSJoseph Mingrone * Incoming and outgoing, so accept both
35326f9cba8fSJoseph Mingrone * incoming and outgoing packets.
35336f9cba8fSJoseph Mingrone *
35346f9cba8fSJoseph Mingrone * It's guaranteed, at this point, that d is a valid
35356f9cba8fSJoseph Mingrone * direction value, so we know that this is PCAP_D_INOUT
35366f9cba8fSJoseph Mingrone * if it's not PCAP_D_IN or PCAP_D_OUT.
35376f9cba8fSJoseph Mingrone */
35386f9cba8fSJoseph Mingrone direction = BPF_D_INOUT;
35396f9cba8fSJoseph Mingrone direction_name = "\"incoming and outgoing\"";
35406f9cba8fSJoseph Mingrone break;
35416f9cba8fSJoseph Mingrone }
35426f9cba8fSJoseph Mingrone
3543560a54e1SJung-uk Kim if (ioctl(p->fd, BIOCSDIRECTION, &direction) == -1) {
3544*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
35456f9cba8fSJoseph Mingrone errno, "Cannot set direction to %s", direction_name);
35466f9cba8fSJoseph Mingrone return (-1);
35476f9cba8fSJoseph Mingrone }
35486f9cba8fSJoseph Mingrone return (0);
35496f9cba8fSJoseph Mingrone }
35506f9cba8fSJoseph Mingrone #elif defined(BIOCSDIRFILT)
35516f9cba8fSJoseph Mingrone static int
35526f9cba8fSJoseph Mingrone pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
35536f9cba8fSJoseph Mingrone {
35546f9cba8fSJoseph Mingrone u_int dirfilt;
35556f9cba8fSJoseph Mingrone const char *direction_name;
35566f9cba8fSJoseph Mingrone
35576f9cba8fSJoseph Mingrone /*
35586f9cba8fSJoseph Mingrone * OpenBSD; same functionality, different names, different
35596f9cba8fSJoseph Mingrone * semantics (the flags mean "*don't* capture packets in
35606f9cba8fSJoseph Mingrone * that direction", not "*capture only* packets in that
35616f9cba8fSJoseph Mingrone * direction").
35626f9cba8fSJoseph Mingrone */
35636f9cba8fSJoseph Mingrone switch (d) {
35646f9cba8fSJoseph Mingrone
35656f9cba8fSJoseph Mingrone case PCAP_D_IN:
35666f9cba8fSJoseph Mingrone /*
35676f9cba8fSJoseph Mingrone * Incoming, but not outgoing, so filter out
35686f9cba8fSJoseph Mingrone * outgoing packets.
35696f9cba8fSJoseph Mingrone */
35706f9cba8fSJoseph Mingrone dirfilt = BPF_DIRECTION_OUT;
35716f9cba8fSJoseph Mingrone direction_name = "\"incoming only\"";
35726f9cba8fSJoseph Mingrone break;
35736f9cba8fSJoseph Mingrone
35746f9cba8fSJoseph Mingrone case PCAP_D_OUT:
35756f9cba8fSJoseph Mingrone /*
35766f9cba8fSJoseph Mingrone * Outgoing, but not incoming, so filter out
35776f9cba8fSJoseph Mingrone * incoming packets.
35786f9cba8fSJoseph Mingrone */
35796f9cba8fSJoseph Mingrone dirfilt = BPF_DIRECTION_IN;
35806f9cba8fSJoseph Mingrone direction_name = "\"outgoing only\"";
35816f9cba8fSJoseph Mingrone break;
35826f9cba8fSJoseph Mingrone
35836f9cba8fSJoseph Mingrone default:
35846f9cba8fSJoseph Mingrone /*
35856f9cba8fSJoseph Mingrone * Incoming and outgoing, so don't filter out
35866f9cba8fSJoseph Mingrone * any packets based on direction.
35876f9cba8fSJoseph Mingrone *
35886f9cba8fSJoseph Mingrone * It's guaranteed, at this point, that d is a valid
35896f9cba8fSJoseph Mingrone * direction value, so we know that this is PCAP_D_INOUT
35906f9cba8fSJoseph Mingrone * if it's not PCAP_D_IN or PCAP_D_OUT.
35916f9cba8fSJoseph Mingrone */
35926f9cba8fSJoseph Mingrone dirfilt = 0;
35936f9cba8fSJoseph Mingrone direction_name = "\"incoming and outgoing\"";
35946f9cba8fSJoseph Mingrone break;
35956f9cba8fSJoseph Mingrone }
35966f9cba8fSJoseph Mingrone if (ioctl(p->fd, BIOCSDIRFILT, &dirfilt) == -1) {
3597*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
35986f9cba8fSJoseph Mingrone errno, "Cannot set direction to %s", direction_name);
3599560a54e1SJung-uk Kim return (-1);
3600560a54e1SJung-uk Kim }
3601560a54e1SJung-uk Kim return (0);
360257e22627SCy Schubert }
3603560a54e1SJung-uk Kim #elif defined(BIOCSSEESENT)
360457e22627SCy Schubert static int
360557e22627SCy Schubert pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
360657e22627SCy Schubert {
3607ee2dd488SSam Leffler u_int seesent;
36086f9cba8fSJoseph Mingrone const char *direction_name;
3609ee2dd488SSam Leffler
3610ee2dd488SSam Leffler /*
36116f9cba8fSJoseph Mingrone * OS with just BIOCSSEESENT.
3612ee2dd488SSam Leffler */
36136f9cba8fSJoseph Mingrone switch (d) {
36146f9cba8fSJoseph Mingrone
36156f9cba8fSJoseph Mingrone case PCAP_D_IN:
36166f9cba8fSJoseph Mingrone /*
36176f9cba8fSJoseph Mingrone * Incoming, but not outgoing, so we don't want to
36186f9cba8fSJoseph Mingrone * see transmitted packets.
36196f9cba8fSJoseph Mingrone */
36206f9cba8fSJoseph Mingrone seesent = 0;
36216f9cba8fSJoseph Mingrone direction_name = "\"incoming only\"";
36226f9cba8fSJoseph Mingrone break;
36236f9cba8fSJoseph Mingrone
36246f9cba8fSJoseph Mingrone case PCAP_D_OUT:
36256f9cba8fSJoseph Mingrone /*
36266f9cba8fSJoseph Mingrone * Outgoing, but not incoming; we can't specify that.
36276f9cba8fSJoseph Mingrone */
36286f9cba8fSJoseph Mingrone snprintf(p->errbuf, sizeof(p->errbuf),
36296f9cba8fSJoseph Mingrone "Setting direction to \"outgoing only\" is not supported on this device");
36306f9cba8fSJoseph Mingrone return (-1);
36316f9cba8fSJoseph Mingrone
36326f9cba8fSJoseph Mingrone default:
36336f9cba8fSJoseph Mingrone /*
36346f9cba8fSJoseph Mingrone * Incoming and outgoing, so we want to see transmitted
36356f9cba8fSJoseph Mingrone * packets.
36366f9cba8fSJoseph Mingrone *
36376f9cba8fSJoseph Mingrone * It's guaranteed, at this point, that d is a valid
36386f9cba8fSJoseph Mingrone * direction value, so we know that this is PCAP_D_INOUT
36396f9cba8fSJoseph Mingrone * if it's not PCAP_D_IN or PCAP_D_OUT.
36406f9cba8fSJoseph Mingrone */
36416f9cba8fSJoseph Mingrone seesent = 1;
36426f9cba8fSJoseph Mingrone direction_name = "\"incoming and outgoing\"";
36436f9cba8fSJoseph Mingrone break;
3644ee2dd488SSam Leffler }
3645560a54e1SJung-uk Kim
3646ee2dd488SSam Leffler if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
3647*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
36486f9cba8fSJoseph Mingrone errno, "Cannot set direction to %s", direction_name);
3649ee2dd488SSam Leffler return (-1);
3650ee2dd488SSam Leffler }
3651ee2dd488SSam Leffler return (0);
365257e22627SCy Schubert }
3653ee2dd488SSam Leffler #else
365457e22627SCy Schubert static int
365557e22627SCy Schubert pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d _U_)
365657e22627SCy Schubert {
36576f9cba8fSJoseph Mingrone (void) snprintf(p->errbuf, sizeof(p->errbuf),
36586f9cba8fSJoseph Mingrone "Setting direction is not supported on this device");
3659ee2dd488SSam Leffler return (-1);
3660ee2dd488SSam Leffler }
366157e22627SCy Schubert #endif
3662ee2dd488SSam Leffler
366357e22627SCy Schubert #ifdef BIOCSDLT
3664feb4ecdbSBruce M Simpson static int
3665feb4ecdbSBruce M Simpson pcap_set_datalink_bpf(pcap_t *p, int dlt)
366609f33d61SBill Fenner {
366709f33d61SBill Fenner if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
3668*afdbf109SJoseph Mingrone pcapint_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
3669b00ab754SHans Petter Selasky errno, "Cannot set DLT %d", dlt);
3670feb4ecdbSBruce M Simpson return (-1);
367109f33d61SBill Fenner }
3672feb4ecdbSBruce M Simpson return (0);
367309f33d61SBill Fenner }
367457e22627SCy Schubert #else
367557e22627SCy Schubert static int
367657e22627SCy Schubert pcap_set_datalink_bpf(pcap_t *p _U_, int dlt _U_)
367757e22627SCy Schubert {
367857e22627SCy Schubert return (0);
367957e22627SCy Schubert }
368057e22627SCy Schubert #endif
3681b00ab754SHans Petter Selasky
3682b00ab754SHans Petter Selasky /*
3683b00ab754SHans Petter Selasky * Platform-specific information.
3684b00ab754SHans Petter Selasky */
3685b00ab754SHans Petter Selasky const char *
3686b00ab754SHans Petter Selasky pcap_lib_version(void)
3687b00ab754SHans Petter Selasky {
3688b00ab754SHans Petter Selasky #ifdef HAVE_ZEROCOPY_BPF
3689b00ab754SHans Petter Selasky return (PCAP_VERSION_STRING " (with zerocopy support)");
3690b00ab754SHans Petter Selasky #else
3691b00ab754SHans Petter Selasky return (PCAP_VERSION_STRING);
3692b00ab754SHans Petter Selasky #endif
3693b00ab754SHans Petter Selasky }
3694