1 /*#define CHASE_CHAIN*/ 2 /* 3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that: (1) source code distributions 8 * retain the above copyright notice and this paragraph in its entirety, (2) 9 * distributions including binary code include the above copyright notice and 10 * this paragraph in its entirety in the documentation or other materials 11 * provided with the distribution, and (3) all advertising materials mentioning 12 * features or use of this software display the following acknowledgement: 13 * ``This product includes software developed by the University of California, 14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 15 * the University nor the names of its contributors may be used to endorse 16 * or promote products derived from this software without specific prior 17 * written permission. 18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 21 */ 22 #ifndef lint 23 static const char rcsid[] = 24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.140.2.1 2001/01/14 06:48:35 guy Exp $ (LBL)"; 25 #endif 26 27 #ifdef HAVE_CONFIG_H 28 #include "config.h" 29 #endif 30 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <sys/time.h> 34 #ifdef __NetBSD__ 35 #include <sys/param.h> 36 #endif 37 38 struct mbuf; 39 struct rtentry; 40 #include <net/if.h> 41 42 #include <netinet/in.h> 43 44 #include <stdlib.h> 45 #include <string.h> 46 #include <memory.h> 47 #include <setjmp.h> 48 #include <stdarg.h> 49 50 #include "pcap-int.h" 51 52 #include "ethertype.h" 53 #include "nlpid.h" 54 #include "gencode.h" 55 #include "ppp.h" 56 #include "sll.h" 57 #include <pcap-namedb.h> 58 #ifdef INET6 59 #include <netdb.h> 60 #include <sys/socket.h> 61 #endif /*INET6*/ 62 63 #define LLC_SNAP_LSAP 0xaa 64 #define LLC_ISO_LSAP 0xfe 65 66 #define ETHERMTU 1500 67 68 #ifdef HAVE_OS_PROTO_H 69 #include "os-proto.h" 70 #endif 71 72 #define JMP(c) ((c)|BPF_JMP|BPF_K) 73 74 /* Locals */ 75 static jmp_buf top_ctx; 76 static pcap_t *bpf_pcap; 77 78 /* XXX */ 79 #ifdef PCAP_FDDIPAD 80 int pcap_fddipad = PCAP_FDDIPAD; 81 #else 82 int pcap_fddipad; 83 #endif 84 85 /* VARARGS */ 86 void 87 bpf_error(const char *fmt, ...) 88 89 { 90 va_list ap; 91 92 va_start(ap, fmt); 93 if (bpf_pcap != NULL) 94 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE, 95 fmt, ap); 96 va_end(ap); 97 longjmp(top_ctx, 1); 98 /* NOTREACHED */ 99 } 100 101 static void init_linktype(int); 102 103 static int alloc_reg(void); 104 static void free_reg(int); 105 106 static struct block *root; 107 108 /* 109 * We divy out chunks of memory rather than call malloc each time so 110 * we don't have to worry about leaking memory. It's probably 111 * not a big deal if all this memory was wasted but it this ever 112 * goes into a library that would probably not be a good idea. 113 */ 114 #define NCHUNKS 16 115 #define CHUNK0SIZE 1024 116 struct chunk { 117 u_int n_left; 118 void *m; 119 }; 120 121 static struct chunk chunks[NCHUNKS]; 122 static int cur_chunk; 123 124 static void *newchunk(u_int); 125 static void freechunks(void); 126 static inline struct block *new_block(int); 127 static inline struct slist *new_stmt(int); 128 static struct block *gen_retblk(int); 129 static inline void syntax(void); 130 131 static void backpatch(struct block *, struct block *); 132 static void merge(struct block *, struct block *); 133 static struct block *gen_cmp(u_int, u_int, bpf_int32); 134 static struct block *gen_cmp_gt(u_int, u_int, bpf_int32); 135 static struct block *gen_mcmp(u_int, u_int, bpf_int32, bpf_u_int32); 136 static struct block *gen_bcmp(u_int, u_int, const u_char *); 137 static struct block *gen_uncond(int); 138 static inline struct block *gen_true(void); 139 static inline struct block *gen_false(void); 140 static struct block *gen_linktype(int); 141 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int); 142 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int); 143 #ifdef INET6 144 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int); 145 #endif 146 static struct block *gen_ehostop(const u_char *, int); 147 static struct block *gen_fhostop(const u_char *, int); 148 static struct block *gen_thostop(const u_char *, int); 149 static struct block *gen_dnhostop(bpf_u_int32, int, u_int); 150 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int); 151 #ifdef INET6 152 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int); 153 #endif 154 #ifndef INET6 155 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int); 156 #endif 157 static struct block *gen_ipfrag(void); 158 static struct block *gen_portatom(int, bpf_int32); 159 #ifdef INET6 160 static struct block *gen_portatom6(int, bpf_int32); 161 #endif 162 struct block *gen_portop(int, int, int); 163 static struct block *gen_port(int, int, int); 164 #ifdef INET6 165 struct block *gen_portop6(int, int, int); 166 static struct block *gen_port6(int, int, int); 167 #endif 168 static int lookup_proto(const char *, int); 169 static struct block *gen_protochain(int, int, int); 170 static struct block *gen_proto(int, int, int); 171 static struct slist *xfer_to_x(struct arth *); 172 static struct slist *xfer_to_a(struct arth *); 173 static struct block *gen_len(int, int); 174 175 static void * 176 newchunk(n) 177 u_int n; 178 { 179 struct chunk *cp; 180 int k, size; 181 182 #ifndef __NetBSD__ 183 /* XXX Round up to nearest long. */ 184 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1); 185 #else 186 /* XXX Round up to structure boundary. */ 187 n = ALIGN(n); 188 #endif 189 190 cp = &chunks[cur_chunk]; 191 if (n > cp->n_left) { 192 ++cp, k = ++cur_chunk; 193 if (k >= NCHUNKS) 194 bpf_error("out of memory"); 195 size = CHUNK0SIZE << k; 196 cp->m = (void *)malloc(size); 197 memset((char *)cp->m, 0, size); 198 cp->n_left = size; 199 if (n > size) 200 bpf_error("out of memory"); 201 } 202 cp->n_left -= n; 203 return (void *)((char *)cp->m + cp->n_left); 204 } 205 206 static void 207 freechunks() 208 { 209 int i; 210 211 cur_chunk = 0; 212 for (i = 0; i < NCHUNKS; ++i) 213 if (chunks[i].m != NULL) { 214 free(chunks[i].m); 215 chunks[i].m = NULL; 216 } 217 } 218 219 /* 220 * A strdup whose allocations are freed after code generation is over. 221 */ 222 char * 223 sdup(s) 224 register const char *s; 225 { 226 int n = strlen(s) + 1; 227 char *cp = newchunk(n); 228 229 strlcpy(cp, s, n); 230 return (cp); 231 } 232 233 static inline struct block * 234 new_block(code) 235 int code; 236 { 237 struct block *p; 238 239 p = (struct block *)newchunk(sizeof(*p)); 240 p->s.code = code; 241 p->head = p; 242 243 return p; 244 } 245 246 static inline struct slist * 247 new_stmt(code) 248 int code; 249 { 250 struct slist *p; 251 252 p = (struct slist *)newchunk(sizeof(*p)); 253 p->s.code = code; 254 255 return p; 256 } 257 258 static struct block * 259 gen_retblk(v) 260 int v; 261 { 262 struct block *b = new_block(BPF_RET|BPF_K); 263 264 b->s.k = v; 265 return b; 266 } 267 268 static inline void 269 syntax() 270 { 271 bpf_error("syntax error in filter expression"); 272 } 273 274 static bpf_u_int32 netmask; 275 static int snaplen; 276 int no_optimize; 277 278 int 279 pcap_compile(pcap_t *p, struct bpf_program *program, 280 char *buf, int optimize, bpf_u_int32 mask) 281 { 282 extern int n_errors; 283 int len; 284 285 no_optimize = 0; 286 n_errors = 0; 287 root = NULL; 288 bpf_pcap = p; 289 if (setjmp(top_ctx)) { 290 lex_cleanup(); 291 freechunks(); 292 return (-1); 293 } 294 295 netmask = mask; 296 297 snaplen = pcap_snapshot(p); 298 if (snaplen == 0) { 299 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 300 "snaplen of 0 rejects all packets"); 301 return -1; 302 } 303 304 lex_init(buf ? buf : ""); 305 init_linktype(pcap_datalink(p)); 306 (void)pcap_parse(); 307 308 if (n_errors) 309 syntax(); 310 311 if (root == NULL) 312 root = gen_retblk(snaplen); 313 314 if (optimize && !no_optimize) { 315 bpf_optimize(&root); 316 if (root == NULL || 317 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0)) 318 bpf_error("expression rejects all packets"); 319 } 320 program->bf_insns = icode_to_fcode(root, &len); 321 program->bf_len = len; 322 323 lex_cleanup(); 324 freechunks(); 325 return (0); 326 } 327 328 /* 329 * entry point for using the compiler with no pcap open 330 * pass in all the stuff that is needed explicitly instead. 331 */ 332 int 333 pcap_compile_nopcap(int snaplen_arg, int linktype_arg, 334 struct bpf_program *program, 335 char *buf, int optimize, bpf_u_int32 mask) 336 { 337 pcap_t *p; 338 int ret; 339 340 p = pcap_open_dead(linktype_arg, snaplen_arg); 341 if (p == NULL) 342 return (-1); 343 ret = pcap_compile(p, program, buf, optimize, mask); 344 pcap_close(p); 345 return (ret); 346 } 347 348 /* 349 * Clean up a "struct bpf_program" by freeing all the memory allocated 350 * in it. 351 */ 352 void 353 pcap_freecode(struct bpf_program *program) 354 { 355 program->bf_len = 0; 356 if (program->bf_insns != NULL) { 357 free((char *)program->bf_insns); 358 program->bf_insns = NULL; 359 } 360 } 361 362 /* 363 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates 364 * which of the jt and jf fields has been resolved and which is a pointer 365 * back to another unresolved block (or nil). At least one of the fields 366 * in each block is already resolved. 367 */ 368 static void 369 backpatch(list, target) 370 struct block *list, *target; 371 { 372 struct block *next; 373 374 while (list) { 375 if (!list->sense) { 376 next = JT(list); 377 JT(list) = target; 378 } else { 379 next = JF(list); 380 JF(list) = target; 381 } 382 list = next; 383 } 384 } 385 386 /* 387 * Merge the lists in b0 and b1, using the 'sense' field to indicate 388 * which of jt and jf is the link. 389 */ 390 static void 391 merge(b0, b1) 392 struct block *b0, *b1; 393 { 394 register struct block **p = &b0; 395 396 /* Find end of list. */ 397 while (*p) 398 p = !((*p)->sense) ? &JT(*p) : &JF(*p); 399 400 /* Concatenate the lists. */ 401 *p = b1; 402 } 403 404 void 405 finish_parse(p) 406 struct block *p; 407 { 408 backpatch(p, gen_retblk(snaplen)); 409 p->sense = !p->sense; 410 backpatch(p, gen_retblk(0)); 411 root = p->head; 412 } 413 414 void 415 gen_and(b0, b1) 416 struct block *b0, *b1; 417 { 418 backpatch(b0, b1->head); 419 b0->sense = !b0->sense; 420 b1->sense = !b1->sense; 421 merge(b1, b0); 422 b1->sense = !b1->sense; 423 b1->head = b0->head; 424 } 425 426 void 427 gen_or(b0, b1) 428 struct block *b0, *b1; 429 { 430 b0->sense = !b0->sense; 431 backpatch(b0, b1->head); 432 b0->sense = !b0->sense; 433 merge(b1, b0); 434 b1->head = b0->head; 435 } 436 437 void 438 gen_not(b) 439 struct block *b; 440 { 441 b->sense = !b->sense; 442 } 443 444 static struct block * 445 gen_cmp(offset, size, v) 446 u_int offset, size; 447 bpf_int32 v; 448 { 449 struct slist *s; 450 struct block *b; 451 452 s = new_stmt(BPF_LD|BPF_ABS|size); 453 s->s.k = offset; 454 455 b = new_block(JMP(BPF_JEQ)); 456 b->stmts = s; 457 b->s.k = v; 458 459 return b; 460 } 461 462 static struct block * 463 gen_cmp_gt(offset, size, v) 464 u_int offset, size; 465 bpf_int32 v; 466 { 467 struct slist *s; 468 struct block *b; 469 470 s = new_stmt(BPF_LD|BPF_ABS|size); 471 s->s.k = offset; 472 473 b = new_block(JMP(BPF_JGT)); 474 b->stmts = s; 475 b->s.k = v; 476 477 return b; 478 } 479 480 static struct block * 481 gen_mcmp(offset, size, v, mask) 482 u_int offset, size; 483 bpf_int32 v; 484 bpf_u_int32 mask; 485 { 486 struct block *b = gen_cmp(offset, size, v); 487 struct slist *s; 488 489 if (mask != 0xffffffff) { 490 s = new_stmt(BPF_ALU|BPF_AND|BPF_K); 491 s->s.k = mask; 492 b->stmts->next = s; 493 } 494 return b; 495 } 496 497 static struct block * 498 gen_bcmp(offset, size, v) 499 register u_int offset, size; 500 register const u_char *v; 501 { 502 register struct block *b, *tmp; 503 504 b = NULL; 505 while (size >= 4) { 506 register const u_char *p = &v[size - 4]; 507 bpf_int32 w = ((bpf_int32)p[0] << 24) | 508 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3]; 509 510 tmp = gen_cmp(offset + size - 4, BPF_W, w); 511 if (b != NULL) 512 gen_and(b, tmp); 513 b = tmp; 514 size -= 4; 515 } 516 while (size >= 2) { 517 register const u_char *p = &v[size - 2]; 518 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1]; 519 520 tmp = gen_cmp(offset + size - 2, BPF_H, w); 521 if (b != NULL) 522 gen_and(b, tmp); 523 b = tmp; 524 size -= 2; 525 } 526 if (size > 0) { 527 tmp = gen_cmp(offset, BPF_B, (bpf_int32)v[0]); 528 if (b != NULL) 529 gen_and(b, tmp); 530 b = tmp; 531 } 532 return b; 533 } 534 535 /* 536 * Various code constructs need to know the layout of the data link 537 * layer. These variables give the necessary offsets. off_linktype 538 * is set to -1 for no encapsulation, in which case, IP is assumed. 539 */ 540 static u_int off_linktype; 541 static u_int off_nl; 542 static int linktype; 543 544 static void 545 init_linktype(type) 546 int type; 547 { 548 linktype = type; 549 550 switch (type) { 551 552 case DLT_EN10MB: 553 off_linktype = 12; 554 off_nl = 14; 555 return; 556 557 case DLT_SLIP: 558 /* 559 * SLIP doesn't have a link level type. The 16 byte 560 * header is hacked into our SLIP driver. 561 */ 562 off_linktype = -1; 563 off_nl = 16; 564 return; 565 566 case DLT_SLIP_BSDOS: 567 /* XXX this may be the same as the DLT_PPP_BSDOS case */ 568 off_linktype = -1; 569 /* XXX end */ 570 off_nl = 24; 571 return; 572 573 case DLT_NULL: 574 case DLT_LOOP: 575 off_linktype = 0; 576 off_nl = 4; 577 return; 578 579 case DLT_PPP: 580 case DLT_C_HDLC: 581 case DLT_PPP_SERIAL: 582 off_linktype = 2; 583 off_nl = 4; 584 return; 585 586 case DLT_PPP_BSDOS: 587 off_linktype = 5; 588 off_nl = 24; 589 return; 590 591 case DLT_FDDI: 592 /* 593 * FDDI doesn't really have a link-level type field. 594 * We assume that SSAP = SNAP is being used and pick 595 * out the encapsulated Ethernet type. 596 * 597 * XXX - should we generate code to check for SNAP? 598 */ 599 off_linktype = 19; 600 #ifdef PCAP_FDDIPAD 601 off_linktype += pcap_fddipad; 602 #endif 603 off_nl = 21; 604 #ifdef PCAP_FDDIPAD 605 off_nl += pcap_fddipad; 606 #endif 607 return; 608 609 case DLT_IEEE802: 610 /* 611 * Token Ring doesn't really have a link-level type field. 612 * We assume that SSAP = SNAP is being used and pick 613 * out the encapsulated Ethernet type. 614 * 615 * XXX - should we generate code to check for SNAP? 616 * 617 * XXX - the header is actually variable-length. 618 * Some various Linux patched versions gave 38 619 * as "off_linktype" and 40 as "off_nl"; however, 620 * if a token ring packet has *no* routing 621 * information, i.e. is not source-routed, the correct 622 * values are 20 and 22, as they are in the vanilla code. 623 * 624 * A packet is source-routed iff the uppermost bit 625 * of the first byte of the source address, at an 626 * offset of 8, has the uppermost bit set. If the 627 * packet is source-routed, the total number of bytes 628 * of routing information is 2 plus bits 0x1F00 of 629 * the 16-bit value at an offset of 14 (shifted right 630 * 8 - figure out which byte that is). 631 */ 632 off_linktype = 20; 633 off_nl = 22; 634 return; 635 636 case DLT_ATM_RFC1483: 637 /* 638 * assume routed, non-ISO PDUs 639 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00) 640 */ 641 off_linktype = 6; 642 off_nl = 8; 643 return; 644 645 case DLT_RAW: 646 off_linktype = -1; 647 off_nl = 0; 648 return; 649 650 case DLT_ATM_CLIP: /* Linux ATM defines this */ 651 off_linktype = 6; 652 off_nl = 8; 653 return; 654 655 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */ 656 off_linktype = 14; 657 off_nl = 16; 658 return; 659 } 660 bpf_error("unknown data link type %d", linktype); 661 /* NOTREACHED */ 662 } 663 664 static struct block * 665 gen_uncond(rsense) 666 int rsense; 667 { 668 struct block *b; 669 struct slist *s; 670 671 s = new_stmt(BPF_LD|BPF_IMM); 672 s->s.k = !rsense; 673 b = new_block(JMP(BPF_JEQ)); 674 b->stmts = s; 675 676 return b; 677 } 678 679 static inline struct block * 680 gen_true() 681 { 682 return gen_uncond(1); 683 } 684 685 static inline struct block * 686 gen_false() 687 { 688 return gen_uncond(0); 689 } 690 691 /* 692 * Byte-swap a 32-bit number. 693 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on 694 * big-endian platforms.) 695 */ 696 #define SWAPLONG(y) \ 697 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff)) 698 699 static struct block * 700 gen_linktype(proto) 701 register int proto; 702 { 703 struct block *b0, *b1; 704 705 /* If we're not using encapsulation, we're done */ 706 if (off_linktype == -1) 707 return gen_true(); 708 709 switch (linktype) { 710 711 case DLT_EN10MB: 712 /* 713 * XXX - handle other LLC-encapsulated protocols here 714 * (IPX, OSI)? 715 */ 716 switch (proto) { 717 718 case LLC_ISO_LSAP: 719 /* 720 * OSI protocols always use 802.2 encapsulation. 721 */ 722 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU); 723 gen_not(b0); 724 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32) 725 ((LLC_ISO_LSAP << 8) | LLC_ISO_LSAP)); 726 gen_and(b0, b1); 727 return b1; 728 729 case ETHERTYPE_ATALK: 730 case ETHERTYPE_AARP: 731 /* 732 * EtherTalk (AppleTalk protocols on Ethernet link 733 * layer) may use 802.2 encapsulation. 734 */ 735 736 /* 737 * Check for 802.2 encapsulation (EtherTalk phase 2?); 738 * we check for an Ethernet type field less than 739 * 1500, which means it's an 802.3 length field. 740 */ 741 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU); 742 gen_not(b0); 743 744 /* 745 * 802.2-encapsulated ETHERTYPE_ATALK packets are 746 * SNAP packets with an organization code of 747 * 0x080007 (Apple, for Appletalk) and a protocol 748 * type of ETHERTYPE_ATALK (Appletalk). 749 * 750 * 802.2-encapsulated ETHERTYPE_AARP packets are 751 * SNAP packets with an organization code of 752 * 0x000000 (encapsulated Ethernet) and a protocol 753 * type of ETHERTYPE_AARP (Appletalk ARP). 754 */ 755 if (proto == ETHERTYPE_ATALK) 756 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14); 757 else /* proto == ETHERTYPE_AARP */ 758 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14); 759 gen_and(b0, b1); 760 761 /* 762 * Check for Ethernet encapsulation (Ethertalk 763 * phase 1?); we just check for the Ethernet 764 * protocol type. 765 */ 766 b0 = gen_cmp(off_linktype, BPF_H, (bpf_int32)proto); 767 768 gen_or(b0, b1); 769 return b1; 770 } 771 break; 772 773 case DLT_SLIP: 774 return gen_false(); 775 776 case DLT_PPP: 777 case DLT_PPP_SERIAL: 778 /* 779 * We use Ethernet protocol types inside libpcap; 780 * map them to the corresponding PPP protocol types. 781 */ 782 switch (proto) { 783 784 case ETHERTYPE_IP: 785 proto = PPP_IP; /* XXX was 0x21 */ 786 break; 787 788 #ifdef INET6 789 case ETHERTYPE_IPV6: 790 proto = PPP_IPV6; 791 break; 792 #endif 793 794 case ETHERTYPE_DN: 795 proto = PPP_DECNET; 796 break; 797 798 case ETHERTYPE_ATALK: 799 proto = PPP_APPLE; 800 break; 801 802 case ETHERTYPE_NS: 803 proto = PPP_NS; 804 break; 805 806 case LLC_ISO_LSAP: 807 proto = PPP_OSI; 808 break; 809 } 810 break; 811 812 case DLT_PPP_BSDOS: 813 /* 814 * We use Ethernet protocol types inside libpcap; 815 * map them to the corresponding PPP protocol types. 816 */ 817 switch (proto) { 818 819 case ETHERTYPE_IP: 820 b0 = gen_cmp(off_linktype, BPF_H, PPP_IP); 821 b1 = gen_cmp(off_linktype, BPF_H, PPP_VJC); 822 gen_or(b0, b1); 823 b0 = gen_cmp(off_linktype, BPF_H, PPP_VJNC); 824 gen_or(b1, b0); 825 return b0; 826 827 #ifdef INET6 828 case ETHERTYPE_IPV6: 829 proto = PPP_IPV6; 830 /* more to go? */ 831 break; 832 #endif 833 834 case ETHERTYPE_DN: 835 proto = PPP_DECNET; 836 break; 837 838 case ETHERTYPE_ATALK: 839 proto = PPP_APPLE; 840 break; 841 842 case ETHERTYPE_NS: 843 proto = PPP_NS; 844 break; 845 846 case LLC_ISO_LSAP: 847 proto = PPP_OSI; 848 break; 849 } 850 break; 851 852 case DLT_NULL: 853 case DLT_LOOP: 854 /* 855 * For DLT_NULL, the link-layer header is a 32-bit 856 * word containing an AF_ value in *host* byte order. 857 * 858 * In addition, if we're reading a saved capture file, 859 * the host byte order in the capture may not be the 860 * same as the host byte order on this machine. 861 * 862 * For DLT_LOOP, the link-layer header is a 32-bit 863 * word containing an AF_ value in *network* byte order. 864 * 865 * XXX - AF_ values may, unfortunately, be platform- 866 * dependent; for example, FreeBSD's AF_INET6 is 24 867 * whilst NetBSD's and OpenBSD's is 26. 868 * 869 * This means that, when reading a capture file, just 870 * checking for our AF_INET6 value won't work if the 871 * capture file came from another OS. 872 */ 873 switch (proto) { 874 875 case ETHERTYPE_IP: 876 proto = AF_INET; 877 break; 878 879 #ifdef INET6 880 case ETHERTYPE_IPV6: 881 proto = AF_INET6; 882 break; 883 #endif 884 885 default: 886 /* 887 * Not a type on which we support filtering. 888 * XXX - support those that have AF_ values 889 * #defined on this platform, at least? 890 */ 891 return gen_false(); 892 } 893 894 if (linktype == DLT_NULL) { 895 /* 896 * The AF_ value is in host byte order, but 897 * the BPF interpreter will convert it to 898 * network byte order. 899 * 900 * If this is a save file, and it's from a 901 * machine with the opposite byte order to 902 * ours, we byte-swap the AF_ value. 903 * 904 * Then we run it through "htonl()", and 905 * generate code to compare against the result. 906 */ 907 if (bpf_pcap->sf.rfile != NULL && 908 bpf_pcap->sf.swapped) 909 proto = SWAPLONG(proto); 910 proto = htonl(proto); 911 } 912 return (gen_cmp(0, BPF_W, (bpf_int32)proto)); 913 } 914 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto); 915 } 916 917 /* 918 * Check for an LLC SNAP packet with a given organization code and 919 * protocol type; we check the entire contents of the 802.2 LLC and 920 * snap headers, checking for DSAP and SSAP of SNAP and a control 921 * field of 0x03 in the LLC header, and for the specified organization 922 * code and protocol type in the SNAP header. 923 */ 924 static struct block * 925 gen_snap(orgcode, ptype, offset) 926 bpf_u_int32 orgcode; 927 bpf_u_int32 ptype; 928 u_int offset; 929 { 930 u_char snapblock[8]; 931 932 snapblock[0] = LLC_SNAP_LSAP; /* DSAP = SNAP */ 933 snapblock[1] = LLC_SNAP_LSAP; /* SSAP = SNAP */ 934 snapblock[2] = 0x03; /* control = UI */ 935 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */ 936 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */ 937 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */ 938 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */ 939 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */ 940 return gen_bcmp(offset, 8, snapblock); 941 } 942 943 static struct block * 944 gen_hostop(addr, mask, dir, proto, src_off, dst_off) 945 bpf_u_int32 addr; 946 bpf_u_int32 mask; 947 int dir, proto; 948 u_int src_off, dst_off; 949 { 950 struct block *b0, *b1; 951 u_int offset; 952 953 switch (dir) { 954 955 case Q_SRC: 956 offset = src_off; 957 break; 958 959 case Q_DST: 960 offset = dst_off; 961 break; 962 963 case Q_AND: 964 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off); 965 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off); 966 gen_and(b0, b1); 967 return b1; 968 969 case Q_OR: 970 case Q_DEFAULT: 971 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off); 972 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off); 973 gen_or(b0, b1); 974 return b1; 975 976 default: 977 abort(); 978 } 979 b0 = gen_linktype(proto); 980 b1 = gen_mcmp(offset, BPF_W, (bpf_int32)addr, mask); 981 gen_and(b0, b1); 982 return b1; 983 } 984 985 #ifdef INET6 986 static struct block * 987 gen_hostop6(addr, mask, dir, proto, src_off, dst_off) 988 struct in6_addr *addr; 989 struct in6_addr *mask; 990 int dir, proto; 991 u_int src_off, dst_off; 992 { 993 struct block *b0, *b1; 994 u_int offset; 995 u_int32_t *a, *m; 996 997 switch (dir) { 998 999 case Q_SRC: 1000 offset = src_off; 1001 break; 1002 1003 case Q_DST: 1004 offset = dst_off; 1005 break; 1006 1007 case Q_AND: 1008 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off); 1009 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off); 1010 gen_and(b0, b1); 1011 return b1; 1012 1013 case Q_OR: 1014 case Q_DEFAULT: 1015 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off); 1016 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off); 1017 gen_or(b0, b1); 1018 return b1; 1019 1020 default: 1021 abort(); 1022 } 1023 /* this order is important */ 1024 a = (u_int32_t *)addr; 1025 m = (u_int32_t *)mask; 1026 b1 = gen_mcmp(offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3])); 1027 b0 = gen_mcmp(offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2])); 1028 gen_and(b0, b1); 1029 b0 = gen_mcmp(offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1])); 1030 gen_and(b0, b1); 1031 b0 = gen_mcmp(offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0])); 1032 gen_and(b0, b1); 1033 b0 = gen_linktype(proto); 1034 gen_and(b0, b1); 1035 return b1; 1036 } 1037 #endif /*INET6*/ 1038 1039 static struct block * 1040 gen_ehostop(eaddr, dir) 1041 register const u_char *eaddr; 1042 register int dir; 1043 { 1044 register struct block *b0, *b1; 1045 1046 switch (dir) { 1047 case Q_SRC: 1048 return gen_bcmp(6, 6, eaddr); 1049 1050 case Q_DST: 1051 return gen_bcmp(0, 6, eaddr); 1052 1053 case Q_AND: 1054 b0 = gen_ehostop(eaddr, Q_SRC); 1055 b1 = gen_ehostop(eaddr, Q_DST); 1056 gen_and(b0, b1); 1057 return b1; 1058 1059 case Q_DEFAULT: 1060 case Q_OR: 1061 b0 = gen_ehostop(eaddr, Q_SRC); 1062 b1 = gen_ehostop(eaddr, Q_DST); 1063 gen_or(b0, b1); 1064 return b1; 1065 } 1066 abort(); 1067 /* NOTREACHED */ 1068 } 1069 1070 /* 1071 * Like gen_ehostop, but for DLT_FDDI 1072 */ 1073 static struct block * 1074 gen_fhostop(eaddr, dir) 1075 register const u_char *eaddr; 1076 register int dir; 1077 { 1078 struct block *b0, *b1; 1079 1080 switch (dir) { 1081 case Q_SRC: 1082 #ifdef PCAP_FDDIPAD 1083 return gen_bcmp(6 + 1 + pcap_fddipad, 6, eaddr); 1084 #else 1085 return gen_bcmp(6 + 1, 6, eaddr); 1086 #endif 1087 1088 case Q_DST: 1089 #ifdef PCAP_FDDIPAD 1090 return gen_bcmp(0 + 1 + pcap_fddipad, 6, eaddr); 1091 #else 1092 return gen_bcmp(0 + 1, 6, eaddr); 1093 #endif 1094 1095 case Q_AND: 1096 b0 = gen_fhostop(eaddr, Q_SRC); 1097 b1 = gen_fhostop(eaddr, Q_DST); 1098 gen_and(b0, b1); 1099 return b1; 1100 1101 case Q_DEFAULT: 1102 case Q_OR: 1103 b0 = gen_fhostop(eaddr, Q_SRC); 1104 b1 = gen_fhostop(eaddr, Q_DST); 1105 gen_or(b0, b1); 1106 return b1; 1107 } 1108 abort(); 1109 /* NOTREACHED */ 1110 } 1111 1112 /* 1113 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring) 1114 */ 1115 static struct block * 1116 gen_thostop(eaddr, dir) 1117 register const u_char *eaddr; 1118 register int dir; 1119 { 1120 register struct block *b0, *b1; 1121 1122 switch (dir) { 1123 case Q_SRC: 1124 return gen_bcmp(8, 6, eaddr); 1125 1126 case Q_DST: 1127 return gen_bcmp(2, 6, eaddr); 1128 1129 case Q_AND: 1130 b0 = gen_thostop(eaddr, Q_SRC); 1131 b1 = gen_thostop(eaddr, Q_DST); 1132 gen_and(b0, b1); 1133 return b1; 1134 1135 case Q_DEFAULT: 1136 case Q_OR: 1137 b0 = gen_thostop(eaddr, Q_SRC); 1138 b1 = gen_thostop(eaddr, Q_DST); 1139 gen_or(b0, b1); 1140 return b1; 1141 } 1142 abort(); 1143 /* NOTREACHED */ 1144 } 1145 1146 /* 1147 * This is quite tricky because there may be pad bytes in front of the 1148 * DECNET header, and then there are two possible data packet formats that 1149 * carry both src and dst addresses, plus 5 packet types in a format that 1150 * carries only the src node, plus 2 types that use a different format and 1151 * also carry just the src node. 1152 * 1153 * Yuck. 1154 * 1155 * Instead of doing those all right, we just look for data packets with 1156 * 0 or 1 bytes of padding. If you want to look at other packets, that 1157 * will require a lot more hacking. 1158 * 1159 * To add support for filtering on DECNET "areas" (network numbers) 1160 * one would want to add a "mask" argument to this routine. That would 1161 * make the filter even more inefficient, although one could be clever 1162 * and not generate masking instructions if the mask is 0xFFFF. 1163 */ 1164 static struct block * 1165 gen_dnhostop(addr, dir, base_off) 1166 bpf_u_int32 addr; 1167 int dir; 1168 u_int base_off; 1169 { 1170 struct block *b0, *b1, *b2, *tmp; 1171 u_int offset_lh; /* offset if long header is received */ 1172 u_int offset_sh; /* offset if short header is received */ 1173 1174 switch (dir) { 1175 1176 case Q_DST: 1177 offset_sh = 1; /* follows flags */ 1178 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */ 1179 break; 1180 1181 case Q_SRC: 1182 offset_sh = 3; /* follows flags, dstnode */ 1183 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */ 1184 break; 1185 1186 case Q_AND: 1187 /* Inefficient because we do our Calvinball dance twice */ 1188 b0 = gen_dnhostop(addr, Q_SRC, base_off); 1189 b1 = gen_dnhostop(addr, Q_DST, base_off); 1190 gen_and(b0, b1); 1191 return b1; 1192 1193 case Q_OR: 1194 case Q_DEFAULT: 1195 /* Inefficient because we do our Calvinball dance twice */ 1196 b0 = gen_dnhostop(addr, Q_SRC, base_off); 1197 b1 = gen_dnhostop(addr, Q_DST, base_off); 1198 gen_or(b0, b1); 1199 return b1; 1200 1201 case Q_ISO: 1202 bpf_error("ISO host filtering not implemented"); 1203 1204 default: 1205 abort(); 1206 } 1207 b0 = gen_linktype(ETHERTYPE_DN); 1208 /* Check for pad = 1, long header case */ 1209 tmp = gen_mcmp(base_off + 2, BPF_H, 1210 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF)); 1211 b1 = gen_cmp(base_off + 2 + 1 + offset_lh, 1212 BPF_H, (bpf_int32)ntohs(addr)); 1213 gen_and(tmp, b1); 1214 /* Check for pad = 0, long header case */ 1215 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7); 1216 b2 = gen_cmp(base_off + 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr)); 1217 gen_and(tmp, b2); 1218 gen_or(b2, b1); 1219 /* Check for pad = 1, short header case */ 1220 tmp = gen_mcmp(base_off + 2, BPF_H, 1221 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF)); 1222 b2 = gen_cmp(base_off + 2 + 1 + offset_sh, 1223 BPF_H, (bpf_int32)ntohs(addr)); 1224 gen_and(tmp, b2); 1225 gen_or(b2, b1); 1226 /* Check for pad = 0, short header case */ 1227 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7); 1228 b2 = gen_cmp(base_off + 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr)); 1229 gen_and(tmp, b2); 1230 gen_or(b2, b1); 1231 1232 /* Combine with test for linktype */ 1233 gen_and(b0, b1); 1234 return b1; 1235 } 1236 1237 static struct block * 1238 gen_host(addr, mask, proto, dir) 1239 bpf_u_int32 addr; 1240 bpf_u_int32 mask; 1241 int proto; 1242 int dir; 1243 { 1244 struct block *b0, *b1; 1245 1246 switch (proto) { 1247 1248 case Q_DEFAULT: 1249 b0 = gen_host(addr, mask, Q_IP, dir); 1250 if (off_linktype != -1) { 1251 b1 = gen_host(addr, mask, Q_ARP, dir); 1252 gen_or(b0, b1); 1253 b0 = gen_host(addr, mask, Q_RARP, dir); 1254 gen_or(b1, b0); 1255 } 1256 return b0; 1257 1258 case Q_IP: 1259 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 1260 off_nl + 12, off_nl + 16); 1261 1262 case Q_RARP: 1263 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 1264 off_nl + 14, off_nl + 24); 1265 1266 case Q_ARP: 1267 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 1268 off_nl + 14, off_nl + 24); 1269 1270 case Q_TCP: 1271 bpf_error("'tcp' modifier applied to host"); 1272 1273 case Q_UDP: 1274 bpf_error("'udp' modifier applied to host"); 1275 1276 case Q_ICMP: 1277 bpf_error("'icmp' modifier applied to host"); 1278 1279 case Q_IGMP: 1280 bpf_error("'igmp' modifier applied to host"); 1281 1282 case Q_IGRP: 1283 bpf_error("'igrp' modifier applied to host"); 1284 1285 case Q_PIM: 1286 bpf_error("'pim' modifier applied to host"); 1287 1288 case Q_ATALK: 1289 bpf_error("ATALK host filtering not implemented"); 1290 1291 case Q_AARP: 1292 bpf_error("AARP host filtering not implemented"); 1293 1294 case Q_DECNET: 1295 return gen_dnhostop(addr, dir, off_nl); 1296 1297 case Q_SCA: 1298 bpf_error("SCA host filtering not implemented"); 1299 1300 case Q_LAT: 1301 bpf_error("LAT host filtering not implemented"); 1302 1303 case Q_MOPDL: 1304 bpf_error("MOPDL host filtering not implemented"); 1305 1306 case Q_MOPRC: 1307 bpf_error("MOPRC host filtering not implemented"); 1308 1309 #ifdef INET6 1310 case Q_IPV6: 1311 bpf_error("'ip6' modifier applied to ip host"); 1312 1313 case Q_ICMPV6: 1314 bpf_error("'icmp6' modifier applied to host"); 1315 #endif /* INET6 */ 1316 1317 case Q_AH: 1318 bpf_error("'ah' modifier applied to host"); 1319 1320 case Q_ESP: 1321 bpf_error("'esp' modifier applied to host"); 1322 1323 case Q_ISO: 1324 bpf_error("ISO host filtering not implemented"); 1325 1326 case Q_ESIS: 1327 bpf_error("'esis' modifier applied to host"); 1328 1329 case Q_ISIS: 1330 bpf_error("'isis' modifier applied to host"); 1331 1332 case Q_CLNP: 1333 bpf_error("'clnp' modifier applied to host"); 1334 1335 default: 1336 abort(); 1337 } 1338 /* NOTREACHED */ 1339 } 1340 1341 #ifdef INET6 1342 static struct block * 1343 gen_host6(addr, mask, proto, dir) 1344 struct in6_addr *addr; 1345 struct in6_addr *mask; 1346 int proto; 1347 int dir; 1348 { 1349 switch (proto) { 1350 1351 case Q_DEFAULT: 1352 return gen_host6(addr, mask, Q_IPV6, dir); 1353 1354 case Q_IP: 1355 bpf_error("'ip' modifier applied to ip6 host"); 1356 1357 case Q_RARP: 1358 bpf_error("'rarp' modifier applied to ip6 host"); 1359 1360 case Q_ARP: 1361 bpf_error("'arp' modifier applied to ip6 host"); 1362 1363 case Q_TCP: 1364 bpf_error("'tcp' modifier applied to host"); 1365 1366 case Q_UDP: 1367 bpf_error("'udp' modifier applied to host"); 1368 1369 case Q_ICMP: 1370 bpf_error("'icmp' modifier applied to host"); 1371 1372 case Q_IGMP: 1373 bpf_error("'igmp' modifier applied to host"); 1374 1375 case Q_IGRP: 1376 bpf_error("'igrp' modifier applied to host"); 1377 1378 case Q_PIM: 1379 bpf_error("'pim' modifier applied to host"); 1380 1381 case Q_ATALK: 1382 bpf_error("ATALK host filtering not implemented"); 1383 1384 case Q_AARP: 1385 bpf_error("AARP host filtering not implemented"); 1386 1387 case Q_DECNET: 1388 bpf_error("'decnet' modifier applied to ip6 host"); 1389 1390 case Q_SCA: 1391 bpf_error("SCA host filtering not implemented"); 1392 1393 case Q_LAT: 1394 bpf_error("LAT host filtering not implemented"); 1395 1396 case Q_MOPDL: 1397 bpf_error("MOPDL host filtering not implemented"); 1398 1399 case Q_MOPRC: 1400 bpf_error("MOPRC host filtering not implemented"); 1401 1402 case Q_IPV6: 1403 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 1404 off_nl + 8, off_nl + 24); 1405 1406 case Q_ICMPV6: 1407 bpf_error("'icmp6' modifier applied to host"); 1408 1409 case Q_AH: 1410 bpf_error("'ah' modifier applied to host"); 1411 1412 case Q_ESP: 1413 bpf_error("'esp' modifier applied to host"); 1414 1415 case Q_ISO: 1416 bpf_error("ISO host filtering not implemented"); 1417 1418 case Q_ESIS: 1419 bpf_error("'esis' modifier applied to host"); 1420 1421 case Q_ISIS: 1422 bpf_error("'isis' modifier applied to host"); 1423 1424 case Q_CLNP: 1425 bpf_error("'clnp' modifier applied to host"); 1426 1427 default: 1428 abort(); 1429 } 1430 /* NOTREACHED */ 1431 } 1432 #endif /*INET6*/ 1433 1434 #ifndef INET6 1435 static struct block * 1436 gen_gateway(eaddr, alist, proto, dir) 1437 const u_char *eaddr; 1438 bpf_u_int32 **alist; 1439 int proto; 1440 int dir; 1441 { 1442 struct block *b0, *b1, *tmp; 1443 1444 if (dir != 0) 1445 bpf_error("direction applied to 'gateway'"); 1446 1447 switch (proto) { 1448 case Q_DEFAULT: 1449 case Q_IP: 1450 case Q_ARP: 1451 case Q_RARP: 1452 if (linktype == DLT_EN10MB) 1453 b0 = gen_ehostop(eaddr, Q_OR); 1454 else if (linktype == DLT_FDDI) 1455 b0 = gen_fhostop(eaddr, Q_OR); 1456 else if (linktype == DLT_IEEE802) 1457 b0 = gen_thostop(eaddr, Q_OR); 1458 else 1459 bpf_error( 1460 "'gateway' supported only on ethernet, FDDI or token ring"); 1461 1462 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR); 1463 while (*alist) { 1464 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR); 1465 gen_or(b1, tmp); 1466 b1 = tmp; 1467 } 1468 gen_not(b1); 1469 gen_and(b0, b1); 1470 return b1; 1471 } 1472 bpf_error("illegal modifier of 'gateway'"); 1473 /* NOTREACHED */ 1474 } 1475 #endif 1476 1477 struct block * 1478 gen_proto_abbrev(proto) 1479 int proto; 1480 { 1481 #ifdef INET6 1482 struct block *b0; 1483 #endif 1484 struct block *b1; 1485 1486 switch (proto) { 1487 1488 case Q_TCP: 1489 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT); 1490 #ifdef INET6 1491 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT); 1492 gen_or(b0, b1); 1493 #endif 1494 break; 1495 1496 case Q_UDP: 1497 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT); 1498 #ifdef INET6 1499 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT); 1500 gen_or(b0, b1); 1501 #endif 1502 break; 1503 1504 case Q_ICMP: 1505 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT); 1506 break; 1507 1508 #ifndef IPPROTO_IGMP 1509 #define IPPROTO_IGMP 2 1510 #endif 1511 1512 case Q_IGMP: 1513 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT); 1514 break; 1515 1516 #ifndef IPPROTO_IGRP 1517 #define IPPROTO_IGRP 9 1518 #endif 1519 case Q_IGRP: 1520 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT); 1521 break; 1522 1523 #ifndef IPPROTO_PIM 1524 #define IPPROTO_PIM 103 1525 #endif 1526 1527 case Q_PIM: 1528 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT); 1529 #ifdef INET6 1530 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT); 1531 gen_or(b0, b1); 1532 #endif 1533 break; 1534 1535 case Q_IP: 1536 b1 = gen_linktype(ETHERTYPE_IP); 1537 break; 1538 1539 case Q_ARP: 1540 b1 = gen_linktype(ETHERTYPE_ARP); 1541 break; 1542 1543 case Q_RARP: 1544 b1 = gen_linktype(ETHERTYPE_REVARP); 1545 break; 1546 1547 case Q_LINK: 1548 bpf_error("link layer applied in wrong context"); 1549 1550 case Q_ATALK: 1551 b1 = gen_linktype(ETHERTYPE_ATALK); 1552 break; 1553 1554 case Q_AARP: 1555 b1 = gen_linktype(ETHERTYPE_AARP); 1556 break; 1557 1558 case Q_DECNET: 1559 b1 = gen_linktype(ETHERTYPE_DN); 1560 break; 1561 1562 case Q_SCA: 1563 b1 = gen_linktype(ETHERTYPE_SCA); 1564 break; 1565 1566 case Q_LAT: 1567 b1 = gen_linktype(ETHERTYPE_LAT); 1568 break; 1569 1570 case Q_MOPDL: 1571 b1 = gen_linktype(ETHERTYPE_MOPDL); 1572 break; 1573 1574 case Q_MOPRC: 1575 b1 = gen_linktype(ETHERTYPE_MOPRC); 1576 break; 1577 1578 #ifdef INET6 1579 case Q_IPV6: 1580 b1 = gen_linktype(ETHERTYPE_IPV6); 1581 break; 1582 1583 #ifndef IPPROTO_ICMPV6 1584 #define IPPROTO_ICMPV6 58 1585 #endif 1586 case Q_ICMPV6: 1587 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT); 1588 break; 1589 #endif /* INET6 */ 1590 1591 #ifndef IPPROTO_AH 1592 #define IPPROTO_AH 51 1593 #endif 1594 case Q_AH: 1595 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT); 1596 #ifdef INET6 1597 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT); 1598 gen_or(b0, b1); 1599 #endif 1600 break; 1601 1602 #ifndef IPPROTO_ESP 1603 #define IPPROTO_ESP 50 1604 #endif 1605 case Q_ESP: 1606 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT); 1607 #ifdef INET6 1608 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT); 1609 gen_or(b0, b1); 1610 #endif 1611 break; 1612 1613 case Q_ISO: 1614 b1 = gen_linktype(LLC_ISO_LSAP); 1615 break; 1616 1617 case Q_ESIS: 1618 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT); 1619 break; 1620 1621 case Q_ISIS: 1622 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT); 1623 break; 1624 1625 case Q_CLNP: 1626 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT); 1627 break; 1628 1629 default: 1630 abort(); 1631 } 1632 return b1; 1633 } 1634 1635 static struct block * 1636 gen_ipfrag() 1637 { 1638 struct slist *s; 1639 struct block *b; 1640 1641 /* not ip frag */ 1642 s = new_stmt(BPF_LD|BPF_H|BPF_ABS); 1643 s->s.k = off_nl + 6; 1644 b = new_block(JMP(BPF_JSET)); 1645 b->s.k = 0x1fff; 1646 b->stmts = s; 1647 gen_not(b); 1648 1649 return b; 1650 } 1651 1652 static struct block * 1653 gen_portatom(off, v) 1654 int off; 1655 bpf_int32 v; 1656 { 1657 struct slist *s; 1658 struct block *b; 1659 1660 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B); 1661 s->s.k = off_nl; 1662 1663 s->next = new_stmt(BPF_LD|BPF_IND|BPF_H); 1664 s->next->s.k = off_nl + off; 1665 1666 b = new_block(JMP(BPF_JEQ)); 1667 b->stmts = s; 1668 b->s.k = v; 1669 1670 return b; 1671 } 1672 1673 #ifdef INET6 1674 static struct block * 1675 gen_portatom6(off, v) 1676 int off; 1677 bpf_int32 v; 1678 { 1679 return gen_cmp(off_nl + 40 + off, BPF_H, v); 1680 } 1681 #endif/*INET6*/ 1682 1683 struct block * 1684 gen_portop(port, proto, dir) 1685 int port, proto, dir; 1686 { 1687 struct block *b0, *b1, *tmp; 1688 1689 /* ip proto 'proto' */ 1690 tmp = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)proto); 1691 b0 = gen_ipfrag(); 1692 gen_and(tmp, b0); 1693 1694 switch (dir) { 1695 case Q_SRC: 1696 b1 = gen_portatom(0, (bpf_int32)port); 1697 break; 1698 1699 case Q_DST: 1700 b1 = gen_portatom(2, (bpf_int32)port); 1701 break; 1702 1703 case Q_OR: 1704 case Q_DEFAULT: 1705 tmp = gen_portatom(0, (bpf_int32)port); 1706 b1 = gen_portatom(2, (bpf_int32)port); 1707 gen_or(tmp, b1); 1708 break; 1709 1710 case Q_AND: 1711 tmp = gen_portatom(0, (bpf_int32)port); 1712 b1 = gen_portatom(2, (bpf_int32)port); 1713 gen_and(tmp, b1); 1714 break; 1715 1716 default: 1717 abort(); 1718 } 1719 gen_and(b0, b1); 1720 1721 return b1; 1722 } 1723 1724 static struct block * 1725 gen_port(port, ip_proto, dir) 1726 int port; 1727 int ip_proto; 1728 int dir; 1729 { 1730 struct block *b0, *b1, *tmp; 1731 1732 /* ether proto ip */ 1733 b0 = gen_linktype(ETHERTYPE_IP); 1734 1735 switch (ip_proto) { 1736 case IPPROTO_UDP: 1737 case IPPROTO_TCP: 1738 b1 = gen_portop(port, ip_proto, dir); 1739 break; 1740 1741 case PROTO_UNDEF: 1742 tmp = gen_portop(port, IPPROTO_TCP, dir); 1743 b1 = gen_portop(port, IPPROTO_UDP, dir); 1744 gen_or(tmp, b1); 1745 break; 1746 1747 default: 1748 abort(); 1749 } 1750 gen_and(b0, b1); 1751 return b1; 1752 } 1753 1754 #ifdef INET6 1755 struct block * 1756 gen_portop6(port, proto, dir) 1757 int port, proto, dir; 1758 { 1759 struct block *b0, *b1, *tmp; 1760 1761 /* ip proto 'proto' */ 1762 b0 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)proto); 1763 1764 switch (dir) { 1765 case Q_SRC: 1766 b1 = gen_portatom6(0, (bpf_int32)port); 1767 break; 1768 1769 case Q_DST: 1770 b1 = gen_portatom6(2, (bpf_int32)port); 1771 break; 1772 1773 case Q_OR: 1774 case Q_DEFAULT: 1775 tmp = gen_portatom6(0, (bpf_int32)port); 1776 b1 = gen_portatom6(2, (bpf_int32)port); 1777 gen_or(tmp, b1); 1778 break; 1779 1780 case Q_AND: 1781 tmp = gen_portatom6(0, (bpf_int32)port); 1782 b1 = gen_portatom6(2, (bpf_int32)port); 1783 gen_and(tmp, b1); 1784 break; 1785 1786 default: 1787 abort(); 1788 } 1789 gen_and(b0, b1); 1790 1791 return b1; 1792 } 1793 1794 static struct block * 1795 gen_port6(port, ip_proto, dir) 1796 int port; 1797 int ip_proto; 1798 int dir; 1799 { 1800 struct block *b0, *b1, *tmp; 1801 1802 /* ether proto ip */ 1803 b0 = gen_linktype(ETHERTYPE_IPV6); 1804 1805 switch (ip_proto) { 1806 case IPPROTO_UDP: 1807 case IPPROTO_TCP: 1808 b1 = gen_portop6(port, ip_proto, dir); 1809 break; 1810 1811 case PROTO_UNDEF: 1812 tmp = gen_portop6(port, IPPROTO_TCP, dir); 1813 b1 = gen_portop6(port, IPPROTO_UDP, dir); 1814 gen_or(tmp, b1); 1815 break; 1816 1817 default: 1818 abort(); 1819 } 1820 gen_and(b0, b1); 1821 return b1; 1822 } 1823 #endif /* INET6 */ 1824 1825 static int 1826 lookup_proto(name, proto) 1827 register const char *name; 1828 register int proto; 1829 { 1830 register int v; 1831 1832 switch (proto) { 1833 1834 case Q_DEFAULT: 1835 case Q_IP: 1836 v = pcap_nametoproto(name); 1837 if (v == PROTO_UNDEF) 1838 bpf_error("unknown ip proto '%s'", name); 1839 break; 1840 1841 case Q_LINK: 1842 /* XXX should look up h/w protocol type based on linktype */ 1843 v = pcap_nametoeproto(name); 1844 if (v == PROTO_UNDEF) 1845 bpf_error("unknown ether proto '%s'", name); 1846 break; 1847 1848 case Q_ISO: 1849 if (strcmp(name, "esis") == 0) 1850 v = ISO9542_ESIS; 1851 else if (strcmp(name, "isis") == 0) 1852 v = ISO10589_ISIS; 1853 else if (strcmp(name, "clnp") == 0) 1854 v = ISO8473_CLNP; 1855 else 1856 bpf_error("unknown osi proto '%s'", name); 1857 break; 1858 1859 default: 1860 v = PROTO_UNDEF; 1861 break; 1862 } 1863 return v; 1864 } 1865 1866 #if 0 1867 struct stmt * 1868 gen_joinsp(s, n) 1869 struct stmt **s; 1870 int n; 1871 { 1872 return NULL; 1873 } 1874 #endif 1875 1876 static struct block * 1877 gen_protochain(v, proto, dir) 1878 int v; 1879 int proto; 1880 int dir; 1881 { 1882 #ifdef NO_PROTOCHAIN 1883 return gen_proto(v, proto, dir); 1884 #else 1885 struct block *b0, *b; 1886 struct slist *s[100]; 1887 int fix2, fix3, fix4, fix5; 1888 int ahcheck, again, end; 1889 int i, max; 1890 int reg2 = alloc_reg(); 1891 1892 memset(s, 0, sizeof(s)); 1893 fix2 = fix3 = fix4 = fix5 = 0; 1894 1895 switch (proto) { 1896 case Q_IP: 1897 case Q_IPV6: 1898 break; 1899 case Q_DEFAULT: 1900 b0 = gen_protochain(v, Q_IP, dir); 1901 b = gen_protochain(v, Q_IPV6, dir); 1902 gen_or(b0, b); 1903 return b; 1904 default: 1905 bpf_error("bad protocol applied for 'protochain'"); 1906 /*NOTREACHED*/ 1907 } 1908 1909 no_optimize = 1; /*this code is not compatible with optimzer yet */ 1910 1911 /* 1912 * s[0] is a dummy entry to protect other BPF insn from damaged 1913 * by s[fix] = foo with uninitialized variable "fix". It is somewhat 1914 * hard to find interdependency made by jump table fixup. 1915 */ 1916 i = 0; 1917 s[i] = new_stmt(0); /*dummy*/ 1918 i++; 1919 1920 switch (proto) { 1921 case Q_IP: 1922 b0 = gen_linktype(ETHERTYPE_IP); 1923 1924 /* A = ip->ip_p */ 1925 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B); 1926 s[i]->s.k = off_nl + 9; 1927 i++; 1928 /* X = ip->ip_hl << 2 */ 1929 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B); 1930 s[i]->s.k = off_nl; 1931 i++; 1932 break; 1933 #ifdef INET6 1934 case Q_IPV6: 1935 b0 = gen_linktype(ETHERTYPE_IPV6); 1936 1937 /* A = ip6->ip_nxt */ 1938 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B); 1939 s[i]->s.k = off_nl + 6; 1940 i++; 1941 /* X = sizeof(struct ip6_hdr) */ 1942 s[i] = new_stmt(BPF_LDX|BPF_IMM); 1943 s[i]->s.k = 40; 1944 i++; 1945 break; 1946 #endif 1947 default: 1948 bpf_error("unsupported proto to gen_protochain"); 1949 /*NOTREACHED*/ 1950 } 1951 1952 /* again: if (A == v) goto end; else fall through; */ 1953 again = i; 1954 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); 1955 s[i]->s.k = v; 1956 s[i]->s.jt = NULL; /*later*/ 1957 s[i]->s.jf = NULL; /*update in next stmt*/ 1958 fix5 = i; 1959 i++; 1960 1961 #ifndef IPPROTO_NONE 1962 #define IPPROTO_NONE 59 1963 #endif 1964 /* if (A == IPPROTO_NONE) goto end */ 1965 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); 1966 s[i]->s.jt = NULL; /*later*/ 1967 s[i]->s.jf = NULL; /*update in next stmt*/ 1968 s[i]->s.k = IPPROTO_NONE; 1969 s[fix5]->s.jf = s[i]; 1970 fix2 = i; 1971 i++; 1972 1973 #ifdef INET6 1974 if (proto == Q_IPV6) { 1975 int v6start, v6end, v6advance, j; 1976 1977 v6start = i; 1978 /* if (A == IPPROTO_HOPOPTS) goto v6advance */ 1979 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); 1980 s[i]->s.jt = NULL; /*later*/ 1981 s[i]->s.jf = NULL; /*update in next stmt*/ 1982 s[i]->s.k = IPPROTO_HOPOPTS; 1983 s[fix2]->s.jf = s[i]; 1984 i++; 1985 /* if (A == IPPROTO_DSTOPTS) goto v6advance */ 1986 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); 1987 s[i]->s.jt = NULL; /*later*/ 1988 s[i]->s.jf = NULL; /*update in next stmt*/ 1989 s[i]->s.k = IPPROTO_DSTOPTS; 1990 i++; 1991 /* if (A == IPPROTO_ROUTING) goto v6advance */ 1992 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); 1993 s[i]->s.jt = NULL; /*later*/ 1994 s[i]->s.jf = NULL; /*update in next stmt*/ 1995 s[i]->s.k = IPPROTO_ROUTING; 1996 i++; 1997 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */ 1998 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); 1999 s[i]->s.jt = NULL; /*later*/ 2000 s[i]->s.jf = NULL; /*later*/ 2001 s[i]->s.k = IPPROTO_FRAGMENT; 2002 fix3 = i; 2003 v6end = i; 2004 i++; 2005 2006 /* v6advance: */ 2007 v6advance = i; 2008 2009 /* 2010 * in short, 2011 * A = P[X]; 2012 * X = X + (P[X + 1] + 1) * 8; 2013 */ 2014 /* A = X */ 2015 s[i] = new_stmt(BPF_MISC|BPF_TXA); 2016 i++; 2017 /* A = P[X + packet head] */ 2018 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B); 2019 s[i]->s.k = off_nl; 2020 i++; 2021 /* MEM[reg2] = A */ 2022 s[i] = new_stmt(BPF_ST); 2023 s[i]->s.k = reg2; 2024 i++; 2025 /* A = X */ 2026 s[i] = new_stmt(BPF_MISC|BPF_TXA); 2027 i++; 2028 /* A += 1 */ 2029 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); 2030 s[i]->s.k = 1; 2031 i++; 2032 /* X = A */ 2033 s[i] = new_stmt(BPF_MISC|BPF_TAX); 2034 i++; 2035 /* A = P[X + packet head]; */ 2036 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B); 2037 s[i]->s.k = off_nl; 2038 i++; 2039 /* A += 1 */ 2040 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); 2041 s[i]->s.k = 1; 2042 i++; 2043 /* A *= 8 */ 2044 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K); 2045 s[i]->s.k = 8; 2046 i++; 2047 /* X = A; */ 2048 s[i] = new_stmt(BPF_MISC|BPF_TAX); 2049 i++; 2050 /* A = MEM[reg2] */ 2051 s[i] = new_stmt(BPF_LD|BPF_MEM); 2052 s[i]->s.k = reg2; 2053 i++; 2054 2055 /* goto again; (must use BPF_JA for backward jump) */ 2056 s[i] = new_stmt(BPF_JMP|BPF_JA); 2057 s[i]->s.k = again - i - 1; 2058 s[i - 1]->s.jf = s[i]; 2059 i++; 2060 2061 /* fixup */ 2062 for (j = v6start; j <= v6end; j++) 2063 s[j]->s.jt = s[v6advance]; 2064 } else 2065 #endif 2066 { 2067 /* nop */ 2068 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); 2069 s[i]->s.k = 0; 2070 s[fix2]->s.jf = s[i]; 2071 i++; 2072 } 2073 2074 /* ahcheck: */ 2075 ahcheck = i; 2076 /* if (A == IPPROTO_AH) then fall through; else goto end; */ 2077 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K); 2078 s[i]->s.jt = NULL; /*later*/ 2079 s[i]->s.jf = NULL; /*later*/ 2080 s[i]->s.k = IPPROTO_AH; 2081 if (fix3) 2082 s[fix3]->s.jf = s[ahcheck]; 2083 fix4 = i; 2084 i++; 2085 2086 /* 2087 * in short, 2088 * A = P[X]; 2089 * X = X + (P[X + 1] + 2) * 4; 2090 */ 2091 /* A = X */ 2092 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA); 2093 i++; 2094 /* A = P[X + packet head]; */ 2095 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B); 2096 s[i]->s.k = off_nl; 2097 i++; 2098 /* MEM[reg2] = A */ 2099 s[i] = new_stmt(BPF_ST); 2100 s[i]->s.k = reg2; 2101 i++; 2102 /* A = X */ 2103 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA); 2104 i++; 2105 /* A += 1 */ 2106 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); 2107 s[i]->s.k = 1; 2108 i++; 2109 /* X = A */ 2110 s[i] = new_stmt(BPF_MISC|BPF_TAX); 2111 i++; 2112 /* A = P[X + packet head] */ 2113 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B); 2114 s[i]->s.k = off_nl; 2115 i++; 2116 /* A += 2 */ 2117 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); 2118 s[i]->s.k = 2; 2119 i++; 2120 /* A *= 4 */ 2121 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K); 2122 s[i]->s.k = 4; 2123 i++; 2124 /* X = A; */ 2125 s[i] = new_stmt(BPF_MISC|BPF_TAX); 2126 i++; 2127 /* A = MEM[reg2] */ 2128 s[i] = new_stmt(BPF_LD|BPF_MEM); 2129 s[i]->s.k = reg2; 2130 i++; 2131 2132 /* goto again; (must use BPF_JA for backward jump) */ 2133 s[i] = new_stmt(BPF_JMP|BPF_JA); 2134 s[i]->s.k = again - i - 1; 2135 i++; 2136 2137 /* end: nop */ 2138 end = i; 2139 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K); 2140 s[i]->s.k = 0; 2141 s[fix2]->s.jt = s[end]; 2142 s[fix4]->s.jf = s[end]; 2143 s[fix5]->s.jt = s[end]; 2144 i++; 2145 2146 /* 2147 * make slist chain 2148 */ 2149 max = i; 2150 for (i = 0; i < max - 1; i++) 2151 s[i]->next = s[i + 1]; 2152 s[max - 1]->next = NULL; 2153 2154 /* 2155 * emit final check 2156 */ 2157 b = new_block(JMP(BPF_JEQ)); 2158 b->stmts = s[1]; /*remember, s[0] is dummy*/ 2159 b->s.k = v; 2160 2161 free_reg(reg2); 2162 2163 gen_and(b0, b); 2164 return b; 2165 #endif 2166 } 2167 2168 static struct block * 2169 gen_proto(v, proto, dir) 2170 int v; 2171 int proto; 2172 int dir; 2173 { 2174 struct block *b0, *b1; 2175 2176 if (dir != Q_DEFAULT) 2177 bpf_error("direction applied to 'proto'"); 2178 2179 switch (proto) { 2180 case Q_DEFAULT: 2181 #ifdef INET6 2182 b0 = gen_proto(v, Q_IP, dir); 2183 b1 = gen_proto(v, Q_IPV6, dir); 2184 gen_or(b0, b1); 2185 return b1; 2186 #else 2187 /*FALLTHROUGH*/ 2188 #endif 2189 case Q_IP: 2190 b0 = gen_linktype(ETHERTYPE_IP); 2191 #ifndef CHASE_CHAIN 2192 b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)v); 2193 #else 2194 b1 = gen_protochain(v, Q_IP); 2195 #endif 2196 gen_and(b0, b1); 2197 return b1; 2198 2199 case Q_ISO: 2200 b0 = gen_linktype(LLC_ISO_LSAP); 2201 b1 = gen_cmp(off_nl + 3, BPF_B, (long)v); 2202 gen_and(b0, b1); 2203 return b1; 2204 2205 case Q_ARP: 2206 bpf_error("arp does not encapsulate another protocol"); 2207 /* NOTREACHED */ 2208 2209 case Q_RARP: 2210 bpf_error("rarp does not encapsulate another protocol"); 2211 /* NOTREACHED */ 2212 2213 case Q_ATALK: 2214 bpf_error("atalk encapsulation is not specifiable"); 2215 /* NOTREACHED */ 2216 2217 case Q_DECNET: 2218 bpf_error("decnet encapsulation is not specifiable"); 2219 /* NOTREACHED */ 2220 2221 case Q_SCA: 2222 bpf_error("sca does not encapsulate another protocol"); 2223 /* NOTREACHED */ 2224 2225 case Q_LAT: 2226 bpf_error("lat does not encapsulate another protocol"); 2227 /* NOTREACHED */ 2228 2229 case Q_MOPRC: 2230 bpf_error("moprc does not encapsulate another protocol"); 2231 /* NOTREACHED */ 2232 2233 case Q_MOPDL: 2234 bpf_error("mopdl does not encapsulate another protocol"); 2235 /* NOTREACHED */ 2236 2237 case Q_LINK: 2238 return gen_linktype(v); 2239 2240 case Q_UDP: 2241 bpf_error("'udp proto' is bogus"); 2242 /* NOTREACHED */ 2243 2244 case Q_TCP: 2245 bpf_error("'tcp proto' is bogus"); 2246 /* NOTREACHED */ 2247 2248 case Q_ICMP: 2249 bpf_error("'icmp proto' is bogus"); 2250 /* NOTREACHED */ 2251 2252 case Q_IGMP: 2253 bpf_error("'igmp proto' is bogus"); 2254 /* NOTREACHED */ 2255 2256 case Q_IGRP: 2257 bpf_error("'igrp proto' is bogus"); 2258 /* NOTREACHED */ 2259 2260 case Q_PIM: 2261 bpf_error("'pim proto' is bogus"); 2262 /* NOTREACHED */ 2263 2264 #ifdef INET6 2265 case Q_IPV6: 2266 b0 = gen_linktype(ETHERTYPE_IPV6); 2267 #ifndef CHASE_CHAIN 2268 b1 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)v); 2269 #else 2270 b1 = gen_protochain(v, Q_IPV6); 2271 #endif 2272 gen_and(b0, b1); 2273 return b1; 2274 2275 case Q_ICMPV6: 2276 bpf_error("'icmp6 proto' is bogus"); 2277 #endif /* INET6 */ 2278 2279 case Q_AH: 2280 bpf_error("'ah proto' is bogus"); 2281 2282 case Q_ESP: 2283 bpf_error("'ah proto' is bogus"); 2284 2285 default: 2286 abort(); 2287 /* NOTREACHED */ 2288 } 2289 /* NOTREACHED */ 2290 } 2291 2292 struct block * 2293 gen_scode(name, q) 2294 register const char *name; 2295 struct qual q; 2296 { 2297 int proto = q.proto; 2298 int dir = q.dir; 2299 int tproto; 2300 u_char *eaddr; 2301 bpf_u_int32 mask, addr; 2302 #ifndef INET6 2303 bpf_u_int32 **alist; 2304 #else 2305 int tproto6; 2306 struct sockaddr_in *sin; 2307 struct sockaddr_in6 *sin6; 2308 struct addrinfo *res, *res0; 2309 struct in6_addr mask128; 2310 #endif /*INET6*/ 2311 struct block *b, *tmp; 2312 int port, real_proto; 2313 2314 switch (q.addr) { 2315 2316 case Q_NET: 2317 addr = pcap_nametonetaddr(name); 2318 if (addr == 0) 2319 bpf_error("unknown network '%s'", name); 2320 /* Left justify network addr and calculate its network mask */ 2321 mask = 0xffffffff; 2322 while (addr && (addr & 0xff000000) == 0) { 2323 addr <<= 8; 2324 mask <<= 8; 2325 } 2326 return gen_host(addr, mask, proto, dir); 2327 2328 case Q_DEFAULT: 2329 case Q_HOST: 2330 if (proto == Q_LINK) { 2331 switch (linktype) { 2332 2333 case DLT_EN10MB: 2334 eaddr = pcap_ether_hostton(name); 2335 if (eaddr == NULL) 2336 bpf_error( 2337 "unknown ether host '%s'", name); 2338 return gen_ehostop(eaddr, dir); 2339 2340 case DLT_FDDI: 2341 eaddr = pcap_ether_hostton(name); 2342 if (eaddr == NULL) 2343 bpf_error( 2344 "unknown FDDI host '%s'", name); 2345 return gen_fhostop(eaddr, dir); 2346 2347 case DLT_IEEE802: 2348 eaddr = pcap_ether_hostton(name); 2349 if (eaddr == NULL) 2350 bpf_error( 2351 "unknown token ring host '%s'", name); 2352 return gen_thostop(eaddr, dir); 2353 2354 default: 2355 bpf_error( 2356 "only ethernet/FDDI/token ring supports link-level host name"); 2357 break; 2358 } 2359 } else if (proto == Q_DECNET) { 2360 unsigned short dn_addr = __pcap_nametodnaddr(name); 2361 /* 2362 * I don't think DECNET hosts can be multihomed, so 2363 * there is no need to build up a list of addresses 2364 */ 2365 return (gen_host(dn_addr, 0, proto, dir)); 2366 } else { 2367 #ifndef INET6 2368 alist = pcap_nametoaddr(name); 2369 if (alist == NULL || *alist == NULL) 2370 bpf_error("unknown host '%s'", name); 2371 tproto = proto; 2372 if (off_linktype == -1 && tproto == Q_DEFAULT) 2373 tproto = Q_IP; 2374 b = gen_host(**alist++, 0xffffffff, tproto, dir); 2375 while (*alist) { 2376 tmp = gen_host(**alist++, 0xffffffff, 2377 tproto, dir); 2378 gen_or(b, tmp); 2379 b = tmp; 2380 } 2381 return b; 2382 #else 2383 memset(&mask128, 0xff, sizeof(mask128)); 2384 res0 = res = pcap_nametoaddrinfo(name); 2385 if (res == NULL) 2386 bpf_error("unknown host '%s'", name); 2387 b = tmp = NULL; 2388 tproto = tproto6 = proto; 2389 if (off_linktype == -1 && tproto == Q_DEFAULT) { 2390 tproto = Q_IP; 2391 tproto6 = Q_IPV6; 2392 } 2393 for (res = res0; res; res = res->ai_next) { 2394 switch (res->ai_family) { 2395 case AF_INET: 2396 if (tproto == Q_IPV6) 2397 continue; 2398 2399 sin = (struct sockaddr_in *) 2400 res->ai_addr; 2401 tmp = gen_host(ntohl(sin->sin_addr.s_addr), 2402 0xffffffff, tproto, dir); 2403 break; 2404 case AF_INET6: 2405 if (tproto6 == Q_IP) 2406 continue; 2407 2408 sin6 = (struct sockaddr_in6 *) 2409 res->ai_addr; 2410 tmp = gen_host6(&sin6->sin6_addr, 2411 &mask128, tproto6, dir); 2412 break; 2413 } 2414 if (b) 2415 gen_or(b, tmp); 2416 b = tmp; 2417 } 2418 freeaddrinfo(res0); 2419 if (b == NULL) { 2420 bpf_error("unknown host '%s'%s", name, 2421 (proto == Q_DEFAULT) 2422 ? "" 2423 : " for specified address family"); 2424 } 2425 return b; 2426 #endif /*INET6*/ 2427 } 2428 2429 case Q_PORT: 2430 if (proto != Q_DEFAULT && proto != Q_UDP && proto != Q_TCP) 2431 bpf_error("illegal qualifier of 'port'"); 2432 if (pcap_nametoport(name, &port, &real_proto) == 0) 2433 bpf_error("unknown port '%s'", name); 2434 if (proto == Q_UDP) { 2435 if (real_proto == IPPROTO_TCP) 2436 bpf_error("port '%s' is tcp", name); 2437 else 2438 /* override PROTO_UNDEF */ 2439 real_proto = IPPROTO_UDP; 2440 } 2441 if (proto == Q_TCP) { 2442 if (real_proto == IPPROTO_UDP) 2443 bpf_error("port '%s' is udp", name); 2444 else 2445 /* override PROTO_UNDEF */ 2446 real_proto = IPPROTO_TCP; 2447 } 2448 #ifndef INET6 2449 return gen_port(port, real_proto, dir); 2450 #else 2451 { 2452 struct block *b; 2453 b = gen_port(port, real_proto, dir); 2454 gen_or(gen_port6(port, real_proto, dir), b); 2455 return b; 2456 } 2457 #endif /* INET6 */ 2458 2459 case Q_GATEWAY: 2460 #ifndef INET6 2461 eaddr = pcap_ether_hostton(name); 2462 if (eaddr == NULL) 2463 bpf_error("unknown ether host: %s", name); 2464 2465 alist = pcap_nametoaddr(name); 2466 if (alist == NULL || *alist == NULL) 2467 bpf_error("unknown host '%s'", name); 2468 return gen_gateway(eaddr, alist, proto, dir); 2469 #else 2470 bpf_error("'gateway' not supported in this configuration"); 2471 #endif /*INET6*/ 2472 2473 case Q_PROTO: 2474 real_proto = lookup_proto(name, proto); 2475 if (real_proto >= 0) 2476 return gen_proto(real_proto, proto, dir); 2477 else 2478 bpf_error("unknown protocol: %s", name); 2479 2480 case Q_PROTOCHAIN: 2481 real_proto = lookup_proto(name, proto); 2482 if (real_proto >= 0) 2483 return gen_protochain(real_proto, proto, dir); 2484 else 2485 bpf_error("unknown protocol: %s", name); 2486 2487 2488 case Q_UNDEF: 2489 syntax(); 2490 /* NOTREACHED */ 2491 } 2492 abort(); 2493 /* NOTREACHED */ 2494 } 2495 2496 struct block * 2497 gen_mcode(s1, s2, masklen, q) 2498 register const char *s1, *s2; 2499 register int masklen; 2500 struct qual q; 2501 { 2502 register int nlen, mlen; 2503 bpf_u_int32 n, m; 2504 2505 nlen = __pcap_atoin(s1, &n); 2506 /* Promote short ipaddr */ 2507 n <<= 32 - nlen; 2508 2509 if (s2 != NULL) { 2510 mlen = __pcap_atoin(s2, &m); 2511 /* Promote short ipaddr */ 2512 m <<= 32 - mlen; 2513 if ((n & ~m) != 0) 2514 bpf_error("non-network bits set in \"%s mask %s\"", 2515 s1, s2); 2516 } else { 2517 /* Convert mask len to mask */ 2518 if (masklen > 32) 2519 bpf_error("mask length must be <= 32"); 2520 m = 0xffffffff << (32 - masklen); 2521 if ((n & ~m) != 0) 2522 bpf_error("non-network bits set in \"%s/%d\"", 2523 s1, masklen); 2524 } 2525 2526 switch (q.addr) { 2527 2528 case Q_NET: 2529 return gen_host(n, m, q.proto, q.dir); 2530 2531 default: 2532 bpf_error("Mask syntax for networks only"); 2533 /* NOTREACHED */ 2534 } 2535 } 2536 2537 struct block * 2538 gen_ncode(s, v, q) 2539 register const char *s; 2540 bpf_u_int32 v; 2541 struct qual q; 2542 { 2543 bpf_u_int32 mask; 2544 int proto = q.proto; 2545 int dir = q.dir; 2546 register int vlen; 2547 2548 if (s == NULL) 2549 vlen = 32; 2550 else if (q.proto == Q_DECNET) 2551 vlen = __pcap_atodn(s, &v); 2552 else 2553 vlen = __pcap_atoin(s, &v); 2554 2555 switch (q.addr) { 2556 2557 case Q_DEFAULT: 2558 case Q_HOST: 2559 case Q_NET: 2560 if (proto == Q_DECNET) 2561 return gen_host(v, 0, proto, dir); 2562 else if (proto == Q_LINK) { 2563 bpf_error("illegal link layer address"); 2564 } else { 2565 mask = 0xffffffff; 2566 if (s == NULL && q.addr == Q_NET) { 2567 /* Promote short net number */ 2568 while (v && (v & 0xff000000) == 0) { 2569 v <<= 8; 2570 mask <<= 8; 2571 } 2572 } else { 2573 /* Promote short ipaddr */ 2574 v <<= 32 - vlen; 2575 mask <<= 32 - vlen; 2576 } 2577 return gen_host(v, mask, proto, dir); 2578 } 2579 2580 case Q_PORT: 2581 if (proto == Q_UDP) 2582 proto = IPPROTO_UDP; 2583 else if (proto == Q_TCP) 2584 proto = IPPROTO_TCP; 2585 else if (proto == Q_DEFAULT) 2586 proto = PROTO_UNDEF; 2587 else 2588 bpf_error("illegal qualifier of 'port'"); 2589 2590 #ifndef INET6 2591 return gen_port((int)v, proto, dir); 2592 #else 2593 { 2594 struct block *b; 2595 b = gen_port((int)v, proto, dir); 2596 gen_or(gen_port6((int)v, proto, dir), b); 2597 return b; 2598 } 2599 #endif /* INET6 */ 2600 2601 case Q_GATEWAY: 2602 bpf_error("'gateway' requires a name"); 2603 /* NOTREACHED */ 2604 2605 case Q_PROTO: 2606 return gen_proto((int)v, proto, dir); 2607 2608 case Q_PROTOCHAIN: 2609 return gen_protochain((int)v, proto, dir); 2610 2611 case Q_UNDEF: 2612 syntax(); 2613 /* NOTREACHED */ 2614 2615 default: 2616 abort(); 2617 /* NOTREACHED */ 2618 } 2619 /* NOTREACHED */ 2620 } 2621 2622 #ifdef INET6 2623 struct block * 2624 gen_mcode6(s1, s2, masklen, q) 2625 register const char *s1, *s2; 2626 register int masklen; 2627 struct qual q; 2628 { 2629 struct addrinfo *res; 2630 struct in6_addr *addr; 2631 struct in6_addr mask; 2632 struct block *b; 2633 u_int32_t *a, *m; 2634 2635 if (s2) 2636 bpf_error("no mask %s supported", s2); 2637 2638 res = pcap_nametoaddrinfo(s1); 2639 if (!res) 2640 bpf_error("invalid ip6 address %s", s1); 2641 if (res->ai_next) 2642 bpf_error("%s resolved to multiple address", s1); 2643 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 2644 2645 if (sizeof(mask) * 8 < masklen) 2646 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8)); 2647 memset(&mask, 0xff, masklen / 8); 2648 if (masklen % 8) { 2649 mask.s6_addr[masklen / 8] = 2650 (0xff << (8 - masklen % 8)) & 0xff; 2651 } 2652 2653 a = (u_int32_t *)addr; 2654 m = (u_int32_t *)&mask; 2655 if ((a[0] & ~m[0]) || (a[1] & ~m[1]) 2656 || (a[2] & ~m[2]) || (a[3] & ~m[3])) { 2657 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen); 2658 } 2659 2660 switch (q.addr) { 2661 2662 case Q_DEFAULT: 2663 case Q_HOST: 2664 if (masklen != 128) 2665 bpf_error("Mask syntax for networks only"); 2666 /* FALLTHROUGH */ 2667 2668 case Q_NET: 2669 b = gen_host6(addr, &mask, q.proto, q.dir); 2670 freeaddrinfo(res); 2671 return b; 2672 2673 default: 2674 bpf_error("invalid qualifier against IPv6 address"); 2675 /* NOTREACHED */ 2676 } 2677 } 2678 #endif /*INET6*/ 2679 2680 struct block * 2681 gen_ecode(eaddr, q) 2682 register const u_char *eaddr; 2683 struct qual q; 2684 { 2685 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) { 2686 if (linktype == DLT_EN10MB) 2687 return gen_ehostop(eaddr, (int)q.dir); 2688 if (linktype == DLT_FDDI) 2689 return gen_fhostop(eaddr, (int)q.dir); 2690 if (linktype == DLT_IEEE802) 2691 return gen_thostop(eaddr, (int)q.dir); 2692 bpf_error("ethernet addresses supported only on ethernet, FDDI or token ring"); 2693 } 2694 bpf_error("ethernet address used in non-ether expression"); 2695 /* NOTREACHED */ 2696 } 2697 2698 void 2699 sappend(s0, s1) 2700 struct slist *s0, *s1; 2701 { 2702 /* 2703 * This is definitely not the best way to do this, but the 2704 * lists will rarely get long. 2705 */ 2706 while (s0->next) 2707 s0 = s0->next; 2708 s0->next = s1; 2709 } 2710 2711 static struct slist * 2712 xfer_to_x(a) 2713 struct arth *a; 2714 { 2715 struct slist *s; 2716 2717 s = new_stmt(BPF_LDX|BPF_MEM); 2718 s->s.k = a->regno; 2719 return s; 2720 } 2721 2722 static struct slist * 2723 xfer_to_a(a) 2724 struct arth *a; 2725 { 2726 struct slist *s; 2727 2728 s = new_stmt(BPF_LD|BPF_MEM); 2729 s->s.k = a->regno; 2730 return s; 2731 } 2732 2733 struct arth * 2734 gen_load(proto, index, size) 2735 int proto; 2736 struct arth *index; 2737 int size; 2738 { 2739 struct slist *s, *tmp; 2740 struct block *b; 2741 int regno = alloc_reg(); 2742 2743 free_reg(index->regno); 2744 switch (size) { 2745 2746 default: 2747 bpf_error("data size must be 1, 2, or 4"); 2748 2749 case 1: 2750 size = BPF_B; 2751 break; 2752 2753 case 2: 2754 size = BPF_H; 2755 break; 2756 2757 case 4: 2758 size = BPF_W; 2759 break; 2760 } 2761 switch (proto) { 2762 default: 2763 bpf_error("unsupported index operation"); 2764 2765 case Q_LINK: 2766 s = xfer_to_x(index); 2767 tmp = new_stmt(BPF_LD|BPF_IND|size); 2768 sappend(s, tmp); 2769 sappend(index->s, s); 2770 break; 2771 2772 case Q_IP: 2773 case Q_ARP: 2774 case Q_RARP: 2775 case Q_ATALK: 2776 case Q_DECNET: 2777 case Q_SCA: 2778 case Q_LAT: 2779 case Q_MOPRC: 2780 case Q_MOPDL: 2781 #ifdef INET6 2782 case Q_IPV6: 2783 #endif 2784 /* XXX Note that we assume a fixed link header here. */ 2785 s = xfer_to_x(index); 2786 tmp = new_stmt(BPF_LD|BPF_IND|size); 2787 tmp->s.k = off_nl; 2788 sappend(s, tmp); 2789 sappend(index->s, s); 2790 2791 b = gen_proto_abbrev(proto); 2792 if (index->b) 2793 gen_and(index->b, b); 2794 index->b = b; 2795 break; 2796 2797 case Q_TCP: 2798 case Q_UDP: 2799 case Q_ICMP: 2800 case Q_IGMP: 2801 case Q_IGRP: 2802 case Q_PIM: 2803 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B); 2804 s->s.k = off_nl; 2805 sappend(s, xfer_to_a(index)); 2806 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X)); 2807 sappend(s, new_stmt(BPF_MISC|BPF_TAX)); 2808 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size)); 2809 tmp->s.k = off_nl; 2810 sappend(index->s, s); 2811 2812 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag()); 2813 if (index->b) 2814 gen_and(index->b, b); 2815 #ifdef INET6 2816 gen_and(gen_proto_abbrev(Q_IP), b); 2817 #endif 2818 index->b = b; 2819 break; 2820 #ifdef INET6 2821 case Q_ICMPV6: 2822 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]"); 2823 /*NOTREACHED*/ 2824 #endif 2825 } 2826 index->regno = regno; 2827 s = new_stmt(BPF_ST); 2828 s->s.k = regno; 2829 sappend(index->s, s); 2830 2831 return index; 2832 } 2833 2834 struct block * 2835 gen_relation(code, a0, a1, reversed) 2836 int code; 2837 struct arth *a0, *a1; 2838 int reversed; 2839 { 2840 struct slist *s0, *s1, *s2; 2841 struct block *b, *tmp; 2842 2843 s0 = xfer_to_x(a1); 2844 s1 = xfer_to_a(a0); 2845 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X); 2846 b = new_block(JMP(code)); 2847 if (code == BPF_JGT || code == BPF_JGE) { 2848 reversed = !reversed; 2849 b->s.k = 0x80000000; 2850 } 2851 if (reversed) 2852 gen_not(b); 2853 2854 sappend(s1, s2); 2855 sappend(s0, s1); 2856 sappend(a1->s, s0); 2857 sappend(a0->s, a1->s); 2858 2859 b->stmts = a0->s; 2860 2861 free_reg(a0->regno); 2862 free_reg(a1->regno); 2863 2864 /* 'and' together protocol checks */ 2865 if (a0->b) { 2866 if (a1->b) { 2867 gen_and(a0->b, tmp = a1->b); 2868 } 2869 else 2870 tmp = a0->b; 2871 } else 2872 tmp = a1->b; 2873 2874 if (tmp) 2875 gen_and(tmp, b); 2876 2877 return b; 2878 } 2879 2880 struct arth * 2881 gen_loadlen() 2882 { 2883 int regno = alloc_reg(); 2884 struct arth *a = (struct arth *)newchunk(sizeof(*a)); 2885 struct slist *s; 2886 2887 s = new_stmt(BPF_LD|BPF_LEN); 2888 s->next = new_stmt(BPF_ST); 2889 s->next->s.k = regno; 2890 a->s = s; 2891 a->regno = regno; 2892 2893 return a; 2894 } 2895 2896 struct arth * 2897 gen_loadi(val) 2898 int val; 2899 { 2900 struct arth *a; 2901 struct slist *s; 2902 int reg; 2903 2904 a = (struct arth *)newchunk(sizeof(*a)); 2905 2906 reg = alloc_reg(); 2907 2908 s = new_stmt(BPF_LD|BPF_IMM); 2909 s->s.k = val; 2910 s->next = new_stmt(BPF_ST); 2911 s->next->s.k = reg; 2912 a->s = s; 2913 a->regno = reg; 2914 2915 return a; 2916 } 2917 2918 struct arth * 2919 gen_neg(a) 2920 struct arth *a; 2921 { 2922 struct slist *s; 2923 2924 s = xfer_to_a(a); 2925 sappend(a->s, s); 2926 s = new_stmt(BPF_ALU|BPF_NEG); 2927 s->s.k = 0; 2928 sappend(a->s, s); 2929 s = new_stmt(BPF_ST); 2930 s->s.k = a->regno; 2931 sappend(a->s, s); 2932 2933 return a; 2934 } 2935 2936 struct arth * 2937 gen_arth(code, a0, a1) 2938 int code; 2939 struct arth *a0, *a1; 2940 { 2941 struct slist *s0, *s1, *s2; 2942 2943 s0 = xfer_to_x(a1); 2944 s1 = xfer_to_a(a0); 2945 s2 = new_stmt(BPF_ALU|BPF_X|code); 2946 2947 sappend(s1, s2); 2948 sappend(s0, s1); 2949 sappend(a1->s, s0); 2950 sappend(a0->s, a1->s); 2951 2952 free_reg(a1->regno); 2953 2954 s0 = new_stmt(BPF_ST); 2955 a0->regno = s0->s.k = alloc_reg(); 2956 sappend(a0->s, s0); 2957 2958 return a0; 2959 } 2960 2961 /* 2962 * Here we handle simple allocation of the scratch registers. 2963 * If too many registers are alloc'd, the allocator punts. 2964 */ 2965 static int regused[BPF_MEMWORDS]; 2966 static int curreg; 2967 2968 /* 2969 * Return the next free register. 2970 */ 2971 static int 2972 alloc_reg() 2973 { 2974 int n = BPF_MEMWORDS; 2975 2976 while (--n >= 0) { 2977 if (regused[curreg]) 2978 curreg = (curreg + 1) % BPF_MEMWORDS; 2979 else { 2980 regused[curreg] = 1; 2981 return curreg; 2982 } 2983 } 2984 bpf_error("too many registers needed to evaluate expression"); 2985 /* NOTREACHED */ 2986 } 2987 2988 /* 2989 * Return a register to the table so it can 2990 * be used later. 2991 */ 2992 static void 2993 free_reg(n) 2994 int n; 2995 { 2996 regused[n] = 0; 2997 } 2998 2999 static struct block * 3000 gen_len(jmp, n) 3001 int jmp, n; 3002 { 3003 struct slist *s; 3004 struct block *b; 3005 3006 s = new_stmt(BPF_LD|BPF_LEN); 3007 b = new_block(JMP(jmp)); 3008 b->stmts = s; 3009 b->s.k = n; 3010 3011 return b; 3012 } 3013 3014 struct block * 3015 gen_greater(n) 3016 int n; 3017 { 3018 return gen_len(BPF_JGE, n); 3019 } 3020 3021 /* 3022 * Actually, this is less than or equal. 3023 */ 3024 struct block * 3025 gen_less(n) 3026 int n; 3027 { 3028 struct block *b; 3029 3030 b = gen_len(BPF_JGT, n); 3031 gen_not(b); 3032 3033 return b; 3034 } 3035 3036 struct block * 3037 gen_byteop(op, idx, val) 3038 int op, idx, val; 3039 { 3040 struct block *b; 3041 struct slist *s; 3042 3043 switch (op) { 3044 default: 3045 abort(); 3046 3047 case '=': 3048 return gen_cmp((u_int)idx, BPF_B, (bpf_int32)val); 3049 3050 case '<': 3051 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val); 3052 b->s.code = JMP(BPF_JGE); 3053 gen_not(b); 3054 return b; 3055 3056 case '>': 3057 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val); 3058 b->s.code = JMP(BPF_JGT); 3059 return b; 3060 3061 case '|': 3062 s = new_stmt(BPF_ALU|BPF_OR|BPF_K); 3063 break; 3064 3065 case '&': 3066 s = new_stmt(BPF_ALU|BPF_AND|BPF_K); 3067 break; 3068 } 3069 s->s.k = val; 3070 b = new_block(JMP(BPF_JEQ)); 3071 b->stmts = s; 3072 gen_not(b); 3073 3074 return b; 3075 } 3076 3077 struct block * 3078 gen_broadcast(proto) 3079 int proto; 3080 { 3081 bpf_u_int32 hostmask; 3082 struct block *b0, *b1, *b2; 3083 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 3084 3085 switch (proto) { 3086 3087 case Q_DEFAULT: 3088 case Q_LINK: 3089 if (linktype == DLT_EN10MB) 3090 return gen_ehostop(ebroadcast, Q_DST); 3091 if (linktype == DLT_FDDI) 3092 return gen_fhostop(ebroadcast, Q_DST); 3093 if (linktype == DLT_IEEE802) 3094 return gen_thostop(ebroadcast, Q_DST); 3095 bpf_error("not a broadcast link"); 3096 break; 3097 3098 case Q_IP: 3099 b0 = gen_linktype(ETHERTYPE_IP); 3100 hostmask = ~netmask; 3101 b1 = gen_mcmp(off_nl + 16, BPF_W, (bpf_int32)0, hostmask); 3102 b2 = gen_mcmp(off_nl + 16, BPF_W, 3103 (bpf_int32)(~0 & hostmask), hostmask); 3104 gen_or(b1, b2); 3105 gen_and(b0, b2); 3106 return b2; 3107 } 3108 bpf_error("only ether/ip broadcast filters supported"); 3109 } 3110 3111 struct block * 3112 gen_multicast(proto) 3113 int proto; 3114 { 3115 register struct block *b0, *b1; 3116 register struct slist *s; 3117 3118 switch (proto) { 3119 3120 case Q_DEFAULT: 3121 case Q_LINK: 3122 if (linktype == DLT_EN10MB) { 3123 /* ether[0] & 1 != 0 */ 3124 s = new_stmt(BPF_LD|BPF_B|BPF_ABS); 3125 s->s.k = 0; 3126 b0 = new_block(JMP(BPF_JSET)); 3127 b0->s.k = 1; 3128 b0->stmts = s; 3129 return b0; 3130 } 3131 3132 if (linktype == DLT_FDDI) { 3133 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */ 3134 /* fddi[1] & 1 != 0 */ 3135 s = new_stmt(BPF_LD|BPF_B|BPF_ABS); 3136 s->s.k = 1; 3137 b0 = new_block(JMP(BPF_JSET)); 3138 b0->s.k = 1; 3139 b0->stmts = s; 3140 return b0; 3141 } 3142 3143 /* TODO - check how token ring handles multicast */ 3144 /* if (linktype == DLT_IEEE802) ... */ 3145 3146 /* Link not known to support multicasts */ 3147 break; 3148 3149 case Q_IP: 3150 b0 = gen_linktype(ETHERTYPE_IP); 3151 b1 = gen_cmp(off_nl + 16, BPF_B, (bpf_int32)224); 3152 b1->s.code = JMP(BPF_JGE); 3153 gen_and(b0, b1); 3154 return b1; 3155 3156 #ifdef INET6 3157 case Q_IPV6: 3158 b0 = gen_linktype(ETHERTYPE_IPV6); 3159 b1 = gen_cmp(off_nl + 24, BPF_B, (bpf_int32)255); 3160 gen_and(b0, b1); 3161 return b1; 3162 #endif /* INET6 */ 3163 } 3164 bpf_error("only IP multicast filters supported on ethernet/FDDI"); 3165 } 3166 3167 /* 3168 * generate command for inbound/outbound. It's here so we can 3169 * make it link-type specific. 'dir' = 0 implies "inbound", 3170 * = 1 implies "outbound". 3171 */ 3172 struct block * 3173 gen_inbound(dir) 3174 int dir; 3175 { 3176 register struct block *b0; 3177 3178 /* 3179 * Only some data link types support inbound/outbound qualifiers. 3180 */ 3181 switch (linktype) { 3182 case DLT_SLIP: 3183 case DLT_PPP: 3184 b0 = gen_relation(BPF_JEQ, 3185 gen_load(Q_LINK, gen_loadi(0), 1), 3186 gen_loadi(0), 3187 dir); 3188 break; 3189 3190 default: 3191 bpf_error("inbound/outbound not supported on linktype %d\n", 3192 linktype); 3193 b0 = NULL; 3194 /* NOTREACHED */ 3195 } 3196 return (b0); 3197 } 3198 3199 /* 3200 * support IEEE 802.1Q VLAN trunk over ethernet 3201 */ 3202 struct block * 3203 gen_vlan(vlan_num) 3204 int vlan_num; 3205 { 3206 static u_int orig_linktype = -1, orig_nl = -1; 3207 struct block *b0; 3208 3209 /* 3210 * Change the offsets to point to the type and data fields within 3211 * the VLAN packet. This is somewhat of a kludge. 3212 */ 3213 if (orig_nl == (u_int)-1) { 3214 orig_linktype = off_linktype; /* save original values */ 3215 orig_nl = off_nl; 3216 3217 switch (linktype) { 3218 3219 case DLT_EN10MB: 3220 off_linktype = 16; 3221 off_nl = 18; 3222 break; 3223 3224 default: 3225 bpf_error("no VLAN support for data link type %d", 3226 linktype); 3227 /*NOTREACHED*/ 3228 } 3229 } 3230 3231 /* check for VLAN */ 3232 b0 = gen_cmp(orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q); 3233 3234 /* If a specific VLAN is requested, check VLAN id */ 3235 if (vlan_num >= 0) { 3236 struct block *b1; 3237 3238 b1 = gen_cmp(orig_nl, BPF_H, (bpf_int32)vlan_num); 3239 gen_and(b0, b1); 3240 b0 = b1; 3241 } 3242 3243 return (b0); 3244 } 3245