• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/timeb.h>
33 #include <sys/dir.h>
34 #include <sys/fcntlcom.h>
35 #include <sys/file.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/stropts.h>
39 
40 #include <net/if.h>
41 #include <net/nit.h>
42 #include <net/nit_if.h>
43 #include <net/nit_pf.h>
44 #include <net/nit_buf.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <netinet/if_ether.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/udp.h>
52 #include <netinet/udp_var.h>
53 #include <netinet/tcp.h>
54 #include <netinet/tcpip.h>
55 
56 #include <errno.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <unistd.h>
60 
61 #include "pcap-int.h"
62 
63 #ifdef HAVE_OS_PROTO_H
64 #include "os-proto.h"
65 #endif
66 
67 /*
68  * The chunk size for NIT.  This is the amount of buffering
69  * done for read calls.
70  */
71 #define CHUNKSIZE (2*1024)
72 
73 /*
74  * The total buffer space used by NIT.
75  */
76 #define BUFSPACE (4*CHUNKSIZE)
77 
78 /* Forwards */
79 static int nit_setflags(int, int, int, char *);
80 
81 /*
82  * Private data for capturing on STREAMS NIT devices.
83  */
84 struct pcap_snit {
85 	struct pcap_stat stat;
86 };
87 
88 static int
pcap_stats_snit(pcap_t * p,struct pcap_stat * ps)89 pcap_stats_snit(pcap_t *p, struct pcap_stat *ps)
90 {
91 	struct pcap_snit *psn = p->priv;
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 = psn->stat;
111 	return (0);
112 }
113 
114 static int
pcap_read_snit(pcap_t * p,int cnt,pcap_handler callback,u_char * user)115 pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
116 {
117 	struct pcap_snit *psn = p->priv;
118 	register int cc, n;
119 	register u_char *bp, *cp, *ep;
120 	register struct nit_bufhdr *hdrp;
121 	register struct nit_iftime *ntp;
122 	register struct nit_iflen *nlp;
123 	register struct nit_ifdrops *ndp;
124 	register int caplen;
125 
126 	cc = p->cc;
127 	if (cc == 0) {
128 		cc = read(p->fd, (char *)p->buffer, p->bufsize);
129 		if (cc < 0) {
130 			if (errno == EWOULDBLOCK)
131 				return (0);
132 			pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
133 			    errno, "pcap_read");
134 			return (-1);
135 		}
136 		bp = (u_char *)p->buffer;
137 	} else
138 		bp = p->bp;
139 
140 	/*
141 	 * loop through each snapshot in the chunk
142 	 */
143 	n = 0;
144 	ep = bp + cc;
145 	while (bp < ep) {
146 		/*
147 		 * Has "pcap_breakloop()" been called?
148 		 * If so, return immediately - if we haven't read any
149 		 * packets, clear the flag and return -2 to indicate
150 		 * that we were told to break out of the loop, otherwise
151 		 * leave the flag set, so that the *next* call will break
152 		 * out of the loop without having read any packets, and
153 		 * return the number of packets we've processed so far.
154 		 */
155 		if (p->break_loop) {
156 			if (n == 0) {
157 				p->break_loop = 0;
158 				return (-2);
159 			} else {
160 				p->bp = bp;
161 				p->cc = ep - bp;
162 				return (n);
163 			}
164 		}
165 
166 		++psn->stat.ps_recv;
167 		cp = bp;
168 
169 		/* get past NIT buffer  */
170 		hdrp = (struct nit_bufhdr *)cp;
171 		cp += sizeof(*hdrp);
172 
173 		/* get past NIT timer   */
174 		ntp = (struct nit_iftime *)cp;
175 		cp += sizeof(*ntp);
176 
177 		ndp = (struct nit_ifdrops *)cp;
178 		psn->stat.ps_drop = ndp->nh_drops;
179 		cp += sizeof *ndp;
180 
181 		/* get past packet len  */
182 		nlp = (struct nit_iflen *)cp;
183 		cp += sizeof(*nlp);
184 
185 		/* next snapshot        */
186 		bp += hdrp->nhb_totlen;
187 
188 		caplen = nlp->nh_pktlen;
189 		if (caplen > p->snapshot)
190 			caplen = p->snapshot;
191 
192 		if (pcap_filter(p->fcode.bf_insns, cp, nlp->nh_pktlen, caplen)) {
193 			struct pcap_pkthdr h;
194 			h.ts = ntp->nh_timestamp;
195 			h.len = nlp->nh_pktlen;
196 			h.caplen = caplen;
197 			(*callback)(user, &h, cp);
198 			if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
199 				p->cc = ep - bp;
200 				p->bp = bp;
201 				return (n);
202 			}
203 		}
204 	}
205 	p->cc = 0;
206 	return (n);
207 }
208 
209 static int
pcap_inject_snit(pcap_t * p,const void * buf,int size)210 pcap_inject_snit(pcap_t *p, const void *buf, int size)
211 {
212 	struct strbuf ctl, data;
213 
214 	/*
215 	 * XXX - can we just do
216 	 *
217 	ret = write(pd->f, buf, size);
218 	 */
219 	ctl.len = sizeof(*sa);	/* XXX - what was this? */
220 	ctl.buf = (char *)sa;
221 	data.buf = buf;
222 	data.len = size;
223 	ret = putmsg(p->fd, &ctl, &data);
224 	if (ret == -1) {
225 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
226 		    errno, "send");
227 		return (-1);
228 	}
229 	return (ret);
230 }
231 
232 static int
nit_setflags(pcap_t * p)233 nit_setflags(pcap_t *p)
234 {
235 	bpf_u_int32 flags;
236 	struct strioctl si;
237 	u_int zero = 0;
238 	struct timeval timeout;
239 
240 	if (p->opt.immediate) {
241 		/*
242 		 * Set the chunk size to zero, so that chunks get sent
243 		 * up immediately.
244 		 */
245 		si.ic_cmd = NIOCSCHUNK;
246 		si.ic_len = sizeof(zero);
247 		si.ic_dp = (char *)&zero;
248 		if (ioctl(p->fd, I_STR, (char *)&si) < 0) {
249 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
250 			    errno, "NIOCSCHUNK");
251 			return (-1);
252 		}
253 	}
254 	si.ic_timout = INFTIM;
255 	if (p->opt.timeout != 0) {
256 		timeout.tv_sec = p->opt.timeout / 1000;
257 		timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
258 		si.ic_cmd = NIOCSTIME;
259 		si.ic_len = sizeof(timeout);
260 		si.ic_dp = (char *)&timeout;
261 		if (ioctl(p->fd, I_STR, (char *)&si) < 0) {
262 			pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
263 			    errno, "NIOCSTIME");
264 			return (-1);
265 		}
266 	}
267 	flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
268 	if (p->opt.promisc)
269 		flags |= NI_PROMISC;
270 	si.ic_cmd = NIOCSFLAGS;
271 	si.ic_len = sizeof(flags);
272 	si.ic_dp = (char *)&flags;
273 	if (ioctl(p->fd, I_STR, (char *)&si) < 0) {
274 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
275 		    errno, "NIOCSFLAGS");
276 		return (-1);
277 	}
278 	return (0);
279 }
280 
281 static int
pcap_activate_snit(pcap_t * p)282 pcap_activate_snit(pcap_t *p)
283 {
284 	struct strioctl si;		/* struct for ioctl() */
285 	struct ifreq ifr;		/* interface request struct */
286 	int chunksize = CHUNKSIZE;
287 	int fd;
288 	static const char dev[] = "/dev/nit";
289 	int err;
290 
291 	if (p->opt.rfmon) {
292 		/*
293 		 * No monitor mode on SunOS 4.x (no Wi-Fi devices on
294 		 * hardware supported by SunOS 4.x).
295 		 */
296 		return (PCAP_ERROR_RFMON_NOTSUP);
297 	}
298 
299 	/*
300 	 * Turn a negative snapshot value (invalid), a snapshot value of
301 	 * 0 (unspecified), or a value bigger than the normal maximum
302 	 * value, into the maximum allowed value.
303 	 *
304 	 * If some application really *needs* a bigger snapshot
305 	 * length, we should just increase MAXIMUM_SNAPLEN.
306 	 */
307 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
308 		p->snapshot = MAXIMUM_SNAPLEN;
309 
310 	if (p->snapshot < 96)
311 		/*
312 		 * NIT requires a snapshot length of at least 96.
313 		 */
314 		p->snapshot = 96;
315 
316 	/*
317 	 * Initially try a read/write open (to allow the inject
318 	 * method to work).  If that fails due to permission
319 	 * issues, fall back to read-only.  This allows a
320 	 * non-root user to be granted specific access to pcap
321 	 * capabilities via file permissions.
322 	 *
323 	 * XXX - we should have an API that has a flag that
324 	 * controls whether to open read-only or read-write,
325 	 * so that denial of permission to send (or inability
326 	 * to send, if sending packets isn't supported on
327 	 * the device in question) can be indicated at open
328 	 * time.
329 	 */
330 	p->fd = fd = open(dev, O_RDWR);
331 	if (fd < 0 && errno == EACCES)
332 		p->fd = fd = open(dev, O_RDONLY);
333 	if (fd < 0) {
334 		if (errno == EACCES)
335 			err = PCAP_ERROR_PERM_DENIED;
336 		else
337 			err = PCAP_ERROR;
338 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
339 		    errno, "%s", dev);
340 		goto bad;
341 	}
342 
343 	/* arrange to get discrete messages from the STREAM and use NIT_BUF */
344 	if (ioctl(fd, I_SRDOPT, (char *)RMSGD) < 0) {
345 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
346 		    errno, "I_SRDOPT");
347 		err = PCAP_ERROR;
348 		goto bad;
349 	}
350 	if (ioctl(fd, I_PUSH, "nbuf") < 0) {
351 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
352 		    errno, "push nbuf");
353 		err = PCAP_ERROR;
354 		goto bad;
355 	}
356 	/* set the chunksize */
357 	si.ic_cmd = NIOCSCHUNK;
358 	si.ic_timout = INFTIM;
359 	si.ic_len = sizeof(chunksize);
360 	si.ic_dp = (char *)&chunksize;
361 	if (ioctl(fd, I_STR, (char *)&si) < 0) {
362 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
363 		    errno, "NIOCSCHUNK");
364 		err = PCAP_ERROR;
365 		goto bad;
366 	}
367 
368 	/* request the interface */
369 	strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
370 	ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
371 	si.ic_cmd = NIOCBIND;
372 	si.ic_len = sizeof(ifr);
373 	si.ic_dp = (char *)&ifr;
374 	if (ioctl(fd, I_STR, (char *)&si) < 0) {
375 		/*
376 		 * XXX - is there an error that means "no such device"?
377 		 * Is there one that means "that device doesn't support
378 		 * STREAMS NIT"?
379 		 */
380 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
381 		    errno, "NIOCBIND: %s", ifr.ifr_name);
382 		err = PCAP_ERROR;
383 		goto bad;
384 	}
385 
386 	/* set the snapshot length */
387 	si.ic_cmd = NIOCSSNAP;
388 	si.ic_len = sizeof(p->snapshot);
389 	si.ic_dp = (char *)&p->snapshot;
390 	if (ioctl(fd, I_STR, (char *)&si) < 0) {
391 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
392 		    errno, "NIOCSSNAP");
393 		err = PCAP_ERROR;
394 		goto bad;
395 	}
396 	if (nit_setflags(p) < 0) {
397 		err = PCAP_ERROR;
398 		goto bad;
399 	}
400 
401 	(void)ioctl(fd, I_FLUSH, (char *)FLUSHR);
402 	/*
403 	 * NIT supports only ethernets.
404 	 */
405 	p->linktype = DLT_EN10MB;
406 
407 	p->bufsize = BUFSPACE;
408 	p->buffer = malloc(p->bufsize);
409 	if (p->buffer == NULL) {
410 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
411 		    errno, "malloc");
412 		err = PCAP_ERROR;
413 		goto bad;
414 	}
415 
416 	/*
417 	 * "p->fd" is an FD for a STREAMS device, so "select()" and
418 	 * "poll()" should work on it.
419 	 */
420 	p->selectable_fd = p->fd;
421 
422 	/*
423 	 * This is (presumably) a real Ethernet capture; give it a
424 	 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
425 	 * that an application can let you choose it, in case you're
426 	 * capturing DOCSIS traffic that a Cisco Cable Modem
427 	 * Termination System is putting out onto an Ethernet (it
428 	 * doesn't put an Ethernet header onto the wire, it puts raw
429 	 * DOCSIS frames out on the wire inside the low-level
430 	 * Ethernet framing).
431 	 */
432 	p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
433 	/*
434 	 * If that fails, just leave the list empty.
435 	 */
436 	if (p->dlt_list != NULL) {
437 		p->dlt_list[0] = DLT_EN10MB;
438 		p->dlt_list[1] = DLT_DOCSIS;
439 		p->dlt_count = 2;
440 	}
441 
442 	p->read_op = pcap_read_snit;
443 	p->inject_op = pcap_inject_snit;
444 	p->setfilter_op = install_bpf_program;	/* no kernel filtering */
445 	p->setdirection_op = NULL;	/* Not implemented. */
446 	p->set_datalink_op = NULL;	/* can't change data link type */
447 	p->getnonblock_op = pcap_getnonblock_fd;
448 	p->setnonblock_op = pcap_setnonblock_fd;
449 	p->stats_op = pcap_stats_snit;
450 
451 	return (0);
452  bad:
453 	pcap_cleanup_live_common(p);
454 	return (err);
455 }
456 
457 pcap_t *
pcap_create_interface(const char * device _U_,char * ebuf)458 pcap_create_interface(const char *device _U_, char *ebuf)
459 {
460 	pcap_t *p;
461 
462 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_snit);
463 	if (p == NULL)
464 		return (NULL);
465 
466 	p->activate_op = pcap_activate_snit;
467 	return (p);
468 }
469 
470 /*
471  * XXX - there's probably a NIOCBIND error that means "that device
472  * doesn't support NIT"; if so, we should try an NIOCBIND and use that.
473  */
474 static int
can_be_bound(const char * name _U_)475 can_be_bound(const char *name _U_)
476 {
477 	return (1);
478 }
479 
480 static int
get_if_flags(const char * name _U_,bpf_u_int32 * flags _U_,char * errbuf _U_)481 get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
482 {
483 	/*
484 	 * Nothing we can do.
485 	 * XXX - is there a way to find out whether an adapter has
486 	 * something plugged into it?
487 	 */
488 	return (0);
489 }
490 
491 int
pcap_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)492 pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
493 {
494 	return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
495 	    get_if_flags));
496 }
497 
498 /*
499  * Libpcap version string.
500  */
501 const char *
pcap_lib_version(void)502 pcap_lib_version(void)
503 {
504 	return (PCAP_VERSION_STRING);
505 }
506