xref: /netbsd-src/external/bsd/libpcap/dist/pcap-snit.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: pcap-snit.c,v 1.1.1.3 2013/04/06 15:57:46 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
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  * Modifications made to accommodate the new SunOS4.0 NIT facility by
24  * Micky Liu, micky@cunixc.cc.columbia.edu, Columbia University in May, 1989.
25  * This module now handles the STREAMS based NIT.
26  */
27 
28 #ifndef lint
29 static const char rcsid[] _U_ =
30     "@(#) Header: /tcpdump/master/libpcap/pcap-snit.c,v 1.77 2008-04-14 20:40:58 guy Exp  (LBL)";
31 #endif
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <sys/types.h>
38 #include <sys/time.h>
39 #include <sys/timeb.h>
40 #include <sys/dir.h>
41 #include <sys/fcntlcom.h>
42 #include <sys/file.h>
43 #include <sys/ioctl.h>
44 #include <sys/socket.h>
45 #include <sys/stropts.h>
46 
47 #include <net/if.h>
48 #include <net/nit.h>
49 #include <net/nit_if.h>
50 #include <net/nit_pf.h>
51 #include <net/nit_buf.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/udp.h>
59 #include <netinet/udp_var.h>
60 #include <netinet/tcp.h>
61 #include <netinet/tcpip.h>
62 
63 #include <ctype.h>
64 #include <errno.h>
65 #include <stdio.h>
66 #include <string.h>
67 #include <unistd.h>
68 
69 #include "pcap-int.h"
70 
71 #ifdef HAVE_OS_PROTO_H
72 #include "os-proto.h"
73 #endif
74 
75 /*
76  * The chunk size for NIT.  This is the amount of buffering
77  * done for read calls.
78  */
79 #define CHUNKSIZE (2*1024)
80 
81 /*
82  * The total buffer space used by NIT.
83  */
84 #define BUFSPACE (4*CHUNKSIZE)
85 
86 /* Forwards */
87 static int nit_setflags(int, int, int, char *);
88 
89 static int
90 pcap_stats_snit(pcap_t *p, struct pcap_stat *ps)
91 {
92 
93 	/*
94 	 * "ps_recv" counts packets handed to the filter, not packets
95 	 * that passed the filter.  As filtering is done in userland,
96 	 * this does not include packets dropped because we ran out
97 	 * of buffer space.
98 	 *
99 	 * "ps_drop" counts packets dropped inside the "/dev/nit"
100 	 * device because of flow control requirements or resource
101 	 * exhaustion; it doesn't count packets dropped by the
102 	 * interface driver, or packets dropped upstream.  As filtering
103 	 * is done in userland, it counts packets regardless of whether
104 	 * they would've passed the filter.
105 	 *
106 	 * These statistics don't include packets not yet read from the
107 	 * kernel by libpcap or packets not yet read from libpcap by the
108 	 * application.
109 	 */
110 	*ps = p->md.stat;
111 	return (0);
112 }
113 
114 static int
115 pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
116 {
117 	register int cc, n;
118 	register u_char *bp, *cp, *ep;
119 	register struct nit_bufhdr *hdrp;
120 	register struct nit_iftime *ntp;
121 	register struct nit_iflen *nlp;
122 	register struct nit_ifdrops *ndp;
123 	register int caplen;
124 
125 	cc = p->cc;
126 	if (cc == 0) {
127 		cc = read(p->fd, (char *)p->buffer, p->bufsize);
128 		if (cc < 0) {
129 			if (errno == EWOULDBLOCK)
130 				return (0);
131 			snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
132 				pcap_strerror(errno));
133 			return (-1);
134 		}
135 		bp = p->buffer;
136 	} else
137 		bp = p->bp;
138 
139 	/*
140 	 * loop through each snapshot in the chunk
141 	 */
142 	n = 0;
143 	ep = bp + cc;
144 	while (bp < ep) {
145 		/*
146 		 * Has "pcap_breakloop()" been called?
147 		 * If so, return immediately - if we haven't read any
148 		 * packets, clear the flag and return -2 to indicate
149 		 * that we were told to break out of the loop, otherwise
150 		 * leave the flag set, so that the *next* call will break
151 		 * out of the loop without having read any packets, and
152 		 * return the number of packets we've processed so far.
153 		 */
154 		if (p->break_loop) {
155 			if (n == 0) {
156 				p->break_loop = 0;
157 				return (-2);
158 			} else {
159 				p->bp = bp;
160 				p->cc = ep - bp;
161 				return (n);
162 			}
163 		}
164 
165 		++p->md.stat.ps_recv;
166 		cp = bp;
167 
168 		/* get past NIT buffer  */
169 		hdrp = (struct nit_bufhdr *)cp;
170 		cp += sizeof(*hdrp);
171 
172 		/* get past NIT timer   */
173 		ntp = (struct nit_iftime *)cp;
174 		cp += sizeof(*ntp);
175 
176 		ndp = (struct nit_ifdrops *)cp;
177 		p->md.stat.ps_drop = ndp->nh_drops;
178 		cp += sizeof *ndp;
179 
180 		/* get past packet len  */
181 		nlp = (struct nit_iflen *)cp;
182 		cp += sizeof(*nlp);
183 
184 		/* next snapshot        */
185 		bp += hdrp->nhb_totlen;
186 
187 		caplen = nlp->nh_pktlen;
188 		if (caplen > p->snapshot)
189 			caplen = p->snapshot;
190 
191 		if (bpf_filter(p->fcode.bf_insns, cp, nlp->nh_pktlen, caplen)) {
192 			struct pcap_pkthdr h;
193 			h.ts = ntp->nh_timestamp;
194 			h.len = nlp->nh_pktlen;
195 			h.caplen = caplen;
196 			(*callback)(user, &h, cp);
197 			if (++n >= cnt && cnt > 0) {
198 				p->cc = ep - bp;
199 				p->bp = bp;
200 				return (n);
201 			}
202 		}
203 	}
204 	p->cc = 0;
205 	return (n);
206 }
207 
208 static int
209 pcap_inject_snit(pcap_t *p, const void *buf, size_t size)
210 {
211 	struct strbuf ctl, data;
212 
213 	/*
214 	 * XXX - can we just do
215 	 *
216 	ret = write(pd->f, buf, size);
217 	 */
218 	ctl.len = sizeof(*sa);	/* XXX - what was this? */
219 	ctl.buf = (char *)sa;
220 	data.buf = buf;
221 	data.len = size;
222 	ret = putmsg(p->fd, &ctl, &data);
223 	if (ret == -1) {
224 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
225 		    pcap_strerror(errno));
226 		return (-1);
227 	}
228 	return (ret);
229 }
230 
231 static int
232 nit_setflags(int fd, int promisc, int to_ms, char *ebuf)
233 {
234 	bpf_u_int32 flags;
235 	struct strioctl si;
236 	struct timeval timeout;
237 
238 	si.ic_timout = INFTIM;
239 	if (to_ms != 0) {
240 		timeout.tv_sec = to_ms / 1000;
241 		timeout.tv_usec = (to_ms * 1000) % 1000000;
242 		si.ic_cmd = NIOCSTIME;
243 		si.ic_len = sizeof(timeout);
244 		si.ic_dp = (char *)&timeout;
245 		if (ioctl(fd, I_STR, (char *)&si) < 0) {
246 			snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSTIME: %s",
247 			    pcap_strerror(errno));
248 			return (-1);
249 		}
250 	}
251 	flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
252 	if (promisc)
253 		flags |= NI_PROMISC;
254 	si.ic_cmd = NIOCSFLAGS;
255 	si.ic_len = sizeof(flags);
256 	si.ic_dp = (char *)&flags;
257 	if (ioctl(fd, I_STR, (char *)&si) < 0) {
258 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "NIOCSFLAGS: %s",
259 		    pcap_strerror(errno));
260 		return (-1);
261 	}
262 	return (0);
263 }
264 
265 static int
266 pcap_activate_snit(pcap_t *p)
267 {
268 	struct strioctl si;		/* struct for ioctl() */
269 	struct ifreq ifr;		/* interface request struct */
270 	int chunksize = CHUNKSIZE;
271 	int fd;
272 	static char dev[] = "/dev/nit";
273 
274 	if (p->opt.rfmon) {
275 		/*
276 		 * No monitor mode on SunOS 4.x (no Wi-Fi devices on
277 		 * hardware supported by SunOS 4.x).
278 		 */
279 		return (PCAP_ERROR_RFMON_NOTSUP);
280 	}
281 
282 	if (p->snapshot < 96)
283 		/*
284 		 * NIT requires a snapshot length of at least 96.
285 		 */
286 		p->snapshot = 96;
287 
288 	/*
289 	 * Initially try a read/write open (to allow the inject
290 	 * method to work).  If that fails due to permission
291 	 * issues, fall back to read-only.  This allows a
292 	 * non-root user to be granted specific access to pcap
293 	 * capabilities via file permissions.
294 	 *
295 	 * XXX - we should have an API that has a flag that
296 	 * controls whether to open read-only or read-write,
297 	 * so that denial of permission to send (or inability
298 	 * to send, if sending packets isn't supported on
299 	 * the device in question) can be indicated at open
300 	 * time.
301 	 */
302 	p->fd = fd = open(dev, O_RDWR);
303 	if (fd < 0 && errno == EACCES)
304 		p->fd = fd = open(dev, O_RDONLY);
305 	if (fd < 0) {
306 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dev,
307 		    pcap_strerror(errno));
308 		goto bad;
309 	}
310 
311 	/* arrange to get discrete messages from the STREAM and use NIT_BUF */
312 	if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) {
313 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "I_SRDOPT: %s",
314 		    pcap_strerror(errno));
315 		goto bad;
316 	}
317 	if (ioctl(fd, I_PUSH, "nbuf") < 0) {
318 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "push nbuf: %s",
319 		    pcap_strerror(errno));
320 		goto bad;
321 	}
322 	/* set the chunksize */
323 	si.ic_cmd = NIOCSCHUNK;
324 	si.ic_timout = INFTIM;
325 	si.ic_len = sizeof(chunksize);
326 	si.ic_dp = (char *)&chunksize;
327 	if (ioctl(fd, I_STR, (char *)&si) < 0) {
328 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSCHUNK: %s",
329 		    pcap_strerror(errno));
330 		goto bad;
331 	}
332 
333 	/* request the interface */
334 	strncpy(ifr.ifr_name, p->opt.source, sizeof(ifr.ifr_name));
335 	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
336 	si.ic_cmd = NIOCBIND;
337 	si.ic_len = sizeof(ifr);
338 	si.ic_dp = (char *)&ifr;
339 	if (ioctl(fd, I_STR, (char *)&si) < 0) {
340 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCBIND: %s: %s",
341 			ifr.ifr_name, pcap_strerror(errno));
342 		goto bad;
343 	}
344 
345 	/* set the snapshot length */
346 	si.ic_cmd = NIOCSSNAP;
347 	si.ic_len = sizeof(p->snapshot);
348 	si.ic_dp = (char *)&p->snapshot;
349 	if (ioctl(fd, I_STR, (char *)&si) < 0) {
350 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "NIOCSSNAP: %s",
351 		    pcap_strerror(errno));
352 		goto bad;
353 	}
354 	if (nit_setflags(p->fd, p->opt.promisc, p->md.timeout, p->errbuf) < 0)
355 		goto bad;
356 
357 	(void)ioctl(fd, I_FLUSH, (char *)FLUSHR);
358 	/*
359 	 * NIT supports only ethernets.
360 	 */
361 	p->linktype = DLT_EN10MB;
362 
363 	p->bufsize = BUFSPACE;
364 	p->buffer = (u_char *)malloc(p->bufsize);
365 	if (p->buffer == NULL) {
366 		strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
367 		goto bad;
368 	}
369 
370 	/*
371 	 * "p->fd" is an FD for a STREAMS device, so "select()" and
372 	 * "poll()" should work on it.
373 	 */
374 	p->selectable_fd = p->fd;
375 
376 	/*
377 	 * This is (presumably) a real Ethernet capture; give it a
378 	 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
379 	 * that an application can let you choose it, in case you're
380 	 * capturing DOCSIS traffic that a Cisco Cable Modem
381 	 * Termination System is putting out onto an Ethernet (it
382 	 * doesn't put an Ethernet header onto the wire, it puts raw
383 	 * DOCSIS frames out on the wire inside the low-level
384 	 * Ethernet framing).
385 	 */
386 	p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
387 	/*
388 	 * If that fails, just leave the list empty.
389 	 */
390 	if (p->dlt_list != NULL) {
391 		p->dlt_list[0] = DLT_EN10MB;
392 		p->dlt_list[1] = DLT_DOCSIS;
393 		p->dlt_count = 2;
394 	}
395 
396 	p->read_op = pcap_read_snit;
397 	p->inject_op = pcap_inject_snit;
398 	p->setfilter_op = install_bpf_program;	/* no kernel filtering */
399 	p->setdirection_op = NULL;	/* Not implemented. */
400 	p->set_datalink_op = NULL;	/* can't change data link type */
401 	p->getnonblock_op = pcap_getnonblock_fd;
402 	p->setnonblock_op = pcap_setnonblock_fd;
403 	p->stats_op = pcap_stats_snit;
404 
405 	return (0);
406  bad:
407 	pcap_cleanup_live_common(p);
408 	return (PCAP_ERROR);
409 }
410 
411 pcap_t *
412 pcap_create(const char *device, char *ebuf)
413 {
414 	pcap_t *p;
415 
416 	p = pcap_create_common(device, ebuf);
417 	if (p == NULL)
418 		return (NULL);
419 
420 	p->activate_op = pcap_activate_snit;
421 	return (p);
422 }
423 
424 int
425 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
426 {
427 	return (0);
428 }
429