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