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