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