1 /* $NetBSD: gencode.c,v 1.15 2026/03/18 23:43:20 christos 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 24 #include <sys/cdefs.h> 25 __RCSID("$NetBSD: gencode.c,v 1.15 2026/03/18 23:43:20 christos Exp $"); 26 27 #include <config.h> 28 29 #ifdef _WIN32 30 #include <ws2tcpip.h> 31 #else 32 #include <netinet/in.h> 33 #endif /* _WIN32 */ 34 35 #include <stdlib.h> 36 #include <string.h> 37 #include <memory.h> 38 #include <setjmp.h> 39 #include <stdarg.h> 40 #include <stdio.h> 41 42 #ifdef MSDOS 43 #include "pcap-dos.h" 44 #endif 45 46 #include "pcap-int.h" 47 48 #include "extract.h" 49 50 #include "ethertype.h" 51 #include "nlpid.h" 52 #include "llc.h" 53 #include "gencode.h" 54 #include "ieee80211.h" 55 #include "atmuni31.h" 56 #include "sunatmpos.h" 57 #include "pflog.h" 58 #include "ppp.h" 59 #include "pcap/sll.h" 60 #include "pcap/ipnet.h" 61 #include "arcnet.h" 62 #include "diag-control.h" 63 64 #include "scanner.h" 65 66 #if defined(__linux__) 67 #include <linux/types.h> 68 #include <linux/if_packet.h> 69 #include <linux/filter.h> 70 #endif 71 72 #ifndef offsetof 73 #define offsetof(s, e) ((size_t)&((s *)0)->e) 74 #endif 75 76 #ifdef _WIN32 77 #ifdef HAVE_NPCAP_BPF_H 78 /* Defines BPF extensions for Npcap */ 79 #include <npcap-bpf.h> 80 #endif 81 #ifdef INET6 82 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) 83 /* IPv6 address */ 84 struct in6_addr 85 { 86 union 87 { 88 uint8_t u6_addr8[16]; 89 uint16_t u6_addr16[8]; 90 uint32_t u6_addr32[4]; 91 } in6_u; 92 #define s6_addr in6_u.u6_addr8 93 #define s6_addr16 in6_u.u6_addr16 94 #define s6_addr32 in6_u.u6_addr32 95 #define s6_addr64 in6_u.u6_addr64 96 }; 97 98 typedef unsigned short sa_family_t; 99 100 #define __SOCKADDR_COMMON(sa_prefix) \ 101 sa_family_t sa_prefix##family 102 103 /* Ditto, for IPv6. */ 104 struct sockaddr_in6 105 { 106 __SOCKADDR_COMMON (sin6_); 107 uint16_t sin6_port; /* Transport layer port # */ 108 uint32_t sin6_flowinfo; /* IPv6 flow information */ 109 struct in6_addr sin6_addr; /* IPv6 address */ 110 }; 111 112 #ifndef EAI_ADDRFAMILY 113 struct addrinfo { 114 int ai_flags; /* AI_PASSIVE, AI_CANONNAME */ 115 int ai_family; /* PF_xxx */ 116 int ai_socktype; /* SOCK_xxx */ 117 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */ 118 size_t ai_addrlen; /* length of ai_addr */ 119 char *ai_canonname; /* canonical name for hostname */ 120 struct sockaddr *ai_addr; /* binary address */ 121 struct addrinfo *ai_next; /* next structure in linked list */ 122 }; 123 #endif /* EAI_ADDRFAMILY */ 124 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */ 125 #endif /* INET6 */ 126 #else /* _WIN32 */ 127 #include <netdb.h> /* for "struct addrinfo" */ 128 #endif /* _WIN32 */ 129 #include <pcap/namedb.h> 130 131 #include "nametoaddr.h" 132 133 #define ETHERMTU 1500 134 135 #ifndef IPPROTO_HOPOPTS 136 #define IPPROTO_HOPOPTS 0 137 #endif 138 #ifndef IPPROTO_ROUTING 139 #define IPPROTO_ROUTING 43 140 #endif 141 #ifndef IPPROTO_FRAGMENT 142 #define IPPROTO_FRAGMENT 44 143 #endif 144 #ifndef IPPROTO_DSTOPTS 145 #define IPPROTO_DSTOPTS 60 146 #endif 147 #ifndef IPPROTO_SCTP 148 #define IPPROTO_SCTP 132 149 #endif 150 151 #define GENEVE_PORT 6081 152 153 #ifdef HAVE_OS_PROTO_H 154 #include "os-proto.h" 155 #endif 156 157 #define JMP(c) ((c)|BPF_JMP|BPF_K) 158 159 /* 160 * "Push" the current value of the link-layer header type and link-layer 161 * header offset onto a "stack", and set a new value. (It's not a 162 * full-blown stack; we keep only the top two items.) 163 */ 164 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \ 165 { \ 166 (cs)->prevlinktype = (cs)->linktype; \ 167 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \ 168 (cs)->linktype = (new_linktype); \ 169 (cs)->off_linkhdr.is_variable = (new_is_variable); \ 170 (cs)->off_linkhdr.constant_part = (new_constant_part); \ 171 (cs)->off_linkhdr.reg = (new_reg); \ 172 (cs)->is_geneve = 0; \ 173 } 174 175 /* 176 * Offset "not set" value. 177 */ 178 #define OFFSET_NOT_SET 0xffffffffU 179 180 /* 181 * Absolute offsets, which are offsets from the beginning of the raw 182 * packet data, are, in the general case, the sum of a variable value 183 * and a constant value; the variable value may be absent, in which 184 * case the offset is only the constant value, and the constant value 185 * may be zero, in which case the offset is only the variable value. 186 * 187 * bpf_abs_offset is a structure containing all that information: 188 * 189 * is_variable is 1 if there's a variable part. 190 * 191 * constant_part is the constant part of the value, possibly zero; 192 * 193 * if is_variable is 1, reg is the register number for a register 194 * containing the variable value if the register has been assigned, 195 * and -1 otherwise. 196 */ 197 typedef struct { 198 int is_variable; 199 u_int constant_part; 200 int reg; 201 } bpf_abs_offset; 202 203 /* 204 * Value passed to gen_load_a() to indicate what the offset argument 205 * is relative to the beginning of. 206 */ 207 enum e_offrel { 208 OR_PACKET, /* full packet data */ 209 OR_LINKHDR, /* link-layer header */ 210 OR_PREVLINKHDR, /* previous link-layer header */ 211 OR_LLC, /* 802.2 LLC header */ 212 OR_PREVMPLSHDR, /* previous MPLS header */ 213 OR_LINKTYPE, /* link-layer type */ 214 OR_LINKPL, /* link-layer payload */ 215 OR_LINKPL_NOSNAP, /* link-layer payload, with no SNAP header at the link layer */ 216 OR_TRAN_IPV4, /* transport-layer header, with IPv4 network layer */ 217 OR_TRAN_IPV6 /* transport-layer header, with IPv6 network layer */ 218 }; 219 220 /* 221 * We divvy out chunks of memory rather than call malloc each time so 222 * we don't have to worry about leaking memory. It's probably 223 * not a big deal if all this memory was wasted but if this ever 224 * goes into a library that would probably not be a good idea. 225 * 226 * XXX - this *is* in a library.... 227 */ 228 #define NCHUNKS 16 229 #define CHUNK0SIZE 1024 230 struct chunk { 231 size_t n_left; 232 void *m; 233 }; 234 235 /* 236 * A chunk can store any of: 237 * - a string (guaranteed alignment 1 but present for completeness) 238 * - a block 239 * - an slist 240 * - an arth 241 * For this simple allocator every allocated chunk gets rounded up to the 242 * alignment needed for any chunk. 243 */ 244 struct chunk_align { 245 char dummy; 246 union { 247 char c; 248 struct block b; 249 struct slist s; 250 struct arth a; 251 } u; 252 }; 253 #define CHUNK_ALIGN (offsetof(struct chunk_align, u)) 254 255 /* Code generator state */ 256 257 struct _compiler_state { 258 jmp_buf top_ctx; 259 pcap_t *bpf_pcap; 260 int error_set; 261 262 struct icode ic; 263 264 int snaplen; 265 266 int linktype; 267 int prevlinktype; 268 int outermostlinktype; 269 270 bpf_u_int32 netmask; 271 int no_optimize; 272 273 /* Hack for handling VLAN and MPLS stacks. */ 274 u_int label_stack_depth; 275 u_int vlan_stack_depth; 276 277 /* XXX */ 278 u_int pcap_fddipad; 279 280 /* 281 * As errors are handled by a longjmp, anything allocated must 282 * be freed in the longjmp handler, so it must be reachable 283 * from that handler. 284 * 285 * One thing that's allocated is the result of pcap_nametoaddrinfo(); 286 * it must be freed with freeaddrinfo(). This variable points to 287 * any addrinfo structure that would need to be freed. 288 */ 289 struct addrinfo *ai; 290 291 /* 292 * Another thing that's allocated is the result of pcap_ether_aton(); 293 * it must be freed with free(). This variable points to any 294 * address that would need to be freed. 295 */ 296 u_char *e; 297 298 /* 299 * Various code constructs need to know the layout of the packet. 300 * These values give the necessary offsets from the beginning 301 * of the packet data. 302 */ 303 304 /* 305 * Absolute offset of the beginning of the link-layer header. 306 */ 307 bpf_abs_offset off_linkhdr; 308 309 /* 310 * If we're checking a link-layer header for a packet encapsulated 311 * in another protocol layer, this is the equivalent information 312 * for the previous layers' link-layer header from the beginning 313 * of the raw packet data. 314 */ 315 bpf_abs_offset off_prevlinkhdr; 316 317 /* 318 * This is the equivalent information for the outermost layers' 319 * link-layer header. 320 */ 321 bpf_abs_offset off_outermostlinkhdr; 322 323 /* 324 * Absolute offset of the beginning of the link-layer payload. 325 */ 326 bpf_abs_offset off_linkpl; 327 328 /* 329 * "off_linktype" is the offset to information in the link-layer 330 * header giving the packet type. This is an absolute offset 331 * from the beginning of the packet. 332 * 333 * For Ethernet, it's the offset of the Ethernet type field; this 334 * means that it must have a value that skips VLAN tags. 335 * 336 * For link-layer types that always use 802.2 headers, it's the 337 * offset of the LLC header; this means that it must have a value 338 * that skips VLAN tags. 339 * 340 * For PPP, it's the offset of the PPP type field. 341 * 342 * For Cisco HDLC, it's the offset of the CHDLC type field. 343 * 344 * For BSD loopback, it's the offset of the AF_ value. 345 * 346 * For Linux cooked sockets, it's the offset of the type field. 347 * 348 * off_linktype.constant_part is set to OFFSET_NOT_SET for no 349 * encapsulation, in which case, IP is assumed. 350 */ 351 bpf_abs_offset off_linktype; 352 353 /* 354 * TRUE if the link layer includes an ATM pseudo-header. 355 */ 356 int is_atm; 357 358 /* 359 * TRUE if "geneve" appeared in the filter; it causes us to 360 * generate code that checks for a Geneve header and assume 361 * that later filters apply to the encapsulated payload. 362 */ 363 int is_geneve; 364 365 /* 366 * TRUE if we need variable length part of VLAN offset 367 */ 368 int is_vlan_vloffset; 369 370 /* 371 * These are offsets for the ATM pseudo-header. 372 */ 373 u_int off_vpi; 374 u_int off_vci; 375 u_int off_proto; 376 377 /* 378 * These are offsets for the MTP2 fields. 379 */ 380 u_int off_li; 381 u_int off_li_hsl; 382 383 /* 384 * These are offsets for the MTP3 fields. 385 */ 386 u_int off_sio; 387 u_int off_opc; 388 u_int off_dpc; 389 u_int off_sls; 390 391 /* 392 * This is the offset of the first byte after the ATM pseudo_header, 393 * or -1 if there is no ATM pseudo-header. 394 */ 395 u_int off_payload; 396 397 /* 398 * These are offsets to the beginning of the network-layer header. 399 * They are relative to the beginning of the link-layer payload 400 * (i.e., they don't include off_linkhdr.constant_part or 401 * off_linkpl.constant_part). 402 * 403 * If the link layer never uses 802.2 LLC: 404 * 405 * "off_nl" and "off_nl_nosnap" are the same. 406 * 407 * If the link layer always uses 802.2 LLC: 408 * 409 * "off_nl" is the offset if there's a SNAP header following 410 * the 802.2 header; 411 * 412 * "off_nl_nosnap" is the offset if there's no SNAP header. 413 * 414 * If the link layer is Ethernet: 415 * 416 * "off_nl" is the offset if the packet is an Ethernet II packet 417 * (we assume no 802.3+802.2+SNAP); 418 * 419 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet 420 * with an 802.2 header following it. 421 */ 422 u_int off_nl; 423 u_int off_nl_nosnap; 424 425 /* 426 * Here we handle simple allocation of the scratch registers. 427 * If too many registers are alloc'd, the allocator punts. 428 */ 429 int regused[BPF_MEMWORDS]; 430 int curreg; 431 432 /* 433 * Memory chunks. 434 */ 435 struct chunk chunks[NCHUNKS]; 436 int cur_chunk; 437 }; 438 439 /* 440 * For use by routines outside this file. 441 */ 442 /* VARARGS */ 443 void 444 bpf_set_error(compiler_state_t *cstate, const char *fmt, ...) 445 { 446 va_list ap; 447 448 /* 449 * If we've already set an error, don't override it. 450 * The lexical analyzer reports some errors by setting 451 * the error and then returning a LEX_ERROR token, which 452 * is not recognized by any grammar rule, and thus forces 453 * the parse to stop. We don't want the error reported 454 * by the lexical analyzer to be overwritten by the syntax 455 * error. 456 */ 457 if (!cstate->error_set) { 458 va_start(ap, fmt); 459 (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE, 460 fmt, ap); 461 va_end(ap); 462 cstate->error_set = 1; 463 } 464 } 465 466 /* 467 * For use *ONLY* in routines in this file. 468 */ 469 static void PCAP_NORETURN bpf_error(compiler_state_t *, const char *, ...) 470 PCAP_PRINTFLIKE(2, 3); 471 472 /* VARARGS */ 473 static void PCAP_NORETURN 474 bpf_error(compiler_state_t *cstate, const char *fmt, ...) 475 { 476 va_list ap; 477 478 va_start(ap, fmt); 479 (void)vsnprintf(cstate->bpf_pcap->errbuf, PCAP_ERRBUF_SIZE, 480 fmt, ap); 481 va_end(ap); 482 longjmp(cstate->top_ctx, 1); 483 /*NOTREACHED*/ 484 #ifdef _AIX 485 PCAP_UNREACHABLE 486 #endif /* _AIX */ 487 } 488 489 static int init_linktype(compiler_state_t *, pcap_t *); 490 491 static void init_regs(compiler_state_t *); 492 static int alloc_reg(compiler_state_t *); 493 static void free_reg(compiler_state_t *, int); 494 495 static void initchunks(compiler_state_t *cstate); 496 static void *newchunk_nolongjmp(compiler_state_t *cstate, size_t); 497 static void *newchunk(compiler_state_t *cstate, size_t); 498 static void freechunks(compiler_state_t *cstate); 499 static inline struct block *new_block(compiler_state_t *cstate, int); 500 static inline struct slist *new_stmt(compiler_state_t *cstate, int); 501 static struct block *gen_retblk(compiler_state_t *cstate, int); 502 static inline void syntax(compiler_state_t *cstate); 503 504 static void backpatch(struct block *, struct block *); 505 static void merge(struct block *, struct block *); 506 static struct block *gen_cmp(compiler_state_t *, enum e_offrel, u_int, 507 u_int, bpf_u_int32); 508 static struct block *gen_cmp_gt(compiler_state_t *, enum e_offrel, u_int, 509 u_int, bpf_u_int32); 510 static struct block *gen_cmp_ge(compiler_state_t *, enum e_offrel, u_int, 511 u_int, bpf_u_int32); 512 static struct block *gen_cmp_lt(compiler_state_t *, enum e_offrel, u_int, 513 u_int, bpf_u_int32); 514 static struct block *gen_cmp_le(compiler_state_t *, enum e_offrel, u_int, 515 u_int, bpf_u_int32); 516 static struct block *gen_mcmp(compiler_state_t *, enum e_offrel, u_int, 517 u_int, bpf_u_int32, bpf_u_int32); 518 static struct block *gen_bcmp(compiler_state_t *, enum e_offrel, u_int, 519 u_int, const u_char *); 520 static struct block *gen_ncmp(compiler_state_t *, enum e_offrel, u_int, 521 u_int, bpf_u_int32, int, int, bpf_u_int32); 522 static struct slist *gen_load_absoffsetrel(compiler_state_t *, bpf_abs_offset *, 523 u_int, u_int); 524 static struct slist *gen_load_a(compiler_state_t *, enum e_offrel, u_int, 525 u_int); 526 static struct slist *gen_loadx_iphdrlen(compiler_state_t *); 527 static struct block *gen_uncond(compiler_state_t *, int); 528 static inline struct block *gen_true(compiler_state_t *); 529 static inline struct block *gen_false(compiler_state_t *); 530 static struct block *gen_ether_linktype(compiler_state_t *, bpf_u_int32); 531 static struct block *gen_ipnet_linktype(compiler_state_t *, bpf_u_int32); 532 static struct block *gen_linux_sll_linktype(compiler_state_t *, bpf_u_int32); 533 static struct slist *gen_load_pflog_llprefixlen(compiler_state_t *); 534 static struct slist *gen_load_prism_llprefixlen(compiler_state_t *); 535 static struct slist *gen_load_avs_llprefixlen(compiler_state_t *); 536 static struct slist *gen_load_radiotap_llprefixlen(compiler_state_t *); 537 static struct slist *gen_load_ppi_llprefixlen(compiler_state_t *); 538 static void insert_compute_vloffsets(compiler_state_t *, struct block *); 539 static struct slist *gen_abs_offset_varpart(compiler_state_t *, 540 bpf_abs_offset *); 541 static bpf_u_int32 ethertype_to_ppptype(bpf_u_int32); 542 static struct block *gen_linktype(compiler_state_t *, bpf_u_int32); 543 static struct block *gen_snap(compiler_state_t *, bpf_u_int32, bpf_u_int32); 544 static struct block *gen_llc_linktype(compiler_state_t *, bpf_u_int32); 545 static struct block *gen_hostop(compiler_state_t *, bpf_u_int32, bpf_u_int32, 546 int, bpf_u_int32, u_int, u_int); 547 #ifdef INET6 548 static struct block *gen_hostop6(compiler_state_t *, struct in6_addr *, 549 struct in6_addr *, int, bpf_u_int32, u_int, u_int); 550 #endif 551 static struct block *gen_ahostop(compiler_state_t *, const uint8_t, int); 552 static struct block *gen_ehostop(compiler_state_t *, const u_char *, int); 553 static struct block *gen_fhostop(compiler_state_t *, const u_char *, int); 554 static struct block *gen_thostop(compiler_state_t *, const u_char *, int); 555 static struct block *gen_wlanhostop(compiler_state_t *, const u_char *, int); 556 static struct block *gen_ipfchostop(compiler_state_t *, const u_char *, int); 557 static struct block *gen_dnhostop(compiler_state_t *, bpf_u_int32, int); 558 static struct block *gen_mpls_linktype(compiler_state_t *, bpf_u_int32); 559 static struct block *gen_host(compiler_state_t *, bpf_u_int32, bpf_u_int32, 560 int, int, int); 561 #ifdef INET6 562 static struct block *gen_host6(compiler_state_t *, struct in6_addr *, 563 struct in6_addr *, int, int, int); 564 #endif 565 #ifndef INET6 566 static struct block *gen_gateway(compiler_state_t *, const u_char *, 567 struct addrinfo *, int, int); 568 #endif 569 static struct block *gen_ipfrag(compiler_state_t *); 570 static struct block *gen_portatom(compiler_state_t *, int, bpf_u_int32); 571 static struct block *gen_portrangeatom(compiler_state_t *, u_int, bpf_u_int32, 572 bpf_u_int32); 573 static struct block *gen_portatom6(compiler_state_t *, int, bpf_u_int32); 574 static struct block *gen_portrangeatom6(compiler_state_t *, u_int, bpf_u_int32, 575 bpf_u_int32); 576 static struct block *gen_portop(compiler_state_t *, u_int, u_int, int); 577 static struct block *gen_port(compiler_state_t *, u_int, int, int); 578 static struct block *gen_portrangeop(compiler_state_t *, u_int, u_int, 579 bpf_u_int32, int); 580 static struct block *gen_portrange(compiler_state_t *, u_int, u_int, int, int); 581 struct block *gen_portop6(compiler_state_t *, u_int, u_int, int); 582 static struct block *gen_port6(compiler_state_t *, u_int, int, int); 583 static struct block *gen_portrangeop6(compiler_state_t *, u_int, u_int, 584 bpf_u_int32, int); 585 static struct block *gen_portrange6(compiler_state_t *, u_int, u_int, int, int); 586 static int lookup_proto(compiler_state_t *, const char *, int); 587 #if !defined(NO_PROTOCHAIN) 588 static struct block *gen_protochain(compiler_state_t *, bpf_u_int32, int); 589 #endif /* !defined(NO_PROTOCHAIN) */ 590 static struct block *gen_proto(compiler_state_t *, bpf_u_int32, int, int); 591 static struct slist *xfer_to_x(compiler_state_t *, struct arth *); 592 static struct slist *xfer_to_a(compiler_state_t *, struct arth *); 593 static struct block *gen_mac_multicast(compiler_state_t *, int); 594 static struct block *gen_len(compiler_state_t *, int, int); 595 static struct block *gen_check_802_11_data_frame(compiler_state_t *); 596 static struct block *gen_geneve_ll_check(compiler_state_t *cstate); 597 598 static struct block *gen_ppi_dlt_check(compiler_state_t *); 599 static struct block *gen_atmfield_code_internal(compiler_state_t *, int, 600 bpf_u_int32, int, int); 601 static struct block *gen_atmtype_llc(compiler_state_t *); 602 static struct block *gen_msg_abbrev(compiler_state_t *, int type); 603 604 static void 605 initchunks(compiler_state_t *cstate) 606 { 607 int i; 608 609 for (i = 0; i < NCHUNKS; i++) { 610 cstate->chunks[i].n_left = 0; 611 cstate->chunks[i].m = NULL; 612 } 613 cstate->cur_chunk = 0; 614 } 615 616 static void * 617 newchunk_nolongjmp(compiler_state_t *cstate, size_t n) 618 { 619 struct chunk *cp; 620 int k; 621 size_t size; 622 623 /* Round up to chunk alignment. */ 624 n = (n + CHUNK_ALIGN - 1) & ~(CHUNK_ALIGN - 1); 625 626 cp = &cstate->chunks[cstate->cur_chunk]; 627 if (n > cp->n_left) { 628 ++cp; 629 k = ++cstate->cur_chunk; 630 if (k >= NCHUNKS) { 631 bpf_set_error(cstate, "out of memory"); 632 return (NULL); 633 } 634 size = CHUNK0SIZE << k; 635 cp->m = (void *)malloc(size); 636 if (cp->m == NULL) { 637 bpf_set_error(cstate, "out of memory"); 638 return (NULL); 639 } 640 memset((char *)cp->m, 0, size); 641 cp->n_left = size; 642 if (n > size) { 643 bpf_set_error(cstate, "out of memory"); 644 return (NULL); 645 } 646 } 647 cp->n_left -= n; 648 return (void *)((char *)cp->m + cp->n_left); 649 } 650 651 static void * 652 newchunk(compiler_state_t *cstate, size_t n) 653 { 654 void *p; 655 656 p = newchunk_nolongjmp(cstate, n); 657 if (p == NULL) { 658 longjmp(cstate->top_ctx, 1); 659 /*NOTREACHED*/ 660 } 661 return (p); 662 } 663 664 static void 665 freechunks(compiler_state_t *cstate) 666 { 667 int i; 668 669 for (i = 0; i < NCHUNKS; ++i) 670 if (cstate->chunks[i].m != NULL) 671 free(cstate->chunks[i].m); 672 } 673 674 /* 675 * A strdup whose allocations are freed after code generation is over. 676 * This is used by the lexical analyzer, so it can't longjmp; it just 677 * returns NULL on an allocation error, and the callers must check 678 * for it. 679 */ 680 char * 681 sdup(compiler_state_t *cstate, const char *s) 682 { 683 size_t n = strlen(s) + 1; 684 char *cp = newchunk_nolongjmp(cstate, n); 685 686 if (cp == NULL) 687 return (NULL); 688 pcapint_strlcpy(cp, s, n); 689 return (cp); 690 } 691 692 static inline struct block * 693 new_block(compiler_state_t *cstate, int code) 694 { 695 struct block *p; 696 697 p = (struct block *)newchunk(cstate, sizeof(*p)); 698 p->s.code = code; 699 p->head = p; 700 701 return p; 702 } 703 704 static inline struct slist * 705 new_stmt(compiler_state_t *cstate, int code) 706 { 707 struct slist *p; 708 709 p = (struct slist *)newchunk(cstate, sizeof(*p)); 710 p->s.code = code; 711 712 return p; 713 } 714 715 static struct block * 716 gen_retblk_internal(compiler_state_t *cstate, int v) 717 { 718 struct block *b = new_block(cstate, BPF_RET|BPF_K); 719 720 b->s.k = v; 721 return b; 722 } 723 724 static struct block * 725 gen_retblk(compiler_state_t *cstate, int v) 726 { 727 if (setjmp(cstate->top_ctx)) { 728 /* 729 * gen_retblk() only fails because a memory 730 * allocation failed in newchunk(), meaning 731 * that it can't return a pointer. 732 * 733 * Return NULL. 734 */ 735 return NULL; 736 } 737 return gen_retblk_internal(cstate, v); 738 } 739 740 static inline PCAP_NORETURN_DEF void 741 syntax(compiler_state_t *cstate) 742 { 743 bpf_error(cstate, "syntax error in filter expression"); 744 } 745 746 int 747 pcap_compile(pcap_t *p, struct bpf_program *program, 748 const char *buf, int optimize, bpf_u_int32 mask) 749 { 750 #ifdef _WIN32 751 static int done = 0; 752 #endif 753 compiler_state_t cstate; 754 yyscan_t scanner = NULL; 755 YY_BUFFER_STATE in_buffer = NULL; 756 u_int len; 757 int rc; 758 759 /* 760 * If this pcap_t hasn't been activated, it doesn't have a 761 * link-layer type, so we can't use it. 762 */ 763 if (!p->activated) { 764 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 765 "not-yet-activated pcap_t passed to pcap_compile"); 766 return (PCAP_ERROR); 767 } 768 769 #ifdef _WIN32 770 if (!done) { 771 pcap_wsockinit(); 772 done = 1; 773 } 774 #endif 775 776 #ifdef ENABLE_REMOTE 777 /* 778 * If the device on which we're capturing need to be notified 779 * that a new filter is being compiled, do so. 780 * 781 * This allows them to save a copy of it, in case, for example, 782 * they're implementing a form of remote packet capture, and 783 * want the remote machine to filter out the packets in which 784 * it's sending the packets it's captured. 785 * 786 * XXX - the fact that we happen to be compiling a filter 787 * doesn't necessarily mean we'll be installing it as the 788 * filter for this pcap_t; we might be running it from userland 789 * on captured packets to do packet classification. We really 790 * need a better way of handling this, but this is all that 791 * the WinPcap remote capture code did. 792 */ 793 if (p->save_current_filter_op != NULL) 794 (p->save_current_filter_op)(p, buf); 795 #endif 796 797 initchunks(&cstate); 798 cstate.no_optimize = 0; 799 #ifdef INET6 800 cstate.ai = NULL; 801 #endif 802 cstate.e = NULL; 803 cstate.ic.root = NULL; 804 cstate.ic.cur_mark = 0; 805 cstate.bpf_pcap = p; 806 cstate.error_set = 0; 807 init_regs(&cstate); 808 809 cstate.netmask = mask; 810 811 cstate.snaplen = pcap_snapshot(p); 812 if (cstate.snaplen == 0) { 813 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 814 "snaplen of 0 rejects all packets"); 815 rc = PCAP_ERROR; 816 goto quit; 817 } 818 819 if (pcap_lex_init(&scanner) != 0) { 820 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 821 errno, "can't initialize scanner"); 822 rc = PCAP_ERROR; 823 goto quit; 824 } 825 in_buffer = pcap__scan_string(buf ? buf : "", scanner); 826 827 /* 828 * Associate the compiler state with the lexical analyzer 829 * state. 830 */ 831 pcap_set_extra(&cstate, scanner); 832 833 if (init_linktype(&cstate, p) == -1) { 834 rc = PCAP_ERROR; 835 goto quit; 836 } 837 if (pcap_parse(scanner, &cstate) != 0) { 838 #ifdef INET6 839 if (cstate.ai != NULL) 840 freeaddrinfo(cstate.ai); 841 #endif 842 if (cstate.e != NULL) 843 free(cstate.e); 844 rc = PCAP_ERROR; 845 goto quit; 846 } 847 848 if (cstate.ic.root == NULL) { 849 cstate.ic.root = gen_retblk(&cstate, cstate.snaplen); 850 851 /* 852 * Catch errors reported by gen_retblk(). 853 */ 854 if (cstate.ic.root== NULL) { 855 rc = PCAP_ERROR; 856 goto quit; 857 } 858 } 859 860 if (optimize && !cstate.no_optimize) { 861 if (bpf_optimize(&cstate.ic, p->errbuf) == -1) { 862 /* Failure */ 863 rc = PCAP_ERROR; 864 goto quit; 865 } 866 if (cstate.ic.root == NULL || 867 (cstate.ic.root->s.code == (BPF_RET|BPF_K) && cstate.ic.root->s.k == 0)) { 868 (void)snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 869 "expression rejects all packets"); 870 rc = PCAP_ERROR; 871 goto quit; 872 } 873 } 874 program->bf_insns = icode_to_fcode(&cstate.ic, 875 cstate.ic.root, &len, p->errbuf); 876 if (program->bf_insns == NULL) { 877 /* Failure */ 878 rc = PCAP_ERROR; 879 goto quit; 880 } 881 program->bf_len = len; 882 883 rc = 0; /* We're all okay */ 884 885 quit: 886 /* 887 * Clean up everything for the lexical analyzer. 888 */ 889 if (in_buffer != NULL) 890 pcap__delete_buffer(in_buffer, scanner); 891 if (scanner != NULL) 892 pcap_lex_destroy(scanner); 893 894 /* 895 * Clean up our own allocated memory. 896 */ 897 freechunks(&cstate); 898 899 return (rc); 900 } 901 902 /* 903 * entry point for using the compiler with no pcap open 904 * pass in all the stuff that is needed explicitly instead. 905 */ 906 int 907 pcap_compile_nopcap(int snaplen_arg, int linktype_arg, 908 struct bpf_program *program, 909 const char *buf, int optimize, bpf_u_int32 mask) 910 { 911 pcap_t *p; 912 int ret; 913 914 p = pcap_open_dead(linktype_arg, snaplen_arg); 915 if (p == NULL) 916 return (PCAP_ERROR); 917 ret = pcap_compile(p, program, buf, optimize, mask); 918 pcap_close(p); 919 return (ret); 920 } 921 922 /* 923 * Clean up a "struct bpf_program" by freeing all the memory allocated 924 * in it. 925 */ 926 void 927 pcap_freecode(struct bpf_program *program) 928 { 929 program->bf_len = 0; 930 if (program->bf_insns != NULL) { 931 free((char *)program->bf_insns); 932 program->bf_insns = NULL; 933 } 934 } 935 936 /* 937 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates 938 * which of the jt and jf fields has been resolved and which is a pointer 939 * back to another unresolved block (or nil). At least one of the fields 940 * in each block is already resolved. 941 */ 942 static void 943 backpatch(struct block *list, struct block *target) 944 { 945 struct block *next; 946 947 while (list) { 948 if (!list->sense) { 949 next = JT(list); 950 JT(list) = target; 951 } else { 952 next = JF(list); 953 JF(list) = target; 954 } 955 list = next; 956 } 957 } 958 959 /* 960 * Merge the lists in b0 and b1, using the 'sense' field to indicate 961 * which of jt and jf is the link. 962 */ 963 static void 964 merge(struct block *b0, struct block *b1) 965 { 966 register struct block **p = &b0; 967 968 /* Find end of list. */ 969 while (*p) 970 p = !((*p)->sense) ? &JT(*p) : &JF(*p); 971 972 /* Concatenate the lists. */ 973 *p = b1; 974 } 975 976 int 977 finish_parse(compiler_state_t *cstate, struct block *p) 978 { 979 struct block *ppi_dlt_check; 980 981 /* 982 * Catch errors reported by us and routines below us, and return -1 983 * on an error. 984 */ 985 if (setjmp(cstate->top_ctx)) 986 return (-1); 987 988 /* 989 * Insert before the statements of the first (root) block any 990 * statements needed to load the lengths of any variable-length 991 * headers into registers. 992 * 993 * XXX - a fancier strategy would be to insert those before the 994 * statements of all blocks that use those lengths and that 995 * have no predecessors that use them, so that we only compute 996 * the lengths if we need them. There might be even better 997 * approaches than that. 998 * 999 * However, those strategies would be more complicated, and 1000 * as we don't generate code to compute a length if the 1001 * program has no tests that use the length, and as most 1002 * tests will probably use those lengths, we would just 1003 * postpone computing the lengths so that it's not done 1004 * for tests that fail early, and it's not clear that's 1005 * worth the effort. 1006 */ 1007 insert_compute_vloffsets(cstate, p->head); 1008 1009 /* 1010 * For DLT_PPI captures, generate a check of the per-packet 1011 * DLT value to make sure it's DLT_IEEE802_11. 1012 * 1013 * XXX - TurboCap cards use DLT_PPI for Ethernet. 1014 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header 1015 * with appropriate Ethernet information and use that rather 1016 * than using something such as DLT_PPI where you don't know 1017 * the link-layer header type until runtime, which, in the 1018 * general case, would force us to generate both Ethernet *and* 1019 * 802.11 code (*and* anything else for which PPI is used) 1020 * and choose between them early in the BPF program? 1021 */ 1022 ppi_dlt_check = gen_ppi_dlt_check(cstate); 1023 if (ppi_dlt_check != NULL) 1024 gen_and(ppi_dlt_check, p); 1025 1026 backpatch(p, gen_retblk_internal(cstate, cstate->snaplen)); 1027 p->sense = !p->sense; 1028 backpatch(p, gen_retblk_internal(cstate, 0)); 1029 cstate->ic.root = p->head; 1030 return (0); 1031 } 1032 1033 void 1034 gen_and(struct block *b0, struct block *b1) 1035 { 1036 backpatch(b0, b1->head); 1037 b0->sense = !b0->sense; 1038 b1->sense = !b1->sense; 1039 merge(b1, b0); 1040 b1->sense = !b1->sense; 1041 b1->head = b0->head; 1042 } 1043 1044 void 1045 gen_or(struct block *b0, struct block *b1) 1046 { 1047 b0->sense = !b0->sense; 1048 backpatch(b0, b1->head); 1049 b0->sense = !b0->sense; 1050 merge(b1, b0); 1051 b1->head = b0->head; 1052 } 1053 1054 void 1055 gen_not(struct block *b) 1056 { 1057 b->sense = !b->sense; 1058 } 1059 1060 static struct block * 1061 gen_cmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1062 u_int size, bpf_u_int32 v) 1063 { 1064 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v); 1065 } 1066 1067 static struct block * 1068 gen_cmp_gt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1069 u_int size, bpf_u_int32 v) 1070 { 1071 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 0, v); 1072 } 1073 1074 static struct block * 1075 gen_cmp_ge(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1076 u_int size, bpf_u_int32 v) 1077 { 1078 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 0, v); 1079 } 1080 1081 static struct block * 1082 gen_cmp_lt(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1083 u_int size, bpf_u_int32 v) 1084 { 1085 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGE, 1, v); 1086 } 1087 1088 static struct block * 1089 gen_cmp_le(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1090 u_int size, bpf_u_int32 v) 1091 { 1092 return gen_ncmp(cstate, offrel, offset, size, 0xffffffff, BPF_JGT, 1, v); 1093 } 1094 1095 static struct block * 1096 gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1097 u_int size, bpf_u_int32 v, bpf_u_int32 mask) 1098 { 1099 return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v); 1100 } 1101 1102 static struct block * 1103 gen_bcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1104 u_int size, const u_char *v) 1105 { 1106 register struct block *b, *tmp; 1107 1108 b = NULL; 1109 while (size >= 4) { 1110 register const u_char *p = &v[size - 4]; 1111 1112 tmp = gen_cmp(cstate, offrel, offset + size - 4, BPF_W, 1113 EXTRACT_BE_U_4(p)); 1114 if (b != NULL) 1115 gen_and(b, tmp); 1116 b = tmp; 1117 size -= 4; 1118 } 1119 while (size >= 2) { 1120 register const u_char *p = &v[size - 2]; 1121 1122 tmp = gen_cmp(cstate, offrel, offset + size - 2, BPF_H, 1123 EXTRACT_BE_U_2(p)); 1124 if (b != NULL) 1125 gen_and(b, tmp); 1126 b = tmp; 1127 size -= 2; 1128 } 1129 if (size > 0) { 1130 tmp = gen_cmp(cstate, offrel, offset, BPF_B, v[0]); 1131 if (b != NULL) 1132 gen_and(b, tmp); 1133 b = tmp; 1134 } 1135 return b; 1136 } 1137 1138 /* 1139 * AND the field of size "size" at offset "offset" relative to the header 1140 * specified by "offrel" with "mask", and compare it with the value "v" 1141 * with the test specified by "jtype"; if "reverse" is true, the test 1142 * should test the opposite of "jtype". 1143 */ 1144 static struct block * 1145 gen_ncmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1146 u_int size, bpf_u_int32 mask, int jtype, int reverse, 1147 bpf_u_int32 v) 1148 { 1149 struct slist *s, *s2; 1150 struct block *b; 1151 1152 s = gen_load_a(cstate, offrel, offset, size); 1153 1154 if (mask != 0xffffffff) { 1155 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); 1156 s2->s.k = mask; 1157 sappend(s, s2); 1158 } 1159 1160 b = new_block(cstate, JMP(jtype)); 1161 b->stmts = s; 1162 b->s.k = v; 1163 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE)) 1164 gen_not(b); 1165 return b; 1166 } 1167 1168 static int 1169 init_linktype(compiler_state_t *cstate, pcap_t *p) 1170 { 1171 cstate->pcap_fddipad = p->fddipad; 1172 1173 /* 1174 * We start out with only one link-layer header. 1175 */ 1176 cstate->outermostlinktype = pcap_datalink(p); 1177 cstate->off_outermostlinkhdr.constant_part = 0; 1178 cstate->off_outermostlinkhdr.is_variable = 0; 1179 cstate->off_outermostlinkhdr.reg = -1; 1180 1181 cstate->prevlinktype = cstate->outermostlinktype; 1182 cstate->off_prevlinkhdr.constant_part = 0; 1183 cstate->off_prevlinkhdr.is_variable = 0; 1184 cstate->off_prevlinkhdr.reg = -1; 1185 1186 cstate->linktype = cstate->outermostlinktype; 1187 cstate->off_linkhdr.constant_part = 0; 1188 cstate->off_linkhdr.is_variable = 0; 1189 cstate->off_linkhdr.reg = -1; 1190 1191 /* 1192 * XXX 1193 */ 1194 cstate->off_linkpl.constant_part = 0; 1195 cstate->off_linkpl.is_variable = 0; 1196 cstate->off_linkpl.reg = -1; 1197 1198 cstate->off_linktype.constant_part = 0; 1199 cstate->off_linktype.is_variable = 0; 1200 cstate->off_linktype.reg = -1; 1201 1202 /* 1203 * Assume it's not raw ATM with a pseudo-header, for now. 1204 */ 1205 cstate->is_atm = 0; 1206 cstate->off_vpi = OFFSET_NOT_SET; 1207 cstate->off_vci = OFFSET_NOT_SET; 1208 cstate->off_proto = OFFSET_NOT_SET; 1209 cstate->off_payload = OFFSET_NOT_SET; 1210 1211 /* 1212 * And not Geneve. 1213 */ 1214 cstate->is_geneve = 0; 1215 1216 /* 1217 * No variable length VLAN offset by default 1218 */ 1219 cstate->is_vlan_vloffset = 0; 1220 1221 /* 1222 * And assume we're not doing SS7. 1223 */ 1224 cstate->off_li = OFFSET_NOT_SET; 1225 cstate->off_li_hsl = OFFSET_NOT_SET; 1226 cstate->off_sio = OFFSET_NOT_SET; 1227 cstate->off_opc = OFFSET_NOT_SET; 1228 cstate->off_dpc = OFFSET_NOT_SET; 1229 cstate->off_sls = OFFSET_NOT_SET; 1230 1231 cstate->label_stack_depth = 0; 1232 cstate->vlan_stack_depth = 0; 1233 1234 switch (cstate->linktype) { 1235 1236 case DLT_ARCNET: 1237 cstate->off_linktype.constant_part = 2; 1238 cstate->off_linkpl.constant_part = 6; 1239 cstate->off_nl = 0; /* XXX in reality, variable! */ 1240 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1241 break; 1242 1243 case DLT_ARCNET_LINUX: 1244 cstate->off_linktype.constant_part = 4; 1245 cstate->off_linkpl.constant_part = 8; 1246 cstate->off_nl = 0; /* XXX in reality, variable! */ 1247 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1248 break; 1249 1250 case DLT_EN10MB: 1251 cstate->off_linktype.constant_part = 12; 1252 cstate->off_linkpl.constant_part = 14; /* Ethernet header length */ 1253 cstate->off_nl = 0; /* Ethernet II */ 1254 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ 1255 break; 1256 1257 case DLT_SLIP: 1258 /* 1259 * SLIP doesn't have a link level type. The 16 byte 1260 * header is hacked into our SLIP driver. 1261 */ 1262 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1263 cstate->off_linkpl.constant_part = 16; 1264 cstate->off_nl = 0; 1265 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1266 break; 1267 1268 case DLT_SLIP_BSDOS: 1269 /* XXX this may be the same as the DLT_PPP_BSDOS case */ 1270 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1271 /* XXX end */ 1272 cstate->off_linkpl.constant_part = 24; 1273 cstate->off_nl = 0; 1274 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1275 break; 1276 1277 case DLT_NULL: 1278 case DLT_LOOP: 1279 cstate->off_linktype.constant_part = 0; 1280 cstate->off_linkpl.constant_part = 4; 1281 cstate->off_nl = 0; 1282 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1283 break; 1284 1285 case DLT_ENC: 1286 cstate->off_linktype.constant_part = 0; 1287 cstate->off_linkpl.constant_part = 12; 1288 cstate->off_nl = 0; 1289 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1290 break; 1291 1292 case DLT_PPP: 1293 case DLT_PPP_PPPD: 1294 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */ 1295 case DLT_HDLC: /* NetBSD (Cisco) HDLC */ 1296 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */ 1297 cstate->off_linktype.constant_part = 2; /* skip HDLC-like framing */ 1298 cstate->off_linkpl.constant_part = 4; /* skip HDLC-like framing and protocol field */ 1299 cstate->off_nl = 0; 1300 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1301 break; 1302 1303 case DLT_PPP_ETHER: 1304 /* 1305 * This does no include the Ethernet header, and 1306 * only covers session state. 1307 */ 1308 cstate->off_linktype.constant_part = 6; 1309 cstate->off_linkpl.constant_part = 8; 1310 cstate->off_nl = 0; 1311 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1312 break; 1313 1314 case DLT_PPP_BSDOS: 1315 cstate->off_linktype.constant_part = 5; 1316 cstate->off_linkpl.constant_part = 24; 1317 cstate->off_nl = 0; 1318 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1319 break; 1320 1321 case DLT_FDDI: 1322 /* 1323 * FDDI doesn't really have a link-level type field. 1324 * We set "off_linktype" to the offset of the LLC header. 1325 * 1326 * To check for Ethernet types, we assume that SSAP = SNAP 1327 * is being used and pick out the encapsulated Ethernet type. 1328 * XXX - should we generate code to check for SNAP? 1329 */ 1330 cstate->off_linktype.constant_part = 13; 1331 cstate->off_linktype.constant_part += cstate->pcap_fddipad; 1332 cstate->off_linkpl.constant_part = 13; /* FDDI MAC header length */ 1333 cstate->off_linkpl.constant_part += cstate->pcap_fddipad; 1334 cstate->off_nl = 8; /* 802.2+SNAP */ 1335 cstate->off_nl_nosnap = 3; /* 802.2 */ 1336 break; 1337 1338 case DLT_IEEE802: 1339 /* 1340 * Token Ring doesn't really have a link-level type field. 1341 * We set "off_linktype" to the offset of the LLC header. 1342 * 1343 * To check for Ethernet types, we assume that SSAP = SNAP 1344 * is being used and pick out the encapsulated Ethernet type. 1345 * XXX - should we generate code to check for SNAP? 1346 * 1347 * XXX - the header is actually variable-length. 1348 * Some various Linux patched versions gave 38 1349 * as "off_linktype" and 40 as "off_nl"; however, 1350 * if a token ring packet has *no* routing 1351 * information, i.e. is not source-routed, the correct 1352 * values are 20 and 22, as they are in the vanilla code. 1353 * 1354 * A packet is source-routed iff the uppermost bit 1355 * of the first byte of the source address, at an 1356 * offset of 8, has the uppermost bit set. If the 1357 * packet is source-routed, the total number of bytes 1358 * of routing information is 2 plus bits 0x1F00 of 1359 * the 16-bit value at an offset of 14 (shifted right 1360 * 8 - figure out which byte that is). 1361 */ 1362 cstate->off_linktype.constant_part = 14; 1363 cstate->off_linkpl.constant_part = 14; /* Token Ring MAC header length */ 1364 cstate->off_nl = 8; /* 802.2+SNAP */ 1365 cstate->off_nl_nosnap = 3; /* 802.2 */ 1366 break; 1367 1368 case DLT_PRISM_HEADER: 1369 case DLT_IEEE802_11_RADIO_AVS: 1370 case DLT_IEEE802_11_RADIO: 1371 cstate->off_linkhdr.is_variable = 1; 1372 /* Fall through, 802.11 doesn't have a variable link 1373 * prefix but is otherwise the same. */ 1374 /* FALLTHROUGH */ 1375 1376 case DLT_IEEE802_11: 1377 /* 1378 * 802.11 doesn't really have a link-level type field. 1379 * We set "off_linktype.constant_part" to the offset of 1380 * the LLC header. 1381 * 1382 * To check for Ethernet types, we assume that SSAP = SNAP 1383 * is being used and pick out the encapsulated Ethernet type. 1384 * XXX - should we generate code to check for SNAP? 1385 * 1386 * We also handle variable-length radio headers here. 1387 * The Prism header is in theory variable-length, but in 1388 * practice it's always 144 bytes long. However, some 1389 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but 1390 * sometimes or always supply an AVS header, so we 1391 * have to check whether the radio header is a Prism 1392 * header or an AVS header, so, in practice, it's 1393 * variable-length. 1394 */ 1395 cstate->off_linktype.constant_part = 24; 1396 cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ 1397 cstate->off_linkpl.is_variable = 1; 1398 cstate->off_nl = 8; /* 802.2+SNAP */ 1399 cstate->off_nl_nosnap = 3; /* 802.2 */ 1400 break; 1401 1402 case DLT_PPI: 1403 /* 1404 * At the moment we treat PPI the same way that we treat 1405 * normal Radiotap encoded packets. The difference is in 1406 * the function that generates the code at the beginning 1407 * to compute the header length. Since this code generator 1408 * of PPI supports bare 802.11 encapsulation only (i.e. 1409 * the encapsulated DLT should be DLT_IEEE802_11) we 1410 * generate code to check for this too. 1411 */ 1412 cstate->off_linktype.constant_part = 24; 1413 cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ 1414 cstate->off_linkpl.is_variable = 1; 1415 cstate->off_linkhdr.is_variable = 1; 1416 cstate->off_nl = 8; /* 802.2+SNAP */ 1417 cstate->off_nl_nosnap = 3; /* 802.2 */ 1418 break; 1419 1420 case DLT_ATM_RFC1483: 1421 case DLT_ATM_CLIP: /* Linux ATM defines this */ 1422 /* 1423 * assume routed, non-ISO PDUs 1424 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00) 1425 * 1426 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS, 1427 * or PPP with the PPP NLPID (e.g., PPPoA)? The 1428 * latter would presumably be treated the way PPPoE 1429 * should be, so you can do "pppoe and udp port 2049" 1430 * or "pppoa and tcp port 80" and have it check for 1431 * PPPo{A,E} and a PPP protocol of IP and.... 1432 */ 1433 cstate->off_linktype.constant_part = 0; 1434 cstate->off_linkpl.constant_part = 0; /* packet begins with LLC header */ 1435 cstate->off_nl = 8; /* 802.2+SNAP */ 1436 cstate->off_nl_nosnap = 3; /* 802.2 */ 1437 break; 1438 1439 case DLT_SUNATM: 1440 /* 1441 * Full Frontal ATM; you get AALn PDUs with an ATM 1442 * pseudo-header. 1443 */ 1444 cstate->is_atm = 1; 1445 cstate->off_vpi = SUNATM_VPI_POS; 1446 cstate->off_vci = SUNATM_VCI_POS; 1447 cstate->off_proto = PROTO_POS; 1448 cstate->off_payload = SUNATM_PKT_BEGIN_POS; 1449 cstate->off_linktype.constant_part = cstate->off_payload; 1450 cstate->off_linkpl.constant_part = cstate->off_payload; /* if LLC-encapsulated */ 1451 cstate->off_nl = 8; /* 802.2+SNAP */ 1452 cstate->off_nl_nosnap = 3; /* 802.2 */ 1453 break; 1454 1455 case DLT_RAW: 1456 case DLT_IPV4: 1457 case DLT_IPV6: 1458 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1459 cstate->off_linkpl.constant_part = 0; 1460 cstate->off_nl = 0; 1461 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1462 break; 1463 1464 case DLT_LINUX_SLL: /* fake header for Linux cooked socket v1 */ 1465 cstate->off_linktype.constant_part = 14; 1466 cstate->off_linkpl.constant_part = 16; 1467 cstate->off_nl = 0; 1468 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1469 break; 1470 1471 case DLT_LINUX_SLL2: /* fake header for Linux cooked socket v2 */ 1472 cstate->off_linktype.constant_part = 0; 1473 cstate->off_linkpl.constant_part = 20; 1474 cstate->off_nl = 0; 1475 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1476 break; 1477 1478 case DLT_LTALK: 1479 /* 1480 * LocalTalk does have a 1-byte type field in the LLAP header, 1481 * but really it just indicates whether there is a "short" or 1482 * "long" DDP packet following. 1483 */ 1484 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1485 cstate->off_linkpl.constant_part = 0; 1486 cstate->off_nl = 0; 1487 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1488 break; 1489 1490 case DLT_IP_OVER_FC: 1491 /* 1492 * RFC 2625 IP-over-Fibre-Channel doesn't really have a 1493 * link-level type field. We set "off_linktype" to the 1494 * offset of the LLC header. 1495 * 1496 * To check for Ethernet types, we assume that SSAP = SNAP 1497 * is being used and pick out the encapsulated Ethernet type. 1498 * XXX - should we generate code to check for SNAP? RFC 1499 * 2625 says SNAP should be used. 1500 */ 1501 cstate->off_linktype.constant_part = 16; 1502 cstate->off_linkpl.constant_part = 16; 1503 cstate->off_nl = 8; /* 802.2+SNAP */ 1504 cstate->off_nl_nosnap = 3; /* 802.2 */ 1505 break; 1506 1507 case DLT_FRELAY: 1508 /* 1509 * XXX - we should set this to handle SNAP-encapsulated 1510 * frames (NLPID of 0x80). 1511 */ 1512 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1513 cstate->off_linkpl.constant_part = 0; 1514 cstate->off_nl = 0; 1515 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1516 break; 1517 1518 /* 1519 * the only BPF-interesting FRF.16 frames are non-control frames; 1520 * Frame Relay has a variable length link-layer 1521 * so lets start with offset 4 for now and increments later on (FIXME); 1522 */ 1523 case DLT_MFR: 1524 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1525 cstate->off_linkpl.constant_part = 0; 1526 cstate->off_nl = 4; 1527 cstate->off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */ 1528 break; 1529 1530 case DLT_APPLE_IP_OVER_IEEE1394: 1531 cstate->off_linktype.constant_part = 16; 1532 cstate->off_linkpl.constant_part = 18; 1533 cstate->off_nl = 0; 1534 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1535 break; 1536 1537 case DLT_SYMANTEC_FIREWALL: 1538 cstate->off_linktype.constant_part = 6; 1539 cstate->off_linkpl.constant_part = 44; 1540 cstate->off_nl = 0; /* Ethernet II */ 1541 cstate->off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */ 1542 break; 1543 1544 case DLT_PFLOG: 1545 cstate->off_linktype.constant_part = 0; 1546 cstate->off_linkpl.constant_part = 0; /* link-layer header is variable-length */ 1547 cstate->off_linkpl.is_variable = 1; 1548 cstate->off_nl = 0; 1549 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 1550 break; 1551 1552 case DLT_JUNIPER_MFR: 1553 case DLT_JUNIPER_MLFR: 1554 case DLT_JUNIPER_MLPPP: 1555 case DLT_JUNIPER_PPP: 1556 case DLT_JUNIPER_CHDLC: 1557 case DLT_JUNIPER_FRELAY: 1558 cstate->off_linktype.constant_part = 4; 1559 cstate->off_linkpl.constant_part = 4; 1560 cstate->off_nl = 0; 1561 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1562 break; 1563 1564 case DLT_JUNIPER_ATM1: 1565 cstate->off_linktype.constant_part = 4; /* in reality variable between 4-8 */ 1566 cstate->off_linkpl.constant_part = 4; /* in reality variable between 4-8 */ 1567 cstate->off_nl = 0; 1568 cstate->off_nl_nosnap = 10; 1569 break; 1570 1571 case DLT_JUNIPER_ATM2: 1572 cstate->off_linktype.constant_part = 8; /* in reality variable between 8-12 */ 1573 cstate->off_linkpl.constant_part = 8; /* in reality variable between 8-12 */ 1574 cstate->off_nl = 0; 1575 cstate->off_nl_nosnap = 10; 1576 break; 1577 1578 /* frames captured on a Juniper PPPoE service PIC 1579 * contain raw ethernet frames */ 1580 case DLT_JUNIPER_PPPOE: 1581 case DLT_JUNIPER_ETHER: 1582 cstate->off_linkpl.constant_part = 14; 1583 cstate->off_linktype.constant_part = 16; 1584 cstate->off_nl = 18; /* Ethernet II */ 1585 cstate->off_nl_nosnap = 21; /* 802.3+802.2 */ 1586 break; 1587 1588 case DLT_JUNIPER_PPPOE_ATM: 1589 cstate->off_linktype.constant_part = 4; 1590 cstate->off_linkpl.constant_part = 6; 1591 cstate->off_nl = 0; 1592 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1593 break; 1594 1595 case DLT_JUNIPER_GGSN: 1596 cstate->off_linktype.constant_part = 6; 1597 cstate->off_linkpl.constant_part = 12; 1598 cstate->off_nl = 0; 1599 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1600 break; 1601 1602 case DLT_JUNIPER_ES: 1603 cstate->off_linktype.constant_part = 6; 1604 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */ 1605 cstate->off_nl = OFFSET_NOT_SET; /* not really a network layer but raw IP addresses */ 1606 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1607 break; 1608 1609 case DLT_JUNIPER_MONITOR: 1610 cstate->off_linktype.constant_part = 12; 1611 cstate->off_linkpl.constant_part = 12; 1612 cstate->off_nl = 0; /* raw IP/IP6 header */ 1613 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1614 break; 1615 1616 case DLT_BACNET_MS_TP: 1617 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1618 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1619 cstate->off_nl = OFFSET_NOT_SET; 1620 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1621 break; 1622 1623 case DLT_JUNIPER_SERVICES: 1624 cstate->off_linktype.constant_part = 12; 1625 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */ 1626 cstate->off_nl = OFFSET_NOT_SET; /* L3 proto location dep. on cookie type */ 1627 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1628 break; 1629 1630 case DLT_JUNIPER_VP: 1631 cstate->off_linktype.constant_part = 18; 1632 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1633 cstate->off_nl = OFFSET_NOT_SET; 1634 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1635 break; 1636 1637 case DLT_JUNIPER_ST: 1638 cstate->off_linktype.constant_part = 18; 1639 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1640 cstate->off_nl = OFFSET_NOT_SET; 1641 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1642 break; 1643 1644 case DLT_JUNIPER_ISM: 1645 cstate->off_linktype.constant_part = 8; 1646 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1647 cstate->off_nl = OFFSET_NOT_SET; 1648 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1649 break; 1650 1651 case DLT_JUNIPER_VS: 1652 case DLT_JUNIPER_SRX_E2E: 1653 case DLT_JUNIPER_FIBRECHANNEL: 1654 case DLT_JUNIPER_ATM_CEMIC: 1655 cstate->off_linktype.constant_part = 8; 1656 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1657 cstate->off_nl = OFFSET_NOT_SET; 1658 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1659 break; 1660 1661 case DLT_MTP2: 1662 cstate->off_li = 2; 1663 cstate->off_li_hsl = 4; 1664 cstate->off_sio = 3; 1665 cstate->off_opc = 4; 1666 cstate->off_dpc = 4; 1667 cstate->off_sls = 7; 1668 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1669 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1670 cstate->off_nl = OFFSET_NOT_SET; 1671 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1672 break; 1673 1674 case DLT_MTP2_WITH_PHDR: 1675 cstate->off_li = 6; 1676 cstate->off_li_hsl = 8; 1677 cstate->off_sio = 7; 1678 cstate->off_opc = 8; 1679 cstate->off_dpc = 8; 1680 cstate->off_sls = 11; 1681 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1682 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1683 cstate->off_nl = OFFSET_NOT_SET; 1684 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1685 break; 1686 1687 case DLT_ERF: 1688 cstate->off_li = 22; 1689 cstate->off_li_hsl = 24; 1690 cstate->off_sio = 23; 1691 cstate->off_opc = 24; 1692 cstate->off_dpc = 24; 1693 cstate->off_sls = 27; 1694 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1695 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1696 cstate->off_nl = OFFSET_NOT_SET; 1697 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1698 break; 1699 1700 case DLT_PFSYNC: 1701 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1702 cstate->off_linkpl.constant_part = 4; 1703 cstate->off_nl = 0; 1704 cstate->off_nl_nosnap = 0; 1705 break; 1706 1707 case DLT_AX25_KISS: 1708 /* 1709 * Currently, only raw "link[N:M]" filtering is supported. 1710 */ 1711 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ 1712 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1713 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ 1714 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1715 break; 1716 1717 case DLT_IPNET: 1718 cstate->off_linktype.constant_part = 1; 1719 cstate->off_linkpl.constant_part = 24; /* ipnet header length */ 1720 cstate->off_nl = 0; 1721 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1722 break; 1723 1724 case DLT_NETANALYZER: 1725 cstate->off_linkhdr.constant_part = 4; /* Ethernet header is past 4-byte pseudo-header */ 1726 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; 1727 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+Ethernet header length */ 1728 cstate->off_nl = 0; /* Ethernet II */ 1729 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ 1730 break; 1731 1732 case DLT_NETANALYZER_TRANSPARENT: 1733 cstate->off_linkhdr.constant_part = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */ 1734 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; 1735 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* pseudo-header+preamble+SFD+Ethernet header length */ 1736 cstate->off_nl = 0; /* Ethernet II */ 1737 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ 1738 break; 1739 1740 case DLT_EN3MB: 1741 /* 1742 * Currently, only raw "link[N:M]" filtering is supported. 1743 */ 1744 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ 1745 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1746 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ 1747 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1748 break; 1749 1750 case DLT_AX25: 1751 /* 1752 * Currently, only raw "link[N:M]" filtering is supported. 1753 */ 1754 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ 1755 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1756 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ 1757 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1758 break; 1759 1760 case DLT_PRONET: 1761 /* 1762 * Currently, only raw "link[N:M]" filtering is supported. 1763 */ 1764 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ 1765 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1766 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ 1767 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1768 break; 1769 1770 case DLT_CHAOS: 1771 /* 1772 * Currently, only raw "link[N:M]" filtering is supported. 1773 */ 1774 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ 1775 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1776 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ 1777 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1778 break; 1779 1780 #ifdef DLT_HIPPI 1781 case DLT_HIPPI: 1782 /* 1783 * Currently, only raw "link[N:M]" filtering is supported. 1784 */ 1785 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ 1786 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1787 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ 1788 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1789 break; 1790 1791 #endif 1792 1793 case DLT_REDBACK_SMARTEDGE: 1794 /* 1795 * Currently, only raw "link[N:M]" filtering is supported. 1796 */ 1797 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ 1798 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1799 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ 1800 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1801 break; 1802 1803 1804 #ifdef DLT_HHDLC 1805 case DLT_HHDLC: 1806 /* 1807 * Currently, only raw "link[N:M]" filtering is supported. 1808 */ 1809 cstate->off_linktype.constant_part = OFFSET_NOT_SET; /* variable, min 15, max 71 steps of 7 */ 1810 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1811 cstate->off_nl = OFFSET_NOT_SET; /* variable, min 16, max 71 steps of 7 */ 1812 cstate->off_nl_nosnap = OFFSET_NOT_SET; /* no 802.2 LLC */ 1813 break; 1814 1815 #endif 1816 1817 default: 1818 /* 1819 * For values in the range in which we've assigned new 1820 * DLT_ values, only raw "link[N:M]" filtering is supported. 1821 */ 1822 if (cstate->linktype >= DLT_HIGH_MATCHING_MIN && 1823 cstate->linktype <= DLT_HIGH_MATCHING_MAX) { 1824 cstate->off_linktype.constant_part = OFFSET_NOT_SET; 1825 cstate->off_linkpl.constant_part = OFFSET_NOT_SET; 1826 cstate->off_nl = OFFSET_NOT_SET; 1827 cstate->off_nl_nosnap = OFFSET_NOT_SET; 1828 } else { 1829 bpf_set_error(cstate, "unknown data link type %d", 1830 cstate->linktype); 1831 return (-1); 1832 } 1833 break; 1834 } 1835 1836 cstate->off_outermostlinkhdr = cstate->off_prevlinkhdr = cstate->off_linkhdr; 1837 return (0); 1838 } 1839 1840 /* 1841 * Load a value relative to the specified absolute offset. 1842 */ 1843 static struct slist * 1844 gen_load_absoffsetrel(compiler_state_t *cstate, bpf_abs_offset *abs_offset, 1845 u_int offset, u_int size) 1846 { 1847 struct slist *s, *s2; 1848 1849 s = gen_abs_offset_varpart(cstate, abs_offset); 1850 1851 /* 1852 * If "s" is non-null, it has code to arrange that the X register 1853 * contains the variable part of the absolute offset, so we 1854 * generate a load relative to that, with an offset of 1855 * abs_offset->constant_part + offset. 1856 * 1857 * Otherwise, we can do an absolute load with an offset of 1858 * abs_offset->constant_part + offset. 1859 */ 1860 if (s != NULL) { 1861 /* 1862 * "s" points to a list of statements that puts the 1863 * variable part of the absolute offset into the X register. 1864 * Do an indirect load, to use the X register as an offset. 1865 */ 1866 s2 = new_stmt(cstate, BPF_LD|BPF_IND|size); 1867 s2->s.k = abs_offset->constant_part + offset; 1868 sappend(s, s2); 1869 } else { 1870 /* 1871 * There is no variable part of the absolute offset, so 1872 * just do an absolute load. 1873 */ 1874 s = new_stmt(cstate, BPF_LD|BPF_ABS|size); 1875 s->s.k = abs_offset->constant_part + offset; 1876 } 1877 return s; 1878 } 1879 1880 /* 1881 * Load a value relative to the beginning of the specified header. 1882 */ 1883 static struct slist * 1884 gen_load_a(compiler_state_t *cstate, enum e_offrel offrel, u_int offset, 1885 u_int size) 1886 { 1887 struct slist *s, *s2; 1888 1889 /* 1890 * Squelch warnings from compilers that *don't* assume that 1891 * offrel always has a valid enum value and therefore don't 1892 * assume that we'll always go through one of the case arms. 1893 * 1894 * If we have a default case, compilers that *do* assume that 1895 * will then complain about the default case code being 1896 * unreachable. 1897 * 1898 * Damned if you do, damned if you don't. 1899 */ 1900 s = NULL; 1901 1902 switch (offrel) { 1903 1904 case OR_PACKET: 1905 s = new_stmt(cstate, BPF_LD|BPF_ABS|size); 1906 s->s.k = offset; 1907 break; 1908 1909 case OR_LINKHDR: 1910 s = gen_load_absoffsetrel(cstate, &cstate->off_linkhdr, offset, size); 1911 break; 1912 1913 case OR_PREVLINKHDR: 1914 s = gen_load_absoffsetrel(cstate, &cstate->off_prevlinkhdr, offset, size); 1915 break; 1916 1917 case OR_LLC: 1918 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, offset, size); 1919 break; 1920 1921 case OR_PREVMPLSHDR: 1922 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl - 4 + offset, size); 1923 break; 1924 1925 case OR_LINKPL: 1926 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + offset, size); 1927 break; 1928 1929 case OR_LINKPL_NOSNAP: 1930 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl_nosnap + offset, size); 1931 break; 1932 1933 case OR_LINKTYPE: 1934 s = gen_load_absoffsetrel(cstate, &cstate->off_linktype, offset, size); 1935 break; 1936 1937 case OR_TRAN_IPV4: 1938 /* 1939 * Load the X register with the length of the IPv4 header 1940 * (plus the offset of the link-layer header, if it's 1941 * preceded by a variable-length header such as a radio 1942 * header), in bytes. 1943 */ 1944 s = gen_loadx_iphdrlen(cstate); 1945 1946 /* 1947 * Load the item at {offset of the link-layer payload} + 1948 * {offset, relative to the start of the link-layer 1949 * payload, of the IPv4 header} + {length of the IPv4 header} + 1950 * {specified offset}. 1951 * 1952 * If the offset of the link-layer payload is variable, 1953 * the variable part of that offset is included in the 1954 * value in the X register, and we include the constant 1955 * part in the offset of the load. 1956 */ 1957 s2 = new_stmt(cstate, BPF_LD|BPF_IND|size); 1958 s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + offset; 1959 sappend(s, s2); 1960 break; 1961 1962 case OR_TRAN_IPV6: 1963 s = gen_load_absoffsetrel(cstate, &cstate->off_linkpl, cstate->off_nl + 40 + offset, size); 1964 break; 1965 } 1966 return s; 1967 } 1968 1969 /* 1970 * Generate code to load into the X register the sum of the length of 1971 * the IPv4 header and the variable part of the offset of the link-layer 1972 * payload. 1973 */ 1974 static struct slist * 1975 gen_loadx_iphdrlen(compiler_state_t *cstate) 1976 { 1977 struct slist *s, *s2; 1978 1979 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); 1980 if (s != NULL) { 1981 /* 1982 * The offset of the link-layer payload has a variable 1983 * part. "s" points to a list of statements that put 1984 * the variable part of that offset into the X register. 1985 * 1986 * The 4*([k]&0xf) addressing mode can't be used, as we 1987 * don't have a constant offset, so we have to load the 1988 * value in question into the A register and add to it 1989 * the value from the X register. 1990 */ 1991 s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); 1992 s2->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; 1993 sappend(s, s2); 1994 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); 1995 s2->s.k = 0xf; 1996 sappend(s, s2); 1997 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); 1998 s2->s.k = 2; 1999 sappend(s, s2); 2000 2001 /* 2002 * The A register now contains the length of the IP header. 2003 * We need to add to it the variable part of the offset of 2004 * the link-layer payload, which is still in the X 2005 * register, and move the result into the X register. 2006 */ 2007 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); 2008 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); 2009 } else { 2010 /* 2011 * The offset of the link-layer payload is a constant, 2012 * so no code was generated to load the (nonexistent) 2013 * variable part of that offset. 2014 * 2015 * This means we can use the 4*([k]&0xf) addressing 2016 * mode. Load the length of the IPv4 header, which 2017 * is at an offset of cstate->off_nl from the beginning of 2018 * the link-layer payload, and thus at an offset of 2019 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning 2020 * of the raw packet data, using that addressing mode. 2021 */ 2022 s = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B); 2023 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; 2024 } 2025 return s; 2026 } 2027 2028 2029 static struct block * 2030 gen_uncond(compiler_state_t *cstate, int rsense) 2031 { 2032 struct block *b; 2033 struct slist *s; 2034 2035 s = new_stmt(cstate, BPF_LD|BPF_IMM); 2036 s->s.k = !rsense; 2037 b = new_block(cstate, JMP(BPF_JEQ)); 2038 b->stmts = s; 2039 2040 return b; 2041 } 2042 2043 static inline struct block * 2044 gen_true(compiler_state_t *cstate) 2045 { 2046 return gen_uncond(cstate, 1); 2047 } 2048 2049 static inline struct block * 2050 gen_false(compiler_state_t *cstate) 2051 { 2052 return gen_uncond(cstate, 0); 2053 } 2054 2055 /* 2056 * Byte-swap a 32-bit number. 2057 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on 2058 * big-endian platforms.) 2059 */ 2060 #define SWAPLONG(y) \ 2061 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff)) 2062 2063 /* 2064 * Generate code to match a particular packet type. 2065 * 2066 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP 2067 * value, if <= ETHERMTU. We use that to determine whether to 2068 * match the type/length field or to check the type/length field for 2069 * a value <= ETHERMTU to see whether it's a type field and then do 2070 * the appropriate test. 2071 */ 2072 static struct block * 2073 gen_ether_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) 2074 { 2075 struct block *b0, *b1; 2076 2077 switch (ll_proto) { 2078 2079 case LLCSAP_ISONS: 2080 case LLCSAP_IP: 2081 case LLCSAP_NETBEUI: 2082 /* 2083 * OSI protocols and NetBEUI always use 802.2 encapsulation, 2084 * so we check the DSAP and SSAP. 2085 * 2086 * LLCSAP_IP checks for IP-over-802.2, rather 2087 * than IP-over-Ethernet or IP-over-SNAP. 2088 * 2089 * XXX - should we check both the DSAP and the 2090 * SSAP, like this, or should we check just the 2091 * DSAP, as we do for other types <= ETHERMTU 2092 * (i.e., other SAP values)? 2093 */ 2094 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); 2095 gen_not(b0); 2096 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto); 2097 gen_and(b0, b1); 2098 return b1; 2099 2100 case LLCSAP_IPX: 2101 /* 2102 * Check for; 2103 * 2104 * Ethernet_II frames, which are Ethernet 2105 * frames with a frame type of ETHERTYPE_IPX; 2106 * 2107 * Ethernet_802.3 frames, which are 802.3 2108 * frames (i.e., the type/length field is 2109 * a length field, <= ETHERMTU, rather than 2110 * a type field) with the first two bytes 2111 * after the Ethernet/802.3 header being 2112 * 0xFFFF; 2113 * 2114 * Ethernet_802.2 frames, which are 802.3 2115 * frames with an 802.2 LLC header and 2116 * with the IPX LSAP as the DSAP in the LLC 2117 * header; 2118 * 2119 * Ethernet_SNAP frames, which are 802.3 2120 * frames with an LLC header and a SNAP 2121 * header and with an OUI of 0x000000 2122 * (encapsulated Ethernet) and a protocol 2123 * ID of ETHERTYPE_IPX in the SNAP header. 2124 * 2125 * XXX - should we generate the same code both 2126 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX? 2127 */ 2128 2129 /* 2130 * This generates code to check both for the 2131 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3. 2132 */ 2133 b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); 2134 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF); 2135 gen_or(b0, b1); 2136 2137 /* 2138 * Now we add code to check for SNAP frames with 2139 * ETHERTYPE_IPX, i.e. Ethernet_SNAP. 2140 */ 2141 b0 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX); 2142 gen_or(b0, b1); 2143 2144 /* 2145 * Now we generate code to check for 802.3 2146 * frames in general. 2147 */ 2148 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); 2149 gen_not(b0); 2150 2151 /* 2152 * Now add the check for 802.3 frames before the 2153 * check for Ethernet_802.2 and Ethernet_802.3, 2154 * as those checks should only be done on 802.3 2155 * frames, not on Ethernet frames. 2156 */ 2157 gen_and(b0, b1); 2158 2159 /* 2160 * Now add the check for Ethernet_II frames, and 2161 * do that before checking for the other frame 2162 * types. 2163 */ 2164 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX); 2165 gen_or(b0, b1); 2166 return b1; 2167 2168 case ETHERTYPE_ATALK: 2169 case ETHERTYPE_AARP: 2170 /* 2171 * EtherTalk (AppleTalk protocols on Ethernet link 2172 * layer) may use 802.2 encapsulation. 2173 */ 2174 2175 /* 2176 * Check for 802.2 encapsulation (EtherTalk phase 2?); 2177 * we check for an Ethernet type field less than 2178 * 1500, which means it's an 802.3 length field. 2179 */ 2180 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); 2181 gen_not(b0); 2182 2183 /* 2184 * 802.2-encapsulated ETHERTYPE_ATALK packets are 2185 * SNAP packets with an organization code of 2186 * 0x080007 (Apple, for Appletalk) and a protocol 2187 * type of ETHERTYPE_ATALK (Appletalk). 2188 * 2189 * 802.2-encapsulated ETHERTYPE_AARP packets are 2190 * SNAP packets with an organization code of 2191 * 0x000000 (encapsulated Ethernet) and a protocol 2192 * type of ETHERTYPE_AARP (Appletalk ARP). 2193 */ 2194 if (ll_proto == ETHERTYPE_ATALK) 2195 b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); 2196 else /* ll_proto == ETHERTYPE_AARP */ 2197 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP); 2198 gen_and(b0, b1); 2199 2200 /* 2201 * Check for Ethernet encapsulation (Ethertalk 2202 * phase 1?); we just check for the Ethernet 2203 * protocol type. 2204 */ 2205 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); 2206 2207 gen_or(b0, b1); 2208 return b1; 2209 2210 default: 2211 if (ll_proto <= ETHERMTU) { 2212 /* 2213 * This is an LLC SAP value, so the frames 2214 * that match would be 802.2 frames. 2215 * Check that the frame is an 802.2 frame 2216 * (i.e., that the length/type field is 2217 * a length field, <= ETHERMTU) and 2218 * then check the DSAP. 2219 */ 2220 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); 2221 gen_not(b0); 2222 b1 = gen_cmp(cstate, OR_LINKTYPE, 2, BPF_B, ll_proto); 2223 gen_and(b0, b1); 2224 return b1; 2225 } else { 2226 /* 2227 * This is an Ethernet type, so compare 2228 * the length/type field with it (if 2229 * the frame is an 802.2 frame, the length 2230 * field will be <= ETHERMTU, and, as 2231 * "ll_proto" is > ETHERMTU, this test 2232 * will fail and the frame won't match, 2233 * which is what we want). 2234 */ 2235 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); 2236 } 2237 } 2238 } 2239 2240 static struct block * 2241 gen_loopback_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) 2242 { 2243 /* 2244 * For DLT_NULL, the link-layer header is a 32-bit word 2245 * containing an AF_ value in *host* byte order, and for 2246 * DLT_ENC, the link-layer header begins with a 32-bit 2247 * word containing an AF_ value in host byte order. 2248 * 2249 * In addition, if we're reading a saved capture file, 2250 * the host byte order in the capture may not be the 2251 * same as the host byte order on this machine. 2252 * 2253 * For DLT_LOOP, the link-layer header is a 32-bit 2254 * word containing an AF_ value in *network* byte order. 2255 */ 2256 if (cstate->linktype == DLT_NULL || cstate->linktype == DLT_ENC) { 2257 /* 2258 * The AF_ value is in host byte order, but the BPF 2259 * interpreter will convert it to network byte order. 2260 * 2261 * If this is a save file, and it's from a machine 2262 * with the opposite byte order to ours, we byte-swap 2263 * the AF_ value. 2264 * 2265 * Then we run it through "htonl()", and generate 2266 * code to compare against the result. 2267 */ 2268 if (cstate->bpf_pcap->rfile != NULL && cstate->bpf_pcap->swapped) 2269 ll_proto = SWAPLONG(ll_proto); 2270 ll_proto = htonl(ll_proto); 2271 } 2272 return (gen_cmp(cstate, OR_LINKHDR, 0, BPF_W, ll_proto)); 2273 } 2274 2275 /* 2276 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4 2277 * or IPv6 then we have an error. 2278 */ 2279 static struct block * 2280 gen_ipnet_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) 2281 { 2282 switch (ll_proto) { 2283 2284 case ETHERTYPE_IP: 2285 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET); 2286 /*NOTREACHED*/ 2287 2288 case ETHERTYPE_IPV6: 2289 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, IPH_AF_INET6); 2290 /*NOTREACHED*/ 2291 2292 default: 2293 break; 2294 } 2295 2296 return gen_false(cstate); 2297 } 2298 2299 /* 2300 * Generate code to match a particular packet type. 2301 * 2302 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP 2303 * value, if <= ETHERMTU. We use that to determine whether to 2304 * match the type field or to check the type field for the special 2305 * LINUX_SLL_P_802_2 value and then do the appropriate test. 2306 */ 2307 static struct block * 2308 gen_linux_sll_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) 2309 { 2310 struct block *b0, *b1; 2311 2312 switch (ll_proto) { 2313 2314 case LLCSAP_ISONS: 2315 case LLCSAP_IP: 2316 case LLCSAP_NETBEUI: 2317 /* 2318 * OSI protocols and NetBEUI always use 802.2 encapsulation, 2319 * so we check the DSAP and SSAP. 2320 * 2321 * LLCSAP_IP checks for IP-over-802.2, rather 2322 * than IP-over-Ethernet or IP-over-SNAP. 2323 * 2324 * XXX - should we check both the DSAP and the 2325 * SSAP, like this, or should we check just the 2326 * DSAP, as we do for other types <= ETHERMTU 2327 * (i.e., other SAP values)? 2328 */ 2329 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); 2330 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, (ll_proto << 8) | ll_proto); 2331 gen_and(b0, b1); 2332 return b1; 2333 2334 case LLCSAP_IPX: 2335 /* 2336 * Ethernet_II frames, which are Ethernet 2337 * frames with a frame type of ETHERTYPE_IPX; 2338 * 2339 * Ethernet_802.3 frames, which have a frame 2340 * type of LINUX_SLL_P_802_3; 2341 * 2342 * Ethernet_802.2 frames, which are 802.3 2343 * frames with an 802.2 LLC header (i.e, have 2344 * a frame type of LINUX_SLL_P_802_2) and 2345 * with the IPX LSAP as the DSAP in the LLC 2346 * header; 2347 * 2348 * Ethernet_SNAP frames, which are 802.3 2349 * frames with an LLC header and a SNAP 2350 * header and with an OUI of 0x000000 2351 * (encapsulated Ethernet) and a protocol 2352 * ID of ETHERTYPE_IPX in the SNAP header. 2353 * 2354 * First, do the checks on LINUX_SLL_P_802_2 2355 * frames; generate the check for either 2356 * Ethernet_802.2 or Ethernet_SNAP frames, and 2357 * then put a check for LINUX_SLL_P_802_2 frames 2358 * before it. 2359 */ 2360 b0 = gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); 2361 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_IPX); 2362 gen_or(b0, b1); 2363 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); 2364 gen_and(b0, b1); 2365 2366 /* 2367 * Now check for 802.3 frames and OR that with 2368 * the previous test. 2369 */ 2370 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_3); 2371 gen_or(b0, b1); 2372 2373 /* 2374 * Now add the check for Ethernet_II frames, and 2375 * do that before checking for the other frame 2376 * types. 2377 */ 2378 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ETHERTYPE_IPX); 2379 gen_or(b0, b1); 2380 return b1; 2381 2382 case ETHERTYPE_ATALK: 2383 case ETHERTYPE_AARP: 2384 /* 2385 * EtherTalk (AppleTalk protocols on Ethernet link 2386 * layer) may use 802.2 encapsulation. 2387 */ 2388 2389 /* 2390 * Check for 802.2 encapsulation (EtherTalk phase 2?); 2391 * we check for the 802.2 protocol type in the 2392 * "Ethernet type" field. 2393 */ 2394 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); 2395 2396 /* 2397 * 802.2-encapsulated ETHERTYPE_ATALK packets are 2398 * SNAP packets with an organization code of 2399 * 0x080007 (Apple, for Appletalk) and a protocol 2400 * type of ETHERTYPE_ATALK (Appletalk). 2401 * 2402 * 802.2-encapsulated ETHERTYPE_AARP packets are 2403 * SNAP packets with an organization code of 2404 * 0x000000 (encapsulated Ethernet) and a protocol 2405 * type of ETHERTYPE_AARP (Appletalk ARP). 2406 */ 2407 if (ll_proto == ETHERTYPE_ATALK) 2408 b1 = gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); 2409 else /* ll_proto == ETHERTYPE_AARP */ 2410 b1 = gen_snap(cstate, 0x000000, ETHERTYPE_AARP); 2411 gen_and(b0, b1); 2412 2413 /* 2414 * Check for Ethernet encapsulation (Ethertalk 2415 * phase 1?); we just check for the Ethernet 2416 * protocol type. 2417 */ 2418 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); 2419 2420 gen_or(b0, b1); 2421 return b1; 2422 2423 default: 2424 if (ll_proto <= ETHERMTU) { 2425 /* 2426 * This is an LLC SAP value, so the frames 2427 * that match would be 802.2 frames. 2428 * Check for the 802.2 protocol type 2429 * in the "Ethernet type" field, and 2430 * then check the DSAP. 2431 */ 2432 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, LINUX_SLL_P_802_2); 2433 b1 = gen_cmp(cstate, OR_LINKHDR, cstate->off_linkpl.constant_part, BPF_B, 2434 ll_proto); 2435 gen_and(b0, b1); 2436 return b1; 2437 } else { 2438 /* 2439 * This is an Ethernet type, so compare 2440 * the length/type field with it (if 2441 * the frame is an 802.2 frame, the length 2442 * field will be <= ETHERMTU, and, as 2443 * "ll_proto" is > ETHERMTU, this test 2444 * will fail and the frame won't match, 2445 * which is what we want). 2446 */ 2447 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); 2448 } 2449 } 2450 } 2451 2452 /* 2453 * Load a value relative to the beginning of the link-layer header after the 2454 * pflog header. 2455 */ 2456 static struct slist * 2457 gen_load_pflog_llprefixlen(compiler_state_t *cstate) 2458 { 2459 struct slist *s1, *s2; 2460 2461 /* 2462 * Generate code to load the length of the pflog header into 2463 * the register assigned to hold that length, if one has been 2464 * assigned. (If one hasn't been assigned, no code we've 2465 * generated uses that prefix, so we don't need to generate any 2466 * code to load it.) 2467 */ 2468 if (cstate->off_linkpl.reg != -1) { 2469 /* 2470 * The length is in the first byte of the header. 2471 */ 2472 s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); 2473 s1->s.k = 0; 2474 2475 /* 2476 * Round it up to a multiple of 4. 2477 * Add 3, and clear the lower 2 bits. 2478 */ 2479 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 2480 s2->s.k = 3; 2481 sappend(s1, s2); 2482 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); 2483 s2->s.k = 0xfffffffc; 2484 sappend(s1, s2); 2485 2486 /* 2487 * Now allocate a register to hold that value and store 2488 * it. 2489 */ 2490 s2 = new_stmt(cstate, BPF_ST); 2491 s2->s.k = cstate->off_linkpl.reg; 2492 sappend(s1, s2); 2493 2494 /* 2495 * Now move it into the X register. 2496 */ 2497 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); 2498 sappend(s1, s2); 2499 2500 return (s1); 2501 } else 2502 return (NULL); 2503 } 2504 2505 static struct slist * 2506 gen_load_prism_llprefixlen(compiler_state_t *cstate) 2507 { 2508 struct slist *s1, *s2; 2509 struct slist *sjeq_avs_cookie; 2510 struct slist *sjcommon; 2511 2512 /* 2513 * This code is not compatible with the optimizer, as 2514 * we are generating jmp instructions within a normal 2515 * slist of instructions 2516 */ 2517 cstate->no_optimize = 1; 2518 2519 /* 2520 * Generate code to load the length of the radio header into 2521 * the register assigned to hold that length, if one has been 2522 * assigned. (If one hasn't been assigned, no code we've 2523 * generated uses that prefix, so we don't need to generate any 2524 * code to load it.) 2525 * 2526 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes 2527 * or always use the AVS header rather than the Prism header. 2528 * We load a 4-byte big-endian value at the beginning of the 2529 * raw packet data, and see whether, when masked with 0xFFFFF000, 2530 * it's equal to 0x80211000. If so, that indicates that it's 2531 * an AVS header (the masked-out bits are the version number). 2532 * Otherwise, it's a Prism header. 2533 * 2534 * XXX - the Prism header is also, in theory, variable-length, 2535 * but no known software generates headers that aren't 144 2536 * bytes long. 2537 */ 2538 if (cstate->off_linkhdr.reg != -1) { 2539 /* 2540 * Load the cookie. 2541 */ 2542 s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); 2543 s1->s.k = 0; 2544 2545 /* 2546 * AND it with 0xFFFFF000. 2547 */ 2548 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); 2549 s2->s.k = 0xFFFFF000; 2550 sappend(s1, s2); 2551 2552 /* 2553 * Compare with 0x80211000. 2554 */ 2555 sjeq_avs_cookie = new_stmt(cstate, JMP(BPF_JEQ)); 2556 sjeq_avs_cookie->s.k = 0x80211000; 2557 sappend(s1, sjeq_avs_cookie); 2558 2559 /* 2560 * If it's AVS: 2561 * 2562 * The 4 bytes at an offset of 4 from the beginning of 2563 * the AVS header are the length of the AVS header. 2564 * That field is big-endian. 2565 */ 2566 s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); 2567 s2->s.k = 4; 2568 sappend(s1, s2); 2569 sjeq_avs_cookie->s.jt = s2; 2570 2571 /* 2572 * Now jump to the code to allocate a register 2573 * into which to save the header length and 2574 * store the length there. (The "jump always" 2575 * instruction needs to have the k field set; 2576 * it's added to the PC, so, as we're jumping 2577 * over a single instruction, it should be 1.) 2578 */ 2579 sjcommon = new_stmt(cstate, JMP(BPF_JA)); 2580 sjcommon->s.k = 1; 2581 sappend(s1, sjcommon); 2582 2583 /* 2584 * Now for the code that handles the Prism header. 2585 * Just load the length of the Prism header (144) 2586 * into the A register. Have the test for an AVS 2587 * header branch here if we don't have an AVS header. 2588 */ 2589 s2 = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM); 2590 s2->s.k = 144; 2591 sappend(s1, s2); 2592 sjeq_avs_cookie->s.jf = s2; 2593 2594 /* 2595 * Now allocate a register to hold that value and store 2596 * it. The code for the AVS header will jump here after 2597 * loading the length of the AVS header. 2598 */ 2599 s2 = new_stmt(cstate, BPF_ST); 2600 s2->s.k = cstate->off_linkhdr.reg; 2601 sappend(s1, s2); 2602 sjcommon->s.jf = s2; 2603 2604 /* 2605 * Now move it into the X register. 2606 */ 2607 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); 2608 sappend(s1, s2); 2609 2610 return (s1); 2611 } else 2612 return (NULL); 2613 } 2614 2615 static struct slist * 2616 gen_load_avs_llprefixlen(compiler_state_t *cstate) 2617 { 2618 struct slist *s1, *s2; 2619 2620 /* 2621 * Generate code to load the length of the AVS header into 2622 * the register assigned to hold that length, if one has been 2623 * assigned. (If one hasn't been assigned, no code we've 2624 * generated uses that prefix, so we don't need to generate any 2625 * code to load it.) 2626 */ 2627 if (cstate->off_linkhdr.reg != -1) { 2628 /* 2629 * The 4 bytes at an offset of 4 from the beginning of 2630 * the AVS header are the length of the AVS header. 2631 * That field is big-endian. 2632 */ 2633 s1 = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); 2634 s1->s.k = 4; 2635 2636 /* 2637 * Now allocate a register to hold that value and store 2638 * it. 2639 */ 2640 s2 = new_stmt(cstate, BPF_ST); 2641 s2->s.k = cstate->off_linkhdr.reg; 2642 sappend(s1, s2); 2643 2644 /* 2645 * Now move it into the X register. 2646 */ 2647 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); 2648 sappend(s1, s2); 2649 2650 return (s1); 2651 } else 2652 return (NULL); 2653 } 2654 2655 static struct slist * 2656 gen_load_radiotap_llprefixlen(compiler_state_t *cstate) 2657 { 2658 struct slist *s1, *s2; 2659 2660 /* 2661 * Generate code to load the length of the radiotap header into 2662 * the register assigned to hold that length, if one has been 2663 * assigned. (If one hasn't been assigned, no code we've 2664 * generated uses that prefix, so we don't need to generate any 2665 * code to load it.) 2666 */ 2667 if (cstate->off_linkhdr.reg != -1) { 2668 /* 2669 * The 2 bytes at offsets of 2 and 3 from the beginning 2670 * of the radiotap header are the length of the radiotap 2671 * header; unfortunately, it's little-endian, so we have 2672 * to load it a byte at a time and construct the value. 2673 */ 2674 2675 /* 2676 * Load the high-order byte, at an offset of 3, shift it 2677 * left a byte, and put the result in the X register. 2678 */ 2679 s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); 2680 s1->s.k = 3; 2681 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); 2682 sappend(s1, s2); 2683 s2->s.k = 8; 2684 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); 2685 sappend(s1, s2); 2686 2687 /* 2688 * Load the next byte, at an offset of 2, and OR the 2689 * value from the X register into it. 2690 */ 2691 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); 2692 sappend(s1, s2); 2693 s2->s.k = 2; 2694 s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X); 2695 sappend(s1, s2); 2696 2697 /* 2698 * Now allocate a register to hold that value and store 2699 * it. 2700 */ 2701 s2 = new_stmt(cstate, BPF_ST); 2702 s2->s.k = cstate->off_linkhdr.reg; 2703 sappend(s1, s2); 2704 2705 /* 2706 * Now move it into the X register. 2707 */ 2708 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); 2709 sappend(s1, s2); 2710 2711 return (s1); 2712 } else 2713 return (NULL); 2714 } 2715 2716 /* 2717 * At the moment we treat PPI as normal Radiotap encoded 2718 * packets. The difference is in the function that generates 2719 * the code at the beginning to compute the header length. 2720 * Since this code generator of PPI supports bare 802.11 2721 * encapsulation only (i.e. the encapsulated DLT should be 2722 * DLT_IEEE802_11) we generate code to check for this too; 2723 * that's done in finish_parse(). 2724 */ 2725 static struct slist * 2726 gen_load_ppi_llprefixlen(compiler_state_t *cstate) 2727 { 2728 struct slist *s1, *s2; 2729 2730 /* 2731 * Generate code to load the length of the radiotap header 2732 * into the register assigned to hold that length, if one has 2733 * been assigned. 2734 */ 2735 if (cstate->off_linkhdr.reg != -1) { 2736 /* 2737 * The 2 bytes at offsets of 2 and 3 from the beginning 2738 * of the radiotap header are the length of the radiotap 2739 * header; unfortunately, it's little-endian, so we have 2740 * to load it a byte at a time and construct the value. 2741 */ 2742 2743 /* 2744 * Load the high-order byte, at an offset of 3, shift it 2745 * left a byte, and put the result in the X register. 2746 */ 2747 s1 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); 2748 s1->s.k = 3; 2749 s2 = new_stmt(cstate, BPF_ALU|BPF_LSH|BPF_K); 2750 sappend(s1, s2); 2751 s2->s.k = 8; 2752 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); 2753 sappend(s1, s2); 2754 2755 /* 2756 * Load the next byte, at an offset of 2, and OR the 2757 * value from the X register into it. 2758 */ 2759 s2 = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); 2760 sappend(s1, s2); 2761 s2->s.k = 2; 2762 s2 = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_X); 2763 sappend(s1, s2); 2764 2765 /* 2766 * Now allocate a register to hold that value and store 2767 * it. 2768 */ 2769 s2 = new_stmt(cstate, BPF_ST); 2770 s2->s.k = cstate->off_linkhdr.reg; 2771 sappend(s1, s2); 2772 2773 /* 2774 * Now move it into the X register. 2775 */ 2776 s2 = new_stmt(cstate, BPF_MISC|BPF_TAX); 2777 sappend(s1, s2); 2778 2779 return (s1); 2780 } else 2781 return (NULL); 2782 } 2783 2784 /* 2785 * Load a value relative to the beginning of the link-layer header after the 802.11 2786 * header, i.e. LLC_SNAP. 2787 * The link-layer header doesn't necessarily begin at the beginning 2788 * of the packet data; there might be a variable-length prefix containing 2789 * radio information. 2790 */ 2791 static struct slist * 2792 gen_load_802_11_header_len(compiler_state_t *cstate, struct slist *s, struct slist *snext) 2793 { 2794 struct slist *s2; 2795 struct slist *sjset_data_frame_1; 2796 struct slist *sjset_data_frame_2; 2797 struct slist *sjset_qos; 2798 struct slist *sjset_radiotap_flags_present; 2799 struct slist *sjset_radiotap_ext_present; 2800 struct slist *sjset_radiotap_tsft_present; 2801 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad; 2802 struct slist *s_roundup; 2803 2804 if (cstate->off_linkpl.reg == -1) { 2805 /* 2806 * No register has been assigned to the offset of 2807 * the link-layer payload, which means nobody needs 2808 * it; don't bother computing it - just return 2809 * what we already have. 2810 */ 2811 return (s); 2812 } 2813 2814 /* 2815 * This code is not compatible with the optimizer, as 2816 * we are generating jmp instructions within a normal 2817 * slist of instructions 2818 */ 2819 cstate->no_optimize = 1; 2820 2821 /* 2822 * If "s" is non-null, it has code to arrange that the X register 2823 * contains the length of the prefix preceding the link-layer 2824 * header. 2825 * 2826 * Otherwise, the length of the prefix preceding the link-layer 2827 * header is "off_outermostlinkhdr.constant_part". 2828 */ 2829 if (s == NULL) { 2830 /* 2831 * There is no variable-length header preceding the 2832 * link-layer header. 2833 * 2834 * Load the length of the fixed-length prefix preceding 2835 * the link-layer header (if any) into the X register, 2836 * and store it in the cstate->off_linkpl.reg register. 2837 * That length is off_outermostlinkhdr.constant_part. 2838 */ 2839 s = new_stmt(cstate, BPF_LDX|BPF_IMM); 2840 s->s.k = cstate->off_outermostlinkhdr.constant_part; 2841 } 2842 2843 /* 2844 * The X register contains the offset of the beginning of the 2845 * link-layer header; add 24, which is the minimum length 2846 * of the MAC header for a data frame, to that, and store it 2847 * in cstate->off_linkpl.reg, and then load the Frame Control field, 2848 * which is at the offset in the X register, with an indexed load. 2849 */ 2850 s2 = new_stmt(cstate, BPF_MISC|BPF_TXA); 2851 sappend(s, s2); 2852 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 2853 s2->s.k = 24; 2854 sappend(s, s2); 2855 s2 = new_stmt(cstate, BPF_ST); 2856 s2->s.k = cstate->off_linkpl.reg; 2857 sappend(s, s2); 2858 2859 s2 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); 2860 s2->s.k = 0; 2861 sappend(s, s2); 2862 2863 /* 2864 * Check the Frame Control field to see if this is a data frame; 2865 * a data frame has the 0x08 bit (b3) in that field set and the 2866 * 0x04 bit (b2) clear. 2867 */ 2868 sjset_data_frame_1 = new_stmt(cstate, JMP(BPF_JSET)); 2869 sjset_data_frame_1->s.k = 0x08; 2870 sappend(s, sjset_data_frame_1); 2871 2872 /* 2873 * If b3 is set, test b2, otherwise go to the first statement of 2874 * the rest of the program. 2875 */ 2876 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(cstate, JMP(BPF_JSET)); 2877 sjset_data_frame_2->s.k = 0x04; 2878 sappend(s, sjset_data_frame_2); 2879 sjset_data_frame_1->s.jf = snext; 2880 2881 /* 2882 * If b2 is not set, this is a data frame; test the QoS bit. 2883 * Otherwise, go to the first statement of the rest of the 2884 * program. 2885 */ 2886 sjset_data_frame_2->s.jt = snext; 2887 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(cstate, JMP(BPF_JSET)); 2888 sjset_qos->s.k = 0x80; /* QoS bit */ 2889 sappend(s, sjset_qos); 2890 2891 /* 2892 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS 2893 * field. 2894 * Otherwise, go to the first statement of the rest of the 2895 * program. 2896 */ 2897 sjset_qos->s.jt = s2 = new_stmt(cstate, BPF_LD|BPF_MEM); 2898 s2->s.k = cstate->off_linkpl.reg; 2899 sappend(s, s2); 2900 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); 2901 s2->s.k = 2; 2902 sappend(s, s2); 2903 s2 = new_stmt(cstate, BPF_ST); 2904 s2->s.k = cstate->off_linkpl.reg; 2905 sappend(s, s2); 2906 2907 /* 2908 * If we have a radiotap header, look at it to see whether 2909 * there's Atheros padding between the MAC-layer header 2910 * and the payload. 2911 * 2912 * Note: all of the fields in the radiotap header are 2913 * little-endian, so we byte-swap all of the values 2914 * we test against, as they will be loaded as big-endian 2915 * values. 2916 * 2917 * XXX - in the general case, we would have to scan through 2918 * *all* the presence bits, if there's more than one word of 2919 * presence bits. That would require a loop, meaning that 2920 * we wouldn't be able to run the filter in the kernel. 2921 * 2922 * We assume here that the Atheros adapters that insert the 2923 * annoying padding don't have multiple antennae and therefore 2924 * do not generate radiotap headers with multiple presence words. 2925 */ 2926 if (cstate->linktype == DLT_IEEE802_11_RADIO) { 2927 /* 2928 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set 2929 * in the first presence flag word? 2930 */ 2931 sjset_qos->s.jf = s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_W); 2932 s2->s.k = 4; 2933 sappend(s, s2); 2934 2935 sjset_radiotap_flags_present = new_stmt(cstate, JMP(BPF_JSET)); 2936 sjset_radiotap_flags_present->s.k = SWAPLONG(0x00000002); 2937 sappend(s, sjset_radiotap_flags_present); 2938 2939 /* 2940 * If not, skip all of this. 2941 */ 2942 sjset_radiotap_flags_present->s.jf = snext; 2943 2944 /* 2945 * Otherwise, is the "extension" bit set in that word? 2946 */ 2947 sjset_radiotap_ext_present = new_stmt(cstate, JMP(BPF_JSET)); 2948 sjset_radiotap_ext_present->s.k = SWAPLONG(0x80000000); 2949 sappend(s, sjset_radiotap_ext_present); 2950 sjset_radiotap_flags_present->s.jt = sjset_radiotap_ext_present; 2951 2952 /* 2953 * If so, skip all of this. 2954 */ 2955 sjset_radiotap_ext_present->s.jt = snext; 2956 2957 /* 2958 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set? 2959 */ 2960 sjset_radiotap_tsft_present = new_stmt(cstate, JMP(BPF_JSET)); 2961 sjset_radiotap_tsft_present->s.k = SWAPLONG(0x00000001); 2962 sappend(s, sjset_radiotap_tsft_present); 2963 sjset_radiotap_ext_present->s.jf = sjset_radiotap_tsft_present; 2964 2965 /* 2966 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is 2967 * at an offset of 16 from the beginning of the raw packet 2968 * data (8 bytes for the radiotap header and 8 bytes for 2969 * the TSFT field). 2970 * 2971 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) 2972 * is set. 2973 */ 2974 s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); 2975 s2->s.k = 16; 2976 sappend(s, s2); 2977 sjset_radiotap_tsft_present->s.jt = s2; 2978 2979 sjset_tsft_datapad = new_stmt(cstate, JMP(BPF_JSET)); 2980 sjset_tsft_datapad->s.k = 0x20; 2981 sappend(s, sjset_tsft_datapad); 2982 2983 /* 2984 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is 2985 * at an offset of 8 from the beginning of the raw packet 2986 * data (8 bytes for the radiotap header). 2987 * 2988 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20) 2989 * is set. 2990 */ 2991 s2 = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); 2992 s2->s.k = 8; 2993 sappend(s, s2); 2994 sjset_radiotap_tsft_present->s.jf = s2; 2995 2996 sjset_notsft_datapad = new_stmt(cstate, JMP(BPF_JSET)); 2997 sjset_notsft_datapad->s.k = 0x20; 2998 sappend(s, sjset_notsft_datapad); 2999 3000 /* 3001 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is 3002 * set, round the length of the 802.11 header to 3003 * a multiple of 4. Do that by adding 3 and then 3004 * dividing by and multiplying by 4, which we do by 3005 * ANDing with ~3. 3006 */ 3007 s_roundup = new_stmt(cstate, BPF_LD|BPF_MEM); 3008 s_roundup->s.k = cstate->off_linkpl.reg; 3009 sappend(s, s_roundup); 3010 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); 3011 s2->s.k = 3; 3012 sappend(s, s2); 3013 s2 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_IMM); 3014 s2->s.k = (bpf_u_int32)~3; 3015 sappend(s, s2); 3016 s2 = new_stmt(cstate, BPF_ST); 3017 s2->s.k = cstate->off_linkpl.reg; 3018 sappend(s, s2); 3019 3020 sjset_tsft_datapad->s.jt = s_roundup; 3021 sjset_tsft_datapad->s.jf = snext; 3022 sjset_notsft_datapad->s.jt = s_roundup; 3023 sjset_notsft_datapad->s.jf = snext; 3024 } else 3025 sjset_qos->s.jf = snext; 3026 3027 return s; 3028 } 3029 3030 static void 3031 insert_compute_vloffsets(compiler_state_t *cstate, struct block *b) 3032 { 3033 struct slist *s; 3034 3035 /* There is an implicit dependency between the link 3036 * payload and link header since the payload computation 3037 * includes the variable part of the header. Therefore, 3038 * if nobody else has allocated a register for the link 3039 * header and we need it, do it now. */ 3040 if (cstate->off_linkpl.reg != -1 && cstate->off_linkhdr.is_variable && 3041 cstate->off_linkhdr.reg == -1) 3042 cstate->off_linkhdr.reg = alloc_reg(cstate); 3043 3044 /* 3045 * For link-layer types that have a variable-length header 3046 * preceding the link-layer header, generate code to load 3047 * the offset of the link-layer header into the register 3048 * assigned to that offset, if any. 3049 * 3050 * XXX - this, and the next switch statement, won't handle 3051 * encapsulation of 802.11 or 802.11+radio information in 3052 * some other protocol stack. That's significantly more 3053 * complicated. 3054 */ 3055 switch (cstate->outermostlinktype) { 3056 3057 case DLT_PRISM_HEADER: 3058 s = gen_load_prism_llprefixlen(cstate); 3059 break; 3060 3061 case DLT_IEEE802_11_RADIO_AVS: 3062 s = gen_load_avs_llprefixlen(cstate); 3063 break; 3064 3065 case DLT_IEEE802_11_RADIO: 3066 s = gen_load_radiotap_llprefixlen(cstate); 3067 break; 3068 3069 case DLT_PPI: 3070 s = gen_load_ppi_llprefixlen(cstate); 3071 break; 3072 3073 default: 3074 s = NULL; 3075 break; 3076 } 3077 3078 /* 3079 * For link-layer types that have a variable-length link-layer 3080 * header, generate code to load the offset of the link-layer 3081 * payload into the register assigned to that offset, if any. 3082 */ 3083 switch (cstate->outermostlinktype) { 3084 3085 case DLT_IEEE802_11: 3086 case DLT_PRISM_HEADER: 3087 case DLT_IEEE802_11_RADIO_AVS: 3088 case DLT_IEEE802_11_RADIO: 3089 case DLT_PPI: 3090 s = gen_load_802_11_header_len(cstate, s, b->stmts); 3091 break; 3092 3093 case DLT_PFLOG: 3094 s = gen_load_pflog_llprefixlen(cstate); 3095 break; 3096 } 3097 3098 /* 3099 * If there is no initialization yet and we need variable 3100 * length offsets for VLAN, initialize them to zero 3101 */ 3102 if (s == NULL && cstate->is_vlan_vloffset) { 3103 struct slist *s2; 3104 3105 if (cstate->off_linkpl.reg == -1) 3106 cstate->off_linkpl.reg = alloc_reg(cstate); 3107 if (cstate->off_linktype.reg == -1) 3108 cstate->off_linktype.reg = alloc_reg(cstate); 3109 3110 s = new_stmt(cstate, BPF_LD|BPF_W|BPF_IMM); 3111 s->s.k = 0; 3112 s2 = new_stmt(cstate, BPF_ST); 3113 s2->s.k = cstate->off_linkpl.reg; 3114 sappend(s, s2); 3115 s2 = new_stmt(cstate, BPF_ST); 3116 s2->s.k = cstate->off_linktype.reg; 3117 sappend(s, s2); 3118 } 3119 3120 /* 3121 * If we have any offset-loading code, append all the 3122 * existing statements in the block to those statements, 3123 * and make the resulting list the list of statements 3124 * for the block. 3125 */ 3126 if (s != NULL) { 3127 sappend(s, b->stmts); 3128 b->stmts = s; 3129 } 3130 } 3131 3132 static struct block * 3133 gen_ppi_dlt_check(compiler_state_t *cstate) 3134 { 3135 struct slist *s_load_dlt; 3136 struct block *b; 3137 3138 if (cstate->linktype == DLT_PPI) 3139 { 3140 /* Create the statements that check for the DLT 3141 */ 3142 s_load_dlt = new_stmt(cstate, BPF_LD|BPF_W|BPF_ABS); 3143 s_load_dlt->s.k = 4; 3144 3145 b = new_block(cstate, JMP(BPF_JEQ)); 3146 3147 b->stmts = s_load_dlt; 3148 b->s.k = SWAPLONG(DLT_IEEE802_11); 3149 } 3150 else 3151 { 3152 b = NULL; 3153 } 3154 3155 return b; 3156 } 3157 3158 /* 3159 * Take an absolute offset, and: 3160 * 3161 * if it has no variable part, return NULL; 3162 * 3163 * if it has a variable part, generate code to load the register 3164 * containing that variable part into the X register, returning 3165 * a pointer to that code - if no register for that offset has 3166 * been allocated, allocate it first. 3167 * 3168 * (The code to set that register will be generated later, but will 3169 * be placed earlier in the code sequence.) 3170 */ 3171 static struct slist * 3172 gen_abs_offset_varpart(compiler_state_t *cstate, bpf_abs_offset *off) 3173 { 3174 struct slist *s; 3175 3176 if (off->is_variable) { 3177 if (off->reg == -1) { 3178 /* 3179 * We haven't yet assigned a register for the 3180 * variable part of the offset of the link-layer 3181 * header; allocate one. 3182 */ 3183 off->reg = alloc_reg(cstate); 3184 } 3185 3186 /* 3187 * Load the register containing the variable part of the 3188 * offset of the link-layer header into the X register. 3189 */ 3190 s = new_stmt(cstate, BPF_LDX|BPF_MEM); 3191 s->s.k = off->reg; 3192 return s; 3193 } else { 3194 /* 3195 * That offset isn't variable, there's no variable part, 3196 * so we don't need to generate any code. 3197 */ 3198 return NULL; 3199 } 3200 } 3201 3202 /* 3203 * Map an Ethernet type to the equivalent PPP type. 3204 */ 3205 static bpf_u_int32 3206 ethertype_to_ppptype(bpf_u_int32 ll_proto) 3207 { 3208 switch (ll_proto) { 3209 3210 case ETHERTYPE_IP: 3211 ll_proto = PPP_IP; 3212 break; 3213 3214 case ETHERTYPE_IPV6: 3215 ll_proto = PPP_IPV6; 3216 break; 3217 3218 case ETHERTYPE_DN: 3219 ll_proto = PPP_DECNET; 3220 break; 3221 3222 case ETHERTYPE_ATALK: 3223 ll_proto = PPP_APPLE; 3224 break; 3225 3226 case ETHERTYPE_NS: 3227 ll_proto = PPP_NS; 3228 break; 3229 3230 case LLCSAP_ISONS: 3231 ll_proto = PPP_OSI; 3232 break; 3233 3234 case LLCSAP_8021D: 3235 /* 3236 * I'm assuming the "Bridging PDU"s that go 3237 * over PPP are Spanning Tree Protocol 3238 * Bridging PDUs. 3239 */ 3240 ll_proto = PPP_BRPDU; 3241 break; 3242 3243 case LLCSAP_IPX: 3244 ll_proto = PPP_IPX; 3245 break; 3246 } 3247 return (ll_proto); 3248 } 3249 3250 /* 3251 * Generate any tests that, for encapsulation of a link-layer packet 3252 * inside another protocol stack, need to be done to check for those 3253 * link-layer packets (and that haven't already been done by a check 3254 * for that encapsulation). 3255 */ 3256 static struct block * 3257 gen_prevlinkhdr_check(compiler_state_t *cstate) 3258 { 3259 struct block *b0; 3260 3261 if (cstate->is_geneve) 3262 return gen_geneve_ll_check(cstate); 3263 3264 switch (cstate->prevlinktype) { 3265 3266 case DLT_SUNATM: 3267 /* 3268 * This is LANE-encapsulated Ethernet; check that the LANE 3269 * packet doesn't begin with an LE Control marker, i.e. 3270 * that it's data, not a control message. 3271 * 3272 * (We've already generated a test for LANE.) 3273 */ 3274 b0 = gen_cmp(cstate, OR_PREVLINKHDR, SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00); 3275 gen_not(b0); 3276 return b0; 3277 3278 default: 3279 /* 3280 * No such tests are necessary. 3281 */ 3282 return NULL; 3283 } 3284 /*NOTREACHED*/ 3285 } 3286 3287 /* 3288 * The three different values we should check for when checking for an 3289 * IPv6 packet with DLT_NULL. 3290 */ 3291 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */ 3292 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */ 3293 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */ 3294 3295 /* 3296 * Generate code to match a particular packet type by matching the 3297 * link-layer type field or fields in the 802.2 LLC header. 3298 * 3299 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP 3300 * value, if <= ETHERMTU. 3301 */ 3302 static struct block * 3303 gen_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) 3304 { 3305 struct block *b0, *b1, *b2; 3306 const char *description; 3307 3308 /* are we checking MPLS-encapsulated packets? */ 3309 if (cstate->label_stack_depth > 0) 3310 return gen_mpls_linktype(cstate, ll_proto); 3311 3312 switch (cstate->linktype) { 3313 3314 case DLT_EN10MB: 3315 case DLT_NETANALYZER: 3316 case DLT_NETANALYZER_TRANSPARENT: 3317 /* Geneve has an EtherType regardless of whether there is an 3318 * L2 header. */ 3319 if (!cstate->is_geneve) 3320 b0 = gen_prevlinkhdr_check(cstate); 3321 else 3322 b0 = NULL; 3323 3324 b1 = gen_ether_linktype(cstate, ll_proto); 3325 if (b0 != NULL) 3326 gen_and(b0, b1); 3327 return b1; 3328 /*NOTREACHED*/ 3329 3330 case DLT_C_HDLC: 3331 case DLT_HDLC: 3332 switch (ll_proto) { 3333 3334 case LLCSAP_ISONS: 3335 ll_proto = (ll_proto << 8 | LLCSAP_ISONS); 3336 /* fall through */ 3337 3338 default: 3339 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); 3340 /*NOTREACHED*/ 3341 } 3342 3343 case DLT_IEEE802_11: 3344 case DLT_PRISM_HEADER: 3345 case DLT_IEEE802_11_RADIO_AVS: 3346 case DLT_IEEE802_11_RADIO: 3347 case DLT_PPI: 3348 /* 3349 * Check that we have a data frame. 3350 */ 3351 b0 = gen_check_802_11_data_frame(cstate); 3352 3353 /* 3354 * Now check for the specified link-layer type. 3355 */ 3356 b1 = gen_llc_linktype(cstate, ll_proto); 3357 gen_and(b0, b1); 3358 return b1; 3359 /*NOTREACHED*/ 3360 3361 case DLT_FDDI: 3362 /* 3363 * XXX - check for LLC frames. 3364 */ 3365 return gen_llc_linktype(cstate, ll_proto); 3366 /*NOTREACHED*/ 3367 3368 case DLT_IEEE802: 3369 /* 3370 * XXX - check for LLC PDUs, as per IEEE 802.5. 3371 */ 3372 return gen_llc_linktype(cstate, ll_proto); 3373 /*NOTREACHED*/ 3374 3375 case DLT_ATM_RFC1483: 3376 case DLT_ATM_CLIP: 3377 case DLT_IP_OVER_FC: 3378 return gen_llc_linktype(cstate, ll_proto); 3379 /*NOTREACHED*/ 3380 3381 case DLT_SUNATM: 3382 /* 3383 * Check for an LLC-encapsulated version of this protocol; 3384 * if we were checking for LANE, linktype would no longer 3385 * be DLT_SUNATM. 3386 * 3387 * Check for LLC encapsulation and then check the protocol. 3388 */ 3389 b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0); 3390 b1 = gen_llc_linktype(cstate, ll_proto); 3391 gen_and(b0, b1); 3392 return b1; 3393 /*NOTREACHED*/ 3394 3395 case DLT_LINUX_SLL: 3396 return gen_linux_sll_linktype(cstate, ll_proto); 3397 /*NOTREACHED*/ 3398 3399 case DLT_SLIP: 3400 case DLT_SLIP_BSDOS: 3401 case DLT_RAW: 3402 /* 3403 * These types don't provide any type field; packets 3404 * are always IPv4 or IPv6. 3405 * 3406 * XXX - for IPv4, check for a version number of 4, and, 3407 * for IPv6, check for a version number of 6? 3408 */ 3409 switch (ll_proto) { 3410 3411 case ETHERTYPE_IP: 3412 /* Check for a version number of 4. */ 3413 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x40, 0xF0); 3414 3415 case ETHERTYPE_IPV6: 3416 /* Check for a version number of 6. */ 3417 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, 0x60, 0xF0); 3418 3419 default: 3420 return gen_false(cstate); /* always false */ 3421 } 3422 /*NOTREACHED*/ 3423 3424 case DLT_IPV4: 3425 /* 3426 * Raw IPv4, so no type field. 3427 */ 3428 if (ll_proto == ETHERTYPE_IP) 3429 return gen_true(cstate); /* always true */ 3430 3431 /* Checking for something other than IPv4; always false */ 3432 return gen_false(cstate); 3433 /*NOTREACHED*/ 3434 3435 case DLT_IPV6: 3436 /* 3437 * Raw IPv6, so no type field. 3438 */ 3439 if (ll_proto == ETHERTYPE_IPV6) 3440 return gen_true(cstate); /* always true */ 3441 3442 /* Checking for something other than IPv6; always false */ 3443 return gen_false(cstate); 3444 /*NOTREACHED*/ 3445 3446 case DLT_PPP: 3447 case DLT_PPP_PPPD: 3448 case DLT_PPP_SERIAL: 3449 case DLT_PPP_ETHER: 3450 /* 3451 * We use Ethernet protocol types inside libpcap; 3452 * map them to the corresponding PPP protocol types. 3453 */ 3454 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, 3455 ethertype_to_ppptype(ll_proto)); 3456 /*NOTREACHED*/ 3457 3458 case DLT_PPP_BSDOS: 3459 /* 3460 * We use Ethernet protocol types inside libpcap; 3461 * map them to the corresponding PPP protocol types. 3462 */ 3463 switch (ll_proto) { 3464 3465 case ETHERTYPE_IP: 3466 /* 3467 * Also check for Van Jacobson-compressed IP. 3468 * XXX - do this for other forms of PPP? 3469 */ 3470 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_IP); 3471 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJC); 3472 gen_or(b0, b1); 3473 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, PPP_VJNC); 3474 gen_or(b1, b0); 3475 return b0; 3476 3477 default: 3478 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, 3479 ethertype_to_ppptype(ll_proto)); 3480 } 3481 /*NOTREACHED*/ 3482 3483 case DLT_NULL: 3484 case DLT_LOOP: 3485 case DLT_ENC: 3486 switch (ll_proto) { 3487 3488 case ETHERTYPE_IP: 3489 return (gen_loopback_linktype(cstate, AF_INET)); 3490 3491 case ETHERTYPE_IPV6: 3492 /* 3493 * AF_ values may, unfortunately, be platform- 3494 * dependent; AF_INET isn't, because everybody 3495 * used 4.2BSD's value, but AF_INET6 is, because 3496 * 4.2BSD didn't have a value for it (given that 3497 * IPv6 didn't exist back in the early 1980's), 3498 * and they all picked their own values. 3499 * 3500 * This means that, if we're reading from a 3501 * savefile, we need to check for all the 3502 * possible values. 3503 * 3504 * If we're doing a live capture, we only need 3505 * to check for this platform's value; however, 3506 * Npcap uses 24, which isn't Windows's AF_INET6 3507 * value. (Given the multiple different values, 3508 * programs that read pcap files shouldn't be 3509 * checking for their platform's AF_INET6 value 3510 * anyway, they should check for all of the 3511 * possible values. and they might as well do 3512 * that even for live captures.) 3513 */ 3514 if (cstate->bpf_pcap->rfile != NULL) { 3515 /* 3516 * Savefile - check for all three 3517 * possible IPv6 values. 3518 */ 3519 b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_BSD); 3520 b1 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_FREEBSD); 3521 gen_or(b0, b1); 3522 b0 = gen_loopback_linktype(cstate, BSD_AFNUM_INET6_DARWIN); 3523 gen_or(b0, b1); 3524 return (b1); 3525 } else { 3526 /* 3527 * Live capture, so we only need to 3528 * check for the value used on this 3529 * platform. 3530 */ 3531 #ifdef _WIN32 3532 /* 3533 * Npcap doesn't use Windows's AF_INET6, 3534 * as that collides with AF_IPX on 3535 * some BSDs (both have the value 23). 3536 * Instead, it uses 24. 3537 */ 3538 return (gen_loopback_linktype(cstate, 24)); 3539 #else /* _WIN32 */ 3540 #ifdef AF_INET6 3541 return (gen_loopback_linktype(cstate, AF_INET6)); 3542 #else /* AF_INET6 */ 3543 /* 3544 * I guess this platform doesn't support 3545 * IPv6, so we just reject all packets. 3546 */ 3547 return gen_false(cstate); 3548 #endif /* AF_INET6 */ 3549 #endif /* _WIN32 */ 3550 } 3551 3552 default: 3553 /* 3554 * Not a type on which we support filtering. 3555 * XXX - support those that have AF_ values 3556 * #defined on this platform, at least? 3557 */ 3558 return gen_false(cstate); 3559 } 3560 3561 case DLT_PFLOG: 3562 /* 3563 * af field is host byte order in contrast to the rest of 3564 * the packet. 3565 */ 3566 if (ll_proto == ETHERTYPE_IP) 3567 return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af), 3568 BPF_B, AF_INET)); 3569 else if (ll_proto == ETHERTYPE_IPV6) 3570 return (gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, af), 3571 BPF_B, AF_INET6)); 3572 else 3573 return gen_false(cstate); 3574 /*NOTREACHED*/ 3575 3576 case DLT_ARCNET: 3577 case DLT_ARCNET_LINUX: 3578 /* 3579 * XXX should we check for first fragment if the protocol 3580 * uses PHDS? 3581 */ 3582 switch (ll_proto) { 3583 3584 default: 3585 return gen_false(cstate); 3586 3587 case ETHERTYPE_IPV6: 3588 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, 3589 ARCTYPE_INET6)); 3590 3591 case ETHERTYPE_IP: 3592 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, 3593 ARCTYPE_IP); 3594 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, 3595 ARCTYPE_IP_OLD); 3596 gen_or(b0, b1); 3597 return (b1); 3598 3599 case ETHERTYPE_ARP: 3600 b0 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, 3601 ARCTYPE_ARP); 3602 b1 = gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, 3603 ARCTYPE_ARP_OLD); 3604 gen_or(b0, b1); 3605 return (b1); 3606 3607 case ETHERTYPE_REVARP: 3608 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, 3609 ARCTYPE_REVARP)); 3610 3611 case ETHERTYPE_ATALK: 3612 return (gen_cmp(cstate, OR_LINKTYPE, 0, BPF_B, 3613 ARCTYPE_ATALK)); 3614 } 3615 /*NOTREACHED*/ 3616 3617 case DLT_LTALK: 3618 switch (ll_proto) { 3619 case ETHERTYPE_ATALK: 3620 return gen_true(cstate); 3621 default: 3622 return gen_false(cstate); 3623 } 3624 /*NOTREACHED*/ 3625 3626 case DLT_FRELAY: 3627 /* 3628 * XXX - assumes a 2-byte Frame Relay header with 3629 * DLCI and flags. What if the address is longer? 3630 */ 3631 switch (ll_proto) { 3632 3633 case ETHERTYPE_IP: 3634 /* 3635 * Check for the special NLPID for IP. 3636 */ 3637 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0xcc); 3638 3639 case ETHERTYPE_IPV6: 3640 /* 3641 * Check for the special NLPID for IPv6. 3642 */ 3643 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | 0x8e); 3644 3645 case LLCSAP_ISONS: 3646 /* 3647 * Check for several OSI protocols. 3648 * 3649 * Frame Relay packets typically have an OSI 3650 * NLPID at the beginning; we check for each 3651 * of them. 3652 * 3653 * What we check for is the NLPID and a frame 3654 * control field of UI, i.e. 0x03 followed 3655 * by the NLPID. 3656 */ 3657 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO8473_CLNP); 3658 b1 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO9542_ESIS); 3659 b2 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | ISO10589_ISIS); 3660 gen_or(b1, b2); 3661 gen_or(b0, b2); 3662 return b2; 3663 3664 default: 3665 return gen_false(cstate); 3666 } 3667 /*NOTREACHED*/ 3668 3669 case DLT_MFR: 3670 bpf_error(cstate, "Multi-link Frame Relay link-layer type filtering not implemented"); 3671 3672 case DLT_JUNIPER_MFR: 3673 case DLT_JUNIPER_MLFR: 3674 case DLT_JUNIPER_MLPPP: 3675 case DLT_JUNIPER_ATM1: 3676 case DLT_JUNIPER_ATM2: 3677 case DLT_JUNIPER_PPPOE: 3678 case DLT_JUNIPER_PPPOE_ATM: 3679 case DLT_JUNIPER_GGSN: 3680 case DLT_JUNIPER_ES: 3681 case DLT_JUNIPER_MONITOR: 3682 case DLT_JUNIPER_SERVICES: 3683 case DLT_JUNIPER_ETHER: 3684 case DLT_JUNIPER_PPP: 3685 case DLT_JUNIPER_FRELAY: 3686 case DLT_JUNIPER_CHDLC: 3687 case DLT_JUNIPER_VP: 3688 case DLT_JUNIPER_ST: 3689 case DLT_JUNIPER_ISM: 3690 case DLT_JUNIPER_VS: 3691 case DLT_JUNIPER_SRX_E2E: 3692 case DLT_JUNIPER_FIBRECHANNEL: 3693 case DLT_JUNIPER_ATM_CEMIC: 3694 3695 /* just lets verify the magic number for now - 3696 * on ATM we may have up to 6 different encapsulations on the wire 3697 * and need a lot of heuristics to figure out that the payload 3698 * might be; 3699 * 3700 * FIXME encapsulation specific BPF_ filters 3701 */ 3702 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */ 3703 3704 case DLT_BACNET_MS_TP: 3705 return gen_mcmp(cstate, OR_LINKHDR, 0, BPF_W, 0x55FF0000, 0xffff0000); 3706 3707 case DLT_IPNET: 3708 return gen_ipnet_linktype(cstate, ll_proto); 3709 3710 case DLT_LINUX_IRDA: 3711 bpf_error(cstate, "IrDA link-layer type filtering not implemented"); 3712 3713 case DLT_DOCSIS: 3714 bpf_error(cstate, "DOCSIS link-layer type filtering not implemented"); 3715 3716 case DLT_MTP2: 3717 case DLT_MTP2_WITH_PHDR: 3718 bpf_error(cstate, "MTP2 link-layer type filtering not implemented"); 3719 3720 case DLT_ERF: 3721 bpf_error(cstate, "ERF link-layer type filtering not implemented"); 3722 3723 case DLT_PFSYNC: 3724 bpf_error(cstate, "PFSYNC link-layer type filtering not implemented"); 3725 3726 case DLT_LINUX_LAPD: 3727 bpf_error(cstate, "LAPD link-layer type filtering not implemented"); 3728 3729 case DLT_USB_FREEBSD: 3730 case DLT_USB_LINUX: 3731 case DLT_USB_LINUX_MMAPPED: 3732 case DLT_USBPCAP: 3733 bpf_error(cstate, "USB link-layer type filtering not implemented"); 3734 3735 case DLT_BLUETOOTH_HCI_H4: 3736 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR: 3737 bpf_error(cstate, "Bluetooth link-layer type filtering not implemented"); 3738 3739 case DLT_CAN20B: 3740 case DLT_CAN_SOCKETCAN: 3741 bpf_error(cstate, "CAN link-layer type filtering not implemented"); 3742 3743 case DLT_IEEE802_15_4: 3744 case DLT_IEEE802_15_4_LINUX: 3745 case DLT_IEEE802_15_4_NONASK_PHY: 3746 case DLT_IEEE802_15_4_NOFCS: 3747 case DLT_IEEE802_15_4_TAP: 3748 bpf_error(cstate, "IEEE 802.15.4 link-layer type filtering not implemented"); 3749 3750 case DLT_IEEE802_16_MAC_CPS_RADIO: 3751 bpf_error(cstate, "IEEE 802.16 link-layer type filtering not implemented"); 3752 3753 case DLT_SITA: 3754 bpf_error(cstate, "SITA link-layer type filtering not implemented"); 3755 3756 case DLT_RAIF1: 3757 bpf_error(cstate, "RAIF1 link-layer type filtering not implemented"); 3758 3759 case DLT_IPMB_KONTRON: 3760 case DLT_IPMB_LINUX: 3761 bpf_error(cstate, "IPMB link-layer type filtering not implemented"); 3762 3763 case DLT_AX25_KISS: 3764 bpf_error(cstate, "AX.25 link-layer type filtering not implemented"); 3765 3766 case DLT_NFLOG: 3767 /* Using the fixed-size NFLOG header it is possible to tell only 3768 * the address family of the packet, other meaningful data is 3769 * either missing or behind TLVs. 3770 */ 3771 bpf_error(cstate, "NFLOG link-layer type filtering not implemented"); 3772 3773 default: 3774 /* 3775 * Does this link-layer header type have a field 3776 * indicating the type of the next protocol? If 3777 * so, off_linktype.constant_part will be the offset of that 3778 * field in the packet; if not, it will be OFFSET_NOT_SET. 3779 */ 3780 if (cstate->off_linktype.constant_part != OFFSET_NOT_SET) { 3781 /* 3782 * Yes; assume it's an Ethernet type. (If 3783 * it's not, it needs to be handled specially 3784 * above.) 3785 */ 3786 return gen_cmp(cstate, OR_LINKTYPE, 0, BPF_H, ll_proto); 3787 /*NOTREACHED */ 3788 } else { 3789 /* 3790 * No; report an error. 3791 */ 3792 description = pcap_datalink_val_to_description_or_dlt(cstate->linktype); 3793 bpf_error(cstate, "%s link-layer type filtering not implemented", 3794 description); 3795 /*NOTREACHED */ 3796 } 3797 } 3798 } 3799 3800 /* 3801 * Check for an LLC SNAP packet with a given organization code and 3802 * protocol type; we check the entire contents of the 802.2 LLC and 3803 * snap headers, checking for DSAP and SSAP of SNAP and a control 3804 * field of 0x03 in the LLC header, and for the specified organization 3805 * code and protocol type in the SNAP header. 3806 */ 3807 static struct block * 3808 gen_snap(compiler_state_t *cstate, bpf_u_int32 orgcode, bpf_u_int32 ptype) 3809 { 3810 u_char snapblock[8]; 3811 3812 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */ 3813 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */ 3814 snapblock[2] = 0x03; /* control = UI */ 3815 snapblock[3] = (u_char)(orgcode >> 16); /* upper 8 bits of organization code */ 3816 snapblock[4] = (u_char)(orgcode >> 8); /* middle 8 bits of organization code */ 3817 snapblock[5] = (u_char)(orgcode >> 0); /* lower 8 bits of organization code */ 3818 snapblock[6] = (u_char)(ptype >> 8); /* upper 8 bits of protocol type */ 3819 snapblock[7] = (u_char)(ptype >> 0); /* lower 8 bits of protocol type */ 3820 return gen_bcmp(cstate, OR_LLC, 0, 8, snapblock); 3821 } 3822 3823 /* 3824 * Generate code to match frames with an LLC header. 3825 */ 3826 static struct block * 3827 gen_llc_internal(compiler_state_t *cstate) 3828 { 3829 struct block *b0, *b1; 3830 3831 switch (cstate->linktype) { 3832 3833 case DLT_EN10MB: 3834 /* 3835 * We check for an Ethernet type field less than 3836 * 1500, which means it's an 802.3 length field. 3837 */ 3838 b0 = gen_cmp_gt(cstate, OR_LINKTYPE, 0, BPF_H, ETHERMTU); 3839 gen_not(b0); 3840 3841 /* 3842 * Now check for the purported DSAP and SSAP not being 3843 * 0xFF, to rule out NetWare-over-802.3. 3844 */ 3845 b1 = gen_cmp(cstate, OR_LLC, 0, BPF_H, 0xFFFF); 3846 gen_not(b1); 3847 gen_and(b0, b1); 3848 return b1; 3849 3850 case DLT_SUNATM: 3851 /* 3852 * We check for LLC traffic. 3853 */ 3854 b0 = gen_atmtype_llc(cstate); 3855 return b0; 3856 3857 case DLT_IEEE802: /* Token Ring */ 3858 /* 3859 * XXX - check for LLC frames. 3860 */ 3861 return gen_true(cstate); 3862 3863 case DLT_FDDI: 3864 /* 3865 * XXX - check for LLC frames. 3866 */ 3867 return gen_true(cstate); 3868 3869 case DLT_ATM_RFC1483: 3870 /* 3871 * For LLC encapsulation, these are defined to have an 3872 * 802.2 LLC header. 3873 * 3874 * For VC encapsulation, they don't, but there's no 3875 * way to check for that; the protocol used on the VC 3876 * is negotiated out of band. 3877 */ 3878 return gen_true(cstate); 3879 3880 case DLT_IEEE802_11: 3881 case DLT_PRISM_HEADER: 3882 case DLT_IEEE802_11_RADIO: 3883 case DLT_IEEE802_11_RADIO_AVS: 3884 case DLT_PPI: 3885 /* 3886 * Check that we have a data frame. 3887 */ 3888 b0 = gen_check_802_11_data_frame(cstate); 3889 return b0; 3890 3891 default: 3892 bpf_error(cstate, "'llc' not supported for %s", 3893 pcap_datalink_val_to_description_or_dlt(cstate->linktype)); 3894 /*NOTREACHED*/ 3895 } 3896 } 3897 3898 struct block * 3899 gen_llc(compiler_state_t *cstate) 3900 { 3901 /* 3902 * Catch errors reported by us and routines below us, and return NULL 3903 * on an error. 3904 */ 3905 if (setjmp(cstate->top_ctx)) 3906 return (NULL); 3907 3908 return gen_llc_internal(cstate); 3909 } 3910 3911 struct block * 3912 gen_llc_i(compiler_state_t *cstate) 3913 { 3914 struct block *b0, *b1; 3915 struct slist *s; 3916 3917 /* 3918 * Catch errors reported by us and routines below us, and return NULL 3919 * on an error. 3920 */ 3921 if (setjmp(cstate->top_ctx)) 3922 return (NULL); 3923 3924 /* 3925 * Check whether this is an LLC frame. 3926 */ 3927 b0 = gen_llc_internal(cstate); 3928 3929 /* 3930 * Load the control byte and test the low-order bit; it must 3931 * be clear for I frames. 3932 */ 3933 s = gen_load_a(cstate, OR_LLC, 2, BPF_B); 3934 b1 = new_block(cstate, JMP(BPF_JSET)); 3935 b1->s.k = 0x01; 3936 b1->stmts = s; 3937 gen_not(b1); 3938 gen_and(b0, b1); 3939 return b1; 3940 } 3941 3942 struct block * 3943 gen_llc_s(compiler_state_t *cstate) 3944 { 3945 struct block *b0, *b1; 3946 3947 /* 3948 * Catch errors reported by us and routines below us, and return NULL 3949 * on an error. 3950 */ 3951 if (setjmp(cstate->top_ctx)) 3952 return (NULL); 3953 3954 /* 3955 * Check whether this is an LLC frame. 3956 */ 3957 b0 = gen_llc_internal(cstate); 3958 3959 /* 3960 * Now compare the low-order 2 bit of the control byte against 3961 * the appropriate value for S frames. 3962 */ 3963 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_S_FMT, 0x03); 3964 gen_and(b0, b1); 3965 return b1; 3966 } 3967 3968 struct block * 3969 gen_llc_u(compiler_state_t *cstate) 3970 { 3971 struct block *b0, *b1; 3972 3973 /* 3974 * Catch errors reported by us and routines below us, and return NULL 3975 * on an error. 3976 */ 3977 if (setjmp(cstate->top_ctx)) 3978 return (NULL); 3979 3980 /* 3981 * Check whether this is an LLC frame. 3982 */ 3983 b0 = gen_llc_internal(cstate); 3984 3985 /* 3986 * Now compare the low-order 2 bit of the control byte against 3987 * the appropriate value for U frames. 3988 */ 3989 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, LLC_U_FMT, 0x03); 3990 gen_and(b0, b1); 3991 return b1; 3992 } 3993 3994 struct block * 3995 gen_llc_s_subtype(compiler_state_t *cstate, bpf_u_int32 subtype) 3996 { 3997 struct block *b0, *b1; 3998 3999 /* 4000 * Catch errors reported by us and routines below us, and return NULL 4001 * on an error. 4002 */ 4003 if (setjmp(cstate->top_ctx)) 4004 return (NULL); 4005 4006 /* 4007 * Check whether this is an LLC frame. 4008 */ 4009 b0 = gen_llc_internal(cstate); 4010 4011 /* 4012 * Now check for an S frame with the appropriate type. 4013 */ 4014 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_S_CMD_MASK); 4015 gen_and(b0, b1); 4016 return b1; 4017 } 4018 4019 struct block * 4020 gen_llc_u_subtype(compiler_state_t *cstate, bpf_u_int32 subtype) 4021 { 4022 struct block *b0, *b1; 4023 4024 /* 4025 * Catch errors reported by us and routines below us, and return NULL 4026 * on an error. 4027 */ 4028 if (setjmp(cstate->top_ctx)) 4029 return (NULL); 4030 4031 /* 4032 * Check whether this is an LLC frame. 4033 */ 4034 b0 = gen_llc_internal(cstate); 4035 4036 /* 4037 * Now check for a U frame with the appropriate type. 4038 */ 4039 b1 = gen_mcmp(cstate, OR_LLC, 2, BPF_B, subtype, LLC_U_CMD_MASK); 4040 gen_and(b0, b1); 4041 return b1; 4042 } 4043 4044 /* 4045 * Generate code to match a particular packet type, for link-layer types 4046 * using 802.2 LLC headers. 4047 * 4048 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used 4049 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues. 4050 * 4051 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP 4052 * value, if <= ETHERMTU. We use that to determine whether to 4053 * match the DSAP or both DSAP and LSAP or to check the OUI and 4054 * protocol ID in a SNAP header. 4055 */ 4056 static struct block * 4057 gen_llc_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) 4058 { 4059 /* 4060 * XXX - handle token-ring variable-length header. 4061 */ 4062 switch (ll_proto) { 4063 4064 case LLCSAP_IP: 4065 case LLCSAP_ISONS: 4066 case LLCSAP_NETBEUI: 4067 /* 4068 * XXX - should we check both the DSAP and the 4069 * SSAP, like this, or should we check just the 4070 * DSAP, as we do for other SAP values? 4071 */ 4072 return gen_cmp(cstate, OR_LLC, 0, BPF_H, (bpf_u_int32) 4073 ((ll_proto << 8) | ll_proto)); 4074 4075 case LLCSAP_IPX: 4076 /* 4077 * XXX - are there ever SNAP frames for IPX on 4078 * non-Ethernet 802.x networks? 4079 */ 4080 return gen_cmp(cstate, OR_LLC, 0, BPF_B, LLCSAP_IPX); 4081 4082 case ETHERTYPE_ATALK: 4083 /* 4084 * 802.2-encapsulated ETHERTYPE_ATALK packets are 4085 * SNAP packets with an organization code of 4086 * 0x080007 (Apple, for Appletalk) and a protocol 4087 * type of ETHERTYPE_ATALK (Appletalk). 4088 * 4089 * XXX - check for an organization code of 4090 * encapsulated Ethernet as well? 4091 */ 4092 return gen_snap(cstate, 0x080007, ETHERTYPE_ATALK); 4093 4094 default: 4095 /* 4096 * XXX - we don't have to check for IPX 802.3 4097 * here, but should we check for the IPX Ethertype? 4098 */ 4099 if (ll_proto <= ETHERMTU) { 4100 /* 4101 * This is an LLC SAP value, so check 4102 * the DSAP. 4103 */ 4104 return gen_cmp(cstate, OR_LLC, 0, BPF_B, ll_proto); 4105 } else { 4106 /* 4107 * This is an Ethernet type; we assume that it's 4108 * unlikely that it'll appear in the right place 4109 * at random, and therefore check only the 4110 * location that would hold the Ethernet type 4111 * in a SNAP frame with an organization code of 4112 * 0x000000 (encapsulated Ethernet). 4113 * 4114 * XXX - if we were to check for the SNAP DSAP and 4115 * LSAP, as per XXX, and were also to check for an 4116 * organization code of 0x000000 (encapsulated 4117 * Ethernet), we'd do 4118 * 4119 * return gen_snap(cstate, 0x000000, ll_proto); 4120 * 4121 * here; for now, we don't, as per the above. 4122 * I don't know whether it's worth the extra CPU 4123 * time to do the right check or not. 4124 */ 4125 return gen_cmp(cstate, OR_LLC, 6, BPF_H, ll_proto); 4126 } 4127 } 4128 } 4129 4130 static struct block * 4131 gen_hostop(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask, 4132 int dir, bpf_u_int32 ll_proto, u_int src_off, u_int dst_off) 4133 { 4134 struct block *b0, *b1; 4135 u_int offset; 4136 4137 switch (dir) { 4138 4139 case Q_SRC: 4140 offset = src_off; 4141 break; 4142 4143 case Q_DST: 4144 offset = dst_off; 4145 break; 4146 4147 case Q_AND: 4148 b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off); 4149 b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off); 4150 gen_and(b0, b1); 4151 return b1; 4152 4153 case Q_DEFAULT: 4154 case Q_OR: 4155 b0 = gen_hostop(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off); 4156 b1 = gen_hostop(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off); 4157 gen_or(b0, b1); 4158 return b1; 4159 4160 case Q_ADDR1: 4161 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4162 /*NOTREACHED*/ 4163 4164 case Q_ADDR2: 4165 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4166 /*NOTREACHED*/ 4167 4168 case Q_ADDR3: 4169 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4170 /*NOTREACHED*/ 4171 4172 case Q_ADDR4: 4173 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4174 /*NOTREACHED*/ 4175 4176 case Q_RA: 4177 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses"); 4178 /*NOTREACHED*/ 4179 4180 case Q_TA: 4181 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses"); 4182 /*NOTREACHED*/ 4183 4184 default: 4185 abort(); 4186 /*NOTREACHED*/ 4187 } 4188 b0 = gen_linktype(cstate, ll_proto); 4189 b1 = gen_mcmp(cstate, OR_LINKPL, offset, BPF_W, addr, mask); 4190 gen_and(b0, b1); 4191 return b1; 4192 } 4193 4194 #ifdef INET6 4195 static struct block * 4196 gen_hostop6(compiler_state_t *cstate, struct in6_addr *addr, 4197 struct in6_addr *mask, int dir, bpf_u_int32 ll_proto, u_int src_off, 4198 u_int dst_off) 4199 { 4200 struct block *b0, *b1; 4201 u_int offset; 4202 /* 4203 * Code below needs to access four separate 32-bit parts of the 128-bit 4204 * IPv6 address and mask. In some OSes this is as simple as using the 4205 * s6_addr32 pseudo-member of struct in6_addr, which contains a union of 4206 * 8-, 16- and 32-bit arrays. In other OSes this is not the case, as 4207 * far as libpcap sees it. Hence copy the data before use to avoid 4208 * potential unaligned memory access and the associated compiler 4209 * warnings (whether genuine or not). 4210 */ 4211 bpf_u_int32 a[4], m[4]; 4212 4213 switch (dir) { 4214 4215 case Q_SRC: 4216 offset = src_off; 4217 break; 4218 4219 case Q_DST: 4220 offset = dst_off; 4221 break; 4222 4223 case Q_AND: 4224 b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off); 4225 b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off); 4226 gen_and(b0, b1); 4227 return b1; 4228 4229 case Q_DEFAULT: 4230 case Q_OR: 4231 b0 = gen_hostop6(cstate, addr, mask, Q_SRC, ll_proto, src_off, dst_off); 4232 b1 = gen_hostop6(cstate, addr, mask, Q_DST, ll_proto, src_off, dst_off); 4233 gen_or(b0, b1); 4234 return b1; 4235 4236 case Q_ADDR1: 4237 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4238 /*NOTREACHED*/ 4239 4240 case Q_ADDR2: 4241 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4242 /*NOTREACHED*/ 4243 4244 case Q_ADDR3: 4245 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4246 /*NOTREACHED*/ 4247 4248 case Q_ADDR4: 4249 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4250 /*NOTREACHED*/ 4251 4252 case Q_RA: 4253 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses"); 4254 /*NOTREACHED*/ 4255 4256 case Q_TA: 4257 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses"); 4258 /*NOTREACHED*/ 4259 4260 default: 4261 abort(); 4262 /*NOTREACHED*/ 4263 } 4264 /* this order is important */ 4265 memcpy(a, addr, sizeof(a)); 4266 memcpy(m, mask, sizeof(m)); 4267 b1 = gen_mcmp(cstate, OR_LINKPL, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3])); 4268 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2])); 4269 gen_and(b0, b1); 4270 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1])); 4271 gen_and(b0, b1); 4272 b0 = gen_mcmp(cstate, OR_LINKPL, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0])); 4273 gen_and(b0, b1); 4274 b0 = gen_linktype(cstate, ll_proto); 4275 gen_and(b0, b1); 4276 return b1; 4277 } 4278 #endif 4279 4280 static struct block * 4281 gen_ehostop(compiler_state_t *cstate, const u_char *eaddr, int dir) 4282 { 4283 register struct block *b0, *b1; 4284 4285 switch (dir) { 4286 case Q_SRC: 4287 return gen_bcmp(cstate, OR_LINKHDR, 6, 6, eaddr); 4288 4289 case Q_DST: 4290 return gen_bcmp(cstate, OR_LINKHDR, 0, 6, eaddr); 4291 4292 case Q_AND: 4293 b0 = gen_ehostop(cstate, eaddr, Q_SRC); 4294 b1 = gen_ehostop(cstate, eaddr, Q_DST); 4295 gen_and(b0, b1); 4296 return b1; 4297 4298 case Q_DEFAULT: 4299 case Q_OR: 4300 b0 = gen_ehostop(cstate, eaddr, Q_SRC); 4301 b1 = gen_ehostop(cstate, eaddr, Q_DST); 4302 gen_or(b0, b1); 4303 return b1; 4304 4305 case Q_ADDR1: 4306 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers"); 4307 /*NOTREACHED*/ 4308 4309 case Q_ADDR2: 4310 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers"); 4311 /*NOTREACHED*/ 4312 4313 case Q_ADDR3: 4314 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers"); 4315 /*NOTREACHED*/ 4316 4317 case Q_ADDR4: 4318 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers"); 4319 /*NOTREACHED*/ 4320 4321 case Q_RA: 4322 bpf_error(cstate, "'ra' is only supported on 802.11 with 802.11 headers"); 4323 /*NOTREACHED*/ 4324 4325 case Q_TA: 4326 bpf_error(cstate, "'ta' is only supported on 802.11 with 802.11 headers"); 4327 /*NOTREACHED*/ 4328 } 4329 abort(); 4330 /*NOTREACHED*/ 4331 } 4332 4333 /* 4334 * Like gen_ehostop, but for DLT_FDDI 4335 */ 4336 static struct block * 4337 gen_fhostop(compiler_state_t *cstate, const u_char *eaddr, int dir) 4338 { 4339 struct block *b0, *b1; 4340 4341 switch (dir) { 4342 case Q_SRC: 4343 return gen_bcmp(cstate, OR_LINKHDR, 6 + 1 + cstate->pcap_fddipad, 6, eaddr); 4344 4345 case Q_DST: 4346 return gen_bcmp(cstate, OR_LINKHDR, 0 + 1 + cstate->pcap_fddipad, 6, eaddr); 4347 4348 case Q_AND: 4349 b0 = gen_fhostop(cstate, eaddr, Q_SRC); 4350 b1 = gen_fhostop(cstate, eaddr, Q_DST); 4351 gen_and(b0, b1); 4352 return b1; 4353 4354 case Q_DEFAULT: 4355 case Q_OR: 4356 b0 = gen_fhostop(cstate, eaddr, Q_SRC); 4357 b1 = gen_fhostop(cstate, eaddr, Q_DST); 4358 gen_or(b0, b1); 4359 return b1; 4360 4361 case Q_ADDR1: 4362 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11"); 4363 /*NOTREACHED*/ 4364 4365 case Q_ADDR2: 4366 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11"); 4367 /*NOTREACHED*/ 4368 4369 case Q_ADDR3: 4370 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11"); 4371 /*NOTREACHED*/ 4372 4373 case Q_ADDR4: 4374 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11"); 4375 /*NOTREACHED*/ 4376 4377 case Q_RA: 4378 bpf_error(cstate, "'ra' is only supported on 802.11"); 4379 /*NOTREACHED*/ 4380 4381 case Q_TA: 4382 bpf_error(cstate, "'ta' is only supported on 802.11"); 4383 /*NOTREACHED*/ 4384 } 4385 abort(); 4386 /*NOTREACHED*/ 4387 } 4388 4389 /* 4390 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring) 4391 */ 4392 static struct block * 4393 gen_thostop(compiler_state_t *cstate, const u_char *eaddr, int dir) 4394 { 4395 register struct block *b0, *b1; 4396 4397 switch (dir) { 4398 case Q_SRC: 4399 return gen_bcmp(cstate, OR_LINKHDR, 8, 6, eaddr); 4400 4401 case Q_DST: 4402 return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr); 4403 4404 case Q_AND: 4405 b0 = gen_thostop(cstate, eaddr, Q_SRC); 4406 b1 = gen_thostop(cstate, eaddr, Q_DST); 4407 gen_and(b0, b1); 4408 return b1; 4409 4410 case Q_DEFAULT: 4411 case Q_OR: 4412 b0 = gen_thostop(cstate, eaddr, Q_SRC); 4413 b1 = gen_thostop(cstate, eaddr, Q_DST); 4414 gen_or(b0, b1); 4415 return b1; 4416 4417 case Q_ADDR1: 4418 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11"); 4419 /*NOTREACHED*/ 4420 4421 case Q_ADDR2: 4422 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11"); 4423 /*NOTREACHED*/ 4424 4425 case Q_ADDR3: 4426 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11"); 4427 /*NOTREACHED*/ 4428 4429 case Q_ADDR4: 4430 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11"); 4431 /*NOTREACHED*/ 4432 4433 case Q_RA: 4434 bpf_error(cstate, "'ra' is only supported on 802.11"); 4435 /*NOTREACHED*/ 4436 4437 case Q_TA: 4438 bpf_error(cstate, "'ta' is only supported on 802.11"); 4439 /*NOTREACHED*/ 4440 } 4441 abort(); 4442 /*NOTREACHED*/ 4443 } 4444 4445 /* 4446 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and 4447 * various 802.11 + radio headers. 4448 */ 4449 static struct block * 4450 gen_wlanhostop(compiler_state_t *cstate, const u_char *eaddr, int dir) 4451 { 4452 register struct block *b0, *b1, *b2; 4453 register struct slist *s; 4454 4455 #ifdef ENABLE_WLAN_FILTERING_PATCH 4456 /* 4457 * TODO GV 20070613 4458 * We need to disable the optimizer because the optimizer is buggy 4459 * and wipes out some LD instructions generated by the below 4460 * code to validate the Frame Control bits 4461 */ 4462 cstate->no_optimize = 1; 4463 #endif /* ENABLE_WLAN_FILTERING_PATCH */ 4464 4465 switch (dir) { 4466 case Q_SRC: 4467 /* 4468 * Oh, yuk. 4469 * 4470 * For control frames, there is no SA. 4471 * 4472 * For management frames, SA is at an 4473 * offset of 10 from the beginning of 4474 * the packet. 4475 * 4476 * For data frames, SA is at an offset 4477 * of 10 from the beginning of the packet 4478 * if From DS is clear, at an offset of 4479 * 16 from the beginning of the packet 4480 * if From DS is set and To DS is clear, 4481 * and an offset of 24 from the beginning 4482 * of the packet if From DS is set and To DS 4483 * is set. 4484 */ 4485 4486 /* 4487 * Generate the tests to be done for data frames 4488 * with From DS set. 4489 * 4490 * First, check for To DS set, i.e. check "link[1] & 0x01". 4491 */ 4492 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); 4493 b1 = new_block(cstate, JMP(BPF_JSET)); 4494 b1->s.k = 0x01; /* To DS */ 4495 b1->stmts = s; 4496 4497 /* 4498 * If To DS is set, the SA is at 24. 4499 */ 4500 b0 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr); 4501 gen_and(b1, b0); 4502 4503 /* 4504 * Now, check for To DS not set, i.e. check 4505 * "!(link[1] & 0x01)". 4506 */ 4507 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); 4508 b2 = new_block(cstate, JMP(BPF_JSET)); 4509 b2->s.k = 0x01; /* To DS */ 4510 b2->stmts = s; 4511 gen_not(b2); 4512 4513 /* 4514 * If To DS is not set, the SA is at 16. 4515 */ 4516 b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); 4517 gen_and(b2, b1); 4518 4519 /* 4520 * Now OR together the last two checks. That gives 4521 * the complete set of checks for data frames with 4522 * From DS set. 4523 */ 4524 gen_or(b1, b0); 4525 4526 /* 4527 * Now check for From DS being set, and AND that with 4528 * the ORed-together checks. 4529 */ 4530 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); 4531 b1 = new_block(cstate, JMP(BPF_JSET)); 4532 b1->s.k = 0x02; /* From DS */ 4533 b1->stmts = s; 4534 gen_and(b1, b0); 4535 4536 /* 4537 * Now check for data frames with From DS not set. 4538 */ 4539 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); 4540 b2 = new_block(cstate, JMP(BPF_JSET)); 4541 b2->s.k = 0x02; /* From DS */ 4542 b2->stmts = s; 4543 gen_not(b2); 4544 4545 /* 4546 * If From DS isn't set, the SA is at 10. 4547 */ 4548 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); 4549 gen_and(b2, b1); 4550 4551 /* 4552 * Now OR together the checks for data frames with 4553 * From DS not set and for data frames with From DS 4554 * set; that gives the checks done for data frames. 4555 */ 4556 gen_or(b1, b0); 4557 4558 /* 4559 * Now check for a data frame. 4560 * I.e, check "link[0] & 0x08". 4561 */ 4562 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 4563 b1 = new_block(cstate, JMP(BPF_JSET)); 4564 b1->s.k = 0x08; 4565 b1->stmts = s; 4566 4567 /* 4568 * AND that with the checks done for data frames. 4569 */ 4570 gen_and(b1, b0); 4571 4572 /* 4573 * If the high-order bit of the type value is 0, this 4574 * is a management frame. 4575 * I.e, check "!(link[0] & 0x08)". 4576 */ 4577 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 4578 b2 = new_block(cstate, JMP(BPF_JSET)); 4579 b2->s.k = 0x08; 4580 b2->stmts = s; 4581 gen_not(b2); 4582 4583 /* 4584 * For management frames, the SA is at 10. 4585 */ 4586 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); 4587 gen_and(b2, b1); 4588 4589 /* 4590 * OR that with the checks done for data frames. 4591 * That gives the checks done for management and 4592 * data frames. 4593 */ 4594 gen_or(b1, b0); 4595 4596 /* 4597 * If the low-order bit of the type value is 1, 4598 * this is either a control frame or a frame 4599 * with a reserved type, and thus not a 4600 * frame with an SA. 4601 * 4602 * I.e., check "!(link[0] & 0x04)". 4603 */ 4604 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 4605 b1 = new_block(cstate, JMP(BPF_JSET)); 4606 b1->s.k = 0x04; 4607 b1->stmts = s; 4608 gen_not(b1); 4609 4610 /* 4611 * AND that with the checks for data and management 4612 * frames. 4613 */ 4614 gen_and(b1, b0); 4615 return b0; 4616 4617 case Q_DST: 4618 /* 4619 * Oh, yuk. 4620 * 4621 * For control frames, there is no DA. 4622 * 4623 * For management frames, DA is at an 4624 * offset of 4 from the beginning of 4625 * the packet. 4626 * 4627 * For data frames, DA is at an offset 4628 * of 4 from the beginning of the packet 4629 * if To DS is clear and at an offset of 4630 * 16 from the beginning of the packet 4631 * if To DS is set. 4632 */ 4633 4634 /* 4635 * Generate the tests to be done for data frames. 4636 * 4637 * First, check for To DS set, i.e. "link[1] & 0x01". 4638 */ 4639 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); 4640 b1 = new_block(cstate, JMP(BPF_JSET)); 4641 b1->s.k = 0x01; /* To DS */ 4642 b1->stmts = s; 4643 4644 /* 4645 * If To DS is set, the DA is at 16. 4646 */ 4647 b0 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); 4648 gen_and(b1, b0); 4649 4650 /* 4651 * Now, check for To DS not set, i.e. check 4652 * "!(link[1] & 0x01)". 4653 */ 4654 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); 4655 b2 = new_block(cstate, JMP(BPF_JSET)); 4656 b2->s.k = 0x01; /* To DS */ 4657 b2->stmts = s; 4658 gen_not(b2); 4659 4660 /* 4661 * If To DS is not set, the DA is at 4. 4662 */ 4663 b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); 4664 gen_and(b2, b1); 4665 4666 /* 4667 * Now OR together the last two checks. That gives 4668 * the complete set of checks for data frames. 4669 */ 4670 gen_or(b1, b0); 4671 4672 /* 4673 * Now check for a data frame. 4674 * I.e, check "link[0] & 0x08". 4675 */ 4676 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 4677 b1 = new_block(cstate, JMP(BPF_JSET)); 4678 b1->s.k = 0x08; 4679 b1->stmts = s; 4680 4681 /* 4682 * AND that with the checks done for data frames. 4683 */ 4684 gen_and(b1, b0); 4685 4686 /* 4687 * If the high-order bit of the type value is 0, this 4688 * is a management frame. 4689 * I.e, check "!(link[0] & 0x08)". 4690 */ 4691 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 4692 b2 = new_block(cstate, JMP(BPF_JSET)); 4693 b2->s.k = 0x08; 4694 b2->stmts = s; 4695 gen_not(b2); 4696 4697 /* 4698 * For management frames, the DA is at 4. 4699 */ 4700 b1 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); 4701 gen_and(b2, b1); 4702 4703 /* 4704 * OR that with the checks done for data frames. 4705 * That gives the checks done for management and 4706 * data frames. 4707 */ 4708 gen_or(b1, b0); 4709 4710 /* 4711 * If the low-order bit of the type value is 1, 4712 * this is either a control frame or a frame 4713 * with a reserved type, and thus not a 4714 * frame with an SA. 4715 * 4716 * I.e., check "!(link[0] & 0x04)". 4717 */ 4718 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 4719 b1 = new_block(cstate, JMP(BPF_JSET)); 4720 b1->s.k = 0x04; 4721 b1->stmts = s; 4722 gen_not(b1); 4723 4724 /* 4725 * AND that with the checks for data and management 4726 * frames. 4727 */ 4728 gen_and(b1, b0); 4729 return b0; 4730 4731 case Q_AND: 4732 b0 = gen_wlanhostop(cstate, eaddr, Q_SRC); 4733 b1 = gen_wlanhostop(cstate, eaddr, Q_DST); 4734 gen_and(b0, b1); 4735 return b1; 4736 4737 case Q_DEFAULT: 4738 case Q_OR: 4739 b0 = gen_wlanhostop(cstate, eaddr, Q_SRC); 4740 b1 = gen_wlanhostop(cstate, eaddr, Q_DST); 4741 gen_or(b0, b1); 4742 return b1; 4743 4744 /* 4745 * XXX - add BSSID keyword? 4746 */ 4747 case Q_ADDR1: 4748 return (gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr)); 4749 4750 case Q_ADDR2: 4751 /* 4752 * Not present in CTS or ACK control frames. 4753 */ 4754 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, 4755 IEEE80211_FC0_TYPE_MASK); 4756 gen_not(b0); 4757 b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, 4758 IEEE80211_FC0_SUBTYPE_MASK); 4759 gen_not(b1); 4760 b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, 4761 IEEE80211_FC0_SUBTYPE_MASK); 4762 gen_not(b2); 4763 gen_and(b1, b2); 4764 gen_or(b0, b2); 4765 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); 4766 gen_and(b2, b1); 4767 return b1; 4768 4769 case Q_ADDR3: 4770 /* 4771 * Not present in control frames. 4772 */ 4773 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, 4774 IEEE80211_FC0_TYPE_MASK); 4775 gen_not(b0); 4776 b1 = gen_bcmp(cstate, OR_LINKHDR, 16, 6, eaddr); 4777 gen_and(b0, b1); 4778 return b1; 4779 4780 case Q_ADDR4: 4781 /* 4782 * Present only if the direction mask has both "From DS" 4783 * and "To DS" set. Neither control frames nor management 4784 * frames should have both of those set, so we don't 4785 * check the frame type. 4786 */ 4787 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, 4788 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK); 4789 b1 = gen_bcmp(cstate, OR_LINKHDR, 24, 6, eaddr); 4790 gen_and(b0, b1); 4791 return b1; 4792 4793 case Q_RA: 4794 /* 4795 * Not present in management frames; addr1 in other 4796 * frames. 4797 */ 4798 4799 /* 4800 * If the high-order bit of the type value is 0, this 4801 * is a management frame. 4802 * I.e, check "(link[0] & 0x08)". 4803 */ 4804 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 4805 b1 = new_block(cstate, JMP(BPF_JSET)); 4806 b1->s.k = 0x08; 4807 b1->stmts = s; 4808 4809 /* 4810 * Check addr1. 4811 */ 4812 b0 = gen_bcmp(cstate, OR_LINKHDR, 4, 6, eaddr); 4813 4814 /* 4815 * AND that with the check of addr1. 4816 */ 4817 gen_and(b1, b0); 4818 return (b0); 4819 4820 case Q_TA: 4821 /* 4822 * Not present in management frames; addr2, if present, 4823 * in other frames. 4824 */ 4825 4826 /* 4827 * Not present in CTS or ACK control frames. 4828 */ 4829 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_TYPE_CTL, 4830 IEEE80211_FC0_TYPE_MASK); 4831 gen_not(b0); 4832 b1 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS, 4833 IEEE80211_FC0_SUBTYPE_MASK); 4834 gen_not(b1); 4835 b2 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK, 4836 IEEE80211_FC0_SUBTYPE_MASK); 4837 gen_not(b2); 4838 gen_and(b1, b2); 4839 gen_or(b0, b2); 4840 4841 /* 4842 * If the high-order bit of the type value is 0, this 4843 * is a management frame. 4844 * I.e, check "(link[0] & 0x08)". 4845 */ 4846 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 4847 b1 = new_block(cstate, JMP(BPF_JSET)); 4848 b1->s.k = 0x08; 4849 b1->stmts = s; 4850 4851 /* 4852 * AND that with the check for frames other than 4853 * CTS and ACK frames. 4854 */ 4855 gen_and(b1, b2); 4856 4857 /* 4858 * Check addr2. 4859 */ 4860 b1 = gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); 4861 gen_and(b2, b1); 4862 return b1; 4863 } 4864 abort(); 4865 /*NOTREACHED*/ 4866 } 4867 4868 /* 4869 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel. 4870 * (We assume that the addresses are IEEE 48-bit MAC addresses, 4871 * as the RFC states.) 4872 */ 4873 static struct block * 4874 gen_ipfchostop(compiler_state_t *cstate, const u_char *eaddr, int dir) 4875 { 4876 register struct block *b0, *b1; 4877 4878 switch (dir) { 4879 case Q_SRC: 4880 return gen_bcmp(cstate, OR_LINKHDR, 10, 6, eaddr); 4881 4882 case Q_DST: 4883 return gen_bcmp(cstate, OR_LINKHDR, 2, 6, eaddr); 4884 4885 case Q_AND: 4886 b0 = gen_ipfchostop(cstate, eaddr, Q_SRC); 4887 b1 = gen_ipfchostop(cstate, eaddr, Q_DST); 4888 gen_and(b0, b1); 4889 return b1; 4890 4891 case Q_DEFAULT: 4892 case Q_OR: 4893 b0 = gen_ipfchostop(cstate, eaddr, Q_SRC); 4894 b1 = gen_ipfchostop(cstate, eaddr, Q_DST); 4895 gen_or(b0, b1); 4896 return b1; 4897 4898 case Q_ADDR1: 4899 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11"); 4900 /*NOTREACHED*/ 4901 4902 case Q_ADDR2: 4903 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11"); 4904 /*NOTREACHED*/ 4905 4906 case Q_ADDR3: 4907 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11"); 4908 /*NOTREACHED*/ 4909 4910 case Q_ADDR4: 4911 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11"); 4912 /*NOTREACHED*/ 4913 4914 case Q_RA: 4915 bpf_error(cstate, "'ra' is only supported on 802.11"); 4916 /*NOTREACHED*/ 4917 4918 case Q_TA: 4919 bpf_error(cstate, "'ta' is only supported on 802.11"); 4920 /*NOTREACHED*/ 4921 } 4922 abort(); 4923 /*NOTREACHED*/ 4924 } 4925 4926 /* 4927 * This is quite tricky because there may be pad bytes in front of the 4928 * DECNET header, and then there are two possible data packet formats that 4929 * carry both src and dst addresses, plus 5 packet types in a format that 4930 * carries only the src node, plus 2 types that use a different format and 4931 * also carry just the src node. 4932 * 4933 * Yuck. 4934 * 4935 * Instead of doing those all right, we just look for data packets with 4936 * 0 or 1 bytes of padding. If you want to look at other packets, that 4937 * will require a lot more hacking. 4938 * 4939 * To add support for filtering on DECNET "areas" (network numbers) 4940 * one would want to add a "mask" argument to this routine. That would 4941 * make the filter even more inefficient, although one could be clever 4942 * and not generate masking instructions if the mask is 0xFFFF. 4943 */ 4944 static struct block * 4945 gen_dnhostop(compiler_state_t *cstate, bpf_u_int32 addr, int dir) 4946 { 4947 struct block *b0, *b1, *b2, *tmp; 4948 u_int offset_lh; /* offset if long header is received */ 4949 u_int offset_sh; /* offset if short header is received */ 4950 4951 switch (dir) { 4952 4953 case Q_DST: 4954 offset_sh = 1; /* follows flags */ 4955 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */ 4956 break; 4957 4958 case Q_SRC: 4959 offset_sh = 3; /* follows flags, dstnode */ 4960 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */ 4961 break; 4962 4963 case Q_AND: 4964 /* Inefficient because we do our Calvinball dance twice */ 4965 b0 = gen_dnhostop(cstate, addr, Q_SRC); 4966 b1 = gen_dnhostop(cstate, addr, Q_DST); 4967 gen_and(b0, b1); 4968 return b1; 4969 4970 case Q_DEFAULT: 4971 case Q_OR: 4972 /* Inefficient because we do our Calvinball dance twice */ 4973 b0 = gen_dnhostop(cstate, addr, Q_SRC); 4974 b1 = gen_dnhostop(cstate, addr, Q_DST); 4975 gen_or(b0, b1); 4976 return b1; 4977 4978 case Q_ADDR1: 4979 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4980 /*NOTREACHED*/ 4981 4982 case Q_ADDR2: 4983 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4984 /*NOTREACHED*/ 4985 4986 case Q_ADDR3: 4987 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4988 /*NOTREACHED*/ 4989 4990 case Q_ADDR4: 4991 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses"); 4992 /*NOTREACHED*/ 4993 4994 case Q_RA: 4995 bpf_error(cstate, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses"); 4996 /*NOTREACHED*/ 4997 4998 case Q_TA: 4999 bpf_error(cstate, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses"); 5000 /*NOTREACHED*/ 5001 5002 default: 5003 abort(); 5004 /*NOTREACHED*/ 5005 } 5006 b0 = gen_linktype(cstate, ETHERTYPE_DN); 5007 /* Check for pad = 1, long header case */ 5008 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H, 5009 (bpf_u_int32)ntohs(0x0681), (bpf_u_int32)ntohs(0x07FF)); 5010 b1 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_lh, 5011 BPF_H, (bpf_u_int32)ntohs((u_short)addr)); 5012 gen_and(tmp, b1); 5013 /* Check for pad = 0, long header case */ 5014 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x06, 5015 (bpf_u_int32)0x7); 5016 b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_lh, BPF_H, 5017 (bpf_u_int32)ntohs((u_short)addr)); 5018 gen_and(tmp, b2); 5019 gen_or(b2, b1); 5020 /* Check for pad = 1, short header case */ 5021 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_H, 5022 (bpf_u_int32)ntohs(0x0281), (bpf_u_int32)ntohs(0x07FF)); 5023 b2 = gen_cmp(cstate, OR_LINKPL, 2 + 1 + offset_sh, BPF_H, 5024 (bpf_u_int32)ntohs((u_short)addr)); 5025 gen_and(tmp, b2); 5026 gen_or(b2, b1); 5027 /* Check for pad = 0, short header case */ 5028 tmp = gen_mcmp(cstate, OR_LINKPL, 2, BPF_B, (bpf_u_int32)0x02, 5029 (bpf_u_int32)0x7); 5030 b2 = gen_cmp(cstate, OR_LINKPL, 2 + offset_sh, BPF_H, 5031 (bpf_u_int32)ntohs((u_short)addr)); 5032 gen_and(tmp, b2); 5033 gen_or(b2, b1); 5034 5035 /* Combine with test for cstate->linktype */ 5036 gen_and(b0, b1); 5037 return b1; 5038 } 5039 5040 /* 5041 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets; 5042 * test the bottom-of-stack bit, and then check the version number 5043 * field in the IP header. 5044 */ 5045 static struct block * 5046 gen_mpls_linktype(compiler_state_t *cstate, bpf_u_int32 ll_proto) 5047 { 5048 struct block *b0, *b1; 5049 5050 switch (ll_proto) { 5051 5052 case ETHERTYPE_IP: 5053 /* match the bottom-of-stack bit */ 5054 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01); 5055 /* match the IPv4 version number */ 5056 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x40, 0xf0); 5057 gen_and(b0, b1); 5058 return b1; 5059 5060 case ETHERTYPE_IPV6: 5061 /* match the bottom-of-stack bit */ 5062 b0 = gen_mcmp(cstate, OR_LINKPL, (u_int)-2, BPF_B, 0x01, 0x01); 5063 /* match the IPv4 version number */ 5064 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_B, 0x60, 0xf0); 5065 gen_and(b0, b1); 5066 return b1; 5067 5068 default: 5069 /* FIXME add other L3 proto IDs */ 5070 bpf_error(cstate, "unsupported protocol over mpls"); 5071 /*NOTREACHED*/ 5072 } 5073 } 5074 5075 static struct block * 5076 gen_host(compiler_state_t *cstate, bpf_u_int32 addr, bpf_u_int32 mask, 5077 int proto, int dir, int type) 5078 { 5079 struct block *b0, *b1; 5080 const char *typestr; 5081 5082 if (type == Q_NET) 5083 typestr = "net"; 5084 else 5085 typestr = "host"; 5086 5087 switch (proto) { 5088 5089 case Q_DEFAULT: 5090 b0 = gen_host(cstate, addr, mask, Q_IP, dir, type); 5091 /* 5092 * Only check for non-IPv4 addresses if we're not 5093 * checking MPLS-encapsulated packets. 5094 */ 5095 if (cstate->label_stack_depth == 0) { 5096 b1 = gen_host(cstate, addr, mask, Q_ARP, dir, type); 5097 gen_or(b0, b1); 5098 b0 = gen_host(cstate, addr, mask, Q_RARP, dir, type); 5099 gen_or(b1, b0); 5100 } 5101 return b0; 5102 5103 case Q_LINK: 5104 bpf_error(cstate, "link-layer modifier applied to %s", typestr); 5105 5106 case Q_IP: 5107 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_IP, 12, 16); 5108 5109 case Q_RARP: 5110 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_REVARP, 14, 24); 5111 5112 case Q_ARP: 5113 return gen_hostop(cstate, addr, mask, dir, ETHERTYPE_ARP, 14, 24); 5114 5115 case Q_SCTP: 5116 bpf_error(cstate, "'sctp' modifier applied to %s", typestr); 5117 5118 case Q_TCP: 5119 bpf_error(cstate, "'tcp' modifier applied to %s", typestr); 5120 5121 case Q_UDP: 5122 bpf_error(cstate, "'udp' modifier applied to %s", typestr); 5123 5124 case Q_ICMP: 5125 bpf_error(cstate, "'icmp' modifier applied to %s", typestr); 5126 5127 case Q_IGMP: 5128 bpf_error(cstate, "'igmp' modifier applied to %s", typestr); 5129 5130 case Q_IGRP: 5131 bpf_error(cstate, "'igrp' modifier applied to %s", typestr); 5132 5133 case Q_ATALK: 5134 bpf_error(cstate, "AppleTalk host filtering not implemented"); 5135 5136 case Q_DECNET: 5137 return gen_dnhostop(cstate, addr, dir); 5138 5139 case Q_LAT: 5140 bpf_error(cstate, "LAT host filtering not implemented"); 5141 5142 case Q_SCA: 5143 bpf_error(cstate, "SCA host filtering not implemented"); 5144 5145 case Q_MOPRC: 5146 bpf_error(cstate, "MOPRC host filtering not implemented"); 5147 5148 case Q_MOPDL: 5149 bpf_error(cstate, "MOPDL host filtering not implemented"); 5150 5151 case Q_IPV6: 5152 bpf_error(cstate, "'ip6' modifier applied to ip host"); 5153 5154 case Q_ICMPV6: 5155 bpf_error(cstate, "'icmp6' modifier applied to %s", typestr); 5156 5157 case Q_AH: 5158 bpf_error(cstate, "'ah' modifier applied to %s", typestr); 5159 5160 case Q_ESP: 5161 bpf_error(cstate, "'esp' modifier applied to %s", typestr); 5162 5163 case Q_PIM: 5164 bpf_error(cstate, "'pim' modifier applied to %s", typestr); 5165 5166 case Q_VRRP: 5167 bpf_error(cstate, "'vrrp' modifier applied to %s", typestr); 5168 5169 case Q_AARP: 5170 bpf_error(cstate, "AARP host filtering not implemented"); 5171 5172 case Q_ISO: 5173 bpf_error(cstate, "ISO host filtering not implemented"); 5174 5175 case Q_ESIS: 5176 bpf_error(cstate, "'esis' modifier applied to %s", typestr); 5177 5178 case Q_ISIS: 5179 bpf_error(cstate, "'isis' modifier applied to %s", typestr); 5180 5181 case Q_CLNP: 5182 bpf_error(cstate, "'clnp' modifier applied to %s", typestr); 5183 5184 case Q_STP: 5185 bpf_error(cstate, "'stp' modifier applied to %s", typestr); 5186 5187 case Q_IPX: 5188 bpf_error(cstate, "IPX host filtering not implemented"); 5189 5190 case Q_NETBEUI: 5191 bpf_error(cstate, "'netbeui' modifier applied to %s", typestr); 5192 5193 case Q_ISIS_L1: 5194 bpf_error(cstate, "'l1' modifier applied to %s", typestr); 5195 5196 case Q_ISIS_L2: 5197 bpf_error(cstate, "'l2' modifier applied to %s", typestr); 5198 5199 case Q_ISIS_IIH: 5200 bpf_error(cstate, "'iih' modifier applied to %s", typestr); 5201 5202 case Q_ISIS_SNP: 5203 bpf_error(cstate, "'snp' modifier applied to %s", typestr); 5204 5205 case Q_ISIS_CSNP: 5206 bpf_error(cstate, "'csnp' modifier applied to %s", typestr); 5207 5208 case Q_ISIS_PSNP: 5209 bpf_error(cstate, "'psnp' modifier applied to %s", typestr); 5210 5211 case Q_ISIS_LSP: 5212 bpf_error(cstate, "'lsp' modifier applied to %s", typestr); 5213 5214 case Q_RADIO: 5215 bpf_error(cstate, "'radio' modifier applied to %s", typestr); 5216 5217 case Q_CARP: 5218 bpf_error(cstate, "'carp' modifier applied to %s", typestr); 5219 5220 default: 5221 abort(); 5222 } 5223 /*NOTREACHED*/ 5224 } 5225 5226 #ifdef INET6 5227 static struct block * 5228 gen_host6(compiler_state_t *cstate, struct in6_addr *addr, 5229 struct in6_addr *mask, int proto, int dir, int type) 5230 { 5231 const char *typestr; 5232 5233 if (type == Q_NET) 5234 typestr = "net"; 5235 else 5236 typestr = "host"; 5237 5238 switch (proto) { 5239 5240 case Q_DEFAULT: 5241 return gen_host6(cstate, addr, mask, Q_IPV6, dir, type); 5242 5243 case Q_LINK: 5244 bpf_error(cstate, "link-layer modifier applied to ip6 %s", typestr); 5245 5246 case Q_IP: 5247 bpf_error(cstate, "'ip' modifier applied to ip6 %s", typestr); 5248 5249 case Q_RARP: 5250 bpf_error(cstate, "'rarp' modifier applied to ip6 %s", typestr); 5251 5252 case Q_ARP: 5253 bpf_error(cstate, "'arp' modifier applied to ip6 %s", typestr); 5254 5255 case Q_SCTP: 5256 bpf_error(cstate, "'sctp' modifier applied to ip6 %s", typestr); 5257 5258 case Q_TCP: 5259 bpf_error(cstate, "'tcp' modifier applied to ip6 %s", typestr); 5260 5261 case Q_UDP: 5262 bpf_error(cstate, "'udp' modifier applied to ip6 %s", typestr); 5263 5264 case Q_ICMP: 5265 bpf_error(cstate, "'icmp' modifier applied to ip6 %s", typestr); 5266 5267 case Q_IGMP: 5268 bpf_error(cstate, "'igmp' modifier applied to ip6 %s", typestr); 5269 5270 case Q_IGRP: 5271 bpf_error(cstate, "'igrp' modifier applied to ip6 %s", typestr); 5272 5273 case Q_ATALK: 5274 bpf_error(cstate, "AppleTalk modifier applied to ip6 %s", typestr); 5275 5276 case Q_DECNET: 5277 bpf_error(cstate, "'decnet' modifier applied to ip6 %s", typestr); 5278 5279 case Q_LAT: 5280 bpf_error(cstate, "'lat' modifier applied to ip6 %s", typestr); 5281 5282 case Q_SCA: 5283 bpf_error(cstate, "'sca' modifier applied to ip6 %s", typestr); 5284 5285 case Q_MOPRC: 5286 bpf_error(cstate, "'moprc' modifier applied to ip6 %s", typestr); 5287 5288 case Q_MOPDL: 5289 bpf_error(cstate, "'mopdl' modifier applied to ip6 %s", typestr); 5290 5291 case Q_IPV6: 5292 return gen_hostop6(cstate, addr, mask, dir, ETHERTYPE_IPV6, 8, 24); 5293 5294 case Q_ICMPV6: 5295 bpf_error(cstate, "'icmp6' modifier applied to ip6 %s", typestr); 5296 5297 case Q_AH: 5298 bpf_error(cstate, "'ah' modifier applied to ip6 %s", typestr); 5299 5300 case Q_ESP: 5301 bpf_error(cstate, "'esp' modifier applied to ip6 %s", typestr); 5302 5303 case Q_PIM: 5304 bpf_error(cstate, "'pim' modifier applied to ip6 %s", typestr); 5305 5306 case Q_VRRP: 5307 bpf_error(cstate, "'vrrp' modifier applied to ip6 %s", typestr); 5308 5309 case Q_AARP: 5310 bpf_error(cstate, "'aarp' modifier applied to ip6 %s", typestr); 5311 5312 case Q_ISO: 5313 bpf_error(cstate, "'iso' modifier applied to ip6 %s", typestr); 5314 5315 case Q_ESIS: 5316 bpf_error(cstate, "'esis' modifier applied to ip6 %s", typestr); 5317 5318 case Q_ISIS: 5319 bpf_error(cstate, "'isis' modifier applied to ip6 %s", typestr); 5320 5321 case Q_CLNP: 5322 bpf_error(cstate, "'clnp' modifier applied to ip6 %s", typestr); 5323 5324 case Q_STP: 5325 bpf_error(cstate, "'stp' modifier applied to ip6 %s", typestr); 5326 5327 case Q_IPX: 5328 bpf_error(cstate, "'ipx' modifier applied to ip6 %s", typestr); 5329 5330 case Q_NETBEUI: 5331 bpf_error(cstate, "'netbeui' modifier applied to ip6 %s", typestr); 5332 5333 case Q_ISIS_L1: 5334 bpf_error(cstate, "'l1' modifier applied to ip6 %s", typestr); 5335 5336 case Q_ISIS_L2: 5337 bpf_error(cstate, "'l2' modifier applied to ip6 %s", typestr); 5338 5339 case Q_ISIS_IIH: 5340 bpf_error(cstate, "'iih' modifier applied to ip6 %s", typestr); 5341 5342 case Q_ISIS_SNP: 5343 bpf_error(cstate, "'snp' modifier applied to ip6 %s", typestr); 5344 5345 case Q_ISIS_CSNP: 5346 bpf_error(cstate, "'csnp' modifier applied to ip6 %s", typestr); 5347 5348 case Q_ISIS_PSNP: 5349 bpf_error(cstate, "'psnp' modifier applied to ip6 %s", typestr); 5350 5351 case Q_ISIS_LSP: 5352 bpf_error(cstate, "'lsp' modifier applied to ip6 %s", typestr); 5353 5354 case Q_RADIO: 5355 bpf_error(cstate, "'radio' modifier applied to ip6 %s", typestr); 5356 5357 case Q_CARP: 5358 bpf_error(cstate, "'carp' modifier applied to ip6 %s", typestr); 5359 5360 default: 5361 abort(); 5362 } 5363 /*NOTREACHED*/ 5364 } 5365 #endif 5366 5367 #ifndef INET6 5368 static struct block * 5369 gen_gateway(compiler_state_t *cstate, const u_char *eaddr, 5370 struct addrinfo *alist, int proto, int dir) 5371 { 5372 struct block *b0, *b1, *tmp; 5373 struct addrinfo *ai; 5374 struct sockaddr_in *sin; 5375 5376 if (dir != 0) 5377 bpf_error(cstate, "direction applied to 'gateway'"); 5378 5379 switch (proto) { 5380 case Q_DEFAULT: 5381 case Q_IP: 5382 case Q_ARP: 5383 case Q_RARP: 5384 switch (cstate->linktype) { 5385 case DLT_EN10MB: 5386 case DLT_NETANALYZER: 5387 case DLT_NETANALYZER_TRANSPARENT: 5388 b1 = gen_prevlinkhdr_check(cstate); 5389 b0 = gen_ehostop(cstate, eaddr, Q_OR); 5390 if (b1 != NULL) 5391 gen_and(b1, b0); 5392 break; 5393 case DLT_FDDI: 5394 b0 = gen_fhostop(cstate, eaddr, Q_OR); 5395 break; 5396 case DLT_IEEE802: 5397 b0 = gen_thostop(cstate, eaddr, Q_OR); 5398 break; 5399 case DLT_IEEE802_11: 5400 case DLT_PRISM_HEADER: 5401 case DLT_IEEE802_11_RADIO_AVS: 5402 case DLT_IEEE802_11_RADIO: 5403 case DLT_PPI: 5404 b0 = gen_wlanhostop(cstate, eaddr, Q_OR); 5405 break; 5406 case DLT_SUNATM: 5407 /* 5408 * This is LLC-multiplexed traffic; if it were 5409 * LANE, cstate->linktype would have been set to 5410 * DLT_EN10MB. 5411 */ 5412 bpf_error(cstate, 5413 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); 5414 case DLT_IP_OVER_FC: 5415 b0 = gen_ipfchostop(cstate, eaddr, Q_OR); 5416 break; 5417 default: 5418 bpf_error(cstate, 5419 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); 5420 } 5421 b1 = NULL; 5422 for (ai = alist; ai != NULL; ai = ai->ai_next) { 5423 /* 5424 * Does it have an address? 5425 */ 5426 if (ai->ai_addr != NULL) { 5427 /* 5428 * Yes. Is it an IPv4 address? 5429 */ 5430 if (ai->ai_addr->sa_family == AF_INET) { 5431 /* 5432 * Generate an entry for it. 5433 */ 5434 sin = (struct sockaddr_in *)ai->ai_addr; 5435 tmp = gen_host(cstate, 5436 ntohl(sin->sin_addr.s_addr), 5437 0xffffffff, proto, Q_OR, Q_HOST); 5438 /* 5439 * Is it the *first* IPv4 address? 5440 */ 5441 if (b1 == NULL) { 5442 /* 5443 * Yes, so start with it. 5444 */ 5445 b1 = tmp; 5446 } else { 5447 /* 5448 * No, so OR it into the 5449 * existing set of 5450 * addresses. 5451 */ 5452 gen_or(b1, tmp); 5453 b1 = tmp; 5454 } 5455 } 5456 } 5457 } 5458 if (b1 == NULL) { 5459 /* 5460 * No IPv4 addresses found. 5461 */ 5462 return (NULL); 5463 } 5464 gen_not(b1); 5465 gen_and(b0, b1); 5466 return b1; 5467 } 5468 bpf_error(cstate, "illegal modifier of 'gateway'"); 5469 /*NOTREACHED*/ 5470 } 5471 #endif 5472 5473 static struct block * 5474 gen_proto_abbrev_internal(compiler_state_t *cstate, int proto) 5475 { 5476 struct block *b0; 5477 struct block *b1; 5478 5479 switch (proto) { 5480 5481 case Q_SCTP: 5482 b1 = gen_proto(cstate, IPPROTO_SCTP, Q_DEFAULT, Q_DEFAULT); 5483 break; 5484 5485 case Q_TCP: 5486 b1 = gen_proto(cstate, IPPROTO_TCP, Q_DEFAULT, Q_DEFAULT); 5487 break; 5488 5489 case Q_UDP: 5490 b1 = gen_proto(cstate, IPPROTO_UDP, Q_DEFAULT, Q_DEFAULT); 5491 break; 5492 5493 case Q_ICMP: 5494 b1 = gen_proto(cstate, IPPROTO_ICMP, Q_IP, Q_DEFAULT); 5495 break; 5496 5497 #ifndef IPPROTO_IGMP 5498 #define IPPROTO_IGMP 2 5499 #endif 5500 5501 case Q_IGMP: 5502 b1 = gen_proto(cstate, IPPROTO_IGMP, Q_IP, Q_DEFAULT); 5503 break; 5504 5505 #ifndef IPPROTO_IGRP 5506 #define IPPROTO_IGRP 9 5507 #endif 5508 case Q_IGRP: 5509 b1 = gen_proto(cstate, IPPROTO_IGRP, Q_IP, Q_DEFAULT); 5510 break; 5511 5512 #ifndef IPPROTO_PIM 5513 #define IPPROTO_PIM 103 5514 #endif 5515 5516 case Q_PIM: 5517 b1 = gen_proto(cstate, IPPROTO_PIM, Q_DEFAULT, Q_DEFAULT); 5518 break; 5519 5520 #ifndef IPPROTO_VRRP 5521 #define IPPROTO_VRRP 112 5522 #endif 5523 5524 case Q_VRRP: 5525 b1 = gen_proto(cstate, IPPROTO_VRRP, Q_IP, Q_DEFAULT); 5526 break; 5527 5528 #ifndef IPPROTO_CARP 5529 #define IPPROTO_CARP 112 5530 #endif 5531 5532 case Q_CARP: 5533 b1 = gen_proto(cstate, IPPROTO_CARP, Q_IP, Q_DEFAULT); 5534 break; 5535 5536 case Q_IP: 5537 b1 = gen_linktype(cstate, ETHERTYPE_IP); 5538 break; 5539 5540 case Q_ARP: 5541 b1 = gen_linktype(cstate, ETHERTYPE_ARP); 5542 break; 5543 5544 case Q_RARP: 5545 b1 = gen_linktype(cstate, ETHERTYPE_REVARP); 5546 break; 5547 5548 case Q_LINK: 5549 bpf_error(cstate, "link layer applied in wrong context"); 5550 5551 case Q_ATALK: 5552 b1 = gen_linktype(cstate, ETHERTYPE_ATALK); 5553 break; 5554 5555 case Q_AARP: 5556 b1 = gen_linktype(cstate, ETHERTYPE_AARP); 5557 break; 5558 5559 case Q_DECNET: 5560 b1 = gen_linktype(cstate, ETHERTYPE_DN); 5561 break; 5562 5563 case Q_SCA: 5564 b1 = gen_linktype(cstate, ETHERTYPE_SCA); 5565 break; 5566 5567 case Q_LAT: 5568 b1 = gen_linktype(cstate, ETHERTYPE_LAT); 5569 break; 5570 5571 case Q_MOPDL: 5572 b1 = gen_linktype(cstate, ETHERTYPE_MOPDL); 5573 break; 5574 5575 case Q_MOPRC: 5576 b1 = gen_linktype(cstate, ETHERTYPE_MOPRC); 5577 break; 5578 5579 case Q_IPV6: 5580 b1 = gen_linktype(cstate, ETHERTYPE_IPV6); 5581 break; 5582 5583 #ifndef IPPROTO_ICMPV6 5584 #define IPPROTO_ICMPV6 58 5585 #endif 5586 case Q_ICMPV6: 5587 b1 = gen_proto(cstate, IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT); 5588 break; 5589 5590 #ifndef IPPROTO_AH 5591 #define IPPROTO_AH 51 5592 #endif 5593 case Q_AH: 5594 b1 = gen_proto(cstate, IPPROTO_AH, Q_DEFAULT, Q_DEFAULT); 5595 break; 5596 5597 #ifndef IPPROTO_ESP 5598 #define IPPROTO_ESP 50 5599 #endif 5600 case Q_ESP: 5601 b1 = gen_proto(cstate, IPPROTO_ESP, Q_DEFAULT, Q_DEFAULT); 5602 break; 5603 5604 case Q_ISO: 5605 b1 = gen_linktype(cstate, LLCSAP_ISONS); 5606 break; 5607 5608 case Q_ESIS: 5609 b1 = gen_proto(cstate, ISO9542_ESIS, Q_ISO, Q_DEFAULT); 5610 break; 5611 5612 case Q_ISIS: 5613 b1 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT); 5614 break; 5615 5616 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */ 5617 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT); 5618 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */ 5619 gen_or(b0, b1); 5620 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT); 5621 gen_or(b0, b1); 5622 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); 5623 gen_or(b0, b1); 5624 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); 5625 gen_or(b0, b1); 5626 break; 5627 5628 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */ 5629 b0 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT); 5630 b1 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */ 5631 gen_or(b0, b1); 5632 b0 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT); 5633 gen_or(b0, b1); 5634 b0 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); 5635 gen_or(b0, b1); 5636 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); 5637 gen_or(b0, b1); 5638 break; 5639 5640 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */ 5641 b0 = gen_proto(cstate, ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT); 5642 b1 = gen_proto(cstate, ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT); 5643 gen_or(b0, b1); 5644 b0 = gen_proto(cstate, ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); 5645 gen_or(b0, b1); 5646 break; 5647 5648 case Q_ISIS_LSP: 5649 b0 = gen_proto(cstate, ISIS_L1_LSP, Q_ISIS, Q_DEFAULT); 5650 b1 = gen_proto(cstate, ISIS_L2_LSP, Q_ISIS, Q_DEFAULT); 5651 gen_or(b0, b1); 5652 break; 5653 5654 case Q_ISIS_SNP: 5655 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); 5656 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); 5657 gen_or(b0, b1); 5658 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); 5659 gen_or(b0, b1); 5660 b0 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); 5661 gen_or(b0, b1); 5662 break; 5663 5664 case Q_ISIS_CSNP: 5665 b0 = gen_proto(cstate, ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT); 5666 b1 = gen_proto(cstate, ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT); 5667 gen_or(b0, b1); 5668 break; 5669 5670 case Q_ISIS_PSNP: 5671 b0 = gen_proto(cstate, ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT); 5672 b1 = gen_proto(cstate, ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT); 5673 gen_or(b0, b1); 5674 break; 5675 5676 case Q_CLNP: 5677 b1 = gen_proto(cstate, ISO8473_CLNP, Q_ISO, Q_DEFAULT); 5678 break; 5679 5680 case Q_STP: 5681 b1 = gen_linktype(cstate, LLCSAP_8021D); 5682 break; 5683 5684 case Q_IPX: 5685 b1 = gen_linktype(cstate, LLCSAP_IPX); 5686 break; 5687 5688 case Q_NETBEUI: 5689 b1 = gen_linktype(cstate, LLCSAP_NETBEUI); 5690 break; 5691 5692 case Q_RADIO: 5693 bpf_error(cstate, "'radio' is not a valid protocol type"); 5694 5695 default: 5696 abort(); 5697 } 5698 return b1; 5699 } 5700 5701 struct block * 5702 gen_proto_abbrev(compiler_state_t *cstate, int proto) 5703 { 5704 /* 5705 * Catch errors reported by us and routines below us, and return NULL 5706 * on an error. 5707 */ 5708 if (setjmp(cstate->top_ctx)) 5709 return (NULL); 5710 5711 return gen_proto_abbrev_internal(cstate, proto); 5712 } 5713 5714 static struct block * 5715 gen_ipfrag(compiler_state_t *cstate) 5716 { 5717 struct slist *s; 5718 struct block *b; 5719 5720 /* not IPv4 frag other than the first frag */ 5721 s = gen_load_a(cstate, OR_LINKPL, 6, BPF_H); 5722 b = new_block(cstate, JMP(BPF_JSET)); 5723 b->s.k = 0x1fff; 5724 b->stmts = s; 5725 gen_not(b); 5726 5727 return b; 5728 } 5729 5730 /* 5731 * Generate a comparison to a port value in the transport-layer header 5732 * at the specified offset from the beginning of that header. 5733 * 5734 * XXX - this handles a variable-length prefix preceding the link-layer 5735 * header, such as the radiotap or AVS radio prefix, but doesn't handle 5736 * variable-length link-layer headers (such as Token Ring or 802.11 5737 * headers). 5738 */ 5739 static struct block * 5740 gen_portatom(compiler_state_t *cstate, int off, bpf_u_int32 v) 5741 { 5742 return gen_cmp(cstate, OR_TRAN_IPV4, off, BPF_H, v); 5743 } 5744 5745 static struct block * 5746 gen_portatom6(compiler_state_t *cstate, int off, bpf_u_int32 v) 5747 { 5748 return gen_cmp(cstate, OR_TRAN_IPV6, off, BPF_H, v); 5749 } 5750 5751 static struct block * 5752 gen_portop(compiler_state_t *cstate, u_int port, u_int proto, int dir) 5753 { 5754 struct block *b0, *b1, *tmp; 5755 5756 /* ip proto 'proto' and not a fragment other than the first fragment */ 5757 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto); 5758 b0 = gen_ipfrag(cstate); 5759 gen_and(tmp, b0); 5760 5761 switch (dir) { 5762 case Q_SRC: 5763 b1 = gen_portatom(cstate, 0, port); 5764 break; 5765 5766 case Q_DST: 5767 b1 = gen_portatom(cstate, 2, port); 5768 break; 5769 5770 case Q_AND: 5771 tmp = gen_portatom(cstate, 0, port); 5772 b1 = gen_portatom(cstate, 2, port); 5773 gen_and(tmp, b1); 5774 break; 5775 5776 case Q_DEFAULT: 5777 case Q_OR: 5778 tmp = gen_portatom(cstate, 0, port); 5779 b1 = gen_portatom(cstate, 2, port); 5780 gen_or(tmp, b1); 5781 break; 5782 5783 case Q_ADDR1: 5784 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for ports"); 5785 /*NOTREACHED*/ 5786 5787 case Q_ADDR2: 5788 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for ports"); 5789 /*NOTREACHED*/ 5790 5791 case Q_ADDR3: 5792 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for ports"); 5793 /*NOTREACHED*/ 5794 5795 case Q_ADDR4: 5796 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for ports"); 5797 /*NOTREACHED*/ 5798 5799 case Q_RA: 5800 bpf_error(cstate, "'ra' is not a valid qualifier for ports"); 5801 /*NOTREACHED*/ 5802 5803 case Q_TA: 5804 bpf_error(cstate, "'ta' is not a valid qualifier for ports"); 5805 /*NOTREACHED*/ 5806 5807 default: 5808 abort(); 5809 /*NOTREACHED*/ 5810 } 5811 gen_and(b0, b1); 5812 5813 return b1; 5814 } 5815 5816 static struct block * 5817 gen_port(compiler_state_t *cstate, u_int port, int ip_proto, int dir) 5818 { 5819 struct block *b0, *b1, *tmp; 5820 5821 /* 5822 * ether proto ip 5823 * 5824 * For FDDI, RFC 1188 says that SNAP encapsulation is used, 5825 * not LLC encapsulation with LLCSAP_IP. 5826 * 5827 * For IEEE 802 networks - which includes 802.5 token ring 5828 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 5829 * says that SNAP encapsulation is used, not LLC encapsulation 5830 * with LLCSAP_IP. 5831 * 5832 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and 5833 * RFC 2225 say that SNAP encapsulation is used, not LLC 5834 * encapsulation with LLCSAP_IP. 5835 * 5836 * So we always check for ETHERTYPE_IP. 5837 */ 5838 b0 = gen_linktype(cstate, ETHERTYPE_IP); 5839 5840 switch (ip_proto) { 5841 case IPPROTO_UDP: 5842 case IPPROTO_TCP: 5843 case IPPROTO_SCTP: 5844 b1 = gen_portop(cstate, port, (u_int)ip_proto, dir); 5845 break; 5846 5847 case PROTO_UNDEF: 5848 tmp = gen_portop(cstate, port, IPPROTO_TCP, dir); 5849 b1 = gen_portop(cstate, port, IPPROTO_UDP, dir); 5850 gen_or(tmp, b1); 5851 tmp = gen_portop(cstate, port, IPPROTO_SCTP, dir); 5852 gen_or(tmp, b1); 5853 break; 5854 5855 default: 5856 abort(); 5857 } 5858 gen_and(b0, b1); 5859 return b1; 5860 } 5861 5862 struct block * 5863 gen_portop6(compiler_state_t *cstate, u_int port, u_int proto, int dir) 5864 { 5865 struct block *b0, *b1, *tmp; 5866 5867 /* ip6 proto 'proto' */ 5868 /* XXX - catch the first fragment of a fragmented packet? */ 5869 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto); 5870 5871 switch (dir) { 5872 case Q_SRC: 5873 b1 = gen_portatom6(cstate, 0, port); 5874 break; 5875 5876 case Q_DST: 5877 b1 = gen_portatom6(cstate, 2, port); 5878 break; 5879 5880 case Q_AND: 5881 tmp = gen_portatom6(cstate, 0, port); 5882 b1 = gen_portatom6(cstate, 2, port); 5883 gen_and(tmp, b1); 5884 break; 5885 5886 case Q_DEFAULT: 5887 case Q_OR: 5888 tmp = gen_portatom6(cstate, 0, port); 5889 b1 = gen_portatom6(cstate, 2, port); 5890 gen_or(tmp, b1); 5891 break; 5892 5893 default: 5894 abort(); 5895 } 5896 gen_and(b0, b1); 5897 5898 return b1; 5899 } 5900 5901 static struct block * 5902 gen_port6(compiler_state_t *cstate, u_int port, int ip_proto, int dir) 5903 { 5904 struct block *b0, *b1, *tmp; 5905 5906 /* link proto ip6 */ 5907 b0 = gen_linktype(cstate, ETHERTYPE_IPV6); 5908 5909 switch (ip_proto) { 5910 case IPPROTO_UDP: 5911 case IPPROTO_TCP: 5912 case IPPROTO_SCTP: 5913 b1 = gen_portop6(cstate, port, (u_int)ip_proto, dir); 5914 break; 5915 5916 case PROTO_UNDEF: 5917 tmp = gen_portop6(cstate, port, IPPROTO_TCP, dir); 5918 b1 = gen_portop6(cstate, port, IPPROTO_UDP, dir); 5919 gen_or(tmp, b1); 5920 tmp = gen_portop6(cstate, port, IPPROTO_SCTP, dir); 5921 gen_or(tmp, b1); 5922 break; 5923 5924 default: 5925 abort(); 5926 } 5927 gen_and(b0, b1); 5928 return b1; 5929 } 5930 5931 /* gen_portrange code */ 5932 static struct block * 5933 gen_portrangeatom(compiler_state_t *cstate, u_int off, bpf_u_int32 v1, 5934 bpf_u_int32 v2) 5935 { 5936 struct block *b1, *b2; 5937 5938 if (v1 > v2) { 5939 /* 5940 * Reverse the order of the ports, so v1 is the lower one. 5941 */ 5942 bpf_u_int32 vtemp; 5943 5944 vtemp = v1; 5945 v1 = v2; 5946 v2 = vtemp; 5947 } 5948 5949 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV4, off, BPF_H, v1); 5950 b2 = gen_cmp_le(cstate, OR_TRAN_IPV4, off, BPF_H, v2); 5951 5952 gen_and(b1, b2); 5953 5954 return b2; 5955 } 5956 5957 static struct block * 5958 gen_portrangeop(compiler_state_t *cstate, u_int port1, u_int port2, 5959 bpf_u_int32 proto, int dir) 5960 { 5961 struct block *b0, *b1, *tmp; 5962 5963 /* ip proto 'proto' and not a fragment other than the first fragment */ 5964 tmp = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, proto); 5965 b0 = gen_ipfrag(cstate); 5966 gen_and(tmp, b0); 5967 5968 switch (dir) { 5969 case Q_SRC: 5970 b1 = gen_portrangeatom(cstate, 0, port1, port2); 5971 break; 5972 5973 case Q_DST: 5974 b1 = gen_portrangeatom(cstate, 2, port1, port2); 5975 break; 5976 5977 case Q_AND: 5978 tmp = gen_portrangeatom(cstate, 0, port1, port2); 5979 b1 = gen_portrangeatom(cstate, 2, port1, port2); 5980 gen_and(tmp, b1); 5981 break; 5982 5983 case Q_DEFAULT: 5984 case Q_OR: 5985 tmp = gen_portrangeatom(cstate, 0, port1, port2); 5986 b1 = gen_portrangeatom(cstate, 2, port1, port2); 5987 gen_or(tmp, b1); 5988 break; 5989 5990 case Q_ADDR1: 5991 bpf_error(cstate, "'addr1' and 'address1' are not valid qualifiers for port ranges"); 5992 /*NOTREACHED*/ 5993 5994 case Q_ADDR2: 5995 bpf_error(cstate, "'addr2' and 'address2' are not valid qualifiers for port ranges"); 5996 /*NOTREACHED*/ 5997 5998 case Q_ADDR3: 5999 bpf_error(cstate, "'addr3' and 'address3' are not valid qualifiers for port ranges"); 6000 /*NOTREACHED*/ 6001 6002 case Q_ADDR4: 6003 bpf_error(cstate, "'addr4' and 'address4' are not valid qualifiers for port ranges"); 6004 /*NOTREACHED*/ 6005 6006 case Q_RA: 6007 bpf_error(cstate, "'ra' is not a valid qualifier for port ranges"); 6008 /*NOTREACHED*/ 6009 6010 case Q_TA: 6011 bpf_error(cstate, "'ta' is not a valid qualifier for port ranges"); 6012 /*NOTREACHED*/ 6013 6014 default: 6015 abort(); 6016 /*NOTREACHED*/ 6017 } 6018 gen_and(b0, b1); 6019 6020 return b1; 6021 } 6022 6023 static struct block * 6024 gen_portrange(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto, 6025 int dir) 6026 { 6027 struct block *b0, *b1, *tmp; 6028 6029 /* link proto ip */ 6030 b0 = gen_linktype(cstate, ETHERTYPE_IP); 6031 6032 switch (ip_proto) { 6033 case IPPROTO_UDP: 6034 case IPPROTO_TCP: 6035 case IPPROTO_SCTP: 6036 b1 = gen_portrangeop(cstate, port1, port2, (bpf_u_int32)ip_proto, 6037 dir); 6038 break; 6039 6040 case PROTO_UNDEF: 6041 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_TCP, dir); 6042 b1 = gen_portrangeop(cstate, port1, port2, IPPROTO_UDP, dir); 6043 gen_or(tmp, b1); 6044 tmp = gen_portrangeop(cstate, port1, port2, IPPROTO_SCTP, dir); 6045 gen_or(tmp, b1); 6046 break; 6047 6048 default: 6049 abort(); 6050 } 6051 gen_and(b0, b1); 6052 return b1; 6053 } 6054 6055 static struct block * 6056 gen_portrangeatom6(compiler_state_t *cstate, u_int off, bpf_u_int32 v1, 6057 bpf_u_int32 v2) 6058 { 6059 struct block *b1, *b2; 6060 6061 if (v1 > v2) { 6062 /* 6063 * Reverse the order of the ports, so v1 is the lower one. 6064 */ 6065 bpf_u_int32 vtemp; 6066 6067 vtemp = v1; 6068 v1 = v2; 6069 v2 = vtemp; 6070 } 6071 6072 b1 = gen_cmp_ge(cstate, OR_TRAN_IPV6, off, BPF_H, v1); 6073 b2 = gen_cmp_le(cstate, OR_TRAN_IPV6, off, BPF_H, v2); 6074 6075 gen_and(b1, b2); 6076 6077 return b2; 6078 } 6079 6080 static struct block * 6081 gen_portrangeop6(compiler_state_t *cstate, u_int port1, u_int port2, 6082 bpf_u_int32 proto, int dir) 6083 { 6084 struct block *b0, *b1, *tmp; 6085 6086 /* ip6 proto 'proto' */ 6087 /* XXX - catch the first fragment of a fragmented packet? */ 6088 b0 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, proto); 6089 6090 switch (dir) { 6091 case Q_SRC: 6092 b1 = gen_portrangeatom6(cstate, 0, port1, port2); 6093 break; 6094 6095 case Q_DST: 6096 b1 = gen_portrangeatom6(cstate, 2, port1, port2); 6097 break; 6098 6099 case Q_AND: 6100 tmp = gen_portrangeatom6(cstate, 0, port1, port2); 6101 b1 = gen_portrangeatom6(cstate, 2, port1, port2); 6102 gen_and(tmp, b1); 6103 break; 6104 6105 case Q_DEFAULT: 6106 case Q_OR: 6107 tmp = gen_portrangeatom6(cstate, 0, port1, port2); 6108 b1 = gen_portrangeatom6(cstate, 2, port1, port2); 6109 gen_or(tmp, b1); 6110 break; 6111 6112 default: 6113 abort(); 6114 } 6115 gen_and(b0, b1); 6116 6117 return b1; 6118 } 6119 6120 static struct block * 6121 gen_portrange6(compiler_state_t *cstate, u_int port1, u_int port2, int ip_proto, 6122 int dir) 6123 { 6124 struct block *b0, *b1, *tmp; 6125 6126 /* link proto ip6 */ 6127 b0 = gen_linktype(cstate, ETHERTYPE_IPV6); 6128 6129 switch (ip_proto) { 6130 case IPPROTO_UDP: 6131 case IPPROTO_TCP: 6132 case IPPROTO_SCTP: 6133 b1 = gen_portrangeop6(cstate, port1, port2, (bpf_u_int32)ip_proto, 6134 dir); 6135 break; 6136 6137 case PROTO_UNDEF: 6138 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_TCP, dir); 6139 b1 = gen_portrangeop6(cstate, port1, port2, IPPROTO_UDP, dir); 6140 gen_or(tmp, b1); 6141 tmp = gen_portrangeop6(cstate, port1, port2, IPPROTO_SCTP, dir); 6142 gen_or(tmp, b1); 6143 break; 6144 6145 default: 6146 abort(); 6147 } 6148 gen_and(b0, b1); 6149 return b1; 6150 } 6151 6152 static int 6153 lookup_proto(compiler_state_t *cstate, const char *name, int proto) 6154 { 6155 register int v; 6156 6157 switch (proto) { 6158 6159 case Q_DEFAULT: 6160 case Q_IP: 6161 case Q_IPV6: 6162 v = pcap_nametoproto(name); 6163 if (v == PROTO_UNDEF) 6164 bpf_error(cstate, "unknown ip proto '%s'", name); 6165 break; 6166 6167 case Q_LINK: 6168 /* XXX should look up h/w protocol type based on cstate->linktype */ 6169 v = pcap_nametoeproto(name); 6170 if (v == PROTO_UNDEF) { 6171 v = pcap_nametollc(name); 6172 if (v == PROTO_UNDEF) 6173 bpf_error(cstate, "unknown ether proto '%s'", name); 6174 } 6175 break; 6176 6177 case Q_ISO: 6178 if (strcmp(name, "esis") == 0) 6179 v = ISO9542_ESIS; 6180 else if (strcmp(name, "isis") == 0) 6181 v = ISO10589_ISIS; 6182 else if (strcmp(name, "clnp") == 0) 6183 v = ISO8473_CLNP; 6184 else 6185 bpf_error(cstate, "unknown osi proto '%s'", name); 6186 break; 6187 6188 default: 6189 v = PROTO_UNDEF; 6190 break; 6191 } 6192 return v; 6193 } 6194 6195 #if !defined(NO_PROTOCHAIN) 6196 static struct block * 6197 gen_protochain(compiler_state_t *cstate, bpf_u_int32 v, int proto) 6198 { 6199 struct block *b0, *b; 6200 struct slist *s[100]; 6201 int fix2, fix3, fix4, fix5; 6202 int ahcheck, again, end; 6203 int i, max; 6204 int reg2 = alloc_reg(cstate); 6205 6206 memset(s, 0, sizeof(s)); 6207 fix3 = fix4 = fix5 = 0; 6208 6209 switch (proto) { 6210 case Q_IP: 6211 case Q_IPV6: 6212 break; 6213 case Q_DEFAULT: 6214 b0 = gen_protochain(cstate, v, Q_IP); 6215 b = gen_protochain(cstate, v, Q_IPV6); 6216 gen_or(b0, b); 6217 return b; 6218 default: 6219 bpf_error(cstate, "bad protocol applied for 'protochain'"); 6220 /*NOTREACHED*/ 6221 } 6222 6223 /* 6224 * We don't handle variable-length prefixes before the link-layer 6225 * header, or variable-length link-layer headers, here yet. 6226 * We might want to add BPF instructions to do the protochain 6227 * work, to simplify that and, on platforms that have a BPF 6228 * interpreter with the new instructions, let the filtering 6229 * be done in the kernel. (We already require a modified BPF 6230 * engine to do the protochain stuff, to support backward 6231 * branches, and backward branch support is unlikely to appear 6232 * in kernel BPF engines.) 6233 */ 6234 if (cstate->off_linkpl.is_variable) 6235 bpf_error(cstate, "'protochain' not supported with variable length headers"); 6236 6237 /* 6238 * To quote a comment in optimize.c: 6239 * 6240 * "These data structures are used in a Cocke and Schwartz style 6241 * value numbering scheme. Since the flowgraph is acyclic, 6242 * exit values can be propagated from a node's predecessors 6243 * provided it is uniquely defined." 6244 * 6245 * "Acyclic" means "no backward branches", which means "no 6246 * loops", so we have to turn the optimizer off. 6247 */ 6248 cstate->no_optimize = 1; 6249 6250 /* 6251 * s[0] is a dummy entry to protect other BPF insn from damage 6252 * by s[fix] = foo with uninitialized variable "fix". It is somewhat 6253 * hard to find interdependency made by jump table fixup. 6254 */ 6255 i = 0; 6256 s[i] = new_stmt(cstate, 0); /*dummy*/ 6257 i++; 6258 6259 switch (proto) { 6260 case Q_IP: 6261 b0 = gen_linktype(cstate, ETHERTYPE_IP); 6262 6263 /* A = ip->ip_p */ 6264 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); 6265 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 9; 6266 i++; 6267 /* X = ip->ip_hl << 2 */ 6268 s[i] = new_stmt(cstate, BPF_LDX|BPF_MSH|BPF_B); 6269 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; 6270 i++; 6271 break; 6272 6273 case Q_IPV6: 6274 b0 = gen_linktype(cstate, ETHERTYPE_IPV6); 6275 6276 /* A = ip6->ip_nxt */ 6277 s[i] = new_stmt(cstate, BPF_LD|BPF_ABS|BPF_B); 6278 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 6; 6279 i++; 6280 /* X = sizeof(struct ip6_hdr) */ 6281 s[i] = new_stmt(cstate, BPF_LDX|BPF_IMM); 6282 s[i]->s.k = 40; 6283 i++; 6284 break; 6285 6286 default: 6287 bpf_error(cstate, "unsupported proto to gen_protochain"); 6288 /*NOTREACHED*/ 6289 } 6290 6291 /* again: if (A == v) goto end; else fall through; */ 6292 again = i; 6293 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); 6294 s[i]->s.k = v; 6295 s[i]->s.jt = NULL; /*later*/ 6296 s[i]->s.jf = NULL; /*update in next stmt*/ 6297 fix5 = i; 6298 i++; 6299 6300 #ifndef IPPROTO_NONE 6301 #define IPPROTO_NONE 59 6302 #endif 6303 /* if (A == IPPROTO_NONE) goto end */ 6304 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); 6305 s[i]->s.jt = NULL; /*later*/ 6306 s[i]->s.jf = NULL; /*update in next stmt*/ 6307 s[i]->s.k = IPPROTO_NONE; 6308 s[fix5]->s.jf = s[i]; 6309 fix2 = i; 6310 i++; 6311 6312 if (proto == Q_IPV6) { 6313 int v6start, v6end, v6advance, j; 6314 6315 v6start = i; 6316 /* if (A == IPPROTO_HOPOPTS) goto v6advance */ 6317 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); 6318 s[i]->s.jt = NULL; /*later*/ 6319 s[i]->s.jf = NULL; /*update in next stmt*/ 6320 s[i]->s.k = IPPROTO_HOPOPTS; 6321 s[fix2]->s.jf = s[i]; 6322 i++; 6323 /* if (A == IPPROTO_DSTOPTS) goto v6advance */ 6324 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); 6325 s[i]->s.jt = NULL; /*later*/ 6326 s[i]->s.jf = NULL; /*update in next stmt*/ 6327 s[i]->s.k = IPPROTO_DSTOPTS; 6328 i++; 6329 /* if (A == IPPROTO_ROUTING) goto v6advance */ 6330 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); 6331 s[i]->s.jt = NULL; /*later*/ 6332 s[i]->s.jf = NULL; /*update in next stmt*/ 6333 s[i]->s.k = IPPROTO_ROUTING; 6334 i++; 6335 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */ 6336 s[i - 1]->s.jf = s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); 6337 s[i]->s.jt = NULL; /*later*/ 6338 s[i]->s.jf = NULL; /*later*/ 6339 s[i]->s.k = IPPROTO_FRAGMENT; 6340 fix3 = i; 6341 v6end = i; 6342 i++; 6343 6344 /* v6advance: */ 6345 v6advance = i; 6346 6347 /* 6348 * in short, 6349 * A = P[X + packet head]; 6350 * X = X + (P[X + packet head + 1] + 1) * 8; 6351 */ 6352 /* A = P[X + packet head] */ 6353 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); 6354 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; 6355 i++; 6356 /* MEM[reg2] = A */ 6357 s[i] = new_stmt(cstate, BPF_ST); 6358 s[i]->s.k = reg2; 6359 i++; 6360 /* A = P[X + packet head + 1]; */ 6361 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); 6362 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 1; 6363 i++; 6364 /* A += 1 */ 6365 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 6366 s[i]->s.k = 1; 6367 i++; 6368 /* A *= 8 */ 6369 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); 6370 s[i]->s.k = 8; 6371 i++; 6372 /* A += X */ 6373 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); 6374 s[i]->s.k = 0; 6375 i++; 6376 /* X = A; */ 6377 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); 6378 i++; 6379 /* A = MEM[reg2] */ 6380 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM); 6381 s[i]->s.k = reg2; 6382 i++; 6383 6384 /* goto again; (must use BPF_JA for backward jump) */ 6385 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA); 6386 s[i]->s.k = again - i - 1; 6387 s[i - 1]->s.jf = s[i]; 6388 i++; 6389 6390 /* fixup */ 6391 for (j = v6start; j <= v6end; j++) 6392 s[j]->s.jt = s[v6advance]; 6393 } else { 6394 /* nop */ 6395 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 6396 s[i]->s.k = 0; 6397 s[fix2]->s.jf = s[i]; 6398 i++; 6399 } 6400 6401 /* ahcheck: */ 6402 ahcheck = i; 6403 /* if (A == IPPROTO_AH) then fall through; else goto end; */ 6404 s[i] = new_stmt(cstate, BPF_JMP|BPF_JEQ|BPF_K); 6405 s[i]->s.jt = NULL; /*later*/ 6406 s[i]->s.jf = NULL; /*later*/ 6407 s[i]->s.k = IPPROTO_AH; 6408 if (fix3) 6409 s[fix3]->s.jf = s[ahcheck]; 6410 fix4 = i; 6411 i++; 6412 6413 /* 6414 * in short, 6415 * A = P[X]; 6416 * X = X + (P[X + 1] + 2) * 4; 6417 */ 6418 /* A = X */ 6419 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA); 6420 i++; 6421 /* A = P[X + packet head]; */ 6422 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); 6423 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; 6424 i++; 6425 /* MEM[reg2] = A */ 6426 s[i] = new_stmt(cstate, BPF_ST); 6427 s[i]->s.k = reg2; 6428 i++; 6429 /* A = X */ 6430 s[i - 1]->s.jt = s[i] = new_stmt(cstate, BPF_MISC|BPF_TXA); 6431 i++; 6432 /* A += 1 */ 6433 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 6434 s[i]->s.k = 1; 6435 i++; 6436 /* X = A */ 6437 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); 6438 i++; 6439 /* A = P[X + packet head] */ 6440 s[i] = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); 6441 s[i]->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; 6442 i++; 6443 /* A += 2 */ 6444 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 6445 s[i]->s.k = 2; 6446 i++; 6447 /* A *= 4 */ 6448 s[i] = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); 6449 s[i]->s.k = 4; 6450 i++; 6451 /* X = A; */ 6452 s[i] = new_stmt(cstate, BPF_MISC|BPF_TAX); 6453 i++; 6454 /* A = MEM[reg2] */ 6455 s[i] = new_stmt(cstate, BPF_LD|BPF_MEM); 6456 s[i]->s.k = reg2; 6457 i++; 6458 6459 /* goto again; (must use BPF_JA for backward jump) */ 6460 s[i] = new_stmt(cstate, BPF_JMP|BPF_JA); 6461 s[i]->s.k = again - i - 1; 6462 i++; 6463 6464 /* end: nop */ 6465 end = i; 6466 s[i] = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 6467 s[i]->s.k = 0; 6468 s[fix2]->s.jt = s[end]; 6469 s[fix4]->s.jf = s[end]; 6470 s[fix5]->s.jt = s[end]; 6471 i++; 6472 6473 /* 6474 * make slist chain 6475 */ 6476 max = i; 6477 for (i = 0; i < max - 1; i++) 6478 s[i]->next = s[i + 1]; 6479 s[max - 1]->next = NULL; 6480 6481 /* 6482 * emit final check 6483 */ 6484 b = new_block(cstate, JMP(BPF_JEQ)); 6485 b->stmts = s[1]; /*remember, s[0] is dummy*/ 6486 b->s.k = v; 6487 6488 free_reg(cstate, reg2); 6489 6490 gen_and(b0, b); 6491 return b; 6492 } 6493 #endif /* !defined(NO_PROTOCHAIN) */ 6494 6495 static struct block * 6496 gen_check_802_11_data_frame(compiler_state_t *cstate) 6497 { 6498 struct slist *s; 6499 struct block *b0, *b1; 6500 6501 /* 6502 * A data frame has the 0x08 bit (b3) in the frame control field set 6503 * and the 0x04 bit (b2) clear. 6504 */ 6505 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 6506 b0 = new_block(cstate, JMP(BPF_JSET)); 6507 b0->s.k = 0x08; 6508 b0->stmts = s; 6509 6510 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 6511 b1 = new_block(cstate, JMP(BPF_JSET)); 6512 b1->s.k = 0x04; 6513 b1->stmts = s; 6514 gen_not(b1); 6515 6516 gen_and(b1, b0); 6517 6518 return b0; 6519 } 6520 6521 /* 6522 * Generate code that checks whether the packet is a packet for protocol 6523 * <proto> and whether the type field in that protocol's header has 6524 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an 6525 * IP packet and checks the protocol number in the IP header against <v>. 6526 * 6527 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks 6528 * against Q_IP and Q_IPV6. 6529 */ 6530 static struct block * 6531 gen_proto(compiler_state_t *cstate, bpf_u_int32 v, int proto, int dir) 6532 { 6533 struct block *b0, *b1; 6534 struct block *b2; 6535 6536 if (dir != Q_DEFAULT) 6537 bpf_error(cstate, "direction applied to 'proto'"); 6538 6539 switch (proto) { 6540 case Q_DEFAULT: 6541 b0 = gen_proto(cstate, v, Q_IP, dir); 6542 b1 = gen_proto(cstate, v, Q_IPV6, dir); 6543 gen_or(b0, b1); 6544 return b1; 6545 6546 case Q_LINK: 6547 return gen_linktype(cstate, v); 6548 6549 case Q_IP: 6550 /* 6551 * For FDDI, RFC 1188 says that SNAP encapsulation is used, 6552 * not LLC encapsulation with LLCSAP_IP. 6553 * 6554 * For IEEE 802 networks - which includes 802.5 token ring 6555 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042 6556 * says that SNAP encapsulation is used, not LLC encapsulation 6557 * with LLCSAP_IP. 6558 * 6559 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and 6560 * RFC 2225 say that SNAP encapsulation is used, not LLC 6561 * encapsulation with LLCSAP_IP. 6562 * 6563 * So we always check for ETHERTYPE_IP. 6564 */ 6565 b0 = gen_linktype(cstate, ETHERTYPE_IP); 6566 b1 = gen_cmp(cstate, OR_LINKPL, 9, BPF_B, v); 6567 gen_and(b0, b1); 6568 return b1; 6569 6570 case Q_ARP: 6571 bpf_error(cstate, "arp does not encapsulate another protocol"); 6572 /*NOTREACHED*/ 6573 6574 case Q_RARP: 6575 bpf_error(cstate, "rarp does not encapsulate another protocol"); 6576 /*NOTREACHED*/ 6577 6578 case Q_SCTP: 6579 bpf_error(cstate, "'sctp proto' is bogus"); 6580 /*NOTREACHED*/ 6581 6582 case Q_TCP: 6583 bpf_error(cstate, "'tcp proto' is bogus"); 6584 /*NOTREACHED*/ 6585 6586 case Q_UDP: 6587 bpf_error(cstate, "'udp proto' is bogus"); 6588 /*NOTREACHED*/ 6589 6590 case Q_ICMP: 6591 bpf_error(cstate, "'icmp proto' is bogus"); 6592 /*NOTREACHED*/ 6593 6594 case Q_IGMP: 6595 bpf_error(cstate, "'igmp proto' is bogus"); 6596 /*NOTREACHED*/ 6597 6598 case Q_IGRP: 6599 bpf_error(cstate, "'igrp proto' is bogus"); 6600 /*NOTREACHED*/ 6601 6602 case Q_ATALK: 6603 bpf_error(cstate, "AppleTalk encapsulation is not specifiable"); 6604 /*NOTREACHED*/ 6605 6606 case Q_DECNET: 6607 bpf_error(cstate, "DECNET encapsulation is not specifiable"); 6608 /*NOTREACHED*/ 6609 6610 case Q_LAT: 6611 bpf_error(cstate, "LAT does not encapsulate another protocol"); 6612 /*NOTREACHED*/ 6613 6614 case Q_SCA: 6615 bpf_error(cstate, "SCA does not encapsulate another protocol"); 6616 /*NOTREACHED*/ 6617 6618 case Q_MOPRC: 6619 bpf_error(cstate, "MOPRC does not encapsulate another protocol"); 6620 /*NOTREACHED*/ 6621 6622 case Q_MOPDL: 6623 bpf_error(cstate, "MOPDL does not encapsulate another protocol"); 6624 /*NOTREACHED*/ 6625 6626 case Q_IPV6: 6627 b0 = gen_linktype(cstate, ETHERTYPE_IPV6); 6628 /* 6629 * Also check for a fragment header before the final 6630 * header. 6631 */ 6632 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, IPPROTO_FRAGMENT); 6633 b1 = gen_cmp(cstate, OR_LINKPL, 40, BPF_B, v); 6634 gen_and(b2, b1); 6635 b2 = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, v); 6636 gen_or(b2, b1); 6637 gen_and(b0, b1); 6638 return b1; 6639 6640 case Q_ICMPV6: 6641 bpf_error(cstate, "'icmp6 proto' is bogus"); 6642 /*NOTREACHED*/ 6643 6644 case Q_AH: 6645 bpf_error(cstate, "'ah proto' is bogus"); 6646 /*NOTREACHED*/ 6647 6648 case Q_ESP: 6649 bpf_error(cstate, "'esp proto' is bogus"); 6650 /*NOTREACHED*/ 6651 6652 case Q_PIM: 6653 bpf_error(cstate, "'pim proto' is bogus"); 6654 /*NOTREACHED*/ 6655 6656 case Q_VRRP: 6657 bpf_error(cstate, "'vrrp proto' is bogus"); 6658 /*NOTREACHED*/ 6659 6660 case Q_AARP: 6661 bpf_error(cstate, "'aarp proto' is bogus"); 6662 /*NOTREACHED*/ 6663 6664 case Q_ISO: 6665 switch (cstate->linktype) { 6666 6667 case DLT_FRELAY: 6668 /* 6669 * Frame Relay packets typically have an OSI 6670 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)" 6671 * generates code to check for all the OSI 6672 * NLPIDs, so calling it and then adding a check 6673 * for the particular NLPID for which we're 6674 * looking is bogus, as we can just check for 6675 * the NLPID. 6676 * 6677 * What we check for is the NLPID and a frame 6678 * control field value of UI, i.e. 0x03 followed 6679 * by the NLPID. 6680 * 6681 * XXX - assumes a 2-byte Frame Relay header with 6682 * DLCI and flags. What if the address is longer? 6683 * 6684 * XXX - what about SNAP-encapsulated frames? 6685 */ 6686 return gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, (0x03<<8) | v); 6687 /*NOTREACHED*/ 6688 6689 case DLT_C_HDLC: 6690 case DLT_HDLC: 6691 /* 6692 * Cisco uses an Ethertype lookalike - for OSI, 6693 * it's 0xfefe. 6694 */ 6695 b0 = gen_linktype(cstate, LLCSAP_ISONS<<8 | LLCSAP_ISONS); 6696 /* OSI in C-HDLC is stuffed with a fudge byte */ 6697 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 1, BPF_B, v); 6698 gen_and(b0, b1); 6699 return b1; 6700 6701 default: 6702 b0 = gen_linktype(cstate, LLCSAP_ISONS); 6703 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 0, BPF_B, v); 6704 gen_and(b0, b1); 6705 return b1; 6706 } 6707 6708 case Q_ESIS: 6709 bpf_error(cstate, "'esis proto' is bogus"); 6710 /*NOTREACHED*/ 6711 6712 case Q_ISIS: 6713 b0 = gen_proto(cstate, ISO10589_ISIS, Q_ISO, Q_DEFAULT); 6714 /* 6715 * 4 is the offset of the PDU type relative to the IS-IS 6716 * header. 6717 */ 6718 b1 = gen_cmp(cstate, OR_LINKPL_NOSNAP, 4, BPF_B, v); 6719 gen_and(b0, b1); 6720 return b1; 6721 6722 case Q_CLNP: 6723 bpf_error(cstate, "'clnp proto' is not supported"); 6724 /*NOTREACHED*/ 6725 6726 case Q_STP: 6727 bpf_error(cstate, "'stp proto' is bogus"); 6728 /*NOTREACHED*/ 6729 6730 case Q_IPX: 6731 bpf_error(cstate, "'ipx proto' is bogus"); 6732 /*NOTREACHED*/ 6733 6734 case Q_NETBEUI: 6735 bpf_error(cstate, "'netbeui proto' is bogus"); 6736 /*NOTREACHED*/ 6737 6738 case Q_ISIS_L1: 6739 bpf_error(cstate, "'l1 proto' is bogus"); 6740 /*NOTREACHED*/ 6741 6742 case Q_ISIS_L2: 6743 bpf_error(cstate, "'l2 proto' is bogus"); 6744 /*NOTREACHED*/ 6745 6746 case Q_ISIS_IIH: 6747 bpf_error(cstate, "'iih proto' is bogus"); 6748 /*NOTREACHED*/ 6749 6750 case Q_ISIS_SNP: 6751 bpf_error(cstate, "'snp proto' is bogus"); 6752 /*NOTREACHED*/ 6753 6754 case Q_ISIS_CSNP: 6755 bpf_error(cstate, "'csnp proto' is bogus"); 6756 /*NOTREACHED*/ 6757 6758 case Q_ISIS_PSNP: 6759 bpf_error(cstate, "'psnp proto' is bogus"); 6760 /*NOTREACHED*/ 6761 6762 case Q_ISIS_LSP: 6763 bpf_error(cstate, "'lsp proto' is bogus"); 6764 /*NOTREACHED*/ 6765 6766 case Q_RADIO: 6767 bpf_error(cstate, "'radio proto' is bogus"); 6768 /*NOTREACHED*/ 6769 6770 case Q_CARP: 6771 bpf_error(cstate, "'carp proto' is bogus"); 6772 /*NOTREACHED*/ 6773 6774 default: 6775 abort(); 6776 /*NOTREACHED*/ 6777 } 6778 /*NOTREACHED*/ 6779 } 6780 6781 /* 6782 * Convert a non-numeric name to a port number. 6783 */ 6784 static int 6785 nametoport(compiler_state_t *cstate, const char *name, int ipproto) 6786 { 6787 struct addrinfo hints, *res, *ai; 6788 int error; 6789 struct sockaddr_in *in4; 6790 #ifdef INET6 6791 struct sockaddr_in6 *in6; 6792 #endif 6793 int port = -1; 6794 6795 /* 6796 * We check for both TCP and UDP in case there are 6797 * ambiguous entries. 6798 */ 6799 memset(&hints, 0, sizeof(hints)); 6800 hints.ai_family = PF_UNSPEC; 6801 hints.ai_socktype = (ipproto == IPPROTO_TCP) ? SOCK_STREAM : SOCK_DGRAM; 6802 hints.ai_protocol = ipproto; 6803 error = getaddrinfo(NULL, name, &hints, &res); 6804 if (error != 0) { 6805 switch (error) { 6806 6807 case EAI_NONAME: 6808 case EAI_SERVICE: 6809 /* 6810 * No such port. Just return -1. 6811 */ 6812 break; 6813 6814 #ifdef EAI_SYSTEM 6815 case EAI_SYSTEM: 6816 /* 6817 * We don't use strerror() because it's not 6818 * guaranteed to be thread-safe on all platforms 6819 * (probably because it might use a non-thread-local 6820 * buffer into which to format an error message 6821 * if the error code isn't one for which it has 6822 * a canned string; three cheers for C string 6823 * handling). 6824 */ 6825 bpf_set_error(cstate, "getaddrinfo(\"%s\" fails with system error: %d", 6826 name, errno); 6827 port = -2; /* a real error */ 6828 break; 6829 #endif 6830 6831 default: 6832 /* 6833 * This is a real error, not just "there's 6834 * no such service name". 6835 * 6836 * We don't use gai_strerror() because it's not 6837 * guaranteed to be thread-safe on all platforms 6838 * (probably because it might use a non-thread-local 6839 * buffer into which to format an error message 6840 * if the error code isn't one for which it has 6841 * a canned string; three cheers for C string 6842 * handling). 6843 */ 6844 bpf_set_error(cstate, "getaddrinfo(\"%s\") fails with error: %d", 6845 name, error); 6846 port = -2; /* a real error */ 6847 break; 6848 } 6849 } else { 6850 /* 6851 * OK, we found it. Did it find anything? 6852 */ 6853 for (ai = res; ai != NULL; ai = ai->ai_next) { 6854 /* 6855 * Does it have an address? 6856 */ 6857 if (ai->ai_addr != NULL) { 6858 /* 6859 * Yes. Get a port number; we're done. 6860 */ 6861 if (ai->ai_addr->sa_family == AF_INET) { 6862 in4 = (struct sockaddr_in *)ai->ai_addr; 6863 port = ntohs(in4->sin_port); 6864 break; 6865 } 6866 #ifdef INET6 6867 if (ai->ai_addr->sa_family == AF_INET6) { 6868 in6 = (struct sockaddr_in6 *)ai->ai_addr; 6869 port = ntohs(in6->sin6_port); 6870 break; 6871 } 6872 #endif 6873 } 6874 } 6875 freeaddrinfo(res); 6876 } 6877 return port; 6878 } 6879 6880 /* 6881 * Convert a string to a port number. 6882 */ 6883 static bpf_u_int32 6884 stringtoport(compiler_state_t *cstate, const char *string, size_t string_size, 6885 int *proto) 6886 { 6887 stoulen_ret ret; 6888 char *cpy; 6889 bpf_u_int32 val; 6890 int tcp_port = -1; 6891 int udp_port = -1; 6892 6893 /* 6894 * See if it's a number. 6895 */ 6896 ret = stoulen(string, string_size, &val, cstate); 6897 switch (ret) { 6898 6899 case STOULEN_OK: 6900 /* Unknown port type - it's just a number. */ 6901 *proto = PROTO_UNDEF; 6902 break; 6903 6904 case STOULEN_NOT_OCTAL_NUMBER: 6905 case STOULEN_NOT_HEX_NUMBER: 6906 case STOULEN_NOT_DECIMAL_NUMBER: 6907 /* 6908 * Not a valid number; try looking it up as a port. 6909 */ 6910 cpy = malloc(string_size + 1); /* +1 for terminating '\0' */ 6911 memcpy(cpy, string, string_size); 6912 cpy[string_size] = '\0'; 6913 tcp_port = nametoport(cstate, cpy, IPPROTO_TCP); 6914 if (tcp_port == -2) { 6915 /* 6916 * We got a hard error; the error string has 6917 * already been set. 6918 */ 6919 free(cpy); 6920 longjmp(cstate->top_ctx, 1); 6921 /*NOTREACHED*/ 6922 } 6923 udp_port = nametoport(cstate, cpy, IPPROTO_UDP); 6924 if (udp_port == -2) { 6925 /* 6926 * We got a hard error; the error string has 6927 * already been set. 6928 */ 6929 free(cpy); 6930 longjmp(cstate->top_ctx, 1); 6931 /*NOTREACHED*/ 6932 } 6933 6934 /* 6935 * We need to check /etc/services for ambiguous entries. 6936 * If we find an ambiguous entry, and it has the 6937 * same port number, change the proto to PROTO_UNDEF 6938 * so both TCP and UDP will be checked. 6939 */ 6940 if (tcp_port >= 0) { 6941 val = (bpf_u_int32)tcp_port; 6942 *proto = IPPROTO_TCP; 6943 if (udp_port >= 0) { 6944 if (udp_port == tcp_port) 6945 *proto = PROTO_UNDEF; 6946 #ifdef notdef 6947 else 6948 /* Can't handle ambiguous names that refer 6949 to different port numbers. */ 6950 warning("ambiguous port %s in /etc/services", 6951 cpy); 6952 #endif 6953 } 6954 free(cpy); 6955 break; 6956 } 6957 if (udp_port >= 0) { 6958 val = (bpf_u_int32)udp_port; 6959 *proto = IPPROTO_UDP; 6960 free(cpy); 6961 break; 6962 } 6963 #if defined(ultrix) || defined(__osf__) 6964 /* Special hack in case NFS isn't in /etc/services */ 6965 if (strcmp(cpy, "nfs") == 0) { 6966 val = 2049; 6967 *proto = PROTO_UNDEF; 6968 free(cpy); 6969 break; 6970 } 6971 #endif 6972 bpf_set_error(cstate, "'%s' is not a valid port", cpy); 6973 free(cpy); 6974 longjmp(cstate->top_ctx, 1); 6975 /*NOTREACHED*/ 6976 6977 case STOULEN_ERROR: 6978 /* Error already set. */ 6979 longjmp(cstate->top_ctx, 1); 6980 /*NOTREACHED*/ 6981 6982 default: 6983 /* Should not happen */ 6984 bpf_set_error(cstate, "stoulen returned %d - this should not happen", ret); 6985 longjmp(cstate->top_ctx, 1); 6986 /*NOTREACHED*/ 6987 } 6988 return (val); 6989 } 6990 6991 /* 6992 * Convert a string in the form PPP-PPP, which correspond to ports, to 6993 * a starting and ending port in a port range. 6994 */ 6995 static void 6996 stringtoportrange(compiler_state_t *cstate, const char *string, 6997 bpf_u_int32 *port1, bpf_u_int32 *port2, int *proto) 6998 { 6999 char *hyphen_off; 7000 const char *first, *second; 7001 size_t first_size, second_size; 7002 int save_proto; 7003 7004 if ((hyphen_off = strchr(string, '-')) == NULL) 7005 bpf_error(cstate, "port range '%s' contains no hyphen", string); 7006 7007 /* 7008 * Make sure there are no other hyphens. 7009 * 7010 * XXX - we support named ports, but there are some port names 7011 * in /etc/services that include hyphens, so this would rule 7012 * that out. 7013 */ 7014 if (strchr(hyphen_off + 1, '-') != NULL) 7015 bpf_error(cstate, "port range '%s' contains more than one hyphen", 7016 string); 7017 7018 /* 7019 * Get the length of the first port. 7020 */ 7021 first = string; 7022 first_size = hyphen_off - string; 7023 if (first_size == 0) { 7024 /* Range of "-port", which we don't support. */ 7025 bpf_error(cstate, "port range '%s' has no starting port", string); 7026 } 7027 7028 /* 7029 * Try to convert it to a port. 7030 */ 7031 *port1 = stringtoport(cstate, first, first_size, proto); 7032 save_proto = *proto; 7033 7034 /* 7035 * Get the length of the second port. 7036 */ 7037 second = hyphen_off + 1; 7038 second_size = strlen(second); 7039 if (second_size == 0) { 7040 /* Range of "port-", which we don't support. */ 7041 bpf_error(cstate, "port range '%s' has no ending port", string); 7042 } 7043 7044 /* 7045 * Try to convert it to a port. 7046 */ 7047 *port2 = stringtoport(cstate, second, second_size, proto); 7048 if (*proto != save_proto) 7049 *proto = PROTO_UNDEF; 7050 } 7051 7052 struct block * 7053 gen_scode(compiler_state_t *cstate, const char *name, struct qual q) 7054 { 7055 int proto = q.proto; 7056 int dir = q.dir; 7057 int tproto; 7058 u_char *eaddrp; 7059 u_char eaddr[6]; 7060 bpf_u_int32 mask, addr; 7061 struct addrinfo *res, *res0; 7062 struct sockaddr_in *sin4; 7063 #ifdef INET6 7064 int tproto6; 7065 struct sockaddr_in6 *sin6; 7066 struct in6_addr mask128; 7067 #endif /*INET6*/ 7068 struct block *b, *tmp; 7069 int port, real_proto; 7070 bpf_u_int32 port1, port2; 7071 7072 /* 7073 * Catch errors reported by us and routines below us, and return NULL 7074 * on an error. 7075 */ 7076 if (setjmp(cstate->top_ctx)) 7077 return (NULL); 7078 7079 switch (q.addr) { 7080 7081 case Q_NET: 7082 addr = pcap_nametonetaddr(name); 7083 if (addr == 0) 7084 bpf_error(cstate, "unknown network '%s'", name); 7085 /* Left justify network addr and calculate its network mask */ 7086 mask = 0xffffffff; 7087 while (addr && (addr & 0xff000000) == 0) { 7088 addr <<= 8; 7089 mask <<= 8; 7090 } 7091 return gen_host(cstate, addr, mask, proto, dir, q.addr); 7092 7093 case Q_DEFAULT: 7094 case Q_HOST: 7095 if (proto == Q_LINK) { 7096 switch (cstate->linktype) { 7097 7098 case DLT_EN10MB: 7099 case DLT_NETANALYZER: 7100 case DLT_NETANALYZER_TRANSPARENT: 7101 eaddrp = pcap_ether_hostton(name); 7102 if (eaddrp == NULL) 7103 bpf_error(cstate, 7104 "unknown ether host '%s'", name); 7105 memcpy(eaddr, eaddrp, sizeof(eaddr)); 7106 free(eaddrp); 7107 tmp = gen_prevlinkhdr_check(cstate); 7108 b = gen_ehostop(cstate, eaddr, dir); 7109 if (tmp != NULL) 7110 gen_and(tmp, b); 7111 return b; 7112 7113 case DLT_FDDI: 7114 eaddrp = pcap_ether_hostton(name); 7115 if (eaddrp == NULL) 7116 bpf_error(cstate, 7117 "unknown FDDI host '%s'", name); 7118 memcpy(eaddr, eaddrp, sizeof(eaddr)); 7119 free(eaddrp); 7120 b = gen_fhostop(cstate, eaddr, dir); 7121 return b; 7122 7123 case DLT_IEEE802: 7124 eaddrp = pcap_ether_hostton(name); 7125 if (eaddrp == NULL) 7126 bpf_error(cstate, 7127 "unknown token ring host '%s'", name); 7128 memcpy(eaddr, eaddrp, sizeof(eaddr)); 7129 free(eaddrp); 7130 b = gen_thostop(cstate, eaddr, dir); 7131 return b; 7132 7133 case DLT_IEEE802_11: 7134 case DLT_PRISM_HEADER: 7135 case DLT_IEEE802_11_RADIO_AVS: 7136 case DLT_IEEE802_11_RADIO: 7137 case DLT_PPI: 7138 eaddrp = pcap_ether_hostton(name); 7139 if (eaddrp == NULL) 7140 bpf_error(cstate, 7141 "unknown 802.11 host '%s'", name); 7142 memcpy(eaddr, eaddrp, sizeof(eaddr)); 7143 free(eaddrp); 7144 b = gen_wlanhostop(cstate, eaddr, dir); 7145 return b; 7146 7147 case DLT_IP_OVER_FC: 7148 eaddrp = pcap_ether_hostton(name); 7149 if (eaddrp == NULL) 7150 bpf_error(cstate, 7151 "unknown Fibre Channel host '%s'", name); 7152 memcpy(eaddr, eaddrp, sizeof(eaddr)); 7153 free(eaddrp); 7154 b = gen_ipfchostop(cstate, eaddr, dir); 7155 return b; 7156 } 7157 7158 bpf_error(cstate, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name"); 7159 } else if (proto == Q_DECNET) { 7160 unsigned short dn_addr; 7161 7162 if (!__pcap_nametodnaddr(name, &dn_addr)) { 7163 #ifdef DECNETLIB 7164 bpf_error(cstate, "unknown decnet host name '%s'\n", name); 7165 #else 7166 bpf_error(cstate, "decnet name support not included, '%s' cannot be translated\n", 7167 name); 7168 #endif 7169 } 7170 /* 7171 * I don't think DECNET hosts can be multihomed, so 7172 * there is no need to build up a list of addresses 7173 */ 7174 return (gen_host(cstate, dn_addr, 0, proto, dir, q.addr)); 7175 } else { 7176 #ifdef INET6 7177 memset(&mask128, 0xff, sizeof(mask128)); 7178 #endif 7179 res0 = res = pcap_nametoaddrinfo(name); 7180 if (res == NULL) 7181 bpf_error(cstate, "unknown host '%s'", name); 7182 cstate->ai = res; 7183 b = tmp = NULL; 7184 tproto = proto; 7185 #ifdef INET6 7186 tproto6 = proto; 7187 #endif 7188 if (cstate->off_linktype.constant_part == OFFSET_NOT_SET && 7189 tproto == Q_DEFAULT) { 7190 tproto = Q_IP; 7191 #ifdef INET6 7192 tproto6 = Q_IPV6; 7193 #endif 7194 } 7195 for (res = res0; res; res = res->ai_next) { 7196 switch (res->ai_family) { 7197 case AF_INET: 7198 #ifdef INET6 7199 /* 7200 * Ignore any IPv4 addresses when resolving 7201 * "ip6 host NAME", validate all other proto 7202 * qualifiers in gen_host(). 7203 */ 7204 if (tproto == Q_IPV6) 7205 continue; 7206 #endif 7207 7208 sin4 = (struct sockaddr_in *) 7209 res->ai_addr; 7210 tmp = gen_host(cstate, ntohl(sin4->sin_addr.s_addr), 7211 0xffffffff, tproto, dir, q.addr); 7212 break; 7213 #ifdef INET6 7214 case AF_INET6: 7215 /* 7216 * Ignore any IPv6 addresses when resolving 7217 * "(arp|ip|rarp) host NAME", validate all 7218 * other proto qualifiers in gen_host6(). 7219 */ 7220 if (tproto6 == Q_ARP || tproto6 == Q_IP || 7221 tproto6 == Q_RARP) 7222 continue; 7223 7224 sin6 = (struct sockaddr_in6 *) 7225 res->ai_addr; 7226 tmp = gen_host6(cstate, &sin6->sin6_addr, 7227 &mask128, tproto6, dir, q.addr); 7228 break; 7229 #endif 7230 default: 7231 continue; 7232 } 7233 if (b) 7234 gen_or(b, tmp); 7235 b = tmp; 7236 } 7237 cstate->ai = NULL; 7238 freeaddrinfo(res0); 7239 if (b == NULL) { 7240 bpf_error(cstate, "unknown host '%s'%s", name, 7241 (proto == Q_DEFAULT) 7242 ? "" 7243 : " for specified address family"); 7244 } 7245 return b; 7246 } 7247 7248 case Q_PORT: 7249 if (proto != Q_DEFAULT && 7250 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP) 7251 bpf_error(cstate, "illegal qualifier of 'port'"); 7252 if (pcap_nametoport(name, &port, &real_proto) == 0) 7253 bpf_error(cstate, "unknown port '%s'", name); 7254 if (proto == Q_UDP) { 7255 if (real_proto == IPPROTO_TCP) 7256 bpf_error(cstate, "port '%s' is tcp", name); 7257 else if (real_proto == IPPROTO_SCTP) 7258 bpf_error(cstate, "port '%s' is sctp", name); 7259 else 7260 /* override PROTO_UNDEF */ 7261 real_proto = IPPROTO_UDP; 7262 } 7263 if (proto == Q_TCP) { 7264 if (real_proto == IPPROTO_UDP) 7265 bpf_error(cstate, "port '%s' is udp", name); 7266 7267 else if (real_proto == IPPROTO_SCTP) 7268 bpf_error(cstate, "port '%s' is sctp", name); 7269 else 7270 /* override PROTO_UNDEF */ 7271 real_proto = IPPROTO_TCP; 7272 } 7273 if (proto == Q_SCTP) { 7274 if (real_proto == IPPROTO_UDP) 7275 bpf_error(cstate, "port '%s' is udp", name); 7276 7277 else if (real_proto == IPPROTO_TCP) 7278 bpf_error(cstate, "port '%s' is tcp", name); 7279 else 7280 /* override PROTO_UNDEF */ 7281 real_proto = IPPROTO_SCTP; 7282 } 7283 if (port < 0) 7284 bpf_error(cstate, "illegal port number %d < 0", port); 7285 if (port > 65535) 7286 bpf_error(cstate, "illegal port number %d > 65535", port); 7287 b = gen_port(cstate, port, real_proto, dir); 7288 gen_or(gen_port6(cstate, port, real_proto, dir), b); 7289 return b; 7290 7291 case Q_PORTRANGE: 7292 if (proto != Q_DEFAULT && 7293 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP) 7294 bpf_error(cstate, "illegal qualifier of 'portrange'"); 7295 stringtoportrange(cstate, name, &port1, &port2, &real_proto); 7296 if (proto == Q_UDP) { 7297 if (real_proto == IPPROTO_TCP) 7298 bpf_error(cstate, "port in range '%s' is tcp", name); 7299 else if (real_proto == IPPROTO_SCTP) 7300 bpf_error(cstate, "port in range '%s' is sctp", name); 7301 else 7302 /* override PROTO_UNDEF */ 7303 real_proto = IPPROTO_UDP; 7304 } 7305 if (proto == Q_TCP) { 7306 if (real_proto == IPPROTO_UDP) 7307 bpf_error(cstate, "port in range '%s' is udp", name); 7308 else if (real_proto == IPPROTO_SCTP) 7309 bpf_error(cstate, "port in range '%s' is sctp", name); 7310 else 7311 /* override PROTO_UNDEF */ 7312 real_proto = IPPROTO_TCP; 7313 } 7314 if (proto == Q_SCTP) { 7315 if (real_proto == IPPROTO_UDP) 7316 bpf_error(cstate, "port in range '%s' is udp", name); 7317 else if (real_proto == IPPROTO_TCP) 7318 bpf_error(cstate, "port in range '%s' is tcp", name); 7319 else 7320 /* override PROTO_UNDEF */ 7321 real_proto = IPPROTO_SCTP; 7322 } 7323 if (port1 > 65535) 7324 bpf_error(cstate, "illegal port number %d > 65535", port1); 7325 if (port2 > 65535) 7326 bpf_error(cstate, "illegal port number %d > 65535", port2); 7327 7328 b = gen_portrange(cstate, port1, port2, real_proto, dir); 7329 gen_or(gen_portrange6(cstate, port1, port2, real_proto, dir), b); 7330 return b; 7331 7332 case Q_GATEWAY: 7333 #ifndef INET6 7334 eaddrp = pcap_ether_hostton(name); 7335 if (eaddrp == NULL) 7336 bpf_error(cstate, "unknown ether host: %s", name); 7337 memcpy(eaddr, eaddrp, sizeof(eaddr)); 7338 free(eaddrp); 7339 7340 res = pcap_nametoaddrinfo(name); 7341 cstate->ai = res; 7342 if (res == NULL) 7343 bpf_error(cstate, "unknown host '%s'", name); 7344 b = gen_gateway(cstate, eaddr, res, proto, dir); 7345 cstate->ai = NULL; 7346 freeaddrinfo(res); 7347 if (b == NULL) 7348 bpf_error(cstate, "unknown host '%s'", name); 7349 return b; 7350 #else 7351 bpf_error(cstate, "'gateway' not supported in this configuration"); 7352 #endif /*INET6*/ 7353 7354 case Q_PROTO: 7355 real_proto = lookup_proto(cstate, name, proto); 7356 if (real_proto >= 0) 7357 return gen_proto(cstate, real_proto, proto, dir); 7358 else 7359 bpf_error(cstate, "unknown protocol: %s", name); 7360 7361 #if !defined(NO_PROTOCHAIN) 7362 case Q_PROTOCHAIN: 7363 real_proto = lookup_proto(cstate, name, proto); 7364 if (real_proto >= 0) 7365 return gen_protochain(cstate, real_proto, proto); 7366 else 7367 bpf_error(cstate, "unknown protocol: %s", name); 7368 #endif /* !defined(NO_PROTOCHAIN) */ 7369 7370 case Q_UNDEF: 7371 syntax(cstate); 7372 /*NOTREACHED*/ 7373 } 7374 abort(); 7375 /*NOTREACHED*/ 7376 } 7377 7378 struct block * 7379 gen_mcode(compiler_state_t *cstate, const char *s1, const char *s2, 7380 bpf_u_int32 masklen, struct qual q) 7381 { 7382 register int nlen, mlen; 7383 bpf_u_int32 n, m; 7384 uint64_t m64; 7385 7386 /* 7387 * Catch errors reported by us and routines below us, and return NULL 7388 * on an error. 7389 */ 7390 if (setjmp(cstate->top_ctx)) 7391 return (NULL); 7392 7393 nlen = __pcap_atoin(s1, &n); 7394 if (nlen < 0) 7395 bpf_error(cstate, "invalid IPv4 address '%s'", s1); 7396 /* Promote short ipaddr */ 7397 n <<= 32 - nlen; 7398 7399 if (s2 != NULL) { 7400 mlen = __pcap_atoin(s2, &m); 7401 if (mlen < 0) 7402 bpf_error(cstate, "invalid IPv4 address '%s'", s2); 7403 /* Promote short ipaddr */ 7404 m <<= 32 - mlen; 7405 if ((n & ~m) != 0) 7406 bpf_error(cstate, "non-network bits set in \"%s mask %s\"", 7407 s1, s2); 7408 } else { 7409 /* Convert mask len to mask */ 7410 if (masklen > 32) 7411 bpf_error(cstate, "mask length must be <= 32"); 7412 m64 = UINT64_C(0xffffffff) << (32 - masklen); 7413 m = (bpf_u_int32)m64; 7414 if ((n & ~m) != 0) 7415 bpf_error(cstate, "non-network bits set in \"%s/%d\"", 7416 s1, masklen); 7417 } 7418 7419 switch (q.addr) { 7420 7421 case Q_NET: 7422 return gen_host(cstate, n, m, q.proto, q.dir, q.addr); 7423 7424 default: 7425 bpf_error(cstate, "Mask syntax for networks only"); 7426 /*NOTREACHED*/ 7427 } 7428 /*NOTREACHED*/ 7429 } 7430 7431 struct block * 7432 gen_ncode(compiler_state_t *cstate, const char *s, bpf_u_int32 v, struct qual q) 7433 { 7434 bpf_u_int32 mask; 7435 int proto; 7436 int dir; 7437 register int vlen; 7438 7439 /* 7440 * Catch errors reported by us and routines below us, and return NULL 7441 * on an error. 7442 */ 7443 if (setjmp(cstate->top_ctx)) 7444 return (NULL); 7445 7446 proto = q.proto; 7447 dir = q.dir; 7448 if (s == NULL) 7449 vlen = 32; 7450 else if (q.proto == Q_DECNET) { 7451 vlen = __pcap_atodn(s, &v); 7452 if (vlen == 0) 7453 bpf_error(cstate, "malformed decnet address '%s'", s); 7454 } else { 7455 vlen = __pcap_atoin(s, &v); 7456 if (vlen < 0) 7457 bpf_error(cstate, "invalid IPv4 address '%s'", s); 7458 } 7459 7460 switch (q.addr) { 7461 7462 case Q_DEFAULT: 7463 case Q_HOST: 7464 case Q_NET: 7465 if (proto == Q_DECNET) 7466 return gen_host(cstate, v, 0, proto, dir, q.addr); 7467 else if (proto == Q_LINK) { 7468 bpf_error(cstate, "illegal link layer address"); 7469 } else { 7470 mask = 0xffffffff; 7471 if (s == NULL && q.addr == Q_NET) { 7472 /* Promote short net number */ 7473 while (v && (v & 0xff000000) == 0) { 7474 v <<= 8; 7475 mask <<= 8; 7476 } 7477 } else { 7478 /* Promote short ipaddr */ 7479 v <<= 32 - vlen; 7480 mask <<= 32 - vlen ; 7481 } 7482 return gen_host(cstate, v, mask, proto, dir, q.addr); 7483 } 7484 7485 case Q_PORT: 7486 if (proto == Q_UDP) 7487 proto = IPPROTO_UDP; 7488 else if (proto == Q_TCP) 7489 proto = IPPROTO_TCP; 7490 else if (proto == Q_SCTP) 7491 proto = IPPROTO_SCTP; 7492 else if (proto == Q_DEFAULT) 7493 proto = PROTO_UNDEF; 7494 else 7495 bpf_error(cstate, "illegal qualifier of 'port'"); 7496 7497 if (v > 65535) 7498 bpf_error(cstate, "illegal port number %u > 65535", v); 7499 7500 { 7501 struct block *b; 7502 b = gen_port(cstate, v, proto, dir); 7503 gen_or(gen_port6(cstate, v, proto, dir), b); 7504 return b; 7505 } 7506 7507 case Q_PORTRANGE: 7508 if (proto == Q_UDP) 7509 proto = IPPROTO_UDP; 7510 else if (proto == Q_TCP) 7511 proto = IPPROTO_TCP; 7512 else if (proto == Q_SCTP) 7513 proto = IPPROTO_SCTP; 7514 else if (proto == Q_DEFAULT) 7515 proto = PROTO_UNDEF; 7516 else 7517 bpf_error(cstate, "illegal qualifier of 'portrange'"); 7518 7519 if (v > 65535) 7520 bpf_error(cstate, "illegal port number %u > 65535", v); 7521 7522 { 7523 struct block *b; 7524 b = gen_portrange(cstate, v, v, proto, dir); 7525 gen_or(gen_portrange6(cstate, v, v, proto, dir), b); 7526 return b; 7527 } 7528 7529 case Q_GATEWAY: 7530 bpf_error(cstate, "'gateway' requires a name"); 7531 /*NOTREACHED*/ 7532 7533 case Q_PROTO: 7534 return gen_proto(cstate, v, proto, dir); 7535 7536 #if !defined(NO_PROTOCHAIN) 7537 case Q_PROTOCHAIN: 7538 return gen_protochain(cstate, v, proto); 7539 #endif 7540 7541 case Q_UNDEF: 7542 syntax(cstate); 7543 /*NOTREACHED*/ 7544 7545 default: 7546 abort(); 7547 /*NOTREACHED*/ 7548 } 7549 /*NOTREACHED*/ 7550 } 7551 7552 #ifdef INET6 7553 struct block * 7554 gen_mcode6(compiler_state_t *cstate, const char *s, bpf_u_int32 masklen, 7555 struct qual q) 7556 { 7557 struct addrinfo *res; 7558 struct in6_addr *addr; 7559 struct in6_addr mask; 7560 struct block *b; 7561 bpf_u_int32 a[4], m[4]; /* Same as in gen_hostop6(). */ 7562 7563 /* 7564 * Catch errors reported by us and routines below us, and return NULL 7565 * on an error. 7566 */ 7567 if (setjmp(cstate->top_ctx)) 7568 return (NULL); 7569 7570 res = pcap_nametoaddrinfo(s); 7571 if (!res) 7572 bpf_error(cstate, "invalid ip6 address %s", s); 7573 cstate->ai = res; 7574 if (res->ai_next) 7575 bpf_error(cstate, "%s resolved to multiple address", s); 7576 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr; 7577 7578 if (masklen > sizeof(mask.s6_addr) * 8) 7579 bpf_error(cstate, "mask length must be <= %zu", sizeof(mask.s6_addr) * 8); 7580 memset(&mask, 0, sizeof(mask)); 7581 memset(&mask.s6_addr, 0xff, masklen / 8); 7582 if (masklen % 8) { 7583 mask.s6_addr[masklen / 8] = 7584 (0xff << (8 - masklen % 8)) & 0xff; 7585 } 7586 7587 memcpy(a, addr, sizeof(a)); 7588 memcpy(m, &mask, sizeof(m)); 7589 if ((a[0] & ~m[0]) || (a[1] & ~m[1]) 7590 || (a[2] & ~m[2]) || (a[3] & ~m[3])) { 7591 bpf_error(cstate, "non-network bits set in \"%s/%d\"", s, masklen); 7592 } 7593 7594 switch (q.addr) { 7595 7596 case Q_DEFAULT: 7597 case Q_HOST: 7598 if (masklen != 128) 7599 bpf_error(cstate, "Mask syntax for networks only"); 7600 /* FALLTHROUGH */ 7601 7602 case Q_NET: 7603 b = gen_host6(cstate, addr, &mask, q.proto, q.dir, q.addr); 7604 cstate->ai = NULL; 7605 freeaddrinfo(res); 7606 return b; 7607 7608 default: 7609 bpf_error(cstate, "invalid qualifier against IPv6 address"); 7610 /*NOTREACHED*/ 7611 } 7612 } 7613 #endif /*INET6*/ 7614 7615 struct block * 7616 gen_ecode(compiler_state_t *cstate, const char *s, struct qual q) 7617 { 7618 struct block *b, *tmp; 7619 7620 /* 7621 * Catch errors reported by us and routines below us, and return NULL 7622 * on an error. 7623 */ 7624 if (setjmp(cstate->top_ctx)) 7625 return (NULL); 7626 7627 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) { 7628 /* 7629 * Because the lexer guards the input string format, in this 7630 * context the function returns NULL iff the implicit malloc() 7631 * has failed. 7632 */ 7633 cstate->e = pcap_ether_aton(s); 7634 if (cstate->e == NULL) 7635 bpf_error(cstate, "malloc"); 7636 switch (cstate->linktype) { 7637 case DLT_EN10MB: 7638 case DLT_NETANALYZER: 7639 case DLT_NETANALYZER_TRANSPARENT: 7640 tmp = gen_prevlinkhdr_check(cstate); 7641 b = gen_ehostop(cstate, cstate->e, (int)q.dir); 7642 if (tmp != NULL) 7643 gen_and(tmp, b); 7644 break; 7645 case DLT_FDDI: 7646 b = gen_fhostop(cstate, cstate->e, (int)q.dir); 7647 break; 7648 case DLT_IEEE802: 7649 b = gen_thostop(cstate, cstate->e, (int)q.dir); 7650 break; 7651 case DLT_IEEE802_11: 7652 case DLT_PRISM_HEADER: 7653 case DLT_IEEE802_11_RADIO_AVS: 7654 case DLT_IEEE802_11_RADIO: 7655 case DLT_PPI: 7656 b = gen_wlanhostop(cstate, cstate->e, (int)q.dir); 7657 break; 7658 case DLT_IP_OVER_FC: 7659 b = gen_ipfchostop(cstate, cstate->e, (int)q.dir); 7660 break; 7661 default: 7662 free(cstate->e); 7663 cstate->e = NULL; 7664 bpf_error(cstate, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel"); 7665 /*NOTREACHED*/ 7666 } 7667 free(cstate->e); 7668 cstate->e = NULL; 7669 return (b); 7670 } 7671 bpf_error(cstate, "ethernet address used in non-ether expression"); 7672 /*NOTREACHED*/ 7673 } 7674 7675 void 7676 sappend(struct slist *s0, struct slist *s1) 7677 { 7678 /* 7679 * This is definitely not the best way to do this, but the 7680 * lists will rarely get long. 7681 */ 7682 while (s0->next) 7683 s0 = s0->next; 7684 s0->next = s1; 7685 } 7686 7687 static struct slist * 7688 xfer_to_x(compiler_state_t *cstate, struct arth *a) 7689 { 7690 struct slist *s; 7691 7692 s = new_stmt(cstate, BPF_LDX|BPF_MEM); 7693 s->s.k = a->regno; 7694 return s; 7695 } 7696 7697 static struct slist * 7698 xfer_to_a(compiler_state_t *cstate, struct arth *a) 7699 { 7700 struct slist *s; 7701 7702 s = new_stmt(cstate, BPF_LD|BPF_MEM); 7703 s->s.k = a->regno; 7704 return s; 7705 } 7706 7707 /* 7708 * Modify "index" to use the value stored into its register as an 7709 * offset relative to the beginning of the header for the protocol 7710 * "proto", and allocate a register and put an item "size" bytes long 7711 * (1, 2, or 4) at that offset into that register, making it the register 7712 * for "index". 7713 */ 7714 static struct arth * 7715 gen_load_internal(compiler_state_t *cstate, int proto, struct arth *inst, 7716 bpf_u_int32 size) 7717 { 7718 int size_code; 7719 struct slist *s, *tmp; 7720 struct block *b; 7721 int regno = alloc_reg(cstate); 7722 7723 free_reg(cstate, inst->regno); 7724 switch (size) { 7725 7726 default: 7727 bpf_error(cstate, "data size must be 1, 2, or 4"); 7728 /*NOTREACHED*/ 7729 7730 case 1: 7731 size_code = BPF_B; 7732 break; 7733 7734 case 2: 7735 size_code = BPF_H; 7736 break; 7737 7738 case 4: 7739 size_code = BPF_W; 7740 break; 7741 } 7742 switch (proto) { 7743 default: 7744 bpf_error(cstate, "unsupported index operation"); 7745 7746 case Q_RADIO: 7747 /* 7748 * The offset is relative to the beginning of the packet 7749 * data, if we have a radio header. (If we don't, this 7750 * is an error.) 7751 */ 7752 if (cstate->linktype != DLT_IEEE802_11_RADIO_AVS && 7753 cstate->linktype != DLT_IEEE802_11_RADIO && 7754 cstate->linktype != DLT_PRISM_HEADER) 7755 bpf_error(cstate, "radio information not present in capture"); 7756 7757 /* 7758 * Load into the X register the offset computed into the 7759 * register specified by "index". 7760 */ 7761 s = xfer_to_x(cstate, inst); 7762 7763 /* 7764 * Load the item at that offset. 7765 */ 7766 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); 7767 sappend(s, tmp); 7768 sappend(inst->s, s); 7769 break; 7770 7771 case Q_LINK: 7772 /* 7773 * The offset is relative to the beginning of 7774 * the link-layer header. 7775 * 7776 * XXX - what about ATM LANE? Should the index be 7777 * relative to the beginning of the AAL5 frame, so 7778 * that 0 refers to the beginning of the LE Control 7779 * field, or relative to the beginning of the LAN 7780 * frame, so that 0 refers, for Ethernet LANE, to 7781 * the beginning of the destination address? 7782 */ 7783 s = gen_abs_offset_varpart(cstate, &cstate->off_linkhdr); 7784 7785 /* 7786 * If "s" is non-null, it has code to arrange that the 7787 * X register contains the length of the prefix preceding 7788 * the link-layer header. Add to it the offset computed 7789 * into the register specified by "index", and move that 7790 * into the X register. Otherwise, just load into the X 7791 * register the offset computed into the register specified 7792 * by "index". 7793 */ 7794 if (s != NULL) { 7795 sappend(s, xfer_to_a(cstate, inst)); 7796 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); 7797 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); 7798 } else 7799 s = xfer_to_x(cstate, inst); 7800 7801 /* 7802 * Load the item at the sum of the offset we've put in the 7803 * X register and the offset of the start of the link 7804 * layer header (which is 0 if the radio header is 7805 * variable-length; that header length is what we put 7806 * into the X register and then added to the index). 7807 */ 7808 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); 7809 tmp->s.k = cstate->off_linkhdr.constant_part; 7810 sappend(s, tmp); 7811 sappend(inst->s, s); 7812 break; 7813 7814 case Q_IP: 7815 case Q_ARP: 7816 case Q_RARP: 7817 case Q_ATALK: 7818 case Q_DECNET: 7819 case Q_SCA: 7820 case Q_LAT: 7821 case Q_MOPRC: 7822 case Q_MOPDL: 7823 case Q_IPV6: 7824 /* 7825 * The offset is relative to the beginning of 7826 * the network-layer header. 7827 * XXX - are there any cases where we want 7828 * cstate->off_nl_nosnap? 7829 */ 7830 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); 7831 7832 /* 7833 * If "s" is non-null, it has code to arrange that the 7834 * X register contains the variable part of the offset 7835 * of the link-layer payload. Add to it the offset 7836 * computed into the register specified by "index", 7837 * and move that into the X register. Otherwise, just 7838 * load into the X register the offset computed into 7839 * the register specified by "index". 7840 */ 7841 if (s != NULL) { 7842 sappend(s, xfer_to_a(cstate, inst)); 7843 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); 7844 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); 7845 } else 7846 s = xfer_to_x(cstate, inst); 7847 7848 /* 7849 * Load the item at the sum of the offset we've put in the 7850 * X register, the offset of the start of the network 7851 * layer header from the beginning of the link-layer 7852 * payload, and the constant part of the offset of the 7853 * start of the link-layer payload. 7854 */ 7855 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); 7856 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; 7857 sappend(s, tmp); 7858 sappend(inst->s, s); 7859 7860 /* 7861 * Do the computation only if the packet contains 7862 * the protocol in question. 7863 */ 7864 b = gen_proto_abbrev_internal(cstate, proto); 7865 if (inst->b) 7866 gen_and(inst->b, b); 7867 inst->b = b; 7868 break; 7869 7870 case Q_SCTP: 7871 case Q_TCP: 7872 case Q_UDP: 7873 case Q_ICMP: 7874 case Q_IGMP: 7875 case Q_IGRP: 7876 case Q_PIM: 7877 case Q_VRRP: 7878 case Q_CARP: 7879 /* 7880 * The offset is relative to the beginning of 7881 * the transport-layer header. 7882 * 7883 * Load the X register with the length of the IPv4 header 7884 * (plus the offset of the link-layer header, if it's 7885 * a variable-length header), in bytes. 7886 * 7887 * XXX - are there any cases where we want 7888 * cstate->off_nl_nosnap? 7889 * XXX - we should, if we're built with 7890 * IPv6 support, generate code to load either 7891 * IPv4, IPv6, or both, as appropriate. 7892 */ 7893 s = gen_loadx_iphdrlen(cstate); 7894 7895 /* 7896 * The X register now contains the sum of the variable 7897 * part of the offset of the link-layer payload and the 7898 * length of the network-layer header. 7899 * 7900 * Load into the A register the offset relative to 7901 * the beginning of the transport layer header, 7902 * add the X register to that, move that to the 7903 * X register, and load with an offset from the 7904 * X register equal to the sum of the constant part of 7905 * the offset of the link-layer payload and the offset, 7906 * relative to the beginning of the link-layer payload, 7907 * of the network-layer header. 7908 */ 7909 sappend(s, xfer_to_a(cstate, inst)); 7910 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); 7911 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); 7912 sappend(s, tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code)); 7913 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl; 7914 sappend(inst->s, s); 7915 7916 /* 7917 * Do the computation only if the packet contains 7918 * the protocol in question - which is true only 7919 * if this is an IP datagram and is the first or 7920 * only fragment of that datagram. 7921 */ 7922 gen_and(gen_proto_abbrev_internal(cstate, proto), b = gen_ipfrag(cstate)); 7923 if (inst->b) 7924 gen_and(inst->b, b); 7925 gen_and(gen_proto_abbrev_internal(cstate, Q_IP), b); 7926 inst->b = b; 7927 break; 7928 case Q_ICMPV6: 7929 /* 7930 * Do the computation only if the packet contains 7931 * the protocol in question. 7932 */ 7933 b = gen_proto_abbrev_internal(cstate, Q_IPV6); 7934 if (inst->b) 7935 gen_and(inst->b, b); 7936 inst->b = b; 7937 7938 /* 7939 * Check if we have an icmp6 next header 7940 */ 7941 b = gen_cmp(cstate, OR_LINKPL, 6, BPF_B, 58); 7942 if (inst->b) 7943 gen_and(inst->b, b); 7944 inst->b = b; 7945 7946 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); 7947 /* 7948 * If "s" is non-null, it has code to arrange that the 7949 * X register contains the variable part of the offset 7950 * of the link-layer payload. Add to it the offset 7951 * computed into the register specified by "index", 7952 * and move that into the X register. Otherwise, just 7953 * load into the X register the offset computed into 7954 * the register specified by "index". 7955 */ 7956 if (s != NULL) { 7957 sappend(s, xfer_to_a(cstate, inst)); 7958 sappend(s, new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X)); 7959 sappend(s, new_stmt(cstate, BPF_MISC|BPF_TAX)); 7960 } else 7961 s = xfer_to_x(cstate, inst); 7962 7963 /* 7964 * Load the item at the sum of the offset we've put in the 7965 * X register, the offset of the start of the network 7966 * layer header from the beginning of the link-layer 7967 * payload, and the constant part of the offset of the 7968 * start of the link-layer payload. 7969 */ 7970 tmp = new_stmt(cstate, BPF_LD|BPF_IND|size_code); 7971 tmp->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 40; 7972 7973 sappend(s, tmp); 7974 sappend(inst->s, s); 7975 7976 break; 7977 } 7978 inst->regno = regno; 7979 s = new_stmt(cstate, BPF_ST); 7980 s->s.k = regno; 7981 sappend(inst->s, s); 7982 7983 return inst; 7984 } 7985 7986 struct arth * 7987 gen_load(compiler_state_t *cstate, int proto, struct arth *inst, 7988 bpf_u_int32 size) 7989 { 7990 /* 7991 * Catch errors reported by us and routines below us, and return NULL 7992 * on an error. 7993 */ 7994 if (setjmp(cstate->top_ctx)) 7995 return (NULL); 7996 7997 return gen_load_internal(cstate, proto, inst, size); 7998 } 7999 8000 static struct block * 8001 gen_relation_internal(compiler_state_t *cstate, int code, struct arth *a0, 8002 struct arth *a1, int reversed) 8003 { 8004 struct slist *s0, *s1, *s2; 8005 struct block *b, *tmp; 8006 8007 s0 = xfer_to_x(cstate, a1); 8008 s1 = xfer_to_a(cstate, a0); 8009 if (code == BPF_JEQ) { 8010 s2 = new_stmt(cstate, BPF_ALU|BPF_SUB|BPF_X); 8011 b = new_block(cstate, JMP(code)); 8012 sappend(s1, s2); 8013 } 8014 else 8015 b = new_block(cstate, BPF_JMP|code|BPF_X); 8016 if (reversed) 8017 gen_not(b); 8018 8019 sappend(s0, s1); 8020 sappend(a1->s, s0); 8021 sappend(a0->s, a1->s); 8022 8023 b->stmts = a0->s; 8024 8025 free_reg(cstate, a0->regno); 8026 free_reg(cstate, a1->regno); 8027 8028 /* 'and' together protocol checks */ 8029 if (a0->b) { 8030 if (a1->b) { 8031 gen_and(a0->b, tmp = a1->b); 8032 } 8033 else 8034 tmp = a0->b; 8035 } else 8036 tmp = a1->b; 8037 8038 if (tmp) 8039 gen_and(tmp, b); 8040 8041 return b; 8042 } 8043 8044 struct block * 8045 gen_relation(compiler_state_t *cstate, int code, struct arth *a0, 8046 struct arth *a1, int reversed) 8047 { 8048 /* 8049 * Catch errors reported by us and routines below us, and return NULL 8050 * on an error. 8051 */ 8052 if (setjmp(cstate->top_ctx)) 8053 return (NULL); 8054 8055 return gen_relation_internal(cstate, code, a0, a1, reversed); 8056 } 8057 8058 struct arth * 8059 gen_loadlen(compiler_state_t *cstate) 8060 { 8061 int regno; 8062 struct arth *a; 8063 struct slist *s; 8064 8065 /* 8066 * Catch errors reported by us and routines below us, and return NULL 8067 * on an error. 8068 */ 8069 if (setjmp(cstate->top_ctx)) 8070 return (NULL); 8071 8072 regno = alloc_reg(cstate); 8073 a = (struct arth *)newchunk(cstate, sizeof(*a)); 8074 s = new_stmt(cstate, BPF_LD|BPF_LEN); 8075 s->next = new_stmt(cstate, BPF_ST); 8076 s->next->s.k = regno; 8077 a->s = s; 8078 a->regno = regno; 8079 8080 return a; 8081 } 8082 8083 static struct arth * 8084 gen_loadi_internal(compiler_state_t *cstate, bpf_u_int32 val) 8085 { 8086 struct arth *a; 8087 struct slist *s; 8088 int reg; 8089 8090 a = (struct arth *)newchunk(cstate, sizeof(*a)); 8091 8092 reg = alloc_reg(cstate); 8093 8094 s = new_stmt(cstate, BPF_LD|BPF_IMM); 8095 s->s.k = val; 8096 s->next = new_stmt(cstate, BPF_ST); 8097 s->next->s.k = reg; 8098 a->s = s; 8099 a->regno = reg; 8100 8101 return a; 8102 } 8103 8104 struct arth * 8105 gen_loadi(compiler_state_t *cstate, bpf_u_int32 val) 8106 { 8107 /* 8108 * Catch errors reported by us and routines below us, and return NULL 8109 * on an error. 8110 */ 8111 if (setjmp(cstate->top_ctx)) 8112 return (NULL); 8113 8114 return gen_loadi_internal(cstate, val); 8115 } 8116 8117 /* 8118 * The a_arg dance is to avoid annoying whining by compilers that 8119 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*? 8120 * It's not *used* after setjmp returns. 8121 */ 8122 struct arth * 8123 gen_neg(compiler_state_t *cstate, struct arth *a_arg) 8124 { 8125 struct arth * volatile a = a_arg; 8126 struct slist *s; 8127 8128 /* 8129 * Catch errors reported by us and routines below us, and return NULL 8130 * on an error. 8131 */ 8132 if (setjmp(cstate->top_ctx)) 8133 return (NULL); 8134 8135 s = xfer_to_a(cstate, a); 8136 sappend(a->s, s); 8137 s = new_stmt(cstate, BPF_ALU|BPF_NEG); 8138 s->s.k = 0; 8139 sappend(a->s, s); 8140 s = new_stmt(cstate, BPF_ST); 8141 s->s.k = a->regno; 8142 sappend(a->s, s); 8143 8144 return a; 8145 } 8146 8147 /* 8148 * The a0_arg dance is to avoid annoying whining by compilers that 8149 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*? 8150 * It's not *used* after setjmp returns. 8151 */ 8152 struct arth * 8153 gen_arth(compiler_state_t *cstate, int code, struct arth *a0_arg, 8154 struct arth *a1) 8155 { 8156 struct arth * volatile a0 = a0_arg; 8157 struct slist *s0, *s1, *s2; 8158 8159 /* 8160 * Catch errors reported by us and routines below us, and return NULL 8161 * on an error. 8162 */ 8163 if (setjmp(cstate->top_ctx)) 8164 return (NULL); 8165 8166 /* 8167 * Disallow division by, or modulus by, zero; we do this here 8168 * so that it gets done even if the optimizer is disabled. 8169 * 8170 * Also disallow shifts by a value greater than 31; we do this 8171 * here, for the same reason. 8172 */ 8173 if (code == BPF_DIV) { 8174 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0) 8175 bpf_error(cstate, "division by zero"); 8176 } else if (code == BPF_MOD) { 8177 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k == 0) 8178 bpf_error(cstate, "modulus by zero"); 8179 } else if (code == BPF_LSH || code == BPF_RSH) { 8180 if (a1->s->s.code == (BPF_LD|BPF_IMM) && a1->s->s.k > 31) 8181 bpf_error(cstate, "shift by more than 31 bits"); 8182 } 8183 s0 = xfer_to_x(cstate, a1); 8184 s1 = xfer_to_a(cstate, a0); 8185 s2 = new_stmt(cstate, BPF_ALU|BPF_X|code); 8186 8187 sappend(s1, s2); 8188 sappend(s0, s1); 8189 sappend(a1->s, s0); 8190 sappend(a0->s, a1->s); 8191 8192 free_reg(cstate, a0->regno); 8193 free_reg(cstate, a1->regno); 8194 8195 s0 = new_stmt(cstate, BPF_ST); 8196 a0->regno = s0->s.k = alloc_reg(cstate); 8197 sappend(a0->s, s0); 8198 8199 return a0; 8200 } 8201 8202 /* 8203 * Initialize the table of used registers and the current register. 8204 */ 8205 static void 8206 init_regs(compiler_state_t *cstate) 8207 { 8208 cstate->curreg = 0; 8209 memset(cstate->regused, 0, sizeof cstate->regused); 8210 } 8211 8212 /* 8213 * Return the next free register. 8214 */ 8215 static int 8216 alloc_reg(compiler_state_t *cstate) 8217 { 8218 int n = BPF_MEMWORDS; 8219 8220 while (--n >= 0) { 8221 if (cstate->regused[cstate->curreg]) 8222 cstate->curreg = (cstate->curreg + 1) % BPF_MEMWORDS; 8223 else { 8224 cstate->regused[cstate->curreg] = 1; 8225 return cstate->curreg; 8226 } 8227 } 8228 bpf_error(cstate, "too many registers needed to evaluate expression"); 8229 /*NOTREACHED*/ 8230 } 8231 8232 /* 8233 * Return a register to the table so it can 8234 * be used later. 8235 */ 8236 static void 8237 free_reg(compiler_state_t *cstate, int n) 8238 { 8239 cstate->regused[n] = 0; 8240 } 8241 8242 static struct block * 8243 gen_len(compiler_state_t *cstate, int jmp, int n) 8244 { 8245 struct slist *s; 8246 struct block *b; 8247 8248 s = new_stmt(cstate, BPF_LD|BPF_LEN); 8249 b = new_block(cstate, JMP(jmp)); 8250 b->stmts = s; 8251 b->s.k = n; 8252 8253 return b; 8254 } 8255 8256 struct block * 8257 gen_greater(compiler_state_t *cstate, int n) 8258 { 8259 /* 8260 * Catch errors reported by us and routines below us, and return NULL 8261 * on an error. 8262 */ 8263 if (setjmp(cstate->top_ctx)) 8264 return (NULL); 8265 8266 return gen_len(cstate, BPF_JGE, n); 8267 } 8268 8269 /* 8270 * Actually, this is less than or equal. 8271 */ 8272 struct block * 8273 gen_less(compiler_state_t *cstate, int n) 8274 { 8275 struct block *b; 8276 8277 /* 8278 * Catch errors reported by us and routines below us, and return NULL 8279 * on an error. 8280 */ 8281 if (setjmp(cstate->top_ctx)) 8282 return (NULL); 8283 8284 b = gen_len(cstate, BPF_JGT, n); 8285 gen_not(b); 8286 8287 return b; 8288 } 8289 8290 /* 8291 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to 8292 * the beginning of the link-layer header. 8293 * XXX - that means you can't test values in the radiotap header, but 8294 * as that header is difficult if not impossible to parse generally 8295 * without a loop, that might not be a severe problem. A new keyword 8296 * "radio" could be added for that, although what you'd really want 8297 * would be a way of testing particular radio header values, which 8298 * would generate code appropriate to the radio header in question. 8299 */ 8300 struct block * 8301 gen_byteop(compiler_state_t *cstate, int op, int idx, bpf_u_int32 val) 8302 { 8303 struct block *b; 8304 struct slist *s; 8305 8306 /* 8307 * Catch errors reported by us and routines below us, and return NULL 8308 * on an error. 8309 */ 8310 if (setjmp(cstate->top_ctx)) 8311 return (NULL); 8312 8313 switch (op) { 8314 default: 8315 abort(); 8316 8317 case '=': 8318 return gen_cmp(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); 8319 8320 case '<': 8321 b = gen_cmp_lt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); 8322 return b; 8323 8324 case '>': 8325 b = gen_cmp_gt(cstate, OR_LINKHDR, (u_int)idx, BPF_B, val); 8326 return b; 8327 8328 case '|': 8329 s = new_stmt(cstate, BPF_ALU|BPF_OR|BPF_K); 8330 break; 8331 8332 case '&': 8333 s = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); 8334 break; 8335 } 8336 s->s.k = val; 8337 b = new_block(cstate, JMP(BPF_JEQ)); 8338 b->stmts = s; 8339 gen_not(b); 8340 8341 return b; 8342 } 8343 8344 struct block * 8345 gen_broadcast(compiler_state_t *cstate, int proto) 8346 { 8347 bpf_u_int32 hostmask; 8348 struct block *b0, *b1, *b2; 8349 static const u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 8350 8351 /* 8352 * Catch errors reported by us and routines below us, and return NULL 8353 * on an error. 8354 */ 8355 if (setjmp(cstate->top_ctx)) 8356 return (NULL); 8357 8358 switch (proto) { 8359 8360 case Q_DEFAULT: 8361 case Q_LINK: 8362 switch (cstate->linktype) { 8363 case DLT_ARCNET: 8364 case DLT_ARCNET_LINUX: 8365 // ARCnet broadcast is [8-bit] destination address 0. 8366 return gen_ahostop(cstate, 0, Q_DST); 8367 case DLT_EN10MB: 8368 case DLT_NETANALYZER: 8369 case DLT_NETANALYZER_TRANSPARENT: 8370 b1 = gen_prevlinkhdr_check(cstate); 8371 b0 = gen_ehostop(cstate, ebroadcast, Q_DST); 8372 if (b1 != NULL) 8373 gen_and(b1, b0); 8374 return b0; 8375 case DLT_FDDI: 8376 return gen_fhostop(cstate, ebroadcast, Q_DST); 8377 case DLT_IEEE802: 8378 return gen_thostop(cstate, ebroadcast, Q_DST); 8379 case DLT_IEEE802_11: 8380 case DLT_PRISM_HEADER: 8381 case DLT_IEEE802_11_RADIO_AVS: 8382 case DLT_IEEE802_11_RADIO: 8383 case DLT_PPI: 8384 return gen_wlanhostop(cstate, ebroadcast, Q_DST); 8385 case DLT_IP_OVER_FC: 8386 return gen_ipfchostop(cstate, ebroadcast, Q_DST); 8387 default: 8388 bpf_error(cstate, "not a broadcast link"); 8389 } 8390 /*NOTREACHED*/ 8391 8392 case Q_IP: 8393 /* 8394 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff) 8395 * as an indication that we don't know the netmask, and fail 8396 * in that case. 8397 */ 8398 if (cstate->netmask == PCAP_NETMASK_UNKNOWN) 8399 bpf_error(cstate, "netmask not known, so 'ip broadcast' not supported"); 8400 b0 = gen_linktype(cstate, ETHERTYPE_IP); 8401 hostmask = ~cstate->netmask; 8402 b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 0, hostmask); 8403 b2 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_W, 8404 ~0 & hostmask, hostmask); 8405 gen_or(b1, b2); 8406 gen_and(b0, b2); 8407 return b2; 8408 } 8409 bpf_error(cstate, "only link-layer/IP broadcast filters supported"); 8410 /*NOTREACHED*/ 8411 } 8412 8413 /* 8414 * Generate code to test the low-order bit of a MAC address (that's 8415 * the bottom bit of the *first* byte). 8416 */ 8417 static struct block * 8418 gen_mac_multicast(compiler_state_t *cstate, int offset) 8419 { 8420 register struct block *b0; 8421 register struct slist *s; 8422 8423 /* link[offset] & 1 != 0 */ 8424 s = gen_load_a(cstate, OR_LINKHDR, offset, BPF_B); 8425 b0 = new_block(cstate, JMP(BPF_JSET)); 8426 b0->s.k = 1; 8427 b0->stmts = s; 8428 return b0; 8429 } 8430 8431 struct block * 8432 gen_multicast(compiler_state_t *cstate, int proto) 8433 { 8434 register struct block *b0, *b1, *b2; 8435 register struct slist *s; 8436 8437 /* 8438 * Catch errors reported by us and routines below us, and return NULL 8439 * on an error. 8440 */ 8441 if (setjmp(cstate->top_ctx)) 8442 return (NULL); 8443 8444 switch (proto) { 8445 8446 case Q_DEFAULT: 8447 case Q_LINK: 8448 switch (cstate->linktype) { 8449 case DLT_ARCNET: 8450 case DLT_ARCNET_LINUX: 8451 // ARCnet multicast is the same as broadcast. 8452 return gen_ahostop(cstate, 0, Q_DST); 8453 case DLT_EN10MB: 8454 case DLT_NETANALYZER: 8455 case DLT_NETANALYZER_TRANSPARENT: 8456 b1 = gen_prevlinkhdr_check(cstate); 8457 /* ether[0] & 1 != 0 */ 8458 b0 = gen_mac_multicast(cstate, 0); 8459 if (b1 != NULL) 8460 gen_and(b1, b0); 8461 return b0; 8462 case DLT_FDDI: 8463 /* 8464 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX 8465 * 8466 * XXX - was that referring to bit-order issues? 8467 */ 8468 /* fddi[1] & 1 != 0 */ 8469 return gen_mac_multicast(cstate, 1); 8470 case DLT_IEEE802: 8471 /* tr[2] & 1 != 0 */ 8472 return gen_mac_multicast(cstate, 2); 8473 case DLT_IEEE802_11: 8474 case DLT_PRISM_HEADER: 8475 case DLT_IEEE802_11_RADIO_AVS: 8476 case DLT_IEEE802_11_RADIO: 8477 case DLT_PPI: 8478 /* 8479 * Oh, yuk. 8480 * 8481 * For control frames, there is no DA. 8482 * 8483 * For management frames, DA is at an 8484 * offset of 4 from the beginning of 8485 * the packet. 8486 * 8487 * For data frames, DA is at an offset 8488 * of 4 from the beginning of the packet 8489 * if To DS is clear and at an offset of 8490 * 16 from the beginning of the packet 8491 * if To DS is set. 8492 */ 8493 8494 /* 8495 * Generate the tests to be done for data frames. 8496 * 8497 * First, check for To DS set, i.e. "link[1] & 0x01". 8498 */ 8499 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); 8500 b1 = new_block(cstate, JMP(BPF_JSET)); 8501 b1->s.k = 0x01; /* To DS */ 8502 b1->stmts = s; 8503 8504 /* 8505 * If To DS is set, the DA is at 16. 8506 */ 8507 b0 = gen_mac_multicast(cstate, 16); 8508 gen_and(b1, b0); 8509 8510 /* 8511 * Now, check for To DS not set, i.e. check 8512 * "!(link[1] & 0x01)". 8513 */ 8514 s = gen_load_a(cstate, OR_LINKHDR, 1, BPF_B); 8515 b2 = new_block(cstate, JMP(BPF_JSET)); 8516 b2->s.k = 0x01; /* To DS */ 8517 b2->stmts = s; 8518 gen_not(b2); 8519 8520 /* 8521 * If To DS is not set, the DA is at 4. 8522 */ 8523 b1 = gen_mac_multicast(cstate, 4); 8524 gen_and(b2, b1); 8525 8526 /* 8527 * Now OR together the last two checks. That gives 8528 * the complete set of checks for data frames. 8529 */ 8530 gen_or(b1, b0); 8531 8532 /* 8533 * Now check for a data frame. 8534 * I.e, check "link[0] & 0x08". 8535 */ 8536 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 8537 b1 = new_block(cstate, JMP(BPF_JSET)); 8538 b1->s.k = 0x08; 8539 b1->stmts = s; 8540 8541 /* 8542 * AND that with the checks done for data frames. 8543 */ 8544 gen_and(b1, b0); 8545 8546 /* 8547 * If the high-order bit of the type value is 0, this 8548 * is a management frame. 8549 * I.e, check "!(link[0] & 0x08)". 8550 */ 8551 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 8552 b2 = new_block(cstate, JMP(BPF_JSET)); 8553 b2->s.k = 0x08; 8554 b2->stmts = s; 8555 gen_not(b2); 8556 8557 /* 8558 * For management frames, the DA is at 4. 8559 */ 8560 b1 = gen_mac_multicast(cstate, 4); 8561 gen_and(b2, b1); 8562 8563 /* 8564 * OR that with the checks done for data frames. 8565 * That gives the checks done for management and 8566 * data frames. 8567 */ 8568 gen_or(b1, b0); 8569 8570 /* 8571 * If the low-order bit of the type value is 1, 8572 * this is either a control frame or a frame 8573 * with a reserved type, and thus not a 8574 * frame with an SA. 8575 * 8576 * I.e., check "!(link[0] & 0x04)". 8577 */ 8578 s = gen_load_a(cstate, OR_LINKHDR, 0, BPF_B); 8579 b1 = new_block(cstate, JMP(BPF_JSET)); 8580 b1->s.k = 0x04; 8581 b1->stmts = s; 8582 gen_not(b1); 8583 8584 /* 8585 * AND that with the checks for data and management 8586 * frames. 8587 */ 8588 gen_and(b1, b0); 8589 return b0; 8590 case DLT_IP_OVER_FC: 8591 b0 = gen_mac_multicast(cstate, 2); 8592 return b0; 8593 default: 8594 break; 8595 } 8596 /* Link not known to support multicasts */ 8597 break; 8598 8599 case Q_IP: 8600 b0 = gen_linktype(cstate, ETHERTYPE_IP); 8601 8602 /* 8603 * Compare address with 224.0.0.0/4 8604 */ 8605 b1 = gen_mcmp(cstate, OR_LINKPL, 16, BPF_B, 0xe0, 0xf0); 8606 8607 gen_and(b0, b1); 8608 return b1; 8609 8610 case Q_IPV6: 8611 b0 = gen_linktype(cstate, ETHERTYPE_IPV6); 8612 b1 = gen_cmp(cstate, OR_LINKPL, 24, BPF_B, 255); 8613 gen_and(b0, b1); 8614 return b1; 8615 } 8616 bpf_error(cstate, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel"); 8617 /*NOTREACHED*/ 8618 } 8619 8620 struct block * 8621 gen_ifindex(compiler_state_t *cstate, int ifindex) 8622 { 8623 register struct block *b0; 8624 8625 /* 8626 * Catch errors reported by us and routines below us, and return NULL 8627 * on an error. 8628 */ 8629 if (setjmp(cstate->top_ctx)) 8630 return (NULL); 8631 8632 /* 8633 * Only some data link types support ifindex qualifiers. 8634 */ 8635 switch (cstate->linktype) { 8636 case DLT_LINUX_SLL2: 8637 /* match packets on this interface */ 8638 b0 = gen_cmp(cstate, OR_LINKHDR, 4, BPF_W, ifindex); 8639 break; 8640 default: 8641 #if defined(__linux__) 8642 /* 8643 * This is Linux; we require PF_PACKET support. 8644 * If this is a *live* capture, we can look at 8645 * special meta-data in the filter expression; 8646 * if it's a savefile, we can't. 8647 */ 8648 if (cstate->bpf_pcap->rfile != NULL) { 8649 /* We have a FILE *, so this is a savefile */ 8650 bpf_error(cstate, "ifindex not supported on %s when reading savefiles", 8651 pcap_datalink_val_to_description_or_dlt(cstate->linktype)); 8652 /*NOTREACHED*/ 8653 } 8654 /* match ifindex */ 8655 b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_IFINDEX, BPF_W, 8656 ifindex); 8657 #else /* defined(__linux__) */ 8658 bpf_error(cstate, "ifindex not supported on %s", 8659 pcap_datalink_val_to_description_or_dlt(cstate->linktype)); 8660 /*NOTREACHED*/ 8661 #endif /* defined(__linux__) */ 8662 } 8663 return (b0); 8664 } 8665 8666 /* 8667 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic. 8668 * Outbound traffic is sent by this machine, while inbound traffic is 8669 * sent by a remote machine (and may include packets destined for a 8670 * unicast or multicast link-layer address we are not subscribing to). 8671 * These are the same definitions implemented by pcap_setdirection(). 8672 * Capturing only unicast traffic destined for this host is probably 8673 * better accomplished using a higher-layer filter. 8674 */ 8675 struct block * 8676 gen_inbound(compiler_state_t *cstate, int dir) 8677 { 8678 register struct block *b0; 8679 8680 /* 8681 * Catch errors reported by us and routines below us, and return NULL 8682 * on an error. 8683 */ 8684 if (setjmp(cstate->top_ctx)) 8685 return (NULL); 8686 8687 /* 8688 * Only some data link types support inbound/outbound qualifiers. 8689 */ 8690 switch (cstate->linktype) { 8691 case DLT_SLIP: 8692 b0 = gen_relation_internal(cstate, BPF_JEQ, 8693 gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1), 8694 gen_loadi_internal(cstate, 0), 8695 dir); 8696 break; 8697 8698 case DLT_IPNET: 8699 if (dir) { 8700 /* match outgoing packets */ 8701 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_OUTBOUND); 8702 } else { 8703 /* match incoming packets */ 8704 b0 = gen_cmp(cstate, OR_LINKHDR, 2, BPF_H, IPNET_INBOUND); 8705 } 8706 break; 8707 8708 case DLT_LINUX_SLL: 8709 /* match outgoing packets */ 8710 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_H, LINUX_SLL_OUTGOING); 8711 if (!dir) { 8712 /* to filter on inbound traffic, invert the match */ 8713 gen_not(b0); 8714 } 8715 break; 8716 8717 case DLT_LINUX_SLL2: 8718 /* match outgoing packets */ 8719 b0 = gen_cmp(cstate, OR_LINKHDR, 10, BPF_B, LINUX_SLL_OUTGOING); 8720 if (!dir) { 8721 /* to filter on inbound traffic, invert the match */ 8722 gen_not(b0); 8723 } 8724 break; 8725 8726 case DLT_PFLOG: 8727 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, dir), BPF_B, 8728 ((dir == 0) ? PF_IN : PF_OUT)); 8729 break; 8730 8731 case DLT_PPP_PPPD: 8732 if (dir) { 8733 /* match outgoing packets */ 8734 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_OUT); 8735 } else { 8736 /* match incoming packets */ 8737 b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, PPP_PPPD_IN); 8738 } 8739 break; 8740 8741 case DLT_JUNIPER_MFR: 8742 case DLT_JUNIPER_MLFR: 8743 case DLT_JUNIPER_MLPPP: 8744 case DLT_JUNIPER_ATM1: 8745 case DLT_JUNIPER_ATM2: 8746 case DLT_JUNIPER_PPPOE: 8747 case DLT_JUNIPER_PPPOE_ATM: 8748 case DLT_JUNIPER_GGSN: 8749 case DLT_JUNIPER_ES: 8750 case DLT_JUNIPER_MONITOR: 8751 case DLT_JUNIPER_SERVICES: 8752 case DLT_JUNIPER_ETHER: 8753 case DLT_JUNIPER_PPP: 8754 case DLT_JUNIPER_FRELAY: 8755 case DLT_JUNIPER_CHDLC: 8756 case DLT_JUNIPER_VP: 8757 case DLT_JUNIPER_ST: 8758 case DLT_JUNIPER_ISM: 8759 case DLT_JUNIPER_VS: 8760 case DLT_JUNIPER_SRX_E2E: 8761 case DLT_JUNIPER_FIBRECHANNEL: 8762 case DLT_JUNIPER_ATM_CEMIC: 8763 8764 /* juniper flags (including direction) are stored 8765 * the byte after the 3-byte magic number */ 8766 if (dir) { 8767 /* match outgoing packets */ 8768 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 0, 0x01); 8769 } else { 8770 /* match incoming packets */ 8771 b0 = gen_mcmp(cstate, OR_LINKHDR, 3, BPF_B, 1, 0x01); 8772 } 8773 break; 8774 8775 default: 8776 /* 8777 * If we have packet meta-data indicating a direction, 8778 * and that metadata can be checked by BPF code, check 8779 * it. Otherwise, give up, as this link-layer type has 8780 * nothing in the packet data. 8781 * 8782 * Currently, the only platform where a BPF filter can 8783 * check that metadata is Linux with the in-kernel 8784 * BPF interpreter. If other packet capture mechanisms 8785 * and BPF filters also supported this, it would be 8786 * nice. It would be even better if they made that 8787 * metadata available so that we could provide it 8788 * with newer capture APIs, allowing it to be saved 8789 * in pcapng files. 8790 */ 8791 #if defined(__linux__) 8792 /* 8793 * This is Linux; we require PF_PACKET support. 8794 * If this is a *live* capture, we can look at 8795 * special meta-data in the filter expression; 8796 * if it's a savefile, we can't. 8797 */ 8798 if (cstate->bpf_pcap->rfile != NULL) { 8799 /* We have a FILE *, so this is a savefile */ 8800 bpf_error(cstate, "inbound/outbound not supported on %s when reading savefiles", 8801 pcap_datalink_val_to_description_or_dlt(cstate->linktype)); 8802 /*NOTREACHED*/ 8803 } 8804 /* match outgoing packets */ 8805 b0 = gen_cmp(cstate, OR_LINKHDR, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H, 8806 PACKET_OUTGOING); 8807 if (!dir) { 8808 /* to filter on inbound traffic, invert the match */ 8809 gen_not(b0); 8810 } 8811 #else /* defined(__linux__) */ 8812 bpf_error(cstate, "inbound/outbound not supported on %s", 8813 pcap_datalink_val_to_description_or_dlt(cstate->linktype)); 8814 /*NOTREACHED*/ 8815 #endif /* defined(__linux__) */ 8816 } 8817 return (b0); 8818 } 8819 8820 /* PF firewall log matched interface */ 8821 struct block * 8822 gen_pf_ifname(compiler_state_t *cstate, const char *ifname) 8823 { 8824 struct block *b0; 8825 u_int len, off; 8826 8827 /* 8828 * Catch errors reported by us and routines below us, and return NULL 8829 * on an error. 8830 */ 8831 if (setjmp(cstate->top_ctx)) 8832 return (NULL); 8833 8834 if (cstate->linktype != DLT_PFLOG) { 8835 bpf_error(cstate, "ifname supported only on PF linktype"); 8836 /*NOTREACHED*/ 8837 } 8838 len = sizeof(((struct pfloghdr *)0)->ifname); 8839 off = offsetof(struct pfloghdr, ifname); 8840 if (strlen(ifname) >= len) { 8841 bpf_error(cstate, "ifname interface names can only be %d characters", 8842 len-1); 8843 /*NOTREACHED*/ 8844 } 8845 b0 = gen_bcmp(cstate, OR_LINKHDR, off, (u_int)strlen(ifname), 8846 (const u_char *)ifname); 8847 return (b0); 8848 } 8849 8850 /* PF firewall log ruleset name */ 8851 struct block * 8852 gen_pf_ruleset(compiler_state_t *cstate, char *ruleset) 8853 { 8854 struct block *b0; 8855 8856 /* 8857 * Catch errors reported by us and routines below us, and return NULL 8858 * on an error. 8859 */ 8860 if (setjmp(cstate->top_ctx)) 8861 return (NULL); 8862 8863 if (cstate->linktype != DLT_PFLOG) { 8864 bpf_error(cstate, "ruleset supported only on PF linktype"); 8865 /*NOTREACHED*/ 8866 } 8867 8868 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) { 8869 bpf_error(cstate, "ruleset names can only be %ld characters", 8870 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1)); 8871 /*NOTREACHED*/ 8872 } 8873 8874 b0 = gen_bcmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, ruleset), 8875 (u_int)strlen(ruleset), (const u_char *)ruleset); 8876 return (b0); 8877 } 8878 8879 /* PF firewall log rule number */ 8880 struct block * 8881 gen_pf_rnr(compiler_state_t *cstate, int rnr) 8882 { 8883 struct block *b0; 8884 8885 /* 8886 * Catch errors reported by us and routines below us, and return NULL 8887 * on an error. 8888 */ 8889 if (setjmp(cstate->top_ctx)) 8890 return (NULL); 8891 8892 if (cstate->linktype != DLT_PFLOG) { 8893 bpf_error(cstate, "rnr supported only on PF linktype"); 8894 /*NOTREACHED*/ 8895 } 8896 8897 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, rulenr), BPF_W, 8898 (bpf_u_int32)rnr); 8899 return (b0); 8900 } 8901 8902 /* PF firewall log sub-rule number */ 8903 struct block * 8904 gen_pf_srnr(compiler_state_t *cstate, int srnr) 8905 { 8906 struct block *b0; 8907 8908 /* 8909 * Catch errors reported by us and routines below us, and return NULL 8910 * on an error. 8911 */ 8912 if (setjmp(cstate->top_ctx)) 8913 return (NULL); 8914 8915 if (cstate->linktype != DLT_PFLOG) { 8916 bpf_error(cstate, "srnr supported only on PF linktype"); 8917 /*NOTREACHED*/ 8918 } 8919 8920 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, subrulenr), BPF_W, 8921 (bpf_u_int32)srnr); 8922 return (b0); 8923 } 8924 8925 /* PF firewall log reason code */ 8926 struct block * 8927 gen_pf_reason(compiler_state_t *cstate, int reason) 8928 { 8929 struct block *b0; 8930 8931 /* 8932 * Catch errors reported by us and routines below us, and return NULL 8933 * on an error. 8934 */ 8935 if (setjmp(cstate->top_ctx)) 8936 return (NULL); 8937 8938 if (cstate->linktype != DLT_PFLOG) { 8939 bpf_error(cstate, "reason supported only on PF linktype"); 8940 /*NOTREACHED*/ 8941 } 8942 8943 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, reason), BPF_B, 8944 (bpf_u_int32)reason); 8945 return (b0); 8946 } 8947 8948 /* PF firewall log action */ 8949 struct block * 8950 gen_pf_action(compiler_state_t *cstate, int action) 8951 { 8952 struct block *b0; 8953 8954 /* 8955 * Catch errors reported by us and routines below us, and return NULL 8956 * on an error. 8957 */ 8958 if (setjmp(cstate->top_ctx)) 8959 return (NULL); 8960 8961 if (cstate->linktype != DLT_PFLOG) { 8962 bpf_error(cstate, "action supported only on PF linktype"); 8963 /*NOTREACHED*/ 8964 } 8965 8966 b0 = gen_cmp(cstate, OR_LINKHDR, offsetof(struct pfloghdr, action), BPF_B, 8967 (bpf_u_int32)action); 8968 return (b0); 8969 } 8970 8971 /* IEEE 802.11 wireless header */ 8972 struct block * 8973 gen_p80211_type(compiler_state_t *cstate, bpf_u_int32 type, bpf_u_int32 mask) 8974 { 8975 struct block *b0; 8976 8977 /* 8978 * Catch errors reported by us and routines below us, and return NULL 8979 * on an error. 8980 */ 8981 if (setjmp(cstate->top_ctx)) 8982 return (NULL); 8983 8984 switch (cstate->linktype) { 8985 8986 case DLT_IEEE802_11: 8987 case DLT_PRISM_HEADER: 8988 case DLT_IEEE802_11_RADIO_AVS: 8989 case DLT_IEEE802_11_RADIO: 8990 b0 = gen_mcmp(cstate, OR_LINKHDR, 0, BPF_B, type, mask); 8991 break; 8992 8993 default: 8994 bpf_error(cstate, "802.11 link-layer types supported only on 802.11"); 8995 /*NOTREACHED*/ 8996 } 8997 8998 return (b0); 8999 } 9000 9001 struct block * 9002 gen_p80211_fcdir(compiler_state_t *cstate, bpf_u_int32 fcdir) 9003 { 9004 struct block *b0; 9005 9006 /* 9007 * Catch errors reported by us and routines below us, and return NULL 9008 * on an error. 9009 */ 9010 if (setjmp(cstate->top_ctx)) 9011 return (NULL); 9012 9013 switch (cstate->linktype) { 9014 9015 case DLT_IEEE802_11: 9016 case DLT_PRISM_HEADER: 9017 case DLT_IEEE802_11_RADIO_AVS: 9018 case DLT_IEEE802_11_RADIO: 9019 break; 9020 9021 default: 9022 bpf_error(cstate, "frame direction supported only with 802.11 headers"); 9023 /*NOTREACHED*/ 9024 } 9025 9026 b0 = gen_mcmp(cstate, OR_LINKHDR, 1, BPF_B, fcdir, 9027 IEEE80211_FC1_DIR_MASK); 9028 9029 return (b0); 9030 } 9031 9032 // Process an ARCnet host address string. 9033 struct block * 9034 gen_acode(compiler_state_t *cstate, const char *s, struct qual q) 9035 { 9036 /* 9037 * Catch errors reported by us and routines below us, and return NULL 9038 * on an error. 9039 */ 9040 if (setjmp(cstate->top_ctx)) 9041 return (NULL); 9042 9043 switch (cstate->linktype) { 9044 9045 case DLT_ARCNET: 9046 case DLT_ARCNET_LINUX: 9047 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && 9048 q.proto == Q_LINK) { 9049 uint8_t addr; 9050 /* 9051 * The lexer currently defines the address format in a 9052 * way that makes this error condition never true. 9053 * Let's check it anyway in case this part of the lexer 9054 * changes in future. 9055 */ 9056 if (! pcapint_atoan(s, &addr)) 9057 bpf_error(cstate, "invalid ARCnet address '%s'", s); 9058 return gen_ahostop(cstate, addr, (int)q.dir); 9059 } else 9060 bpf_error(cstate, "ARCnet address used in non-arc expression"); 9061 /*NOTREACHED*/ 9062 9063 default: 9064 bpf_error(cstate, "aid supported only on ARCnet"); 9065 /*NOTREACHED*/ 9066 } 9067 } 9068 9069 // Compare an ARCnet host address with the given value. 9070 static struct block * 9071 gen_ahostop(compiler_state_t *cstate, const uint8_t eaddr, int dir) 9072 { 9073 register struct block *b0, *b1; 9074 9075 switch (dir) { 9076 /* 9077 * ARCnet is different from Ethernet: the source address comes before 9078 * the destination address, each is one byte long. This holds for all 9079 * three "buffer formats" in RFC 1201 Section 2.1, see also page 4-10 9080 * in the 1983 edition of the "ARCNET Designer's Handbook" published 9081 * by Datapoint (document number 61610-01). 9082 */ 9083 case Q_SRC: 9084 return gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, eaddr); 9085 9086 case Q_DST: 9087 return gen_cmp(cstate, OR_LINKHDR, 1, BPF_B, eaddr); 9088 9089 case Q_AND: 9090 b0 = gen_ahostop(cstate, eaddr, Q_SRC); 9091 b1 = gen_ahostop(cstate, eaddr, Q_DST); 9092 gen_and(b0, b1); 9093 return b1; 9094 9095 case Q_DEFAULT: 9096 case Q_OR: 9097 b0 = gen_ahostop(cstate, eaddr, Q_SRC); 9098 b1 = gen_ahostop(cstate, eaddr, Q_DST); 9099 gen_or(b0, b1); 9100 return b1; 9101 9102 case Q_ADDR1: 9103 bpf_error(cstate, "'addr1' and 'address1' are only supported on 802.11"); 9104 /*NOTREACHED*/ 9105 9106 case Q_ADDR2: 9107 bpf_error(cstate, "'addr2' and 'address2' are only supported on 802.11"); 9108 /*NOTREACHED*/ 9109 9110 case Q_ADDR3: 9111 bpf_error(cstate, "'addr3' and 'address3' are only supported on 802.11"); 9112 /*NOTREACHED*/ 9113 9114 case Q_ADDR4: 9115 bpf_error(cstate, "'addr4' and 'address4' are only supported on 802.11"); 9116 /*NOTREACHED*/ 9117 9118 case Q_RA: 9119 bpf_error(cstate, "'ra' is only supported on 802.11"); 9120 /*NOTREACHED*/ 9121 9122 case Q_TA: 9123 bpf_error(cstate, "'ta' is only supported on 802.11"); 9124 /*NOTREACHED*/ 9125 } 9126 abort(); 9127 /*NOTREACHED*/ 9128 } 9129 9130 static struct block * 9131 gen_vlan_tpid_test(compiler_state_t *cstate) 9132 { 9133 struct block *b0, *b1; 9134 9135 /* check for VLAN, including 802.1ad and QinQ */ 9136 b0 = gen_linktype(cstate, ETHERTYPE_8021Q); 9137 b1 = gen_linktype(cstate, ETHERTYPE_8021AD); 9138 gen_or(b0,b1); 9139 b0 = b1; 9140 b1 = gen_linktype(cstate, ETHERTYPE_8021QINQ); 9141 gen_or(b0,b1); 9142 9143 return b1; 9144 } 9145 9146 static struct block * 9147 gen_vlan_vid_test(compiler_state_t *cstate, bpf_u_int32 vlan_num) 9148 { 9149 if (vlan_num > 0x0fff) { 9150 bpf_error(cstate, "VLAN tag %u greater than maximum %u", 9151 vlan_num, 0x0fff); 9152 } 9153 return gen_mcmp(cstate, OR_LINKPL, 0, BPF_H, vlan_num, 0x0fff); 9154 } 9155 9156 static struct block * 9157 gen_vlan_no_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num, 9158 int has_vlan_tag) 9159 { 9160 struct block *b0, *b1; 9161 9162 b0 = gen_vlan_tpid_test(cstate); 9163 9164 if (has_vlan_tag) { 9165 b1 = gen_vlan_vid_test(cstate, vlan_num); 9166 gen_and(b0, b1); 9167 b0 = b1; 9168 } 9169 9170 /* 9171 * Both payload and link header type follow the VLAN tags so that 9172 * both need to be updated. 9173 */ 9174 cstate->off_linkpl.constant_part += 4; 9175 cstate->off_linktype.constant_part += 4; 9176 9177 return b0; 9178 } 9179 9180 #if defined(SKF_AD_VLAN_TAG_PRESENT) 9181 /* add v to variable part of off */ 9182 static void 9183 gen_vlan_vloffset_add(compiler_state_t *cstate, bpf_abs_offset *off, 9184 bpf_u_int32 v, struct slist *s) 9185 { 9186 struct slist *s2; 9187 9188 if (!off->is_variable) 9189 off->is_variable = 1; 9190 if (off->reg == -1) 9191 off->reg = alloc_reg(cstate); 9192 9193 s2 = new_stmt(cstate, BPF_LD|BPF_MEM); 9194 s2->s.k = off->reg; 9195 sappend(s, s2); 9196 s2 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_IMM); 9197 s2->s.k = v; 9198 sappend(s, s2); 9199 s2 = new_stmt(cstate, BPF_ST); 9200 s2->s.k = off->reg; 9201 sappend(s, s2); 9202 } 9203 9204 /* 9205 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload 9206 * and link type offsets first 9207 */ 9208 static void 9209 gen_vlan_patch_tpid_test(compiler_state_t *cstate, struct block *b_tpid) 9210 { 9211 struct slist s; 9212 9213 /* offset determined at run time, shift variable part */ 9214 s.next = NULL; 9215 cstate->is_vlan_vloffset = 1; 9216 gen_vlan_vloffset_add(cstate, &cstate->off_linkpl, 4, &s); 9217 gen_vlan_vloffset_add(cstate, &cstate->off_linktype, 4, &s); 9218 9219 /* we get a pointer to a chain of or-ed blocks, patch first of them */ 9220 sappend(s.next, b_tpid->head->stmts); 9221 b_tpid->head->stmts = s.next; 9222 } 9223 9224 /* 9225 * patch block b_vid (VLAN id test) to load VID value either from packet 9226 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true 9227 */ 9228 static void 9229 gen_vlan_patch_vid_test(compiler_state_t *cstate, struct block *b_vid) 9230 { 9231 struct slist *s, *s2, *sjeq; 9232 unsigned cnt; 9233 9234 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); 9235 s->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); 9236 9237 /* true -> next instructions, false -> beginning of b_vid */ 9238 sjeq = new_stmt(cstate, JMP(BPF_JEQ)); 9239 sjeq->s.k = 1; 9240 sjeq->s.jf = b_vid->stmts; 9241 sappend(s, sjeq); 9242 9243 s2 = new_stmt(cstate, BPF_LD|BPF_H|BPF_ABS); 9244 s2->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG); 9245 sappend(s, s2); 9246 sjeq->s.jt = s2; 9247 9248 /* Jump to the test in b_vid. We need to jump one instruction before 9249 * the end of the b_vid block so that we only skip loading the TCI 9250 * from packet data and not the 'and' instruction extracting VID. 9251 */ 9252 cnt = 0; 9253 for (s2 = b_vid->stmts; s2; s2 = s2->next) 9254 cnt++; 9255 s2 = new_stmt(cstate, JMP(BPF_JA)); 9256 s2->s.k = cnt - 1; 9257 sappend(s, s2); 9258 9259 /* insert our statements at the beginning of b_vid */ 9260 sappend(s, b_vid->stmts); 9261 b_vid->stmts = s; 9262 } 9263 9264 /* 9265 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF 9266 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN 9267 * tag can be either in metadata or in packet data; therefore if the 9268 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link 9269 * header for VLAN tag. As the decision is done at run time, we need 9270 * update variable part of the offsets 9271 */ 9272 static struct block * 9273 gen_vlan_bpf_extensions(compiler_state_t *cstate, bpf_u_int32 vlan_num, 9274 int has_vlan_tag) 9275 { 9276 struct block *b0, *b_tpid, *b_vid = NULL; 9277 struct slist *s; 9278 9279 /* generate new filter code based on extracting packet 9280 * metadata */ 9281 s = new_stmt(cstate, BPF_LD|BPF_B|BPF_ABS); 9282 s->s.k = (bpf_u_int32)(SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); 9283 9284 b0 = new_block(cstate, JMP(BPF_JEQ)); 9285 b0->stmts = s; 9286 b0->s.k = 1; 9287 9288 /* 9289 * This is tricky. We need to insert the statements updating variable 9290 * parts of offsets before the traditional TPID and VID tests so 9291 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but 9292 * we do not want this update to affect those checks. That's why we 9293 * generate both test blocks first and insert the statements updating 9294 * variable parts of both offsets after that. This wouldn't work if 9295 * there already were variable length link header when entering this 9296 * function but gen_vlan_bpf_extensions() isn't called in that case. 9297 */ 9298 b_tpid = gen_vlan_tpid_test(cstate); 9299 if (has_vlan_tag) 9300 b_vid = gen_vlan_vid_test(cstate, vlan_num); 9301 9302 gen_vlan_patch_tpid_test(cstate, b_tpid); 9303 gen_or(b0, b_tpid); 9304 b0 = b_tpid; 9305 9306 if (has_vlan_tag) { 9307 gen_vlan_patch_vid_test(cstate, b_vid); 9308 gen_and(b0, b_vid); 9309 b0 = b_vid; 9310 } 9311 9312 return b0; 9313 } 9314 #endif 9315 9316 /* 9317 * support IEEE 802.1Q VLAN trunk over ethernet 9318 */ 9319 struct block * 9320 gen_vlan(compiler_state_t *cstate, bpf_u_int32 vlan_num, int has_vlan_tag) 9321 { 9322 struct block *b0; 9323 9324 /* 9325 * Catch errors reported by us and routines below us, and return NULL 9326 * on an error. 9327 */ 9328 if (setjmp(cstate->top_ctx)) 9329 return (NULL); 9330 9331 /* can't check for VLAN-encapsulated packets inside MPLS */ 9332 if (cstate->label_stack_depth > 0) 9333 bpf_error(cstate, "no VLAN match after MPLS"); 9334 9335 /* 9336 * Check for a VLAN packet, and then change the offsets to point 9337 * to the type and data fields within the VLAN packet. Just 9338 * increment the offsets, so that we can support a hierarchy, e.g. 9339 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within 9340 * VLAN 100. 9341 * 9342 * XXX - this is a bit of a kludge. If we were to split the 9343 * compiler into a parser that parses an expression and 9344 * generates an expression tree, and a code generator that 9345 * takes an expression tree (which could come from our 9346 * parser or from some other parser) and generates BPF code, 9347 * we could perhaps make the offsets parameters of routines 9348 * and, in the handler for an "AND" node, pass to subnodes 9349 * other than the VLAN node the adjusted offsets. 9350 * 9351 * This would mean that "vlan" would, instead of changing the 9352 * behavior of *all* tests after it, change only the behavior 9353 * of tests ANDed with it. That would change the documented 9354 * semantics of "vlan", which might break some expressions. 9355 * However, it would mean that "(vlan and ip) or ip" would check 9356 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than 9357 * checking only for VLAN-encapsulated IP, so that could still 9358 * be considered worth doing; it wouldn't break expressions 9359 * that are of the form "vlan and ..." or "vlan N and ...", 9360 * which I suspect are the most common expressions involving 9361 * "vlan". "vlan or ..." doesn't necessarily do what the user 9362 * would really want, now, as all the "or ..." tests would 9363 * be done assuming a VLAN, even though the "or" could be viewed 9364 * as meaning "or, if this isn't a VLAN packet...". 9365 */ 9366 switch (cstate->linktype) { 9367 9368 case DLT_EN10MB: 9369 case DLT_NETANALYZER: 9370 case DLT_NETANALYZER_TRANSPARENT: 9371 #if defined(SKF_AD_VLAN_TAG_PRESENT) 9372 /* Verify that this is the outer part of the packet and 9373 * not encapsulated somehow. */ 9374 if (cstate->vlan_stack_depth == 0 && !cstate->off_linkhdr.is_variable && 9375 cstate->off_linkhdr.constant_part == 9376 cstate->off_outermostlinkhdr.constant_part) { 9377 /* 9378 * Do we need special VLAN handling? 9379 */ 9380 if (cstate->bpf_pcap->bpf_codegen_flags & BPF_SPECIAL_VLAN_HANDLING) 9381 b0 = gen_vlan_bpf_extensions(cstate, vlan_num, 9382 has_vlan_tag); 9383 else 9384 b0 = gen_vlan_no_bpf_extensions(cstate, 9385 vlan_num, has_vlan_tag); 9386 } else 9387 #endif 9388 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, 9389 has_vlan_tag); 9390 break; 9391 9392 case DLT_IEEE802_11: 9393 case DLT_PRISM_HEADER: 9394 case DLT_IEEE802_11_RADIO_AVS: 9395 case DLT_IEEE802_11_RADIO: 9396 b0 = gen_vlan_no_bpf_extensions(cstate, vlan_num, has_vlan_tag); 9397 break; 9398 9399 default: 9400 bpf_error(cstate, "no VLAN support for %s", 9401 pcap_datalink_val_to_description_or_dlt(cstate->linktype)); 9402 /*NOTREACHED*/ 9403 } 9404 9405 cstate->vlan_stack_depth++; 9406 9407 return (b0); 9408 } 9409 9410 /* 9411 * support for MPLS 9412 * 9413 * The label_num_arg dance is to avoid annoying whining by compilers that 9414 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*? 9415 * It's not *used* after setjmp returns. 9416 */ 9417 static struct block * 9418 gen_mpls_internal(compiler_state_t *cstate, bpf_u_int32 label_num, 9419 int has_label_num) 9420 { 9421 struct block *b0, *b1; 9422 9423 if (cstate->label_stack_depth > 0) { 9424 /* just match the bottom-of-stack bit clear */ 9425 b0 = gen_mcmp(cstate, OR_PREVMPLSHDR, 2, BPF_B, 0, 0x01); 9426 } else { 9427 /* 9428 * We're not in an MPLS stack yet, so check the link-layer 9429 * type against MPLS. 9430 */ 9431 switch (cstate->linktype) { 9432 9433 case DLT_C_HDLC: /* fall through */ 9434 case DLT_HDLC: 9435 case DLT_EN10MB: 9436 case DLT_NETANALYZER: 9437 case DLT_NETANALYZER_TRANSPARENT: 9438 b0 = gen_linktype(cstate, ETHERTYPE_MPLS); 9439 break; 9440 9441 case DLT_PPP: 9442 b0 = gen_linktype(cstate, PPP_MPLS_UCAST); 9443 break; 9444 9445 /* FIXME add other DLT_s ... 9446 * for Frame-Relay/and ATM this may get messy due to SNAP headers 9447 * leave it for now */ 9448 9449 default: 9450 bpf_error(cstate, "no MPLS support for %s", 9451 pcap_datalink_val_to_description_or_dlt(cstate->linktype)); 9452 /*NOTREACHED*/ 9453 } 9454 } 9455 9456 /* If a specific MPLS label is requested, check it */ 9457 if (has_label_num) { 9458 if (label_num > 0xFFFFF) { 9459 bpf_error(cstate, "MPLS label %u greater than maximum %u", 9460 label_num, 0xFFFFF); 9461 } 9462 label_num = label_num << 12; /* label is shifted 12 bits on the wire */ 9463 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, label_num, 9464 0xfffff000); /* only compare the first 20 bits */ 9465 gen_and(b0, b1); 9466 b0 = b1; 9467 } 9468 9469 /* 9470 * Change the offsets to point to the type and data fields within 9471 * the MPLS packet. Just increment the offsets, so that we 9472 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to 9473 * capture packets with an outer label of 100000 and an inner 9474 * label of 1024. 9475 * 9476 * Increment the MPLS stack depth as well; this indicates that 9477 * we're checking MPLS-encapsulated headers, to make sure higher 9478 * level code generators don't try to match against IP-related 9479 * protocols such as Q_ARP, Q_RARP etc. 9480 * 9481 * XXX - this is a bit of a kludge. See comments in gen_vlan(). 9482 */ 9483 cstate->off_nl_nosnap += 4; 9484 cstate->off_nl += 4; 9485 cstate->label_stack_depth++; 9486 return (b0); 9487 } 9488 9489 struct block * 9490 gen_mpls(compiler_state_t *cstate, bpf_u_int32 label_num, int has_label_num) 9491 { 9492 /* 9493 * Catch errors reported by us and routines below us, and return NULL 9494 * on an error. 9495 */ 9496 if (setjmp(cstate->top_ctx)) 9497 return (NULL); 9498 9499 return gen_mpls_internal(cstate, label_num, has_label_num); 9500 } 9501 9502 /* 9503 * Support PPPOE discovery and session. 9504 */ 9505 struct block * 9506 gen_pppoed(compiler_state_t *cstate) 9507 { 9508 /* 9509 * Catch errors reported by us and routines below us, and return NULL 9510 * on an error. 9511 */ 9512 if (setjmp(cstate->top_ctx)) 9513 return (NULL); 9514 9515 /* check for PPPoE discovery */ 9516 return gen_linktype(cstate, ETHERTYPE_PPPOED); 9517 } 9518 9519 struct block * 9520 gen_pppoes(compiler_state_t *cstate, bpf_u_int32 sess_num, int has_sess_num) 9521 { 9522 struct block *b0, *b1; 9523 9524 /* 9525 * Catch errors reported by us and routines below us, and return NULL 9526 * on an error. 9527 */ 9528 if (setjmp(cstate->top_ctx)) 9529 return (NULL); 9530 9531 /* 9532 * Test against the PPPoE session link-layer type. 9533 */ 9534 b0 = gen_linktype(cstate, ETHERTYPE_PPPOES); 9535 9536 /* If a specific session is requested, check PPPoE session id */ 9537 if (has_sess_num) { 9538 if (sess_num > 0x0000ffff) { 9539 bpf_error(cstate, "PPPoE session number %u greater than maximum %u", 9540 sess_num, 0x0000ffff); 9541 } 9542 b1 = gen_mcmp(cstate, OR_LINKPL, 0, BPF_W, sess_num, 0x0000ffff); 9543 gen_and(b0, b1); 9544 b0 = b1; 9545 } 9546 9547 /* 9548 * Change the offsets to point to the type and data fields within 9549 * the PPP packet, and note that this is PPPoE rather than 9550 * raw PPP. 9551 * 9552 * XXX - this is a bit of a kludge. See the comments in 9553 * gen_vlan(). 9554 * 9555 * The "network-layer" protocol is PPPoE, which has a 6-byte 9556 * PPPoE header, followed by a PPP packet. 9557 * 9558 * There is no HDLC encapsulation for the PPP packet (it's 9559 * encapsulated in PPPoES instead), so the link-layer type 9560 * starts at the first byte of the PPP packet. For PPPoE, 9561 * that offset is relative to the beginning of the total 9562 * link-layer payload, including any 802.2 LLC header, so 9563 * it's 6 bytes past cstate->off_nl. 9564 */ 9565 PUSH_LINKHDR(cstate, DLT_PPP, cstate->off_linkpl.is_variable, 9566 cstate->off_linkpl.constant_part + cstate->off_nl + 6, /* 6 bytes past the PPPoE header */ 9567 cstate->off_linkpl.reg); 9568 9569 cstate->off_linktype = cstate->off_linkhdr; 9570 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 2; 9571 9572 cstate->off_nl = 0; 9573 cstate->off_nl_nosnap = 0; /* no 802.2 LLC */ 9574 9575 return b0; 9576 } 9577 9578 /* Check that this is Geneve and the VNI is correct if 9579 * specified. Parameterized to handle both IPv4 and IPv6. */ 9580 static struct block * 9581 gen_geneve_check(compiler_state_t *cstate, 9582 struct block *(*gen_portfn)(compiler_state_t *, u_int, int, int), 9583 enum e_offrel offrel, bpf_u_int32 vni, int has_vni) 9584 { 9585 struct block *b0, *b1; 9586 9587 b0 = gen_portfn(cstate, GENEVE_PORT, IPPROTO_UDP, Q_DST); 9588 9589 /* Check that we are operating on version 0. Otherwise, we 9590 * can't decode the rest of the fields. The version is 2 bits 9591 * in the first byte of the Geneve header. */ 9592 b1 = gen_mcmp(cstate, offrel, 8, BPF_B, 0, 0xc0); 9593 gen_and(b0, b1); 9594 b0 = b1; 9595 9596 if (has_vni) { 9597 if (vni > 0xffffff) { 9598 bpf_error(cstate, "Geneve VNI %u greater than maximum %u", 9599 vni, 0xffffff); 9600 } 9601 vni <<= 8; /* VNI is in the upper 3 bytes */ 9602 b1 = gen_mcmp(cstate, offrel, 12, BPF_W, vni, 0xffffff00); 9603 gen_and(b0, b1); 9604 b0 = b1; 9605 } 9606 9607 return b0; 9608 } 9609 9610 /* The IPv4 and IPv6 Geneve checks need to do two things: 9611 * - Verify that this actually is Geneve with the right VNI. 9612 * - Place the IP header length (plus variable link prefix if 9613 * needed) into register A to be used later to compute 9614 * the inner packet offsets. */ 9615 static struct block * 9616 gen_geneve4(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) 9617 { 9618 struct block *b0, *b1; 9619 struct slist *s, *s1; 9620 9621 b0 = gen_geneve_check(cstate, gen_port, OR_TRAN_IPV4, vni, has_vni); 9622 9623 /* Load the IP header length into A. */ 9624 s = gen_loadx_iphdrlen(cstate); 9625 9626 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); 9627 sappend(s, s1); 9628 9629 /* Forcibly append these statements to the true condition 9630 * of the protocol check by creating a new block that is 9631 * always true and ANDing them. */ 9632 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X); 9633 b1->stmts = s; 9634 b1->s.k = 0; 9635 9636 gen_and(b0, b1); 9637 9638 return b1; 9639 } 9640 9641 static struct block * 9642 gen_geneve6(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) 9643 { 9644 struct block *b0, *b1; 9645 struct slist *s, *s1; 9646 9647 b0 = gen_geneve_check(cstate, gen_port6, OR_TRAN_IPV6, vni, has_vni); 9648 9649 /* Load the IP header length. We need to account for a 9650 * variable length link prefix if there is one. */ 9651 s = gen_abs_offset_varpart(cstate, &cstate->off_linkpl); 9652 if (s) { 9653 s1 = new_stmt(cstate, BPF_LD|BPF_IMM); 9654 s1->s.k = 40; 9655 sappend(s, s1); 9656 9657 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); 9658 s1->s.k = 0; 9659 sappend(s, s1); 9660 } else { 9661 s = new_stmt(cstate, BPF_LD|BPF_IMM); 9662 s->s.k = 40; 9663 } 9664 9665 /* Forcibly append these statements to the true condition 9666 * of the protocol check by creating a new block that is 9667 * always true and ANDing them. */ 9668 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); 9669 sappend(s, s1); 9670 9671 b1 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X); 9672 b1->stmts = s; 9673 b1->s.k = 0; 9674 9675 gen_and(b0, b1); 9676 9677 return b1; 9678 } 9679 9680 /* We need to store three values based on the Geneve header:: 9681 * - The offset of the linktype. 9682 * - The offset of the end of the Geneve header. 9683 * - The offset of the end of the encapsulated MAC header. */ 9684 static struct slist * 9685 gen_geneve_offsets(compiler_state_t *cstate) 9686 { 9687 struct slist *s, *s1, *s_proto; 9688 9689 /* First we need to calculate the offset of the Geneve header 9690 * itself. This is composed of the IP header previously calculated 9691 * (include any variable link prefix) and stored in A plus the 9692 * fixed sized headers (fixed link prefix, MAC length, and UDP 9693 * header). */ 9694 s = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 9695 s->s.k = cstate->off_linkpl.constant_part + cstate->off_nl + 8; 9696 9697 /* Stash this in X since we'll need it later. */ 9698 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); 9699 sappend(s, s1); 9700 9701 /* The EtherType in Geneve is 2 bytes in. Calculate this and 9702 * store it. */ 9703 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 9704 s1->s.k = 2; 9705 sappend(s, s1); 9706 9707 cstate->off_linktype.reg = alloc_reg(cstate); 9708 cstate->off_linktype.is_variable = 1; 9709 cstate->off_linktype.constant_part = 0; 9710 9711 s1 = new_stmt(cstate, BPF_ST); 9712 s1->s.k = cstate->off_linktype.reg; 9713 sappend(s, s1); 9714 9715 /* Load the Geneve option length and mask and shift to get the 9716 * number of bytes. It is stored in the first byte of the Geneve 9717 * header. */ 9718 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_B); 9719 s1->s.k = 0; 9720 sappend(s, s1); 9721 9722 s1 = new_stmt(cstate, BPF_ALU|BPF_AND|BPF_K); 9723 s1->s.k = 0x3f; 9724 sappend(s, s1); 9725 9726 s1 = new_stmt(cstate, BPF_ALU|BPF_MUL|BPF_K); 9727 s1->s.k = 4; 9728 sappend(s, s1); 9729 9730 /* Add in the rest of the Geneve base header. */ 9731 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 9732 s1->s.k = 8; 9733 sappend(s, s1); 9734 9735 /* Add the Geneve header length to its offset and store. */ 9736 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_X); 9737 s1->s.k = 0; 9738 sappend(s, s1); 9739 9740 /* Set the encapsulated type as Ethernet. Even though we may 9741 * not actually have Ethernet inside there are two reasons this 9742 * is useful: 9743 * - The linktype field is always in EtherType format regardless 9744 * of whether it is in Geneve or an inner Ethernet frame. 9745 * - The only link layer that we have specific support for is 9746 * Ethernet. We will confirm that the packet actually is 9747 * Ethernet at runtime before executing these checks. */ 9748 PUSH_LINKHDR(cstate, DLT_EN10MB, 1, 0, alloc_reg(cstate)); 9749 9750 s1 = new_stmt(cstate, BPF_ST); 9751 s1->s.k = cstate->off_linkhdr.reg; 9752 sappend(s, s1); 9753 9754 /* Calculate whether we have an Ethernet header or just raw IP/ 9755 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset 9756 * and linktype by 14 bytes so that the network header can be found 9757 * seamlessly. Otherwise, keep what we've calculated already. */ 9758 9759 /* We have a bare jmp so we can't use the optimizer. */ 9760 cstate->no_optimize = 1; 9761 9762 /* Load the EtherType in the Geneve header, 2 bytes in. */ 9763 s1 = new_stmt(cstate, BPF_LD|BPF_IND|BPF_H); 9764 s1->s.k = 2; 9765 sappend(s, s1); 9766 9767 /* Load X with the end of the Geneve header. */ 9768 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM); 9769 s1->s.k = cstate->off_linkhdr.reg; 9770 sappend(s, s1); 9771 9772 /* Check if the EtherType is Transparent Ethernet Bridging. At the 9773 * end of this check, we should have the total length in X. In 9774 * the non-Ethernet case, it's already there. */ 9775 s_proto = new_stmt(cstate, JMP(BPF_JEQ)); 9776 s_proto->s.k = ETHERTYPE_TEB; 9777 sappend(s, s_proto); 9778 9779 s1 = new_stmt(cstate, BPF_MISC|BPF_TXA); 9780 sappend(s, s1); 9781 s_proto->s.jt = s1; 9782 9783 /* Since this is Ethernet, use the EtherType of the payload 9784 * directly as the linktype. Overwrite what we already have. */ 9785 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 9786 s1->s.k = 12; 9787 sappend(s, s1); 9788 9789 s1 = new_stmt(cstate, BPF_ST); 9790 s1->s.k = cstate->off_linktype.reg; 9791 sappend(s, s1); 9792 9793 /* Advance two bytes further to get the end of the Ethernet 9794 * header. */ 9795 s1 = new_stmt(cstate, BPF_ALU|BPF_ADD|BPF_K); 9796 s1->s.k = 2; 9797 sappend(s, s1); 9798 9799 /* Move the result to X. */ 9800 s1 = new_stmt(cstate, BPF_MISC|BPF_TAX); 9801 sappend(s, s1); 9802 9803 /* Store the final result of our linkpl calculation. */ 9804 cstate->off_linkpl.reg = alloc_reg(cstate); 9805 cstate->off_linkpl.is_variable = 1; 9806 cstate->off_linkpl.constant_part = 0; 9807 9808 s1 = new_stmt(cstate, BPF_STX); 9809 s1->s.k = cstate->off_linkpl.reg; 9810 sappend(s, s1); 9811 s_proto->s.jf = s1; 9812 9813 cstate->off_nl = 0; 9814 9815 return s; 9816 } 9817 9818 /* Check to see if this is a Geneve packet. */ 9819 struct block * 9820 gen_geneve(compiler_state_t *cstate, bpf_u_int32 vni, int has_vni) 9821 { 9822 struct block *b0, *b1; 9823 struct slist *s; 9824 9825 /* 9826 * Catch errors reported by us and routines below us, and return NULL 9827 * on an error. 9828 */ 9829 if (setjmp(cstate->top_ctx)) 9830 return (NULL); 9831 9832 b0 = gen_geneve4(cstate, vni, has_vni); 9833 b1 = gen_geneve6(cstate, vni, has_vni); 9834 9835 gen_or(b0, b1); 9836 b0 = b1; 9837 9838 /* Later filters should act on the payload of the Geneve frame, 9839 * update all of the header pointers. Attach this code so that 9840 * it gets executed in the event that the Geneve filter matches. */ 9841 s = gen_geneve_offsets(cstate); 9842 9843 b1 = gen_true(cstate); 9844 sappend(s, b1->stmts); 9845 b1->stmts = s; 9846 9847 gen_and(b0, b1); 9848 9849 cstate->is_geneve = 1; 9850 9851 return b1; 9852 } 9853 9854 /* Check that the encapsulated frame has a link layer header 9855 * for Ethernet filters. */ 9856 static struct block * 9857 gen_geneve_ll_check(compiler_state_t *cstate) 9858 { 9859 struct block *b0; 9860 struct slist *s, *s1; 9861 9862 /* The easiest way to see if there is a link layer present 9863 * is to check if the link layer header and payload are not 9864 * the same. */ 9865 9866 /* Geneve always generates pure variable offsets so we can 9867 * compare only the registers. */ 9868 s = new_stmt(cstate, BPF_LD|BPF_MEM); 9869 s->s.k = cstate->off_linkhdr.reg; 9870 9871 s1 = new_stmt(cstate, BPF_LDX|BPF_MEM); 9872 s1->s.k = cstate->off_linkpl.reg; 9873 sappend(s, s1); 9874 9875 b0 = new_block(cstate, BPF_JMP|BPF_JEQ|BPF_X); 9876 b0->stmts = s; 9877 b0->s.k = 0; 9878 gen_not(b0); 9879 9880 return b0; 9881 } 9882 9883 static struct block * 9884 gen_atmfield_code_internal(compiler_state_t *cstate, int atmfield, 9885 bpf_u_int32 jvalue, int jtype, int reverse) 9886 { 9887 struct block *b0; 9888 9889 switch (atmfield) { 9890 9891 case A_VPI: 9892 if (!cstate->is_atm) 9893 bpf_error(cstate, "'vpi' supported only on raw ATM"); 9894 if (cstate->off_vpi == OFFSET_NOT_SET) 9895 abort(); 9896 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vpi, BPF_B, 9897 0xffffffffU, jtype, reverse, jvalue); 9898 break; 9899 9900 case A_VCI: 9901 if (!cstate->is_atm) 9902 bpf_error(cstate, "'vci' supported only on raw ATM"); 9903 if (cstate->off_vci == OFFSET_NOT_SET) 9904 abort(); 9905 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_vci, BPF_H, 9906 0xffffffffU, jtype, reverse, jvalue); 9907 break; 9908 9909 case A_PROTOTYPE: 9910 if (cstate->off_proto == OFFSET_NOT_SET) 9911 abort(); /* XXX - this isn't on FreeBSD */ 9912 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, 9913 0x0fU, jtype, reverse, jvalue); 9914 break; 9915 9916 case A_MSGTYPE: 9917 if (cstate->off_payload == OFFSET_NOT_SET) 9918 abort(); 9919 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_payload + MSG_TYPE_POS, BPF_B, 9920 0xffffffffU, jtype, reverse, jvalue); 9921 break; 9922 9923 case A_CALLREFTYPE: 9924 if (!cstate->is_atm) 9925 bpf_error(cstate, "'callref' supported only on raw ATM"); 9926 if (cstate->off_proto == OFFSET_NOT_SET) 9927 abort(); 9928 b0 = gen_ncmp(cstate, OR_LINKHDR, cstate->off_proto, BPF_B, 9929 0xffffffffU, jtype, reverse, jvalue); 9930 break; 9931 9932 default: 9933 abort(); 9934 } 9935 return b0; 9936 } 9937 9938 static struct block * 9939 gen_atmtype_metac(compiler_state_t *cstate) 9940 { 9941 struct block *b0, *b1; 9942 9943 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); 9944 b1 = gen_atmfield_code_internal(cstate, A_VCI, 1, BPF_JEQ, 0); 9945 gen_and(b0, b1); 9946 return b1; 9947 } 9948 9949 static struct block * 9950 gen_atmtype_sc(compiler_state_t *cstate) 9951 { 9952 struct block *b0, *b1; 9953 9954 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); 9955 b1 = gen_atmfield_code_internal(cstate, A_VCI, 5, BPF_JEQ, 0); 9956 gen_and(b0, b1); 9957 return b1; 9958 } 9959 9960 static struct block * 9961 gen_atmtype_llc(compiler_state_t *cstate) 9962 { 9963 struct block *b0; 9964 9965 b0 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LLC, BPF_JEQ, 0); 9966 cstate->linktype = cstate->prevlinktype; 9967 return b0; 9968 } 9969 9970 struct block * 9971 gen_atmfield_code(compiler_state_t *cstate, int atmfield, 9972 bpf_u_int32 jvalue, int jtype, int reverse) 9973 { 9974 /* 9975 * Catch errors reported by us and routines below us, and return NULL 9976 * on an error. 9977 */ 9978 if (setjmp(cstate->top_ctx)) 9979 return (NULL); 9980 9981 return gen_atmfield_code_internal(cstate, atmfield, jvalue, jtype, 9982 reverse); 9983 } 9984 9985 struct block * 9986 gen_atmtype_abbrev(compiler_state_t *cstate, int type) 9987 { 9988 struct block *b0, *b1; 9989 9990 /* 9991 * Catch errors reported by us and routines below us, and return NULL 9992 * on an error. 9993 */ 9994 if (setjmp(cstate->top_ctx)) 9995 return (NULL); 9996 9997 switch (type) { 9998 9999 case A_METAC: 10000 /* Get all packets in Meta signalling Circuit */ 10001 if (!cstate->is_atm) 10002 bpf_error(cstate, "'metac' supported only on raw ATM"); 10003 b1 = gen_atmtype_metac(cstate); 10004 break; 10005 10006 case A_BCC: 10007 /* Get all packets in Broadcast Circuit*/ 10008 if (!cstate->is_atm) 10009 bpf_error(cstate, "'bcc' supported only on raw ATM"); 10010 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); 10011 b1 = gen_atmfield_code_internal(cstate, A_VCI, 2, BPF_JEQ, 0); 10012 gen_and(b0, b1); 10013 break; 10014 10015 case A_OAMF4SC: 10016 /* Get all cells in Segment OAM F4 circuit*/ 10017 if (!cstate->is_atm) 10018 bpf_error(cstate, "'oam4sc' supported only on raw ATM"); 10019 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); 10020 b1 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0); 10021 gen_and(b0, b1); 10022 break; 10023 10024 case A_OAMF4EC: 10025 /* Get all cells in End-to-End OAM F4 Circuit*/ 10026 if (!cstate->is_atm) 10027 bpf_error(cstate, "'oam4ec' supported only on raw ATM"); 10028 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); 10029 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0); 10030 gen_and(b0, b1); 10031 break; 10032 10033 case A_SC: 10034 /* Get all packets in connection Signalling Circuit */ 10035 if (!cstate->is_atm) 10036 bpf_error(cstate, "'sc' supported only on raw ATM"); 10037 b1 = gen_atmtype_sc(cstate); 10038 break; 10039 10040 case A_ILMIC: 10041 /* Get all packets in ILMI Circuit */ 10042 if (!cstate->is_atm) 10043 bpf_error(cstate, "'ilmic' supported only on raw ATM"); 10044 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); 10045 b1 = gen_atmfield_code_internal(cstate, A_VCI, 16, BPF_JEQ, 0); 10046 gen_and(b0, b1); 10047 break; 10048 10049 case A_LANE: 10050 /* Get all LANE packets */ 10051 if (!cstate->is_atm) 10052 bpf_error(cstate, "'lane' supported only on raw ATM"); 10053 b1 = gen_atmfield_code_internal(cstate, A_PROTOTYPE, PT_LANE, BPF_JEQ, 0); 10054 10055 /* 10056 * Arrange that all subsequent tests assume LANE 10057 * rather than LLC-encapsulated packets, and set 10058 * the offsets appropriately for LANE-encapsulated 10059 * Ethernet. 10060 * 10061 * We assume LANE means Ethernet, not Token Ring. 10062 */ 10063 PUSH_LINKHDR(cstate, DLT_EN10MB, 0, 10064 cstate->off_payload + 2, /* Ethernet header */ 10065 -1); 10066 cstate->off_linktype.constant_part = cstate->off_linkhdr.constant_part + 12; 10067 cstate->off_linkpl.constant_part = cstate->off_linkhdr.constant_part + 14; /* Ethernet */ 10068 cstate->off_nl = 0; /* Ethernet II */ 10069 cstate->off_nl_nosnap = 3; /* 802.3+802.2 */ 10070 break; 10071 10072 case A_LLC: 10073 /* Get all LLC-encapsulated packets */ 10074 if (!cstate->is_atm) 10075 bpf_error(cstate, "'llc' supported only on raw ATM"); 10076 b1 = gen_atmtype_llc(cstate); 10077 break; 10078 10079 default: 10080 abort(); 10081 } 10082 return b1; 10083 } 10084 10085 /* 10086 * Filtering for MTP2 messages based on li value 10087 * FISU, length is null 10088 * LSSU, length is 1 or 2 10089 * MSU, length is 3 or more 10090 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits 10091 */ 10092 struct block * 10093 gen_mtp2type_abbrev(compiler_state_t *cstate, int type) 10094 { 10095 struct block *b0, *b1; 10096 10097 /* 10098 * Catch errors reported by us and routines below us, and return NULL 10099 * on an error. 10100 */ 10101 if (setjmp(cstate->top_ctx)) 10102 return (NULL); 10103 10104 switch (type) { 10105 10106 case M_FISU: 10107 if ( (cstate->linktype != DLT_MTP2) && 10108 (cstate->linktype != DLT_ERF) && 10109 (cstate->linktype != DLT_MTP2_WITH_PHDR) ) 10110 bpf_error(cstate, "'fisu' supported only on MTP2"); 10111 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */ 10112 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 10113 0x3fU, BPF_JEQ, 0, 0U); 10114 break; 10115 10116 case M_LSSU: 10117 if ( (cstate->linktype != DLT_MTP2) && 10118 (cstate->linktype != DLT_ERF) && 10119 (cstate->linktype != DLT_MTP2_WITH_PHDR) ) 10120 bpf_error(cstate, "'lssu' supported only on MTP2"); 10121 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 10122 0x3fU, BPF_JGT, 1, 2U); 10123 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 10124 0x3fU, BPF_JGT, 0, 0U); 10125 gen_and(b1, b0); 10126 break; 10127 10128 case M_MSU: 10129 if ( (cstate->linktype != DLT_MTP2) && 10130 (cstate->linktype != DLT_ERF) && 10131 (cstate->linktype != DLT_MTP2_WITH_PHDR) ) 10132 bpf_error(cstate, "'msu' supported only on MTP2"); 10133 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li, BPF_B, 10134 0x3fU, BPF_JGT, 0, 2U); 10135 break; 10136 10137 case MH_FISU: 10138 if ( (cstate->linktype != DLT_MTP2) && 10139 (cstate->linktype != DLT_ERF) && 10140 (cstate->linktype != DLT_MTP2_WITH_PHDR) ) 10141 bpf_error(cstate, "'hfisu' supported only on MTP2_HSL"); 10142 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */ 10143 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 10144 0xff80U, BPF_JEQ, 0, 0U); 10145 break; 10146 10147 case MH_LSSU: 10148 if ( (cstate->linktype != DLT_MTP2) && 10149 (cstate->linktype != DLT_ERF) && 10150 (cstate->linktype != DLT_MTP2_WITH_PHDR) ) 10151 bpf_error(cstate, "'hlssu' supported only on MTP2_HSL"); 10152 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 10153 0xff80U, BPF_JGT, 1, 0x0100U); 10154 b1 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 10155 0xff80U, BPF_JGT, 0, 0U); 10156 gen_and(b1, b0); 10157 break; 10158 10159 case MH_MSU: 10160 if ( (cstate->linktype != DLT_MTP2) && 10161 (cstate->linktype != DLT_ERF) && 10162 (cstate->linktype != DLT_MTP2_WITH_PHDR) ) 10163 bpf_error(cstate, "'hmsu' supported only on MTP2_HSL"); 10164 b0 = gen_ncmp(cstate, OR_PACKET, cstate->off_li_hsl, BPF_H, 10165 0xff80U, BPF_JGT, 0, 0x0100U); 10166 break; 10167 10168 default: 10169 abort(); 10170 } 10171 return b0; 10172 } 10173 10174 static struct block * 10175 gen_mtp3field_code_internal(compiler_state_t *cstate, int mtp3field, 10176 bpf_u_int32 jvalue, int jtype, int reverse) 10177 { 10178 struct block *b0; 10179 bpf_u_int32 val1 , val2 , val3; 10180 u_int newoff_sio; 10181 u_int newoff_opc; 10182 u_int newoff_dpc; 10183 u_int newoff_sls; 10184 10185 newoff_sio = cstate->off_sio; 10186 newoff_opc = cstate->off_opc; 10187 newoff_dpc = cstate->off_dpc; 10188 newoff_sls = cstate->off_sls; 10189 switch (mtp3field) { 10190 10191 case MH_SIO: 10192 newoff_sio += 3; /* offset for MTP2_HSL */ 10193 /* FALLTHROUGH */ 10194 10195 case M_SIO: 10196 if (cstate->off_sio == OFFSET_NOT_SET) 10197 bpf_error(cstate, "'sio' supported only on SS7"); 10198 /* sio coded on 1 byte so max value 255 */ 10199 if(jvalue > 255) 10200 bpf_error(cstate, "sio value %u too big; max value = 255", 10201 jvalue); 10202 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sio, BPF_B, 0xffffffffU, 10203 jtype, reverse, jvalue); 10204 break; 10205 10206 case MH_OPC: 10207 newoff_opc += 3; 10208 10209 /* FALLTHROUGH */ 10210 case M_OPC: 10211 if (cstate->off_opc == OFFSET_NOT_SET) 10212 bpf_error(cstate, "'opc' supported only on SS7"); 10213 /* opc coded on 14 bits so max value 16383 */ 10214 if (jvalue > 16383) 10215 bpf_error(cstate, "opc value %u too big; max value = 16383", 10216 jvalue); 10217 /* the following instructions are made to convert jvalue 10218 * to the form used to write opc in an ss7 message*/ 10219 val1 = jvalue & 0x00003c00; 10220 val1 = val1 >>10; 10221 val2 = jvalue & 0x000003fc; 10222 val2 = val2 <<6; 10223 val3 = jvalue & 0x00000003; 10224 val3 = val3 <<22; 10225 jvalue = val1 + val2 + val3; 10226 b0 = gen_ncmp(cstate, OR_PACKET, newoff_opc, BPF_W, 0x00c0ff0fU, 10227 jtype, reverse, jvalue); 10228 break; 10229 10230 case MH_DPC: 10231 newoff_dpc += 3; 10232 /* FALLTHROUGH */ 10233 10234 case M_DPC: 10235 if (cstate->off_dpc == OFFSET_NOT_SET) 10236 bpf_error(cstate, "'dpc' supported only on SS7"); 10237 /* dpc coded on 14 bits so max value 16383 */ 10238 if (jvalue > 16383) 10239 bpf_error(cstate, "dpc value %u too big; max value = 16383", 10240 jvalue); 10241 /* the following instructions are made to convert jvalue 10242 * to the forme used to write dpc in an ss7 message*/ 10243 val1 = jvalue & 0x000000ff; 10244 val1 = val1 << 24; 10245 val2 = jvalue & 0x00003f00; 10246 val2 = val2 << 8; 10247 jvalue = val1 + val2; 10248 b0 = gen_ncmp(cstate, OR_PACKET, newoff_dpc, BPF_W, 0xff3f0000U, 10249 jtype, reverse, jvalue); 10250 break; 10251 10252 case MH_SLS: 10253 newoff_sls += 3; 10254 /* FALLTHROUGH */ 10255 10256 case M_SLS: 10257 if (cstate->off_sls == OFFSET_NOT_SET) 10258 bpf_error(cstate, "'sls' supported only on SS7"); 10259 /* sls coded on 4 bits so max value 15 */ 10260 if (jvalue > 15) 10261 bpf_error(cstate, "sls value %u too big; max value = 15", 10262 jvalue); 10263 /* the following instruction is made to convert jvalue 10264 * to the forme used to write sls in an ss7 message*/ 10265 jvalue = jvalue << 4; 10266 b0 = gen_ncmp(cstate, OR_PACKET, newoff_sls, BPF_B, 0xf0U, 10267 jtype, reverse, jvalue); 10268 break; 10269 10270 default: 10271 abort(); 10272 } 10273 return b0; 10274 } 10275 10276 struct block * 10277 gen_mtp3field_code(compiler_state_t *cstate, int mtp3field, 10278 bpf_u_int32 jvalue, int jtype, int reverse) 10279 { 10280 /* 10281 * Catch errors reported by us and routines below us, and return NULL 10282 * on an error. 10283 */ 10284 if (setjmp(cstate->top_ctx)) 10285 return (NULL); 10286 10287 return gen_mtp3field_code_internal(cstate, mtp3field, jvalue, jtype, 10288 reverse); 10289 } 10290 10291 static struct block * 10292 gen_msg_abbrev(compiler_state_t *cstate, int type) 10293 { 10294 struct block *b1; 10295 10296 /* 10297 * Q.2931 signalling protocol messages for handling virtual circuits 10298 * establishment and teardown 10299 */ 10300 switch (type) { 10301 10302 case A_SETUP: 10303 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, SETUP, BPF_JEQ, 0); 10304 break; 10305 10306 case A_CALLPROCEED: 10307 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0); 10308 break; 10309 10310 case A_CONNECT: 10311 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT, BPF_JEQ, 0); 10312 break; 10313 10314 case A_CONNECTACK: 10315 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0); 10316 break; 10317 10318 case A_RELEASE: 10319 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE, BPF_JEQ, 0); 10320 break; 10321 10322 case A_RELEASE_DONE: 10323 b1 = gen_atmfield_code_internal(cstate, A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0); 10324 break; 10325 10326 default: 10327 abort(); 10328 } 10329 return b1; 10330 } 10331 10332 struct block * 10333 gen_atmmulti_abbrev(compiler_state_t *cstate, int type) 10334 { 10335 struct block *b0, *b1; 10336 10337 /* 10338 * Catch errors reported by us and routines below us, and return NULL 10339 * on an error. 10340 */ 10341 if (setjmp(cstate->top_ctx)) 10342 return (NULL); 10343 10344 switch (type) { 10345 10346 case A_OAM: 10347 if (!cstate->is_atm) 10348 bpf_error(cstate, "'oam' supported only on raw ATM"); 10349 /* OAM F4 type */ 10350 b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0); 10351 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0); 10352 gen_or(b0, b1); 10353 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); 10354 gen_and(b0, b1); 10355 break; 10356 10357 case A_OAMF4: 10358 if (!cstate->is_atm) 10359 bpf_error(cstate, "'oamf4' supported only on raw ATM"); 10360 /* OAM F4 type */ 10361 b0 = gen_atmfield_code_internal(cstate, A_VCI, 3, BPF_JEQ, 0); 10362 b1 = gen_atmfield_code_internal(cstate, A_VCI, 4, BPF_JEQ, 0); 10363 gen_or(b0, b1); 10364 b0 = gen_atmfield_code_internal(cstate, A_VPI, 0, BPF_JEQ, 0); 10365 gen_and(b0, b1); 10366 break; 10367 10368 case A_CONNECTMSG: 10369 /* 10370 * Get Q.2931 signalling messages for switched 10371 * virtual connection 10372 */ 10373 if (!cstate->is_atm) 10374 bpf_error(cstate, "'connectmsg' supported only on raw ATM"); 10375 b0 = gen_msg_abbrev(cstate, A_SETUP); 10376 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED); 10377 gen_or(b0, b1); 10378 b0 = gen_msg_abbrev(cstate, A_CONNECT); 10379 gen_or(b0, b1); 10380 b0 = gen_msg_abbrev(cstate, A_CONNECTACK); 10381 gen_or(b0, b1); 10382 b0 = gen_msg_abbrev(cstate, A_RELEASE); 10383 gen_or(b0, b1); 10384 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE); 10385 gen_or(b0, b1); 10386 b0 = gen_atmtype_sc(cstate); 10387 gen_and(b0, b1); 10388 break; 10389 10390 case A_METACONNECT: 10391 if (!cstate->is_atm) 10392 bpf_error(cstate, "'metaconnect' supported only on raw ATM"); 10393 b0 = gen_msg_abbrev(cstate, A_SETUP); 10394 b1 = gen_msg_abbrev(cstate, A_CALLPROCEED); 10395 gen_or(b0, b1); 10396 b0 = gen_msg_abbrev(cstate, A_CONNECT); 10397 gen_or(b0, b1); 10398 b0 = gen_msg_abbrev(cstate, A_RELEASE); 10399 gen_or(b0, b1); 10400 b0 = gen_msg_abbrev(cstate, A_RELEASE_DONE); 10401 gen_or(b0, b1); 10402 b0 = gen_atmtype_metac(cstate); 10403 gen_and(b0, b1); 10404 break; 10405 10406 default: 10407 abort(); 10408 } 10409 return b1; 10410 } 10411