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