xref: /netbsd-src/external/bsd/libpcap/dist/pcap-nit.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: pcap-nit.c,v 1.1.1.4 2013/12/31 16:57:19 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 #ifndef lint
24 static const char rcsid[] _U_ =
25     "@(#) Header: /tcpdump/master/libpcap/pcap-nit.c,v 1.62 2008-04-14 20:40:58 guy Exp  (LBL)";
26 #endif
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/timeb.h>
35 #include <sys/file.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 
39 #include <net/if.h>
40 #include <net/nit.h>
41 
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/if_ether.h>
46 #include <netinet/ip_var.h>
47 #include <netinet/udp.h>
48 #include <netinet/udp_var.h>
49 #include <netinet/tcp.h>
50 #include <netinet/tcpip.h>
51 
52 #include <ctype.h>
53 #include <errno.h>
54 #include <stdio.h>
55 
56 #include "pcap-int.h"
57 
58 #ifdef HAVE_OS_PROTO_H
59 #include "os-proto.h"
60 #endif
61 
62 /*
63  * The chunk size for NIT.  This is the amount of buffering
64  * done for read calls.
65  */
66 #define CHUNKSIZE (2*1024)
67 
68 /*
69  * The total buffer space used by NIT.
70  */
71 #define BUFSPACE (4*CHUNKSIZE)
72 
73 /* Forwards */
74 static int nit_setflags(int, int, int, char *);
75 
76 /*
77  * Private data for capturing on NIT devices.
78  */
79 struct pcap_nit {
80 	struct pcap_stat stat;
81 };
82 
83 static int
84 pcap_stats_nit(pcap_t *p, struct pcap_stat *ps)
85 {
86 	struct pcap_nit *pn = p->priv;
87 
88 	/*
89 	 * "ps_recv" counts packets handed to the filter, not packets
90 	 * that passed the filter.  As filtering is done in userland,
91 	 * this does not include packets dropped because we ran out
92 	 * of buffer space.
93 	 *
94 	 * "ps_drop" presumably counts packets dropped by the socket
95 	 * because of flow control requirements or resource exhaustion;
96 	 * it doesn't count packets dropped by the interface driver.
97 	 * As filtering is done in userland, it counts packets regardless
98 	 * of whether they would've passed the filter.
99 	 *
100 	 * These statistics don't include packets not yet read from the
101 	 * kernel by libpcap or packets not yet read from libpcap by the
102 	 * application.
103 	 */
104 	*ps = pn->stat;
105 	return (0);
106 }
107 
108 static int
109 pcap_read_nit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
110 {
111 	struct pcap_nit *pn = p->priv;
112 	register int cc, n;
113 	register u_char *bp, *cp, *ep;
114 	register struct nit_hdr *nh;
115 	register int caplen;
116 
117 	cc = p->cc;
118 	if (cc == 0) {
119 		cc = read(p->fd, (char *)p->buffer, p->bufsize);
120 		if (cc < 0) {
121 			if (errno == EWOULDBLOCK)
122 				return (0);
123 			snprintf(p->errbuf, sizeof(p->errbuf), "pcap_read: %s",
124 				pcap_strerror(errno));
125 			return (-1);
126 		}
127 		bp = p->buffer;
128 	} else
129 		bp = p->bp;
130 
131 	/*
132 	 * Loop through each packet.  The increment expression
133 	 * rounds up to the next int boundary past the end of
134 	 * the previous packet.
135 	 */
136 	n = 0;
137 	ep = bp + cc;
138 	while (bp < ep) {
139 		/*
140 		 * Has "pcap_breakloop()" been called?
141 		 * If so, return immediately - if we haven't read any
142 		 * packets, clear the flag and return -2 to indicate
143 		 * that we were told to break out of the loop, otherwise
144 		 * leave the flag set, so that the *next* call will break
145 		 * out of the loop without having read any packets, and
146 		 * return the number of packets we've processed so far.
147 		 */
148 		if (p->break_loop) {
149 			if (n == 0) {
150 				p->break_loop = 0;
151 				return (-2);
152 			} else {
153 				p->cc = ep - bp;
154 				p->bp = bp;
155 				return (n);
156 			}
157 		}
158 
159 		nh = (struct nit_hdr *)bp;
160 		cp = bp + sizeof(*nh);
161 
162 		switch (nh->nh_state) {
163 
164 		case NIT_CATCH:
165 			break;
166 
167 		case NIT_NOMBUF:
168 		case NIT_NOCLUSTER:
169 		case NIT_NOSPACE:
170 			pn->stat.ps_drop = nh->nh_dropped;
171 			continue;
172 
173 		case NIT_SEQNO:
174 			continue;
175 
176 		default:
177 			snprintf(p->errbuf, sizeof(p->errbuf),
178 			    "bad nit state %d", nh->nh_state);
179 			return (-1);
180 		}
181 		++pn->stat.ps_recv;
182 		bp += ((sizeof(struct nit_hdr) + nh->nh_datalen +
183 		    sizeof(int) - 1) & ~(sizeof(int) - 1));
184 
185 		caplen = nh->nh_wirelen;
186 		if (caplen > p->snapshot)
187 			caplen = p->snapshot;
188 		if (bpf_filter(p->fcode.bf_insns, cp, nh->nh_wirelen, caplen)) {
189 			struct pcap_pkthdr h;
190 			h.ts = nh->nh_timestamp;
191 			h.len = nh->nh_wirelen;
192 			h.caplen = caplen;
193 			(*callback)(user, &h, cp);
194 			if (++n >= cnt && cnt > 0) {
195 				p->cc = ep - bp;
196 				p->bp = bp;
197 				return (n);
198 			}
199 		}
200 	}
201 	p->cc = 0;
202 	return (n);
203 }
204 
205 static int
206 pcap_inject_nit(pcap_t *p, const void *buf, size_t size)
207 {
208 	struct sockaddr sa;
209 	int ret;
210 
211 	memset(&sa, 0, sizeof(sa));
212 	strncpy(sa.sa_data, device, sizeof(sa.sa_data));
213 	ret = sendto(p->fd, buf, size, 0, &sa, sizeof(sa));
214 	if (ret == -1) {
215 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
216 		    pcap_strerror(errno));
217 		return (-1);
218 	}
219 	return (ret);
220 }
221 
222 static int
223 nit_setflags(pcap_t *p)
224 {
225 	struct nit_ioc nioc;
226 
227 	memset(&nioc, 0, sizeof(nioc));
228 	nioc.nioc_typetomatch = NT_ALLTYPES;
229 	nioc.nioc_snaplen = p->snapshot;
230 	nioc.nioc_bufalign = sizeof(int);
231 	nioc.nioc_bufoffset = 0;
232 
233 	if (p->opt.buffer_size != 0)
234 		nioc.nioc_bufspace = p->opt.buffer_size;
235 	else {
236 		/* Default buffer size */
237 		nioc.nioc_bufspace = BUFSPACE;
238 	}
239 
240 	if (p->opt.immediate) {
241 		/*
242 		 * XXX - will this cause packets to be delivered immediately?
243 		 * XXX - given that this is for SunOS prior to 4.0, do
244 		 * we care?
245 		 */
246 		nioc.nioc_chunksize = 0;
247 	} else
248 		nioc.nioc_chunksize = CHUNKSIZE;
249 	if (p->opt.timeout != 0) {
250 		nioc.nioc_flags |= NF_TIMEOUT;
251 		nioc.nioc_timeout.tv_sec = p->opt.timeout / 1000;
252 		nioc.nioc_timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
253 	}
254 	if (p->opt.promisc)
255 		nioc.nioc_flags |= NF_PROMISC;
256 
257 	if (ioctl(p->fd, SIOCSNIT, &nioc) < 0) {
258 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "SIOCSNIT: %s",
259 		    pcap_strerror(errno));
260 		return (-1);
261 	}
262 	return (0);
263 }
264 
265 static int
266 pcap_activate_nit(pcap_t *p)
267 {
268 	int fd;
269 	struct sockaddr_nit snit;
270 
271 	if (p->opt.rfmon) {
272 		/*
273 		 * No monitor mode on SunOS 3.x or earlier (no
274 		 * Wi-Fi *devices* for the hardware that supported
275 		 * them!).
276 		 */
277 		return (PCAP_ERROR_RFMON_NOTSUP);
278 	}
279 
280 	if (p->snapshot < 96)
281 		/*
282 		 * NIT requires a snapshot length of at least 96.
283 		 */
284 		p->snapshot = 96;
285 
286 	memset(p, 0, sizeof(*p));
287 	p->fd = fd = socket(AF_NIT, SOCK_RAW, NITPROTO_RAW);
288 	if (fd < 0) {
289 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
290 		    "socket: %s", pcap_strerror(errno));
291 		goto bad;
292 	}
293 	snit.snit_family = AF_NIT;
294 	(void)strncpy(snit.snit_ifname, p->opt.source, NITIFSIZ);
295 
296 	if (bind(fd, (struct sockaddr *)&snit, sizeof(snit))) {
297 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
298 		    "bind: %s: %s", snit.snit_ifname, pcap_strerror(errno));
299 		goto bad;
300 	}
301 	if (nit_setflags(p) < 0)
302 		goto bad;
303 
304 	/*
305 	 * NIT supports only ethernets.
306 	 */
307 	p->linktype = DLT_EN10MB;
308 
309 	p->bufsize = BUFSPACE;
310 	p->buffer = (u_char *)malloc(p->bufsize);
311 	if (p->buffer == NULL) {
312 		strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
313 		goto bad;
314 	}
315 
316 	/*
317 	 * "p->fd" is a socket, so "select()" should work on it.
318 	 */
319 	p->selectable_fd = p->fd;
320 
321 	/*
322 	 * This is (presumably) a real Ethernet capture; give it a
323 	 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
324 	 * that an application can let you choose it, in case you're
325 	 * capturing DOCSIS traffic that a Cisco Cable Modem
326 	 * Termination System is putting out onto an Ethernet (it
327 	 * doesn't put an Ethernet header onto the wire, it puts raw
328 	 * DOCSIS frames out on the wire inside the low-level
329 	 * Ethernet framing).
330 	 */
331 	p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
332 	/*
333 	 * If that fails, just leave the list empty.
334 	 */
335 	if (p->dlt_list != NULL) {
336 		p->dlt_list[0] = DLT_EN10MB;
337 		p->dlt_list[1] = DLT_DOCSIS;
338 		p->dlt_count = 2;
339 	}
340 
341 	p->read_op = pcap_read_nit;
342 	p->inject_op = pcap_inject_nit;
343 	p->setfilter_op = install_bpf_program;	/* no kernel filtering */
344 	p->setdirection_op = NULL;	/* Not implemented. */
345 	p->set_datalink_op = NULL;	/* can't change data link type */
346 	p->getnonblock_op = pcap_getnonblock_fd;
347 	p->setnonblock_op = pcap_setnonblock_fd;
348 	p->stats_op = pcap_stats_nit;
349 
350 	return (0);
351  bad:
352 	pcap_cleanup_live_common(p);
353 	return (PCAP_ERROR);
354 }
355 
356 pcap_t *
357 pcap_create_interface(const char *device, char *ebuf)
358 {
359 	pcap_t *p;
360 
361 	p = pcap_create_common(device, ebuf, sizeof (struct pcap_nit));
362 	if (p == NULL)
363 		return (NULL);
364 
365 	p->activate_op = pcap_activate_nit;
366 	return (p);
367 }
368 
369 int
370 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
371 {
372 	return (0);
373 }
374