Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / libpcap / pcap-bpf.c
blobe50010e4cf960e14ec2925062abea6fbe32653b9
1 /*
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
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.
21 #ifndef lint
22 static const char rcsid[] _U_ =
23 "@(#) $Header: /pub/NetBSD/misc/repositories/cvsroot/src/dist/libpcap/pcap-bpf.c,v 1.2 2006/02/27 15:51:38 drochner Exp $ (LBL)";
24 #endif
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include <sys/param.h> /* optionally get BSD define */
31 #include <sys/time.h>
32 #include <sys/timeb.h>
33 #include <sys/socket.h>
34 #include <sys/file.h>
35 #include <sys/ioctl.h>
36 #include <sys/utsname.h>
37 #ifdef __NetBSD__
38 #include <paths.h>
39 #endif
41 #include <net/if.h>
43 #ifdef _AIX
46 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the
47 * native OS version, as we need "struct bpf_config" from it.
49 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
51 #include <sys/types.h>
54 * Prevent bpf.h from redefining the DLT_ values to their
55 * IFT_ values, as we're going to return the standard libpcap
56 * values, not IBM's non-standard IFT_ values.
58 #undef _AIX
59 #include <net/bpf.h>
60 #define _AIX
62 #include <net/if_types.h> /* for IFT_ values */
63 #include <sys/sysconfig.h>
64 #include <sys/device.h>
65 #include <sys/cfgodm.h>
66 #include <cf.h>
68 #ifdef __64BIT__
69 #define domakedev makedev64
70 #define getmajor major64
71 #define bpf_hdr bpf_hdr32
72 #else /* __64BIT__ */
73 #define domakedev makedev
74 #define getmajor major
75 #endif /* __64BIT__ */
77 #define BPF_NAME "bpf"
78 #define BPF_MINORS 4
79 #define DRIVER_PATH "/usr/lib/drivers"
80 #define BPF_NODE "/dev/bpf"
81 static int bpfloadedflag = 0;
82 static int odmlockid = 0;
84 #else /* _AIX */
86 #include <net/bpf.h>
88 #endif /* _AIX */
90 #include <ctype.h>
91 #include <errno.h>
92 #include <netdb.h>
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <string.h>
96 #include <unistd.h>
98 #include "pcap-int.h"
100 #ifdef HAVE_DAG_API
101 #include "pcap-dag.h"
102 #endif /* HAVE_DAG_API */
104 #ifdef HAVE_OS_PROTO_H
105 #include "os-proto.h"
106 #endif
108 #include "gencode.h" /* for "no_optimize" */
110 static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
111 static int pcap_setdirection_bpf(pcap_t *, pcap_direction_t);
112 static int pcap_set_datalink_bpf(pcap_t *p, int dlt);
114 static int
115 pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
117 struct bpf_stat s;
120 * "ps_recv" counts packets handed to the filter, not packets
121 * that passed the filter. This includes packets later dropped
122 * because we ran out of buffer space.
124 * "ps_drop" counts packets dropped inside the BPF device
125 * because we ran out of buffer space. It doesn't count
126 * packets dropped by the interface driver. It counts
127 * only packets that passed the filter.
129 * Both statistics include packets not yet read from the kernel
130 * by libpcap, and thus not yet seen by the application.
132 if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
133 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s",
134 pcap_strerror(errno));
135 return (-1);
138 ps->ps_recv = s.bs_recv;
139 ps->ps_drop = s.bs_drop;
140 return (0);
143 static int
144 pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
146 int cc;
147 int n = 0;
148 register u_char *bp, *ep;
149 u_char *datap;
150 struct bpf_insn *fcode;
151 #ifdef PCAP_FDDIPAD
152 register int pad;
153 #endif
155 fcode = p->md.use_bpf ? NULL : p->fcode.bf_insns;
156 again:
158 * Has "pcap_breakloop()" been called?
160 if (p->break_loop) {
162 * Yes - clear the flag that indicates that it
163 * has, and return -2 to indicate that we were
164 * told to break out of the loop.
166 p->break_loop = 0;
167 return (-2);
169 cc = p->cc;
170 if (p->cc == 0) {
171 cc = read(p->fd, (char *)p->buffer, p->bufsize);
172 if (cc < 0) {
173 /* Don't choke when we get ptraced */
174 switch (errno) {
176 case EINTR:
177 goto again;
179 #ifdef _AIX
180 case EFAULT:
182 * Sigh. More AIX wonderfulness.
184 * For some unknown reason the uiomove()
185 * operation in the bpf kernel extension
186 * used to copy the buffer into user
187 * space sometimes returns EFAULT. I have
188 * no idea why this is the case given that
189 * a kernel debugger shows the user buffer
190 * is correct. This problem appears to
191 * be mostly mitigated by the memset of
192 * the buffer before it is first used.
193 * Very strange.... Shaun Clowes
195 * In any case this means that we shouldn't
196 * treat EFAULT as a fatal error; as we
197 * don't have an API for returning
198 * a "some packets were dropped since
199 * the last packet you saw" indication,
200 * we just ignore EFAULT and keep reading.
202 goto again;
203 #endif
205 case EWOULDBLOCK:
206 return (0);
207 #if defined(sun) && !defined(BSD)
209 * Due to a SunOS bug, after 2^31 bytes, the kernel
210 * file offset overflows and read fails with EINVAL.
211 * The lseek() to 0 will fix things.
213 case EINVAL:
214 if (lseek(p->fd, 0L, SEEK_CUR) +
215 p->bufsize < 0) {
216 (void)lseek(p->fd, 0L, SEEK_SET);
217 goto again;
219 /* fall through */
220 #endif
222 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s",
223 pcap_strerror(errno));
224 return (-1);
226 bp = p->buffer;
227 } else
228 bp = p->bp;
231 * Loop through each packet.
233 #define bhp ((struct bpf_hdr *)bp)
234 ep = bp + cc;
235 #ifdef PCAP_FDDIPAD
236 pad = p->fddipad;
237 #endif
238 while (bp < ep) {
239 register int caplen, hdrlen;
242 * Has "pcap_breakloop()" been called?
243 * If so, return immediately - if we haven't read any
244 * packets, clear the flag and return -2 to indicate
245 * that we were told to break out of the loop, otherwise
246 * leave the flag set, so that the *next* call will break
247 * out of the loop without having read any packets, and
248 * return the number of packets we've processed so far.
250 if (p->break_loop) {
251 if (n == 0) {
252 p->break_loop = 0;
253 return (-2);
254 } else {
255 p->bp = bp;
256 p->cc = ep - bp;
257 return (n);
261 caplen = bhp->bh_caplen;
262 hdrlen = bhp->bh_hdrlen;
263 datap = bp + hdrlen;
265 * Short-circuit evaluation: if using BPF filter
266 * in kernel, no need to do it now.
268 #ifdef PCAP_FDDIPAD
269 * Note: the filter code was generated assuming
270 * that p->fddipad was the amount of padding
271 * before the header, as that's what's required
272 * in the kernel, so we run the filter before
273 * skipping that padding.
274 #endif
276 if (fcode == NULL ||
277 bpf_filter(fcode, datap, bhp->bh_datalen, caplen)) {
278 struct pcap_pkthdr pkthdr;
280 pkthdr.ts.tv_sec = bhp->bh_tstamp.tv_sec;
281 #ifdef _AIX
283 * AIX's BPF returns seconds/nanoseconds time
284 * stamps, not seconds/microseconds time stamps.
286 pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec/1000;
287 #else
288 pkthdr.ts.tv_usec = bhp->bh_tstamp.tv_usec;
289 #endif
290 #ifdef PCAP_FDDIPAD
291 if (caplen > pad)
292 pkthdr.caplen = caplen - pad;
293 else
294 pkthdr.caplen = 0;
295 if (bhp->bh_datalen > pad)
296 pkthdr.len = bhp->bh_datalen - pad;
297 else
298 pkthdr.len = 0;
299 datap += pad;
300 #else
301 pkthdr.caplen = caplen;
302 pkthdr.len = bhp->bh_datalen;
303 #endif
304 (*callback)(user, &pkthdr, datap);
305 bp += BPF_WORDALIGN(caplen + hdrlen);
306 if (++n >= cnt && cnt > 0) {
307 p->bp = bp;
308 p->cc = ep - bp;
309 return (n);
311 } else {
313 * Skip this packet.
315 bp += BPF_WORDALIGN(caplen + hdrlen);
318 #undef bhp
319 p->cc = 0;
320 return (n);
323 static int
324 pcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
326 int ret;
328 ret = write(p->fd, buf, size);
329 #ifdef __APPLE__
330 if (ret == -1 && errno == EAFNOSUPPORT) {
332 * In Mac OS X, there's a bug wherein setting the
333 * BIOCSHDRCMPLT flag causes writes to fail; see,
334 * for example:
336 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
338 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
339 * assume it's due to that bug, and turn off that flag
340 * and try again. If we succeed, it either means that
341 * somebody applied the fix from that URL, or other patches
342 * for that bug from
344 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
346 * and are running a Darwin kernel with those fixes, or
347 * that Apple fixed the problem in some OS X release.
349 u_int spoof_eth_src = 0;
351 if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
352 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
353 "send: can't turn off BIOCSHDRCMPLT: %s",
354 pcap_strerror(errno));
355 return (-1);
359 * Now try the write again.
361 ret = write(p->fd, buf, size);
363 #endif /* __APPLE__ */
364 if (ret == -1) {
365 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
366 pcap_strerror(errno));
367 return (-1);
369 return (ret);
372 #ifdef _AIX
373 static int
374 bpf_odminit(char *errbuf)
376 char *errstr;
378 if (odm_initialize() == -1) {
379 if (odm_err_msg(odmerrno, &errstr) == -1)
380 errstr = "Unknown error";
381 snprintf(errbuf, PCAP_ERRBUF_SIZE,
382 "bpf_load: odm_initialize failed: %s",
383 errstr);
384 return (-1);
387 if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
388 if (odm_err_msg(odmerrno, &errstr) == -1)
389 errstr = "Unknown error";
390 snprintf(errbuf, PCAP_ERRBUF_SIZE,
391 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
392 errstr);
393 return (-1);
396 return (0);
399 static int
400 bpf_odmcleanup(char *errbuf)
402 char *errstr;
404 if (odm_unlock(odmlockid) == -1) {
405 if (odm_err_msg(odmerrno, &errstr) == -1)
406 errstr = "Unknown error";
407 snprintf(errbuf, PCAP_ERRBUF_SIZE,
408 "bpf_load: odm_unlock failed: %s",
409 errstr);
410 return (-1);
413 if (odm_terminate() == -1) {
414 if (odm_err_msg(odmerrno, &errstr) == -1)
415 errstr = "Unknown error";
416 snprintf(errbuf, PCAP_ERRBUF_SIZE,
417 "bpf_load: odm_terminate failed: %s",
418 errstr);
419 return (-1);
422 return (0);
425 static int
426 bpf_load(char *errbuf)
428 long major;
429 int *minors;
430 int numminors, i, rc;
431 char buf[1024];
432 struct stat sbuf;
433 struct bpf_config cfg_bpf;
434 struct cfg_load cfg_ld;
435 struct cfg_kmod cfg_km;
438 * This is very very close to what happens in the real implementation
439 * but I've fixed some (unlikely) bug situations.
441 if (bpfloadedflag)
442 return (0);
444 if (bpf_odminit(errbuf) != 0)
445 return (-1);
447 major = genmajor(BPF_NAME);
448 if (major == -1) {
449 snprintf(errbuf, PCAP_ERRBUF_SIZE,
450 "bpf_load: genmajor failed: %s", pcap_strerror(errno));
451 return (-1);
454 minors = getminor(major, &numminors, BPF_NAME);
455 if (!minors) {
456 minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
457 if (!minors) {
458 snprintf(errbuf, PCAP_ERRBUF_SIZE,
459 "bpf_load: genminor failed: %s",
460 pcap_strerror(errno));
461 return (-1);
465 if (bpf_odmcleanup(errbuf))
466 return (-1);
468 rc = stat(BPF_NODE "0", &sbuf);
469 if (rc == -1 && errno != ENOENT) {
470 snprintf(errbuf, PCAP_ERRBUF_SIZE,
471 "bpf_load: can't stat %s: %s",
472 BPF_NODE "0", pcap_strerror(errno));
473 return (-1);
476 if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
477 for (i = 0; i < BPF_MINORS; i++) {
478 sprintf(buf, "%s%d", BPF_NODE, i);
479 unlink(buf);
480 if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
481 snprintf(errbuf, PCAP_ERRBUF_SIZE,
482 "bpf_load: can't mknod %s: %s",
483 buf, pcap_strerror(errno));
484 return (-1);
489 /* Check if the driver is loaded */
490 memset(&cfg_ld, 0x0, sizeof(cfg_ld));
491 cfg_ld.path = buf;
492 sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME);
493 if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
494 (cfg_ld.kmid == 0)) {
495 /* Driver isn't loaded, load it now */
496 if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
497 snprintf(errbuf, PCAP_ERRBUF_SIZE,
498 "bpf_load: could not load driver: %s",
499 strerror(errno));
500 return (-1);
504 /* Configure the driver */
505 cfg_km.cmd = CFG_INIT;
506 cfg_km.kmid = cfg_ld.kmid;
507 cfg_km.mdilen = sizeof(cfg_bpf);
508 cfg_km.mdiptr = (void *)&cfg_bpf;
509 for (i = 0; i < BPF_MINORS; i++) {
510 cfg_bpf.devno = domakedev(major, i);
511 if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
512 snprintf(errbuf, PCAP_ERRBUF_SIZE,
513 "bpf_load: could not configure driver: %s",
514 strerror(errno));
515 return (-1);
519 bpfloadedflag = 1;
521 return (0);
523 #endif
525 static inline int
526 bpf_open(pcap_t *p, char *errbuf)
528 int fd;
529 #ifndef _PATH_BPF
530 int n = 0;
531 char device[sizeof "/dev/bpf0000000000"];
532 #else
533 const char *device = _PATH_BPF;
534 #endif
536 #ifdef _AIX
538 * Load the bpf driver, if it isn't already loaded,
539 * and create the BPF device entries, if they don't
540 * already exist.
542 if (bpf_load(errbuf) == -1)
543 return (-1);
544 #endif
546 #ifndef _PATH_BPF
548 * Go through all the minors and find one that isn't in use.
550 do {
551 (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
553 * Initially try a read/write open (to allow the inject
554 * method to work). If that fails due to permission
555 * issues, fall back to read-only. This allows a
556 * non-root user to be granted specific access to pcap
557 * capabilities via file permissions.
559 * XXX - we should have an API that has a flag that
560 * controls whether to open read-only or read-write,
561 * so that denial of permission to send (or inability
562 * to send, if sending packets isn't supported on
563 * the device in question) can be indicated at open
564 * time.
566 fd = open(device, O_RDWR);
567 if (fd == -1 && errno == EACCES)
568 fd = open(device, O_RDONLY);
569 } while (fd < 0 && errno == EBUSY);
572 * XXX better message for all minors used
574 if (fd < 0)
575 snprintf(errbuf, PCAP_ERRBUF_SIZE, "(no devices found) %s: %s",
576 device, pcap_strerror(errno));
577 #else
578 if ((fd = open(device, O_RDWR)) == -1 &&
579 (errno != EACCES || (fd = open(device, O_RDONLY)) == -1))
580 snprintf(errbuf, PCAP_ERRBUF_SIZE,
581 "(cannot open device) %s: %s", device, pcap_strerror(errno));
582 #endif
584 return (fd);
588 * We include the OS's <net/bpf.h>, not our "pcap-bpf.h", so we probably
589 * don't get DLT_DOCSIS defined.
591 #ifndef DLT_DOCSIS
592 #define DLT_DOCSIS 143
593 #endif
595 pcap_t *
596 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
597 char *ebuf)
599 int fd;
600 struct ifreq ifr;
601 struct bpf_version bv;
602 #ifdef BIOCGDLTLIST
603 struct bpf_dltlist bdl;
604 #endif
605 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
606 u_int spoof_eth_src = 1;
607 #endif
608 u_int v;
609 pcap_t *p;
610 struct bpf_insn total_insn;
611 struct bpf_program total_prog;
612 struct utsname osinfo;
614 #ifdef HAVE_DAG_API
615 if (strstr(device, "dag")) {
616 return dag_open_live(device, snaplen, promisc, to_ms, ebuf);
618 #endif /* HAVE_DAG_API */
620 #ifdef BIOCGDLTLIST
621 memset(&bdl, 0, sizeof(bdl));
622 #endif
624 p = (pcap_t *)malloc(sizeof(*p));
625 if (p == NULL) {
626 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
627 pcap_strerror(errno));
628 return (NULL);
630 memset(p, 0, sizeof(*p));
631 fd = bpf_open(p, ebuf);
632 if (fd < 0)
633 goto bad;
635 p->fd = fd;
636 p->snapshot = snaplen;
638 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
639 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s",
640 pcap_strerror(errno));
641 goto bad;
643 if (bv.bv_major != BPF_MAJOR_VERSION ||
644 bv.bv_minor < BPF_MINOR_VERSION) {
645 snprintf(ebuf, PCAP_ERRBUF_SIZE,
646 "kernel bpf filter out of date");
647 goto bad;
651 * Try finding a good size for the buffer; 32768 may be too
652 * big, so keep cutting it in half until we find a size
653 * that works, or run out of sizes to try. If the default
654 * is larger, don't make it smaller.
656 * XXX - there should be a user-accessible hook to set the
657 * initial buffer size.
659 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || v < 32768)
660 v = 32768;
661 for ( ; v != 0; v >>= 1) {
662 /* Ignore the return value - this is because the call fails
663 * on BPF systems that don't have kernel malloc. And if
664 * the call fails, it's no big deal, we just continue to
665 * use the standard buffer size.
667 (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
669 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
670 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
671 break; /* that size worked; we're done */
673 if (errno != ENOBUFS) {
674 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s",
675 device, pcap_strerror(errno));
676 goto bad;
680 if (v == 0) {
681 snprintf(ebuf, PCAP_ERRBUF_SIZE,
682 "BIOCSBLEN: %s: No buffer size worked", device);
683 goto bad;
686 /* Get the data link layer type. */
687 if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
688 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s",
689 pcap_strerror(errno));
690 goto bad;
692 #ifdef _AIX
694 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
696 switch (v) {
698 case IFT_ETHER:
699 case IFT_ISO88023:
700 v = DLT_EN10MB;
701 break;
703 case IFT_FDDI:
704 v = DLT_FDDI;
705 break;
707 case IFT_ISO88025:
708 v = DLT_IEEE802;
709 break;
711 case IFT_LOOP:
712 v = DLT_NULL;
713 break;
715 default:
717 * We don't know what to map this to yet.
719 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
721 goto bad;
723 #endif
724 #if _BSDI_VERSION - 0 >= 199510
725 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
726 switch (v) {
728 case DLT_SLIP:
729 v = DLT_SLIP_BSDOS;
730 break;
732 case DLT_PPP:
733 v = DLT_PPP_BSDOS;
734 break;
736 case 11: /*DLT_FR*/
737 v = DLT_FRELAY;
738 break;
740 case 12: /*DLT_C_HDLC*/
741 v = DLT_CHDLC;
742 break;
744 #endif
745 #ifdef PCAP_FDDIPAD
746 if (v == DLT_FDDI)
747 p->fddipad = PCAP_FDDIPAD;
748 else
749 p->fddipad = 0;
750 #endif
751 p->linktype = v;
753 #ifdef BIOCGDLTLIST
755 * We know the default link type -- now determine all the DLTs
756 * this interface supports. If this fails with EINVAL, it's
757 * not fatal; we just don't get to use the feature later.
759 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)&bdl) == 0) {
760 u_int i;
761 int is_ethernet;
763 bdl.bfl_list = (u_int *) malloc(sizeof(u_int) * bdl.bfl_len + 1);
764 if (bdl.bfl_list == NULL) {
765 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
766 pcap_strerror(errno));
767 goto bad;
770 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)&bdl) < 0) {
771 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
772 "BIOCGDLTLIST: %s", pcap_strerror(errno));
773 free(bdl.bfl_list);
774 goto bad;
778 * OK, for real Ethernet devices, add DLT_DOCSIS to the
779 * list, so that an application can let you choose it,
780 * in case you're capturing DOCSIS traffic that a Cisco
781 * Cable Modem Termination System is putting out onto
782 * an Ethernet (it doesn't put an Ethernet header onto
783 * the wire, it puts raw DOCSIS frames out on the wire
784 * inside the low-level Ethernet framing).
786 * A "real Ethernet device" is defined here as a device
787 * that has a link-layer type of DLT_EN10MB and that has
788 * no alternate link-layer types; that's done to exclude
789 * 802.11 interfaces (which might or might not be the
790 * right thing to do, but I suspect it is - Ethernet <->
791 * 802.11 bridges would probably badly mishandle frames
792 * that don't have Ethernet headers).
794 if (p->linktype == DLT_EN10MB) {
795 is_ethernet = 1;
796 for (i = 0; i < bdl.bfl_len; i++) {
797 if (bdl.bfl_list[i] != DLT_EN10MB) {
798 is_ethernet = 0;
799 break;
802 if (is_ethernet) {
804 * We reserved one more slot at the end of
805 * the list.
807 bdl.bfl_list[bdl.bfl_len] = DLT_DOCSIS;
808 bdl.bfl_len++;
811 p->dlt_count = bdl.bfl_len;
812 p->dlt_list = bdl.bfl_list;
813 } else {
814 if (errno != EINVAL) {
815 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
816 "BIOCGDLTLIST: %s", pcap_strerror(errno));
817 goto bad;
820 #endif
823 * If this is an Ethernet device, and we don't have a DLT_ list,
824 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
825 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
826 * do, but there's not much we can do about that without finding
827 * some other way of determining whether it's an Ethernet or 802.11
828 * device.)
830 if (p->linktype == DLT_EN10MB && p->dlt_count == 0) {
831 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
833 * If that fails, just leave the list empty.
835 if (p->dlt_list != NULL) {
836 p->dlt_list[0] = DLT_EN10MB;
837 p->dlt_list[1] = DLT_DOCSIS;
838 p->dlt_count = 2;
842 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
844 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
845 * the link-layer source address isn't forcibly overwritten.
846 * (Should we ignore errors? Should we do this only if
847 * we're open for writing?)
849 * XXX - I seem to remember some packet-sending bug in some
850 * BSDs - check CVS log for "bpf.c"?
852 if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
853 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
854 "BIOCSHDRCMPLT: %s", pcap_strerror(errno));
855 goto bad;
857 #endif
858 /* set timeout */
859 if (to_ms != 0) {
861 * XXX - is this seconds/nanoseconds in AIX?
862 * (Treating it as such doesn't fix the timeout
863 * problem described below.)
865 struct timeval to;
866 to.tv_sec = to_ms / 1000;
867 to.tv_usec = (to_ms * 1000) % 1000000;
868 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
869 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT: %s",
870 pcap_strerror(errno));
871 goto bad;
875 #ifdef _AIX
876 #ifdef BIOCIMMEDIATE
878 * Darren Reed notes that
880 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
881 * timeout appears to be ignored and it waits until the buffer
882 * is filled before returning. The result of not having it
883 * set is almost worse than useless if your BPF filter
884 * is reducing things to only a few packets (i.e. one every
885 * second or so).
887 * so we turn BIOCIMMEDIATE mode on if this is AIX.
889 * We don't turn it on for other platforms, as that means we
890 * get woken up for every packet, which may not be what we want;
891 * in the Winter 1993 USENIX paper on BPF, they say:
893 * Since a process might want to look at every packet on a
894 * network and the time between packets can be only a few
895 * microseconds, it is not possible to do a read system call
896 * per packet and BPF must collect the data from several
897 * packets and return it as a unit when the monitoring
898 * application does a read.
900 * which I infer is the reason for the timeout - it means we
901 * wait that amount of time, in the hopes that more packets
902 * will arrive and we'll get them all with one read.
904 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
905 * BSDs) causes the timeout to be ignored.
907 * On the other hand, some platforms (e.g., Linux) don't support
908 * timeouts, they just hand stuff to you as soon as it arrives;
909 * if that doesn't cause a problem on those platforms, it may
910 * be OK to have BIOCIMMEDIATE mode on BSD as well.
912 * (Note, though, that applications may depend on the read
913 * completing, even if no packets have arrived, when the timeout
914 * expires, e.g. GUI applications that have to check for input
915 * while waiting for packets to arrive; a non-zero timeout
916 * prevents "select()" from working right on FreeBSD and
917 * possibly other BSDs, as the timer doesn't start until a
918 * "read()" is done, so the timer isn't in effect if the
919 * application is blocked on a "select()", and the "select()"
920 * doesn't get woken up for a BPF device until the buffer
921 * fills up.)
923 v = 1;
924 if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
925 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCIMMEDIATE: %s",
926 pcap_strerror(errno));
927 goto bad;
929 #endif /* BIOCIMMEDIATE */
930 #endif /* _AIX */
932 if (promisc) {
933 /* set promiscuous mode, okay if it fails */
934 if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
935 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s",
936 pcap_strerror(errno));
940 if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
941 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s",
942 pcap_strerror(errno));
943 goto bad;
945 p->bufsize = v;
946 p->buffer = (u_char *)malloc(p->bufsize);
947 if (p->buffer == NULL) {
948 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
949 pcap_strerror(errno));
950 goto bad;
952 #ifdef _AIX
953 /* For some strange reason this seems to prevent the EFAULT
954 * problems we have experienced from AIX BPF. */
955 memset(p->buffer, 0x0, p->bufsize);
956 #endif
959 * If there's no filter program installed, there's
960 * no indication to the kernel of what the snapshot
961 * length should be, so no snapshotting is done.
963 * Therefore, when we open the device, we install
964 * an "accept everything" filter with the specified
965 * snapshot length.
967 total_insn.code = (u_short)(BPF_RET | BPF_K);
968 total_insn.jt = 0;
969 total_insn.jf = 0;
970 total_insn.k = snaplen;
972 total_prog.bf_len = 1;
973 total_prog.bf_insns = &total_insn;
974 if (ioctl(p->fd, BIOCSETF, (caddr_t)&total_prog) < 0) {
975 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
976 pcap_strerror(errno));
977 goto bad;
981 * On most BPF platforms, either you can do a "select()" or
982 * "poll()" on a BPF file descriptor and it works correctly,
983 * or you can do it and it will return "readable" if the
984 * hold buffer is full but not if the timeout expires *and*
985 * a non-blocking read will, if the hold buffer is empty
986 * but the store buffer isn't empty, rotate the buffers
987 * and return what packets are available.
989 * In the latter case, the fact that a non-blocking read
990 * will give you the available packets means you can work
991 * around the failure of "select()" and "poll()" to wake up
992 * and return "readable" when the timeout expires by using
993 * the timeout as the "select()" or "poll()" timeout, putting
994 * the BPF descriptor into non-blocking mode, and read from
995 * it regardless of whether "select()" reports it as readable
996 * or not.
998 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
999 * won't wake up and return "readable" if the timer expires
1000 * and non-blocking reads return EWOULDBLOCK if the hold
1001 * buffer is empty, even if the store buffer is non-empty.
1003 * This means the workaround in question won't work.
1005 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
1006 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
1007 * here". On all other BPF platforms, we set it to the FD for
1008 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
1009 * read will, if the hold buffer is empty and the store buffer
1010 * isn't empty, rotate the buffers and return what packets are
1011 * there (and in sufficiently recent versions of OpenBSD
1012 * "select()" and "poll()" should work correctly).
1014 * XXX - what about AIX?
1016 p->selectable_fd = p->fd; /* assume select() works until we know otherwise */
1017 if (uname(&osinfo) == 0) {
1019 * We can check what OS this is.
1021 if (strcmp(osinfo.sysname, "FreeBSD") == 0) {
1022 if (strncmp(osinfo.release, "4.3-", 4) == 0 ||
1023 strncmp(osinfo.release, "4.4-", 4) == 0)
1024 p->selectable_fd = -1;
1028 p->read_op = pcap_read_bpf;
1029 p->inject_op = pcap_inject_bpf;
1030 p->setfilter_op = pcap_setfilter_bpf;
1031 p->setdirection_op = pcap_setdirection_bpf;
1032 p->set_datalink_op = pcap_set_datalink_bpf;
1033 p->getnonblock_op = pcap_getnonblock_fd;
1034 p->setnonblock_op = pcap_setnonblock_fd;
1035 p->stats_op = pcap_stats_bpf;
1036 p->close_op = pcap_close_common;
1038 return (p);
1039 bad:
1040 (void)close(fd);
1041 if (p->dlt_list != NULL)
1042 free(p->dlt_list);
1043 free(p);
1044 return (NULL);
1048 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
1050 #ifdef HAVE_DAG_API
1051 if (dag_platform_finddevs(alldevsp, errbuf) < 0)
1052 return (-1);
1053 #endif /* HAVE_DAG_API */
1055 return (0);
1058 static int
1059 pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
1062 * It looks that BPF code generated by gen_protochain() is not
1063 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
1064 * Take a safer side for now.
1066 if (no_optimize) {
1068 * XXX - what if we already have a filter in the kernel?
1070 if (install_bpf_program(p, fp) < 0)
1071 return (-1);
1072 p->md.use_bpf = 0; /* filtering in userland */
1073 return (0);
1077 * Free any user-mode filter we might happen to have installed.
1079 pcap_freecode(&p->fcode);
1082 * Try to install the kernel filter.
1084 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
1085 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
1086 pcap_strerror(errno));
1087 return (-1);
1089 p->md.use_bpf = 1; /* filtering in the kernel */
1092 * Discard any previously-received packets, as they might have
1093 * passed whatever filter was formerly in effect, but might
1094 * not pass this filter (BIOCSETF discards packets buffered
1095 * in the kernel, so you can lose packets in any case).
1097 p->cc = 0;
1098 return (0);
1102 * Set direction flag: Which packets do we accept on a forwarding
1103 * single device? IN, OUT or both?
1105 static int
1106 pcap_setdirection_bpf(pcap_t *p, pcap_direction_t d)
1108 #ifdef BIOCSSEESENT
1109 u_int seesent;
1110 #endif
1113 * We don't support PCAP_D_OUT.
1115 if (d == PCAP_D_OUT) {
1116 snprintf(p->errbuf, sizeof(p->errbuf),
1117 "Setting direction to PCAP_D_OUT is not supported on BPF");
1118 return -1;
1120 #ifdef BIOCSSEESENT
1121 seesent = (d == PCAP_D_INOUT);
1122 if (ioctl(p->fd, BIOCSSEESENT, &seesent) == -1) {
1123 (void) snprintf(p->errbuf, sizeof(p->errbuf),
1124 "Cannot set direction to %s: %s",
1125 (d == PCAP_D_INOUT) ? "PCAP_D_INOUT" : "PCAP_D_IN",
1126 strerror(errno));
1127 return (-1);
1129 return (0);
1130 #else
1131 (void) snprintf(p->errbuf, sizeof(p->errbuf),
1132 "This system doesn't support BIOCSSEESENT, so the direction can't be set");
1133 return (-1);
1134 #endif
1137 static int
1138 pcap_set_datalink_bpf(pcap_t *p, int dlt)
1140 #ifdef BIOCSDLT
1141 if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
1142 (void) snprintf(p->errbuf, sizeof(p->errbuf),
1143 "Cannot set DLT %d: %s", dlt, strerror(errno));
1144 return (-1);
1146 #endif
1147 return (0);