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