xref: /freebsd/contrib/libpcap/pcap-pf.c (revision a5921bc3653e2e286715e6fe8d473ec0d02da38c)
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 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28 
29 #include <sys/types.h>
30 #include <sys/time.h>
31 #include <sys/timeb.h>
32 #include <sys/socket.h>
33 #include <sys/file.h>
34 #include <sys/ioctl.h>
35 #include <net/pfilt.h>
36 
37 struct mbuf;
38 struct rtentry;
39 #include <net/if.h>
40 
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>
50 
51 #include <ctype.h>
52 #include <errno.h>
53 #include <netdb.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 /*
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.
62  */
63 #define PCAP_DONT_INCLUDE_PCAP_BPF_H
64 #include <net/bpf.h>
65 
66 #include "pcap-int.h"
67 
68 #ifdef HAVE_OS_PROTO_H
69 #include "os-proto.h"
70 #endif
71 
72 /*
73  * FDDI packets are padded to make everything line up on a nice boundary.
74  */
75 #define       PCAP_FDDIPAD 3
76 
77 /*
78  * Private data for capturing on Ultrix and DEC OSF/1^WDigital UNIX^W^W
79  * Tru64 UNIX packetfilter devices.
80  */
81 struct pcap_pf {
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 */
88 };
89 
90 static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
91 
92 /*
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).
97  */
98 #define BUFSPACE (200 * 256)
99 
100 static int
101 pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
102 {
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;
107 #ifdef LBL_ALIGN
108 	struct enstamp stamp;
109 #endif
110 	register int pad;
111 
112  again:
113 	cc = pc->cc;
114 	if (cc == 0) {
115 		cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
116 		if (cc < 0) {
117 			if (errno == EWOULDBLOCK)
118 				return (0);
119 			if (errno == EINVAL &&
120 			    lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
121 				/*
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.
126 				 */
127 				(void)lseek(pc->fd, 0L, SEEK_SET);
128 				goto again;
129 			}
130 			snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s",
131 				pcap_strerror(errno));
132 			return (-1);
133 		}
134 		bp = pc->buffer + pc->offset;
135 	} else
136 		bp = pc->bp;
137 	/*
138 	 * Loop through each packet.
139 	 */
140 	n = 0;
141 	pad = pc->fddipad;
142 	while (cc > 0) {
143 		/*
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.
151 		 */
152 		if (pc->break_loop) {
153 			if (n == 0) {
154 				pc->break_loop = 0;
155 				return (-2);
156 			} else {
157 				pc->cc = cc;
158 				pc->bp = bp;
159 				return (n);
160 			}
161 		}
162 		if (cc < sizeof(*sp)) {
163 			snprintf(pc->errbuf, sizeof(pc->errbuf),
164 			    "pf short read (%d)", cc);
165 			return (-1);
166 		}
167 #ifdef LBL_ALIGN
168 		if ((long)bp & 3) {
169 			sp = &stamp;
170 			memcpy((char *)sp, (char *)bp, sizeof(*sp));
171 		} else
172 #endif
173 			sp = (struct enstamp *)bp;
174 		if (sp->ens_stamplen != sizeof(*sp)) {
175 			snprintf(pc->errbuf, sizeof(pc->errbuf),
176 			    "pf short stamplen (%d)",
177 			    sp->ens_stamplen);
178 			return (-1);
179 		}
180 
181 		p = bp + sp->ens_stamplen;
182 		buflen = sp->ens_count;
183 		if (buflen > pc->snapshot)
184 			buflen = pc->snapshot;
185 
186 		/* Calculate inc before possible pad update */
187 		inc = ENALIGN(buflen + sp->ens_stamplen);
188 		cc -= inc;
189 		bp += inc;
190 		pf->TotPkts++;
191 		pf->TotDrops += sp->ens_dropped;
192 		pf->TotMissed = sp->ens_ifoverflows;
193 		if (pf->OrigMissed < 0)
194 			pf->OrigMissed = pf->TotMissed;
195 
196 		/*
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.
200 		 *
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.
206 		 */
207 		if (pf->filtering_in_kernel ||
208 		    bpf_filter(pc->fcode.bf_insns, p, sp->ens_count, buflen)) {
209 			struct pcap_pkthdr h;
210 			pf->TotAccepted++;
211 			h.ts = sp->ens_tstamp;
212 			h.len = sp->ens_count - pad;
213 			p += pad;
214 			buflen -= pad;
215 			h.caplen = buflen;
216 			(*callback)(user, &h, p);
217 			if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
218 				pc->cc = cc;
219 				pc->bp = bp;
220 				return (n);
221 			}
222 		}
223 	}
224 	pc->cc = 0;
225 	return (n);
226 }
227 
228 static int
229 pcap_inject_pf(pcap_t *p, const void *buf, size_t size)
230 {
231 	int ret;
232 
233 	ret = write(p->fd, buf, size);
234 	if (ret == -1) {
235 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
236 		    pcap_strerror(errno));
237 		return (-1);
238 	}
239 	return (ret);
240 }
241 
242 static int
243 pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
244 {
245 	struct pcap_pf *pf = p->priv;
246 
247 	/*
248 	 * If packet filtering is being done in the kernel:
249 	 *
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....)
258 	 *
259 	 *	"ps_drop" counts packets that passed the kernel filter
260 	 *	(if any) but were dropped because the input queue was
261 	 *	full.
262 	 *
263 	 *	"ps_ifdrop" counts packets dropped by the network
264 	 *	inteface (regardless of whether they would have passed
265 	 *	the input filter, of course).
266 	 *
267 	 * If packet filtering is not being done in the kernel:
268 	 *
269 	 *	"ps_recv" counts only packets that passed the filter.
270 	 *
271 	 *	"ps_drop" counts packets that were dropped because the
272 	 *	input queue was full, regardless of whether they passed
273 	 *	the userland filter.
274 	 *
275 	 *	"ps_ifdrop" counts packets dropped by the network
276 	 *	inteface (regardless of whether they would have passed
277 	 *	the input filter, of course).
278 	 *
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.
282 	 */
283 	ps->ps_recv = pf->TotAccepted;
284 	ps->ps_drop = pf->TotDrops;
285 	ps->ps_ifdrop = pf->TotMissed - pf->OrigMissed;
286 	return (0);
287 }
288 
289 /*
290  * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
291  * don't get DLT_DOCSIS defined.
292  */
293 #ifndef DLT_DOCSIS
294 #define DLT_DOCSIS	143
295 #endif
296 
297 static int
298 pcap_activate_pf(pcap_t *p)
299 {
300 	struct pcap_pf *pf = p->priv;
301 	short enmode;
302 	int backlog = -1;	/* request the most */
303 	struct enfilter Filter;
304 	struct endevp devparams;
305 
306 	/*
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.
312 	 *
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
318 	 * time.
319 	 *
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.
324 	 */
325 	p->fd = pfopen(p->opt.source, O_RDWR);
326 	if (p->fd == -1 && errno == EACCES)
327 		p->fd = pfopen(p->opt.source, O_RDONLY);
328 	if (p->fd < 0) {
329 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\
330 your system may not be properly configured; see the packetfilter(4) man page\n",
331 			p->opt.source, pcap_strerror(errno));
332 		goto bad;
333 	}
334 	pf->OrigMissed = -1;
335 	enmode = ENTSTAMP|ENNONEXCL;
336 	if (!p->opt.immediate)
337 		enmode |= ENBATCH;
338 	if (p->opt.promisc)
339 		enmode |= ENPROMISC;
340 	if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
341 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s",
342 		    pcap_strerror(errno));
343 		goto bad;
344 	}
345 #ifdef	ENCOPYALL
346 	/* Try to set COPYALL mode so that we see packets to ourself */
347 	enmode = ENCOPYALL;
348 	(void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
349 #endif
350 	/* set the backlog */
351 	if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
352 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s",
353 		    pcap_strerror(errno));
354 		goto bad;
355 	}
356 	/* discover interface type */
357 	if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
358 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s",
359 		    pcap_strerror(errno));
360 		goto bad;
361 	}
362 	/* HACK: to compile prior to Ultrix 4.2 */
363 #ifndef	ENDT_FDDI
364 #define	ENDT_FDDI	4
365 #endif
366 	switch (devparams.end_dev_type) {
367 
368 	case ENDT_10MB:
369 		p->linktype = DLT_EN10MB;
370 		p->offset = 2;
371 		/*
372 		 * This is (presumably) a real Ethernet capture; give it a
373 		 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
374 		 * that an application can let you choose it, in case you're
375 		 * capturing DOCSIS traffic that a Cisco Cable Modem
376 		 * Termination System is putting out onto an Ethernet (it
377 		 * doesn't put an Ethernet header onto the wire, it puts raw
378 		 * DOCSIS frames out on the wire inside the low-level
379 		 * Ethernet framing).
380 		 */
381 		p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
382 		/*
383 		 * If that fails, just leave the list empty.
384 		 */
385 		if (p->dlt_list != NULL) {
386 			p->dlt_list[0] = DLT_EN10MB;
387 			p->dlt_list[1] = DLT_DOCSIS;
388 			p->dlt_count = 2;
389 		}
390 		break;
391 
392 	case ENDT_FDDI:
393 		p->linktype = DLT_FDDI;
394 		break;
395 
396 #ifdef ENDT_SLIP
397 	case ENDT_SLIP:
398 		p->linktype = DLT_SLIP;
399 		break;
400 #endif
401 
402 #ifdef ENDT_PPP
403 	case ENDT_PPP:
404 		p->linktype = DLT_PPP;
405 		break;
406 #endif
407 
408 #ifdef ENDT_LOOPBACK
409 	case ENDT_LOOPBACK:
410 		/*
411 		 * It appears to use Ethernet framing, at least on
412 		 * Digital UNIX 4.0.
413 		 */
414 		p->linktype = DLT_EN10MB;
415 		p->offset = 2;
416 		break;
417 #endif
418 
419 #ifdef ENDT_TRN
420 	case ENDT_TRN:
421 		p->linktype = DLT_IEEE802;
422 		break;
423 #endif
424 
425 	default:
426 		/*
427 		 * XXX - what about ENDT_IEEE802?  The pfilt.h header
428 		 * file calls this "IEEE 802 networks (non-Ethernet)",
429 		 * but that doesn't specify a specific link layer type;
430 		 * it could be 802.4, or 802.5 (except that 802.5 is
431 		 * ENDT_TRN), or 802.6, or 802.11, or....  That's why
432 		 * DLT_IEEE802 was hijacked to mean Token Ring in various
433 		 * BSDs, and why we went along with that hijacking.
434 		 *
435 		 * XXX - what about ENDT_HDLC and ENDT_NULL?
436 		 * Presumably, as ENDT_OTHER is just "Miscellaneous
437 		 * framing", there's not much we can do, as that
438 		 * doesn't specify a particular type of header.
439 		 */
440 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
441 		    "unknown data-link type %u", devparams.end_dev_type);
442 		goto bad;
443 	}
444 	/* set truncation */
445 	if (p->linktype == DLT_FDDI) {
446 		p->fddipad = PCAP_FDDIPAD;
447 
448 		/* packetfilter includes the padding in the snapshot */
449 		p->snapshot += PCAP_FDDIPAD;
450 	} else
451 		p->fddipad = 0;
452 	if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&p->snapshot) < 0) {
453 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s",
454 		    pcap_strerror(errno));
455 		goto bad;
456 	}
457 	/* accept all packets */
458 	memset(&Filter, 0, sizeof(Filter));
459 	Filter.enf_Priority = 37;	/* anything > 2 */
460 	Filter.enf_FilterLen = 0;	/* means "always true" */
461 	if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
462 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s",
463 		    pcap_strerror(errno));
464 		goto bad;
465 	}
466 
467 	if (p->opt.timeout != 0) {
468 		struct timeval timeout;
469 		timeout.tv_sec = p->opt.timeout / 1000;
470 		timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
471 		if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
472 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s",
473 				pcap_strerror(errno));
474 			goto bad;
475 		}
476 	}
477 
478 	p->bufsize = BUFSPACE;
479 	p->buffer = (u_char*)malloc(p->bufsize + p->offset);
480 	if (p->buffer == NULL) {
481 		strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
482 		goto bad;
483 	}
484 
485 	/*
486 	 * "select()" and "poll()" work on packetfilter devices.
487 	 */
488 	p->selectable_fd = p->fd;
489 
490 	p->read_op = pcap_read_pf;
491 	p->inject_op = pcap_inject_pf;
492 	p->setfilter_op = pcap_setfilter_pf;
493 	p->setdirection_op = NULL;	/* Not implemented. */
494 	p->set_datalink_op = NULL;	/* can't change data link type */
495 	p->getnonblock_op = pcap_getnonblock_fd;
496 	p->setnonblock_op = pcap_setnonblock_fd;
497 	p->stats_op = pcap_stats_pf;
498 
499 	return (0);
500  bad:
501 	pcap_cleanup_live_common(p);
502 	return (PCAP_ERROR);
503 }
504 
505 pcap_t *
506 pcap_create_interface(const char *device, char *ebuf)
507 {
508 	pcap_t *p;
509 
510 	p = pcap_create_common(device, ebuf, sizeof (struct pcap_pf));
511 	if (p == NULL)
512 		return (NULL);
513 
514 	p->activate_op = pcap_activate_pf;
515 	return (p);
516 }
517 
518 int
519 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
520 {
521 	return (0);
522 }
523 
524 static int
525 pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
526 {
527 	struct pcap_pf *pf = p->priv;
528 	struct bpf_version bv;
529 
530 	/*
531 	 * See if BIOCVERSION works.  If not, we assume the kernel doesn't
532 	 * support BPF-style filters (it's not documented in the bpf(7)
533 	 * or packetfiler(7) man pages, but the code used to fail if
534 	 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
535 	 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
536 	 * there, at least).
537 	 */
538 	if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) {
539 		/*
540 		 * OK, we have the version of the BPF interpreter;
541 		 * is it the same major version as us, and the same
542 		 * or better minor version?
543 		 */
544 		if (bv.bv_major == BPF_MAJOR_VERSION &&
545 		    bv.bv_minor >= BPF_MINOR_VERSION) {
546 			/*
547 			 * Yes.  Try to install the filter.
548 			 */
549 			if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
550 				snprintf(p->errbuf, sizeof(p->errbuf),
551 				    "BIOCSETF: %s", pcap_strerror(errno));
552 				return (-1);
553 			}
554 
555 			/*
556 			 * OK, that succeeded.  We're doing filtering in
557 			 * the kernel.  (We assume we don't have a
558 			 * userland filter installed - that'd require
559 			 * a previous version check to have failed but
560 			 * this one to succeed.)
561 			 *
562 			 * XXX - this message should be supplied to the
563 			 * application as a warning of some sort,
564 			 * except that if it's a GUI application, it's
565 			 * not clear that it should be displayed in
566 			 * a window to annoy the user.
567 			 */
568 			fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
569 			pf->filtering_in_kernel = 1;
570 
571 			/*
572 			 * Discard any previously-received packets,
573 			 * as they might have passed whatever filter
574 			 * was formerly in effect, but might not pass
575 			 * this filter (BIOCSETF discards packets buffered
576 			 * in the kernel, so you can lose packets in any
577 			 * case).
578 			 */
579 			p->cc = 0;
580 			return (0);
581 		}
582 
583 		/*
584 		 * We can't use the kernel's BPF interpreter; don't give
585 		 * up, just log a message and be inefficient.
586 		 *
587 		 * XXX - this should really be supplied to the application
588 		 * as a warning of some sort.
589 		 */
590 		fprintf(stderr,
591 	    "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
592 		    BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
593 		    bv.bv_major, bv.bv_minor);
594 	}
595 
596 	/*
597 	 * We couldn't do filtering in the kernel; do it in userland.
598 	 */
599 	if (install_bpf_program(p, fp) < 0)
600 		return (-1);
601 
602 	/*
603 	 * XXX - this message should be supplied by the application as
604 	 * a warning of some sort.
605 	 */
606 	fprintf(stderr, "tcpdump: Filtering in user process\n");
607 	pf->filtering_in_kernel = 0;
608 	return (0);
609 }
610