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