blob: ca6222cf01666aac7d185a281ff6dac6eaf146e0 [file] [log] [blame]
The Android Open Source Project478ab6c2009-03-03 19:30:05 -08001/*
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
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080026#ifdef HAVE_CONFIG_H
Haibo Huang165065a2018-07-23 17:26:52 -070027#include <config.h>
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080028#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
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080056#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 */
79static int nit_setflags(int, int, int, char *);
80
JP Abgrall511eca32014-02-12 13:46:45 -080081/*
82 * Private data for capturing on STREAMS NIT devices.
83 */
84struct pcap_snit {
85 struct pcap_stat stat;
86};
87
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080088static int
89pcap_stats_snit(pcap_t *p, struct pcap_stat *ps)
90{
JP Abgrall511eca32014-02-12 13:46:45 -080091 struct pcap_snit *psn = p->priv;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080092
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 */
JP Abgrall511eca32014-02-12 13:46:45 -0800110 *ps = psn->stat;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800111 return (0);
112}
113
114static int
115pcap_read_snit(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
116{
JP Abgrall511eca32014-02-12 13:46:45 -0800117 struct pcap_snit *psn = p->priv;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800118 register int cc, n;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800119 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);
Haibo Huang165065a2018-07-23 17:26:52 -0700132 pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf),
133 errno, "pcap_read");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800134 return (-1);
135 }
Elliott Hughes965a4b52017-05-15 10:37:39 -0700136 bp = (u_char *)p->buffer;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800137 } 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
JP Abgrall511eca32014-02-12 13:46:45 -0800166 ++psn->stat.ps_recv;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800167 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;
JP Abgrall511eca32014-02-12 13:46:45 -0800178 psn->stat.ps_drop = ndp->nh_drops;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800179 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
Haibo Huangee759ce2021-01-05 21:34:29 -0800192 if (pcap_filter(p->fcode.bf_insns, cp, nlp->nh_pktlen, caplen)) {
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800193 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);
JP Abgrall511eca32014-02-12 13:46:45 -0800198 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800199 p->cc = ep - bp;
200 p->bp = bp;
201 return (n);
202 }
203 }
204 }
205 p->cc = 0;
206 return (n);
207}
208
209static int
Haibo Huangee759ce2021-01-05 21:34:29 -0800210pcap_inject_snit(pcap_t *p, const void *buf, int size)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800211{
212 struct strbuf ctl, data;
Elliott Hughesd8845d72015-10-19 18:07:04 -0700213
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800214 /*
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) {
Haibo Huang165065a2018-07-23 17:26:52 -0700225 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
226 errno, "send");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800227 return (-1);
228 }
229 return (ret);
230}
231
232static int
JP Abgrall511eca32014-02-12 13:46:45 -0800233nit_setflags(pcap_t *p)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800234{
235 bpf_u_int32 flags;
236 struct strioctl si;
JP Abgrall511eca32014-02-12 13:46:45 -0800237 u_int zero = 0;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800238 struct timeval timeout;
239
JP Abgrall511eca32014-02-12 13:46:45 -0800240 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) {
Haibo Huang165065a2018-07-23 17:26:52 -0700249 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
250 errno, "NIOCSCHUNK");
JP Abgrall511eca32014-02-12 13:46:45 -0800251 return (-1);
252 }
253 }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800254 si.ic_timout = INFTIM;
JP Abgrall511eca32014-02-12 13:46:45 -0800255 if (p->opt.timeout != 0) {
256 timeout.tv_sec = p->opt.timeout / 1000;
257 timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800258 si.ic_cmd = NIOCSTIME;
259 si.ic_len = sizeof(timeout);
260 si.ic_dp = (char *)&timeout;
JP Abgrall511eca32014-02-12 13:46:45 -0800261 if (ioctl(p->fd, I_STR, (char *)&si) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700262 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
263 errno, "NIOCSTIME");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800264 return (-1);
265 }
266 }
267 flags = NI_TIMESTAMP | NI_LEN | NI_DROPS;
JP Abgrall511eca32014-02-12 13:46:45 -0800268 if (p->opt.promisc)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800269 flags |= NI_PROMISC;
270 si.ic_cmd = NIOCSFLAGS;
271 si.ic_len = sizeof(flags);
272 si.ic_dp = (char *)&flags;
JP Abgrall511eca32014-02-12 13:46:45 -0800273 if (ioctl(p->fd, I_STR, (char *)&si) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700274 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
275 errno, "NIOCSFLAGS");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800276 return (-1);
277 }
278 return (0);
279}
280
JP Abgrall511eca32014-02-12 13:46:45 -0800281static int
282pcap_activate_snit(pcap_t *p)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800283{
284 struct strioctl si; /* struct for ioctl() */
285 struct ifreq ifr; /* interface request struct */
286 int chunksize = CHUNKSIZE;
287 int fd;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700288 static const char dev[] = "/dev/nit";
Haibo Huang165065a2018-07-23 17:26:52 -0700289 int err;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800290
JP Abgrall511eca32014-02-12 13:46:45 -0800291 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);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800297 }
298
Haibo Huang165065a2018-07-23 17:26:52 -0700299 /*
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
JP Abgrall511eca32014-02-12 13:46:45 -0800310 if (p->snapshot < 96)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800311 /*
312 * NIT requires a snapshot length of at least 96.
313 */
JP Abgrall511eca32014-02-12 13:46:45 -0800314 p->snapshot = 96;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800315
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800316 /*
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) {
Haibo Huang165065a2018-07-23 17:26:52 -0700334 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);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800340 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) {
Haibo Huang165065a2018-07-23 17:26:52 -0700345 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
346 errno, "I_SRDOPT");
347 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800348 goto bad;
349 }
350 if (ioctl(fd, I_PUSH, "nbuf") < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700351 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
352 errno, "push nbuf");
353 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800354 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) {
Haibo Huang165065a2018-07-23 17:26:52 -0700362 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
363 errno, "NIOCSCHUNK");
364 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800365 goto bad;
366 }
367
368 /* request the interface */
Elliott Hughes965a4b52017-05-15 10:37:39 -0700369 strncpy(ifr.ifr_name, p->opt.device, sizeof(ifr.ifr_name));
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800370 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) {
Elliott Hughes965a4b52017-05-15 10:37:39 -0700375 /*
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 */
Haibo Huang165065a2018-07-23 17:26:52 -0700380 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
381 errno, "NIOCBIND: %s", ifr.ifr_name);
382 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800383 goto bad;
384 }
385
386 /* set the snapshot length */
387 si.ic_cmd = NIOCSSNAP;
JP Abgrall511eca32014-02-12 13:46:45 -0800388 si.ic_len = sizeof(p->snapshot);
389 si.ic_dp = (char *)&p->snapshot;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800390 if (ioctl(fd, I_STR, (char *)&si) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700391 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
392 errno, "NIOCSSNAP");
393 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800394 goto bad;
395 }
Haibo Huang165065a2018-07-23 17:26:52 -0700396 if (nit_setflags(p) < 0) {
397 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800398 goto bad;
Haibo Huang165065a2018-07-23 17:26:52 -0700399 }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800400
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;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700408 p->buffer = malloc(p->bufsize);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800409 if (p->buffer == NULL) {
Haibo Huang165065a2018-07-23 17:26:52 -0700410 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
411 errno, "malloc");
412 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800413 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;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800450
JP Abgrall511eca32014-02-12 13:46:45 -0800451 return (0);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800452 bad:
JP Abgrall511eca32014-02-12 13:46:45 -0800453 pcap_cleanup_live_common(p);
Haibo Huang165065a2018-07-23 17:26:52 -0700454 return (err);
JP Abgrall511eca32014-02-12 13:46:45 -0800455}
456
457pcap_t *
Elliott Hughes965a4b52017-05-15 10:37:39 -0700458pcap_create_interface(const char *device _U_, char *ebuf)
JP Abgrall511eca32014-02-12 13:46:45 -0800459{
460 pcap_t *p;
461
Haibo Huangee759ce2021-01-05 21:34:29 -0800462 p = PCAP_CREATE_COMMON(ebuf, struct pcap_snit);
JP Abgrall511eca32014-02-12 13:46:45 -0800463 if (p == NULL)
464 return (NULL);
465
466 p->activate_op = pcap_activate_snit;
467 return (p);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800468}
469
Elliott Hughes965a4b52017-05-15 10:37:39 -0700470/*
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 */
474static int
475can_be_bound(const char *name _U_)
476{
477 return (1);
478}
479
Haibo Huang165065a2018-07-23 17:26:52 -0700480static int
481get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800482{
Haibo Huang165065a2018-07-23 17:26:52 -0700483 /*
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
491int
492pcap_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 */
501const char *
502pcap_lib_version(void)
503{
504 return (PCAP_VERSION_STRING);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800505}