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