xref: /aosp_15_r20/external/libpcap/pcap-snoop.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1993, 1994, 1995, 1996, 1997
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 
22*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
23*8b26181fSAndroid Build Coastguard Worker #include <config.h>
24*8b26181fSAndroid Build Coastguard Worker #endif
25*8b26181fSAndroid Build Coastguard Worker 
26*8b26181fSAndroid Build Coastguard Worker #include <sys/param.h>
27*8b26181fSAndroid Build Coastguard Worker #include <sys/file.h>
28*8b26181fSAndroid Build Coastguard Worker #include <sys/ioctl.h>
29*8b26181fSAndroid Build Coastguard Worker #include <sys/socket.h>
30*8b26181fSAndroid Build Coastguard Worker #include <sys/time.h>
31*8b26181fSAndroid Build Coastguard Worker 
32*8b26181fSAndroid Build Coastguard Worker #include <net/raw.h>
33*8b26181fSAndroid Build Coastguard Worker #include <net/if.h>
34*8b26181fSAndroid Build Coastguard Worker 
35*8b26181fSAndroid Build Coastguard Worker #include <netinet/in.h>
36*8b26181fSAndroid Build Coastguard Worker #include <netinet/in_systm.h>
37*8b26181fSAndroid Build Coastguard Worker #include <netinet/ip.h>
38*8b26181fSAndroid Build Coastguard Worker #include <netinet/if_ether.h>
39*8b26181fSAndroid Build Coastguard Worker #include <netinet/ip_var.h>
40*8b26181fSAndroid Build Coastguard Worker #include <netinet/udp.h>
41*8b26181fSAndroid Build Coastguard Worker #include <netinet/udp_var.h>
42*8b26181fSAndroid Build Coastguard Worker #include <netinet/tcp.h>
43*8b26181fSAndroid Build Coastguard Worker #include <netinet/tcpip.h>
44*8b26181fSAndroid Build Coastguard Worker 
45*8b26181fSAndroid Build Coastguard Worker #include <errno.h>
46*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
47*8b26181fSAndroid Build Coastguard Worker #include <stdlib.h>
48*8b26181fSAndroid Build Coastguard Worker #include <string.h>
49*8b26181fSAndroid Build Coastguard Worker #include <unistd.h>
50*8b26181fSAndroid Build Coastguard Worker 
51*8b26181fSAndroid Build Coastguard Worker #include "pcap-int.h"
52*8b26181fSAndroid Build Coastguard Worker 
53*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_OS_PROTO_H
54*8b26181fSAndroid Build Coastguard Worker #include "os-proto.h"
55*8b26181fSAndroid Build Coastguard Worker #endif
56*8b26181fSAndroid Build Coastguard Worker 
57*8b26181fSAndroid Build Coastguard Worker /*
58*8b26181fSAndroid Build Coastguard Worker  * Private data for capturing on snoop devices.
59*8b26181fSAndroid Build Coastguard Worker  */
60*8b26181fSAndroid Build Coastguard Worker struct pcap_snoop {
61*8b26181fSAndroid Build Coastguard Worker 	struct pcap_stat stat;
62*8b26181fSAndroid Build Coastguard Worker };
63*8b26181fSAndroid Build Coastguard Worker 
64*8b26181fSAndroid Build Coastguard Worker static int
pcap_read_snoop(pcap_t * p,int cnt,pcap_handler callback,u_char * user)65*8b26181fSAndroid Build Coastguard Worker pcap_read_snoop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
66*8b26181fSAndroid Build Coastguard Worker {
67*8b26181fSAndroid Build Coastguard Worker 	struct pcap_snoop *psn = p->priv;
68*8b26181fSAndroid Build Coastguard Worker 	int cc;
69*8b26181fSAndroid Build Coastguard Worker 	register struct snoopheader *sh;
70*8b26181fSAndroid Build Coastguard Worker 	register u_int datalen;
71*8b26181fSAndroid Build Coastguard Worker 	register u_int caplen;
72*8b26181fSAndroid Build Coastguard Worker 	register u_char *cp;
73*8b26181fSAndroid Build Coastguard Worker 
74*8b26181fSAndroid Build Coastguard Worker again:
75*8b26181fSAndroid Build Coastguard Worker 	/*
76*8b26181fSAndroid Build Coastguard Worker 	 * Has "pcap_breakloop()" been called?
77*8b26181fSAndroid Build Coastguard Worker 	 */
78*8b26181fSAndroid Build Coastguard Worker 	if (p->break_loop) {
79*8b26181fSAndroid Build Coastguard Worker 		/*
80*8b26181fSAndroid Build Coastguard Worker 		 * Yes - clear the flag that indicates that it
81*8b26181fSAndroid Build Coastguard Worker 		 * has, and return -2 to indicate that we were
82*8b26181fSAndroid Build Coastguard Worker 		 * told to break out of the loop.
83*8b26181fSAndroid Build Coastguard Worker 		 */
84*8b26181fSAndroid Build Coastguard Worker 		p->break_loop = 0;
85*8b26181fSAndroid Build Coastguard Worker 		return (-2);
86*8b26181fSAndroid Build Coastguard Worker 	}
87*8b26181fSAndroid Build Coastguard Worker 	cc = read(p->fd, (char *)p->buffer, p->bufsize);
88*8b26181fSAndroid Build Coastguard Worker 	if (cc < 0) {
89*8b26181fSAndroid Build Coastguard Worker 		/* Don't choke when we get ptraced */
90*8b26181fSAndroid Build Coastguard Worker 		switch (errno) {
91*8b26181fSAndroid Build Coastguard Worker 
92*8b26181fSAndroid Build Coastguard Worker 		case EINTR:
93*8b26181fSAndroid Build Coastguard Worker 			goto again;
94*8b26181fSAndroid Build Coastguard Worker 
95*8b26181fSAndroid Build Coastguard Worker 		case EWOULDBLOCK:
96*8b26181fSAndroid Build Coastguard Worker 			return (0);			/* XXX */
97*8b26181fSAndroid Build Coastguard Worker 		}
98*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
99*8b26181fSAndroid Build Coastguard Worker 		    errno, "read");
100*8b26181fSAndroid Build Coastguard Worker 		return (-1);
101*8b26181fSAndroid Build Coastguard Worker 	}
102*8b26181fSAndroid Build Coastguard Worker 	sh = (struct snoopheader *)p->buffer;
103*8b26181fSAndroid Build Coastguard Worker 	datalen = sh->snoop_packetlen;
104*8b26181fSAndroid Build Coastguard Worker 
105*8b26181fSAndroid Build Coastguard Worker 	/*
106*8b26181fSAndroid Build Coastguard Worker 	 * XXX - Sigh, snoop_packetlen is a 16 bit quantity.  If we
107*8b26181fSAndroid Build Coastguard Worker 	 * got a short length, but read a full sized snoop pakcet,
108*8b26181fSAndroid Build Coastguard Worker 	 * assume we overflowed and add back the 64K...
109*8b26181fSAndroid Build Coastguard Worker 	 */
110*8b26181fSAndroid Build Coastguard Worker 	if (cc == (p->snapshot + sizeof(struct snoopheader)) &&
111*8b26181fSAndroid Build Coastguard Worker 	    (datalen < p->snapshot))
112*8b26181fSAndroid Build Coastguard Worker 		datalen += (64 * 1024);
113*8b26181fSAndroid Build Coastguard Worker 
114*8b26181fSAndroid Build Coastguard Worker 	caplen = (datalen < p->snapshot) ? datalen : p->snapshot;
115*8b26181fSAndroid Build Coastguard Worker 	cp = (u_char *)(sh + 1) + p->offset;		/* XXX */
116*8b26181fSAndroid Build Coastguard Worker 
117*8b26181fSAndroid Build Coastguard Worker 	/*
118*8b26181fSAndroid Build Coastguard Worker 	 * XXX unfortunately snoop loopback isn't exactly like
119*8b26181fSAndroid Build Coastguard Worker 	 * BSD's.  The address family is encoded in the first 2
120*8b26181fSAndroid Build Coastguard Worker 	 * bytes rather than the first 4 bytes!  Luckily the last
121*8b26181fSAndroid Build Coastguard Worker 	 * two snoop loopback bytes are zeroed.
122*8b26181fSAndroid Build Coastguard Worker 	 */
123*8b26181fSAndroid Build Coastguard Worker 	if (p->linktype == DLT_NULL && *((short *)(cp + 2)) == 0) {
124*8b26181fSAndroid Build Coastguard Worker 		u_int *uip = (u_int *)cp;
125*8b26181fSAndroid Build Coastguard Worker 		*uip >>= 16;
126*8b26181fSAndroid Build Coastguard Worker 	}
127*8b26181fSAndroid Build Coastguard Worker 
128*8b26181fSAndroid Build Coastguard Worker 	if (p->fcode.bf_insns == NULL ||
129*8b26181fSAndroid Build Coastguard Worker 	    pcap_filter(p->fcode.bf_insns, cp, datalen, caplen)) {
130*8b26181fSAndroid Build Coastguard Worker 		struct pcap_pkthdr h;
131*8b26181fSAndroid Build Coastguard Worker 		++psn->stat.ps_recv;
132*8b26181fSAndroid Build Coastguard Worker 		h.ts.tv_sec = sh->snoop_timestamp.tv_sec;
133*8b26181fSAndroid Build Coastguard Worker 		h.ts.tv_usec = sh->snoop_timestamp.tv_usec;
134*8b26181fSAndroid Build Coastguard Worker 		h.len = datalen;
135*8b26181fSAndroid Build Coastguard Worker 		h.caplen = caplen;
136*8b26181fSAndroid Build Coastguard Worker 		(*callback)(user, &h, cp);
137*8b26181fSAndroid Build Coastguard Worker 		return (1);
138*8b26181fSAndroid Build Coastguard Worker 	}
139*8b26181fSAndroid Build Coastguard Worker 	return (0);
140*8b26181fSAndroid Build Coastguard Worker }
141*8b26181fSAndroid Build Coastguard Worker 
142*8b26181fSAndroid Build Coastguard Worker static int
pcap_inject_snoop(pcap_t * p,const void * buf,int size)143*8b26181fSAndroid Build Coastguard Worker pcap_inject_snoop(pcap_t *p, const void *buf, int size)
144*8b26181fSAndroid Build Coastguard Worker {
145*8b26181fSAndroid Build Coastguard Worker 	int ret;
146*8b26181fSAndroid Build Coastguard Worker 
147*8b26181fSAndroid Build Coastguard Worker 	/*
148*8b26181fSAndroid Build Coastguard Worker 	 * XXX - libnet overwrites the source address with what I
149*8b26181fSAndroid Build Coastguard Worker 	 * presume is the interface's address; is that required?
150*8b26181fSAndroid Build Coastguard Worker 	 */
151*8b26181fSAndroid Build Coastguard Worker 	ret = write(p->fd, buf, size);
152*8b26181fSAndroid Build Coastguard Worker 	if (ret == -1) {
153*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
154*8b26181fSAndroid Build Coastguard Worker 		    errno, "send");
155*8b26181fSAndroid Build Coastguard Worker 		return (-1);
156*8b26181fSAndroid Build Coastguard Worker 	}
157*8b26181fSAndroid Build Coastguard Worker 	return (ret);
158*8b26181fSAndroid Build Coastguard Worker }
159*8b26181fSAndroid Build Coastguard Worker 
160*8b26181fSAndroid Build Coastguard Worker static int
pcap_stats_snoop(pcap_t * p,struct pcap_stat * ps)161*8b26181fSAndroid Build Coastguard Worker pcap_stats_snoop(pcap_t *p, struct pcap_stat *ps)
162*8b26181fSAndroid Build Coastguard Worker {
163*8b26181fSAndroid Build Coastguard Worker 	struct pcap_snoop *psn = p->priv;
164*8b26181fSAndroid Build Coastguard Worker 	register struct rawstats *rs;
165*8b26181fSAndroid Build Coastguard Worker 	struct rawstats rawstats;
166*8b26181fSAndroid Build Coastguard Worker 
167*8b26181fSAndroid Build Coastguard Worker 	rs = &rawstats;
168*8b26181fSAndroid Build Coastguard Worker 	memset(rs, 0, sizeof(*rs));
169*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) {
170*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
171*8b26181fSAndroid Build Coastguard Worker 		    errno, "SIOCRAWSTATS");
172*8b26181fSAndroid Build Coastguard Worker 		return (-1);
173*8b26181fSAndroid Build Coastguard Worker 	}
174*8b26181fSAndroid Build Coastguard Worker 
175*8b26181fSAndroid Build Coastguard Worker 	/*
176*8b26181fSAndroid Build Coastguard Worker 	 * "ifdrops" are those dropped by the network interface
177*8b26181fSAndroid Build Coastguard Worker 	 * due to resource shortages or hardware errors.
178*8b26181fSAndroid Build Coastguard Worker 	 *
179*8b26181fSAndroid Build Coastguard Worker 	 * "sbdrops" are those dropped due to socket buffer limits.
180*8b26181fSAndroid Build Coastguard Worker 	 *
181*8b26181fSAndroid Build Coastguard Worker 	 * As filter is done in userland, "sbdrops" counts packets
182*8b26181fSAndroid Build Coastguard Worker 	 * regardless of whether they would've passed the filter.
183*8b26181fSAndroid Build Coastguard Worker 	 *
184*8b26181fSAndroid Build Coastguard Worker 	 * XXX - does this count *all* Snoop or Drain sockets,
185*8b26181fSAndroid Build Coastguard Worker 	 * rather than just this socket?  If not, why does it have
186*8b26181fSAndroid Build Coastguard Worker 	 * both Snoop and Drain statistics?
187*8b26181fSAndroid Build Coastguard Worker 	 */
188*8b26181fSAndroid Build Coastguard Worker 	psn->stat.ps_drop =
189*8b26181fSAndroid Build Coastguard Worker 	    rs->rs_snoop.ss_ifdrops + rs->rs_snoop.ss_sbdrops +
190*8b26181fSAndroid Build Coastguard Worker 	    rs->rs_drain.ds_ifdrops + rs->rs_drain.ds_sbdrops;
191*8b26181fSAndroid Build Coastguard Worker 
192*8b26181fSAndroid Build Coastguard Worker 	/*
193*8b26181fSAndroid Build Coastguard Worker 	 * "ps_recv" counts only packets that passed the filter.
194*8b26181fSAndroid Build Coastguard Worker 	 * As filtering is done in userland, this does not include
195*8b26181fSAndroid Build Coastguard Worker 	 * packets dropped because we ran out of buffer space.
196*8b26181fSAndroid Build Coastguard Worker 	 */
197*8b26181fSAndroid Build Coastguard Worker 	*ps = psn->stat;
198*8b26181fSAndroid Build Coastguard Worker 	return (0);
199*8b26181fSAndroid Build Coastguard Worker }
200*8b26181fSAndroid Build Coastguard Worker 
201*8b26181fSAndroid Build Coastguard Worker /* XXX can't disable promiscuous */
202*8b26181fSAndroid Build Coastguard Worker static int
pcap_activate_snoop(pcap_t * p)203*8b26181fSAndroid Build Coastguard Worker pcap_activate_snoop(pcap_t *p)
204*8b26181fSAndroid Build Coastguard Worker {
205*8b26181fSAndroid Build Coastguard Worker 	int fd;
206*8b26181fSAndroid Build Coastguard Worker 	struct sockaddr_raw sr;
207*8b26181fSAndroid Build Coastguard Worker 	struct snoopfilter sf;
208*8b26181fSAndroid Build Coastguard Worker 	u_int v;
209*8b26181fSAndroid Build Coastguard Worker 	int ll_hdrlen;
210*8b26181fSAndroid Build Coastguard Worker 	int snooplen;
211*8b26181fSAndroid Build Coastguard Worker 	struct ifreq ifr;
212*8b26181fSAndroid Build Coastguard Worker 
213*8b26181fSAndroid Build Coastguard Worker 	fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);
214*8b26181fSAndroid Build Coastguard Worker 	if (fd < 0) {
215*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
216*8b26181fSAndroid Build Coastguard Worker 		    errno, "snoop socket");
217*8b26181fSAndroid Build Coastguard Worker 		goto bad;
218*8b26181fSAndroid Build Coastguard Worker 	}
219*8b26181fSAndroid Build Coastguard Worker 	p->fd = fd;
220*8b26181fSAndroid Build Coastguard Worker 	memset(&sr, 0, sizeof(sr));
221*8b26181fSAndroid Build Coastguard Worker 	sr.sr_family = AF_RAW;
222*8b26181fSAndroid Build Coastguard Worker 	(void)strncpy(sr.sr_ifname, p->opt.device, sizeof(sr.sr_ifname));
223*8b26181fSAndroid Build Coastguard Worker 	if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) {
224*8b26181fSAndroid Build Coastguard Worker 		/*
225*8b26181fSAndroid Build Coastguard Worker 		 * XXX - there's probably a particular bind error that
226*8b26181fSAndroid Build Coastguard Worker 		 * means "there's no such device" and a particular bind
227*8b26181fSAndroid Build Coastguard Worker 		 * error that means "that device doesn't support snoop";
228*8b26181fSAndroid Build Coastguard Worker 		 * they might be the same error, if they both end up
229*8b26181fSAndroid Build Coastguard Worker 		 * meaning "snoop doesn't know about that device".
230*8b26181fSAndroid Build Coastguard Worker 		 */
231*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
232*8b26181fSAndroid Build Coastguard Worker 		    errno, "snoop bind");
233*8b26181fSAndroid Build Coastguard Worker 		goto bad;
234*8b26181fSAndroid Build Coastguard Worker 	}
235*8b26181fSAndroid Build Coastguard Worker 	memset(&sf, 0, sizeof(sf));
236*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) {
237*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
238*8b26181fSAndroid Build Coastguard Worker 		    errno, "SIOCADDSNOOP");
239*8b26181fSAndroid Build Coastguard Worker 		goto bad;
240*8b26181fSAndroid Build Coastguard Worker 	}
241*8b26181fSAndroid Build Coastguard Worker 	if (p->opt.buffer_size != 0)
242*8b26181fSAndroid Build Coastguard Worker 		v = p->opt.buffer_size;
243*8b26181fSAndroid Build Coastguard Worker 	else
244*8b26181fSAndroid Build Coastguard Worker 		v = 64 * 1024;	/* default to 64K buffer size */
245*8b26181fSAndroid Build Coastguard Worker 	(void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&v, sizeof(v));
246*8b26181fSAndroid Build Coastguard Worker 	/*
247*8b26181fSAndroid Build Coastguard Worker 	 * XXX hack - map device name to link layer type
248*8b26181fSAndroid Build Coastguard Worker 	 */
249*8b26181fSAndroid Build Coastguard Worker 	if (strncmp("et", p->opt.device, 2) == 0 ||	/* Challenge 10 Mbit */
250*8b26181fSAndroid Build Coastguard Worker 	    strncmp("ec", p->opt.device, 2) == 0 ||	/* Indigo/Indy 10 Mbit,
251*8b26181fSAndroid Build Coastguard Worker 							   O2 10/100 */
252*8b26181fSAndroid Build Coastguard Worker 	    strncmp("ef", p->opt.device, 2) == 0 ||	/* O200/2000 10/100 Mbit */
253*8b26181fSAndroid Build Coastguard Worker 	    strncmp("eg", p->opt.device, 2) == 0 ||	/* Octane/O2xxx/O3xxx Gigabit */
254*8b26181fSAndroid Build Coastguard Worker 	    strncmp("gfe", p->opt.device, 3) == 0 ||	/* GIO 100 Mbit */
255*8b26181fSAndroid Build Coastguard Worker 	    strncmp("fxp", p->opt.device, 3) == 0 ||	/* Challenge VME Enet */
256*8b26181fSAndroid Build Coastguard Worker 	    strncmp("ep", p->opt.device, 2) == 0 ||	/* Challenge 8x10 Mbit EPLEX */
257*8b26181fSAndroid Build Coastguard Worker 	    strncmp("vfe", p->opt.device, 3) == 0 ||	/* Challenge VME 100Mbit */
258*8b26181fSAndroid Build Coastguard Worker 	    strncmp("fa", p->opt.device, 2) == 0 ||
259*8b26181fSAndroid Build Coastguard Worker 	    strncmp("qaa", p->opt.device, 3) == 0 ||
260*8b26181fSAndroid Build Coastguard Worker 	    strncmp("cip", p->opt.device, 3) == 0 ||
261*8b26181fSAndroid Build Coastguard Worker 	    strncmp("el", p->opt.device, 2) == 0) {
262*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_EN10MB;
263*8b26181fSAndroid Build Coastguard Worker 		p->offset = RAW_HDRPAD(sizeof(struct ether_header));
264*8b26181fSAndroid Build Coastguard Worker 		ll_hdrlen = sizeof(struct ether_header);
265*8b26181fSAndroid Build Coastguard Worker 		/*
266*8b26181fSAndroid Build Coastguard Worker 		 * This is (presumably) a real Ethernet capture; give it a
267*8b26181fSAndroid Build Coastguard Worker 		 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
268*8b26181fSAndroid Build Coastguard Worker 		 * that an application can let you choose it, in case you're
269*8b26181fSAndroid Build Coastguard Worker 		 * capturing DOCSIS traffic that a Cisco Cable Modem
270*8b26181fSAndroid Build Coastguard Worker 		 * Termination System is putting out onto an Ethernet (it
271*8b26181fSAndroid Build Coastguard Worker 		 * doesn't put an Ethernet header onto the wire, it puts raw
272*8b26181fSAndroid Build Coastguard Worker 		 * DOCSIS frames out on the wire inside the low-level
273*8b26181fSAndroid Build Coastguard Worker 		 * Ethernet framing).
274*8b26181fSAndroid Build Coastguard Worker 		 *
275*8b26181fSAndroid Build Coastguard Worker 		 * XXX - are there any sorts of "fake Ethernet" that have
276*8b26181fSAndroid Build Coastguard Worker 		 * Ethernet link-layer headers but that *shouldn't offer
277*8b26181fSAndroid Build Coastguard Worker 		 * DLT_DOCSIS as a Cisco CMTS won't put traffic onto it
278*8b26181fSAndroid Build Coastguard Worker 		 * or get traffic bridged onto it?  "el" is for ATM LANE
279*8b26181fSAndroid Build Coastguard Worker 		 * Ethernet devices, so that might be the case for them;
280*8b26181fSAndroid Build Coastguard Worker 		 * the same applies for "qaa" classical IP devices.  If
281*8b26181fSAndroid Build Coastguard Worker 		 * "fa" devices are for FORE SPANS, that'd apply to them
282*8b26181fSAndroid Build Coastguard Worker 		 * as well; what are "cip" devices - some other ATM
283*8b26181fSAndroid Build Coastguard Worker 		 * Classical IP devices?
284*8b26181fSAndroid Build Coastguard Worker 		 */
285*8b26181fSAndroid Build Coastguard Worker 		p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
286*8b26181fSAndroid Build Coastguard Worker 		/*
287*8b26181fSAndroid Build Coastguard Worker 		 * If that fails, just leave the list empty.
288*8b26181fSAndroid Build Coastguard Worker 		 */
289*8b26181fSAndroid Build Coastguard Worker 		if (p->dlt_list != NULL) {
290*8b26181fSAndroid Build Coastguard Worker 			p->dlt_list[0] = DLT_EN10MB;
291*8b26181fSAndroid Build Coastguard Worker 			p->dlt_list[1] = DLT_DOCSIS;
292*8b26181fSAndroid Build Coastguard Worker 			p->dlt_count = 2;
293*8b26181fSAndroid Build Coastguard Worker 		}
294*8b26181fSAndroid Build Coastguard Worker 	} else if (strncmp("ipg", p->opt.device, 3) == 0 ||
295*8b26181fSAndroid Build Coastguard Worker 		   strncmp("rns", p->opt.device, 3) == 0 ||	/* O2/200/2000 FDDI */
296*8b26181fSAndroid Build Coastguard Worker 		   strncmp("xpi", p->opt.device, 3) == 0) {
297*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_FDDI;
298*8b26181fSAndroid Build Coastguard Worker 		p->offset = 3;				/* XXX yeah? */
299*8b26181fSAndroid Build Coastguard Worker 		ll_hdrlen = 13;
300*8b26181fSAndroid Build Coastguard Worker 	} else if (strncmp("ppp", p->opt.device, 3) == 0) {
301*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_RAW;
302*8b26181fSAndroid Build Coastguard Worker 		ll_hdrlen = 0;	/* DLT_RAW meaning "no PPP header, just the IP packet"? */
303*8b26181fSAndroid Build Coastguard Worker 	} else if (strncmp("qfa", p->opt.device, 3) == 0) {
304*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_IP_OVER_FC;
305*8b26181fSAndroid Build Coastguard Worker 		ll_hdrlen = 24;
306*8b26181fSAndroid Build Coastguard Worker 	} else if (strncmp("pl", p->opt.device, 2) == 0) {
307*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_RAW;
308*8b26181fSAndroid Build Coastguard Worker 		ll_hdrlen = 0;	/* Cray UNICOS/mp pseudo link */
309*8b26181fSAndroid Build Coastguard Worker 	} else if (strncmp("lo", p->opt.device, 2) == 0) {
310*8b26181fSAndroid Build Coastguard Worker 		p->linktype = DLT_NULL;
311*8b26181fSAndroid Build Coastguard Worker 		ll_hdrlen = 4;
312*8b26181fSAndroid Build Coastguard Worker 	} else {
313*8b26181fSAndroid Build Coastguard Worker 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
314*8b26181fSAndroid Build Coastguard Worker 		    "snoop: unknown physical layer type");
315*8b26181fSAndroid Build Coastguard Worker 		goto bad;
316*8b26181fSAndroid Build Coastguard Worker 	}
317*8b26181fSAndroid Build Coastguard Worker 
318*8b26181fSAndroid Build Coastguard Worker 	if (p->opt.rfmon) {
319*8b26181fSAndroid Build Coastguard Worker 		/*
320*8b26181fSAndroid Build Coastguard Worker 		 * No monitor mode on Irix (no Wi-Fi devices on
321*8b26181fSAndroid Build Coastguard Worker 		 * hardware supported by Irix).
322*8b26181fSAndroid Build Coastguard Worker 		 */
323*8b26181fSAndroid Build Coastguard Worker 		return (PCAP_ERROR_RFMON_NOTSUP);
324*8b26181fSAndroid Build Coastguard Worker 	}
325*8b26181fSAndroid Build Coastguard Worker 
326*8b26181fSAndroid Build Coastguard Worker 	/*
327*8b26181fSAndroid Build Coastguard Worker 	 * Turn a negative snapshot value (invalid), a snapshot value of
328*8b26181fSAndroid Build Coastguard Worker 	 * 0 (unspecified), or a value bigger than the normal maximum
329*8b26181fSAndroid Build Coastguard Worker 	 * value, into the maximum allowed value.
330*8b26181fSAndroid Build Coastguard Worker 	 *
331*8b26181fSAndroid Build Coastguard Worker 	 * If some application really *needs* a bigger snapshot
332*8b26181fSAndroid Build Coastguard Worker 	 * length, we should just increase MAXIMUM_SNAPLEN.
333*8b26181fSAndroid Build Coastguard Worker 	 */
334*8b26181fSAndroid Build Coastguard Worker 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
335*8b26181fSAndroid Build Coastguard Worker 		p->snapshot = MAXIMUM_SNAPLEN;
336*8b26181fSAndroid Build Coastguard Worker 
337*8b26181fSAndroid Build Coastguard Worker #ifdef SIOCGIFMTU
338*8b26181fSAndroid Build Coastguard Worker 	/*
339*8b26181fSAndroid Build Coastguard Worker 	 * XXX - IRIX appears to give you an error if you try to set the
340*8b26181fSAndroid Build Coastguard Worker 	 * capture length to be greater than the MTU, so let's try to get
341*8b26181fSAndroid Build Coastguard Worker 	 * the MTU first and, if that succeeds, trim the snap length
342*8b26181fSAndroid Build Coastguard Worker 	 * to be no greater than the MTU.
343*8b26181fSAndroid Build Coastguard Worker 	 */
344*8b26181fSAndroid Build Coastguard Worker 	(void)strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
345*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(fd, SIOCGIFMTU, (char *)&ifr) < 0) {
346*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
347*8b26181fSAndroid Build Coastguard Worker 		    errno, "SIOCGIFMTU");
348*8b26181fSAndroid Build Coastguard Worker 		goto bad;
349*8b26181fSAndroid Build Coastguard Worker 	}
350*8b26181fSAndroid Build Coastguard Worker 	/*
351*8b26181fSAndroid Build Coastguard Worker 	 * OK, we got it.
352*8b26181fSAndroid Build Coastguard Worker 	 *
353*8b26181fSAndroid Build Coastguard Worker 	 * XXX - some versions of IRIX 6.5 define "ifr_mtu" and have an
354*8b26181fSAndroid Build Coastguard Worker 	 * "ifru_metric" member of the "ifr_ifru" union in an "ifreq"
355*8b26181fSAndroid Build Coastguard Worker 	 * structure, others don't.
356*8b26181fSAndroid Build Coastguard Worker 	 *
357*8b26181fSAndroid Build Coastguard Worker 	 * I've no idea what's going on, so, if "ifr_mtu" isn't defined,
358*8b26181fSAndroid Build Coastguard Worker 	 * we define it as "ifr_metric", as using that field appears to
359*8b26181fSAndroid Build Coastguard Worker 	 * work on the versions that lack "ifr_mtu" (and, on those that
360*8b26181fSAndroid Build Coastguard Worker 	 * don't lack it, "ifru_metric" and "ifru_mtu" are both "int"
361*8b26181fSAndroid Build Coastguard Worker 	 * members of the "ifr_ifru" union, which suggests that they
362*8b26181fSAndroid Build Coastguard Worker 	 * may be interchangeable in this case).
363*8b26181fSAndroid Build Coastguard Worker 	 */
364*8b26181fSAndroid Build Coastguard Worker #ifndef ifr_mtu
365*8b26181fSAndroid Build Coastguard Worker #define ifr_mtu	ifr_metric
366*8b26181fSAndroid Build Coastguard Worker #endif
367*8b26181fSAndroid Build Coastguard Worker 	if (p->snapshot > ifr.ifr_mtu + ll_hdrlen)
368*8b26181fSAndroid Build Coastguard Worker 		p->snapshot = ifr.ifr_mtu + ll_hdrlen;
369*8b26181fSAndroid Build Coastguard Worker #endif
370*8b26181fSAndroid Build Coastguard Worker 
371*8b26181fSAndroid Build Coastguard Worker 	/*
372*8b26181fSAndroid Build Coastguard Worker 	 * The argument to SIOCSNOOPLEN is the number of link-layer
373*8b26181fSAndroid Build Coastguard Worker 	 * payload bytes to capture - it doesn't count link-layer
374*8b26181fSAndroid Build Coastguard Worker 	 * header bytes.
375*8b26181fSAndroid Build Coastguard Worker 	 */
376*8b26181fSAndroid Build Coastguard Worker 	snooplen = p->snapshot - ll_hdrlen;
377*8b26181fSAndroid Build Coastguard Worker 	if (snooplen < 0)
378*8b26181fSAndroid Build Coastguard Worker 		snooplen = 0;
379*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(fd, SIOCSNOOPLEN, &snooplen) < 0) {
380*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
381*8b26181fSAndroid Build Coastguard Worker 		    errno, "SIOCSNOOPLEN");
382*8b26181fSAndroid Build Coastguard Worker 		goto bad;
383*8b26181fSAndroid Build Coastguard Worker 	}
384*8b26181fSAndroid Build Coastguard Worker 	v = 1;
385*8b26181fSAndroid Build Coastguard Worker 	if (ioctl(fd, SIOCSNOOPING, &v) < 0) {
386*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
387*8b26181fSAndroid Build Coastguard Worker 		    errno, "SIOCSNOOPING");
388*8b26181fSAndroid Build Coastguard Worker 		goto bad;
389*8b26181fSAndroid Build Coastguard Worker 	}
390*8b26181fSAndroid Build Coastguard Worker 
391*8b26181fSAndroid Build Coastguard Worker 	p->bufsize = 4096;				/* XXX */
392*8b26181fSAndroid Build Coastguard Worker 	p->buffer = malloc(p->bufsize);
393*8b26181fSAndroid Build Coastguard Worker 	if (p->buffer == NULL) {
394*8b26181fSAndroid Build Coastguard Worker 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
395*8b26181fSAndroid Build Coastguard Worker 		    errno, "malloc");
396*8b26181fSAndroid Build Coastguard Worker 		goto bad;
397*8b26181fSAndroid Build Coastguard Worker 	}
398*8b26181fSAndroid Build Coastguard Worker 
399*8b26181fSAndroid Build Coastguard Worker 	/*
400*8b26181fSAndroid Build Coastguard Worker 	 * "p->fd" is a socket, so "select()" should work on it.
401*8b26181fSAndroid Build Coastguard Worker 	 */
402*8b26181fSAndroid Build Coastguard Worker 	p->selectable_fd = p->fd;
403*8b26181fSAndroid Build Coastguard Worker 
404*8b26181fSAndroid Build Coastguard Worker 	p->read_op = pcap_read_snoop;
405*8b26181fSAndroid Build Coastguard Worker 	p->inject_op = pcap_inject_snoop;
406*8b26181fSAndroid Build Coastguard Worker 	p->setfilter_op = install_bpf_program;	/* no kernel filtering */
407*8b26181fSAndroid Build Coastguard Worker 	p->setdirection_op = NULL;	/* Not implemented. */
408*8b26181fSAndroid Build Coastguard Worker 	p->set_datalink_op = NULL;	/* can't change data link type */
409*8b26181fSAndroid Build Coastguard Worker 	p->getnonblock_op = pcap_getnonblock_fd;
410*8b26181fSAndroid Build Coastguard Worker 	p->setnonblock_op = pcap_setnonblock_fd;
411*8b26181fSAndroid Build Coastguard Worker 	p->stats_op = pcap_stats_snoop;
412*8b26181fSAndroid Build Coastguard Worker 
413*8b26181fSAndroid Build Coastguard Worker 	return (0);
414*8b26181fSAndroid Build Coastguard Worker  bad:
415*8b26181fSAndroid Build Coastguard Worker 	pcap_cleanup_live_common(p);
416*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_ERROR);
417*8b26181fSAndroid Build Coastguard Worker }
418*8b26181fSAndroid Build Coastguard Worker 
419*8b26181fSAndroid Build Coastguard Worker pcap_t *
pcap_create_interface(const char * device _U_,char * ebuf)420*8b26181fSAndroid Build Coastguard Worker pcap_create_interface(const char *device _U_, char *ebuf)
421*8b26181fSAndroid Build Coastguard Worker {
422*8b26181fSAndroid Build Coastguard Worker 	pcap_t *p;
423*8b26181fSAndroid Build Coastguard Worker 
424*8b26181fSAndroid Build Coastguard Worker 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_snoop);
425*8b26181fSAndroid Build Coastguard Worker 	if (p == NULL)
426*8b26181fSAndroid Build Coastguard Worker 		return (NULL);
427*8b26181fSAndroid Build Coastguard Worker 
428*8b26181fSAndroid Build Coastguard Worker 	p->activate_op = pcap_activate_snoop;
429*8b26181fSAndroid Build Coastguard Worker 	return (p);
430*8b26181fSAndroid Build Coastguard Worker }
431*8b26181fSAndroid Build Coastguard Worker 
432*8b26181fSAndroid Build Coastguard Worker /*
433*8b26181fSAndroid Build Coastguard Worker  * XXX - there's probably a particular bind error that means "that device
434*8b26181fSAndroid Build Coastguard Worker  * doesn't support snoop"; if so, we should try a bind and use that.
435*8b26181fSAndroid Build Coastguard Worker  */
436*8b26181fSAndroid Build Coastguard Worker static int
can_be_bound(const char * name _U_)437*8b26181fSAndroid Build Coastguard Worker can_be_bound(const char *name _U_)
438*8b26181fSAndroid Build Coastguard Worker {
439*8b26181fSAndroid Build Coastguard Worker 	return (1);
440*8b26181fSAndroid Build Coastguard Worker }
441*8b26181fSAndroid Build Coastguard Worker 
442*8b26181fSAndroid Build Coastguard Worker static int
get_if_flags(const char * name _U_,bpf_u_int32 * flags _U_,char * errbuf _U_)443*8b26181fSAndroid Build Coastguard Worker get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
444*8b26181fSAndroid Build Coastguard Worker {
445*8b26181fSAndroid Build Coastguard Worker 	/*
446*8b26181fSAndroid Build Coastguard Worker 	 * Nothing we can do.
447*8b26181fSAndroid Build Coastguard Worker 	 * XXX - is there a way to find out whether an adapter has
448*8b26181fSAndroid Build Coastguard Worker 	 * something plugged into it?
449*8b26181fSAndroid Build Coastguard Worker 	 */
450*8b26181fSAndroid Build Coastguard Worker 	return (0);
451*8b26181fSAndroid Build Coastguard Worker }
452*8b26181fSAndroid Build Coastguard Worker 
453*8b26181fSAndroid Build Coastguard Worker int
pcap_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)454*8b26181fSAndroid Build Coastguard Worker pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
455*8b26181fSAndroid Build Coastguard Worker {
456*8b26181fSAndroid Build Coastguard Worker 	return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
457*8b26181fSAndroid Build Coastguard Worker 	    get_if_flags));
458*8b26181fSAndroid Build Coastguard Worker }
459*8b26181fSAndroid Build Coastguard Worker 
460*8b26181fSAndroid Build Coastguard Worker /*
461*8b26181fSAndroid Build Coastguard Worker  * Libpcap version string.
462*8b26181fSAndroid Build Coastguard Worker  */
463*8b26181fSAndroid Build Coastguard Worker const char *
pcap_lib_version(void)464*8b26181fSAndroid Build Coastguard Worker pcap_lib_version(void)
465*8b26181fSAndroid Build Coastguard Worker {
466*8b26181fSAndroid Build Coastguard Worker 	return (PCAP_VERSION_STRING);
467*8b26181fSAndroid Build Coastguard Worker }
468