]> The Tcpdump Group git mirrors - libpcap/blob - pcap-bpf.c
Add a "pcap_breakloop()" API to break out of the loop in
[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[] =
23 "@(#) $Header: /tcpdump/master/libpcap/pcap-bpf.c,v 1.67 2003-11-04 07:05:32 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
37 #include <net/if.h>
38
39 #ifdef _AIX
40
41 /*
42 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the
43 * native OS version, as we need "struct bpf_config" from it.
44 */
45 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
46
47 #include <sys/types.h>
48
49 /*
50 * Prevent bpf.h from redefining the DLT_ values to their
51 * IFT_ values, as we're going to return the standard libpcap
52 * values, not IBM's non-standard IFT_ values.
53 */
54 #undef _AIX
55 #include <net/bpf.h>
56 #define _AIX
57
58 #include <net/if_types.h> /* for IFT_ values */
59 #include <sys/sysconfig.h>
60 #include <sys/device.h>
61 #include <odmi.h>
62 #include <cf.h>
63
64 #ifdef __64BIT__
65 #define domakedev makedev64
66 #define getmajor major64
67 #define bpf_hdr bpf_hdr32
68 #else /* __64BIT__ */
69 #define domakedev makedev
70 #define getmajor major
71 #endif /* __64BIT__ */
72
73 #define BPF_NAME "bpf"
74 #define BPF_MINORS 4
75 #define DRIVER_PATH "/usr/lib/drivers"
76 #define BPF_NODE "/dev/bpf"
77 static int bpfloadedflag = 0;
78 static int odmlockid = 0;
79
80 #else /* _AIX */
81
82 #include <net/bpf.h>
83
84 #endif /* _AIX */
85
86 #include <ctype.h>
87 #include <errno.h>
88 #include <netdb.h>
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <unistd.h>
93
94 #include "pcap-int.h"
95
96 #ifdef HAVE_DAG_API
97 #include "pcap-dag.h"
98 #endif /* HAVE_DAG_API */
99
100 #ifdef HAVE_OS_PROTO_H
101 #include "os-proto.h"
102 #endif
103
104 #include "gencode.h" /* for "no_optimize" */
105
106 static int pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp);
107 static int pcap_set_datalink_bpf(pcap_t *p, int dlt);
108
109 static int
110 pcap_stats_bpf(pcap_t *p, struct pcap_stat *ps)
111 {
112 struct bpf_stat s;
113
114 /*
115 * "ps_recv" counts packets handed to the filter, not packets
116 * that passed the filter. This includes packets later dropped
117 * because we ran out of buffer space.
118 *
119 * "ps_drop" counts packets dropped inside the BPF device
120 * because we ran out of buffer space. It doesn't count
121 * packets dropped by the interface driver. It counts
122 * only packets that passed the filter.
123 *
124 * Both statistics include packets not yet read from the kernel
125 * by libpcap, and thus not yet seen by the application.
126 */
127 if (ioctl(p->fd, BIOCGSTATS, (caddr_t)&s) < 0) {
128 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCGSTATS: %s",
129 pcap_strerror(errno));
130 return (-1);
131 }
132
133 ps->ps_recv = s.bs_recv;
134 ps->ps_drop = s.bs_drop;
135 return (0);
136 }
137
138 static int
139 pcap_read_bpf(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
140 {
141 int cc;
142 int n = 0;
143 register u_char *bp, *ep;
144
145 again:
146 /*
147 * Has "pcap_breakloop()" been called?
148 */
149 if (p->break_loop) {
150 /*
151 * Yes - clear the flag that indicates that it
152 * has, and return -2 to indicate that we were
153 * told to break out of the loop.
154 */
155 p->break_loop = 0;
156 return (-2);
157 }
158 cc = p->cc;
159 if (p->cc == 0) {
160 cc = read(p->fd, (char *)p->buffer, p->bufsize);
161 if (cc < 0) {
162 /* Don't choke when we get ptraced */
163 switch (errno) {
164
165 case EINTR:
166 goto again;
167
168 #ifdef _AIX
169 case EFAULT:
170 /*
171 * Sigh. More AIX wonderfulness.
172 *
173 * For some unknown reason the uiomove()
174 * operation in the bpf kernel extension
175 * used to copy the buffer into user
176 * space sometimes returns EFAULT. I have
177 * no idea why this is the case given that
178 * a kernel debugger shows the user buffer
179 * is correct. This problem appears to
180 * be mostly mitigated by the memset of
181 * the buffer before it is first used.
182 * Very strange.... Shaun Clowes
183 *
184 * In any case this means that we shouldn't
185 * treat EFAULT as a fatal error; as we
186 * don't have an API for returning
187 * a "some packets were dropped since
188 * the last packet you saw" indication,
189 * we just ignore EFAULT and keep reading.
190 */
191 goto again;
192 #endif
193
194 case EWOULDBLOCK:
195 return (0);
196 #if defined(sun) && !defined(BSD)
197 /*
198 * Due to a SunOS bug, after 2^31 bytes, the kernel
199 * file offset overflows and read fails with EINVAL.
200 * The lseek() to 0 will fix things.
201 */
202 case EINVAL:
203 if (lseek(p->fd, 0L, SEEK_CUR) +
204 p->bufsize < 0) {
205 (void)lseek(p->fd, 0L, SEEK_SET);
206 goto again;
207 }
208 /* fall through */
209 #endif
210 }
211 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "read: %s",
212 pcap_strerror(errno));
213 return (-1);
214 }
215 bp = p->buffer;
216 } else
217 bp = p->bp;
218
219 /*
220 * Loop through each packet.
221 */
222 #define bhp ((struct bpf_hdr *)bp)
223 ep = bp + cc;
224 while (bp < ep) {
225 register int caplen, hdrlen;
226
227 /*
228 * Has "pcap_breakloop()" been called?
229 * If so, return immediately - if we haven't read any
230 * packets, clear the flag and return -2 to indicate
231 * that we were told to break out of the loop, otherwise
232 * leave the flag set, so that the *next* call will break
233 * out of the loop without having read any packets, and
234 * return the number of packets we've processed so far.
235 */
236 if (p->break_loop) {
237 if (n == 0) {
238 p->break_loop = 0;
239 return (-2);
240 } else {
241 p->bp = bp;
242 p->cc = ep - bp;
243 return (n);
244 }
245 }
246
247 caplen = bhp->bh_caplen;
248 hdrlen = bhp->bh_hdrlen;
249 /*
250 * XXX A bpf_hdr matches a pcap_pkthdr.
251 */
252 #ifdef _AIX
253 /*
254 * AIX's BPF returns seconds/nanoseconds time stamps, not
255 * seconds/microseconds time stamps.
256 *
257 * XXX - I'm guessing here that it's a "struct timestamp";
258 * if not, this code won't compile, but, if not, you
259 * want to send us a bug report and fall back on using
260 * DLPI. It's not as if BPF used to work right on
261 * AIX before this change; this change attempts to fix
262 * the fact that it didn't....
263 */
264 bhp->bh_tstamp.tv_usec = bhp->bh_tstamp.tv_usec/1000;
265 #endif
266 (*callback)(user, (struct pcap_pkthdr*)bp, bp + hdrlen);
267 bp += BPF_WORDALIGN(caplen + hdrlen);
268 if (++n >= cnt && cnt > 0) {
269 p->bp = bp;
270 p->cc = ep - bp;
271 return (n);
272 }
273 }
274 #undef bhp
275 p->cc = 0;
276 return (n);
277 }
278
279 #ifdef _AIX
280 static int
281 bpf_odminit(char *errbuf)
282 {
283 char *errstr;
284
285 if (odm_initialize() == -1) {
286 if (odm_err_msg(odmerrno, &errstr) == -1)
287 errstr = "Unknown error";
288 snprintf(errbuf, PCAP_ERRBUF_SIZE,
289 "bpf_load: odm_initialize failed: %s",
290 errstr);
291 return (-1);
292 }
293
294 if ((odmlockid = odm_lock("/etc/objrepos/config_lock", ODM_WAIT)) == -1) {
295 if (odm_err_msg(odmerrno, &errstr) == -1)
296 errstr = "Unknown error";
297 snprintf(errbuf, PCAP_ERRBUF_SIZE,
298 "bpf_load: odm_lock of /etc/objrepos/config_lock failed: %s",
299 errstr);
300 return (-1);
301 }
302
303 return (0);
304 }
305
306 static int
307 bpf_odmcleanup(char *errbuf)
308 {
309 char *errstr;
310
311 if (odm_unlock(odmlockid) == -1) {
312 if (odm_err_msg(odmerrno, &errstr) == -1)
313 errstr = "Unknown error";
314 snprintf(errbuf, PCAP_ERRBUF_SIZE,
315 "bpf_load: odm_unlock failed: %s",
316 errstr);
317 return (-1);
318 }
319
320 if (odm_terminate() == -1) {
321 if (odm_err_msg(odmerrno, &errstr) == -1)
322 errstr = "Unknown error";
323 snprintf(errbuf, PCAP_ERRBUF_SIZE,
324 "bpf_load: odm_terminate failed: %s",
325 errstr);
326 return (-1);
327 }
328
329 return (0);
330 }
331
332 static int
333 bpf_load(char *errbuf)
334 {
335 long major;
336 int *minors;
337 int numminors, i, rc;
338 char buf[1024];
339 struct stat sbuf;
340 struct bpf_config cfg_bpf;
341 struct cfg_load cfg_ld;
342 struct cfg_kmod cfg_km;
343
344 /*
345 * This is very very close to what happens in the real implementation
346 * but I've fixed some (unlikely) bug situations.
347 */
348 if (bpfloadedflag)
349 return (0);
350
351 if (bpf_odminit(errbuf) != 0)
352 return (-1);
353
354 major = genmajor(BPF_NAME);
355 if (major == -1) {
356 snprintf(errbuf, PCAP_ERRBUF_SIZE,
357 "bpf_load: genmajor failed: %s", pcap_strerror(errno));
358 return (-1);
359 }
360
361 minors = getminor(major, &numminors, BPF_NAME);
362 if (!minors) {
363 minors = genminor("bpf", major, 0, BPF_MINORS, 1, 1);
364 if (!minors) {
365 snprintf(errbuf, PCAP_ERRBUF_SIZE,
366 "bpf_load: genminor failed: %s",
367 pcap_strerror(errno));
368 return (-1);
369 }
370 }
371
372 if (bpf_odmcleanup(errbuf))
373 return (-1);
374
375 rc = stat(BPF_NODE "0", &sbuf);
376 if (rc == -1 && errno != ENOENT) {
377 snprintf(errbuf, PCAP_ERRBUF_SIZE,
378 "bpf_load: can't stat %s: %s",
379 BPF_NODE "0", pcap_strerror(errno));
380 return (-1);
381 }
382
383 if (rc == -1 || getmajor(sbuf.st_rdev) != major) {
384 for (i = 0; i < BPF_MINORS; i++) {
385 sprintf(buf, "%s%d", BPF_NODE, i);
386 unlink(buf);
387 if (mknod(buf, S_IRUSR | S_IFCHR, domakedev(major, i)) == -1) {
388 snprintf(errbuf, PCAP_ERRBUF_SIZE,
389 "bpf_load: can't mknod %s: %s",
390 buf, pcap_strerror(errno));
391 return (-1);
392 }
393 }
394 }
395
396 /* Check if the driver is loaded */
397 memset(&cfg_ld, 0x0, sizeof(cfg_ld));
398 cfg_ld.path = buf;
399 sprintf(cfg_ld.path, "%s/%s", DRIVER_PATH, BPF_NAME);
400 if ((sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) ||
401 (cfg_ld.kmid == 0)) {
402 /* Driver isn't loaded, load it now */
403 if (sysconfig(SYS_SINGLELOAD, (void *)&cfg_ld, sizeof(cfg_ld)) == -1) {
404 snprintf(errbuf, PCAP_ERRBUF_SIZE,
405 "bpf_load: could not load driver: %s",
406 strerror(errno));
407 return (-1);
408 }
409 }
410
411 /* Configure the driver */
412 cfg_km.cmd = CFG_INIT;
413 cfg_km.kmid = cfg_ld.kmid;
414 cfg_km.mdilen = sizeof(cfg_bpf);
415 cfg_km.mdiptr = (void *)&cfg_bpf;
416 for (i = 0; i < BPF_MINORS; i++) {
417 cfg_bpf.devno = domakedev(major, i);
418 if (sysconfig(SYS_CFGKMOD, (void *)&cfg_km, sizeof(cfg_km)) == -1) {
419 snprintf(errbuf, PCAP_ERRBUF_SIZE,
420 "bpf_load: could not configure driver: %s",
421 strerror(errno));
422 return (-1);
423 }
424 }
425
426 bpfloadedflag = 1;
427
428 return (0);
429 }
430 #endif
431
432 static inline int
433 bpf_open(pcap_t *p, char *errbuf)
434 {
435 int fd;
436 int n = 0;
437 char device[sizeof "/dev/bpf0000000000"];
438
439 #ifdef _AIX
440 /*
441 * Load the bpf driver, if it isn't already loaded,
442 * and create the BPF device entries, if they don't
443 * already exist.
444 */
445 if (bpf_load(errbuf) == -1)
446 return (-1);
447 #endif
448
449 /*
450 * Go through all the minors and find one that isn't in use.
451 */
452 do {
453 (void)snprintf(device, sizeof(device), "/dev/bpf%d", n++);
454 fd = open(device, O_RDONLY);
455 } while (fd < 0 && errno == EBUSY);
456
457 /*
458 * XXX better message for all minors used
459 */
460 if (fd < 0)
461 snprintf(errbuf, PCAP_ERRBUF_SIZE, "(no devices found) %s: %s",
462 device, pcap_strerror(errno));
463
464 return (fd);
465 }
466
467 static void
468 pcap_close_bpf(pcap_t *p)
469 {
470 if (p->buffer != NULL)
471 free(p->buffer);
472 if (p->fd >= 0)
473 close(p->fd);
474 }
475
476 /*
477 * XXX - on AIX, IBM's tcpdump (and perhaps the incompatible-with-everybody-
478 * else's libpcap in AIX 5.1) appears to forcibly load the BPF driver
479 * if it's not already loaded, and to create the BPF devices if they
480 * don't exist.
481 *
482 * It'd be nice if we could do the same, although the code to do so
483 * might be version-dependent, alas (the way to do it isn't necessarily
484 * documented).
485 */
486 pcap_t *
487 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
488 char *ebuf)
489 {
490 int fd;
491 struct ifreq ifr;
492 struct bpf_version bv;
493 #ifdef BIOCGDLTLIST
494 struct bpf_dltlist bdl;
495 #endif
496 u_int v;
497 pcap_t *p;
498
499 #ifdef HAVE_DAG_API
500 if (strstr(device, "dag")) {
501 return dag_open_live(device, snaplen, promisc, to_ms, ebuf);
502 }
503 #endif /* HAVE_DAG_API */
504
505 #ifdef BIOCGDLTLIST
506 memset(&bdl, 0, sizeof(bdl));
507 #endif
508
509 p = (pcap_t *)malloc(sizeof(*p));
510 if (p == NULL) {
511 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
512 pcap_strerror(errno));
513 return (NULL);
514 }
515 memset(p, 0, sizeof(*p));
516 fd = bpf_open(p, ebuf);
517 if (fd < 0)
518 goto bad;
519
520 p->fd = fd;
521 p->snapshot = snaplen;
522
523 if (ioctl(fd, BIOCVERSION, (caddr_t)&bv) < 0) {
524 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCVERSION: %s",
525 pcap_strerror(errno));
526 goto bad;
527 }
528 if (bv.bv_major != BPF_MAJOR_VERSION ||
529 bv.bv_minor < BPF_MINOR_VERSION) {
530 snprintf(ebuf, PCAP_ERRBUF_SIZE,
531 "kernel bpf filter out of date");
532 goto bad;
533 }
534
535 /*
536 * Try finding a good size for the buffer; 32768 may be too
537 * big, so keep cutting it in half until we find a size
538 * that works, or run out of sizes to try. If the default
539 * is larger, don't make it smaller.
540 *
541 * XXX - there should be a user-accessible hook to set the
542 * initial buffer size.
543 */
544 if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || v < 32768)
545 v = 32768;
546 for ( ; v != 0; v >>= 1) {
547 /* Ignore the return value - this is because the call fails
548 * on BPF systems that don't have kernel malloc. And if
549 * the call fails, it's no big deal, we just continue to
550 * use the standard buffer size.
551 */
552 (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v);
553
554 (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
555 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0)
556 break; /* that size worked; we're done */
557
558 if (errno != ENOBUFS) {
559 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s",
560 device, pcap_strerror(errno));
561 goto bad;
562 }
563 }
564
565 if (v == 0) {
566 snprintf(ebuf, PCAP_ERRBUF_SIZE,
567 "BIOCSBLEN: %s: No buffer size worked", device);
568 goto bad;
569 }
570
571 /* Get the data link layer type. */
572 if (ioctl(fd, BIOCGDLT, (caddr_t)&v) < 0) {
573 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGDLT: %s",
574 pcap_strerror(errno));
575 goto bad;
576 }
577 #ifdef _AIX
578 /*
579 * AIX's BPF returns IFF_ types, not DLT_ types, in BIOCGDLT.
580 */
581 switch (v) {
582
583 case IFT_ETHER:
584 case IFT_ISO88023:
585 v = DLT_EN10MB;
586 break;
587
588 case IFT_FDDI:
589 v = DLT_FDDI;
590 break;
591
592 case IFT_ISO88025:
593 v = DLT_IEEE802;
594 break;
595
596 case IFT_LOOP:
597 v = DLT_NULL;
598 break;
599
600 default:
601 /*
602 * We don't know what to map this to yet.
603 */
604 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown interface type %u",
605 v);
606 goto bad;
607 }
608 #endif
609 #if _BSDI_VERSION - 0 >= 199510
610 /* The SLIP and PPP link layer header changed in BSD/OS 2.1 */
611 switch (v) {
612
613 case DLT_SLIP:
614 v = DLT_SLIP_BSDOS;
615 break;
616
617 case DLT_PPP:
618 v = DLT_PPP_BSDOS;
619 break;
620
621 case 11: /*DLT_FR*/
622 v = DLT_FRELAY;
623 break;
624
625 case 12: /*DLT_C_HDLC*/
626 v = DLT_CHDLC;
627 break;
628 }
629 #endif
630 p->linktype = v;
631
632 #ifdef BIOCGDLTLIST
633 /*
634 * We know the default link type -- now determine all the DLTs
635 * this interface supports. If this fails with EINVAL, it's
636 * not fatal; we just don't get to use the feature later.
637 */
638 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)&bdl) == 0) {
639 bdl.bfl_list = (u_int *) malloc(sizeof(u_int) * bdl.bfl_len);
640 if (bdl.bfl_list == NULL) {
641 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
642 pcap_strerror(errno));
643 goto bad;
644 }
645
646 if (ioctl(fd, BIOCGDLTLIST, (caddr_t)&bdl) < 0) {
647 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
648 "BIOCGDLTLIST: %s", pcap_strerror(errno));
649 goto bad;
650 }
651
652 p->dlt_count = bdl.bfl_len;
653 p->dlt_list = bdl.bfl_list;
654 } else {
655 if (errno != EINVAL) {
656 (void)snprintf(ebuf, PCAP_ERRBUF_SIZE,
657 "BIOCGDLTLIST: %s", pcap_strerror(errno));
658 goto bad;
659 }
660 }
661 #endif
662
663 /* set timeout */
664 if (to_ms != 0) {
665 /*
666 * XXX - is this seconds/nanoseconds in AIX?
667 * (Treating it as such doesn't fix the timeout
668 * problem described below.)
669 */
670 struct timeval to;
671 to.tv_sec = to_ms / 1000;
672 to.tv_usec = (to_ms * 1000) % 1000000;
673 if (ioctl(p->fd, BIOCSRTIMEOUT, (caddr_t)&to) < 0) {
674 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSRTIMEOUT: %s",
675 pcap_strerror(errno));
676 goto bad;
677 }
678 }
679
680 #ifdef _AIX
681 #ifdef BIOCIMMEDIATE
682 /*
683 * Darren Reed notes that
684 *
685 * On AIX (4.2 at least), if BIOCIMMEDIATE is not set, the
686 * timeout appears to be ignored and it waits until the buffer
687 * is filled before returning. The result of not having it
688 * set is almost worse than useless if your BPF filter
689 * is reducing things to only a few packets (i.e. one every
690 * second or so).
691 *
692 * so we turn BIOCIMMEDIATE mode on if this is AIX.
693 *
694 * We don't turn it on for other platforms, as that means we
695 * get woken up for every packet, which may not be what we want;
696 * in the Winter 1993 USENIX paper on BPF, they say:
697 *
698 * Since a process might want to look at every packet on a
699 * network and the time between packets can be only a few
700 * microseconds, it is not possible to do a read system call
701 * per packet and BPF must collect the data from several
702 * packets and return it as a unit when the monitoring
703 * application does a read.
704 *
705 * which I infer is the reason for the timeout - it means we
706 * wait that amount of time, in the hopes that more packets
707 * will arrive and we'll get them all with one read.
708 *
709 * Setting BIOCIMMEDIATE mode on FreeBSD (and probably other
710 * BSDs) causes the timeout to be ignored.
711 *
712 * On the other hand, some platforms (e.g., Linux) don't support
713 * timeouts, they just hand stuff to you as soon as it arrives;
714 * if that doesn't cause a problem on those platforms, it may
715 * be OK to have BIOCIMMEDIATE mode on BSD as well.
716 *
717 * (Note, though, that applications may depend on the read
718 * completing, even if no packets have arrived, when the timeout
719 * expires, e.g. GUI applications that have to check for input
720 * while waiting for packets to arrive; a non-zero timeout
721 * prevents "select()" from working right on FreeBSD and
722 * possibly other BSDs, as the timer doesn't start until a
723 * "read()" is done, so the timer isn't in effect if the
724 * application is blocked on a "select()", and the "select()"
725 * doesn't get woken up for a BPF device until the buffer
726 * fills up.)
727 */
728 v = 1;
729 if (ioctl(p->fd, BIOCIMMEDIATE, &v) < 0) {
730 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCIMMEDIATE: %s",
731 pcap_strerror(errno));
732 goto bad;
733 }
734 #endif /* BIOCIMMEDIATE */
735 #endif /* _AIX */
736
737 if (promisc) {
738 /* set promiscuous mode, okay if it fails */
739 if (ioctl(p->fd, BIOCPROMISC, NULL) < 0) {
740 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCPROMISC: %s",
741 pcap_strerror(errno));
742 }
743 }
744
745 if (ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) {
746 snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCGBLEN: %s",
747 pcap_strerror(errno));
748 goto bad;
749 }
750 p->bufsize = v;
751 p->buffer = (u_char *)malloc(p->bufsize);
752 if (p->buffer == NULL) {
753 snprintf(ebuf, PCAP_ERRBUF_SIZE, "malloc: %s",
754 pcap_strerror(errno));
755 goto bad;
756 }
757 #ifdef _AIX
758 /* For some strange reason this seems to prevent the EFAULT
759 * problems we have experienced from AIX BPF. */
760 memset(p->buffer, 0x0, p->bufsize);
761 #endif
762
763 p->read_op = pcap_read_bpf;
764 p->setfilter_op = pcap_setfilter_bpf;
765 p->set_datalink_op = pcap_set_datalink_bpf;
766 p->stats_op = pcap_stats_bpf;
767 p->close_op = pcap_close_bpf;
768
769 return (p);
770 bad:
771 (void)close(fd);
772 #ifdef BIOCGDLTLIST
773 if (bdl.bfl_list != NULL)
774 free(bdl.bfl_list);
775 #endif
776 free(p);
777 return (NULL);
778 }
779
780 int
781 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
782 {
783 #ifdef HAVE_DAG_API
784 if (dag_platform_finddevs(alldevsp, errbuf) < 0)
785 return (-1);
786 #endif /* HAVE_DAG_API */
787
788 return (0);
789 }
790
791 static int
792 pcap_setfilter_bpf(pcap_t *p, struct bpf_program *fp)
793 {
794 /*
795 * It looks that BPF code generated by gen_protochain() is not
796 * compatible with some of kernel BPF code (for example BSD/OS 3.1).
797 * Take a safer side for now.
798 */
799 if (no_optimize) {
800 if (install_bpf_program(p, fp) < 0)
801 return (-1);
802 } else if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
803 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "BIOCSETF: %s",
804 pcap_strerror(errno));
805 return (-1);
806 }
807 return (0);
808 }
809
810 static int
811 pcap_set_datalink_bpf(pcap_t *p, int dlt)
812 {
813 #ifdef BIOCSDLT
814 if (ioctl(p->fd, BIOCSDLT, &dlt) == -1) {
815 (void) snprintf(p->errbuf, sizeof(p->errbuf),
816 "Cannot set DLT %d: %s", dlt, strerror(errno));
817 return (-1);
818 }
819 #endif
820 return (0);
821 }