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