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