blob: 4563a225ddc2500015d17715b0653c73398f2684 [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 * packet filter subroutines for tcpdump
22 * Extraction/creation by Jeffrey Mogul, DECWRL
23 */
24
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080025#ifdef HAVE_CONFIG_H
Haibo Huang165065a2018-07-23 17:26:52 -070026#include <config.h>
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080027#endif
28
29#include <sys/types.h>
30#include <sys/time.h>
31#include <sys/timeb.h>
32#include <sys/socket.h>
33#include <sys/file.h>
34#include <sys/ioctl.h>
35#include <net/pfilt.h>
36
37struct mbuf;
38struct rtentry;
39#include <net/if.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
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080051#include <errno.h>
52#include <netdb.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57
58/*
JP Abgrall511eca32014-02-12 13:46:45 -080059 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080060 * native OS version, as we need various BPF ioctls from it.
61 */
62#define PCAP_DONT_INCLUDE_PCAP_BPF_H
63#include <net/bpf.h>
64
65#include "pcap-int.h"
66
67#ifdef HAVE_OS_PROTO_H
68#include "os-proto.h"
69#endif
70
JP Abgrall511eca32014-02-12 13:46:45 -080071/*
72 * FDDI packets are padded to make everything line up on a nice boundary.
73 */
74#define PCAP_FDDIPAD 3
75
76/*
77 * Private data for capturing on Ultrix and DEC OSF/1^WDigital UNIX^W^W
78 * Tru64 UNIX packetfilter devices.
79 */
80struct pcap_pf {
81 int filtering_in_kernel; /* using kernel filter */
82 u_long TotPkts; /* can't oflow for 79 hrs on ether */
83 u_long TotAccepted; /* count accepted by filter */
84 u_long TotDrops; /* count of dropped packets */
85 long TotMissed; /* missed by i/f during this run */
86 long OrigMissed; /* missed by i/f before this run */
87};
88
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080089static int pcap_setfilter_pf(pcap_t *, struct bpf_program *);
90
91/*
92 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump
93 * applications aren't going to need more than 200 bytes of packet header
94 * and the read shouldn't return more packets than packetfilter's internal
95 * queue limit (bounded at 256).
96 */
97#define BUFSPACE (200 * 256)
98
99static int
100pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
101{
JP Abgrall511eca32014-02-12 13:46:45 -0800102 struct pcap_pf *pf = pc->priv;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800103 register u_char *p, *bp;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800104 register int cc, n, buflen, inc;
105 register struct enstamp *sp;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800106 struct enstamp stamp;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700107 register u_int pad;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800108
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800109 again:
110 cc = pc->cc;
111 if (cc == 0) {
112 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize);
113 if (cc < 0) {
114 if (errno == EWOULDBLOCK)
115 return (0);
116 if (errno == EINVAL &&
117 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) {
118 /*
119 * Due to a kernel bug, after 2^31 bytes,
120 * the kernel file offset overflows and
121 * read fails with EINVAL. The lseek()
122 * to 0 will fix things.
123 */
124 (void)lseek(pc->fd, 0L, SEEK_SET);
125 goto again;
126 }
Haibo Huang165065a2018-07-23 17:26:52 -0700127 pcap_fmt_errmsg_for_errno(pc->errbuf,
128 sizeof(pc->errbuf), errno, "pf read");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800129 return (-1);
130 }
Elliott Hughes965a4b52017-05-15 10:37:39 -0700131 bp = (u_char *)pc->buffer + pc->offset;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800132 } else
133 bp = pc->bp;
134 /*
135 * Loop through each packet.
136 */
137 n = 0;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800138 pad = pc->fddipad;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800139 while (cc > 0) {
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 (pc->break_loop) {
150 if (n == 0) {
151 pc->break_loop = 0;
152 return (-2);
153 } else {
154 pc->cc = cc;
155 pc->bp = bp;
156 return (n);
157 }
158 }
159 if (cc < sizeof(*sp)) {
Haibo Huangee759ce2021-01-05 21:34:29 -0800160 snprintf(pc->errbuf, sizeof(pc->errbuf),
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800161 "pf short read (%d)", cc);
162 return (-1);
163 }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800164 if ((long)bp & 3) {
165 sp = &stamp;
166 memcpy((char *)sp, (char *)bp, sizeof(*sp));
167 } else
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800168 sp = (struct enstamp *)bp;
169 if (sp->ens_stamplen != sizeof(*sp)) {
Haibo Huangee759ce2021-01-05 21:34:29 -0800170 snprintf(pc->errbuf, sizeof(pc->errbuf),
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800171 "pf short stamplen (%d)",
172 sp->ens_stamplen);
173 return (-1);
174 }
175
176 p = bp + sp->ens_stamplen;
177 buflen = sp->ens_count;
178 if (buflen > pc->snapshot)
179 buflen = pc->snapshot;
180
181 /* Calculate inc before possible pad update */
182 inc = ENALIGN(buflen + sp->ens_stamplen);
183 cc -= inc;
184 bp += inc;
JP Abgrall511eca32014-02-12 13:46:45 -0800185 pf->TotPkts++;
186 pf->TotDrops += sp->ens_dropped;
187 pf->TotMissed = sp->ens_ifoverflows;
188 if (pf->OrigMissed < 0)
189 pf->OrigMissed = pf->TotMissed;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800190
191 /*
192 * Short-circuit evaluation: if using BPF filter
JP Abgrall511eca32014-02-12 13:46:45 -0800193 * in kernel, no need to do it now - we already know
194 * the packet passed the filter.
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800195 *
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800196 * Note: the filter code was generated assuming
197 * that pc->fddipad was the amount of padding
198 * before the header, as that's what's required
199 * in the kernel, so we run the filter before
200 * skipping that padding.
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800201 */
JP Abgrall511eca32014-02-12 13:46:45 -0800202 if (pf->filtering_in_kernel ||
Haibo Huangee759ce2021-01-05 21:34:29 -0800203 pcap_filter(pc->fcode.bf_insns, p, sp->ens_count, buflen)) {
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800204 struct pcap_pkthdr h;
JP Abgrall511eca32014-02-12 13:46:45 -0800205 pf->TotAccepted++;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800206 h.ts = sp->ens_tstamp;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800207 h.len = sp->ens_count - pad;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800208 p += pad;
209 buflen -= pad;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800210 h.caplen = buflen;
211 (*callback)(user, &h, p);
JP Abgrall511eca32014-02-12 13:46:45 -0800212 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) {
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800213 pc->cc = cc;
214 pc->bp = bp;
215 return (n);
216 }
217 }
218 }
219 pc->cc = 0;
220 return (n);
221}
222
223static int
Haibo Huangee759ce2021-01-05 21:34:29 -0800224pcap_inject_pf(pcap_t *p, const void *buf, int size)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800225{
226 int ret;
227
228 ret = write(p->fd, buf, size);
229 if (ret == -1) {
Haibo Huang165065a2018-07-23 17:26:52 -0700230 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
231 errno, "send");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800232 return (-1);
233 }
234 return (ret);
Elliott Hughesd8845d72015-10-19 18:07:04 -0700235}
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800236
237static int
238pcap_stats_pf(pcap_t *p, struct pcap_stat *ps)
239{
JP Abgrall511eca32014-02-12 13:46:45 -0800240 struct pcap_pf *pf = p->priv;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800241
242 /*
243 * If packet filtering is being done in the kernel:
244 *
245 * "ps_recv" counts only packets that passed the filter.
246 * This does not include packets dropped because we
247 * ran out of buffer space. (XXX - perhaps it should,
248 * by adding "ps_drop" to "ps_recv", for compatibility
249 * with some other platforms. On the other hand, on
250 * some platforms "ps_recv" counts only packets that
251 * passed the filter, and on others it counts packets
252 * that didn't pass the filter....)
253 *
254 * "ps_drop" counts packets that passed the kernel filter
255 * (if any) but were dropped because the input queue was
256 * full.
257 *
258 * "ps_ifdrop" counts packets dropped by the network
Haibo Huangee759ce2021-01-05 21:34:29 -0800259 * interface (regardless of whether they would have passed
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800260 * the input filter, of course).
261 *
262 * If packet filtering is not being done in the kernel:
263 *
264 * "ps_recv" counts only packets that passed the filter.
265 *
266 * "ps_drop" counts packets that were dropped because the
267 * input queue was full, regardless of whether they passed
268 * the userland filter.
269 *
270 * "ps_ifdrop" counts packets dropped by the network
Haibo Huangee759ce2021-01-05 21:34:29 -0800271 * interface (regardless of whether they would have passed
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800272 * the input filter, of course).
273 *
274 * These statistics don't include packets not yet read from
275 * the kernel by libpcap, but they may include packets not
276 * yet read from libpcap by the application.
277 */
JP Abgrall511eca32014-02-12 13:46:45 -0800278 ps->ps_recv = pf->TotAccepted;
279 ps->ps_drop = pf->TotDrops;
280 ps->ps_ifdrop = pf->TotMissed - pf->OrigMissed;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800281 return (0);
282}
283
284/*
JP Abgrall511eca32014-02-12 13:46:45 -0800285 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800286 * don't get DLT_DOCSIS defined.
287 */
288#ifndef DLT_DOCSIS
289#define DLT_DOCSIS 143
290#endif
291
JP Abgrall511eca32014-02-12 13:46:45 -0800292static int
293pcap_activate_pf(pcap_t *p)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800294{
JP Abgrall511eca32014-02-12 13:46:45 -0800295 struct pcap_pf *pf = p->priv;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800296 short enmode;
297 int backlog = -1; /* request the most */
298 struct enfilter Filter;
299 struct endevp devparams;
Haibo Huang165065a2018-07-23 17:26:52 -0700300 int err;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800301
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800302 /*
303 * Initially try a read/write open (to allow the inject
304 * method to work). If that fails due to permission
305 * issues, fall back to read-only. This allows a
306 * non-root user to be granted specific access to pcap
307 * capabilities via file permissions.
308 *
309 * XXX - we should have an API that has a flag that
310 * controls whether to open read-only or read-write,
311 * so that denial of permission to send (or inability
312 * to send, if sending packets isn't supported on
313 * the device in question) can be indicated at open
314 * time.
315 *
316 * XXX - we assume here that "pfopen()" does not, in fact, modify
317 * its argument, even though it takes a "char *" rather than a
318 * "const char *" as its first argument. That appears to be
319 * the case, at least on Digital UNIX 4.0.
Elliott Hughes965a4b52017-05-15 10:37:39 -0700320 *
321 * XXX - is there an error that means "no such device"? Is
322 * there one that means "that device doesn't support pf"?
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800323 */
Elliott Hughes965a4b52017-05-15 10:37:39 -0700324 p->fd = pfopen(p->opt.device, O_RDWR);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800325 if (p->fd == -1 && errno == EACCES)
Elliott Hughes965a4b52017-05-15 10:37:39 -0700326 p->fd = pfopen(p->opt.device, O_RDONLY);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800327 if (p->fd < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700328 if (errno == EACCES) {
Haibo Huangee759ce2021-01-05 21:34:29 -0800329 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
Haibo Huang165065a2018-07-23 17:26:52 -0700330 "pf open: %s: Permission denied\n"
331"your system may not be properly configured; see the packetfilter(4) man page",
332 p->opt.device);
333 err = PCAP_ERROR_PERM_DENIED;
334 } else {
335 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
336 errno, "pf open: %s", p->opt.device);
337 err = PCAP_ERROR;
338 }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800339 goto bad;
340 }
Haibo Huang165065a2018-07-23 17:26:52 -0700341
342 /*
343 * Turn a negative snapshot value (invalid), a snapshot value of
344 * 0 (unspecified), or a value bigger than the normal maximum
345 * value, into the maximum allowed value.
346 *
347 * If some application really *needs* a bigger snapshot
348 * length, we should just increase MAXIMUM_SNAPLEN.
349 */
350 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
351 p->snapshot = MAXIMUM_SNAPLEN;
352
JP Abgrall511eca32014-02-12 13:46:45 -0800353 pf->OrigMissed = -1;
354 enmode = ENTSTAMP|ENNONEXCL;
355 if (!p->opt.immediate)
356 enmode |= ENBATCH;
357 if (p->opt.promisc)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800358 enmode |= ENPROMISC;
359 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700360 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
361 errno, "EIOCMBIS");
362 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800363 goto bad;
364 }
365#ifdef ENCOPYALL
366 /* Try to set COPYALL mode so that we see packets to ourself */
367 enmode = ENCOPYALL;
368 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
369#endif
370 /* set the backlog */
371 if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700372 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
373 errno, "EIOCSETW");
374 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800375 goto bad;
376 }
377 /* discover interface type */
378 if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700379 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
380 errno, "EIOCDEVP");
381 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800382 goto bad;
383 }
384 /* HACK: to compile prior to Ultrix 4.2 */
385#ifndef ENDT_FDDI
386#define ENDT_FDDI 4
387#endif
388 switch (devparams.end_dev_type) {
389
390 case ENDT_10MB:
391 p->linktype = DLT_EN10MB;
392 p->offset = 2;
393 /*
394 * This is (presumably) a real Ethernet capture; give it a
395 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so
396 * that an application can let you choose it, in case you're
397 * capturing DOCSIS traffic that a Cisco Cable Modem
398 * Termination System is putting out onto an Ethernet (it
399 * doesn't put an Ethernet header onto the wire, it puts raw
400 * DOCSIS frames out on the wire inside the low-level
401 * Ethernet framing).
402 */
403 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2);
404 /*
405 * If that fails, just leave the list empty.
406 */
407 if (p->dlt_list != NULL) {
408 p->dlt_list[0] = DLT_EN10MB;
409 p->dlt_list[1] = DLT_DOCSIS;
410 p->dlt_count = 2;
411 }
412 break;
413
414 case ENDT_FDDI:
415 p->linktype = DLT_FDDI;
416 break;
417
418#ifdef ENDT_SLIP
419 case ENDT_SLIP:
420 p->linktype = DLT_SLIP;
421 break;
422#endif
423
424#ifdef ENDT_PPP
425 case ENDT_PPP:
426 p->linktype = DLT_PPP;
427 break;
428#endif
429
430#ifdef ENDT_LOOPBACK
431 case ENDT_LOOPBACK:
432 /*
433 * It appears to use Ethernet framing, at least on
434 * Digital UNIX 4.0.
435 */
436 p->linktype = DLT_EN10MB;
437 p->offset = 2;
438 break;
439#endif
440
441#ifdef ENDT_TRN
442 case ENDT_TRN:
443 p->linktype = DLT_IEEE802;
444 break;
445#endif
446
447 default:
448 /*
449 * XXX - what about ENDT_IEEE802? The pfilt.h header
450 * file calls this "IEEE 802 networks (non-Ethernet)",
451 * but that doesn't specify a specific link layer type;
452 * it could be 802.4, or 802.5 (except that 802.5 is
453 * ENDT_TRN), or 802.6, or 802.11, or.... That's why
454 * DLT_IEEE802 was hijacked to mean Token Ring in various
455 * BSDs, and why we went along with that hijacking.
456 *
457 * XXX - what about ENDT_HDLC and ENDT_NULL?
458 * Presumably, as ENDT_OTHER is just "Miscellaneous
459 * framing", there's not much we can do, as that
460 * doesn't specify a particular type of header.
461 */
Haibo Huangee759ce2021-01-05 21:34:29 -0800462 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
JP Abgrall511eca32014-02-12 13:46:45 -0800463 "unknown data-link type %u", devparams.end_dev_type);
Haibo Huang165065a2018-07-23 17:26:52 -0700464 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800465 goto bad;
466 }
467 /* set truncation */
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800468 if (p->linktype == DLT_FDDI) {
469 p->fddipad = PCAP_FDDIPAD;
470
471 /* packetfilter includes the padding in the snapshot */
JP Abgrall511eca32014-02-12 13:46:45 -0800472 p->snapshot += PCAP_FDDIPAD;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800473 } else
474 p->fddipad = 0;
JP Abgrall511eca32014-02-12 13:46:45 -0800475 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&p->snapshot) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700476 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
477 errno, "EIOCTRUNCATE");
478 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800479 goto bad;
480 }
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800481 /* accept all packets */
482 memset(&Filter, 0, sizeof(Filter));
483 Filter.enf_Priority = 37; /* anything > 2 */
484 Filter.enf_FilterLen = 0; /* means "always true" */
485 if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700486 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
487 errno, "EIOCSETF");
488 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800489 goto bad;
490 }
491
JP Abgrall511eca32014-02-12 13:46:45 -0800492 if (p->opt.timeout != 0) {
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800493 struct timeval timeout;
JP Abgrall511eca32014-02-12 13:46:45 -0800494 timeout.tv_sec = p->opt.timeout / 1000;
495 timeout.tv_usec = (p->opt.timeout * 1000) % 1000000;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800496 if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700497 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
498 errno, "EIOCSRTIMEOUT");
499 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800500 goto bad;
501 }
502 }
503
504 p->bufsize = BUFSPACE;
Elliott Hughes965a4b52017-05-15 10:37:39 -0700505 p->buffer = malloc(p->bufsize + p->offset);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800506 if (p->buffer == NULL) {
Haibo Huang165065a2018-07-23 17:26:52 -0700507 pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
508 errno, "malloc");
509 err = PCAP_ERROR;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800510 goto bad;
511 }
512
513 /*
514 * "select()" and "poll()" work on packetfilter devices.
515 */
516 p->selectable_fd = p->fd;
517
518 p->read_op = pcap_read_pf;
519 p->inject_op = pcap_inject_pf;
520 p->setfilter_op = pcap_setfilter_pf;
521 p->setdirection_op = NULL; /* Not implemented. */
522 p->set_datalink_op = NULL; /* can't change data link type */
523 p->getnonblock_op = pcap_getnonblock_fd;
524 p->setnonblock_op = pcap_setnonblock_fd;
525 p->stats_op = pcap_stats_pf;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800526
JP Abgrall511eca32014-02-12 13:46:45 -0800527 return (0);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800528 bad:
JP Abgrall511eca32014-02-12 13:46:45 -0800529 pcap_cleanup_live_common(p);
Haibo Huang165065a2018-07-23 17:26:52 -0700530 return (err);
JP Abgrall511eca32014-02-12 13:46:45 -0800531}
532
533pcap_t *
Elliott Hughes965a4b52017-05-15 10:37:39 -0700534pcap_create_interface(const char *device _U_, char *ebuf)
JP Abgrall511eca32014-02-12 13:46:45 -0800535{
536 pcap_t *p;
537
Haibo Huangee759ce2021-01-05 21:34:29 -0800538 p = PCAP_CREATE_COMMON(ebuf, struct pcap_pf);
JP Abgrall511eca32014-02-12 13:46:45 -0800539 if (p == NULL)
540 return (NULL);
541
542 p->activate_op = pcap_activate_pf;
543 return (p);
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800544}
545
Elliott Hughes965a4b52017-05-15 10:37:39 -0700546/*
547 * XXX - is there an error from pfopen() that means "no such device"?
548 * Is there one that means "that device doesn't support pf"?
549 */
550static int
551can_be_bound(const char *name _U_)
552{
553 return (1);
554}
555
Haibo Huang165065a2018-07-23 17:26:52 -0700556static int
557get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800558{
Haibo Huang165065a2018-07-23 17:26:52 -0700559 /*
560 * Nothing we can do other than mark loopback devices as "the
561 * connected/disconnected status doesn't apply".
562 *
563 * XXX - is there a way to find out whether an adapter has
564 * something plugged into it?
565 */
566 if (*flags & PCAP_IF_LOOPBACK) {
567 /*
568 * Loopback devices aren't wireless, and "connected"/
569 * "disconnected" doesn't apply to them.
570 */
571 *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
572 return (0);
573 }
574 return (0);
575}
576
577int
578pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
579{
580 return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound,
581 get_if_flags));
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800582}
583
584static int
585pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp)
586{
JP Abgrall511eca32014-02-12 13:46:45 -0800587 struct pcap_pf *pf = p->priv;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800588 struct bpf_version bv;
589
590 /*
591 * See if BIOCVERSION works. If not, we assume the kernel doesn't
592 * support BPF-style filters (it's not documented in the bpf(7)
593 * or packetfiler(7) man pages, but the code used to fail if
594 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do
595 * kernel filtering in DU 4.0, so presumably BIOCVERSION works
596 * there, at least).
597 */
598 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) {
599 /*
600 * OK, we have the version of the BPF interpreter;
601 * is it the same major version as us, and the same
602 * or better minor version?
603 */
604 if (bv.bv_major == BPF_MAJOR_VERSION &&
605 bv.bv_minor >= BPF_MINOR_VERSION) {
606 /*
607 * Yes. Try to install the filter.
608 */
609 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) {
Haibo Huang165065a2018-07-23 17:26:52 -0700610 pcap_fmt_errmsg_for_errno(p->errbuf,
611 sizeof(p->errbuf), errno, "BIOCSETF");
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800612 return (-1);
613 }
614
615 /*
616 * OK, that succeeded. We're doing filtering in
617 * the kernel. (We assume we don't have a
618 * userland filter installed - that'd require
619 * a previous version check to have failed but
620 * this one to succeed.)
621 *
622 * XXX - this message should be supplied to the
623 * application as a warning of some sort,
624 * except that if it's a GUI application, it's
625 * not clear that it should be displayed in
626 * a window to annoy the user.
627 */
628 fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
JP Abgrall511eca32014-02-12 13:46:45 -0800629 pf->filtering_in_kernel = 1;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800630
631 /*
632 * Discard any previously-received packets,
633 * as they might have passed whatever filter
634 * was formerly in effect, but might not pass
635 * this filter (BIOCSETF discards packets buffered
636 * in the kernel, so you can lose packets in any
637 * case).
638 */
639 p->cc = 0;
640 return (0);
641 }
642
643 /*
644 * We can't use the kernel's BPF interpreter; don't give
645 * up, just log a message and be inefficient.
646 *
647 * XXX - this should really be supplied to the application
648 * as a warning of some sort.
649 */
650 fprintf(stderr,
651 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n",
652 BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
653 bv.bv_major, bv.bv_minor);
654 }
655
656 /*
657 * We couldn't do filtering in the kernel; do it in userland.
658 */
659 if (install_bpf_program(p, fp) < 0)
660 return (-1);
661
662 /*
663 * XXX - this message should be supplied by the application as
664 * a warning of some sort.
665 */
666 fprintf(stderr, "tcpdump: Filtering in user process\n");
JP Abgrall511eca32014-02-12 13:46:45 -0800667 pf->filtering_in_kernel = 0;
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800668 return (0);
669}
Haibo Huang165065a2018-07-23 17:26:52 -0700670
671/*
672 * Libpcap version string.
673 */
674const char *
675pcap_lib_version(void)
676{
677 return (PCAP_VERSION_STRING);
678}