]> The Tcpdump Group git mirrors - libpcap/blob - pcap-bpf.c
febae779c6d9ca3b93bf9ce2c9e17b4df28c985a
[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.80 2004-10-05 07:23:39 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 #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
38 #include <net/if.h>
39
40 #ifdef _AIX
41
42 /*
43 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the
44 * native OS version, as we need "struct bpf_config" from it.
45 */
46 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
47
48 #include <sys/types.h>
49
50 /*
51 * Prevent bpf.h from redefining the DLT_ values to their
52 * IFT_ values, as we're going to return the standard libpcap
53 * values, not IBM's non-standard IFT_ values.
54 */
55 #undef _AIX
56 #include <net/bpf.h>
57 #define _AIX
58
59 #include <net/if_types.h> /* for IFT_ values */
60 #include <sys/sysconfig.h>
61 #include <sys/device.h>
62 #include <odmi.h>
63 #include <cf.h>
64
65 #ifdef __64BIT__
66 #define domakedev makedev64
67 #define getmajor major64
68 #define bpf_hdr bpf_hdr32
69 #else /* __64BIT__ */
70 #define domakedev makedev
71 #define getmajor major
72 #endif /* __64BIT__ */
73
74 #define BPF_NAME "bpf"
75 #define BPF_MINORS 4
76 #define DRIVER_PATH "/usr/lib/drivers"
77 #define BPF_NODE "/dev/bpf"
78 static int bpfloadedflag = 0;
79 static int odmlockid = 0;
80
81 #else /* _AIX */
82
83 #include <net/bpf.h>
84
85 #endif /* _AIX */
86
87 #include <ctype.h>
88 #include <errno.h>
89 #include <netdb.h>
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <unistd.h>
94
95 #include "pcap-int.h"
96
97 #ifdef HAVE_DAG_API
98 #include "pcap-dag.h"
99 #endif /* HAVE_DAG_API */
100
101 #ifdef HAVE_OS_PROTO_H
102 #include "os-proto.h"
103 #endif
104
105 #include "gencode.h" /* for "no_optimize" */
106
107 static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
108 static int pcap_set_datalink_bpf(pcap_t *p, int dlt);
109
110 static int
111 pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
112 {
113 struct bpf_stat s;
114
115 /*
116 * "ps_recv" counts packets handed to the filter, not packets
117 * that passed the filter. This includes packets later dropped
118 * because we ran out of buffer space.
119 *
120 * "ps_drop" counts packets dropped inside the BPF device
121 * because we ran out of buffer space. It doesn't count
122 * packets dropped by the interface driver. It counts
123 * only packets that passed the filter.
124 *
125 * Both statistics include packets not yet read from the kernel
126 * by libpcap, and thus not yet seen by the application.
127 */
128 if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
129 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s",
130 pcap_strerror(errno));
131 return (-1);
132 }
133
134 ps->ps_recv = s.bs_recv;
135 ps->ps_drop = s.bs_drop;
136 return (0);
137 }
138
139 static int
140 pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
141 {
142 int cc;
143 int n = 0;
144 register u_char *bp, *ep;
145 struct bpf_insn *fcode;
146
147 fcode = p->md.use_bpf ? NULL : p->fcode.bf_insns;
148 again:
149 /*
150 * Has "pcap_breakloop()" been called?
151 */
152 if (p->break_loop) {
153 /*
154 * Yes - clear the flag that indicates that it
155 * has, and return -2 to indicate that we were
156 * told to break out of the loop.
157 */
158 p->break_loop = 0;
159 return (-2);
160 }
161 cc = p->cc;
162 if (p->cc == 0) {
163 cc = read(p->fd, (char *)p->buffer, p->bufsize);
164 if (cc < 0) {
165 /* Don't choke when we get ptraced */
166 switch (errno) {
167
168 case EINTR:
169 goto again;
170
171 #ifdef _AIX
172 case EFAULT:
173 /*
174 * Sigh. More AIX wonderfulness.
175 *
176 * For some unknown reason the uiomove()
177 * operation in the bpf kernel extension
178 * used to copy the buffer into user
179 * space sometimes returns EFAULT. I have
180 * no idea why this is the case given that
181 * a kernel debugger shows the user buffer
182 * is correct. This problem appears to
183 * be mostly mitigated by the memset of
184 * the buffer before it is first used.
185 * Very strange.... Shaun Clowes
186 *
187 * In any case this means that we shouldn't
188 * treat EFAULT as a fatal error; as we
189 * don't have an API for returning
190 * a "some packets were dropped since
191 * the last packet you saw" indication,
192 * we just ignore EFAULT and keep reading.
193 */
194 goto again;
195 #endif
196
197 case EWOULDBLOCK:
198 return (0);
199 #if defined(sun) && !defined(BSD)
200 /*
201 * Due to a SunOS bug, after 2^31 bytes, the kernel
202 * file offset overflows and read fails with EINVAL.
203 * The lseek() to 0 will fix things.
204 */
205 case EINVAL:
206 if (lseek(p->fd, 0L, SEEK_CUR) +
207 p->bufsize < 0) {
208 (void)lseek(p->fd, 0L, SEEK_SET);
209 goto again;
210 }
211 /* fall through */
212 #endif
213 }
214 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s",
215 pcap_strerror(errno));
216 return (-1);
217 }
218 bp = p->buffer;
219 } else
220 bp = p->bp;
221
222 /*
223 * Loop through each packet.
224 */
225 #define bhp ((struct bpf_hdr *)bp)
226 ep = bp + cc;
227 while (bp < ep) {
228 register int caplen, hdrlen;
229
230 /*
231 * Has "pcap_breakloop()" been called?
232 * If so, return immediately - if we haven't read any
233 * packets, clear the flag and return -2 to indicate
234 * that we were told to break out of the loop, otherwise
235 * leave the flag set, so that the *next* call will break
236 * out of the loop without having read any packets, and
237 * return the number of packets we've processed so far.
238 */
239 if (p->break_loop) {
240 if (n == 0) {
241 p->break_loop = 0;
242 return (-2);
243 } else {
244 p->bp = bp;
245 p->cc = ep - bp;
246 return (n);
247 }
248 }
249
250 caplen = bhp->bh_caplen;
251 hdrlen = bhp->bh_hdrlen;
252 /*
253 * Short-circuit evaluation: if using BPF filter
254 * in kernel, no need to do it now.
255 */
256 if (fcode == NULL ||
257 bpf_filter(fcode, bp + hdrlen, bhp->bh_datalen, caplen)) {
258 #ifdef _AIX
259 /*
260 * AIX's BPF returns seconds/nanoseconds time
261 * stamps, not seconds/microseconds time stamps.
262 *
263 * XXX - I'm guessing here that it's a "struct
264 * timestamp"; if not, this code won't compile,
265 * but, if not, you want to send us a bug report
266 * and fall back on using DLPI. It's not as if
267 * BPF used to work right on AIX before this
268 * change; this change attempts to fix the fact
269 * that it didn't....
270 */
271 bhp->bh_tstamp.tv_usec = bhp->bh_tstamp.tv_usec/1000;
272 #endif
273 /*
274 * XXX A bpf_hdr matches a pcap_pkthdr.
275 */
276 (*callback)(user, (struct pcap_pkthdr*)bp, bp + hdrlen);
277 bp += BPF_WORDALIGN(caplen + hdrlen);
278 if (++n >= cnt && cnt > 0) {
279 p->bp = bp;
280 p->cc = ep - bp;
281 return (n);
282 }
283 } else {
284 /*
285 * Skip this packet.
286 */
287 bp += BPF_WORDALIGN(caplen + hdrlen);
288 }
289 }
290 #undef bhp
291 p->cc = 0;
292 return (n);
293 }
294
295 static int
296 pcap_inject_bpf(pcap_t *p, const void *buf, size_t size)
297 {
298 int ret;
299
300 ret = write(p->fd, buf, size);
301 #ifdef __APPLE__
302 if (ret == -1 && errno == EAFNOSUPPORT) {
303 /*
304 * In Mac OS X, there's a bug wherein setting the
305 * BIOCSHDRCMPLT flag causes writes to fail; see,
306 * for example:
307 *
308 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/BIOCSHDRCMPLT-10.3.3.patch
309 *
310 * So, if, on OS X, we get EAFNOSUPPORT from the write, we
311 * assume it's due to that bug, and turn off that flag
312 * and try again. If we succeed, it either means that
313 * somebody applied the fix from that URL, or other patches
314 * for that bug from
315 *
316 * http://cerberus.sourcefire.com/~jeff/archives/patches/macosx/
317 *
318 * and are running a Darwin kernel with those fixes, or
319 * that Apple fixed the problem in some OS X release.
320 */
321 u_int spoof_eth_src = 0;
322
323 if (ioctl(p->fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
324 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
325 "send: can't turn off BIOCSHDRCMPLT: %s",
326 pcap_strerror(errno));
327 return (-1);
328 }
329
330 /*
331 * Now try the write again.
332 */
333 ret = write(p->fd, buf, size);
334 }
335 #endif /* __APPLE__ */
336 if (ret == -1) {
337 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
338 pcap_strerror(errno));
339 return (-1);
340 }
341 return (ret);
342 }
343
344 #ifdef _AIX
345 static int
346 bpf_odminit(char *errbuf)
347 {
348 char *errstr;
349
350 if (odm_initialize() == -1) {
351 if (odm_err_msg(odmerrno, &errstr) == -1)
352 errstr = "Unknown error";
353 snprintf(errbuf, PCAP_ERRBUF_SIZE,
354 "bpf_load: odm_initialize failed: %s",
355 errstr);
356 return (-1);
357 }
358
359 if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
360 if (odm_err_msg(odmerrno, &errstr) == -1)
361 errstr = "Unknown error";
362 snprintf(errbuf, PCAP_ERRBUF_SIZE,
363 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
364 errstr);
365 return (-1);
366 }
367
368 return (0);
369 }
370
371 static int
372 bpf_odmcleanup(char *errbuf)
373 {
374 char *errstr;
375
376 if (odm_unlock(odmlockid) == -1) {
377 if (odm_err_msg(odmerrno, &errstr) == -1)
378 errstr = "Unknown error";
379 snprintf(errbuf, PCAP_ERRBUF_SIZE,
380 "bpf_load: odm_unlock failed: %s",
381 errstr);
382 return (-1);
383 }
384
385 if (odm_terminate() == -1) {
386 if (odm_err_msg(odmerrno, &errstr) == -1)
387 errstr = "Unknown error";
388 snprintf(errbuf, PCAP_ERRBUF_SIZE,
389 "bpf_load: odm_terminate failed: %s",
390 errstr);
391 return (-1);
392 }
393
394 return (0);
395 }
396
397 static int
398 bpf_load(char *errbuf)
399 {
400 long major;
401 int *minors;
402 int numminors, i, rc;
403 char buf[1024];
404 struct stat sbuf;
405 struct bpf_config cfg_bpf;
406 struct cfg_load cfg_ld;
407 struct cfg_kmod cfg_km;
408
409 /*
410 * This is very very close to what happens in the real implementation
411 * but I've fixed some (unlikely) bug situations.
412 */
413 if (bpfloadedflag)
414 return (0);
415
416 if (bpf_odminit(errbuf) != 0)
417 return (-1);
418
419 major = genmajor(BPF_NAME);
420 if (major == -1) {
421 snprintf(errbuf, PCAP_ERRBUF_SIZE,
422 "bpf_load: genmajor failed: %s", pcap_strerror(errno));
423 return (-1);
424 }
425
426 minors = getminor(major, &numminors, BPF_NAME);
427 if (!minors) {
428 minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
429 if (!minors) {
430 snprintf(errbuf, PCAP_ERRBUF_SIZE,
431 "bpf_load: genminor failed: %s",
432 pcap_strerror(errno));
433 return (-1);
434 }
435 }
436
437 if (bpf_odmcleanup(errbuf))
438 return (-1);
439
440 rc = stat(BPF_NODE "0", &sbuf);
441 if (rc == -1 && errno != ENOENT) {
442 snprintf(errbuf, PCAP_ERRBUF_SIZE,
443 "bpf_load: can't stat %s: %s",
444 BPF_NODE "0", pcap_strerror(errno));
445 return (-1);
446 }
447
448 if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
449 for (i = 0; i < BPF_MINORS; i++) {
450 sprintf(buf, "%s%d", BPF_NODE, i);
451 unlink(buf);
452 if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
453 snprintf(errbuf, PCAP_ERRBUF_SIZE,
454 "bpf_load: can't mknod %s: %s",
455 buf, pcap_strerror(errno));
456 return (-1);
457 }
458 }
459 }
460
461 /* Check if the driver is loaded */
462 memset(&cfg_ld, 0x0, sizeof(cfg_ld));
463 cfg_ld.path = buf;
464 sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME);
465 if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
466 (cfg_ld.kmid == 0)) {
467 /* Driver isn't loaded, load it now */
468 if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
469 snprintf(errbuf, PCAP_ERRBUF_SIZE,
470 "bpf_load: could not load driver: %s",
471 strerror(errno));
472 return (-1);
473 }
474 }
475
476 /* Configure the driver */
477 cfg_km.cmd = CFG_INIT;
478 cfg_km.kmid = cfg_ld.kmid;
479 cfg_km.mdilen = sizeof(cfg_bpf);
480 cfg_km.mdiptr = (void *)&cfg_bpf;
481 for (i = 0; i < BPF_MINORS; i++) {
482 cfg_bpf.devno = domakedev(major, i);
483 if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
484 snprintf(errbuf, PCAP_ERRBUF_SIZE,
485 "bpf_load: could not configure driver: %s",
486 strerror(errno));
487 return (-1);
488 }
489 }
490
491 bpfloadedflag = 1;
492
493 return (0);
494 }
495 #endif
496
497 static inline int
498 bpf_open(pcap_t *p, char *errbuf)
499 {
500 int fd;
501 int n = 0;
502 char device[sizeof "/dev/bpf0000000000"];
503
504 #ifdef _AIX
505 /*
506 * Load the bpf driver, if it isn't already loaded,
507 * and create the BPF device entries, if they don't
508 * already exist.
509 */
510 if (bpf_load(errbuf) == -1)
511 return (-1);
512 #endif
513
514 /*
515 * Go through all the minors and find one that isn't in use.
516 */
517 do {
518 (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
519 /*
520 * Initially try a read/write open (to allow the inject
521 * method to work). If that fails due to permission
522 * issues, fall back to read-only. This allows a
523 * non-root user to be granted specific access to pcap
524 * capabilities via file permissions.
525 *
526 * XXX - we should have an API that has a flag that
527 * controls whether to open read-only or read-write,
528 * so that denial of permission to send (or inability
529 * to send, if sending packets isn't supported on
530 * the device in question) can be indicated at open
531 * time.
532 */
533 fd = open(device, O_RDWR);
534 if (fd == -1 && errno == EACCES)
535 fd = open(device, O_RDONLY);
536 } while (fd < 0 && errno == EBUSY);
537
538 /*
539 * XXX better message for all minors used
540 */
541 if (fd < 0)
542 snprintf(errbuf, PCAP_ERRBUF_SIZE, "(no devices found) %s: %s",
543 device, pcap_strerror(errno));
544
545 return (fd);
546 }
547
548 static void
549 pcap_close_bpf(pcap_t *p)
550 {
551 if (p->buffer != NULL)
552 free(p->buffer);
553 if (p->fd >= 0)
554 close(p->fd);
555 }
556
557 /*
558 * We include the OS's <net/bpf.h>, not our "pcap-bpf.h", so we probably
559 * don't get DLT_DOCSIS defined.
560 */
561 #ifndef DLT_DOCSIS
562 #define DLT_DOCSIS 143
563 #endif
564
565 pcap_t *
566 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
567 char *ebuf)
568 {
569 int fd;
570 struct ifreq ifr;
571 struct bpf_version bv;
572 #ifdef BIOCGDLTLIST
573 struct bpf_dltlist bdl;
574 #endif
575 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
576 u_int spoof_eth_src = 1;
577 #endif
578 u_int v;
579 pcap_t *p;
580 struct utsname osinfo;
581
582 #ifdef HAVE_DAG_API
583 if (strstr(device, "dag")) {
584 return dag_open_live(device, snaplen, promisc, to_ms, ebuf);
585 }
586 #endif /* HAVE_DAG_API */
587
588 #ifdef BIOCGDLTLIST
589 memset(&bdl, 0, sizeof(bdl));
590 #endif
591
592 p = (pcap_t *)malloc(sizeof(*p));
593 if (p == NULL) {
594 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
595 pcap_strerror(errno));
596 return (NULL);
597 }
598 memset(p, 0, sizeof(*p));
599 fd = bpf_open(p, ebuf);
600 if (fd < 0)
601 goto bad;
602
603 p->fd = fd;
604 p->snapshot = snaplen;
605
606 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
607 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s",
608 pcap_strerror(errno));
609 goto bad;
610 }
611 if (bv.bv_major != BPF_MAJOR_VERSION ||
612 bv.bv_minor < BPF_MINOR_VERSION) {
613 snprintf(ebuf, PCAP_ERRBUF_SIZE,
614 "kernel bpf filter out of date");
615 goto bad;
616 }
617
618 /*
619 * Try finding a good size for the buffer; 32768 may be too
620 * big, so keep cutting it in half until we find a size
621 * that works, or run out of sizes to try. If the default
622 * is larger, don't make it smaller.
623 *
624 * XXX - there should be a user-accessible hook to set the
625 * initial buffer size.
626 */
627 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || v < 32768)
628 v = 32768;
629 for ( ; v != 0; v >>= 1) {
630 /* Ignore the return value - this is because the call fails
631 * on BPF systems that don't have kernel malloc. And if
632 * the call fails, it's no big deal, we just continue to
633 * use the standard buffer size.
634 */
635 (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
636
637 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
638 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
639 break; /* that size worked; we're done */
640
641 if (errno != ENOBUFS) {
642 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s",
643 device, pcap_strerror(errno));
644 goto bad;
645 }
646 }
647
648 if (v == 0) {
649 snprintf(ebuf, PCAP_ERRBUF_SIZE,
650 "BIOCSBLEN: %s: No buffer size worked", device);
651 goto bad;
652 }
653
654 /* Get the data link layer type. */
655 if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
656 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s",
657 pcap_strerror(errno));
658 goto bad;
659 }
660 #ifdef _AIX
661 /*
662 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
663 */
664 switch (v) {
665
666 case IFT_ETHER:
667 case IFT_ISO88023:
668 v = DLT_EN10MB;
669 break;
670
671 case IFT_FDDI:
672 v = DLT_FDDI;
673 break;
674
675 case IFT_ISO88025:
676 v = DLT_IEEE802;
677 break;
678
679 case IFT_LOOP:
680 v = DLT_NULL;
681 break;
682
683 default:
684 /*
685 * We don't know what to map this to yet.
686 */
687 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
688 v);
689 goto bad;
690 }
691 #endif
692 #if _BSDI_VERSION - 0 >= 199510
693 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
694 switch (v) {
695
696 case DLT_SLIP:
697 v = DLT_SLIP_BSDOS;
698 break;
699
700 case DLT_PPP:
701 v = DLT_PPP_BSDOS;
702 break;
703
704 case 11: /*DLT_FR*/
705 v = DLT_FRELAY;
706 break;
707
708 case 12: /*DLT_C_HDLC*/
709 v = DLT_CHDLC;
710 break;
711 }
712 #endif
713 p->linktype = v;
714
715 #ifdef BIOCGDLTLIST
716 /*
717 * We know the default link type -- now determine all the DLTs
718 * this interface supports. If this fails with EINVAL, it's
719 * not fatal; we just don't get to use the feature later.
720 */
721 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)&bdl) == 0) {
722 u_int i;
723 int is_ethernet;
724
725 bdl.bfl_list = (u_int *) malloc(sizeof(u_int) * bdl.bfl_len + 1);
726 if (bdl.bfl_list == NULL) {
727 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
728 pcap_strerror(errno));
729 goto bad;
730 }
731
732 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)&bdl) < 0) {
733 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
734 "BIOCGDLTLIST: %s", pcap_strerror(errno));
735 free(bdl.bfl_list);
736 goto bad;
737 }
738
739 /*
740 * OK, for real Ethernet devices, add DLT_DOCSIS to the
741 * list, so that an application can let you choose it,
742 * in case you're capturing DOCSIS traffic that a Cisco
743 * Cable Modem Termination System is putting out onto
744 * an Ethernet (it doesn't put an Ethernet header onto
745 * the wire, it puts raw DOCSIS frames out on the wire
746 * inside the low-level Ethernet framing).
747 *
748 * A "real Ethernet device" is defined here as a device
749 * that has a link-layer type of DLT_EN10MB and that has
750 * no alternate link-layer types; that's done to exclude
751 * 802.11 interfaces (which might or might not be the
752 * right thing to do, but I suspect it is - Ethernet <->
753 * 802.11 bridges would probably badly mishandle frames
754 * that don't have Ethernet headers).
755 */
756 if (p->linktype == DLT_EN10MB) {
757 is_ethernet = 1;
758 for (i = 0; i < bdl.bfl_len; i++) {
759 if (bdl.bfl_list[i] != DLT_EN10MB) {
760 is_ethernet = 0;
761 break;
762 }
763 }
764 if (is_ethernet) {
765 /*
766 * We reserved one more slot at the end of
767 * the list.
768 */
769 bdl.bfl_list[bdl.bfl_len] = DLT_DOCSIS;
770 bdl.bfl_len++;
771 }
772 }
773 p->dlt_count = bdl.bfl_len;
774 p->dlt_list = bdl.bfl_list;
775 } else {
776 if (errno != EINVAL) {
777 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
778 "BIOCGDLTLIST: %s", pcap_strerror(errno));
779 goto bad;
780 }
781 }
782 #endif
783
784 /*
785 * If this is an Ethernet device, and we don't have a DLT_ list,
786 * give it a list with DLT_EN10MB and DLT_DOCSIS. (That'd give
787 * 802.11 interfaces DLT_DOCSIS, which isn't the right thing to
788 * do, but there's not much we can do about that without finding
789 * some other way of determining whether it's an Ethernet or 802.11
790 * device.)
791 */
792 if (p->linktype == DLT_EN10MB && p->dlt_count == 0) {
793 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
794 /*
795 * If that fails, just leave the list empty.
796 */
797 if (p->dlt_list != NULL) {
798 p->dlt_list[0] = DLT_EN10MB;
799 p->dlt_list[1] = DLT_DOCSIS;
800 p->dlt_count = 2;
801 }
802 }
803
804 #if defined(BIOCGHDRCMPLT) && defined(BIOCSHDRCMPLT)
805 /*
806 * Do a BIOCSHDRCMPLT, if defined, to turn that flag on, so
807 * the link-layer source address isn't forcibly overwritten.
808 * (Should we ignore errors? Should we do this only if
809 * we're open for writing?)
810 *
811 * XXX - I seem to remember some packet-sending bug in some
812 * BSDs - check CVS log for "bpf.c"?
813 */
814 if (ioctl(fd, BIOCSHDRCMPLT, &spoof_eth_src) == -1) {
815 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
816 "BIOCSHDRCMPLT: %s", pcap_strerror(errno));
817 goto bad;
818 }
819 #endif
820 /* set timeout */
821 if (to_ms != 0) {
822 /*
823 * XXX - is this seconds/nanoseconds in AIX?
824 * (Treating it as such doesn't fix the timeout
825 * problem described below.)
826 */
827 struct timeval to;
828 to.tv_sec = to_ms / 1000;
829 to.tv_usec = (to_ms * 1000) % 1000000;
830 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
831 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT: %s",
832 pcap_strerror(errno));
833 goto bad;
834 }
835 }
836
837 #ifdef _AIX
838 #ifdef BIOCIMMEDIATE
839 /*
840 * Darren Reed notes that
841 *
842 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
843 * timeout appears to be ignored and it waits until the buffer
844 * is filled before returning. The result of not having it
845 * set is almost worse than useless if your BPF filter
846 * is reducing things to only a few packets (i.e. one every
847 * second or so).
848 *
849 * so we turn BIOCIMMEDIATE mode on if this is AIX.
850 *
851 * We don't turn it on for other platforms, as that means we
852 * get woken up for every packet, which may not be what we want;
853 * in the Winter 1993 USENIX paper on BPF, they say:
854 *
855 * Since a process might want to look at every packet on a
856 * network and the time between packets can be only a few
857 * microseconds, it is not possible to do a read system call
858 * per packet and BPF must collect the data from several
859 * packets and return it as a unit when the monitoring
860 * application does a read.
861 *
862 * which I infer is the reason for the timeout - it means we
863 * wait that amount of time, in the hopes that more packets
864 * will arrive and we'll get them all with one read.
865 *
866 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
867 * BSDs) causes the timeout to be ignored.
868 *
869 * On the other hand, some platforms (e.g., Linux) don't support
870 * timeouts, they just hand stuff to you as soon as it arrives;
871 * if that doesn't cause a problem on those platforms, it may
872 * be OK to have BIOCIMMEDIATE mode on BSD as well.
873 *
874 * (Note, though, that applications may depend on the read
875 * completing, even if no packets have arrived, when the timeout
876 * expires, e.g. GUI applications that have to check for input
877 * while waiting for packets to arrive; a non-zero timeout
878 * prevents "select()" from working right on FreeBSD and
879 * possibly other BSDs, as the timer doesn't start until a
880 * "read()" is done, so the timer isn't in effect if the
881 * application is blocked on a "select()", and the "select()"
882 * doesn't get woken up for a BPF device until the buffer
883 * fills up.)
884 */
885 v = 1;
886 if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
887 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCIMMEDIATE: %s",
888 pcap_strerror(errno));
889 goto bad;
890 }
891 #endif /* BIOCIMMEDIATE */
892 #endif /* _AIX */
893
894 if (promisc) {
895 /* set promiscuous mode, okay if it fails */
896 if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
897 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s",
898 pcap_strerror(errno));
899 }
900 }
901
902 if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
903 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s",
904 pcap_strerror(errno));
905 goto bad;
906 }
907 p->bufsize = v;
908 p->buffer = (u_char *)malloc(p->bufsize);
909 if (p->buffer == NULL) {
910 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
911 pcap_strerror(errno));
912 goto bad;
913 }
914 #ifdef _AIX
915 /* For some strange reason this seems to prevent the EFAULT
916 * problems we have experienced from AIX BPF. */
917 memset(p->buffer, 0x0, p->bufsize);
918 #endif
919
920 /*
921 * On most BPF platforms, either you can do a "select()" or
922 * "poll()" on a BPF file descriptor and it works correctly,
923 * or you can do it and it will return "readable" if the
924 * hold buffer is full but not if the timeout expires *and*
925 * a non-blocking read will, if the hold buffer is empty
926 * but the store buffer isn't empty, rotate the buffers
927 * and return what packets are available.
928 *
929 * In the latter case, the fact that a non-blocking read
930 * will give you the available packets means you can work
931 * around the failure of "select()" and "poll()" to wake up
932 * and return "readable" when the timeout expires by using
933 * the timeout as the "select()" or "poll()" timeout, putting
934 * the BPF descriptor into non-blocking mode, and read from
935 * it regardless of whether "select()" reports it as readable
936 * or not.
937 *
938 * However, in FreeBSD 4.3 and 4.4, "select()" and "poll()"
939 * won't wake up and return "readable" if the timer expires
940 * and non-blocking reads return EWOULDBLOCK if the hold
941 * buffer is empty, even if the store buffer is non-empty.
942 *
943 * This means the workaround in question won't work.
944 *
945 * Therefore, on FreeBSD 4.3 and 4.4, we set "p->selectable_fd"
946 * to -1, which means "sorry, you can't use 'select()' or 'poll()'
947 * here". On all other BPF platforms, we set it to the FD for
948 * the BPF device; in NetBSD, OpenBSD, and Darwin, a non-blocking
949 * read will, if the hold buffer is empty and the store buffer
950 * isn't empty, rotate the buffers and return what packets are
951 * there (and in sufficiently recent versions of OpenBSD
952 * "select()" and "poll()" should work correctly).
953 *
954 * XXX - what about AIX?
955 */
956 if (uname(&osinfo) == 0) {
957 /*
958 * We can check what OS this is.
959 */
960 if (strcmp(osinfo.sysname, "FreeBSD") == 0 &&
961 (strncmp(osinfo.release, "4.3-", 4) == 0 ||
962 strncmp(osinfo.release, "4.4-", 4) == 0))
963 p->selectable_fd = -1;
964 else
965 p->selectable_fd = p->fd;
966 } else {
967 /*
968 * We can't find out what OS this is, so assume we can
969 * do a "select()" or "poll()".
970 */
971 p->selectable_fd = p->fd;
972 }
973
974 p->read_op = pcap_read_bpf;
975 p->inject_op = pcap_inject_bpf;
976 p->setfilter_op = pcap_setfilter_bpf;
977 p->set_datalink_op = pcap_set_datalink_bpf;
978 p->getnonblock_op = pcap_getnonblock_fd;
979 p->setnonblock_op = pcap_setnonblock_fd;
980 p->stats_op = pcap_stats_bpf;
981 p->close_op = pcap_close_bpf;
982
983 return (p);
984 bad:
985 (void)close(fd);
986 if (p->dlt_list != NULL)
987 free(p->dlt_list);
988 free(p);
989 return (NULL);
990 }
991
992 int
993 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
994 {
995 #ifdef HAVE_DAG_API
996 if (dag_platform_finddevs(alldevsp, errbuf) < 0)
997 return (-1);
998 #endif /* HAVE_DAG_API */
999
1000 return (0);
1001 }
1002
1003 static int
1004 pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
1005 {
1006 /*
1007 * It looks that BPF code generated by gen_protochain() is not
1008 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
1009 * Take a safer side for now.
1010 */
1011 if (no_optimize) {
1012 /*
1013 * XXX - what if we already have a filter in the kernel?
1014 */
1015 if (install_bpf_program(p, fp) < 0)
1016 return (-1);
1017 p->md.use_bpf = 0; /* filtering in userland */
1018 return (0);
1019 }
1020
1021 /*
1022 * Free any user-mode filter we might happen to have installed.
1023 */
1024 pcap_freecode(&p->fcode);
1025
1026 /*
1027 * Try to install the kernel filter.
1028 */
1029 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
1030 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
1031 pcap_strerror(errno));
1032 return (-1);
1033 }
1034 p->md.use_bpf = 1; /* filtering in the kernel */
1035 return (0);
1036 }
1037
1038 static int
1039 pcap_set_datalink_bpf(pcap_t *p, int dlt)
1040 {
1041 #ifdef BIOCSDLT
1042 if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
1043 (void) snprintf(p->errbuf, sizeof(p->errbuf),
1044 "Cannot set DLT %d: %s", dlt, strerror(errno));
1045 return (-1);
1046 }
1047 #endif
1048 return (0);
1049 }