]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-bpf.c
2 * Copyright (c) 1993, 1994, 1995, 1996, 1998
3 * The Regents of the University of California. All rights reserved.
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
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.
22 static const char rcsid
[] =
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.56 2003-02-11 01:46:05 guy Exp $ (LBL)";
30 #include <sys/param.h> /* optionally get BSD define */
32 #include <sys/timeb.h>
33 #include <sys/socket.h>
35 #include <sys/ioctl.h>
42 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the
43 * native OS version, as we need "struct bpf_config" from it.
45 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
47 #include <sys/types.h>
50 * Prevent bpf.h from redefining the DLT_ values to their
51 * IFT_ values, as we're going to return the standard libpcap
52 * values, not IBM's non-standard IFT_ values.
58 #include <net/if_types.h> /* for IFT_ values */
59 #include <sys/sysconfig.h>
60 #include <sys/device.h>
65 #define domakedev makedev64
66 #define getmajor major64
67 #define bpf_hdr bpf_hdr32
69 #define domakedev makedev
70 #define getmajor major
71 #endif /* __64BIT__ */
73 #define BPF_NAME "bpf"
75 #define DRIVER_PATH "/usr/lib/drivers"
76 #define BPF_NODE "/dev/bpf"
77 static int bpfloadedflag
= 0;
78 static int odmlockid
= 0;
96 #ifdef HAVE_OS_PROTO_H
103 pcap_stats(pcap_t
*p
, struct pcap_stat
*ps
)
108 * "ps_recv" counts packets handed to the filter, not packets
109 * that passed the filter. This includes packets later dropped
110 * because we ran out of buffer space.
112 * "ps_drop" counts packets dropped inside the BPF device
113 * because we ran out of buffer space. It doesn't count
114 * packets dropped by the interface driver. It counts
115 * only packets that passed the filter.
117 * Both statistics include packets not yet read from the kernel
118 * by libpcap, and thus not yet seen by the application.
120 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
121 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
122 pcap_strerror(errno
));
126 ps
->ps_recv
= s
.bs_recv
;
127 ps
->ps_drop
= s
.bs_drop
;
132 pcap_read(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
136 register u_char
*bp
, *ep
;
141 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
143 /* Don't choke when we get ptraced */
152 * Sigh. More AIX wonderfulness.
154 * It appears, according to Don
155 * Ebright, that a read from a BPF
156 * device returns -1 with "errno"
157 * set to EFAULT as an indication
158 * that packets have been dropped
159 * since the last successful read.
161 * This means that we shouldn't treat
162 * EFAULT as a fatal error; as we
163 * don't have an API for returning
164 * a "some packets were dropped since
165 * the last packet you saw" indication,
166 * we just ignore EFAULT and keep reading.
173 #if defined(sun) && !defined(BSD)
175 * Due to a SunOS bug, after 2^31 bytes, the kernel
176 * file offset overflows and read fails with EINVAL.
177 * The lseek() to 0 will fix things.
180 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
182 (void)lseek(p
->fd
, 0L, SEEK_SET
);
188 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
189 pcap_strerror(errno
));
197 * Loop through each packet.
199 #define bhp ((struct bpf_hdr *)bp)
202 register int caplen
, hdrlen
;
203 caplen
= bhp
->bh_caplen
;
204 hdrlen
= bhp
->bh_hdrlen
;
206 * XXX A bpf_hdr matches a pcap_pkthdr.
210 * AIX's BPF returns seconds/nanoseconds time stamps, not
211 * seconds/microseconds time stamps.
213 * XXX - I'm guessing here that it's a "struct timestamp";
214 * if not, this code won't compile, but, if not, you
215 * want to send us a bug report and fall back on using
216 * DLPI. It's not as if BPF used to work right on
217 * AIX before this change; this change attempts to fix
218 * the fact that it didn't....
220 bhp
->bh_tstamp
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
222 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
223 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
224 if (++n
>= cnt
&& cnt
> 0) {
237 bpf_odminit(char *errbuf
)
239 if (odm_initialize() == -1) {
240 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
241 "bpf_load: odm_initialize failed: %s",
242 pcap_strerror(errno
));
246 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
247 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
248 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
249 pcap_strerror(errno
));
257 bpf_odmcleanup(char *errbuf
)
259 if (odm_unlock(odmlockid
) == -1) {
260 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
261 "bpf_load: odm_unlock failed: %s",
262 pcap_strerror(errno
));
272 bpf_load(char *errbuf
)
276 int numminors
, i
, rc
;
279 struct bpf_config cfg_bpf
;
280 struct cfg_load cfg_ld
;
281 struct cfg_kmod cfg_km
;
284 * This is very very close to what happens in the real implementation
285 * but I've fixed some (unlikely) bug situations.
290 if (bpf_odminit(errbuf
) != 0)
293 major
= genmajor(BPF_NAME
);
295 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
296 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
300 minors
= getminor(major
, &numminors
, BPF_NAME
);
302 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
304 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
305 "bpf_load: genminor failed: %s",
306 pcap_strerror(errno
));
312 if (bpf_odmcleanup(errbuf
))
315 rc
= stat(BPF_NODE
"0", &sbuf
);
316 if (rc
== -1 && errno
!= ENOENT
) {
317 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
318 "bpf_load: can't stat %s: %s",
319 BPF_NODE
"0", pcap_strerror(errno
));
323 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
324 for (i
= 0; i
< BPF_MINORS
; i
++) {
325 sprintf(buf
, "%s%d", BPF_NODE
, i
);
327 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
328 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
329 "bpf_load: can't mknod %s: %s",
330 buf
pcap_strerror(errno
));
336 /* Check if the driver is loaded */
337 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
339 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
340 if (sysconfig(SYS_QUERYLOAD
, (void *) &cfg_ld
, sizeof(cfg_ld
) == -1) ||
341 (cfg_ld
.kmid
== 0)) {
342 /* Driver isn't loaded, load it now */
343 if (sysconfig(SYS_SINGLELOAD
, (void *) &cfg_ld
, sizeof(cfg_ld
)) == -1) {
344 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
345 "bpf_load: could not load driver: %s",
351 /* Configure the driver */
352 cfg_km
.cmd
= CFG_INIT
;
353 cfg_km
.kmid
= cfg_ld
.kmid
;
354 cfg_km
.mdilen
= sizeof(cfg_bpf
);
355 cfg_km
.mdiptr
= (void *) &cfg_bpf
;
356 for (i
= 0; i
< BPF_MINORS
; i
++) {
357 cfg_bpf
.devno
= domakedev(major
, i
);
358 if (sysconfig(SYS_CFGKMOD
, (void *) &cfg_km
, sizeof(cfg_km
)) == -1) {
359 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
360 "bpf_load: could not configure driver: %s",
373 bpf_open(pcap_t
*p
, char *errbuf
)
377 char device
[sizeof "/dev/bpf0000000000"];
381 * Load the bpf driver, if it isn't already loaded,
382 * and create the BPF device entries, if they don't
385 if (bpf_load(errbuf
) == -1)
390 * Go through all the minors and find one that isn't in use.
393 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
394 fd
= open(device
, O_RDONLY
);
395 } while (fd
< 0 && errno
== EBUSY
);
398 * XXX better message for all minors used
401 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
402 device
, pcap_strerror(errno
));
408 * XXX - on AIX, IBM's tcpdump (and perhaps the incompatible-with-everybody-
409 * else's libpcap in AIX 5.1) appears to forcibly load the BPF driver
410 * if it's not already loaded, and to create the BPF devices if they
413 * It'd be nice if we could do the same, although the code to do so
414 * might be version-dependent, alas (the way to do it isn't necessarily
418 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
423 struct bpf_version bv
;
425 struct bpf_dltlist bdl
;
431 bzero(&bdl
, sizeof(bdl
));
434 p
= (pcap_t
*)malloc(sizeof(*p
));
436 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
437 pcap_strerror(errno
));
440 memset(p
, 0, sizeof(*p
));
441 fd
= bpf_open(p
, ebuf
);
446 p
->snapshot
= snaplen
;
448 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
449 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
450 pcap_strerror(errno
));
453 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
454 bv
.bv_minor
< BPF_MINOR_VERSION
) {
455 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
456 "kernel bpf filter out of date");
461 * Try finding a good size for the buffer; 32768 may be too
462 * big, so keep cutting it in half until we find a size
463 * that works, or run out of sizes to try.
465 * XXX - there should be a user-accessible hook to set the
466 * initial buffer size.
468 for (v
= 32768; v
!= 0; v
>>= 1) {
469 /* Ignore the return value - this is because the call fails
470 * on BPF systems that don't have kernel malloc. And if
471 * the call fails, it's no big deal, we just continue to
472 * use the standard buffer size.
474 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
476 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
477 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
478 break; /* that size worked; we're done */
480 if (errno
!= ENOBUFS
) {
481 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
482 device
, pcap_strerror(errno
));
488 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
489 "BIOCSBLEN: %s: No buffer size worked", device
);
493 /* Get the data link layer type. */
494 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
495 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
496 pcap_strerror(errno
));
501 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
520 * We don't know what to map this to yet.
522 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
527 #if _BSDI_VERSION - 0 >= 199510
528 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
543 case 12: /*DLT_C_HDLC*/
552 * We know the default link type -- now determine all the DLTs
553 * this interface supports. If this fails with EINVAL, it's
554 * not fatal; we just don't get to use the feature later.
556 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
) &bdl
) == 0) {
557 bdl
.bfl_list
= (u_int
*) malloc(sizeof(u_int
) * bdl
.bfl_len
);
558 if (bdl
.bfl_list
== NULL
) {
559 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
560 pcap_strerror(errno
));
564 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
) &bdl
) < 0) {
565 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
566 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
570 p
->dlt_count
= bdl
.bfl_len
;
571 p
->dlt_list
= bdl
.bfl_list
;
573 if (errno
!= EINVAL
) {
574 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
575 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
584 * XXX - is this seconds/nanoseconds in AIX?
585 * (Treating it as such doesn't fix the timeout
586 * problem described below.)
589 to
.tv_sec
= to_ms
/ 1000;
590 to
.tv_usec
= (to_ms
* 1000) % 1000000;
591 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
592 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
593 pcap_strerror(errno
));
601 * Darren Reed notes that
603 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
604 * timeout appears to be ignored and it waits until the buffer
605 * is filled before returning. The result of not having it
606 * set is almost worse than useless if your BPF filter
607 * is reducing things to only a few packets (i.e. one every
610 * so we turn BIOCIMMEDIATE mode on if this is AIX.
612 * We don't turn it on for other platforms, as that means we
613 * get woken up for every packet, which may not be what we want;
614 * in the Winter 1993 USENIX paper on BPF, they say:
616 * Since a process might want to look at every packet on a
617 * network and the time between packets can be only a few
618 * microseconds, it is not possible to do a read system call
619 * per packet and BPF must collect the data from several
620 * packets and return it as a unit when the monitoring
621 * application does a read.
623 * which I infer is the reason for the timeout - it means we
624 * wait that amount of time, in the hopes that more packets
625 * will arrive and we'll get them all with one read.
627 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
628 * BSDs) causes the timeout to be ignored.
630 * On the other hand, some platforms (e.g., Linux) don't support
631 * timeouts, they just hand stuff to you as soon as it arrives;
632 * if that doesn't cause a problem on those platforms, it may
633 * be OK to have BIOCIMMEDIATE mode on BSD as well.
635 * (Note, though, that applications may depend on the read
636 * completing, even if no packets have arrived, when the timeout
637 * expires, e.g. GUI applications that have to check for input
638 * while waiting for packets to arrive; a non-zero timeout
639 * prevents "select()" from working right on FreeBSD and
640 * possibly other BSDs, as the timer doesn't start until a
641 * "read()" is done, so the timer isn't in effect if the
642 * application is blocked on a "select()", and the "select()"
643 * doesn't get woken up for a BPF device until the buffer
647 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
648 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
649 pcap_strerror(errno
));
652 #endif /* BIOCIMMEDIATE */
656 /* set promiscuous mode, okay if it fails */
657 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
658 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
659 pcap_strerror(errno
));
663 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
664 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
665 pcap_strerror(errno
));
669 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
670 if (p
->buffer
== NULL
) {
671 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
672 pcap_strerror(errno
));
680 if (bdl
.bfl_list
!= NULL
)
688 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
694 pcap_setfilter(pcap_t
*p
, struct bpf_program
*fp
)
697 * It looks that BPF code generated by gen_protochain() is not
698 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
699 * Take a safer side for now.
702 if (install_bpf_program(p
, fp
) < 0)
704 } else if (p
->sf
.rfile
!= NULL
) {
705 if (install_bpf_program(p
, fp
) < 0)
707 } else if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
708 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
709 pcap_strerror(errno
));
716 pcap_set_datalink_platform(pcap_t
*p
, int dlt
)
719 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
720 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
721 "Cannot set DLT %d: %s", dlt
, strerror(errno
));