pcap-snit.c revision 17683
1103285Sikob/*
2113584Ssimokawa * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3103285Sikob *	The Regents of the University of California.  All rights reserved.
4103285Sikob *
5103285Sikob * Redistribution and use in source and binary forms, with or without
6103285Sikob * modification, are permitted provided that: (1) source code distributions
7103285Sikob * retain the above copyright notice and this paragraph in its entirety, (2)
8103285Sikob * distributions including binary code include the above copyright notice and
9103285Sikob * this paragraph in its entirety in the documentation or other materials
10103285Sikob * provided with the distribution, and (3) all advertising materials mentioning
11103285Sikob * features or use of this software display the following acknowledgement:
12103285Sikob * ``This product includes software developed by the University of California,
13103285Sikob * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14103285Sikob * the University nor the names of its contributors may be used to endorse
15103285Sikob * or promote products derived from this software without specific prior
16103285Sikob * written permission.
17103285Sikob * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18103285Sikob * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19103285Sikob * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20103285Sikob */
21103285Sikob#ifndef lint
22103285Sikobstatic  char rcsid[] =
23103285Sikob    "@(#)$Header: pcap-snit.c,v 1.42 96/07/15 00:48:51 leres Exp $ (LBL)";
24103285Sikob#endif
25103285Sikob
26103285Sikob/*
27103285Sikob * Modifications made to accommodate the new SunOS4.0 NIT facility by
28103285Sikob * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
29103285Sikob * This module now handles the STREAMS based NIT.
30103285Sikob */
31103285Sikob
32103285Sikob#include <sys/types.h>
33103285Sikob#include <sys/time.h>
34103285Sikob#include <sys/timeb.h>
35103285Sikob#include <sys/dir.h>
36103285Sikob#include <sys/fcntlcom.h>
37113584Ssimokawa#include <sys/file.h>
38127468Ssimokawa#include <sys/ioctl.h>
39127468Ssimokawa#include <sys/socket.h>
40127468Ssimokawa#include <sys/stropts.h>
41113584Ssimokawa
42113584Ssimokawa#include <net/if.h>
43113584Ssimokawa#include <net/nit.h>
44113584Ssimokawa#include <net/nit_if.h>
45113584Ssimokawa#include <net/nit_pf.h>
46113584Ssimokawa#include <net/nit_buf.h>
47103285Sikob
48103285Sikob#include <netinet/in.h>
49103285Sikob#include <netinet/in_systm.h>
50103285Sikob#include <netinet/ip.h>
51103285Sikob#include <netinet/if_ether.h>
52127468Ssimokawa#include <netinet/ip_var.h>
53103285Sikob#include <netinet/udp.h>
54121506Ssimokawa#include <netinet/udp_var.h>
55103285Sikob#include <netinet/tcp.h>
56103285Sikob#include <netinet/tcpip.h>
57103285Sikob
58103285Sikob#include <ctype.h>
59103285Sikob#include <errno.h>
60103285Sikob#ifdef HAVE_MALLOC_H
61103285Sikob#include <malloc.h>
62103285Sikob#endif
63103285Sikob#include <stdio.h>
64103285Sikob#include <string.h>
65108527Ssimokawa#include <unistd.h>
66103285Sikob
67113584Ssimokawa#include "pcap-int.h"
68113584Ssimokawa
69109379Ssimokawa#include "gnuc.h"
70113584Ssimokawa#ifdef HAVE_OS_PROTO_H
71113584Ssimokawa#include "os-proto.h"
72113584Ssimokawa#endif
73108276Ssimokawa
74103285Sikob/*
75113584Ssimokawa * The chunk size for NIT.  This is the amount of buffering
76113584Ssimokawa * done for read calls.
77113584Ssimokawa */
78113584Ssimokawa#define CHUNKSIZE (2*1024)
79113584Ssimokawa
80113584Ssimokawa/*
81113584Ssimokawa * The total buffer space used by NIT.
82113584Ssimokawa */
83113584Ssimokawa#define BUFSPACE (4*CHUNKSIZE)
84103285Sikob
85113584Ssimokawa/* Forwards */
86124169Ssimokawastatic int nit_setflags(int, int, int, char *);
87124169Ssimokawa
88124169Ssimokawaint
89124169Ssimokawapcap_stats(pcap_t *p, struct pcap_stat *ps)
90124169Ssimokawa{
91124169Ssimokawa
92124169Ssimokawa	*ps = p->md.stat;
93	return (0);
94}
95
96int
97pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
98{
99	register int cc, n;
100	register struct bpf_insn *fcode = p->fcode.bf_insns;
101	register u_char *bp, *cp, *ep;
102	register struct nit_bufhdr *hdrp;
103	register struct nit_iftime *ntp;
104	register struct nit_iflen *nlp;
105	register struct nit_ifdrops *ndp;
106	register int caplen;
107
108	cc = p->cc;
109	if (cc == 0) {
110		cc = read(p->fd, (char *)p->buffer, p->bufsize);
111		if (cc < 0) {
112			if (errno == EWOULDBLOCK)
113				return (0);
114			sprintf(p->errbuf, "pcap_read: %s",
115				pcap_strerror(errno));
116			return (-1);
117		}
118		bp = p->buffer;
119	} else
120		bp = p->bp;
121
122	/*
123	 * loop through each snapshot in the chunk
124	 */
125	n = 0;
126	ep = bp + cc;
127	while (bp < ep) {
128		++p->md.stat.ps_recv;
129		cp = bp;
130
131		/* get past NIT buffer  */
132		hdrp = (struct nit_bufhdr *)cp;
133		cp += sizeof(*hdrp);
134
135		/* get past NIT timer   */
136		ntp = (struct nit_iftime *)cp;
137		cp += sizeof(*ntp);
138
139		ndp = (struct nit_ifdrops *)cp;
140		p->md.stat.ps_drop = ndp->nh_drops;
141		cp += sizeof *ndp;
142
143		/* get past packet len  */
144		nlp = (struct nit_iflen *)cp;
145		cp += sizeof(*nlp);
146
147		/* next snapshot        */
148		bp += hdrp->nhb_totlen;
149
150		caplen = nlp->nh_pktlen;
151		if (caplen > p->snapshot)
152			caplen = p->snapshot;
153
154		if (bpf_filter(fcode, cp, nlp->nh_pktlen, caplen)) {
155			struct pcap_pkthdr h;
156			h.ts = ntp->nh_timestamp;
157			h.len = nlp->nh_pktlen;
158			h.caplen = caplen;
159			(*callback)(user, &h, cp);
160			if (++n >= cnt && cnt >= 0) {
161				p->cc = ep - bp;
162				p->bp = bp;
163				return (n);
164			}
165		}
166	}
167	p->cc = 0;
168	return (n);
169}
170
171static int
172nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
173{
174	bpf_u_int32 flags;
175	struct strioctl si;
176	struct timeval timeout;
177
178	si.ic_timout = INFTIM;
179	if (to_ms != 0) {
180		timeout.tv_sec = to_ms / 1000;
181		timeout.tv_usec = (to_ms * 1000) % 1000000;
182		si.ic_cmd = NIOCSTIME;
183		si.ic_len = sizeof(timeout);
184		si.ic_dp = (char *)&timeout;
185		if (ioctl(fd, I_STR, (char *)&si) < 0) {
186			sprintf(ebuf, "NIOCSTIME: %s", pcap_strerror(errno));
187			return (-1);
188		}
189	}
190	flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
191	if (promisc)
192		flags |= NI_PROMISC;
193	si.ic_cmd = NIOCSFLAGS;
194	si.ic_len = sizeof(flags);
195	si.ic_dp = (char *)&flags;
196	if (ioctl(fd, I_STR, (char *)&si) < 0) {
197		sprintf(ebuf, "NIOCSFLAGS: %s", pcap_strerror(errno));
198		return (-1);
199	}
200	return (0);
201}
202
203pcap_t *
204pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
205{
206	struct strioctl si;		/* struct for ioctl() */
207	struct ifreq ifr;		/* interface request struct */
208	int chunksize = CHUNKSIZE;
209	int fd;
210	static char dev[] = "/dev/nit";
211	register pcap_t *p;
212
213	p = (pcap_t *)malloc(sizeof(*p));
214	if (p == NULL) {
215		strcpy(ebuf, pcap_strerror(errno));
216		return (NULL);
217	}
218
219	if (snaplen < 96)
220		/*
221		 * NIT requires a snapshot length of at least 96.
222		 */
223		snaplen = 96;
224
225	bzero(p, sizeof(*p));
226	p->fd = fd = open(dev, O_RDONLY);
227	if (fd < 0) {
228		sprintf(ebuf, "%s: %s", dev, pcap_strerror(errno));
229		goto bad;
230	}
231
232	/* arrange to get discrete messages from the STREAM and use NIT_BUF */
233	if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) {
234		sprintf(ebuf, "I_SRDOPT: %s", pcap_strerror(errno));
235		goto bad;
236	}
237	if (ioctl(fd, I_PUSH, "nbuf") < 0) {
238		sprintf(ebuf, "push nbuf: %s", pcap_strerror(errno));
239		goto bad;
240	}
241	/* set the chunksize */
242	si.ic_cmd = NIOCSCHUNK;
243	si.ic_timout = INFTIM;
244	si.ic_len = sizeof(chunksize);
245	si.ic_dp = (char *)&chunksize;
246	if (ioctl(fd, I_STR, (char *)&si) < 0) {
247		sprintf(ebuf, "NIOCSCHUNK: %s", pcap_strerror(errno));
248		goto bad;
249	}
250
251	/* request the interface */
252	strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
253	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = ' ';
254	si.ic_cmd = NIOCBIND;
255	si.ic_len = sizeof(ifr);
256	si.ic_dp = (char *)&ifr;
257	if (ioctl(fd, I_STR, (char *)&si) < 0) {
258		sprintf(ebuf, "NIOCBIND: %s: %s",
259			ifr.ifr_name, pcap_strerror(errno));
260		goto bad;
261	}
262
263	/* set the snapshot length */
264	si.ic_cmd = NIOCSSNAP;
265	si.ic_len = sizeof(snaplen);
266	si.ic_dp = (char *)&snaplen;
267	if (ioctl(fd, I_STR, (char *)&si) < 0) {
268		sprintf(ebuf, "NIOCSSNAP: %s", pcap_strerror(errno));
269		goto bad;
270	}
271	p->snapshot = snaplen;
272	if (nit_setflags(p->fd, promisc, to_ms, ebuf) < 0)
273		goto bad;
274
275	(void)ioctl(fd, I_FLUSH, (char *)FLUSHR);
276	/*
277	 * NIT supports only ethernets.
278	 */
279	p->linktype = DLT_EN10MB;
280
281	p->bufsize = BUFSPACE;
282	p->buffer = (u_char *)malloc(p->bufsize);
283	if (p->buffer == NULL) {
284		strcpy(ebuf, pcap_strerror(errno));
285		goto bad;
286	}
287	return (p);
288 bad:
289	if (fd >= 0)
290		close(fd);
291	free(p);
292	return (NULL);
293}
294
295int
296pcap_setfilter(pcap_t *p, struct bpf_program *fp)
297{
298
299	p->fcode = *fp;
300	return (0);
301}
302