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