xref: /freebsd/contrib/libpcap/pcap-pf.c (revision 17d6c636720d00f77e5d098daf4c278f89d84f7b)
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[] =
27     "@(#) $Header: /tcpdump/master/libpcap/pcap-pf.c,v 1.62 2000/10/28 00:01:30 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 #include "pcap-int.h"
65 
66 #ifdef HAVE_OS_PROTO_H
67 #include "os-proto.h"
68 #endif
69 
70 /*
71  * BUFSPACE is the size in bytes of the packet read buffer.  Most tcpdump
72  * applications aren't going to need more than 200 bytes of packet header
73  * and the read shouldn't return more packets than packetfilter's internal
74  * queue limit (bounded at 256).
75  */
76 #define BUFSPACE (200 * 256)
77 
78 int
79 pcap_read(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
80 {
81 	register u_char *p, *bp;
82 	struct bpf_insn *fcode;
83 	register int cc, n, buflen, inc;
84 	register struct enstamp *sp;
85 #ifdef LBL_ALIGN
86 	struct enstamp stamp;
87 #endif
88 #ifdef PCAP_FDDIPAD
89 	register int pad;
90 #endif
91 
92 	fcode = pc->md.use_bpf ? NULL : pc->fcode.bf_insns;
93  again:
94 	cc = pc->cc;
95 	if (cc == 0) {
96 		cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
97 		if (cc < 0) {
98 			if (errno == EWOULDBLOCK)
99 				return (0);
100 			if (errno == EINVAL &&
101 			    lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
102 				/*
103 				 * Due to a kernel bug, after 2^31 bytes,
104 				 * the kernel file offset overflows and
105 				 * read fails with EINVAL. The lseek()
106 				 * to 0 will fix things.
107 				 */
108 				(void)lseek(pc->fd, 0L, SEEK_SET);
109 				goto again;
110 			}
111 			snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s",
112 				pcap_strerror(errno));
113 			return (-1);
114 		}
115 		bp = pc->buffer + pc->offset;
116 	} else
117 		bp = pc->bp;
118 	/*
119 	 * Loop through each packet.
120 	 */
121 	n = 0;
122 #ifdef PCAP_FDDIPAD
123 	if (pc->linktype == DLT_FDDI)
124 		pad = pcap_fddipad;
125 	else
126 		pad = 0;
127 #endif
128 	while (cc > 0) {
129 		if (cc < sizeof(*sp)) {
130 			snprintf(pc->errbuf, sizeof(pc->errbuf),
131 			    "pf short read (%d)", cc);
132 			return (-1);
133 		}
134 #ifdef LBL_ALIGN
135 		if ((long)bp & 3) {
136 			sp = &stamp;
137 			memcpy((char *)sp, (char *)bp, sizeof(*sp));
138 		} else
139 #endif
140 			sp = (struct enstamp *)bp;
141 		if (sp->ens_stamplen != sizeof(*sp)) {
142 			snprintf(pc->errbuf, sizeof(pc->errbuf),
143 			    "pf short stamplen (%d)",
144 			    sp->ens_stamplen);
145 			return (-1);
146 		}
147 
148 		p = bp + sp->ens_stamplen;
149 		buflen = sp->ens_count;
150 		if (buflen > pc->snapshot)
151 			buflen = pc->snapshot;
152 
153 		/* Calculate inc before possible pad update */
154 		inc = ENALIGN(buflen + sp->ens_stamplen);
155 		cc -= inc;
156 		bp += inc;
157 #ifdef PCAP_FDDIPAD
158 		p += pad;
159 		buflen -= pad;
160 #endif
161 		pc->md.TotPkts++;
162 		pc->md.TotDrops += sp->ens_dropped;
163 		pc->md.TotMissed = sp->ens_ifoverflows;
164 		if (pc->md.OrigMissed < 0)
165 			pc->md.OrigMissed = pc->md.TotMissed;
166 
167 		/*
168 		 * Short-circuit evaluation: if using BPF filter
169 		 * in kernel, no need to do it now.
170 		 */
171 		if (fcode == NULL ||
172 		    bpf_filter(fcode, p, sp->ens_count, buflen)) {
173 			struct pcap_pkthdr h;
174 			pc->md.TotAccepted++;
175 			h.ts = sp->ens_tstamp;
176 #ifdef PCAP_FDDIPAD
177 			h.len = sp->ens_count - pad;
178 #else
179 			h.len = sp->ens_count;
180 #endif
181 			h.caplen = buflen;
182 			(*callback)(user, &h, p);
183 			if (++n >= cnt && cnt > 0) {
184 				pc->cc = cc;
185 				pc->bp = bp;
186 				return (n);
187 			}
188 		}
189 	}
190 	pc->cc = 0;
191 	return (n);
192 }
193 
194 int
195 pcap_stats(pcap_t *p, struct pcap_stat *ps)
196 {
197 
198 	ps->ps_recv = p->md.TotAccepted;
199 	ps->ps_drop = p->md.TotDrops;
200 	ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed;
201 	return (0);
202 }
203 
204 pcap_t *
205 pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
206 {
207 	pcap_t *p;
208 	short enmode;
209 	int backlog = -1;	/* request the most */
210 	struct enfilter Filter;
211 	struct endevp devparams;
212 
213 	p = (pcap_t *)malloc(sizeof(*p));
214 	if (p == NULL) {
215 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
216 		    "pcap_open_live: %s", pcap_strerror(errno));
217 		return (0);
218 	}
219 	memset(p, 0, sizeof(*p));
220 	p->fd = pfopen(device, O_RDONLY);
221 	if (p->fd < 0) {
222 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\
223 your system may not be properly configured; see \"man packetfilter(4)\"\n",
224 			device, pcap_strerror(errno));
225 		goto bad;
226 	}
227 	p->md.OrigMissed = -1;
228 	enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
229 	if (promisc)
230 		enmode |= ENPROMISC;
231 	if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
232 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s",
233 		    pcap_strerror(errno));
234 		goto bad;
235 	}
236 #ifdef	ENCOPYALL
237 	/* Try to set COPYALL mode so that we see packets to ourself */
238 	enmode = ENCOPYALL;
239 	(void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
240 #endif
241 	/* set the backlog */
242 	if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
243 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s",
244 		    pcap_strerror(errno));
245 		goto bad;
246 	}
247 	/* discover interface type */
248 	if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
249 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s",
250 		    pcap_strerror(errno));
251 		goto bad;
252 	}
253 	/* HACK: to compile prior to Ultrix 4.2 */
254 #ifndef	ENDT_FDDI
255 #define	ENDT_FDDI	4
256 #endif
257 	switch (devparams.end_dev_type) {
258 
259 	case ENDT_10MB:
260 		p->linktype = DLT_EN10MB;
261 		p->offset = 2;
262 		break;
263 
264 	case ENDT_FDDI:
265 		p->linktype = DLT_FDDI;
266 		break;
267 
268 	default:
269 		/*
270 		 * XXX
271 		 * Currently, the Ultrix packet filter supports only
272 		 * Ethernet and FDDI.  Eventually, support for SLIP and PPP
273 		 * (and possibly others: T1?) should be added.
274 		 */
275 #ifdef notdef
276 		warning(
277 		   "Packet filter data-link type %d unknown, assuming Ethernet",
278 		    devparams.end_dev_type);
279 #endif
280 		p->linktype = DLT_EN10MB;
281 		p->offset = 2;
282 		break;
283 	}
284 	/* set truncation */
285 #ifdef PCAP_FDDIPAD
286 	if (p->linktype == DLT_FDDI)
287 		/* packetfilter includes the padding in the snapshot */
288 		snaplen += pcap_fddipad;
289 #endif
290 	if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) {
291 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s",
292 		    pcap_strerror(errno));
293 		goto bad;
294 	}
295 	p->snapshot = snaplen;
296 	/* accept all packets */
297 	memset(&Filter, 0, sizeof(Filter));
298 	Filter.enf_Priority = 37;	/* anything > 2 */
299 	Filter.enf_FilterLen = 0;	/* means "always true" */
300 	if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
301 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s",
302 		    pcap_strerror(errno));
303 		goto bad;
304 	}
305 
306 	if (to_ms != 0) {
307 		struct timeval timeout;
308 		timeout.tv_sec = to_ms / 1000;
309 		timeout.tv_usec = (to_ms * 1000) % 1000000;
310 		if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
311 			snprintf(ebuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s",
312 				pcap_strerror(errno));
313 			goto bad;
314 		}
315 	}
316 	p->bufsize = BUFSPACE;
317 	p->buffer = (u_char*)malloc(p->bufsize + p->offset);
318 
319 	return (p);
320  bad:
321 	free(p);
322 	return (NULL);
323 }
324 
325 int
326 pcap_setfilter(pcap_t *p, struct bpf_program *fp)
327 {
328 	/*
329 	 * See if BIOCSETF works.  If it does, the kernel supports
330 	 * BPF-style filters, and we do not need to do post-filtering.
331 	 */
332 	p->md.use_bpf = (ioctl(p->fd, BIOCSETF, (caddr_t)fp) >= 0);
333 	if (p->md.use_bpf) {
334 		struct bpf_version bv;
335 
336 		if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) < 0) {
337 			snprintf(p->errbuf, sizeof(p->errbuf),
338 			    "BIOCVERSION: %s", pcap_strerror(errno));
339 			return (-1);
340 		}
341 		else if (bv.bv_major != BPF_MAJOR_VERSION ||
342 			 bv.bv_minor < BPF_MINOR_VERSION) {
343 			fprintf(stderr,
344 		"requires bpf language %d.%d or higher; kernel is %d.%d",
345 				BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
346 			      bv.bv_major, bv.bv_minor);
347 			/* don't give up, just be inefficient */
348 			p->md.use_bpf = 0;
349 		}
350 	} else {
351 		if (install_bpf_program(p, fp) < 0)
352 			return (-1);
353 	}
354 
355 	/*XXX this goes in tcpdump*/
356 	if (p->md.use_bpf)
357 		fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
358 	else
359 		fprintf(stderr, "tcpdump: Filtering in user process\n");
360 	return (0);
361 }
362