xref: /aosp_15_r20/external/libpcap/pcap-pf.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3*8b26181fSAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker  *
5*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker  * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker  * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker  * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker  * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker  * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker  * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker  * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker  * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker  * written permission.
17*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker  *
21*8b26181fSAndroid Build Coastguard Worker  * packet filter subroutines for tcpdump
22*8b26181fSAndroid Build Coastguard Worker  *	Extraction/creation by Jeffrey Mogul, DECWRL
23*8b26181fSAndroid Build Coastguard Worker  */
24*8b26181fSAndroid Build Coastguard Worker 
25*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
26*8b26181fSAndroid Build Coastguard Worker #include <config.h>
27*8b26181fSAndroid Build Coastguard Worker #endif
28*8b26181fSAndroid Build Coastguard Worker 
29*8b26181fSAndroid Build Coastguard Worker #include <sys/types.h>
30*8b26181fSAndroid Build Coastguard Worker #include <sys/time.h>
31*8b26181fSAndroid Build Coastguard Worker #include <sys/timeb.h>
32*8b26181fSAndroid Build Coastguard Worker #include <sys/socket.h>
33*8b26181fSAndroid Build Coastguard Worker #include <sys/file.h>
34*8b26181fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
35*8b26181fSAndroid Build Coastguard Worker #include <net/pfilt.h>
36*8b26181fSAndroid Build Coastguard Worker 
37*8b26181fSAndroid Build Coastguard Worker struct mbuf;
38*8b26181fSAndroid Build Coastguard Worker struct rtentry;
39*8b26181fSAndroid Build Coastguard Worker #include <net/if.h>
40*8b26181fSAndroid Build Coastguard Worker 
41*8b26181fSAndroid Build Coastguard Worker #include <netinet/in.h>
42*8b26181fSAndroid Build Coastguard Worker #include <netinet/in_systm.h>
43*8b26181fSAndroid Build Coastguard Worker #include <netinet/ip.h>
44*8b26181fSAndroid Build Coastguard Worker #include <netinet/if_ether.h>
45*8b26181fSAndroid Build Coastguard Worker #include <netinet/ip_var.h>
46*8b26181fSAndroid Build Coastguard Worker #include <netinet/udp.h>
47*8b26181fSAndroid Build Coastguard Worker #include <netinet/udp_var.h>
48*8b26181fSAndroid Build Coastguard Worker #include <netinet/tcp.h>
49*8b26181fSAndroid Build Coastguard Worker #include <netinet/tcpip.h>
50*8b26181fSAndroid Build Coastguard Worker 
51*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
52*8b26181fSAndroid Build Coastguard Worker #include <netdb.h>
53*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
54*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
55*8b26181fSAndroid Build Coastguard Worker #include <string.h>
56*8b26181fSAndroid Build Coastguard Worker #include <unistd.h>
57*8b26181fSAndroid Build Coastguard Worker 
58*8b26181fSAndroid Build Coastguard Worker /*
59*8b26181fSAndroid Build Coastguard Worker  * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
60*8b26181fSAndroid Build Coastguard Worker  * native OS version, as we need various BPF ioctls from it.
61*8b26181fSAndroid Build Coastguard Worker  */
62*8b26181fSAndroid Build Coastguard Worker #define PCAP_DONT_INCLUDE_PCAP_BPF_H
63*8b26181fSAndroid Build Coastguard Worker #include <net/bpf.h>
64*8b26181fSAndroid Build Coastguard Worker 
65*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
66*8b26181fSAndroid Build Coastguard Worker 
67*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
68*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
69*8b26181fSAndroid Build Coastguard Worker #endif
70*8b26181fSAndroid Build Coastguard Worker 
71*8b26181fSAndroid Build Coastguard Worker /*
72*8b26181fSAndroid Build Coastguard Worker  * FDDI packets are padded to make everything line up on a nice boundary.
73*8b26181fSAndroid Build Coastguard Worker  */
74*8b26181fSAndroid Build Coastguard Worker #define       PCAP_FDDIPAD 3
75*8b26181fSAndroid Build Coastguard Worker 
76*8b26181fSAndroid Build Coastguard Worker /*
77*8b26181fSAndroid Build Coastguard Worker  * Private data for capturing on Ultrix and DEC OSF/1^WDigital UNIX^W^W
78*8b26181fSAndroid Build Coastguard Worker  * Tru64 UNIX packetfilter devices.
79*8b26181fSAndroid Build Coastguard Worker  */
80*8b26181fSAndroid Build Coastguard Worker struct pcap_pf {
81*8b26181fSAndroid Build Coastguard Worker 	int	filtering_in_kernel; /* using kernel filter */
82*8b26181fSAndroid Build Coastguard Worker 	u_long	TotPkts;	/* can't oflow for 79 hrs on ether */
83*8b26181fSAndroid Build Coastguard Worker 	u_long	TotAccepted;	/* count accepted by filter */
84*8b26181fSAndroid Build Coastguard Worker 	u_long	TotDrops;	/* count of dropped packets */
85*8b26181fSAndroid Build Coastguard Worker 	long	TotMissed;	/* missed by i/f during this run */
86*8b26181fSAndroid Build Coastguard Worker 	long	OrigMissed;	/* missed by i/f before this run */
87*8b26181fSAndroid Build Coastguard Worker };
88*8b26181fSAndroid Build Coastguard Worker 
89*8b26181fSAndroid Build Coastguard Worker static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
90*8b26181fSAndroid Build Coastguard Worker 
91*8b26181fSAndroid Build Coastguard Worker /*
92*8b26181fSAndroid Build Coastguard Worker  * BUFSPACE is the size in bytes of the packet read buffer.  Most tcpdump
93*8b26181fSAndroid Build Coastguard Worker  * applications aren't going to need more than 200 bytes of packet header
94*8b26181fSAndroid Build Coastguard Worker  * and the read shouldn't return more packets than packetfilter's internal
95*8b26181fSAndroid Build Coastguard Worker  * queue limit (bounded at 256).
96*8b26181fSAndroid Build Coastguard Worker  */
97*8b26181fSAndroid Build Coastguard Worker #define BUFSPACE (200 * 256)
98*8b26181fSAndroid Build Coastguard Worker 
99*8b26181fSAndroid Build Coastguard Worker static int
pcap_read_pf(pcap_t * pc,int cnt,pcap_handler callback,u_char * user)100*8b26181fSAndroid Build Coastguard Worker pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
101*8b26181fSAndroid Build Coastguard Worker {
102*8b26181fSAndroid Build Coastguard Worker 	struct pcap_pf *pf = pc->priv;
103*8b26181fSAndroid Build Coastguard Worker 	register u_char *p, *bp;
104*8b26181fSAndroid Build Coastguard Worker 	register int cc, n, buflen, inc;
105*8b26181fSAndroid Build Coastguard Worker 	register struct enstamp *sp;
106*8b26181fSAndroid Build Coastguard Worker 	struct enstamp stamp;
107*8b26181fSAndroid Build Coastguard Worker 	register u_int pad;
108*8b26181fSAndroid Build Coastguard Worker 
109*8b26181fSAndroid Build Coastguard Worker  again:
110*8b26181fSAndroid Build Coastguard Worker 	cc = pc->cc;
111*8b26181fSAndroid Build Coastguard Worker 	if (cc == 0) {
112*8b26181fSAndroid Build Coastguard Worker 		cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
113*8b26181fSAndroid Build Coastguard Worker 		if (cc < 0) {
114*8b26181fSAndroid Build Coastguard Worker 			if (errno == EWOULDBLOCK)
115*8b26181fSAndroid Build Coastguard Worker 				return (0);
116*8b26181fSAndroid Build Coastguard Worker 			if (errno == EINVAL &&
117*8b26181fSAndroid Build Coastguard Worker 			    lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
118*8b26181fSAndroid Build Coastguard Worker 				/*
119*8b26181fSAndroid Build Coastguard Worker 				 * Due to a kernel bug, after 2^31 bytes,
120*8b26181fSAndroid Build Coastguard Worker 				 * the kernel file offset overflows and
121*8b26181fSAndroid Build Coastguard Worker 				 * read fails with EINVAL. The lseek()
122*8b26181fSAndroid Build Coastguard Worker 				 * to 0 will fix things.
123*8b26181fSAndroid Build Coastguard Worker 				 */
124*8b26181fSAndroid Build Coastguard Worker 				(void)lseek(pc->fd, 0L, SEEK_SET);
125*8b26181fSAndroid Build Coastguard Worker 				goto again;
126*8b26181fSAndroid Build Coastguard Worker 			}
127*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(pc->errbuf,
128*8b26181fSAndroid Build Coastguard Worker 			    sizeof(pc->errbuf), errno, "pf read");
129*8b26181fSAndroid Build Coastguard Worker 			return (-1);
130*8b26181fSAndroid Build Coastguard Worker 		}
131*8b26181fSAndroid Build Coastguard Worker 		bp = (u_char *)pc->buffer + pc->offset;
132*8b26181fSAndroid Build Coastguard Worker 	} else
133*8b26181fSAndroid Build Coastguard Worker 		bp = pc->bp;
134*8b26181fSAndroid Build Coastguard Worker 	/*
135*8b26181fSAndroid Build Coastguard Worker 	 * Loop through each packet.
136*8b26181fSAndroid Build Coastguard Worker 	 *
137*8b26181fSAndroid Build Coastguard Worker 	 * This assumes that a single buffer of packets will have
138*8b26181fSAndroid Build Coastguard Worker 	 * <= INT_MAX packets, so the packet count doesn't overflow.
139*8b26181fSAndroid Build Coastguard Worker 	 */
140*8b26181fSAndroid Build Coastguard Worker 	n = 0;
141*8b26181fSAndroid Build Coastguard Worker 	pad = pc->fddipad;
142*8b26181fSAndroid Build Coastguard Worker 	while (cc > 0) {
143*8b26181fSAndroid Build Coastguard Worker 		/*
144*8b26181fSAndroid Build Coastguard Worker 		 * Has "pcap_breakloop()" been called?
145*8b26181fSAndroid Build Coastguard Worker 		 * If so, return immediately - if we haven't read any
146*8b26181fSAndroid Build Coastguard Worker 		 * packets, clear the flag and return -2 to indicate
147*8b26181fSAndroid Build Coastguard Worker 		 * that we were told to break out of the loop, otherwise
148*8b26181fSAndroid Build Coastguard Worker 		 * leave the flag set, so that the *next* call will break
149*8b26181fSAndroid Build Coastguard Worker 		 * out of the loop without having read any packets, and
150*8b26181fSAndroid Build Coastguard Worker 		 * return the number of packets we've processed so far.
151*8b26181fSAndroid Build Coastguard Worker 		 */
152*8b26181fSAndroid Build Coastguard Worker 		if (pc->break_loop) {
153*8b26181fSAndroid Build Coastguard Worker 			if (n == 0) {
154*8b26181fSAndroid Build Coastguard Worker 				pc->break_loop = 0;
155*8b26181fSAndroid Build Coastguard Worker 				return (-2);
156*8b26181fSAndroid Build Coastguard Worker 			} else {
157*8b26181fSAndroid Build Coastguard Worker 				pc->cc = cc;
158*8b26181fSAndroid Build Coastguard Worker 				pc->bp = bp;
159*8b26181fSAndroid Build Coastguard Worker 				return (n);
160*8b26181fSAndroid Build Coastguard Worker 			}
161*8b26181fSAndroid Build Coastguard Worker 		}
162*8b26181fSAndroid Build Coastguard Worker 		if (cc < sizeof(*sp)) {
163*8b26181fSAndroid Build Coastguard Worker 			snprintf(pc->errbuf, sizeof(pc->errbuf),
164*8b26181fSAndroid Build Coastguard Worker 			    "pf short read (%d)", cc);
165*8b26181fSAndroid Build Coastguard Worker 			return (-1);
166*8b26181fSAndroid Build Coastguard Worker 		}
167*8b26181fSAndroid Build Coastguard Worker 		if ((long)bp & 3) {
168*8b26181fSAndroid Build Coastguard Worker 			sp = &stamp;
169*8b26181fSAndroid Build Coastguard Worker 			memcpy((char *)sp, (char *)bp, sizeof(*sp));
170*8b26181fSAndroid Build Coastguard Worker 		} else
171*8b26181fSAndroid Build Coastguard Worker 			sp = (struct enstamp *)bp;
172*8b26181fSAndroid Build Coastguard Worker 		if (sp->ens_stamplen != sizeof(*sp)) {
173*8b26181fSAndroid Build Coastguard Worker 			snprintf(pc->errbuf, sizeof(pc->errbuf),
174*8b26181fSAndroid Build Coastguard Worker 			    "pf short stamplen (%d)",
175*8b26181fSAndroid Build Coastguard Worker 			    sp->ens_stamplen);
176*8b26181fSAndroid Build Coastguard Worker 			return (-1);
177*8b26181fSAndroid Build Coastguard Worker 		}
178*8b26181fSAndroid Build Coastguard Worker 
179*8b26181fSAndroid Build Coastguard Worker 		p = bp + sp->ens_stamplen;
180*8b26181fSAndroid Build Coastguard Worker 		buflen = sp->ens_count;
181*8b26181fSAndroid Build Coastguard Worker 		if (buflen > pc->snapshot)
182*8b26181fSAndroid Build Coastguard Worker 			buflen = pc->snapshot;
183*8b26181fSAndroid Build Coastguard Worker 
184*8b26181fSAndroid Build Coastguard Worker 		/* Calculate inc before possible pad update */
185*8b26181fSAndroid Build Coastguard Worker 		inc = ENALIGN(buflen + sp->ens_stamplen);
186*8b26181fSAndroid Build Coastguard Worker 		cc -= inc;
187*8b26181fSAndroid Build Coastguard Worker 		bp += inc;
188*8b26181fSAndroid Build Coastguard Worker 		pf->TotPkts++;
189*8b26181fSAndroid Build Coastguard Worker 		pf->TotDrops += sp->ens_dropped;
190*8b26181fSAndroid Build Coastguard Worker 		pf->TotMissed = sp->ens_ifoverflows;
191*8b26181fSAndroid Build Coastguard Worker 		if (pf->OrigMissed < 0)
192*8b26181fSAndroid Build Coastguard Worker 			pf->OrigMissed = pf->TotMissed;
193*8b26181fSAndroid Build Coastguard Worker 
194*8b26181fSAndroid Build Coastguard Worker 		/*
195*8b26181fSAndroid Build Coastguard Worker 		 * Short-circuit evaluation: if using BPF filter
196*8b26181fSAndroid Build Coastguard Worker 		 * in kernel, no need to do it now - we already know
197*8b26181fSAndroid Build Coastguard Worker 		 * the packet passed the filter.
198*8b26181fSAndroid Build Coastguard Worker 		 *
199*8b26181fSAndroid Build Coastguard Worker 		 * Note: the filter code was generated assuming
200*8b26181fSAndroid Build Coastguard Worker 		 * that pc->fddipad was the amount of padding
201*8b26181fSAndroid Build Coastguard Worker 		 * before the header, as that's what's required
202*8b26181fSAndroid Build Coastguard Worker 		 * in the kernel, so we run the filter before
203*8b26181fSAndroid Build Coastguard Worker 		 * skipping that padding.
204*8b26181fSAndroid Build Coastguard Worker 		 */
205*8b26181fSAndroid Build Coastguard Worker 		if (pf->filtering_in_kernel ||
206*8b26181fSAndroid Build Coastguard Worker 		    pcap_filter(pc->fcode.bf_insns, p, sp->ens_count, buflen)) {
207*8b26181fSAndroid Build Coastguard Worker 			struct pcap_pkthdr h;
208*8b26181fSAndroid Build Coastguard Worker 			pf->TotAccepted++;
209*8b26181fSAndroid Build Coastguard Worker 			h.ts = sp->ens_tstamp;
210*8b26181fSAndroid Build Coastguard Worker 			h.len = sp->ens_count - pad;
211*8b26181fSAndroid Build Coastguard Worker 			p += pad;
212*8b26181fSAndroid Build Coastguard Worker 			buflen -= pad;
213*8b26181fSAndroid Build Coastguard Worker 			h.caplen = buflen;
214*8b26181fSAndroid Build Coastguard Worker 			(*callback)(user, &h, p);
215*8b26181fSAndroid Build Coastguard Worker 			if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
216*8b26181fSAndroid Build Coastguard Worker 				pc->cc = cc;
217*8b26181fSAndroid Build Coastguard Worker 				pc->bp = bp;
218*8b26181fSAndroid Build Coastguard Worker 				return (n);
219*8b26181fSAndroid Build Coastguard Worker 			}
220*8b26181fSAndroid Build Coastguard Worker 		}
221*8b26181fSAndroid Build Coastguard Worker 	}
222*8b26181fSAndroid Build Coastguard Worker 	pc->cc = 0;
223*8b26181fSAndroid Build Coastguard Worker 	return (n);
224*8b26181fSAndroid Build Coastguard Worker }
225*8b26181fSAndroid Build Coastguard Worker 
226*8b26181fSAndroid Build Coastguard Worker static int
pcap_inject_pf(pcap_t * p,const void * buf,int size)227*8b26181fSAndroid Build Coastguard Worker pcap_inject_pf(pcap_t *p, const void *buf, int size)
228*8b26181fSAndroid Build Coastguard Worker {
229*8b26181fSAndroid Build Coastguard Worker 	int ret;
230*8b26181fSAndroid Build Coastguard Worker 
231*8b26181fSAndroid Build Coastguard Worker 	ret = write(p->fd, buf, size);
232*8b26181fSAndroid Build Coastguard Worker 	if (ret == -1) {
233*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
234*8b26181fSAndroid Build Coastguard Worker 		    errno, "send");
235*8b26181fSAndroid Build Coastguard Worker 		return (-1);
236*8b26181fSAndroid Build Coastguard Worker 	}
237*8b26181fSAndroid Build Coastguard Worker 	return (ret);
238*8b26181fSAndroid Build Coastguard Worker }
239*8b26181fSAndroid Build Coastguard Worker 
240*8b26181fSAndroid Build Coastguard Worker static int
pcap_stats_pf(pcap_t * p,struct pcap_stat * ps)241*8b26181fSAndroid Build Coastguard Worker pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
242*8b26181fSAndroid Build Coastguard Worker {
243*8b26181fSAndroid Build Coastguard Worker 	struct pcap_pf *pf = p->priv;
244*8b26181fSAndroid Build Coastguard Worker 
245*8b26181fSAndroid Build Coastguard Worker 	/*
246*8b26181fSAndroid Build Coastguard Worker 	 * If packet filtering is being done in the kernel:
247*8b26181fSAndroid Build Coastguard Worker 	 *
248*8b26181fSAndroid Build Coastguard Worker 	 *	"ps_recv" counts only packets that passed the filter.
249*8b26181fSAndroid Build Coastguard Worker 	 *	This does not include packets dropped because we
250*8b26181fSAndroid Build Coastguard Worker 	 *	ran out of buffer space.  (XXX - perhaps it should,
251*8b26181fSAndroid Build Coastguard Worker 	 *	by adding "ps_drop" to "ps_recv", for compatibility
252*8b26181fSAndroid Build Coastguard Worker 	 *	with some other platforms.  On the other hand, on
253*8b26181fSAndroid Build Coastguard Worker 	 *	some platforms "ps_recv" counts only packets that
254*8b26181fSAndroid Build Coastguard Worker 	 *	passed the filter, and on others it counts packets
255*8b26181fSAndroid Build Coastguard Worker 	 *	that didn't pass the filter....)
256*8b26181fSAndroid Build Coastguard Worker 	 *
257*8b26181fSAndroid Build Coastguard Worker 	 *	"ps_drop" counts packets that passed the kernel filter
258*8b26181fSAndroid Build Coastguard Worker 	 *	(if any) but were dropped because the input queue was
259*8b26181fSAndroid Build Coastguard Worker 	 *	full.
260*8b26181fSAndroid Build Coastguard Worker 	 *
261*8b26181fSAndroid Build Coastguard Worker 	 *	"ps_ifdrop" counts packets dropped by the network
262*8b26181fSAndroid Build Coastguard Worker 	 *	interface (regardless of whether they would have passed
263*8b26181fSAndroid Build Coastguard Worker 	 *	the input filter, of course).
264*8b26181fSAndroid Build Coastguard Worker 	 *
265*8b26181fSAndroid Build Coastguard Worker 	 * If packet filtering is not being done in the kernel:
266*8b26181fSAndroid Build Coastguard Worker 	 *
267*8b26181fSAndroid Build Coastguard Worker 	 *	"ps_recv" counts only packets that passed the filter.
268*8b26181fSAndroid Build Coastguard Worker 	 *
269*8b26181fSAndroid Build Coastguard Worker 	 *	"ps_drop" counts packets that were dropped because the
270*8b26181fSAndroid Build Coastguard Worker 	 *	input queue was full, regardless of whether they passed
271*8b26181fSAndroid Build Coastguard Worker 	 *	the userland filter.
272*8b26181fSAndroid Build Coastguard Worker 	 *
273*8b26181fSAndroid Build Coastguard Worker 	 *	"ps_ifdrop" counts packets dropped by the network
274*8b26181fSAndroid Build Coastguard Worker 	 *	interface (regardless of whether they would have passed
275*8b26181fSAndroid Build Coastguard Worker 	 *	the input filter, of course).
276*8b26181fSAndroid Build Coastguard Worker 	 *
277*8b26181fSAndroid Build Coastguard Worker 	 * These statistics don't include packets not yet read from
278*8b26181fSAndroid Build Coastguard Worker 	 * the kernel by libpcap, but they may include packets not
279*8b26181fSAndroid Build Coastguard Worker 	 * yet read from libpcap by the application.
280*8b26181fSAndroid Build Coastguard Worker 	 */
281*8b26181fSAndroid Build Coastguard Worker 	ps->ps_recv = pf->TotAccepted;
282*8b26181fSAndroid Build Coastguard Worker 	ps->ps_drop = pf->TotDrops;
283*8b26181fSAndroid Build Coastguard Worker 	ps->ps_ifdrop = pf->TotMissed - pf->OrigMissed;
284*8b26181fSAndroid Build Coastguard Worker 	return (0);
285*8b26181fSAndroid Build Coastguard Worker }
286*8b26181fSAndroid Build Coastguard Worker 
287*8b26181fSAndroid Build Coastguard Worker /*
288*8b26181fSAndroid Build Coastguard Worker  * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
289*8b26181fSAndroid Build Coastguard Worker  * don't get DLT_DOCSIS defined.
290*8b26181fSAndroid Build Coastguard Worker  */
291*8b26181fSAndroid Build Coastguard Worker #ifndef DLT_DOCSIS
292*8b26181fSAndroid Build Coastguard Worker #define DLT_DOCSIS	143
293*8b26181fSAndroid Build Coastguard Worker #endif
294*8b26181fSAndroid Build Coastguard Worker 
295*8b26181fSAndroid Build Coastguard Worker static int
pcap_activate_pf(pcap_t * p)296*8b26181fSAndroid Build Coastguard Worker pcap_activate_pf(pcap_t *p)
297*8b26181fSAndroid Build Coastguard Worker {
298*8b26181fSAndroid Build Coastguard Worker 	struct pcap_pf *pf = p->priv;
299*8b26181fSAndroid Build Coastguard Worker 	short enmode;
300*8b26181fSAndroid Build Coastguard Worker 	int backlog = -1;	/* request the most */
301*8b26181fSAndroid Build Coastguard Worker 	struct enfilter Filter;
302*8b26181fSAndroid Build Coastguard Worker 	struct endevp devparams;
303*8b26181fSAndroid Build Coastguard Worker 	int err;
304*8b26181fSAndroid Build Coastguard Worker 
305*8b26181fSAndroid Build Coastguard Worker 	/*
306*8b26181fSAndroid Build Coastguard Worker 	 * Initially try a read/write open (to allow the inject
307*8b26181fSAndroid Build Coastguard Worker 	 * method to work).  If that fails due to permission
308*8b26181fSAndroid Build Coastguard Worker 	 * issues, fall back to read-only.  This allows a
309*8b26181fSAndroid Build Coastguard Worker 	 * non-root user to be granted specific access to pcap
310*8b26181fSAndroid Build Coastguard Worker 	 * capabilities via file permissions.
311*8b26181fSAndroid Build Coastguard Worker 	 *
312*8b26181fSAndroid Build Coastguard Worker 	 * XXX - we should have an API that has a flag that
313*8b26181fSAndroid Build Coastguard Worker 	 * controls whether to open read-only or read-write,
314*8b26181fSAndroid Build Coastguard Worker 	 * so that denial of permission to send (or inability
315*8b26181fSAndroid Build Coastguard Worker 	 * to send, if sending packets isn't supported on
316*8b26181fSAndroid Build Coastguard Worker 	 * the device in question) can be indicated at open
317*8b26181fSAndroid Build Coastguard Worker 	 * time.
318*8b26181fSAndroid Build Coastguard Worker 	 *
319*8b26181fSAndroid Build Coastguard Worker 	 * XXX - we assume here that "pfopen()" does not, in fact, modify
320*8b26181fSAndroid Build Coastguard Worker 	 * its argument, even though it takes a "char *" rather than a
321*8b26181fSAndroid Build Coastguard Worker 	 * "const char *" as its first argument.  That appears to be
322*8b26181fSAndroid Build Coastguard Worker 	 * the case, at least on Digital UNIX 4.0.
323*8b26181fSAndroid Build Coastguard Worker 	 *
324*8b26181fSAndroid Build Coastguard Worker 	 * XXX - is there an error that means "no such device"?  Is
325*8b26181fSAndroid Build Coastguard Worker 	 * there one that means "that device doesn't support pf"?
326*8b26181fSAndroid Build Coastguard Worker 	 */
327*8b26181fSAndroid Build Coastguard Worker 	p->fd = pfopen(p->opt.device, O_RDWR);
328*8b26181fSAndroid Build Coastguard Worker 	if (p->fd == -1 && errno == EACCES)
329*8b26181fSAndroid Build Coastguard Worker 		p->fd = pfopen(p->opt.device, O_RDONLY);
330*8b26181fSAndroid Build Coastguard Worker 	if (p->fd < 0) {
331*8b26181fSAndroid Build Coastguard Worker 		if (errno == EACCES) {
332*8b26181fSAndroid Build Coastguard Worker 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
333*8b26181fSAndroid Build Coastguard Worker 			    "pf open: %s: Permission denied\n"
334*8b26181fSAndroid Build Coastguard Worker "your system may not be properly configured; see the packetfilter(4) man page",
335*8b26181fSAndroid Build Coastguard Worker 			    p->opt.device);
336*8b26181fSAndroid Build Coastguard Worker 			err = PCAP_ERROR_PERM_DENIED;
337*8b26181fSAndroid Build Coastguard Worker 		} else {
338*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
339*8b26181fSAndroid Build Coastguard Worker 			    errno, "pf open: %s", p->opt.device);
340*8b26181fSAndroid Build Coastguard Worker 			err = PCAP_ERROR;
341*8b26181fSAndroid Build Coastguard Worker 		}
342*8b26181fSAndroid Build Coastguard Worker 		goto bad;
343*8b26181fSAndroid Build Coastguard Worker 	}
344*8b26181fSAndroid Build Coastguard Worker 
345*8b26181fSAndroid Build Coastguard Worker 	/*
346*8b26181fSAndroid Build Coastguard Worker 	 * Turn a negative snapshot value (invalid), a snapshot value of
347*8b26181fSAndroid Build Coastguard Worker 	 * 0 (unspecified), or a value bigger than the normal maximum
348*8b26181fSAndroid Build Coastguard Worker 	 * value, into the maximum allowed value.
349*8b26181fSAndroid Build Coastguard Worker 	 *
350*8b26181fSAndroid Build Coastguard Worker 	 * If some application really *needs* a bigger snapshot
351*8b26181fSAndroid Build Coastguard Worker 	 * length, we should just increase MAXIMUM_SNAPLEN.
352*8b26181fSAndroid Build Coastguard Worker 	 */
353*8b26181fSAndroid Build Coastguard Worker 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
354*8b26181fSAndroid Build Coastguard Worker 		p->snapshot = MAXIMUM_SNAPLEN;
355*8b26181fSAndroid Build Coastguard Worker 
356*8b26181fSAndroid Build Coastguard Worker 	pf->OrigMissed = -1;
357*8b26181fSAndroid Build Coastguard Worker 	enmode = ENTSTAMP|ENNONEXCL;
358*8b26181fSAndroid Build Coastguard Worker 	if (!p->opt.immediate)
359*8b26181fSAndroid Build Coastguard Worker 		enmode |= ENBATCH;
360*8b26181fSAndroid Build Coastguard Worker 	if (p->opt.promisc)
361*8b26181fSAndroid Build Coastguard Worker 		enmode |= ENPROMISC;
362*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
363*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
364*8b26181fSAndroid Build Coastguard Worker 		    errno, "EIOCMBIS");
365*8b26181fSAndroid Build Coastguard Worker 		err = PCAP_ERROR;
366*8b26181fSAndroid Build Coastguard Worker 		goto bad;
367*8b26181fSAndroid Build Coastguard Worker 	}
368*8b26181fSAndroid Build Coastguard Worker #ifdef	ENCOPYALL
369*8b26181fSAndroid Build Coastguard Worker 	/* Try to set COPYALL mode so that we see packets to ourself */
370*8b26181fSAndroid Build Coastguard Worker 	enmode = ENCOPYALL;
371*8b26181fSAndroid Build Coastguard Worker 	(void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
372*8b26181fSAndroid Build Coastguard Worker #endif
373*8b26181fSAndroid Build Coastguard Worker 	/* set the backlog */
374*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
375*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
376*8b26181fSAndroid Build Coastguard Worker 		    errno, "EIOCSETW");
377*8b26181fSAndroid Build Coastguard Worker 		err = PCAP_ERROR;
378*8b26181fSAndroid Build Coastguard Worker 		goto bad;
379*8b26181fSAndroid Build Coastguard Worker 	}
380*8b26181fSAndroid Build Coastguard Worker 	/* discover interface type */
381*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
382*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
383*8b26181fSAndroid Build Coastguard Worker 		    errno, "EIOCDEVP");
384*8b26181fSAndroid Build Coastguard Worker 		err = PCAP_ERROR;
385*8b26181fSAndroid Build Coastguard Worker 		goto bad;
386*8b26181fSAndroid Build Coastguard Worker 	}
387*8b26181fSAndroid Build Coastguard Worker 	/* HACK: to compile prior to Ultrix 4.2 */
388*8b26181fSAndroid Build Coastguard Worker #ifndef	ENDT_FDDI
389*8b26181fSAndroid Build Coastguard Worker #define	ENDT_FDDI	4
390*8b26181fSAndroid Build Coastguard Worker #endif
391*8b26181fSAndroid Build Coastguard Worker 	switch (devparams.end_dev_type) {
392*8b26181fSAndroid Build Coastguard Worker 
393*8b26181fSAndroid Build Coastguard Worker 	case ENDT_10MB:
394*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_EN10MB;
395*8b26181fSAndroid Build Coastguard Worker 		p->offset = 2;
396*8b26181fSAndroid Build Coastguard Worker 		/*
397*8b26181fSAndroid Build Coastguard Worker 		 * This is (presumably) a real Ethernet capture; give it a
398*8b26181fSAndroid Build Coastguard Worker 		 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
399*8b26181fSAndroid Build Coastguard Worker 		 * that an application can let you choose it, in case you're
400*8b26181fSAndroid Build Coastguard Worker 		 * capturing DOCSIS traffic that a Cisco Cable Modem
401*8b26181fSAndroid Build Coastguard Worker 		 * Termination System is putting out onto an Ethernet (it
402*8b26181fSAndroid Build Coastguard Worker 		 * doesn't put an Ethernet header onto the wire, it puts raw
403*8b26181fSAndroid Build Coastguard Worker 		 * DOCSIS frames out on the wire inside the low-level
404*8b26181fSAndroid Build Coastguard Worker 		 * Ethernet framing).
405*8b26181fSAndroid Build Coastguard Worker 		 */
406*8b26181fSAndroid Build Coastguard Worker 		p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
407*8b26181fSAndroid Build Coastguard Worker 		/*
408*8b26181fSAndroid Build Coastguard Worker 		 * If that fails, just leave the list empty.
409*8b26181fSAndroid Build Coastguard Worker 		 */
410*8b26181fSAndroid Build Coastguard Worker 		if (p->dlt_list != NULL) {
411*8b26181fSAndroid Build Coastguard Worker 			p->dlt_list[0] = DLT_EN10MB;
412*8b26181fSAndroid Build Coastguard Worker 			p->dlt_list[1] = DLT_DOCSIS;
413*8b26181fSAndroid Build Coastguard Worker 			p->dlt_count = 2;
414*8b26181fSAndroid Build Coastguard Worker 		}
415*8b26181fSAndroid Build Coastguard Worker 		break;
416*8b26181fSAndroid Build Coastguard Worker 
417*8b26181fSAndroid Build Coastguard Worker 	case ENDT_FDDI:
418*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_FDDI;
419*8b26181fSAndroid Build Coastguard Worker 		break;
420*8b26181fSAndroid Build Coastguard Worker 
421*8b26181fSAndroid Build Coastguard Worker #ifdef ENDT_SLIP
422*8b26181fSAndroid Build Coastguard Worker 	case ENDT_SLIP:
423*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_SLIP;
424*8b26181fSAndroid Build Coastguard Worker 		break;
425*8b26181fSAndroid Build Coastguard Worker #endif
426*8b26181fSAndroid Build Coastguard Worker 
427*8b26181fSAndroid Build Coastguard Worker #ifdef ENDT_PPP
428*8b26181fSAndroid Build Coastguard Worker 	case ENDT_PPP:
429*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_PPP;
430*8b26181fSAndroid Build Coastguard Worker 		break;
431*8b26181fSAndroid Build Coastguard Worker #endif
432*8b26181fSAndroid Build Coastguard Worker 
433*8b26181fSAndroid Build Coastguard Worker #ifdef ENDT_LOOPBACK
434*8b26181fSAndroid Build Coastguard Worker 	case ENDT_LOOPBACK:
435*8b26181fSAndroid Build Coastguard Worker 		/*
436*8b26181fSAndroid Build Coastguard Worker 		 * It appears to use Ethernet framing, at least on
437*8b26181fSAndroid Build Coastguard Worker 		 * Digital UNIX 4.0.
438*8b26181fSAndroid Build Coastguard Worker 		 */
439*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_EN10MB;
440*8b26181fSAndroid Build Coastguard Worker 		p->offset = 2;
441*8b26181fSAndroid Build Coastguard Worker 		break;
442*8b26181fSAndroid Build Coastguard Worker #endif
443*8b26181fSAndroid Build Coastguard Worker 
444*8b26181fSAndroid Build Coastguard Worker #ifdef ENDT_TRN
445*8b26181fSAndroid Build Coastguard Worker 	case ENDT_TRN:
446*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_IEEE802;
447*8b26181fSAndroid Build Coastguard Worker 		break;
448*8b26181fSAndroid Build Coastguard Worker #endif
449*8b26181fSAndroid Build Coastguard Worker 
450*8b26181fSAndroid Build Coastguard Worker 	default:
451*8b26181fSAndroid Build Coastguard Worker 		/*
452*8b26181fSAndroid Build Coastguard Worker 		 * XXX - what about ENDT_IEEE802?  The pfilt.h header
453*8b26181fSAndroid Build Coastguard Worker 		 * file calls this "IEEE 802 networks (non-Ethernet)",
454*8b26181fSAndroid Build Coastguard Worker 		 * but that doesn't specify a specific link layer type;
455*8b26181fSAndroid Build Coastguard Worker 		 * it could be 802.4, or 802.5 (except that 802.5 is
456*8b26181fSAndroid Build Coastguard Worker 		 * ENDT_TRN), or 802.6, or 802.11, or....  That's why
457*8b26181fSAndroid Build Coastguard Worker 		 * DLT_IEEE802 was hijacked to mean Token Ring in various
458*8b26181fSAndroid Build Coastguard Worker 		 * BSDs, and why we went along with that hijacking.
459*8b26181fSAndroid Build Coastguard Worker 		 *
460*8b26181fSAndroid Build Coastguard Worker 		 * XXX - what about ENDT_HDLC and ENDT_NULL?
461*8b26181fSAndroid Build Coastguard Worker 		 * Presumably, as ENDT_OTHER is just "Miscellaneous
462*8b26181fSAndroid Build Coastguard Worker 		 * framing", there's not much we can do, as that
463*8b26181fSAndroid Build Coastguard Worker 		 * doesn't specify a particular type of header.
464*8b26181fSAndroid Build Coastguard Worker 		 */
465*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
466*8b26181fSAndroid Build Coastguard Worker 		    "unknown data-link type %u", devparams.end_dev_type);
467*8b26181fSAndroid Build Coastguard Worker 		err = PCAP_ERROR;
468*8b26181fSAndroid Build Coastguard Worker 		goto bad;
469*8b26181fSAndroid Build Coastguard Worker 	}
470*8b26181fSAndroid Build Coastguard Worker 	/* set truncation */
471*8b26181fSAndroid Build Coastguard Worker 	if (p->linktype == DLT_FDDI) {
472*8b26181fSAndroid Build Coastguard Worker 		p->fddipad = PCAP_FDDIPAD;
473*8b26181fSAndroid Build Coastguard Worker 
474*8b26181fSAndroid Build Coastguard Worker 		/* packetfilter includes the padding in the snapshot */
475*8b26181fSAndroid Build Coastguard Worker 		p->snapshot += PCAP_FDDIPAD;
476*8b26181fSAndroid Build Coastguard Worker 	} else
477*8b26181fSAndroid Build Coastguard Worker 		p->fddipad = 0;
478*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&p->snapshot) < 0) {
479*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
480*8b26181fSAndroid Build Coastguard Worker 		    errno, "EIOCTRUNCATE");
481*8b26181fSAndroid Build Coastguard Worker 		err = PCAP_ERROR;
482*8b26181fSAndroid Build Coastguard Worker 		goto bad;
483*8b26181fSAndroid Build Coastguard Worker 	}
484*8b26181fSAndroid Build Coastguard Worker 	/* accept all packets */
485*8b26181fSAndroid Build Coastguard Worker 	memset(&Filter, 0, sizeof(Filter));
486*8b26181fSAndroid Build Coastguard Worker 	Filter.enf_Priority = 37;	/* anything > 2 */
487*8b26181fSAndroid Build Coastguard Worker 	Filter.enf_FilterLen = 0;	/* means "always true" */
488*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
489*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
490*8b26181fSAndroid Build Coastguard Worker 		    errno, "EIOCSETF");
491*8b26181fSAndroid Build Coastguard Worker 		err = PCAP_ERROR;
492*8b26181fSAndroid Build Coastguard Worker 		goto bad;
493*8b26181fSAndroid Build Coastguard Worker 	}
494*8b26181fSAndroid Build Coastguard Worker 
495*8b26181fSAndroid Build Coastguard Worker 	if (p->opt.timeout != 0) {
496*8b26181fSAndroid Build Coastguard Worker 		struct timeval timeout;
497*8b26181fSAndroid Build Coastguard Worker 		timeout.tv_sec = p->opt.timeout / 1000;
498*8b26181fSAndroid Build Coastguard Worker 		timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
499*8b26181fSAndroid Build Coastguard Worker 		if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
500*8b26181fSAndroid Build Coastguard Worker 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
501*8b26181fSAndroid Build Coastguard Worker 			    errno, "EIOCSRTIMEOUT");
502*8b26181fSAndroid Build Coastguard Worker 			err = PCAP_ERROR;
503*8b26181fSAndroid Build Coastguard Worker 			goto bad;
504*8b26181fSAndroid Build Coastguard Worker 		}
505*8b26181fSAndroid Build Coastguard Worker 	}
506*8b26181fSAndroid Build Coastguard Worker 
507*8b26181fSAndroid Build Coastguard Worker 	p->bufsize = BUFSPACE;
508*8b26181fSAndroid Build Coastguard Worker 	p->buffer = malloc(p->bufsize + p->offset);
509*8b26181fSAndroid Build Coastguard Worker 	if (p->buffer == NULL) {
510*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
511*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
512*8b26181fSAndroid Build Coastguard Worker 		err = PCAP_ERROR;
513*8b26181fSAndroid Build Coastguard Worker 		goto bad;
514*8b26181fSAndroid Build Coastguard Worker 	}
515*8b26181fSAndroid Build Coastguard Worker 
516*8b26181fSAndroid Build Coastguard Worker 	/*
517*8b26181fSAndroid Build Coastguard Worker 	 * "select()" and "poll()" work on packetfilter devices.
518*8b26181fSAndroid Build Coastguard Worker 	 */
519*8b26181fSAndroid Build Coastguard Worker 	p->selectable_fd = p->fd;
520*8b26181fSAndroid Build Coastguard Worker 
521*8b26181fSAndroid Build Coastguard Worker 	p->read_op = pcap_read_pf;
522*8b26181fSAndroid Build Coastguard Worker 	p->inject_op = pcap_inject_pf;
523*8b26181fSAndroid Build Coastguard Worker 	p->setfilter_op = pcap_setfilter_pf;
524*8b26181fSAndroid Build Coastguard Worker 	p->setdirection_op = NULL;	/* Not implemented. */
525*8b26181fSAndroid Build Coastguard Worker 	p->set_datalink_op = NULL;	/* can't change data link type */
526*8b26181fSAndroid Build Coastguard Worker 	p->getnonblock_op = pcap_getnonblock_fd;
527*8b26181fSAndroid Build Coastguard Worker 	p->setnonblock_op = pcap_setnonblock_fd;
528*8b26181fSAndroid Build Coastguard Worker 	p->stats_op = pcap_stats_pf;
529*8b26181fSAndroid Build Coastguard Worker 
530*8b26181fSAndroid Build Coastguard Worker 	return (0);
531*8b26181fSAndroid Build Coastguard Worker  bad:
532*8b26181fSAndroid Build Coastguard Worker 	pcap_cleanup_live_common(p);
533*8b26181fSAndroid Build Coastguard Worker 	return (err);
534*8b26181fSAndroid Build Coastguard Worker }
535*8b26181fSAndroid Build Coastguard Worker 
536*8b26181fSAndroid Build Coastguard Worker pcap_t *
pcap_create_interface(const char * device _U_,char * ebuf)537*8b26181fSAndroid Build Coastguard Worker pcap_create_interface(const char *device _U_, char *ebuf)
538*8b26181fSAndroid Build Coastguard Worker {
539*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
540*8b26181fSAndroid Build Coastguard Worker 
541*8b26181fSAndroid Build Coastguard Worker 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_pf);
542*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL)
543*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
544*8b26181fSAndroid Build Coastguard Worker 
545*8b26181fSAndroid Build Coastguard Worker 	p->activate_op = pcap_activate_pf;
546*8b26181fSAndroid Build Coastguard Worker 	return (p);
547*8b26181fSAndroid Build Coastguard Worker }
548*8b26181fSAndroid Build Coastguard Worker 
549*8b26181fSAndroid Build Coastguard Worker /*
550*8b26181fSAndroid Build Coastguard Worker  * XXX - is there an error from pfopen() that means "no such device"?
551*8b26181fSAndroid Build Coastguard Worker  * Is there one that means "that device doesn't support pf"?
552*8b26181fSAndroid Build Coastguard Worker  */
553*8b26181fSAndroid Build Coastguard Worker static int
can_be_bound(const char * name _U_)554*8b26181fSAndroid Build Coastguard Worker can_be_bound(const char *name _U_)
555*8b26181fSAndroid Build Coastguard Worker {
556*8b26181fSAndroid Build Coastguard Worker 	return (1);
557*8b26181fSAndroid Build Coastguard Worker }
558*8b26181fSAndroid Build Coastguard Worker 
559*8b26181fSAndroid Build Coastguard Worker static int
get_if_flags(const char * name _U_,bpf_u_int32 * flags _U_,char * errbuf _U_)560*8b26181fSAndroid Build Coastguard Worker get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
561*8b26181fSAndroid Build Coastguard Worker {
562*8b26181fSAndroid Build Coastguard Worker 	/*
563*8b26181fSAndroid Build Coastguard Worker 	 * Nothing we can do other than mark loopback devices as "the
564*8b26181fSAndroid Build Coastguard Worker 	 * connected/disconnected status doesn't apply".
565*8b26181fSAndroid Build Coastguard Worker 	 *
566*8b26181fSAndroid Build Coastguard Worker 	 * XXX - is there a way to find out whether an adapter has
567*8b26181fSAndroid Build Coastguard Worker 	 * something plugged into it?
568*8b26181fSAndroid Build Coastguard Worker 	 */
569*8b26181fSAndroid Build Coastguard Worker 	if (*flags & PCAP_IF_LOOPBACK) {
570*8b26181fSAndroid Build Coastguard Worker 		/*
571*8b26181fSAndroid Build Coastguard Worker 		 * Loopback devices aren't wireless, and "connected"/
572*8b26181fSAndroid Build Coastguard Worker 		 * "disconnected" doesn't apply to them.
573*8b26181fSAndroid Build Coastguard Worker 		 */
574*8b26181fSAndroid Build Coastguard Worker 		*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
575*8b26181fSAndroid Build Coastguard Worker 		return (0);
576*8b26181fSAndroid Build Coastguard Worker 	}
577*8b26181fSAndroid Build Coastguard Worker 	return (0);
578*8b26181fSAndroid Build Coastguard Worker }
579*8b26181fSAndroid Build Coastguard Worker 
580*8b26181fSAndroid Build Coastguard Worker int
pcap_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)581*8b26181fSAndroid Build Coastguard Worker pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
582*8b26181fSAndroid Build Coastguard Worker {
583*8b26181fSAndroid Build Coastguard Worker 	return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
584*8b26181fSAndroid Build Coastguard Worker 	    get_if_flags));
585*8b26181fSAndroid Build Coastguard Worker }
586*8b26181fSAndroid Build Coastguard Worker 
587*8b26181fSAndroid Build Coastguard Worker static int
pcap_setfilter_pf(pcap_t * p,struct bpf_program * fp)588*8b26181fSAndroid Build Coastguard Worker pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
589*8b26181fSAndroid Build Coastguard Worker {
590*8b26181fSAndroid Build Coastguard Worker 	struct pcap_pf *pf = p->priv;
591*8b26181fSAndroid Build Coastguard Worker 	struct bpf_version bv;
592*8b26181fSAndroid Build Coastguard Worker 
593*8b26181fSAndroid Build Coastguard Worker 	/*
594*8b26181fSAndroid Build Coastguard Worker 	 * See if BIOCVERSION works.  If not, we assume the kernel doesn't
595*8b26181fSAndroid Build Coastguard Worker 	 * support BPF-style filters (it's not documented in the bpf(7)
596*8b26181fSAndroid Build Coastguard Worker 	 * or packetfiler(7) man pages, but the code used to fail if
597*8b26181fSAndroid Build Coastguard Worker 	 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
598*8b26181fSAndroid Build Coastguard Worker 	 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
599*8b26181fSAndroid Build Coastguard Worker 	 * there, at least).
600*8b26181fSAndroid Build Coastguard Worker 	 */
601*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) {
602*8b26181fSAndroid Build Coastguard Worker 		/*
603*8b26181fSAndroid Build Coastguard Worker 		 * OK, we have the version of the BPF interpreter;
604*8b26181fSAndroid Build Coastguard Worker 		 * is it the same major version as us, and the same
605*8b26181fSAndroid Build Coastguard Worker 		 * or better minor version?
606*8b26181fSAndroid Build Coastguard Worker 		 */
607*8b26181fSAndroid Build Coastguard Worker 		if (bv.bv_major == BPF_MAJOR_VERSION &&
608*8b26181fSAndroid Build Coastguard Worker 		    bv.bv_minor >= BPF_MINOR_VERSION) {
609*8b26181fSAndroid Build Coastguard Worker 			/*
610*8b26181fSAndroid Build Coastguard Worker 			 * Yes.  Try to install the filter.
611*8b26181fSAndroid Build Coastguard Worker 			 */
612*8b26181fSAndroid Build Coastguard Worker 			if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
613*8b26181fSAndroid Build Coastguard Worker 				pcap_fmt_errmsg_for_errno(p->errbuf,
614*8b26181fSAndroid Build Coastguard Worker 				    sizeof(p->errbuf), errno, "BIOCSETF");
615*8b26181fSAndroid Build Coastguard Worker 				return (-1);
616*8b26181fSAndroid Build Coastguard Worker 			}
617*8b26181fSAndroid Build Coastguard Worker 
618*8b26181fSAndroid Build Coastguard Worker 			/*
619*8b26181fSAndroid Build Coastguard Worker 			 * OK, that succeeded.  We're doing filtering in
620*8b26181fSAndroid Build Coastguard Worker 			 * the kernel.  (We assume we don't have a
621*8b26181fSAndroid Build Coastguard Worker 			 * userland filter installed - that'd require
622*8b26181fSAndroid Build Coastguard Worker 			 * a previous version check to have failed but
623*8b26181fSAndroid Build Coastguard Worker 			 * this one to succeed.)
624*8b26181fSAndroid Build Coastguard Worker 			 *
625*8b26181fSAndroid Build Coastguard Worker 			 * XXX - this message should be supplied to the
626*8b26181fSAndroid Build Coastguard Worker 			 * application as a warning of some sort,
627*8b26181fSAndroid Build Coastguard Worker 			 * except that if it's a GUI application, it's
628*8b26181fSAndroid Build Coastguard Worker 			 * not clear that it should be displayed in
629*8b26181fSAndroid Build Coastguard Worker 			 * a window to annoy the user.
630*8b26181fSAndroid Build Coastguard Worker 			 */
631*8b26181fSAndroid Build Coastguard Worker 			fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
632*8b26181fSAndroid Build Coastguard Worker 			pf->filtering_in_kernel = 1;
633*8b26181fSAndroid Build Coastguard Worker 
634*8b26181fSAndroid Build Coastguard Worker 			/*
635*8b26181fSAndroid Build Coastguard Worker 			 * Discard any previously-received packets,
636*8b26181fSAndroid Build Coastguard Worker 			 * as they might have passed whatever filter
637*8b26181fSAndroid Build Coastguard Worker 			 * was formerly in effect, but might not pass
638*8b26181fSAndroid Build Coastguard Worker 			 * this filter (BIOCSETF discards packets buffered
639*8b26181fSAndroid Build Coastguard Worker 			 * in the kernel, so you can lose packets in any
640*8b26181fSAndroid Build Coastguard Worker 			 * case).
641*8b26181fSAndroid Build Coastguard Worker 			 */
642*8b26181fSAndroid Build Coastguard Worker 			p->cc = 0;
643*8b26181fSAndroid Build Coastguard Worker 			return (0);
644*8b26181fSAndroid Build Coastguard Worker 		}
645*8b26181fSAndroid Build Coastguard Worker 
646*8b26181fSAndroid Build Coastguard Worker 		/*
647*8b26181fSAndroid Build Coastguard Worker 		 * We can't use the kernel's BPF interpreter; don't give
648*8b26181fSAndroid Build Coastguard Worker 		 * up, just log a message and be inefficient.
649*8b26181fSAndroid Build Coastguard Worker 		 *
650*8b26181fSAndroid Build Coastguard Worker 		 * XXX - this should really be supplied to the application
651*8b26181fSAndroid Build Coastguard Worker 		 * as a warning of some sort.
652*8b26181fSAndroid Build Coastguard Worker 		 */
653*8b26181fSAndroid Build Coastguard Worker 		fprintf(stderr,
654*8b26181fSAndroid Build Coastguard Worker 	    "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
655*8b26181fSAndroid Build Coastguard Worker 		    BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
656*8b26181fSAndroid Build Coastguard Worker 		    bv.bv_major, bv.bv_minor);
657*8b26181fSAndroid Build Coastguard Worker 	}
658*8b26181fSAndroid Build Coastguard Worker 
659*8b26181fSAndroid Build Coastguard Worker 	/*
660*8b26181fSAndroid Build Coastguard Worker 	 * We couldn't do filtering in the kernel; do it in userland.
661*8b26181fSAndroid Build Coastguard Worker 	 */
662*8b26181fSAndroid Build Coastguard Worker 	if (install_bpf_program(p, fp) < 0)
663*8b26181fSAndroid Build Coastguard Worker 		return (-1);
664*8b26181fSAndroid Build Coastguard Worker 
665*8b26181fSAndroid Build Coastguard Worker 	/*
666*8b26181fSAndroid Build Coastguard Worker 	 * XXX - this message should be supplied by the application as
667*8b26181fSAndroid Build Coastguard Worker 	 * a warning of some sort.
668*8b26181fSAndroid Build Coastguard Worker 	 */
669*8b26181fSAndroid Build Coastguard Worker 	fprintf(stderr, "tcpdump: Filtering in user process\n");
670*8b26181fSAndroid Build Coastguard Worker 	pf->filtering_in_kernel = 0;
671*8b26181fSAndroid Build Coastguard Worker 	return (0);
672*8b26181fSAndroid Build Coastguard Worker }
673*8b26181fSAndroid Build Coastguard Worker 
674*8b26181fSAndroid Build Coastguard Worker /*
675*8b26181fSAndroid Build Coastguard Worker  * Libpcap version string.
676*8b26181fSAndroid Build Coastguard Worker  */
677*8b26181fSAndroid Build Coastguard Worker const char *
pcap_lib_version(void)678*8b26181fSAndroid Build Coastguard Worker pcap_lib_version(void)
679*8b26181fSAndroid Build Coastguard Worker {
680*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_VERSION_STRING);
681*8b26181fSAndroid Build Coastguard Worker }
682