2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * packet filter subroutines for tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
29 #include <sys/types.h>
31 #include <sys/timeb.h>
32 #include <sys/socket.h>
34 #include <sys/ioctl.h>
35 #include <net/pfilt.h>
41 #include <netinet/in.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/ip.h>
44 #include <netinet/if_ether.h>
45 #include <netinet/ip_var.h>
46 #include <netinet/udp.h>
47 #include <netinet/udp_var.h>
48 #include <netinet/tcp.h>
49 #include <netinet/tcpip.h>
60 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
61 * native OS version, as we need various BPF ioctls from it.
63 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
68 #ifdef HAVE_OS_PROTO_H
73 * FDDI packets are padded to make everything line up on a nice boundary.
75 #define PCAP_FDDIPAD 3
78 * Private data for capturing on Ultrix and DEC OSF/1^WDigital UNIX^W^W
79 * Tru64 UNIX packetfilter devices.
82 int filtering_in_kernel; /* using kernel filter */
83 u_long TotPkts; /* can't oflow for 79 hrs on ether */
84 u_long TotAccepted; /* count accepted by filter */
85 u_long TotDrops; /* count of dropped packets */
86 long TotMissed; /* missed by i/f during this run */
87 long OrigMissed; /* missed by i/f before this run */
90 static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
93 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump
94 * applications aren't going to need more than 200 bytes of packet header
95 * and the read shouldn't return more packets than packetfilter's internal
96 * queue limit (bounded at 256).
98 #define BUFSPACE (200 * 256)
101 pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
103 struct pcap_pf *pf = pc->priv;
104 register u_char *p, *bp;
105 register int cc, n, buflen, inc;
106 register struct enstamp *sp;
108 struct enstamp stamp;
115 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
117 if (errno == EWOULDBLOCK)
119 if (errno == EINVAL &&
120 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
122 * Due to a kernel bug, after 2^31 bytes,
123 * the kernel file offset overflows and
124 * read fails with EINVAL. The lseek()
125 * to 0 will fix things.
127 (void)lseek(pc->fd, 0L, SEEK_SET);
130 pcap_snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s",
131 pcap_strerror(errno));
134 bp = (u_char *)pc->buffer + pc->offset;
138 * Loop through each packet.
144 * Has "pcap_breakloop()" been called?
145 * If so, return immediately - if we haven't read any
146 * packets, clear the flag and return -2 to indicate
147 * that we were told to break out of the loop, otherwise
148 * leave the flag set, so that the *next* call will break
149 * out of the loop without having read any packets, and
150 * return the number of packets we've processed so far.
152 if (pc->break_loop) {
162 if (cc < sizeof(*sp)) {
163 pcap_snprintf(pc->errbuf, sizeof(pc->errbuf),
164 "pf short read (%d)", cc);
170 memcpy((char *)sp, (char *)bp, sizeof(*sp));
173 sp = (struct enstamp *)bp;
174 if (sp->ens_stamplen != sizeof(*sp)) {
175 pcap_snprintf(pc->errbuf, sizeof(pc->errbuf),
176 "pf short stamplen (%d)",
181 p = bp + sp->ens_stamplen;
182 buflen = sp->ens_count;
183 if (buflen > pc->snapshot)
184 buflen = pc->snapshot;
186 /* Calculate inc before possible pad update */
187 inc = ENALIGN(buflen + sp->ens_stamplen);
191 pf->TotDrops += sp->ens_dropped;
192 pf->TotMissed = sp->ens_ifoverflows;
193 if (pf->OrigMissed < 0)
194 pf->OrigMissed = pf->TotMissed;
197 * Short-circuit evaluation: if using BPF filter
198 * in kernel, no need to do it now - we already know
199 * the packet passed the filter.
201 * Note: the filter code was generated assuming
202 * that pc->fddipad was the amount of padding
203 * before the header, as that's what's required
204 * in the kernel, so we run the filter before
205 * skipping that padding.
207 if (pf->filtering_in_kernel ||
208 bpf_filter(pc->fcode.bf_insns, p, sp->ens_count, buflen)) {
209 struct pcap_pkthdr h;
211 h.ts = sp->ens_tstamp;
212 h.len = sp->ens_count - pad;
216 (*callback)(user, &h, p);
217 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
229 pcap_inject_pf(pcap_t *p, const void *buf, size_t size)
233 ret = write(p->fd, buf, size);
235 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
236 pcap_strerror(errno));
243 pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
245 struct pcap_pf *pf = p->priv;
248 * If packet filtering is being done in the kernel:
250 * "ps_recv" counts only packets that passed the filter.
251 * This does not include packets dropped because we
252 * ran out of buffer space. (XXX - perhaps it should,
253 * by adding "ps_drop" to "ps_recv", for compatibility
254 * with some other platforms. On the other hand, on
255 * some platforms "ps_recv" counts only packets that
256 * passed the filter, and on others it counts packets
257 * that didn't pass the filter....)
259 * "ps_drop" counts packets that passed the kernel filter
260 * (if any) but were dropped because the input queue was
263 * "ps_ifdrop" counts packets dropped by the network
264 * inteface (regardless of whether they would have passed
265 * the input filter, of course).
267 * If packet filtering is not being done in the kernel:
269 * "ps_recv" counts only packets that passed the filter.
271 * "ps_drop" counts packets that were dropped because the
272 * input queue was full, regardless of whether they passed
273 * the userland filter.
275 * "ps_ifdrop" counts packets dropped by the network
276 * inteface (regardless of whether they would have passed
277 * the input filter, of course).
279 * These statistics don't include packets not yet read from
280 * the kernel by libpcap, but they may include packets not
281 * yet read from libpcap by the application.
283 ps->ps_recv = pf->TotAccepted;
284 ps->ps_drop = pf->TotDrops;
285 ps->ps_ifdrop = pf->TotMissed - pf->OrigMissed;
290 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
291 * don't get DLT_DOCSIS defined.
294 #define DLT_DOCSIS 143
298 pcap_activate_pf(pcap_t *p)
300 struct pcap_pf *pf = p->priv;
302 int backlog = -1; /* request the most */
303 struct enfilter Filter;
304 struct endevp devparams;
307 * Initially try a read/write open (to allow the inject
308 * method to work). If that fails due to permission
309 * issues, fall back to read-only. This allows a
310 * non-root user to be granted specific access to pcap
311 * capabilities via file permissions.
313 * XXX - we should have an API that has a flag that
314 * controls whether to open read-only or read-write,
315 * so that denial of permission to send (or inability
316 * to send, if sending packets isn't supported on
317 * the device in question) can be indicated at open
320 * XXX - we assume here that "pfopen()" does not, in fact, modify
321 * its argument, even though it takes a "char *" rather than a
322 * "const char *" as its first argument. That appears to be
323 * the case, at least on Digital UNIX 4.0.
325 * XXX - is there an error that means "no such device"? Is
326 * there one that means "that device doesn't support pf"?
328 p->fd = pfopen(p->opt.device, O_RDWR);
329 if (p->fd == -1 && errno == EACCES)
330 p->fd = pfopen(p->opt.device, O_RDONLY);
332 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\
333 your system may not be properly configured; see the packetfilter(4) man page\n",
334 p->opt.device, pcap_strerror(errno));
338 enmode = ENTSTAMP|ENNONEXCL;
339 if (!p->opt.immediate)
343 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
344 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s",
345 pcap_strerror(errno));
349 /* Try to set COPYALL mode so that we see packets to ourself */
351 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
353 /* set the backlog */
354 if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
355 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s",
356 pcap_strerror(errno));
359 /* discover interface type */
360 if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
361 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s",
362 pcap_strerror(errno));
365 /* HACK: to compile prior to Ultrix 4.2 */
369 switch (devparams.end_dev_type) {
372 p->linktype = DLT_EN10MB;
375 * This is (presumably) a real Ethernet capture; give it a
376 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
377 * that an application can let you choose it, in case you're
378 * capturing DOCSIS traffic that a Cisco Cable Modem
379 * Termination System is putting out onto an Ethernet (it
380 * doesn't put an Ethernet header onto the wire, it puts raw
381 * DOCSIS frames out on the wire inside the low-level
384 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
386 * If that fails, just leave the list empty.
388 if (p->dlt_list != NULL) {
389 p->dlt_list[0] = DLT_EN10MB;
390 p->dlt_list[1] = DLT_DOCSIS;
396 p->linktype = DLT_FDDI;
401 p->linktype = DLT_SLIP;
407 p->linktype = DLT_PPP;
414 * It appears to use Ethernet framing, at least on
417 p->linktype = DLT_EN10MB;
424 p->linktype = DLT_IEEE802;
430 * XXX - what about ENDT_IEEE802? The pfilt.h header
431 * file calls this "IEEE 802 networks (non-Ethernet)",
432 * but that doesn't specify a specific link layer type;
433 * it could be 802.4, or 802.5 (except that 802.5 is
434 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
435 * DLT_IEEE802 was hijacked to mean Token Ring in various
436 * BSDs, and why we went along with that hijacking.
438 * XXX - what about ENDT_HDLC and ENDT_NULL?
439 * Presumably, as ENDT_OTHER is just "Miscellaneous
440 * framing", there's not much we can do, as that
441 * doesn't specify a particular type of header.
443 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
444 "unknown data-link type %u", devparams.end_dev_type);
448 if (p->linktype == DLT_FDDI) {
449 p->fddipad = PCAP_FDDIPAD;
451 /* packetfilter includes the padding in the snapshot */
452 p->snapshot += PCAP_FDDIPAD;
455 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&p->snapshot) < 0) {
456 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s",
457 pcap_strerror(errno));
460 /* accept all packets */
461 memset(&Filter, 0, sizeof(Filter));
462 Filter.enf_Priority = 37; /* anything > 2 */
463 Filter.enf_FilterLen = 0; /* means "always true" */
464 if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
465 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s",
466 pcap_strerror(errno));
470 if (p->opt.timeout != 0) {
471 struct timeval timeout;
472 timeout.tv_sec = p->opt.timeout / 1000;
473 timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
474 if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
475 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s",
476 pcap_strerror(errno));
481 p->bufsize = BUFSPACE;
482 p->buffer = malloc(p->bufsize + p->offset);
483 if (p->buffer == NULL) {
484 strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
489 * "select()" and "poll()" work on packetfilter devices.
491 p->selectable_fd = p->fd;
493 p->read_op = pcap_read_pf;
494 p->inject_op = pcap_inject_pf;
495 p->setfilter_op = pcap_setfilter_pf;
496 p->setdirection_op = NULL; /* Not implemented. */
497 p->set_datalink_op = NULL; /* can't change data link type */
498 p->getnonblock_op = pcap_getnonblock_fd;
499 p->setnonblock_op = pcap_setnonblock_fd;
500 p->stats_op = pcap_stats_pf;
504 pcap_cleanup_live_common(p);
509 pcap_create_interface(const char *device _U_, char *ebuf)
513 p = pcap_create_common(ebuf, sizeof (struct pcap_pf));
517 p->activate_op = pcap_activate_pf;
522 * XXX - is there an error from pfopen() that means "no such device"?
523 * Is there one that means "that device doesn't support pf"?
526 can_be_bound(const char *name _U_)
532 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
534 return (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound));
538 pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
540 struct pcap_pf *pf = p->priv;
541 struct bpf_version bv;
544 * See if BIOCVERSION works. If not, we assume the kernel doesn't
545 * support BPF-style filters (it's not documented in the bpf(7)
546 * or packetfiler(7) man pages, but the code used to fail if
547 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
548 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
551 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) {
553 * OK, we have the version of the BPF interpreter;
554 * is it the same major version as us, and the same
555 * or better minor version?
557 if (bv.bv_major == BPF_MAJOR_VERSION &&
558 bv.bv_minor >= BPF_MINOR_VERSION) {
560 * Yes. Try to install the filter.
562 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
563 pcap_snprintf(p->errbuf, sizeof(p->errbuf),
564 "BIOCSETF: %s", pcap_strerror(errno));
569 * OK, that succeeded. We're doing filtering in
570 * the kernel. (We assume we don't have a
571 * userland filter installed - that'd require
572 * a previous version check to have failed but
573 * this one to succeed.)
575 * XXX - this message should be supplied to the
576 * application as a warning of some sort,
577 * except that if it's a GUI application, it's
578 * not clear that it should be displayed in
579 * a window to annoy the user.
581 fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
582 pf->filtering_in_kernel = 1;
585 * Discard any previously-received packets,
586 * as they might have passed whatever filter
587 * was formerly in effect, but might not pass
588 * this filter (BIOCSETF discards packets buffered
589 * in the kernel, so you can lose packets in any
597 * We can't use the kernel's BPF interpreter; don't give
598 * up, just log a message and be inefficient.
600 * XXX - this should really be supplied to the application
601 * as a warning of some sort.
604 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
605 BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
606 bv.bv_major, bv.bv_minor);
610 * We couldn't do filtering in the kernel; do it in userland.
612 if (install_bpf_program(p, fp) < 0)
616 * XXX - this message should be supplied by the application as
617 * a warning of some sort.
619 fprintf(stderr, "tcpdump: Filtering in user process\n");
620 pf->filtering_in_kernel = 0;