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