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