1 /* $NetBSD: pcap-pf.c,v 1.4 2017/01/24 22:29:28 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 * packet filter subroutines for tcpdump 24 * Extraction/creation by Jeffrey Mogul, DECWRL 25 */ 26 27 #include <sys/cdefs.h> 28 __RCSID("$NetBSD: pcap-pf.c,v 1.4 2017/01/24 22:29:28 christos Exp $"); 29 30 #ifdef HAVE_CONFIG_H 31 #include "config.h" 32 #endif 33 34 #include <sys/types.h> 35 #include <sys/time.h> 36 #include <sys/timeb.h> 37 #include <sys/socket.h> 38 #include <sys/file.h> 39 #include <sys/ioctl.h> 40 #include <net/pfilt.h> 41 42 struct mbuf; 43 struct rtentry; 44 #include <net/if.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 <ctype.h> 57 #include <errno.h> 58 #include <netdb.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 /* 65 * Make "pcap.h" not include "pcap/bpf.h"; we are going to include the 66 * native OS version, as we need various BPF ioctls from it. 67 */ 68 #define PCAP_DONT_INCLUDE_PCAP_BPF_H 69 #include <net/bpf.h> 70 71 #include "pcap-int.h" 72 73 #ifdef HAVE_OS_PROTO_H 74 #include "os-proto.h" 75 #endif 76 77 /* 78 * FDDI packets are padded to make everything line up on a nice boundary. 79 */ 80 #define PCAP_FDDIPAD 3 81 82 /* 83 * Private data for capturing on Ultrix and DEC OSF/1^WDigital UNIX^W^W 84 * Tru64 UNIX packetfilter devices. 85 */ 86 struct pcap_pf { 87 int filtering_in_kernel; /* using kernel filter */ 88 u_long TotPkts; /* can't oflow for 79 hrs on ether */ 89 u_long TotAccepted; /* count accepted by filter */ 90 u_long TotDrops; /* count of dropped packets */ 91 long TotMissed; /* missed by i/f during this run */ 92 long OrigMissed; /* missed by i/f before this run */ 93 }; 94 95 static int pcap_setfilter_pf(pcap_t *, struct bpf_program *); 96 97 /* 98 * BUFSPACE is the size in bytes of the packet read buffer. Most tcpdump 99 * applications aren't going to need more than 200 bytes of packet header 100 * and the read shouldn't return more packets than packetfilter's internal 101 * queue limit (bounded at 256). 102 */ 103 #define BUFSPACE (200 * 256) 104 105 static int 106 pcap_read_pf(pcap_t *pc, int cnt, pcap_handler callback, u_char *user) 107 { 108 struct pcap_pf *pf = pc->priv; 109 register u_char *p, *bp; 110 register int cc, n, buflen, inc; 111 register struct enstamp *sp; 112 #ifdef LBL_ALIGN 113 struct enstamp stamp; 114 #endif 115 register u_int pad; 116 117 again: 118 cc = pc->cc; 119 if (cc == 0) { 120 cc = read(pc->fd, (char *)pc->buffer + pc->offset, pc->bufsize); 121 if (cc < 0) { 122 if (errno == EWOULDBLOCK) 123 return (0); 124 if (errno == EINVAL && 125 lseek(pc->fd, 0L, SEEK_CUR) + pc->bufsize < 0) { 126 /* 127 * Due to a kernel bug, after 2^31 bytes, 128 * the kernel file offset overflows and 129 * read fails with EINVAL. The lseek() 130 * to 0 will fix things. 131 */ 132 (void)lseek(pc->fd, 0L, SEEK_SET); 133 goto again; 134 } 135 pcap_snprintf(pc->errbuf, sizeof(pc->errbuf), "pf read: %s", 136 pcap_strerror(errno)); 137 return (-1); 138 } 139 bp = (u_char *)pc->buffer + pc->offset; 140 } else 141 bp = pc->bp; 142 /* 143 * Loop through each packet. 144 */ 145 n = 0; 146 pad = pc->fddipad; 147 while (cc > 0) { 148 /* 149 * Has "pcap_breakloop()" been called? 150 * If so, return immediately - if we haven't read any 151 * packets, clear the flag and return -2 to indicate 152 * that we were told to break out of the loop, otherwise 153 * leave the flag set, so that the *next* call will break 154 * out of the loop without having read any packets, and 155 * return the number of packets we've processed so far. 156 */ 157 if (pc->break_loop) { 158 if (n == 0) { 159 pc->break_loop = 0; 160 return (-2); 161 } else { 162 pc->cc = cc; 163 pc->bp = bp; 164 return (n); 165 } 166 } 167 if (cc < sizeof(*sp)) { 168 pcap_snprintf(pc->errbuf, sizeof(pc->errbuf), 169 "pf short read (%d)", cc); 170 return (-1); 171 } 172 #ifdef LBL_ALIGN 173 if ((long)bp & 3) { 174 sp = &stamp; 175 memcpy((char *)sp, (char *)bp, sizeof(*sp)); 176 } else 177 #endif 178 sp = (struct enstamp *)bp; 179 if (sp->ens_stamplen != sizeof(*sp)) { 180 pcap_snprintf(pc->errbuf, sizeof(pc->errbuf), 181 "pf short stamplen (%d)", 182 sp->ens_stamplen); 183 return (-1); 184 } 185 186 p = bp + sp->ens_stamplen; 187 buflen = sp->ens_count; 188 if (buflen > pc->snapshot) 189 buflen = pc->snapshot; 190 191 /* Calculate inc before possible pad update */ 192 inc = ENALIGN(buflen + sp->ens_stamplen); 193 cc -= inc; 194 bp += inc; 195 pf->TotPkts++; 196 pf->TotDrops += sp->ens_dropped; 197 pf->TotMissed = sp->ens_ifoverflows; 198 if (pf->OrigMissed < 0) 199 pf->OrigMissed = pf->TotMissed; 200 201 /* 202 * Short-circuit evaluation: if using BPF filter 203 * in kernel, no need to do it now - we already know 204 * the packet passed the filter. 205 * 206 * Note: the filter code was generated assuming 207 * that pc->fddipad was the amount of padding 208 * before the header, as that's what's required 209 * in the kernel, so we run the filter before 210 * skipping that padding. 211 */ 212 if (pf->filtering_in_kernel || 213 bpf_filter(pc->fcode.bf_insns, p, sp->ens_count, buflen)) { 214 struct pcap_pkthdr h; 215 pf->TotAccepted++; 216 h.ts = sp->ens_tstamp; 217 h.len = sp->ens_count - pad; 218 p += pad; 219 buflen -= pad; 220 h.caplen = buflen; 221 (*callback)(user, &h, p); 222 if (++n >= cnt && !PACKET_COUNT_IS_UNLIMITED(cnt)) { 223 pc->cc = cc; 224 pc->bp = bp; 225 return (n); 226 } 227 } 228 } 229 pc->cc = 0; 230 return (n); 231 } 232 233 static int 234 pcap_inject_pf(pcap_t *p, const void *buf, size_t size) 235 { 236 int ret; 237 238 ret = write(p->fd, buf, size); 239 if (ret == -1) { 240 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 241 pcap_strerror(errno)); 242 return (-1); 243 } 244 return (ret); 245 } 246 247 static int 248 pcap_stats_pf(pcap_t *p, struct pcap_stat *ps) 249 { 250 struct pcap_pf *pf = p->priv; 251 252 /* 253 * If packet filtering is being done in the kernel: 254 * 255 * "ps_recv" counts only packets that passed the filter. 256 * This does not include packets dropped because we 257 * ran out of buffer space. (XXX - perhaps it should, 258 * by adding "ps_drop" to "ps_recv", for compatibility 259 * with some other platforms. On the other hand, on 260 * some platforms "ps_recv" counts only packets that 261 * passed the filter, and on others it counts packets 262 * that didn't pass the filter....) 263 * 264 * "ps_drop" counts packets that passed the kernel filter 265 * (if any) but were dropped because the input queue was 266 * full. 267 * 268 * "ps_ifdrop" counts packets dropped by the network 269 * inteface (regardless of whether they would have passed 270 * the input filter, of course). 271 * 272 * If packet filtering is not being done in the kernel: 273 * 274 * "ps_recv" counts only packets that passed the filter. 275 * 276 * "ps_drop" counts packets that were dropped because the 277 * input queue was full, regardless of whether they passed 278 * the userland filter. 279 * 280 * "ps_ifdrop" counts packets dropped by the network 281 * inteface (regardless of whether they would have passed 282 * the input filter, of course). 283 * 284 * These statistics don't include packets not yet read from 285 * the kernel by libpcap, but they may include packets not 286 * yet read from libpcap by the application. 287 */ 288 ps->ps_recv = pf->TotAccepted; 289 ps->ps_drop = pf->TotDrops; 290 ps->ps_ifdrop = pf->TotMissed - pf->OrigMissed; 291 return (0); 292 } 293 294 /* 295 * We include the OS's <net/bpf.h>, not our "pcap/bpf.h", so we probably 296 * don't get DLT_DOCSIS defined. 297 */ 298 #ifndef DLT_DOCSIS 299 #define DLT_DOCSIS 143 300 #endif 301 302 static int 303 pcap_activate_pf(pcap_t *p) 304 { 305 struct pcap_pf *pf = p->priv; 306 short enmode; 307 int backlog = -1; /* request the most */ 308 struct enfilter Filter; 309 struct endevp devparams; 310 311 /* 312 * Initially try a read/write open (to allow the inject 313 * method to work). If that fails due to permission 314 * issues, fall back to read-only. This allows a 315 * non-root user to be granted specific access to pcap 316 * capabilities via file permissions. 317 * 318 * XXX - we should have an API that has a flag that 319 * controls whether to open read-only or read-write, 320 * so that denial of permission to send (or inability 321 * to send, if sending packets isn't supported on 322 * the device in question) can be indicated at open 323 * time. 324 * 325 * XXX - we assume here that "pfopen()" does not, in fact, modify 326 * its argument, even though it takes a "char *" rather than a 327 * "const char *" as its first argument. That appears to be 328 * the case, at least on Digital UNIX 4.0. 329 * 330 * XXX - is there an error that means "no such device"? Is 331 * there one that means "that device doesn't support pf"? 332 */ 333 p->fd = pfopen(p->opt.device, O_RDWR); 334 if (p->fd == -1 && errno == EACCES) 335 p->fd = pfopen(p->opt.device, O_RDONLY); 336 if (p->fd < 0) { 337 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "pf open: %s: %s\n\ 338 your system may not be properly configured; see the packetfilter(4) man page\n", 339 p->opt.device, pcap_strerror(errno)); 340 goto bad; 341 } 342 pf->OrigMissed = -1; 343 enmode = ENTSTAMP|ENNONEXCL; 344 if (!p->opt.immediate) 345 enmode |= ENBATCH; 346 if (p->opt.promisc) 347 enmode |= ENPROMISC; 348 if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) { 349 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCMBIS: %s", 350 pcap_strerror(errno)); 351 goto bad; 352 } 353 #ifdef ENCOPYALL 354 /* Try to set COPYALL mode so that we see packets to ourself */ 355 enmode = ENCOPYALL; 356 (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */ 357 #endif 358 /* set the backlog */ 359 if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) { 360 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSETW: %s", 361 pcap_strerror(errno)); 362 goto bad; 363 } 364 /* discover interface type */ 365 if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) { 366 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCDEVP: %s", 367 pcap_strerror(errno)); 368 goto bad; 369 } 370 /* HACK: to compile prior to Ultrix 4.2 */ 371 #ifndef ENDT_FDDI 372 #define ENDT_FDDI 4 373 #endif 374 switch (devparams.end_dev_type) { 375 376 case ENDT_10MB: 377 p->linktype = DLT_EN10MB; 378 p->offset = 2; 379 /* 380 * This is (presumably) a real Ethernet capture; give it a 381 * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 382 * that an application can let you choose it, in case you're 383 * capturing DOCSIS traffic that a Cisco Cable Modem 384 * Termination System is putting out onto an Ethernet (it 385 * doesn't put an Ethernet header onto the wire, it puts raw 386 * DOCSIS frames out on the wire inside the low-level 387 * Ethernet framing). 388 */ 389 p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 390 /* 391 * If that fails, just leave the list empty. 392 */ 393 if (p->dlt_list != NULL) { 394 p->dlt_list[0] = DLT_EN10MB; 395 p->dlt_list[1] = DLT_DOCSIS; 396 p->dlt_count = 2; 397 } 398 break; 399 400 case ENDT_FDDI: 401 p->linktype = DLT_FDDI; 402 break; 403 404 #ifdef ENDT_SLIP 405 case ENDT_SLIP: 406 p->linktype = DLT_SLIP; 407 break; 408 #endif 409 410 #ifdef ENDT_PPP 411 case ENDT_PPP: 412 p->linktype = DLT_PPP; 413 break; 414 #endif 415 416 #ifdef ENDT_LOOPBACK 417 case ENDT_LOOPBACK: 418 /* 419 * It appears to use Ethernet framing, at least on 420 * Digital UNIX 4.0. 421 */ 422 p->linktype = DLT_EN10MB; 423 p->offset = 2; 424 break; 425 #endif 426 427 #ifdef ENDT_TRN 428 case ENDT_TRN: 429 p->linktype = DLT_IEEE802; 430 break; 431 #endif 432 433 default: 434 /* 435 * XXX - what about ENDT_IEEE802? The pfilt.h header 436 * file calls this "IEEE 802 networks (non-Ethernet)", 437 * but that doesn't specify a specific link layer type; 438 * it could be 802.4, or 802.5 (except that 802.5 is 439 * ENDT_TRN), or 802.6, or 802.11, or.... That's why 440 * DLT_IEEE802 was hijacked to mean Token Ring in various 441 * BSDs, and why we went along with that hijacking. 442 * 443 * XXX - what about ENDT_HDLC and ENDT_NULL? 444 * Presumably, as ENDT_OTHER is just "Miscellaneous 445 * framing", there's not much we can do, as that 446 * doesn't specify a particular type of header. 447 */ 448 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 449 "unknown data-link type %u", devparams.end_dev_type); 450 goto bad; 451 } 452 /* set truncation */ 453 if (p->linktype == DLT_FDDI) { 454 p->fddipad = PCAP_FDDIPAD; 455 456 /* packetfilter includes the padding in the snapshot */ 457 p->snapshot += PCAP_FDDIPAD; 458 } else 459 p->fddipad = 0; 460 if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&p->snapshot) < 0) { 461 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCTRUNCATE: %s", 462 pcap_strerror(errno)); 463 goto bad; 464 } 465 /* accept all packets */ 466 memset(&Filter, 0, sizeof(Filter)); 467 Filter.enf_Priority = 37; /* anything > 2 */ 468 Filter.enf_FilterLen = 0; /* means "always true" */ 469 if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) { 470 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSETF: %s", 471 pcap_strerror(errno)); 472 goto bad; 473 } 474 475 if (p->opt.timeout != 0) { 476 struct timeval timeout; 477 timeout.tv_sec = p->opt.timeout / 1000; 478 timeout.tv_usec = (p->opt.timeout * 1000) % 1000000; 479 if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) { 480 pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "EIOCSRTIMEOUT: %s", 481 pcap_strerror(errno)); 482 goto bad; 483 } 484 } 485 486 p->bufsize = BUFSPACE; 487 p->buffer = malloc(p->bufsize + p->offset); 488 if (p->buffer == NULL) { 489 strlcpy(p->errbuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 490 goto bad; 491 } 492 493 /* 494 * "select()" and "poll()" work on packetfilter devices. 495 */ 496 p->selectable_fd = p->fd; 497 498 p->read_op = pcap_read_pf; 499 p->inject_op = pcap_inject_pf; 500 p->setfilter_op = pcap_setfilter_pf; 501 p->setdirection_op = NULL; /* Not implemented. */ 502 p->set_datalink_op = NULL; /* can't change data link type */ 503 p->getnonblock_op = pcap_getnonblock_fd; 504 p->setnonblock_op = pcap_setnonblock_fd; 505 p->stats_op = pcap_stats_pf; 506 507 return (0); 508 bad: 509 pcap_cleanup_live_common(p); 510 return (PCAP_ERROR); 511 } 512 513 pcap_t * 514 pcap_create_interface(const char *device _U_, char *ebuf) 515 { 516 pcap_t *p; 517 518 p = pcap_create_common(ebuf, sizeof (struct pcap_pf)); 519 if (p == NULL) 520 return (NULL); 521 522 p->activate_op = pcap_activate_pf; 523 return (p); 524 } 525 526 /* 527 * XXX - is there an error from pfopen() that means "no such device"? 528 * Is there one that means "that device doesn't support pf"? 529 */ 530 static int 531 can_be_bound(const char *name _U_) 532 { 533 return (1); 534 } 535 536 int 537 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 538 { 539 return (pcap_findalldevs_interfaces(alldevsp, errbuf, can_be_bound)); 540 } 541 542 static int 543 pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp) 544 { 545 struct pcap_pf *pf = p->priv; 546 struct bpf_version bv; 547 548 /* 549 * See if BIOCVERSION works. If not, we assume the kernel doesn't 550 * support BPF-style filters (it's not documented in the bpf(7) 551 * or packetfiler(7) man pages, but the code used to fail if 552 * BIOCSETF worked but BIOCVERSION didn't, and I've seen it do 553 * kernel filtering in DU 4.0, so presumably BIOCVERSION works 554 * there, at least). 555 */ 556 if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) >= 0) { 557 /* 558 * OK, we have the version of the BPF interpreter; 559 * is it the same major version as us, and the same 560 * or better minor version? 561 */ 562 if (bv.bv_major == BPF_MAJOR_VERSION && 563 bv.bv_minor >= BPF_MINOR_VERSION) { 564 /* 565 * Yes. Try to install the filter. 566 */ 567 if (ioctl(p->fd, BIOCSETF, (caddr_t)fp) < 0) { 568 pcap_snprintf(p->errbuf, sizeof(p->errbuf), 569 "BIOCSETF: %s", pcap_strerror(errno)); 570 return (-1); 571 } 572 573 /* 574 * OK, that succeeded. We're doing filtering in 575 * the kernel. (We assume we don't have a 576 * userland filter installed - that'd require 577 * a previous version check to have failed but 578 * this one to succeed.) 579 * 580 * XXX - this message should be supplied to the 581 * application as a warning of some sort, 582 * except that if it's a GUI application, it's 583 * not clear that it should be displayed in 584 * a window to annoy the user. 585 */ 586 fprintf(stderr, "tcpdump: Using kernel BPF filter\n"); 587 pf->filtering_in_kernel = 1; 588 589 /* 590 * Discard any previously-received packets, 591 * as they might have passed whatever filter 592 * was formerly in effect, but might not pass 593 * this filter (BIOCSETF discards packets buffered 594 * in the kernel, so you can lose packets in any 595 * case). 596 */ 597 p->cc = 0; 598 return (0); 599 } 600 601 /* 602 * We can't use the kernel's BPF interpreter; don't give 603 * up, just log a message and be inefficient. 604 * 605 * XXX - this should really be supplied to the application 606 * as a warning of some sort. 607 */ 608 fprintf(stderr, 609 "tcpdump: Requires BPF language %d.%d or higher; kernel is %d.%d\n", 610 BPF_MAJOR_VERSION, BPF_MINOR_VERSION, 611 bv.bv_major, bv.bv_minor); 612 } 613 614 /* 615 * We couldn't do filtering in the kernel; do it in userland. 616 */ 617 if (install_bpf_program(p, fp) < 0) 618 return (-1); 619 620 /* 621 * XXX - this message should be supplied by the application as 622 * a warning of some sort. 623 */ 624 fprintf(stderr, "tcpdump: Filtering in user process\n"); 625 pf->filtering_in_kernel = 0; 626 return (0); 627 } 628