pcap-snit.c revision 127664
1/*
2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Modifications made to accommodate the new SunOS4.0 NIT facility by
22 * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
23 * This module now handles the STREAMS based NIT.
24 */
25
26#ifndef lint
27static const char rcsid[] _U_ =
28    "@(#) $Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.66.2.3 2003/11/21 10:20:48 guy Exp $ (LBL)";
29#endif
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <sys/types.h>
36#include <sys/time.h>
37#include <sys/timeb.h>
38#include <sys/dir.h>
39#include <sys/fcntlcom.h>
40#include <sys/file.h>
41#include <sys/ioctl.h>
42#include <sys/socket.h>
43#include <sys/stropts.h>
44
45#include <net/if.h>
46#include <net/nit.h>
47#include <net/nit_if.h>
48#include <net/nit_pf.h>
49#include <net/nit_buf.h>
50
51#include <netinet/in.h>
52#include <netinet/in_systm.h>
53#include <netinet/ip.h>
54#include <netinet/if_ether.h>
55#include <netinet/ip_var.h>
56#include <netinet/udp.h>
57#include <netinet/udp_var.h>
58#include <netinet/tcp.h>
59#include <netinet/tcpip.h>
60
61#include <ctype.h>
62#include <errno.h>
63#include <stdio.h>
64#include <string.h>
65#include <unistd.h>
66
67#include "pcap-int.h"
68
69#ifdef HAVE_OS_PROTO_H
70#include "os-proto.h"
71#endif
72
73/*
74 * The chunk size for NIT.  This is the amount of buffering
75 * done for read calls.
76 */
77#define CHUNKSIZE (2*1024)
78
79/*
80 * The total buffer space used by NIT.
81 */
82#define BUFSPACE (4*CHUNKSIZE)
83
84/* Forwards */
85static int nit_setflags(int, int, int, char *);
86
87static int
88pcap_stats_snit(pcap_t *p, struct pcap_stat *ps)
89{
90
91	/*
92	 * "ps_recv" counts packets handed to the filter, not packets
93	 * that passed the filter.  As filtering is done in userland,
94	 * this does not include packets dropped because we ran out
95	 * of buffer space.
96	 *
97	 * "ps_drop" counts packets dropped inside the "/dev/nit"
98	 * device because of flow control requirements or resource
99	 * exhaustion; it doesn't count packets dropped by the
100	 * interface driver, or packets dropped upstream.  As filtering
101	 * is done in userland, it counts packets regardless of whether
102	 * they would've passed the filter.
103	 *
104	 * These statistics don't include packets not yet read from the
105	 * kernel by libpcap or packets not yet read from libpcap by the
106	 * application.
107	 */
108	*ps = p->md.stat;
109	return (0);
110}
111
112static int
113pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
114{
115	register int cc, n;
116	register struct bpf_insn *fcode = p->fcode.bf_insns;
117	register u_char *bp, *cp, *ep;
118	register struct nit_bufhdr *hdrp;
119	register struct nit_iftime *ntp;
120	register struct nit_iflen *nlp;
121	register struct nit_ifdrops *ndp;
122	register int caplen;
123
124	cc = p->cc;
125	if (cc == 0) {
126		cc = read(p->fd, (char *)p->buffer, p->bufsize);
127		if (cc < 0) {
128			if (errno == EWOULDBLOCK)
129				return (0);
130			snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
131				pcap_strerror(errno));
132			return (-1);
133		}
134		bp = p->buffer;
135	} else
136		bp = p->bp;
137
138	/*
139	 * loop through each snapshot in the chunk
140	 */
141	n = 0;
142	ep = bp + cc;
143	while (bp < ep) {
144		/*
145		 * Has "pcap_breakloop()" been called?
146		 * If so, return immediately - if we haven't read any
147		 * packets, clear the flag and return -2 to indicate
148		 * that we were told to break out of the loop, otherwise
149		 * leave the flag set, so that the *next* call will break
150		 * out of the loop without having read any packets, and
151		 * return the number of packets we've processed so far.
152		 */
153		if (p->break_loop) {
154			if (n == 0) {
155				p->break_loop = 0;
156				return (-2);
157			} else {
158				p->bp = bp;
159				p->cc = ep - bp;
160				return (n);
161			}
162		}
163
164		++p->md.stat.ps_recv;
165		cp = bp;
166
167		/* get past NIT buffer  */
168		hdrp = (struct nit_bufhdr *)cp;
169		cp += sizeof(*hdrp);
170
171		/* get past NIT timer   */
172		ntp = (struct nit_iftime *)cp;
173		cp += sizeof(*ntp);
174
175		ndp = (struct nit_ifdrops *)cp;
176		p->md.stat.ps_drop = ndp->nh_drops;
177		cp += sizeof *ndp;
178
179		/* get past packet len  */
180		nlp = (struct nit_iflen *)cp;
181		cp += sizeof(*nlp);
182
183		/* next snapshot        */
184		bp += hdrp->nhb_totlen;
185
186		caplen = nlp->nh_pktlen;
187		if (caplen > p->snapshot)
188			caplen = p->snapshot;
189
190		if (bpf_filter(fcode, cp, nlp->nh_pktlen, caplen)) {
191			struct pcap_pkthdr h;
192			h.ts = ntp->nh_timestamp;
193			h.len = nlp->nh_pktlen;
194			h.caplen = caplen;
195			(*callback)(user, &h, cp);
196			if (++n >= cnt && cnt >= 0) {
197				p->cc = ep - bp;
198				p->bp = bp;
199				return (n);
200			}
201		}
202	}
203	p->cc = 0;
204	return (n);
205}
206
207static int
208nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
209{
210	bpf_u_int32 flags;
211	struct strioctl si;
212	struct timeval timeout;
213
214	si.ic_timout = INFTIM;
215	if (to_ms != 0) {
216		timeout.tv_sec = to_ms / 1000;
217		timeout.tv_usec = (to_ms * 1000) % 1000000;
218		si.ic_cmd = NIOCSTIME;
219		si.ic_len = sizeof(timeout);
220		si.ic_dp = (char *)&timeout;
221		if (ioctl(fd, I_STR, (char *)&si) < 0) {
222			snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s",
223			    pcap_strerror(errno));
224			return (-1);
225		}
226	}
227	flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
228	if (promisc)
229		flags |= NI_PROMISC;
230	si.ic_cmd = NIOCSFLAGS;
231	si.ic_len = sizeof(flags);
232	si.ic_dp = (char *)&flags;
233	if (ioctl(fd, I_STR, (char *)&si) < 0) {
234		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s",
235		    pcap_strerror(errno));
236		return (-1);
237	}
238	return (0);
239}
240
241static void
242pcap_close_snit(pcap_t *p)
243{
244	if (p->buffer != NULL)
245		free(p->buffer);
246	if (p->fd >= 0)
247		close(p->fd);
248}
249
250pcap_t *
251pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
252    char *ebuf)
253{
254	struct strioctl si;		/* struct for ioctl() */
255	struct ifreq ifr;		/* interface request struct */
256	int chunksize = CHUNKSIZE;
257	int fd;
258	static char dev[] = "/dev/nit";
259	register pcap_t *p;
260
261	p = (pcap_t *)malloc(sizeof(*p));
262	if (p == NULL) {
263		strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
264		return (NULL);
265	}
266
267	if (snaplen < 96)
268		/*
269		 * NIT requires a snapshot length of at least 96.
270		 */
271		snaplen = 96;
272
273	memset(p, 0, sizeof(*p));
274	p->fd = fd = open(dev, O_RDONLY);
275	if (fd < 0) {
276		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dev,
277		    pcap_strerror(errno));
278		goto bad;
279	}
280
281	/* arrange to get discrete messages from the STREAM and use NIT_BUF */
282	if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) {
283		snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s",
284		    pcap_strerror(errno));
285		goto bad;
286	}
287	if (ioctl(fd, I_PUSH, "nbuf") < 0) {
288		snprintf(ebuf, PCAP_ERRBUF_SIZE, "push nbuf: %s",
289		    pcap_strerror(errno));
290		goto bad;
291	}
292	/* set the chunksize */
293	si.ic_cmd = NIOCSCHUNK;
294	si.ic_timout = INFTIM;
295	si.ic_len = sizeof(chunksize);
296	si.ic_dp = (char *)&chunksize;
297	if (ioctl(fd, I_STR, (char *)&si) < 0) {
298		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s",
299		    pcap_strerror(errno));
300		goto bad;
301	}
302
303	/* request the interface */
304	strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
305	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
306	si.ic_cmd = NIOCBIND;
307	si.ic_len = sizeof(ifr);
308	si.ic_dp = (char *)&ifr;
309	if (ioctl(fd, I_STR, (char *)&si) < 0) {
310		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s",
311			ifr.ifr_name, pcap_strerror(errno));
312		goto bad;
313	}
314
315	/* set the snapshot length */
316	si.ic_cmd = NIOCSSNAP;
317	si.ic_len = sizeof(snaplen);
318	si.ic_dp = (char *)&snaplen;
319	if (ioctl(fd, I_STR, (char *)&si) < 0) {
320		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s",
321		    pcap_strerror(errno));
322		goto bad;
323	}
324	p->snapshot = snaplen;
325	if (nit_setflags(p->fd, promisc, to_ms, ebuf) < 0)
326		goto bad;
327
328	(void)ioctl(fd, I_FLUSH, (char *)FLUSHR);
329	/*
330	 * NIT supports only ethernets.
331	 */
332	p->linktype = DLT_EN10MB;
333
334	p->bufsize = BUFSPACE;
335	p->buffer = (u_char *)malloc(p->bufsize);
336	if (p->buffer == NULL) {
337		strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
338		goto bad;
339	}
340
341	/*
342	 * "p->fd" is an FD for a STREAMS device, so "select()" and
343	 * "poll()" should work on it.
344	 */
345	p->selectable_fd = p->fd;
346
347	p->read_op = pcap_read_snit;
348	p->setfilter_op = install_bpf_program;	/* no kernel filtering */
349	p->set_datalink_op = NULL;	/* can't change data link type */
350	p->getnonblock_op = pcap_getnonblock_fd;
351	p->setnonblock_op = pcap_setnonblock_fd;
352	p->stats_op = pcap_stats_snit;
353	p->close_op = pcap_close_snit;
354
355	return (p);
356 bad:
357	if (fd >= 0)
358		close(fd);
359	free(p);
360	return (NULL);
361}
362
363int
364pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
365{
366	return (0);
367}
368