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