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