pcap-snoop.c revision 214518
116880Sgpalmer/*
216880Sgpalmer * Copyright (c) 1993, 1994, 1995, 1996, 1997
316880Sgpalmer *	The Regents of the University of California.  All rights reserved.
416880Sgpalmer *
516880Sgpalmer * Redistribution and use in source and binary forms, with or without
616880Sgpalmer * modification, are permitted provided that: (1) source code distributions
716880Sgpalmer * retain the above copyright notice and this paragraph in its entirety, (2)
816880Sgpalmer * distributions including binary code include the above copyright notice and
916880Sgpalmer * this paragraph in its entirety in the documentation or other materials
1016880Sgpalmer * provided with the distribution, and (3) all advertising materials mentioning
1116880Sgpalmer * features or use of this software display the following acknowledgement:
1216880Sgpalmer * ``This product includes software developed by the University of California,
1316880Sgpalmer * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1416880Sgpalmer * the University nor the names of its contributors may be used to endorse
1516880Sgpalmer * or promote products derived from this software without specific prior
1616880Sgpalmer * written permission.
1716880Sgpalmer * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1816880Sgpalmer * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1916880Sgpalmer * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2016880Sgpalmer */
2116880Sgpalmer#ifndef lint
2216880Sgpalmerstatic const char rcsid[] _U_ =
2316880Sgpalmer    "@(#) $Header: /tcpdump/master/libpcap/pcap-snoop.c,v 1.59 2008-12-02 16:25:14 guy Exp $ (LBL)";
2416880Sgpalmer#endif
2516880Sgpalmer
2616880Sgpalmer#ifdef HAVE_CONFIG_H
2716880Sgpalmer#include "config.h"
2816880Sgpalmer#endif
2916880Sgpalmer
3016880Sgpalmer#include <sys/param.h>
3116880Sgpalmer#include <sys/file.h>
3216880Sgpalmer#include <sys/ioctl.h>
3316880Sgpalmer#include <sys/socket.h>
3416880Sgpalmer#include <sys/time.h>
3516880Sgpalmer
3616880Sgpalmer#include <net/raw.h>
3716880Sgpalmer#include <net/if.h>
3816880Sgpalmer
3916880Sgpalmer#include <netinet/in.h>
4016880Sgpalmer#include <netinet/in_systm.h>
4116880Sgpalmer#include <netinet/ip.h>
4216880Sgpalmer#include <netinet/if_ether.h>
4316880Sgpalmer#include <netinet/ip_var.h>
4416880Sgpalmer#include <netinet/udp.h>
4516880Sgpalmer#include <netinet/udp_var.h>
4616880Sgpalmer#include <netinet/tcp.h>
4716880Sgpalmer#include <netinet/tcpip.h>
4816880Sgpalmer
4916880Sgpalmer#include <errno.h>
5016880Sgpalmer#include <stdio.h>
5116880Sgpalmer#include <stdlib.h>
5216880Sgpalmer#include <string.h>
5316880Sgpalmer#include <unistd.h>
5416880Sgpalmer
5516880Sgpalmer#include "pcap-int.h"
5616880Sgpalmer
5716880Sgpalmer#ifdef HAVE_OS_PROTO_H
5816880Sgpalmer#include "os-proto.h"
5916880Sgpalmer#endif
6016880Sgpalmer
6116880Sgpalmerstatic int
6216880Sgpalmerpcap_read_snoop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
6316880Sgpalmer{
6416880Sgpalmer	int cc;
6516880Sgpalmer	register struct snoopheader *sh;
6616880Sgpalmer	register u_int datalen;
6716880Sgpalmer	register u_int caplen;
6816880Sgpalmer	register u_char *cp;
6916880Sgpalmer
7016880Sgpalmeragain:
7116880Sgpalmer	/*
7216880Sgpalmer	 * Has "pcap_breakloop()" been called?
7316880Sgpalmer	 */
7416880Sgpalmer	if (p->break_loop) {
7516880Sgpalmer		/*
7616880Sgpalmer		 * Yes - clear the flag that indicates that it
7716880Sgpalmer		 * has, and return -2 to indicate that we were
7816880Sgpalmer		 * told to break out of the loop.
7916880Sgpalmer		 */
8016880Sgpalmer		p->break_loop = 0;
8116880Sgpalmer		return (-2);
8216880Sgpalmer	}
8316880Sgpalmer	cc = read(p->fd, (char *)p->buffer, p->bufsize);
8416880Sgpalmer	if (cc < 0) {
8516880Sgpalmer		/* Don't choke when we get ptraced */
8616880Sgpalmer		switch (errno) {
8716880Sgpalmer
8816880Sgpalmer		case EINTR:
8916880Sgpalmer			goto again;
9016880Sgpalmer
9116880Sgpalmer		case EWOULDBLOCK:
9216880Sgpalmer			return (0);			/* XXX */
9316880Sgpalmer		}
9416880Sgpalmer		snprintf(p->errbuf, sizeof(p->errbuf),
9516880Sgpalmer		    "read: %s", pcap_strerror(errno));
9616880Sgpalmer		return (-1);
9716880Sgpalmer	}
9816880Sgpalmer	sh = (struct snoopheader *)p->buffer;
9916880Sgpalmer	datalen = sh->snoop_packetlen;
10016880Sgpalmer
10116880Sgpalmer	/*
10216880Sgpalmer	 * XXX - Sigh, snoop_packetlen is a 16 bit quantity.  If we
10316880Sgpalmer	 * got a short length, but read a full sized snoop pakcet,
10416880Sgpalmer	 * assume we overflowed and add back the 64K...
10516880Sgpalmer	 */
10616880Sgpalmer	if (cc == (p->snapshot + sizeof(struct snoopheader)) &&
10716880Sgpalmer	    (datalen < p->snapshot))
10816880Sgpalmer		datalen += (64 * 1024);
10916880Sgpalmer
11016880Sgpalmer	caplen = (datalen < p->snapshot) ? datalen : p->snapshot;
11116880Sgpalmer	cp = (u_char *)(sh + 1) + p->offset;		/* XXX */
11216880Sgpalmer
11316880Sgpalmer	/*
11416880Sgpalmer	 * XXX unfortunately snoop loopback isn't exactly like
11516880Sgpalmer	 * BSD's.  The address family is encoded in the first 2
11616880Sgpalmer	 * bytes rather than the first 4 bytes!  Luckily the last
11716880Sgpalmer	 * two snoop loopback bytes are zeroed.
11816880Sgpalmer	 */
11916880Sgpalmer	if (p->linktype == DLT_NULL && *((short *)(cp + 2)) == 0) {
12016880Sgpalmer		u_int *uip = (u_int *)cp;
12116880Sgpalmer		*uip >>= 16;
12216880Sgpalmer	}
12316880Sgpalmer
12416880Sgpalmer	if (p->fcode.bf_insns == NULL ||
12516880Sgpalmer	    bpf_filter(p->fcode.bf_insns, cp, datalen, caplen)) {
12616880Sgpalmer		struct pcap_pkthdr h;
12716880Sgpalmer		++p->md.stat.ps_recv;
12816880Sgpalmer		h.ts.tv_sec = sh->snoop_timestamp.tv_sec;
12916880Sgpalmer		h.ts.tv_usec = sh->snoop_timestamp.tv_usec;
13016880Sgpalmer		h.len = datalen;
13116880Sgpalmer		h.caplen = caplen;
13216880Sgpalmer		(*callback)(user, &h, cp);
13316880Sgpalmer		return (1);
13416880Sgpalmer	}
13516880Sgpalmer	return (0);
13616880Sgpalmer}
13716880Sgpalmer
13816880Sgpalmerstatic int
13916880Sgpalmerpcap_inject_snoop(pcap_t *p, const void *buf, size_t size)
14016880Sgpalmer{
14116880Sgpalmer	int ret;
14216880Sgpalmer
14316880Sgpalmer	/*
14416880Sgpalmer	 * XXX - libnet overwrites the source address with what I
14516880Sgpalmer	 * presume is the interface's address; is that required?
14616880Sgpalmer	 */
14716880Sgpalmer	ret = write(p->fd, buf, size);
14816880Sgpalmer	if (ret == -1) {
14916880Sgpalmer		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
15016880Sgpalmer		    pcap_strerror(errno));
15116880Sgpalmer		return (-1);
15216880Sgpalmer	}
15316880Sgpalmer	return (ret);
15416880Sgpalmer}
15516880Sgpalmer
15616880Sgpalmerstatic int
15716880Sgpalmerpcap_stats_snoop(pcap_t *p, struct pcap_stat *ps)
15816880Sgpalmer{
15916880Sgpalmer	register struct rawstats *rs;
16016880Sgpalmer	struct rawstats rawstats;
16116880Sgpalmer
16216880Sgpalmer	rs = &rawstats;
16316880Sgpalmer	memset(rs, 0, sizeof(*rs));
16416880Sgpalmer	if (ioctl(p->fd, SIOCRAWSTATS, (char *)rs) < 0) {
16516880Sgpalmer		snprintf(p->errbuf, sizeof(p->errbuf),
16616880Sgpalmer		    "SIOCRAWSTATS: %s", pcap_strerror(errno));
16716880Sgpalmer		return (-1);
16816880Sgpalmer	}
16916880Sgpalmer
17016880Sgpalmer	/*
17116880Sgpalmer	 * "ifdrops" are those dropped by the network interface
17216880Sgpalmer	 * due to resource shortages or hardware errors.
17316880Sgpalmer	 *
17416880Sgpalmer	 * "sbdrops" are those dropped due to socket buffer limits.
17516880Sgpalmer	 *
17616880Sgpalmer	 * As filter is done in userland, "sbdrops" counts packets
17716880Sgpalmer	 * regardless of whether they would've passed the filter.
17816880Sgpalmer	 *
17916880Sgpalmer	 * XXX - does this count *all* Snoop or Drain sockets,
18016880Sgpalmer	 * rather than just this socket?  If not, why does it have
18116880Sgpalmer	 * both Snoop and Drain statistics?
18216880Sgpalmer	 */
18316880Sgpalmer	p->md.stat.ps_drop =
18416880Sgpalmer	    rs->rs_snoop.ss_ifdrops + rs->rs_snoop.ss_sbdrops +
18516880Sgpalmer	    rs->rs_drain.ds_ifdrops + rs->rs_drain.ds_sbdrops;
18616880Sgpalmer
18716880Sgpalmer	/*
18816880Sgpalmer	 * "ps_recv" counts only packets that passed the filter.
18916880Sgpalmer	 * As filtering is done in userland, this does not include
19016880Sgpalmer	 * packets dropped because we ran out of buffer space.
19116880Sgpalmer	 */
19216880Sgpalmer	*ps = p->md.stat;
19316880Sgpalmer	return (0);
19416880Sgpalmer}
19516880Sgpalmer
19616880Sgpalmer/* XXX can't disable promiscuous */
19716880Sgpalmerstatic int
19816880Sgpalmerpcap_activate_snoop(pcap_t *p)
19916880Sgpalmer{
20016880Sgpalmer	int fd;
20116880Sgpalmer	struct sockaddr_raw sr;
20216880Sgpalmer	struct snoopfilter sf;
20316880Sgpalmer	u_int v;
20416880Sgpalmer	int ll_hdrlen;
20516880Sgpalmer	int snooplen;
20616880Sgpalmer	struct ifreq ifr;
20716880Sgpalmer
20816880Sgpalmer	fd = socket(PF_RAW, SOCK_RAW, RAWPROTO_SNOOP);
20916880Sgpalmer	if (fd < 0) {
21016880Sgpalmer		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snoop socket: %s",
21116880Sgpalmer		    pcap_strerror(errno));
21216880Sgpalmer		goto bad;
21316880Sgpalmer	}
21416880Sgpalmer	p->fd = fd;
21516880Sgpalmer	memset(&sr, 0, sizeof(sr));
21616880Sgpalmer	sr.sr_family = AF_RAW;
21716880Sgpalmer	(void)strncpy(sr.sr_ifname, p->opt.source, sizeof(sr.sr_ifname));
21816880Sgpalmer	if (bind(fd, (struct sockaddr *)&sr, sizeof(sr))) {
21916880Sgpalmer		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "snoop bind: %s",
22016880Sgpalmer		    pcap_strerror(errno));
22116880Sgpalmer		goto bad;
22216880Sgpalmer	}
22316880Sgpalmer	memset(&sf, 0, sizeof(sf));
22416880Sgpalmer	if (ioctl(fd, SIOCADDSNOOP, &sf) < 0) {
225		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCADDSNOOP: %s",
226		    pcap_strerror(errno));
227		goto bad;
228	}
229	if (p->opt.buffer_size != 0)
230		v = p->opt.buffer_size;
231	else
232		v = 64 * 1024;	/* default to 64K buffer size */
233	(void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&v, sizeof(v));
234	/*
235	 * XXX hack - map device name to link layer type
236	 */
237	if (strncmp("et", p->opt.source, 2) == 0 ||	/* Challenge 10 Mbit */
238	    strncmp("ec", p->opt.source, 2) == 0 ||	/* Indigo/Indy 10 Mbit,
239							   O2 10/100 */
240	    strncmp("ef", p->opt.source, 2) == 0 ||	/* O200/2000 10/100 Mbit */
241	    strncmp("eg", p->opt.source, 2) == 0 ||	/* Octane/O2xxx/O3xxx Gigabit */
242	    strncmp("gfe", p->opt.source, 3) == 0 ||	/* GIO 100 Mbit */
243	    strncmp("fxp", p->opt.source, 3) == 0 ||	/* Challenge VME Enet */
244	    strncmp("ep", p->opt.source, 2) == 0 ||	/* Challenge 8x10 Mbit EPLEX */
245	    strncmp("vfe", p->opt.source, 3) == 0 ||	/* Challenge VME 100Mbit */
246	    strncmp("fa", p->opt.source, 2) == 0 ||
247	    strncmp("qaa", p->opt.source, 3) == 0 ||
248	    strncmp("cip", p->opt.source, 3) == 0 ||
249	    strncmp("el", p->opt.source, 2) == 0) {
250		p->linktype = DLT_EN10MB;
251		p->offset = RAW_HDRPAD(sizeof(struct ether_header));
252		ll_hdrlen = sizeof(struct ether_header);
253		/*
254		 * This is (presumably) a real Ethernet capture; give it a
255		 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
256		 * that an application can let you choose it, in case you're
257		 * capturing DOCSIS traffic that a Cisco Cable Modem
258		 * Termination System is putting out onto an Ethernet (it
259		 * doesn't put an Ethernet header onto the wire, it puts raw
260		 * DOCSIS frames out on the wire inside the low-level
261		 * Ethernet framing).
262		 *
263		 * XXX - are there any sorts of "fake Ethernet" that have
264		 * Ethernet link-layer headers but that *shouldn't offer
265		 * DLT_DOCSIS as a Cisco CMTS won't put traffic onto it
266		 * or get traffic bridged onto it?  "el" is for ATM LANE
267		 * Ethernet devices, so that might be the case for them;
268		 * the same applies for "qaa" classical IP devices.  If
269		 * "fa" devices are for FORE SPANS, that'd apply to them
270		 * as well; what are "cip" devices - some other ATM
271		 * Classical IP devices?
272		 */
273		p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
274		/*
275		 * If that fails, just leave the list empty.
276		 */
277		if (p->dlt_list != NULL) {
278			p->dlt_list[0] = DLT_EN10MB;
279			p->dlt_list[1] = DLT_DOCSIS;
280			p->dlt_count = 2;
281		}
282	} else if (strncmp("ipg", p->opt.source, 3) == 0 ||
283		   strncmp("rns", p->opt.source, 3) == 0 ||	/* O2/200/2000 FDDI */
284		   strncmp("xpi", p->opt.source, 3) == 0) {
285		p->linktype = DLT_FDDI;
286		p->offset = 3;				/* XXX yeah? */
287		ll_hdrlen = 13;
288	} else if (strncmp("ppp", p->opt.source, 3) == 0) {
289		p->linktype = DLT_RAW;
290		ll_hdrlen = 0;	/* DLT_RAW meaning "no PPP header, just the IP packet"? */
291	} else if (strncmp("qfa", p->opt.source, 3) == 0) {
292		p->linktype = DLT_IP_OVER_FC;
293		ll_hdrlen = 24;
294	} else if (strncmp("pl", p->opt.source, 2) == 0) {
295		p->linktype = DLT_RAW;
296		ll_hdrlen = 0;	/* Cray UNICOS/mp pseudo link */
297	} else if (strncmp("lo", p->opt.source, 2) == 0) {
298		p->linktype = DLT_NULL;
299		ll_hdrlen = 4;
300	} else {
301		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
302		    "snoop: unknown physical layer type");
303		goto bad;
304	}
305
306	if (p->opt.rfmon) {
307		/*
308		 * No monitor mode on Irix (no Wi-Fi devices on
309		 * hardware supported by Irix).
310		 */
311		return (PCAP_ERROR_RFMON_NOTSUP);
312	}
313
314#ifdef SIOCGIFMTU
315	/*
316	 * XXX - IRIX appears to give you an error if you try to set the
317	 * capture length to be greater than the MTU, so let's try to get
318	 * the MTU first and, if that succeeds, trim the snap length
319	 * to be no greater than the MTU.
320	 */
321	(void)strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name));
322	if (ioctl(fd, SIOCGIFMTU, (char *)&ifr) < 0) {
323		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCGIFMTU: %s",
324		    pcap_strerror(errno));
325		goto bad;
326	}
327	/*
328	 * OK, we got it.
329	 *
330	 * XXX - some versions of IRIX 6.5 define "ifr_mtu" and have an
331	 * "ifru_metric" member of the "ifr_ifru" union in an "ifreq"
332	 * structure, others don't.
333	 *
334	 * I've no idea what's going on, so, if "ifr_mtu" isn't defined,
335	 * we define it as "ifr_metric", as using that field appears to
336	 * work on the versions that lack "ifr_mtu" (and, on those that
337	 * don't lack it, "ifru_metric" and "ifru_mtu" are both "int"
338	 * members of the "ifr_ifru" union, which suggests that they
339	 * may be interchangeable in this case).
340	 */
341#ifndef ifr_mtu
342#define ifr_mtu	ifr_metric
343#endif
344	if (p->snapshot > ifr.ifr_mtu + ll_hdrlen)
345		p->snapshot = ifr.ifr_mtu + ll_hdrlen;
346#endif
347
348	/*
349	 * The argument to SIOCSNOOPLEN is the number of link-layer
350	 * payload bytes to capture - it doesn't count link-layer
351	 * header bytes.
352	 */
353	snooplen = p->snapshot - ll_hdrlen;
354	if (snooplen < 0)
355		snooplen = 0;
356	if (ioctl(fd, SIOCSNOOPLEN, &snooplen) < 0) {
357		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPLEN: %s",
358		    pcap_strerror(errno));
359		goto bad;
360	}
361	v = 1;
362	if (ioctl(fd, SIOCSNOOPING, &v) < 0) {
363		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNOOPING: %s",
364		    pcap_strerror(errno));
365		goto bad;
366	}
367
368	p->bufsize = 4096;				/* XXX */
369	p->buffer = (u_char *)malloc(p->bufsize);
370	if (p->buffer == NULL) {
371		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "malloc: %s",
372		    pcap_strerror(errno));
373		goto bad;
374	}
375
376	/*
377	 * "p->fd" is a socket, so "select()" should work on it.
378	 */
379	p->selectable_fd = p->fd;
380
381	p->read_op = pcap_read_snoop;
382	p->inject_op = pcap_inject_snoop;
383	p->setfilter_op = install_bpf_program;	/* no kernel filtering */
384	p->setdirection_op = NULL;	/* Not implemented. */
385	p->set_datalink_op = NULL;	/* can't change data link type */
386	p->getnonblock_op = pcap_getnonblock_fd;
387	p->setnonblock_op = pcap_setnonblock_fd;
388	p->stats_op = pcap_stats_snoop;
389
390	return (0);
391 bad:
392	pcap_cleanup_live_common(p);
393	return (PCAP_ERROR);
394}
395
396pcap_t *
397pcap_create(const char *device, char *ebuf)
398{
399	pcap_t *p;
400
401	p = pcap_create_common(device, ebuf);
402	if (p == NULL)
403		return (NULL);
404
405	p->activate_op = pcap_activate_snoop;
406	return (p);
407}
408
409int
410pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
411{
412	return (0);
413}
414