pcap-snit.c revision 75107
117683Spst/*
217683Spst * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
317683Spst *	The Regents of the University of California.  All rights reserved.
417683Spst *
517683Spst * Redistribution and use in source and binary forms, with or without
617683Spst * modification, are permitted provided that: (1) source code distributions
717683Spst * retain the above copyright notice and this paragraph in its entirety, (2)
817683Spst * distributions including binary code include the above copyright notice and
917683Spst * this paragraph in its entirety in the documentation or other materials
1017683Spst * provided with the distribution, and (3) all advertising materials mentioning
1117683Spst * features or use of this software display the following acknowledgement:
1217683Spst * ``This product includes software developed by the University of California,
1317683Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
1417683Spst * the University nor the names of its contributors may be used to endorse
1517683Spst * or promote products derived from this software without specific prior
1617683Spst * written permission.
1717683Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
1817683Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
1917683Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2026175Sfenner *
2117683Spst * Modifications made to accommodate the new SunOS4.0 NIT facility by
2217683Spst * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
2317683Spst * This module now handles the STREAMS based NIT.
2417683Spst */
2517683Spst
2626175Sfenner#ifndef lint
2726175Sfennerstatic const char rcsid[] =
2875107Sfenner    "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.54 2000/10/28 00:01:30 guy Exp $ (LBL)";
2926175Sfenner#endif
3026175Sfenner
3175107Sfenner#ifdef HAVE_CONFIG_H
3275107Sfenner#include "config.h"
3375107Sfenner#endif
3475107Sfenner
3517683Spst#include <sys/types.h>
3617683Spst#include <sys/time.h>
3717683Spst#include <sys/timeb.h>
3817683Spst#include <sys/dir.h>
3917683Spst#include <sys/fcntlcom.h>
4017683Spst#include <sys/file.h>
4117683Spst#include <sys/ioctl.h>
4217683Spst#include <sys/socket.h>
4317683Spst#include <sys/stropts.h>
4417683Spst
4517683Spst#include <net/if.h>
4617683Spst#include <net/nit.h>
4717683Spst#include <net/nit_if.h>
4817683Spst#include <net/nit_pf.h>
4917683Spst#include <net/nit_buf.h>
5017683Spst
5117683Spst#include <netinet/in.h>
5217683Spst#include <netinet/in_systm.h>
5317683Spst#include <netinet/ip.h>
5417683Spst#include <netinet/if_ether.h>
5517683Spst#include <netinet/ip_var.h>
5617683Spst#include <netinet/udp.h>
5717683Spst#include <netinet/udp_var.h>
5817683Spst#include <netinet/tcp.h>
5917683Spst#include <netinet/tcpip.h>
6017683Spst
6117683Spst#include <ctype.h>
6217683Spst#include <errno.h>
6317683Spst#include <stdio.h>
6417683Spst#include <string.h>
6517683Spst#include <unistd.h>
6617683Spst
6717683Spst#include "pcap-int.h"
6817683Spst
6917683Spst#ifdef HAVE_OS_PROTO_H
7017683Spst#include "os-proto.h"
7117683Spst#endif
7217683Spst
7317683Spst/*
7417683Spst * The chunk size for NIT.  This is the amount of buffering
7517683Spst * done for read calls.
7617683Spst */
7717683Spst#define CHUNKSIZE (2*1024)
7817683Spst
7917683Spst/*
8017683Spst * The total buffer space used by NIT.
8117683Spst */
8217683Spst#define BUFSPACE (4*CHUNKSIZE)
8317683Spst
8417683Spst/* Forwards */
8517683Spststatic int nit_setflags(int, int, int, char *);
8617683Spst
8717683Spstint
8817683Spstpcap_stats(pcap_t *p, struct pcap_stat *ps)
8917683Spst{
9017683Spst
9117683Spst	*ps = p->md.stat;
9217683Spst	return (0);
9317683Spst}
9417683Spst
9517683Spstint
9617683Spstpcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
9717683Spst{
9817683Spst	register int cc, n;
9917683Spst	register struct bpf_insn *fcode = p->fcode.bf_insns;
10017683Spst	register u_char *bp, *cp, *ep;
10117683Spst	register struct nit_bufhdr *hdrp;
10217683Spst	register struct nit_iftime *ntp;
10317683Spst	register struct nit_iflen *nlp;
10417683Spst	register struct nit_ifdrops *ndp;
10517683Spst	register int caplen;
10617683Spst
10717683Spst	cc = p->cc;
10817683Spst	if (cc == 0) {
10917683Spst		cc = read(p->fd, (char *)p->buffer, p->bufsize);
11017683Spst		if (cc < 0) {
11117683Spst			if (errno == EWOULDBLOCK)
11217683Spst				return (0);
11375107Sfenner			snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
11417683Spst				pcap_strerror(errno));
11517683Spst			return (-1);
11617683Spst		}
11717683Spst		bp = p->buffer;
11817683Spst	} else
11917683Spst		bp = p->bp;
12017683Spst
12117683Spst	/*
12217683Spst	 * loop through each snapshot in the chunk
12317683Spst	 */
12417683Spst	n = 0;
12517683Spst	ep = bp + cc;
12617683Spst	while (bp < ep) {
12717683Spst		++p->md.stat.ps_recv;
12817683Spst		cp = bp;
12917683Spst
13017683Spst		/* get past NIT buffer  */
13117683Spst		hdrp = (struct nit_bufhdr *)cp;
13217683Spst		cp += sizeof(*hdrp);
13317683Spst
13417683Spst		/* get past NIT timer   */
13517683Spst		ntp = (struct nit_iftime *)cp;
13617683Spst		cp += sizeof(*ntp);
13717683Spst
13817683Spst		ndp = (struct nit_ifdrops *)cp;
13917683Spst		p->md.stat.ps_drop = ndp->nh_drops;
14017683Spst		cp += sizeof *ndp;
14117683Spst
14217683Spst		/* get past packet len  */
14317683Spst		nlp = (struct nit_iflen *)cp;
14417683Spst		cp += sizeof(*nlp);
14517683Spst
14617683Spst		/* next snapshot        */
14717683Spst		bp += hdrp->nhb_totlen;
14817683Spst
14917683Spst		caplen = nlp->nh_pktlen;
15017683Spst		if (caplen > p->snapshot)
15117683Spst			caplen = p->snapshot;
15217683Spst
15317683Spst		if (bpf_filter(fcode, cp, nlp->nh_pktlen, caplen)) {
15417683Spst			struct pcap_pkthdr h;
15517683Spst			h.ts = ntp->nh_timestamp;
15617683Spst			h.len = nlp->nh_pktlen;
15717683Spst			h.caplen = caplen;
15817683Spst			(*callback)(user, &h, cp);
15917683Spst			if (++n >= cnt && cnt >= 0) {
16017683Spst				p->cc = ep - bp;
16117683Spst				p->bp = bp;
16217683Spst				return (n);
16317683Spst			}
16417683Spst		}
16517683Spst	}
16617683Spst	p->cc = 0;
16717683Spst	return (n);
16817683Spst}
16917683Spst
17017683Spststatic int
17117683Spstnit_setflags(int fd, int promisc, int to_ms, char *ebuf)
17217683Spst{
17317683Spst	bpf_u_int32 flags;
17417683Spst	struct strioctl si;
17517683Spst	struct timeval timeout;
17617683Spst
17717683Spst	si.ic_timout = INFTIM;
17817683Spst	if (to_ms != 0) {
17917683Spst		timeout.tv_sec = to_ms / 1000;
18017683Spst		timeout.tv_usec = (to_ms * 1000) % 1000000;
18117683Spst		si.ic_cmd = NIOCSTIME;
18217683Spst		si.ic_len = sizeof(timeout);
18317683Spst		si.ic_dp = (char *)&timeout;
18417683Spst		if (ioctl(fd, I_STR, (char *)&si) < 0) {
18575107Sfenner			snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s",
18675107Sfenner			    pcap_strerror(errno));
18717683Spst			return (-1);
18817683Spst		}
18917683Spst	}
19017683Spst	flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
19117683Spst	if (promisc)
19217683Spst		flags |= NI_PROMISC;
19317683Spst	si.ic_cmd = NIOCSFLAGS;
19417683Spst	si.ic_len = sizeof(flags);
19517683Spst	si.ic_dp = (char *)&flags;
19617683Spst	if (ioctl(fd, I_STR, (char *)&si) < 0) {
19775107Sfenner		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s",
19875107Sfenner		    pcap_strerror(errno));
19917683Spst		return (-1);
20017683Spst	}
20117683Spst	return (0);
20217683Spst}
20317683Spst
20417683Spstpcap_t *
20517683Spstpcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
20617683Spst{
20717683Spst	struct strioctl si;		/* struct for ioctl() */
20817683Spst	struct ifreq ifr;		/* interface request struct */
20917683Spst	int chunksize = CHUNKSIZE;
21017683Spst	int fd;
21117683Spst	static char dev[] = "/dev/nit";
21217683Spst	register pcap_t *p;
21317683Spst
21417683Spst	p = (pcap_t *)malloc(sizeof(*p));
21517683Spst	if (p == NULL) {
21675107Sfenner		strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
21717683Spst		return (NULL);
21817683Spst	}
21917683Spst
22017683Spst	if (snaplen < 96)
22117683Spst		/*
22217683Spst		 * NIT requires a snapshot length of at least 96.
22317683Spst		 */
22417683Spst		snaplen = 96;
22517683Spst
22675107Sfenner	memset(p, 0, sizeof(*p));
22717683Spst	p->fd = fd = open(dev, O_RDONLY);
22817683Spst	if (fd < 0) {
22975107Sfenner		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dev,
23075107Sfenner		    pcap_strerror(errno));
23117683Spst		goto bad;
23217683Spst	}
23317683Spst
23417683Spst	/* arrange to get discrete messages from the STREAM and use NIT_BUF */
23517683Spst	if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) {
23675107Sfenner		snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s",
23775107Sfenner		    pcap_strerror(errno));
23817683Spst		goto bad;
23917683Spst	}
24017683Spst	if (ioctl(fd, I_PUSH, "nbuf") < 0) {
24175107Sfenner		snprintf(ebuf, PCAP_ERRBUF_SIZE, "push nbuf: %s",
24275107Sfenner		    pcap_strerror(errno));
24317683Spst		goto bad;
24417683Spst	}
24517683Spst	/* set the chunksize */
24617683Spst	si.ic_cmd = NIOCSCHUNK;
24717683Spst	si.ic_timout = INFTIM;
24817683Spst	si.ic_len = sizeof(chunksize);
24917683Spst	si.ic_dp = (char *)&chunksize;
25017683Spst	if (ioctl(fd, I_STR, (char *)&si) < 0) {
25175107Sfenner		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s",
25275107Sfenner		    pcap_strerror(errno));
25317683Spst		goto bad;
25417683Spst	}
25517683Spst
25617683Spst	/* request the interface */
25717683Spst	strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
25817683Spst	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' ';
25917683Spst	si.ic_cmd = NIOCBIND;
26017683Spst	si.ic_len = sizeof(ifr);
26117683Spst	si.ic_dp = (char *)&ifr;
26217683Spst	if (ioctl(fd, I_STR, (char *)&si) < 0) {
26375107Sfenner		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s",
26417683Spst			ifr.ifr_name, pcap_strerror(errno));
26517683Spst		goto bad;
26617683Spst	}
26717683Spst
26817683Spst	/* set the snapshot length */
26917683Spst	si.ic_cmd = NIOCSSNAP;
27017683Spst	si.ic_len = sizeof(snaplen);
27117683Spst	si.ic_dp = (char *)&snaplen;
27217683Spst	if (ioctl(fd, I_STR, (char *)&si) < 0) {
27375107Sfenner		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s",
27475107Sfenner		    pcap_strerror(errno));
27517683Spst		goto bad;
27617683Spst	}
27717683Spst	p->snapshot = snaplen;
27817683Spst	if (nit_setflags(p->fd, promisc, to_ms, ebuf) < 0)
27917683Spst		goto bad;
28017683Spst
28117683Spst	(void)ioctl(fd, I_FLUSH, (char *)FLUSHR);
28217683Spst	/*
28317683Spst	 * NIT supports only ethernets.
28417683Spst	 */
28517683Spst	p->linktype = DLT_EN10MB;
28617683Spst
28717683Spst	p->bufsize = BUFSPACE;
28817683Spst	p->buffer = (u_char *)malloc(p->bufsize);
28917683Spst	if (p->buffer == NULL) {
29075107Sfenner		strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
29117683Spst		goto bad;
29217683Spst	}
29317683Spst	return (p);
29417683Spst bad:
29517683Spst	if (fd >= 0)
29617683Spst		close(fd);
29717683Spst	free(p);
29817683Spst	return (NULL);
29917683Spst}
30017683Spst
30117683Spstint
30217683Spstpcap_setfilter(pcap_t *p, struct bpf_program *fp)
30317683Spst{
30417683Spst
30575107Sfenner	if (install_bpf_program(p, fp) < 0)
30675107Sfenner		return (-1);
30717683Spst	return (0);
30817683Spst}
309