]> The Tcpdump Group git mirrors - libpcap/blob - pcap-pf.c
ddd20abd9273ab75ae652f62b2e82d08a504e74a
[libpcap] / pcap-pf.c
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
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 * packet filter subroutines for tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
23 */
24
25 #ifndef lint
26 static const char rcsid[] _U_ =
27 "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.88 2004-10-19 07:06:13 guy Exp $ (LBL)";
28 #endif
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <sys/timeb.h>
37 #include <sys/socket.h>
38 #include <sys/file.h>
39 #include <sys/ioctl.h>
40 #include <net/pfilt.h>
41
42 struct mbuf;
43 struct rtentry;
44 #include <net/if.h>
45
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/if_ether.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/udp.h>
52 #include <netinet/udp_var.h>
53 #include <netinet/tcp.h>
54 #include <netinet/tcpip.h>
55
56 #include <ctype.h>
57 #include <errno.h>
58 #include <netdb.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 /*
65 * Make "pcap.h" not include "pcap-bpf.h"; we are going to include the
66 * native OS version, as we need various BPF ioctls from it.
67 */
68 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
69 #include <net/bpf.h>
70
71 #include "pcap-int.h"
72
73 #ifdef HAVE_OS_PROTO_H
74 #include "os-proto.h"
75 #endif
76
77 static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
78
79 /*
80 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump
81 * applications aren't going to need more than 200 bytes of packet header
82 * and the read shouldn't return more packets than packetfilter's internal
83 * queue limit (bounded at 256).
84 */
85 #define BUFSPACE (200 * 256)
86
87 static int
88 pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
89 {
90 register u_char *p, *bp;
91 struct bpf_insn *fcode;
92 register int cc, n, buflen, inc;
93 register struct enstamp *sp;
94 #ifdef LBL_ALIGN
95 struct enstamp stamp;
96 #endif
97 #ifdef PCAP_FDDIPAD
98 register int pad;
99 #endif
100
101 fcode = pc->md.use_bpf ? NULL : pc->fcode.bf_insns;
102 again:
103 cc = pc->cc;
104 if (cc == 0) {
105 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
106 if (cc < 0) {
107 if (errno == EWOULDBLOCK)
108 return (0);
109 if (errno == EINVAL &&
110 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
111 /*
112 * Due to a kernel bug, after 2^31 bytes,
113 * the kernel file offset overflows and
114 * read fails with EINVAL. The lseek()
115 * to 0 will fix things.
116 */
117 (void)lseek(pc->fd, 0L, SEEK_SET);
118 goto again;
119 }
120 snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s",
121 pcap_strerror(errno));
122 return (-1);
123 }
124 bp = pc->buffer + pc->offset;
125 } else
126 bp = pc->bp;
127 /*
128 * Loop through each packet.
129 */
130 n = 0;
131 #ifdef PCAP_FDDIPAD
132 if (pc->linktype == DLT_FDDI)
133 pad = pcap_fddipad;
134 else
135 pad = 0;
136 #endif
137 while (cc > 0) {
138 /*
139 * Has "pcap_breakloop()" been called?
140 * If so, return immediately - if we haven't read any
141 * packets, clear the flag and return -2 to indicate
142 * that we were told to break out of the loop, otherwise
143 * leave the flag set, so that the *next* call will break
144 * out of the loop without having read any packets, and
145 * return the number of packets we've processed so far.
146 */
147 if (pc->break_loop) {
148 if (n == 0) {
149 pc->break_loop = 0;
150 return (-2);
151 } else {
152 pc->cc = cc;
153 pc->bp = bp;
154 return (n);
155 }
156 }
157 if (cc < sizeof(*sp)) {
158 snprintf(pc->errbuf, sizeof(pc->errbuf),
159 "pf short read (%d)", cc);
160 return (-1);
161 }
162 #ifdef LBL_ALIGN
163 if ((long)bp & 3) {
164 sp = &stamp;
165 memcpy((char *)sp, (char *)bp, sizeof(*sp));
166 } else
167 #endif
168 sp = (struct enstamp *)bp;
169 if (sp->ens_stamplen != sizeof(*sp)) {
170 snprintf(pc->errbuf, sizeof(pc->errbuf),
171 "pf short stamplen (%d)",
172 sp->ens_stamplen);
173 return (-1);
174 }
175
176 p = bp + sp->ens_stamplen;
177 buflen = sp->ens_count;
178 if (buflen > pc->snapshot)
179 buflen = pc->snapshot;
180
181 /* Calculate inc before possible pad update */
182 inc = ENALIGN(buflen + sp->ens_stamplen);
183 cc -= inc;
184 bp += inc;
185 #ifdef PCAP_FDDIPAD
186 p += pad;
187 buflen -= pad;
188 #endif
189 pc->md.TotPkts++;
190 pc->md.TotDrops += sp->ens_dropped;
191 pc->md.TotMissed = sp->ens_ifoverflows;
192 if (pc->md.OrigMissed < 0)
193 pc->md.OrigMissed = pc->md.TotMissed;
194
195 /*
196 * Short-circuit evaluation: if using BPF filter
197 * in kernel, no need to do it now.
198 */
199 if (fcode == NULL ||
200 bpf_filter(fcode, p, sp->ens_count, buflen)) {
201 struct pcap_pkthdr h;
202 pc->md.TotAccepted++;
203 h.ts = sp->ens_tstamp;
204 #ifdef PCAP_FDDIPAD
205 h.len = sp->ens_count - pad;
206 #else
207 h.len = sp->ens_count;
208 #endif
209 h.caplen = buflen;
210 (*callback)(user, &h, p);
211 if (++n >= cnt && cnt > 0) {
212 pc->cc = cc;
213 pc->bp = bp;
214 return (n);
215 }
216 }
217 }
218 pc->cc = 0;
219 return (n);
220 }
221
222 static int
223 pcap_inject_pf(pcap_t *p, const void *buf, size_t size)
224 {
225 int ret;
226
227 ret = write(p->fd, buf, size);
228 if (ret == -1) {
229 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
230 pcap_strerror(errno));
231 return (-1);
232 }
233 return (ret);
234 }
235
236 static int
237 pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
238 {
239
240 /*
241 * If packet filtering is being done in the kernel:
242 *
243 * "ps_recv" counts only packets that passed the filter.
244 * This does not include packets dropped because we
245 * ran out of buffer space. (XXX - perhaps it should,
246 * by adding "ps_drop" to "ps_recv", for compatibility
247 * with some other platforms. On the other hand, on
248 * some platforms "ps_recv" counts only packets that
249 * passed the filter, and on others it counts packets
250 * that didn't pass the filter....)
251 *
252 * "ps_drop" counts packets that passed the kernel filter
253 * (if any) but were dropped because the input queue was
254 * full.
255 *
256 * "ps_ifdrop" counts packets dropped by the network
257 * inteface (regardless of whether they would have passed
258 * the input filter, of course).
259 *
260 * If packet filtering is not being done in the kernel:
261 *
262 * "ps_recv" counts only packets that passed the filter.
263 *
264 * "ps_drop" counts packets that were dropped because the
265 * input queue was full, regardless of whether they passed
266 * the userland filter.
267 *
268 * "ps_ifdrop" counts packets dropped by the network
269 * inteface (regardless of whether they would have passed
270 * the input filter, of course).
271 *
272 * These statistics don't include packets not yet read from
273 * the kernel by libpcap, but they may include packets not
274 * yet read from libpcap by the application.
275 */
276 ps->ps_recv = p->md.TotAccepted;
277 ps->ps_drop = p->md.TotDrops;
278 ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed;
279 return (0);
280 }
281
282 /*
283 * We include the OS's <net/bpf.h>, not our "pcap-bpf.h", so we probably
284 * don't get DLT_DOCSIS defined.
285 */
286 #ifndef DLT_DOCSIS
287 #define DLT_DOCSIS 143
288 #endif
289
290 pcap_t *
291 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
292 char *ebuf)
293 {
294 pcap_t *p;
295 short enmode;
296 int backlog = -1; /* request the most */
297 struct enfilter Filter;
298 struct endevp devparams;
299
300 p = (pcap_t *)malloc(sizeof(*p));
301 if (p == NULL) {
302 snprintf(ebuf, PCAP_ERRBUF_SIZE,
303 "pcap_open_live: %s", pcap_strerror(errno));
304 return (0);
305 }
306 memset(p, 0, sizeof(*p));
307 /*
308 * Initially try a read/write open (to allow the inject
309 * method to work). If that fails due to permission
310 * issues, fall back to read-only. This allows a
311 * non-root user to be granted specific access to pcap
312 * capabilities via file permissions.
313 *
314 * XXX - we should have an API that has a flag that
315 * controls whether to open read-only or read-write,
316 * so that denial of permission to send (or inability
317 * to send, if sending packets isn't supported on
318 * the device in question) can be indicated at open
319 * time.
320 *
321 * XXX - we assume here that "pfopen()" does not, in fact, modify
322 * its argument, even though it takes a "char *" rather than a
323 * "const char *" as its first argument. That appears to be
324 * the case, at least on Digital UNIX 4.0.
325 */
326 p->fd = pfopen(device, O_RDWR);
327 if (p->fd == -1 && errno == EACCES)
328 p->fd = pfopen(device, O_RDONLY);
329 if (p->fd < 0) {
330 snprintf(ebuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\
331 your system may not be properly configured; see the packetfilter(4) man page\n",
332 device, pcap_strerror(errno));
333 goto bad;
334 }
335 p->md.OrigMissed = -1;
336 enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
337 if (promisc)
338 enmode |= ENPROMISC;
339 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
340 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s",
341 pcap_strerror(errno));
342 goto bad;
343 }
344 #ifdef ENCOPYALL
345 /* Try to set COPYALL mode so that we see packets to ourself */
346 enmode = ENCOPYALL;
347 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
348 #endif
349 /* set the backlog */
350 if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
351 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s",
352 pcap_strerror(errno));
353 goto bad;
354 }
355 /* discover interface type */
356 if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
357 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s",
358 pcap_strerror(errno));
359 goto bad;
360 }
361 /* HACK: to compile prior to Ultrix 4.2 */
362 #ifndef ENDT_FDDI
363 #define ENDT_FDDI 4
364 #endif
365 switch (devparams.end_dev_type) {
366
367 case ENDT_10MB:
368 p->linktype = DLT_EN10MB;
369 p->offset = 2;
370 /*
371 * This is (presumably) a real Ethernet capture; give it a
372 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
373 * that an application can let you choose it, in case you're
374 * capturing DOCSIS traffic that a Cisco Cable Modem
375 * Termination System is putting out onto an Ethernet (it
376 * doesn't put an Ethernet header onto the wire, it puts raw
377 * DOCSIS frames out on the wire inside the low-level
378 * Ethernet framing).
379 */
380 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
381 /*
382 * If that fails, just leave the list empty.
383 */
384 if (p->dlt_list != NULL) {
385 p->dlt_list[0] = DLT_EN10MB;
386 p->dlt_list[1] = DLT_DOCSIS;
387 p->dlt_count = 2;
388 }
389 break;
390
391 case ENDT_FDDI:
392 p->linktype = DLT_FDDI;
393 break;
394
395 #ifdef ENDT_SLIP
396 case ENDT_SLIP:
397 p->linktype = DLT_SLIP;
398 break;
399 #endif
400
401 #ifdef ENDT_PPP
402 case ENDT_PPP:
403 p->linktype = DLT_PPP;
404 break;
405 #endif
406
407 #ifdef ENDT_LOOPBACK
408 case ENDT_LOOPBACK:
409 /*
410 * It appears to use Ethernet framing, at least on
411 * Digital UNIX 4.0.
412 */
413 p->linktype = DLT_EN10MB;
414 p->offset = 2;
415 break;
416 #endif
417
418 #ifdef ENDT_TRN
419 case ENDT_TRN:
420 p->linktype = DLT_IEEE802;
421 break;
422 #endif
423
424 default:
425 /*
426 * XXX - what about ENDT_IEEE802? The pfilt.h header
427 * file calls this "IEEE 802 networks (non-Ethernet)",
428 * but that doesn't specify a specific link layer type;
429 * it could be 802.4, or 802.5 (except that 802.5 is
430 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
431 * DLT_IEEE802 was hijacked to mean Token Ring in various
432 * BSDs, and why we went along with that hijacking.
433 *
434 * XXX - what about ENDT_HDLC and ENDT_NULL?
435 * Presumably, as ENDT_OTHER is just "Miscellaneous
436 * framing", there's not much we can do, as that
437 * doesn't specify a particular type of header.
438 */
439 snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown data-link type %u",
440 devparams.end_dev_type);
441 goto bad;
442 }
443 /* set truncation */
444 #ifdef PCAP_FDDIPAD
445 if (p->linktype == DLT_FDDI)
446 /* packetfilter includes the padding in the snapshot */
447 snaplen += pcap_fddipad;
448 #endif
449 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) {
450 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s",
451 pcap_strerror(errno));
452 goto bad;
453 }
454 p->snapshot = snaplen;
455 /* accept all packets */
456 memset(&Filter, 0, sizeof(Filter));
457 Filter.enf_Priority = 37; /* anything > 2 */
458 Filter.enf_FilterLen = 0; /* means "always true" */
459 if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
460 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s",
461 pcap_strerror(errno));
462 goto bad;
463 }
464
465 if (to_ms != 0) {
466 struct timeval timeout;
467 timeout.tv_sec = to_ms / 1000;
468 timeout.tv_usec = (to_ms * 1000) % 1000000;
469 if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
470 snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s",
471 pcap_strerror(errno));
472 goto bad;
473 }
474 }
475
476 p->bufsize = BUFSPACE;
477 p->buffer = (u_char*)malloc(p->bufsize + p->offset);
478 if (p->buffer == NULL) {
479 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
480 goto bad;
481 }
482
483 /*
484 * "select()" and "poll()" work on packetfilter devices.
485 */
486 p->selectable_fd = p->fd;
487
488 p->read_op = pcap_read_pf;
489 p->inject_op = pcap_inject_pf;
490 p->setfilter_op = pcap_setfilter_pf;
491 p->set_datalink_op = NULL; /* can't change data link type */
492 p->getnonblock_op = pcap_getnonblock_fd;
493 p->setnonblock_op = pcap_setnonblock_fd;
494 p->stats_op = pcap_stats_pf;
495 p->close_op = pcap_close_common;
496
497 return (p);
498 bad:
499 if (p->fd >= 0)
500 close(p->fd);
501 /*
502 * Get rid of any link-layer type list we allocated.
503 */
504 if (p->dlt_list != NULL)
505 free(p->dlt_list);
506 free(p);
507 return (NULL);
508 }
509
510 int
511 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
512 {
513 return (0);
514 }
515
516 static int
517 pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
518 {
519 struct bpf_version bv;
520
521 /*
522 * See if BIOCVERSION works. If not, we assume the kernel doesn't
523 * support BPF-style filters (it's not documented in the bpf(7)
524 * or packetfiler(7) man pages, but the code used to fail if
525 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
526 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
527 * there, at least).
528 */
529 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) {
530 /*
531 * OK, we have the version of the BPF interpreter;
532 * is it the same major version as us, and the same
533 * or better minor version?
534 */
535 if (bv.bv_major == BPF_MAJOR_VERSION &&
536 bv.bv_minor >= BPF_MINOR_VERSION) {
537 /*
538 * Yes. Try to install the filter.
539 */
540 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
541 snprintf(p->errbuf, sizeof(p->errbuf),
542 "BIOCSETF: %s", pcap_strerror(errno));
543 return (-1);
544 }
545
546 /*
547 * OK, that succeeded. We're doing filtering in
548 * the kernel. (We assume we don't have a
549 * userland filter installed - that'd require
550 * a previous version check to have failed but
551 * this one to succeed.)
552 *
553 * XXX - this message should be supplied to the
554 * application as a warning of some sort,
555 * except that if it's a GUI application, it's
556 * not clear that it should be displayed in
557 * a window to annoy the user.
558 */
559 fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
560 p->md.use_bpf = 1;
561 return (0);
562 }
563
564 /*
565 * We can't use the kernel's BPF interpreter; don't give
566 * up, just log a message and be inefficient.
567 *
568 * XXX - this should really be supplied to the application
569 * as a warning of some sort.
570 */
571 fprintf(stderr,
572 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
573 BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
574 bv.bv_major, bv.bv_minor);
575 }
576
577 /*
578 * We couldn't do filtering in the kernel; do it in userland.
579 */
580 if (install_bpf_program(p, fp) < 0)
581 return (-1);
582
583 /*
584 * XXX - this message should be supplied by the application as
585 * a warning of some sort.
586 */
587 fprintf(stderr, "tcpdump: Filtering in user process\n");
588 p->md.use_bpf = 0;
589 return (0);
590 }