]>
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.66 2003-07-25 05:32:02 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;
98 #endif /* HAVE_DAG_API */
100 #ifdef HAVE_OS_PROTO_H
101 #include "os-proto.h"
104 #include "gencode.h" /* for "no_optimize" */
106 static int pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
);
107 static int pcap_set_datalink_bpf(pcap_t
*p
, int dlt
);
110 pcap_stats_bpf(pcap_t
*p
, struct pcap_stat
*ps
)
115 * "ps_recv" counts packets handed to the filter, not packets
116 * that passed the filter. This includes packets later dropped
117 * because we ran out of buffer space.
119 * "ps_drop" counts packets dropped inside the BPF device
120 * because we ran out of buffer space. It doesn't count
121 * packets dropped by the interface driver. It counts
122 * only packets that passed the filter.
124 * Both statistics include packets not yet read from the kernel
125 * by libpcap, and thus not yet seen by the application.
127 if (ioctl(p
->fd
, BIOCGSTATS
, (caddr_t
)&s
) < 0) {
128 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCGSTATS: %s",
129 pcap_strerror(errno
));
133 ps
->ps_recv
= s
.bs_recv
;
134 ps
->ps_drop
= s
.bs_drop
;
139 pcap_read_bpf(pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*user
)
143 register u_char
*bp
, *ep
;
148 cc
= read(p
->fd
, (char *)p
->buffer
, p
->bufsize
);
150 /* Don't choke when we get ptraced */
159 * Sigh. More AIX wonderfulness.
161 * For some unknown reason the uiomove()
162 * operation in the bpf kernel extension
163 * used to copy the buffer into user
164 * space sometimes returns EFAULT. I have
165 * no idea why this is the case given that
166 * a kernel debugger shows the user buffer
167 * is correct. This problem appears to
168 * be mostly mitigated by the memset of
169 * the buffer before it is first used.
170 * Very strange.... Shaun Clowes
172 * In any case this means that we shouldn't
173 * treat EFAULT as a fatal error; as we
174 * don't have an API for returning
175 * a "some packets were dropped since
176 * the last packet you saw" indication,
177 * we just ignore EFAULT and keep reading.
184 #if defined(sun) && !defined(BSD)
186 * Due to a SunOS bug, after 2^31 bytes, the kernel
187 * file offset overflows and read fails with EINVAL.
188 * The lseek() to 0 will fix things.
191 if (lseek(p
->fd
, 0L, SEEK_CUR
) +
193 (void)lseek(p
->fd
, 0L, SEEK_SET
);
199 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "read: %s",
200 pcap_strerror(errno
));
208 * Loop through each packet.
210 #define bhp ((struct bpf_hdr *)bp)
213 register int caplen
, hdrlen
;
214 caplen
= bhp
->bh_caplen
;
215 hdrlen
= bhp
->bh_hdrlen
;
217 * XXX A bpf_hdr matches a pcap_pkthdr.
221 * AIX's BPF returns seconds/nanoseconds time stamps, not
222 * seconds/microseconds time stamps.
224 * XXX - I'm guessing here that it's a "struct timestamp";
225 * if not, this code won't compile, but, if not, you
226 * want to send us a bug report and fall back on using
227 * DLPI. It's not as if BPF used to work right on
228 * AIX before this change; this change attempts to fix
229 * the fact that it didn't....
231 bhp
->bh_tstamp
.tv_usec
= bhp
->bh_tstamp
.tv_usec
/1000;
233 (*callback
)(user
, (struct pcap_pkthdr
*)bp
, bp
+ hdrlen
);
234 bp
+= BPF_WORDALIGN(caplen
+ hdrlen
);
235 if (++n
>= cnt
&& cnt
> 0) {
248 bpf_odminit(char *errbuf
)
252 if (odm_initialize() == -1) {
253 if (odm_err_msg(odmerrno
, &errstr
) == -1)
254 errstr
= "Unknown error";
255 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
256 "bpf_load: odm_initialize failed: %s",
261 if ((odmlockid
= odm_lock("/etc/objrepos/config_lock", ODM_WAIT
)) == -1) {
262 if (odm_err_msg(odmerrno
, &errstr
) == -1)
263 errstr
= "Unknown error";
264 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
265 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
274 bpf_odmcleanup(char *errbuf
)
278 if (odm_unlock(odmlockid
) == -1) {
279 if (odm_err_msg(odmerrno
, &errstr
) == -1)
280 errstr
= "Unknown error";
281 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
282 "bpf_load: odm_unlock failed: %s",
287 if (odm_terminate() == -1) {
288 if (odm_err_msg(odmerrno
, &errstr
) == -1)
289 errstr
= "Unknown error";
290 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
291 "bpf_load: odm_terminate failed: %s",
300 bpf_load(char *errbuf
)
304 int numminors
, i
, rc
;
307 struct bpf_config cfg_bpf
;
308 struct cfg_load cfg_ld
;
309 struct cfg_kmod cfg_km
;
312 * This is very very close to what happens in the real implementation
313 * but I've fixed some (unlikely) bug situations.
318 if (bpf_odminit(errbuf
) != 0)
321 major
= genmajor(BPF_NAME
);
323 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
324 "bpf_load: genmajor failed: %s", pcap_strerror(errno
));
328 minors
= getminor(major
, &numminors
, BPF_NAME
);
330 minors
= genminor("bpf", major
, 0, BPF_MINORS
, 1, 1);
332 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
333 "bpf_load: genminor failed: %s",
334 pcap_strerror(errno
));
339 if (bpf_odmcleanup(errbuf
))
342 rc
= stat(BPF_NODE
"0", &sbuf
);
343 if (rc
== -1 && errno
!= ENOENT
) {
344 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
345 "bpf_load: can't stat %s: %s",
346 BPF_NODE
"0", pcap_strerror(errno
));
350 if (rc
== -1 || getmajor(sbuf
.st_rdev
) != major
) {
351 for (i
= 0; i
< BPF_MINORS
; i
++) {
352 sprintf(buf
, "%s%d", BPF_NODE
, i
);
354 if (mknod(buf
, S_IRUSR
| S_IFCHR
, domakedev(major
, i
)) == -1) {
355 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
356 "bpf_load: can't mknod %s: %s",
357 buf
, pcap_strerror(errno
));
363 /* Check if the driver is loaded */
364 memset(&cfg_ld
, 0x0, sizeof(cfg_ld
));
366 sprintf(cfg_ld
.path
, "%s/%s", DRIVER_PATH
, BPF_NAME
);
367 if ((sysconfig(SYS_QUERYLOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) ||
368 (cfg_ld
.kmid
== 0)) {
369 /* Driver isn't loaded, load it now */
370 if (sysconfig(SYS_SINGLELOAD
, (void *)&cfg_ld
, sizeof(cfg_ld
)) == -1) {
371 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
372 "bpf_load: could not load driver: %s",
378 /* Configure the driver */
379 cfg_km
.cmd
= CFG_INIT
;
380 cfg_km
.kmid
= cfg_ld
.kmid
;
381 cfg_km
.mdilen
= sizeof(cfg_bpf
);
382 cfg_km
.mdiptr
= (void *)&cfg_bpf
;
383 for (i
= 0; i
< BPF_MINORS
; i
++) {
384 cfg_bpf
.devno
= domakedev(major
, i
);
385 if (sysconfig(SYS_CFGKMOD
, (void *)&cfg_km
, sizeof(cfg_km
)) == -1) {
386 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
387 "bpf_load: could not configure driver: %s",
400 bpf_open(pcap_t
*p
, char *errbuf
)
404 char device
[sizeof "/dev/bpf0000000000"];
408 * Load the bpf driver, if it isn't already loaded,
409 * and create the BPF device entries, if they don't
412 if (bpf_load(errbuf
) == -1)
417 * Go through all the minors and find one that isn't in use.
420 (void)snprintf(device
, sizeof(device
), "/dev/bpf%d", n
++);
421 fd
= open(device
, O_RDONLY
);
422 } while (fd
< 0 && errno
== EBUSY
);
425 * XXX better message for all minors used
428 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "(no devices found) %s: %s",
429 device
, pcap_strerror(errno
));
435 pcap_close_bpf(pcap_t
*p
)
437 if (p
->buffer
!= NULL
)
444 * XXX - on AIX, IBM's tcpdump (and perhaps the incompatible-with-everybody-
445 * else's libpcap in AIX 5.1) appears to forcibly load the BPF driver
446 * if it's not already loaded, and to create the BPF devices if they
449 * It'd be nice if we could do the same, although the code to do so
450 * might be version-dependent, alas (the way to do it isn't necessarily
454 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
459 struct bpf_version bv
;
461 struct bpf_dltlist bdl
;
467 if (strstr(device
, "dag")) {
468 return dag_open_live(device
, snaplen
, promisc
, to_ms
, ebuf
);
470 #endif /* HAVE_DAG_API */
473 bzero(&bdl
, sizeof(bdl
));
476 p
= (pcap_t
*)malloc(sizeof(*p
));
478 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
479 pcap_strerror(errno
));
482 memset(p
, 0, sizeof(*p
));
483 fd
= bpf_open(p
, ebuf
);
488 p
->snapshot
= snaplen
;
490 if (ioctl(fd
, BIOCVERSION
, (caddr_t
)&bv
) < 0) {
491 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCVERSION: %s",
492 pcap_strerror(errno
));
495 if (bv
.bv_major
!= BPF_MAJOR_VERSION
||
496 bv
.bv_minor
< BPF_MINOR_VERSION
) {
497 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
498 "kernel bpf filter out of date");
503 * Try finding a good size for the buffer; 32768 may be too
504 * big, so keep cutting it in half until we find a size
505 * that works, or run out of sizes to try. If the default
506 * is larger, don't make it smaller.
508 * XXX - there should be a user-accessible hook to set the
509 * initial buffer size.
511 if ((ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) || v
< 32768)
513 for ( ; v
!= 0; v
>>= 1) {
514 /* Ignore the return value - this is because the call fails
515 * on BPF systems that don't have kernel malloc. And if
516 * the call fails, it's no big deal, we just continue to
517 * use the standard buffer size.
519 (void) ioctl(fd
, BIOCSBLEN
, (caddr_t
)&v
);
521 (void)strncpy(ifr
.ifr_name
, device
, sizeof(ifr
.ifr_name
));
522 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) >= 0)
523 break; /* that size worked; we're done */
525 if (errno
!= ENOBUFS
) {
526 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSETIF: %s: %s",
527 device
, pcap_strerror(errno
));
533 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
534 "BIOCSBLEN: %s: No buffer size worked", device
);
538 /* Get the data link layer type. */
539 if (ioctl(fd
, BIOCGDLT
, (caddr_t
)&v
) < 0) {
540 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGDLT: %s",
541 pcap_strerror(errno
));
546 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
569 * We don't know what to map this to yet.
571 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "unknown interface type %u",
576 #if _BSDI_VERSION - 0 >= 199510
577 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
592 case 12: /*DLT_C_HDLC*/
601 * We know the default link type -- now determine all the DLTs
602 * this interface supports. If this fails with EINVAL, it's
603 * not fatal; we just don't get to use the feature later.
605 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)&bdl
) == 0) {
606 bdl
.bfl_list
= (u_int
*) malloc(sizeof(u_int
) * bdl
.bfl_len
);
607 if (bdl
.bfl_list
== NULL
) {
608 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
609 pcap_strerror(errno
));
613 if (ioctl(fd
, BIOCGDLTLIST
, (caddr_t
)&bdl
) < 0) {
614 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
615 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
619 p
->dlt_count
= bdl
.bfl_len
;
620 p
->dlt_list
= bdl
.bfl_list
;
622 if (errno
!= EINVAL
) {
623 (void)snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
624 "BIOCGDLTLIST: %s", pcap_strerror(errno
));
633 * XXX - is this seconds/nanoseconds in AIX?
634 * (Treating it as such doesn't fix the timeout
635 * problem described below.)
638 to
.tv_sec
= to_ms
/ 1000;
639 to
.tv_usec
= (to_ms
* 1000) % 1000000;
640 if (ioctl(p
->fd
, BIOCSRTIMEOUT
, (caddr_t
)&to
) < 0) {
641 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCSRTIMEOUT: %s",
642 pcap_strerror(errno
));
650 * Darren Reed notes that
652 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
653 * timeout appears to be ignored and it waits until the buffer
654 * is filled before returning. The result of not having it
655 * set is almost worse than useless if your BPF filter
656 * is reducing things to only a few packets (i.e. one every
659 * so we turn BIOCIMMEDIATE mode on if this is AIX.
661 * We don't turn it on for other platforms, as that means we
662 * get woken up for every packet, which may not be what we want;
663 * in the Winter 1993 USENIX paper on BPF, they say:
665 * Since a process might want to look at every packet on a
666 * network and the time between packets can be only a few
667 * microseconds, it is not possible to do a read system call
668 * per packet and BPF must collect the data from several
669 * packets and return it as a unit when the monitoring
670 * application does a read.
672 * which I infer is the reason for the timeout - it means we
673 * wait that amount of time, in the hopes that more packets
674 * will arrive and we'll get them all with one read.
676 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
677 * BSDs) causes the timeout to be ignored.
679 * On the other hand, some platforms (e.g., Linux) don't support
680 * timeouts, they just hand stuff to you as soon as it arrives;
681 * if that doesn't cause a problem on those platforms, it may
682 * be OK to have BIOCIMMEDIATE mode on BSD as well.
684 * (Note, though, that applications may depend on the read
685 * completing, even if no packets have arrived, when the timeout
686 * expires, e.g. GUI applications that have to check for input
687 * while waiting for packets to arrive; a non-zero timeout
688 * prevents "select()" from working right on FreeBSD and
689 * possibly other BSDs, as the timer doesn't start until a
690 * "read()" is done, so the timer isn't in effect if the
691 * application is blocked on a "select()", and the "select()"
692 * doesn't get woken up for a BPF device until the buffer
696 if (ioctl(p
->fd
, BIOCIMMEDIATE
, &v
) < 0) {
697 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCIMMEDIATE: %s",
698 pcap_strerror(errno
));
701 #endif /* BIOCIMMEDIATE */
705 /* set promiscuous mode, okay if it fails */
706 if (ioctl(p
->fd
, BIOCPROMISC
, NULL
) < 0) {
707 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCPROMISC: %s",
708 pcap_strerror(errno
));
712 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&v
) < 0) {
713 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "BIOCGBLEN: %s",
714 pcap_strerror(errno
));
718 p
->buffer
= (u_char
*)malloc(p
->bufsize
);
719 if (p
->buffer
== NULL
) {
720 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "malloc: %s",
721 pcap_strerror(errno
));
725 /* For some strange reason this seems to prevent the EFAULT
726 * problems we have experienced from AIX BPF. */
727 memset(p
->buffer
, 0x0, p
->bufsize
);
730 p
->read_op
= pcap_read_bpf
;
731 p
->setfilter_op
= pcap_setfilter_bpf
;
732 p
->set_datalink_op
= pcap_set_datalink_bpf
;
733 p
->stats_op
= pcap_stats_bpf
;
734 p
->close_op
= pcap_close_bpf
;
740 if (bdl
.bfl_list
!= NULL
)
748 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
751 if (dag_platform_finddevs(alldevsp
, errbuf
) < 0)
753 #endif /* HAVE_DAG_API */
759 pcap_setfilter_bpf(pcap_t
*p
, struct bpf_program
*fp
)
762 * It looks that BPF code generated by gen_protochain() is not
763 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
764 * Take a safer side for now.
767 if (install_bpf_program(p
, fp
) < 0)
769 } else if (ioctl(p
->fd
, BIOCSETF
, (caddr_t
)fp
) < 0) {
770 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "BIOCSETF: %s",
771 pcap_strerror(errno
));
778 pcap_set_datalink_bpf(pcap_t
*p
, int dlt
)
781 if (ioctl(p
->fd
, BIOCSDLT
, &dlt
) == -1) {
782 (void) snprintf(p
->errbuf
, sizeof(p
->errbuf
),
783 "Cannot set DLT %d: %s", dlt
, strerror(errno
));