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