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