1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 #include <pcap-stdinc.h>
35 #ifdef HAVE_SYS_BITYPES_H
36 #include <sys/bitypes.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
45 #include <sys/param.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
65 #include "ethertype.h"
69 #include "ieee80211.h"
71 #include "sunatmpos.h"
74 #include "pcap/ipnet.h"
80 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
81 #include <linux/types.h>
82 #include <linux/if_packet.h>
83 #include <linux/filter.h>
86 #ifdef HAVE_NET_PFVAR_H
87 #include <sys/socket.h>
89 #include <net/pfvar.h>
90 #include <net/if_pflog.h>
94 #define offsetof(s, e) ((size_t)&((s *)0)->e)
99 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
105 u_int8_t u6_addr8
[16];
106 u_int16_t u6_addr16
[8];
107 u_int32_t u6_addr32
[4];
109 #define s6_addr in6_u.u6_addr8
110 #define s6_addr16 in6_u.u6_addr16
111 #define s6_addr32 in6_u.u6_addr32
112 #define s6_addr64 in6_u.u6_addr64
115 typedef unsigned short sa_family_t
;
117 #define __SOCKADDR_COMMON(sa_prefix) \
118 sa_family_t sa_prefix##family
120 /* Ditto, for IPv6. */
123 __SOCKADDR_COMMON (sin6_
);
124 u_int16_t sin6_port
; /* Transport layer port # */
125 u_int32_t sin6_flowinfo
; /* IPv6 flow information */
126 struct in6_addr sin6_addr
; /* IPv6 address */
129 #ifndef EAI_ADDRFAMILY
131 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
132 int ai_family
; /* PF_xxx */
133 int ai_socktype
; /* SOCK_xxx */
134 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
135 size_t ai_addrlen
; /* length of ai_addr */
136 char *ai_canonname
; /* canonical name for hostname */
137 struct sockaddr
*ai_addr
; /* binary address */
138 struct addrinfo
*ai_next
; /* next structure in linked list */
140 #endif /* EAI_ADDRFAMILY */
141 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
143 #include <netdb.h> /* for "struct addrinfo" */
146 #include <pcap/namedb.h>
148 #include "nametoaddr.h"
150 #define ETHERMTU 1500
152 #ifndef ETHERTYPE_TEB
153 #define ETHERTYPE_TEB 0x6558
156 #ifndef IPPROTO_HOPOPTS
157 #define IPPROTO_HOPOPTS 0
159 #ifndef IPPROTO_ROUTING
160 #define IPPROTO_ROUTING 43
162 #ifndef IPPROTO_FRAGMENT
163 #define IPPROTO_FRAGMENT 44
165 #ifndef IPPROTO_DSTOPTS
166 #define IPPROTO_DSTOPTS 60
169 #define IPPROTO_SCTP 132
172 #define GENEVE_PORT 6081
174 #ifdef HAVE_OS_PROTO_H
175 #include "os-proto.h"
178 #define JMP(c) ((c)|BPF_JMP|BPF_K)
181 * "Push" the current value of the link-layer header type and link-layer
182 * header offset onto a "stack", and set a new value. (It's not a
183 * full-blown stack; we keep only the top two items.)
185 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
187 (cs)->prevlinktype = (cs)->linktype; \
188 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
189 (cs)->linktype = (new_linktype); \
190 (cs)->off_linkhdr.is_variable = (new_is_variable); \
191 (cs)->off_linkhdr.constant_part = (new_constant_part); \
192 (cs)->off_linkhdr.reg = (new_reg); \
193 (cs)->is_geneve = 0; \
197 * Offset "not set" value.
199 #define OFFSET_NOT_SET 0xffffffffU
202 * Absolute offsets, which are offsets from the beginning of the raw
203 * packet data, are, in the general case, the sum of a variable value
204 * and a constant value; the variable value may be absent, in which
205 * case the offset is only the constant value, and the constant value
206 * may be zero, in which case the offset is only the variable value.
208 * bpf_abs_offset is a structure containing all that information:
210 * is_variable is 1 if there's a variable part.
212 * constant_part is the constant part of the value, possibly zero;
214 * if is_variable is 1, reg is the register number for a register
215 * containing the variable value if the register has been assigned,
225 * Value passed to gen_load_a() to indicate what the offset argument
226 * is relative to the beginning of.
229 OR_PACKET
, /* full packet data */
230 OR_LINKHDR
, /* link-layer header */
231 OR_PREVLINKHDR
, /* previous link-layer header */
232 OR_LLC
, /* 802.2 LLC header */
233 OR_PREVMPLSHDR
, /* previous MPLS header */
234 OR_LINKTYPE
, /* link-layer type */
235 OR_LINKPL
, /* link-layer payload */
236 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
237 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
238 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
242 * We divy out chunks of memory rather than call malloc each time so
243 * we don't have to worry about leaking memory. It's probably
244 * not a big deal if all this memory was wasted but if this ever
245 * goes into a library that would probably not be a good idea.
247 * XXX - this *is* in a library....
250 #define CHUNK0SIZE 1024
256 /* Code generator state */
258 struct _compiler_state
{
268 int outermostlinktype
;
273 /* Hack for handling VLAN and MPLS stacks. */
274 u_int label_stack_depth
;
275 u_int vlan_stack_depth
;
282 * As errors are handled by a longjmp, anything allocated must
283 * be freed in the longjmp handler, so it must be reachable
286 * One thing that's allocated is the result of pcap_nametoaddrinfo();
287 * it must be freed with freeaddrinfo(). This variable points to
288 * any addrinfo structure that would need to be freed.
294 * Various code constructs need to know the layout of the packet.
295 * These values give the necessary offsets from the beginning
296 * of the packet data.
300 * Absolute offset of the beginning of the link-layer header.
302 bpf_abs_offset off_linkhdr
;
305 * If we're checking a link-layer header for a packet encapsulated
306 * in another protocol layer, this is the equivalent information
307 * for the previous layers' link-layer header from the beginning
308 * of the raw packet data.
310 bpf_abs_offset off_prevlinkhdr
;
313 * This is the equivalent information for the outermost layers'
316 bpf_abs_offset off_outermostlinkhdr
;
319 * Absolute offset of the beginning of the link-layer payload.
321 bpf_abs_offset off_linkpl
;
324 * "off_linktype" is the offset to information in the link-layer
325 * header giving the packet type. This is an absolute offset
326 * from the beginning of the packet.
328 * For Ethernet, it's the offset of the Ethernet type field; this
329 * means that it must have a value that skips VLAN tags.
331 * For link-layer types that always use 802.2 headers, it's the
332 * offset of the LLC header; this means that it must have a value
333 * that skips VLAN tags.
335 * For PPP, it's the offset of the PPP type field.
337 * For Cisco HDLC, it's the offset of the CHDLC type field.
339 * For BSD loopback, it's the offset of the AF_ value.
341 * For Linux cooked sockets, it's the offset of the type field.
343 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
344 * encapsulation, in which case, IP is assumed.
346 bpf_abs_offset off_linktype
;
349 * TRUE if the link layer includes an ATM pseudo-header.
354 * TRUE if "geneve" appeared in the filter; it causes us to
355 * generate code that checks for a Geneve header and assume
356 * that later filters apply to the encapsulated payload.
361 * These are offsets for the ATM pseudo-header.
368 * These are offsets for the MTP2 fields.
374 * These are offsets for the MTP3 fields.
382 * This is the offset of the first byte after the ATM pseudo_header,
383 * or -1 if there is no ATM pseudo-header.
388 * These are offsets to the beginning of the network-layer header.
389 * They are relative to the beginning of the link-layer payload
390 * (i.e., they don't include off_linkhdr.constant_part or
391 * off_linkpl.constant_part).
393 * If the link layer never uses 802.2 LLC:
395 * "off_nl" and "off_nl_nosnap" are the same.
397 * If the link layer always uses 802.2 LLC:
399 * "off_nl" is the offset if there's a SNAP header following
402 * "off_nl_nosnap" is the offset if there's no SNAP header.
404 * If the link layer is Ethernet:
406 * "off_nl" is the offset if the packet is an Ethernet II packet
407 * (we assume no 802.3+802.2+SNAP);
409 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
410 * with an 802.2 header following it.
416 * Here we handle simple allocation of the scratch registers.
417 * If too many registers are alloc'd, the allocator punts.
419 int regused
[BPF_MEMWORDS
];
425 struct chunk chunks
[NCHUNKS
];
430 bpf_syntax_error(compiler_state_t
*cstate
, const char *msg
)
432 bpf_error(cstate
, "syntax error in filter expression: %s", msg
);
438 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
443 if (cstate
->bpf_pcap
!= NULL
)
444 (void)pcap_vsnprintf(pcap_geterr(cstate
->bpf_pcap
),
445 PCAP_ERRBUF_SIZE
, fmt
, ap
);
447 longjmp(cstate
->top_ctx
, 1);
451 static void init_linktype(compiler_state_t
*, pcap_t
*);
453 static void init_regs(compiler_state_t
*);
454 static int alloc_reg(compiler_state_t
*);
455 static void free_reg(compiler_state_t
*, int);
457 static void initchunks(compiler_state_t
*cstate
);
458 static void *newchunk(compiler_state_t
*cstate
, size_t);
459 static void freechunks(compiler_state_t
*cstate
);
460 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
461 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
462 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
463 static inline void syntax(compiler_state_t
*cstate
);
465 static void backpatch(struct block
*, struct block
*);
466 static void merge(struct block
*, struct block
*);
467 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
469 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
471 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
473 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
475 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
477 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
478 u_int
, bpf_int32
, bpf_u_int32
);
479 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
480 u_int
, const u_char
*);
481 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
482 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
483 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
485 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
487 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
488 static struct block
*gen_uncond(compiler_state_t
*, int);
489 static inline struct block
*gen_true(compiler_state_t
*);
490 static inline struct block
*gen_false(compiler_state_t
*);
491 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
492 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
493 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
494 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
495 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
496 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
497 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
498 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
499 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
501 static int ethertype_to_ppptype(int);
502 static struct block
*gen_linktype(compiler_state_t
*, int);
503 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
504 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
505 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
506 int, int, u_int
, u_int
);
508 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
509 struct in6_addr
*, int, int, u_int
, u_int
);
511 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
512 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
513 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
514 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
515 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
516 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
517 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
518 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
519 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
522 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
523 struct in6_addr
*, int, int, int);
526 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
528 static struct block
*gen_ipfrag(compiler_state_t
*);
529 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
530 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
532 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
533 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
535 struct block
*gen_portop(compiler_state_t
*, int, int, int);
536 static struct block
*gen_port(compiler_state_t
*, int, int, int);
537 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
538 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
539 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
540 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
541 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
542 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
543 static int lookup_proto(compiler_state_t
*, const char *, int);
544 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
545 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
546 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
547 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
548 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
549 static struct block
*gen_len(compiler_state_t
*, int, int);
550 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
551 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
553 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
554 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
557 initchunks(compiler_state_t
*cstate
)
561 for (i
= 0; i
< NCHUNKS
; i
++) {
562 cstate
->chunks
[i
].n_left
= 0;
563 cstate
->chunks
[i
].m
= NULL
;
565 cstate
->cur_chunk
= 0;
569 newchunk(compiler_state_t
*cstate
, size_t n
)
576 /* XXX Round up to nearest long. */
577 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
579 /* XXX Round up to structure boundary. */
583 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
584 if (n
> cp
->n_left
) {
585 ++cp
, k
= ++cstate
->cur_chunk
;
587 bpf_error(cstate
, "out of memory");
588 size
= CHUNK0SIZE
<< k
;
589 cp
->m
= (void *)malloc(size
);
591 bpf_error(cstate
, "out of memory");
592 memset((char *)cp
->m
, 0, size
);
595 bpf_error(cstate
, "out of memory");
598 return (void *)((char *)cp
->m
+ cp
->n_left
);
602 freechunks(compiler_state_t
*cstate
)
606 for (i
= 0; i
< NCHUNKS
; ++i
)
607 if (cstate
->chunks
[i
].m
!= NULL
)
608 free(cstate
->chunks
[i
].m
);
612 * A strdup whose allocations are freed after code generation is over.
615 sdup(compiler_state_t
*cstate
, const char *s
)
617 size_t n
= strlen(s
) + 1;
618 char *cp
= newchunk(cstate
, n
);
624 static inline struct block
*
625 new_block(compiler_state_t
*cstate
, int code
)
629 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
636 static inline struct slist
*
637 new_stmt(compiler_state_t
*cstate
, int code
)
641 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
647 static struct block
*
648 gen_retblk(compiler_state_t
*cstate
, int v
)
650 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
657 syntax(compiler_state_t
*cstate
)
659 bpf_error(cstate
, "syntax error in filter expression");
663 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
664 const char *buf
, int optimize
, bpf_u_int32 mask
)
666 compiler_state_t cstate
;
667 const char * volatile xbuf
= buf
;
668 yyscan_t scanner
= NULL
;
669 YY_BUFFER_STATE in_buffer
= NULL
;
682 * If this pcap_t hasn't been activated, it doesn't have a
683 * link-layer type, so we can't use it.
686 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
687 "not-yet-activated pcap_t passed to pcap_compile");
692 cstate
.no_optimize
= 0;
694 cstate
.ic
.root
= NULL
;
695 cstate
.ic
.cur_mark
= 0;
699 if (setjmp(cstate
.top_ctx
)) {
701 if (cstate
.ai
!= NULL
)
702 freeaddrinfo(cstate
.ai
);
708 cstate
.netmask
= mask
;
710 cstate
.snaplen
= pcap_snapshot(p
);
711 if (cstate
.snaplen
== 0) {
712 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
713 "snaplen of 0 rejects all packets");
718 if (pcap_lex_init(&scanner
) != 0)
719 bpf_error(&cstate
, "can't initialize scanner: %s", pcap_strerror(errno
));
720 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
723 * Associate the compiler state with the lexical analyzer
726 pcap_set_extra(&cstate
, scanner
);
728 init_linktype(&cstate
, p
);
729 (void)pcap_parse(scanner
, &cstate
);
731 if (cstate
.ic
.root
== NULL
)
732 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
734 if (optimize
&& !cstate
.no_optimize
) {
735 bpf_optimize(&cstate
, &cstate
.ic
);
736 if (cstate
.ic
.root
== NULL
||
737 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
738 bpf_error(&cstate
, "expression rejects all packets");
740 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
741 program
->bf_len
= len
;
743 rc
= 0; /* We're all okay */
747 * Clean up everything for the lexical analyzer.
749 if (in_buffer
!= NULL
)
750 pcap__delete_buffer(in_buffer
, scanner
);
752 pcap_lex_destroy(scanner
);
755 * Clean up our own allocated memory.
763 * entry point for using the compiler with no pcap open
764 * pass in all the stuff that is needed explicitly instead.
767 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
768 struct bpf_program
*program
,
769 const char *buf
, int optimize
, bpf_u_int32 mask
)
774 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
777 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
783 * Clean up a "struct bpf_program" by freeing all the memory allocated
787 pcap_freecode(struct bpf_program
*program
)
790 if (program
->bf_insns
!= NULL
) {
791 free((char *)program
->bf_insns
);
792 program
->bf_insns
= NULL
;
797 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
798 * which of the jt and jf fields has been resolved and which is a pointer
799 * back to another unresolved block (or nil). At least one of the fields
800 * in each block is already resolved.
803 backpatch(list
, target
)
804 struct block
*list
, *target
;
821 * Merge the lists in b0 and b1, using the 'sense' field to indicate
822 * which of jt and jf is the link.
826 struct block
*b0
, *b1
;
828 register struct block
**p
= &b0
;
830 /* Find end of list. */
832 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
834 /* Concatenate the lists. */
839 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
841 struct block
*ppi_dlt_check
;
844 * Insert before the statements of the first (root) block any
845 * statements needed to load the lengths of any variable-length
846 * headers into registers.
848 * XXX - a fancier strategy would be to insert those before the
849 * statements of all blocks that use those lengths and that
850 * have no predecessors that use them, so that we only compute
851 * the lengths if we need them. There might be even better
852 * approaches than that.
854 * However, those strategies would be more complicated, and
855 * as we don't generate code to compute a length if the
856 * program has no tests that use the length, and as most
857 * tests will probably use those lengths, we would just
858 * postpone computing the lengths so that it's not done
859 * for tests that fail early, and it's not clear that's
862 insert_compute_vloffsets(cstate
, p
->head
);
865 * For DLT_PPI captures, generate a check of the per-packet
866 * DLT value to make sure it's DLT_IEEE802_11.
868 * XXX - TurboCap cards use DLT_PPI for Ethernet.
869 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
870 * with appropriate Ethernet information and use that rather
871 * than using something such as DLT_PPI where you don't know
872 * the link-layer header type until runtime, which, in the
873 * general case, would force us to generate both Ethernet *and*
874 * 802.11 code (*and* anything else for which PPI is used)
875 * and choose between them early in the BPF program?
877 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
878 if (ppi_dlt_check
!= NULL
)
879 gen_and(ppi_dlt_check
, p
);
881 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
882 p
->sense
= !p
->sense
;
883 backpatch(p
, gen_retblk(cstate
, 0));
884 cstate
->ic
.root
= p
->head
;
889 struct block
*b0
, *b1
;
891 backpatch(b0
, b1
->head
);
892 b0
->sense
= !b0
->sense
;
893 b1
->sense
= !b1
->sense
;
895 b1
->sense
= !b1
->sense
;
901 struct block
*b0
, *b1
;
903 b0
->sense
= !b0
->sense
;
904 backpatch(b0
, b1
->head
);
905 b0
->sense
= !b0
->sense
;
914 b
->sense
= !b
->sense
;
917 static struct block
*
918 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
919 u_int size
, bpf_int32 v
)
921 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
924 static struct block
*
925 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
926 u_int size
, bpf_int32 v
)
928 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
931 static struct block
*
932 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
933 u_int size
, bpf_int32 v
)
935 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
938 static struct block
*
939 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
940 u_int size
, bpf_int32 v
)
942 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
945 static struct block
*
946 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
947 u_int size
, bpf_int32 v
)
949 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
952 static struct block
*
953 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
954 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
956 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
959 static struct block
*
960 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
961 u_int size
, const u_char
*v
)
963 register struct block
*b
, *tmp
;
967 register const u_char
*p
= &v
[size
- 4];
968 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
969 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
971 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
, w
);
978 register const u_char
*p
= &v
[size
- 2];
979 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
981 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
, w
);
988 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
997 * AND the field of size "size" at offset "offset" relative to the header
998 * specified by "offrel" with "mask", and compare it with the value "v"
999 * with the test specified by "jtype"; if "reverse" is true, the test
1000 * should test the opposite of "jtype".
1002 static struct block
*
1003 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1004 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1007 struct slist
*s
, *s2
;
1010 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1012 if (mask
!= 0xffffffff) {
1013 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1018 b
= new_block(cstate
, JMP(jtype
));
1021 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1027 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1029 cstate
->pcap_fddipad
= p
->fddipad
;
1032 * We start out with only one link-layer header.
1034 cstate
->outermostlinktype
= pcap_datalink(p
);
1035 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1036 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1037 cstate
->off_outermostlinkhdr
.reg
= -1;
1039 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1040 cstate
->off_prevlinkhdr
.constant_part
= 0;
1041 cstate
->off_prevlinkhdr
.is_variable
= 0;
1042 cstate
->off_prevlinkhdr
.reg
= -1;
1044 cstate
->linktype
= cstate
->outermostlinktype
;
1045 cstate
->off_linkhdr
.constant_part
= 0;
1046 cstate
->off_linkhdr
.is_variable
= 0;
1047 cstate
->off_linkhdr
.reg
= -1;
1052 cstate
->off_linkpl
.constant_part
= 0;
1053 cstate
->off_linkpl
.is_variable
= 0;
1054 cstate
->off_linkpl
.reg
= -1;
1056 cstate
->off_linktype
.constant_part
= 0;
1057 cstate
->off_linktype
.is_variable
= 0;
1058 cstate
->off_linktype
.reg
= -1;
1061 * Assume it's not raw ATM with a pseudo-header, for now.
1064 cstate
->off_vpi
= -1;
1065 cstate
->off_vci
= -1;
1066 cstate
->off_proto
= -1;
1067 cstate
->off_payload
= -1;
1072 cstate
->is_geneve
= 0;
1075 * And assume we're not doing SS7.
1077 cstate
->off_li
= -1;
1078 cstate
->off_li_hsl
= -1;
1079 cstate
->off_sio
= -1;
1080 cstate
->off_opc
= -1;
1081 cstate
->off_dpc
= -1;
1082 cstate
->off_sls
= -1;
1084 cstate
->label_stack_depth
= 0;
1085 cstate
->vlan_stack_depth
= 0;
1087 switch (cstate
->linktype
) {
1090 cstate
->off_linktype
.constant_part
= 2;
1091 cstate
->off_linkpl
.constant_part
= 6;
1092 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1093 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1096 case DLT_ARCNET_LINUX
:
1097 cstate
->off_linktype
.constant_part
= 4;
1098 cstate
->off_linkpl
.constant_part
= 8;
1099 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1100 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1104 cstate
->off_linktype
.constant_part
= 12;
1105 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1106 cstate
->off_nl
= 0; /* Ethernet II */
1107 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1112 * SLIP doesn't have a link level type. The 16 byte
1113 * header is hacked into our SLIP driver.
1115 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1116 cstate
->off_linkpl
.constant_part
= 16;
1118 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1121 case DLT_SLIP_BSDOS
:
1122 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1123 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1125 cstate
->off_linkpl
.constant_part
= 24;
1127 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1132 cstate
->off_linktype
.constant_part
= 0;
1133 cstate
->off_linkpl
.constant_part
= 4;
1135 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1139 cstate
->off_linktype
.constant_part
= 0;
1140 cstate
->off_linkpl
.constant_part
= 12;
1142 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1147 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1148 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1149 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1150 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1152 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1157 * This does no include the Ethernet header, and
1158 * only covers session state.
1160 cstate
->off_linktype
.constant_part
= 6;
1161 cstate
->off_linkpl
.constant_part
= 8;
1163 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1167 cstate
->off_linktype
.constant_part
= 5;
1168 cstate
->off_linkpl
.constant_part
= 24;
1170 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1175 * FDDI doesn't really have a link-level type field.
1176 * We set "off_linktype" to the offset of the LLC header.
1178 * To check for Ethernet types, we assume that SSAP = SNAP
1179 * is being used and pick out the encapsulated Ethernet type.
1180 * XXX - should we generate code to check for SNAP?
1182 cstate
->off_linktype
.constant_part
= 13;
1183 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1184 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1185 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1186 cstate
->off_nl
= 8; /* 802.2+SNAP */
1187 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1192 * Token Ring doesn't really have a link-level type field.
1193 * We set "off_linktype" to the offset of the LLC header.
1195 * To check for Ethernet types, we assume that SSAP = SNAP
1196 * is being used and pick out the encapsulated Ethernet type.
1197 * XXX - should we generate code to check for SNAP?
1199 * XXX - the header is actually variable-length.
1200 * Some various Linux patched versions gave 38
1201 * as "off_linktype" and 40 as "off_nl"; however,
1202 * if a token ring packet has *no* routing
1203 * information, i.e. is not source-routed, the correct
1204 * values are 20 and 22, as they are in the vanilla code.
1206 * A packet is source-routed iff the uppermost bit
1207 * of the first byte of the source address, at an
1208 * offset of 8, has the uppermost bit set. If the
1209 * packet is source-routed, the total number of bytes
1210 * of routing information is 2 plus bits 0x1F00 of
1211 * the 16-bit value at an offset of 14 (shifted right
1212 * 8 - figure out which byte that is).
1214 cstate
->off_linktype
.constant_part
= 14;
1215 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1216 cstate
->off_nl
= 8; /* 802.2+SNAP */
1217 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1220 case DLT_PRISM_HEADER
:
1221 case DLT_IEEE802_11_RADIO_AVS
:
1222 case DLT_IEEE802_11_RADIO
:
1223 cstate
->off_linkhdr
.is_variable
= 1;
1224 /* Fall through, 802.11 doesn't have a variable link
1225 * prefix but is otherwise the same. */
1227 case DLT_IEEE802_11
:
1229 * 802.11 doesn't really have a link-level type field.
1230 * We set "off_linktype.constant_part" to the offset of
1233 * To check for Ethernet types, we assume that SSAP = SNAP
1234 * is being used and pick out the encapsulated Ethernet type.
1235 * XXX - should we generate code to check for SNAP?
1237 * We also handle variable-length radio headers here.
1238 * The Prism header is in theory variable-length, but in
1239 * practice it's always 144 bytes long. However, some
1240 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1241 * sometimes or always supply an AVS header, so we
1242 * have to check whether the radio header is a Prism
1243 * header or an AVS header, so, in practice, it's
1246 cstate
->off_linktype
.constant_part
= 24;
1247 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1248 cstate
->off_linkpl
.is_variable
= 1;
1249 cstate
->off_nl
= 8; /* 802.2+SNAP */
1250 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1255 * At the moment we treat PPI the same way that we treat
1256 * normal Radiotap encoded packets. The difference is in
1257 * the function that generates the code at the beginning
1258 * to compute the header length. Since this code generator
1259 * of PPI supports bare 802.11 encapsulation only (i.e.
1260 * the encapsulated DLT should be DLT_IEEE802_11) we
1261 * generate code to check for this too.
1263 cstate
->off_linktype
.constant_part
= 24;
1264 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1265 cstate
->off_linkpl
.is_variable
= 1;
1266 cstate
->off_linkhdr
.is_variable
= 1;
1267 cstate
->off_nl
= 8; /* 802.2+SNAP */
1268 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1271 case DLT_ATM_RFC1483
:
1272 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1274 * assume routed, non-ISO PDUs
1275 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1277 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1278 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1279 * latter would presumably be treated the way PPPoE
1280 * should be, so you can do "pppoe and udp port 2049"
1281 * or "pppoa and tcp port 80" and have it check for
1282 * PPPo{A,E} and a PPP protocol of IP and....
1284 cstate
->off_linktype
.constant_part
= 0;
1285 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1286 cstate
->off_nl
= 8; /* 802.2+SNAP */
1287 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1292 * Full Frontal ATM; you get AALn PDUs with an ATM
1296 cstate
->off_vpi
= SUNATM_VPI_POS
;
1297 cstate
->off_vci
= SUNATM_VCI_POS
;
1298 cstate
->off_proto
= PROTO_POS
;
1299 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1300 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1301 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1302 cstate
->off_nl
= 8; /* 802.2+SNAP */
1303 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1309 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1310 cstate
->off_linkpl
.constant_part
= 0;
1312 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1315 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1316 cstate
->off_linktype
.constant_part
= 14;
1317 cstate
->off_linkpl
.constant_part
= 16;
1319 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1324 * LocalTalk does have a 1-byte type field in the LLAP header,
1325 * but really it just indicates whether there is a "short" or
1326 * "long" DDP packet following.
1328 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1329 cstate
->off_linkpl
.constant_part
= 0;
1331 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1334 case DLT_IP_OVER_FC
:
1336 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1337 * link-level type field. We set "off_linktype" to the
1338 * offset of the LLC header.
1340 * To check for Ethernet types, we assume that SSAP = SNAP
1341 * is being used and pick out the encapsulated Ethernet type.
1342 * XXX - should we generate code to check for SNAP? RFC
1343 * 2625 says SNAP should be used.
1345 cstate
->off_linktype
.constant_part
= 16;
1346 cstate
->off_linkpl
.constant_part
= 16;
1347 cstate
->off_nl
= 8; /* 802.2+SNAP */
1348 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1353 * XXX - we should set this to handle SNAP-encapsulated
1354 * frames (NLPID of 0x80).
1356 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1357 cstate
->off_linkpl
.constant_part
= 0;
1359 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1363 * the only BPF-interesting FRF.16 frames are non-control frames;
1364 * Frame Relay has a variable length link-layer
1365 * so lets start with offset 4 for now and increments later on (FIXME);
1368 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1369 cstate
->off_linkpl
.constant_part
= 0;
1371 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1374 case DLT_APPLE_IP_OVER_IEEE1394
:
1375 cstate
->off_linktype
.constant_part
= 16;
1376 cstate
->off_linkpl
.constant_part
= 18;
1378 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1381 case DLT_SYMANTEC_FIREWALL
:
1382 cstate
->off_linktype
.constant_part
= 6;
1383 cstate
->off_linkpl
.constant_part
= 44;
1384 cstate
->off_nl
= 0; /* Ethernet II */
1385 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1388 #ifdef HAVE_NET_PFVAR_H
1390 cstate
->off_linktype
.constant_part
= 0;
1391 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1393 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1397 case DLT_JUNIPER_MFR
:
1398 case DLT_JUNIPER_MLFR
:
1399 case DLT_JUNIPER_MLPPP
:
1400 case DLT_JUNIPER_PPP
:
1401 case DLT_JUNIPER_CHDLC
:
1402 case DLT_JUNIPER_FRELAY
:
1403 cstate
->off_linktype
.constant_part
= 4;
1404 cstate
->off_linkpl
.constant_part
= 4;
1406 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1409 case DLT_JUNIPER_ATM1
:
1410 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1411 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1413 cstate
->off_nl_nosnap
= 10;
1416 case DLT_JUNIPER_ATM2
:
1417 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1418 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1420 cstate
->off_nl_nosnap
= 10;
1423 /* frames captured on a Juniper PPPoE service PIC
1424 * contain raw ethernet frames */
1425 case DLT_JUNIPER_PPPOE
:
1426 case DLT_JUNIPER_ETHER
:
1427 cstate
->off_linkpl
.constant_part
= 14;
1428 cstate
->off_linktype
.constant_part
= 16;
1429 cstate
->off_nl
= 18; /* Ethernet II */
1430 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1433 case DLT_JUNIPER_PPPOE_ATM
:
1434 cstate
->off_linktype
.constant_part
= 4;
1435 cstate
->off_linkpl
.constant_part
= 6;
1437 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1440 case DLT_JUNIPER_GGSN
:
1441 cstate
->off_linktype
.constant_part
= 6;
1442 cstate
->off_linkpl
.constant_part
= 12;
1444 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1447 case DLT_JUNIPER_ES
:
1448 cstate
->off_linktype
.constant_part
= 6;
1449 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1450 cstate
->off_nl
= -1; /* not really a network layer but raw IP addresses */
1451 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1454 case DLT_JUNIPER_MONITOR
:
1455 cstate
->off_linktype
.constant_part
= 12;
1456 cstate
->off_linkpl
.constant_part
= 12;
1457 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1458 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1461 case DLT_BACNET_MS_TP
:
1462 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1463 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1464 cstate
->off_nl
= -1;
1465 cstate
->off_nl_nosnap
= -1;
1468 case DLT_JUNIPER_SERVICES
:
1469 cstate
->off_linktype
.constant_part
= 12;
1470 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1471 cstate
->off_nl
= -1; /* L3 proto location dep. on cookie type */
1472 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1475 case DLT_JUNIPER_VP
:
1476 cstate
->off_linktype
.constant_part
= 18;
1477 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1478 cstate
->off_nl
= -1;
1479 cstate
->off_nl_nosnap
= -1;
1482 case DLT_JUNIPER_ST
:
1483 cstate
->off_linktype
.constant_part
= 18;
1484 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1485 cstate
->off_nl
= -1;
1486 cstate
->off_nl_nosnap
= -1;
1489 case DLT_JUNIPER_ISM
:
1490 cstate
->off_linktype
.constant_part
= 8;
1491 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1492 cstate
->off_nl
= -1;
1493 cstate
->off_nl_nosnap
= -1;
1496 case DLT_JUNIPER_VS
:
1497 case DLT_JUNIPER_SRX_E2E
:
1498 case DLT_JUNIPER_FIBRECHANNEL
:
1499 case DLT_JUNIPER_ATM_CEMIC
:
1500 cstate
->off_linktype
.constant_part
= 8;
1501 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1502 cstate
->off_nl
= -1;
1503 cstate
->off_nl_nosnap
= -1;
1508 cstate
->off_li_hsl
= 4;
1509 cstate
->off_sio
= 3;
1510 cstate
->off_opc
= 4;
1511 cstate
->off_dpc
= 4;
1512 cstate
->off_sls
= 7;
1513 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1514 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1515 cstate
->off_nl
= -1;
1516 cstate
->off_nl_nosnap
= -1;
1519 case DLT_MTP2_WITH_PHDR
:
1521 cstate
->off_li_hsl
= 8;
1522 cstate
->off_sio
= 7;
1523 cstate
->off_opc
= 8;
1524 cstate
->off_dpc
= 8;
1525 cstate
->off_sls
= 11;
1526 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1527 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1528 cstate
->off_nl
= -1;
1529 cstate
->off_nl_nosnap
= -1;
1533 cstate
->off_li
= 22;
1534 cstate
->off_li_hsl
= 24;
1535 cstate
->off_sio
= 23;
1536 cstate
->off_opc
= 24;
1537 cstate
->off_dpc
= 24;
1538 cstate
->off_sls
= 27;
1539 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1540 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1541 cstate
->off_nl
= -1;
1542 cstate
->off_nl_nosnap
= -1;
1546 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1547 cstate
->off_linkpl
.constant_part
= 4;
1549 cstate
->off_nl_nosnap
= 0;
1554 * Currently, only raw "link[N:M]" filtering is supported.
1556 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1557 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1558 cstate
->off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1559 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1563 cstate
->off_linktype
.constant_part
= 1;
1564 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1566 cstate
->off_nl_nosnap
= -1;
1569 case DLT_NETANALYZER
:
1570 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1571 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1572 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1573 cstate
->off_nl
= 0; /* Ethernet II */
1574 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1577 case DLT_NETANALYZER_TRANSPARENT
:
1578 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1579 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1580 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1581 cstate
->off_nl
= 0; /* Ethernet II */
1582 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1587 * For values in the range in which we've assigned new
1588 * DLT_ values, only raw "link[N:M]" filtering is supported.
1590 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1591 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1592 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1593 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1594 cstate
->off_nl
= -1;
1595 cstate
->off_nl_nosnap
= -1;
1597 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1602 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1606 * Load a value relative to the specified absolute offset.
1608 static struct slist
*
1609 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1610 u_int offset
, u_int size
)
1612 struct slist
*s
, *s2
;
1614 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1617 * If "s" is non-null, it has code to arrange that the X register
1618 * contains the variable part of the absolute offset, so we
1619 * generate a load relative to that, with an offset of
1620 * abs_offset->constant_part + offset.
1622 * Otherwise, we can do an absolute load with an offset of
1623 * abs_offset->constant_part + offset.
1627 * "s" points to a list of statements that puts the
1628 * variable part of the absolute offset into the X register.
1629 * Do an indirect load, to use the X register as an offset.
1631 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1632 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1636 * There is no variable part of the absolute offset, so
1637 * just do an absolute load.
1639 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1640 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1646 * Load a value relative to the beginning of the specified header.
1648 static struct slist
*
1649 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1652 struct slist
*s
, *s2
;
1657 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1662 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1665 case OR_PREVLINKHDR
:
1666 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1670 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1673 case OR_PREVMPLSHDR
:
1674 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1678 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1681 case OR_LINKPL_NOSNAP
:
1682 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1686 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1691 * Load the X register with the length of the IPv4 header
1692 * (plus the offset of the link-layer header, if it's
1693 * preceded by a variable-length header such as a radio
1694 * header), in bytes.
1696 s
= gen_loadx_iphdrlen(cstate
);
1699 * Load the item at {offset of the link-layer payload} +
1700 * {offset, relative to the start of the link-layer
1701 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1702 * {specified offset}.
1704 * If the offset of the link-layer payload is variable,
1705 * the variable part of that offset is included in the
1706 * value in the X register, and we include the constant
1707 * part in the offset of the load.
1709 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1710 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1715 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1726 * Generate code to load into the X register the sum of the length of
1727 * the IPv4 header and the variable part of the offset of the link-layer
1730 static struct slist
*
1731 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1733 struct slist
*s
, *s2
;
1735 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1738 * The offset of the link-layer payload has a variable
1739 * part. "s" points to a list of statements that put
1740 * the variable part of that offset into the X register.
1742 * The 4*([k]&0xf) addressing mode can't be used, as we
1743 * don't have a constant offset, so we have to load the
1744 * value in question into the A register and add to it
1745 * the value from the X register.
1747 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1748 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1750 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1753 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1758 * The A register now contains the length of the IP header.
1759 * We need to add to it the variable part of the offset of
1760 * the link-layer payload, which is still in the X
1761 * register, and move the result into the X register.
1763 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1764 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1767 * The offset of the link-layer payload is a constant,
1768 * so no code was generated to load the (non-existent)
1769 * variable part of that offset.
1771 * This means we can use the 4*([k]&0xf) addressing
1772 * mode. Load the length of the IPv4 header, which
1773 * is at an offset of cstate->off_nl from the beginning of
1774 * the link-layer payload, and thus at an offset of
1775 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1776 * of the raw packet data, using that addressing mode.
1778 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1779 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1784 static struct block
*
1785 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1790 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1792 b
= new_block(cstate
, JMP(BPF_JEQ
));
1798 static inline struct block
*
1799 gen_true(compiler_state_t
*cstate
)
1801 return gen_uncond(cstate
, 1);
1804 static inline struct block
*
1805 gen_false(compiler_state_t
*cstate
)
1807 return gen_uncond(cstate
, 0);
1811 * Byte-swap a 32-bit number.
1812 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1813 * big-endian platforms.)
1815 #define SWAPLONG(y) \
1816 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1819 * Generate code to match a particular packet type.
1821 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1822 * value, if <= ETHERMTU. We use that to determine whether to
1823 * match the type/length field or to check the type/length field for
1824 * a value <= ETHERMTU to see whether it's a type field and then do
1825 * the appropriate test.
1827 static struct block
*
1828 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1830 struct block
*b0
, *b1
;
1836 case LLCSAP_NETBEUI
:
1838 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1839 * so we check the DSAP and SSAP.
1841 * LLCSAP_IP checks for IP-over-802.2, rather
1842 * than IP-over-Ethernet or IP-over-SNAP.
1844 * XXX - should we check both the DSAP and the
1845 * SSAP, like this, or should we check just the
1846 * DSAP, as we do for other types <= ETHERMTU
1847 * (i.e., other SAP values)?
1849 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1851 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1852 ((proto
<< 8) | proto
));
1860 * Ethernet_II frames, which are Ethernet
1861 * frames with a frame type of ETHERTYPE_IPX;
1863 * Ethernet_802.3 frames, which are 802.3
1864 * frames (i.e., the type/length field is
1865 * a length field, <= ETHERMTU, rather than
1866 * a type field) with the first two bytes
1867 * after the Ethernet/802.3 header being
1870 * Ethernet_802.2 frames, which are 802.3
1871 * frames with an 802.2 LLC header and
1872 * with the IPX LSAP as the DSAP in the LLC
1875 * Ethernet_SNAP frames, which are 802.3
1876 * frames with an LLC header and a SNAP
1877 * header and with an OUI of 0x000000
1878 * (encapsulated Ethernet) and a protocol
1879 * ID of ETHERTYPE_IPX in the SNAP header.
1881 * XXX - should we generate the same code both
1882 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1886 * This generates code to check both for the
1887 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1889 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1890 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1894 * Now we add code to check for SNAP frames with
1895 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1897 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1901 * Now we generate code to check for 802.3
1902 * frames in general.
1904 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1908 * Now add the check for 802.3 frames before the
1909 * check for Ethernet_802.2 and Ethernet_802.3,
1910 * as those checks should only be done on 802.3
1911 * frames, not on Ethernet frames.
1916 * Now add the check for Ethernet_II frames, and
1917 * do that before checking for the other frame
1920 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1924 case ETHERTYPE_ATALK
:
1925 case ETHERTYPE_AARP
:
1927 * EtherTalk (AppleTalk protocols on Ethernet link
1928 * layer) may use 802.2 encapsulation.
1932 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1933 * we check for an Ethernet type field less than
1934 * 1500, which means it's an 802.3 length field.
1936 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1940 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1941 * SNAP packets with an organization code of
1942 * 0x080007 (Apple, for Appletalk) and a protocol
1943 * type of ETHERTYPE_ATALK (Appletalk).
1945 * 802.2-encapsulated ETHERTYPE_AARP packets are
1946 * SNAP packets with an organization code of
1947 * 0x000000 (encapsulated Ethernet) and a protocol
1948 * type of ETHERTYPE_AARP (Appletalk ARP).
1950 if (proto
== ETHERTYPE_ATALK
)
1951 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
1952 else /* proto == ETHERTYPE_AARP */
1953 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
1957 * Check for Ethernet encapsulation (Ethertalk
1958 * phase 1?); we just check for the Ethernet
1961 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1967 if (proto
<= ETHERMTU
) {
1969 * This is an LLC SAP value, so the frames
1970 * that match would be 802.2 frames.
1971 * Check that the frame is an 802.2 frame
1972 * (i.e., that the length/type field is
1973 * a length field, <= ETHERMTU) and
1974 * then check the DSAP.
1976 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1978 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1983 * This is an Ethernet type, so compare
1984 * the length/type field with it (if
1985 * the frame is an 802.2 frame, the length
1986 * field will be <= ETHERMTU, and, as
1987 * "proto" is > ETHERMTU, this test
1988 * will fail and the frame won't match,
1989 * which is what we want).
1991 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
1997 static struct block
*
1998 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2001 * For DLT_NULL, the link-layer header is a 32-bit word
2002 * containing an AF_ value in *host* byte order, and for
2003 * DLT_ENC, the link-layer header begins with a 32-bit
2004 * word containing an AF_ value in host byte order.
2006 * In addition, if we're reading a saved capture file,
2007 * the host byte order in the capture may not be the
2008 * same as the host byte order on this machine.
2010 * For DLT_LOOP, the link-layer header is a 32-bit
2011 * word containing an AF_ value in *network* byte order.
2013 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2015 * The AF_ value is in host byte order, but the BPF
2016 * interpreter will convert it to network byte order.
2018 * If this is a save file, and it's from a machine
2019 * with the opposite byte order to ours, we byte-swap
2022 * Then we run it through "htonl()", and generate
2023 * code to compare against the result.
2025 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2026 proto
= SWAPLONG(proto
);
2027 proto
= htonl(proto
);
2029 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2033 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2034 * or IPv6 then we have an error.
2036 static struct block
*
2037 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2042 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2045 case ETHERTYPE_IPV6
:
2046 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2047 (bpf_int32
)IPH_AF_INET6
);
2054 return gen_false(cstate
);
2058 * Generate code to match a particular packet type.
2060 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2061 * value, if <= ETHERMTU. We use that to determine whether to
2062 * match the type field or to check the type field for the special
2063 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2065 static struct block
*
2066 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2068 struct block
*b0
, *b1
;
2074 case LLCSAP_NETBEUI
:
2076 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2077 * so we check the DSAP and SSAP.
2079 * LLCSAP_IP checks for IP-over-802.2, rather
2080 * than IP-over-Ethernet or IP-over-SNAP.
2082 * XXX - should we check both the DSAP and the
2083 * SSAP, like this, or should we check just the
2084 * DSAP, as we do for other types <= ETHERMTU
2085 * (i.e., other SAP values)?
2087 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2088 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2089 ((proto
<< 8) | proto
));
2095 * Ethernet_II frames, which are Ethernet
2096 * frames with a frame type of ETHERTYPE_IPX;
2098 * Ethernet_802.3 frames, which have a frame
2099 * type of LINUX_SLL_P_802_3;
2101 * Ethernet_802.2 frames, which are 802.3
2102 * frames with an 802.2 LLC header (i.e, have
2103 * a frame type of LINUX_SLL_P_802_2) and
2104 * with the IPX LSAP as the DSAP in the LLC
2107 * Ethernet_SNAP frames, which are 802.3
2108 * frames with an LLC header and a SNAP
2109 * header and with an OUI of 0x000000
2110 * (encapsulated Ethernet) and a protocol
2111 * ID of ETHERTYPE_IPX in the SNAP header.
2113 * First, do the checks on LINUX_SLL_P_802_2
2114 * frames; generate the check for either
2115 * Ethernet_802.2 or Ethernet_SNAP frames, and
2116 * then put a check for LINUX_SLL_P_802_2 frames
2119 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2120 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2122 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2126 * Now check for 802.3 frames and OR that with
2127 * the previous test.
2129 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2133 * Now add the check for Ethernet_II frames, and
2134 * do that before checking for the other frame
2137 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2141 case ETHERTYPE_ATALK
:
2142 case ETHERTYPE_AARP
:
2144 * EtherTalk (AppleTalk protocols on Ethernet link
2145 * layer) may use 802.2 encapsulation.
2149 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2150 * we check for the 802.2 protocol type in the
2151 * "Ethernet type" field.
2153 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2156 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2157 * SNAP packets with an organization code of
2158 * 0x080007 (Apple, for Appletalk) and a protocol
2159 * type of ETHERTYPE_ATALK (Appletalk).
2161 * 802.2-encapsulated ETHERTYPE_AARP packets are
2162 * SNAP packets with an organization code of
2163 * 0x000000 (encapsulated Ethernet) and a protocol
2164 * type of ETHERTYPE_AARP (Appletalk ARP).
2166 if (proto
== ETHERTYPE_ATALK
)
2167 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2168 else /* proto == ETHERTYPE_AARP */
2169 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2173 * Check for Ethernet encapsulation (Ethertalk
2174 * phase 1?); we just check for the Ethernet
2177 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2183 if (proto
<= ETHERMTU
) {
2185 * This is an LLC SAP value, so the frames
2186 * that match would be 802.2 frames.
2187 * Check for the 802.2 protocol type
2188 * in the "Ethernet type" field, and
2189 * then check the DSAP.
2191 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2192 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2198 * This is an Ethernet type, so compare
2199 * the length/type field with it (if
2200 * the frame is an 802.2 frame, the length
2201 * field will be <= ETHERMTU, and, as
2202 * "proto" is > ETHERMTU, this test
2203 * will fail and the frame won't match,
2204 * which is what we want).
2206 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2211 static struct slist
*
2212 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2214 struct slist
*s1
, *s2
;
2215 struct slist
*sjeq_avs_cookie
;
2216 struct slist
*sjcommon
;
2219 * This code is not compatible with the optimizer, as
2220 * we are generating jmp instructions within a normal
2221 * slist of instructions
2223 cstate
->no_optimize
= 1;
2226 * Generate code to load the length of the radio header into
2227 * the register assigned to hold that length, if one has been
2228 * assigned. (If one hasn't been assigned, no code we've
2229 * generated uses that prefix, so we don't need to generate any
2232 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2233 * or always use the AVS header rather than the Prism header.
2234 * We load a 4-byte big-endian value at the beginning of the
2235 * raw packet data, and see whether, when masked with 0xFFFFF000,
2236 * it's equal to 0x80211000. If so, that indicates that it's
2237 * an AVS header (the masked-out bits are the version number).
2238 * Otherwise, it's a Prism header.
2240 * XXX - the Prism header is also, in theory, variable-length,
2241 * but no known software generates headers that aren't 144
2244 if (cstate
->off_linkhdr
.reg
!= -1) {
2248 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2252 * AND it with 0xFFFFF000.
2254 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2255 s2
->s
.k
= 0xFFFFF000;
2259 * Compare with 0x80211000.
2261 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2262 sjeq_avs_cookie
->s
.k
= 0x80211000;
2263 sappend(s1
, sjeq_avs_cookie
);
2268 * The 4 bytes at an offset of 4 from the beginning of
2269 * the AVS header are the length of the AVS header.
2270 * That field is big-endian.
2272 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2275 sjeq_avs_cookie
->s
.jt
= s2
;
2278 * Now jump to the code to allocate a register
2279 * into which to save the header length and
2280 * store the length there. (The "jump always"
2281 * instruction needs to have the k field set;
2282 * it's added to the PC, so, as we're jumping
2283 * over a single instruction, it should be 1.)
2285 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2287 sappend(s1
, sjcommon
);
2290 * Now for the code that handles the Prism header.
2291 * Just load the length of the Prism header (144)
2292 * into the A register. Have the test for an AVS
2293 * header branch here if we don't have an AVS header.
2295 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2298 sjeq_avs_cookie
->s
.jf
= s2
;
2301 * Now allocate a register to hold that value and store
2302 * it. The code for the AVS header will jump here after
2303 * loading the length of the AVS header.
2305 s2
= new_stmt(cstate
, BPF_ST
);
2306 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2308 sjcommon
->s
.jf
= s2
;
2311 * Now move it into the X register.
2313 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2321 static struct slist
*
2322 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2324 struct slist
*s1
, *s2
;
2327 * Generate code to load the length of the AVS header into
2328 * the register assigned to hold that length, if one has been
2329 * assigned. (If one hasn't been assigned, no code we've
2330 * generated uses that prefix, so we don't need to generate any
2333 if (cstate
->off_linkhdr
.reg
!= -1) {
2335 * The 4 bytes at an offset of 4 from the beginning of
2336 * the AVS header are the length of the AVS header.
2337 * That field is big-endian.
2339 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2343 * Now allocate a register to hold that value and store
2346 s2
= new_stmt(cstate
, BPF_ST
);
2347 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2351 * Now move it into the X register.
2353 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2361 static struct slist
*
2362 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2364 struct slist
*s1
, *s2
;
2367 * Generate code to load the length of the radiotap header into
2368 * the register assigned to hold that length, if one has been
2369 * assigned. (If one hasn't been assigned, no code we've
2370 * generated uses that prefix, so we don't need to generate any
2373 if (cstate
->off_linkhdr
.reg
!= -1) {
2375 * The 2 bytes at offsets of 2 and 3 from the beginning
2376 * of the radiotap header are the length of the radiotap
2377 * header; unfortunately, it's little-endian, so we have
2378 * to load it a byte at a time and construct the value.
2382 * Load the high-order byte, at an offset of 3, shift it
2383 * left a byte, and put the result in the X register.
2385 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2387 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2390 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2394 * Load the next byte, at an offset of 2, and OR the
2395 * value from the X register into it.
2397 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2400 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2404 * Now allocate a register to hold that value and store
2407 s2
= new_stmt(cstate
, BPF_ST
);
2408 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2412 * Now move it into the X register.
2414 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2423 * At the moment we treat PPI as normal Radiotap encoded
2424 * packets. The difference is in the function that generates
2425 * the code at the beginning to compute the header length.
2426 * Since this code generator of PPI supports bare 802.11
2427 * encapsulation only (i.e. the encapsulated DLT should be
2428 * DLT_IEEE802_11) we generate code to check for this too;
2429 * that's done in finish_parse().
2431 static struct slist
*
2432 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2434 struct slist
*s1
, *s2
;
2437 * Generate code to load the length of the radiotap header
2438 * into the register assigned to hold that length, if one has
2441 if (cstate
->off_linkhdr
.reg
!= -1) {
2443 * The 2 bytes at offsets of 2 and 3 from the beginning
2444 * of the radiotap header are the length of the radiotap
2445 * header; unfortunately, it's little-endian, so we have
2446 * to load it a byte at a time and construct the value.
2450 * Load the high-order byte, at an offset of 3, shift it
2451 * left a byte, and put the result in the X register.
2453 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2455 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2458 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2462 * Load the next byte, at an offset of 2, and OR the
2463 * value from the X register into it.
2465 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2468 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2472 * Now allocate a register to hold that value and store
2475 s2
= new_stmt(cstate
, BPF_ST
);
2476 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2480 * Now move it into the X register.
2482 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2491 * Load a value relative to the beginning of the link-layer header after the 802.11
2492 * header, i.e. LLC_SNAP.
2493 * The link-layer header doesn't necessarily begin at the beginning
2494 * of the packet data; there might be a variable-length prefix containing
2495 * radio information.
2497 static struct slist
*
2498 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2501 struct slist
*sjset_data_frame_1
;
2502 struct slist
*sjset_data_frame_2
;
2503 struct slist
*sjset_qos
;
2504 struct slist
*sjset_radiotap_flags_present
;
2505 struct slist
*sjset_radiotap_ext_present
;
2506 struct slist
*sjset_radiotap_tsft_present
;
2507 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2508 struct slist
*s_roundup
;
2510 if (cstate
->off_linkpl
.reg
== -1) {
2512 * No register has been assigned to the offset of
2513 * the link-layer payload, which means nobody needs
2514 * it; don't bother computing it - just return
2515 * what we already have.
2521 * This code is not compatible with the optimizer, as
2522 * we are generating jmp instructions within a normal
2523 * slist of instructions
2525 cstate
->no_optimize
= 1;
2528 * If "s" is non-null, it has code to arrange that the X register
2529 * contains the length of the prefix preceding the link-layer
2532 * Otherwise, the length of the prefix preceding the link-layer
2533 * header is "off_outermostlinkhdr.constant_part".
2537 * There is no variable-length header preceding the
2538 * link-layer header.
2540 * Load the length of the fixed-length prefix preceding
2541 * the link-layer header (if any) into the X register,
2542 * and store it in the cstate->off_linkpl.reg register.
2543 * That length is off_outermostlinkhdr.constant_part.
2545 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2546 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2550 * The X register contains the offset of the beginning of the
2551 * link-layer header; add 24, which is the minimum length
2552 * of the MAC header for a data frame, to that, and store it
2553 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2554 * which is at the offset in the X register, with an indexed load.
2556 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2558 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2561 s2
= new_stmt(cstate
, BPF_ST
);
2562 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2565 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2570 * Check the Frame Control field to see if this is a data frame;
2571 * a data frame has the 0x08 bit (b3) in that field set and the
2572 * 0x04 bit (b2) clear.
2574 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2575 sjset_data_frame_1
->s
.k
= 0x08;
2576 sappend(s
, sjset_data_frame_1
);
2579 * If b3 is set, test b2, otherwise go to the first statement of
2580 * the rest of the program.
2582 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2583 sjset_data_frame_2
->s
.k
= 0x04;
2584 sappend(s
, sjset_data_frame_2
);
2585 sjset_data_frame_1
->s
.jf
= snext
;
2588 * If b2 is not set, this is a data frame; test the QoS bit.
2589 * Otherwise, go to the first statement of the rest of the
2592 sjset_data_frame_2
->s
.jt
= snext
;
2593 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2594 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2595 sappend(s
, sjset_qos
);
2598 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2600 * Otherwise, go to the first statement of the rest of the
2603 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2604 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2606 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2609 s2
= new_stmt(cstate
, BPF_ST
);
2610 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2614 * If we have a radiotap header, look at it to see whether
2615 * there's Atheros padding between the MAC-layer header
2618 * Note: all of the fields in the radiotap header are
2619 * little-endian, so we byte-swap all of the values
2620 * we test against, as they will be loaded as big-endian
2623 * XXX - in the general case, we would have to scan through
2624 * *all* the presence bits, if there's more than one word of
2625 * presence bits. That would require a loop, meaning that
2626 * we wouldn't be able to run the filter in the kernel.
2628 * We assume here that the Atheros adapters that insert the
2629 * annoying padding don't have multiple antennae and therefore
2630 * do not generate radiotap headers with multiple presence words.
2632 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2634 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2635 * in the first presence flag word?
2637 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2641 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2642 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2643 sappend(s
, sjset_radiotap_flags_present
);
2646 * If not, skip all of this.
2648 sjset_radiotap_flags_present
->s
.jf
= snext
;
2651 * Otherwise, is the "extension" bit set in that word?
2653 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2654 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2655 sappend(s
, sjset_radiotap_ext_present
);
2656 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2659 * If so, skip all of this.
2661 sjset_radiotap_ext_present
->s
.jt
= snext
;
2664 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2666 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2667 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2668 sappend(s
, sjset_radiotap_tsft_present
);
2669 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2672 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2673 * at an offset of 16 from the beginning of the raw packet
2674 * data (8 bytes for the radiotap header and 8 bytes for
2677 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2680 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2683 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2685 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2686 sjset_tsft_datapad
->s
.k
= 0x20;
2687 sappend(s
, sjset_tsft_datapad
);
2690 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2691 * at an offset of 8 from the beginning of the raw packet
2692 * data (8 bytes for the radiotap header).
2694 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2697 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2700 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2702 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2703 sjset_notsft_datapad
->s
.k
= 0x20;
2704 sappend(s
, sjset_notsft_datapad
);
2707 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2708 * set, round the length of the 802.11 header to
2709 * a multiple of 4. Do that by adding 3 and then
2710 * dividing by and multiplying by 4, which we do by
2713 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2714 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2715 sappend(s
, s_roundup
);
2716 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2719 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2722 s2
= new_stmt(cstate
, BPF_ST
);
2723 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2726 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2727 sjset_tsft_datapad
->s
.jf
= snext
;
2728 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2729 sjset_notsft_datapad
->s
.jf
= snext
;
2731 sjset_qos
->s
.jf
= snext
;
2737 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2741 /* There is an implicit dependency between the link
2742 * payload and link header since the payload computation
2743 * includes the variable part of the header. Therefore,
2744 * if nobody else has allocated a register for the link
2745 * header and we need it, do it now. */
2746 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2747 cstate
->off_linkhdr
.reg
== -1)
2748 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2751 * For link-layer types that have a variable-length header
2752 * preceding the link-layer header, generate code to load
2753 * the offset of the link-layer header into the register
2754 * assigned to that offset, if any.
2756 * XXX - this, and the next switch statement, won't handle
2757 * encapsulation of 802.11 or 802.11+radio information in
2758 * some other protocol stack. That's significantly more
2761 switch (cstate
->outermostlinktype
) {
2763 case DLT_PRISM_HEADER
:
2764 s
= gen_load_prism_llprefixlen(cstate
);
2767 case DLT_IEEE802_11_RADIO_AVS
:
2768 s
= gen_load_avs_llprefixlen(cstate
);
2771 case DLT_IEEE802_11_RADIO
:
2772 s
= gen_load_radiotap_llprefixlen(cstate
);
2776 s
= gen_load_ppi_llprefixlen(cstate
);
2785 * For link-layer types that have a variable-length link-layer
2786 * header, generate code to load the offset of the link-layer
2787 * payload into the register assigned to that offset, if any.
2789 switch (cstate
->outermostlinktype
) {
2791 case DLT_IEEE802_11
:
2792 case DLT_PRISM_HEADER
:
2793 case DLT_IEEE802_11_RADIO_AVS
:
2794 case DLT_IEEE802_11_RADIO
:
2796 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2801 * If we have any offset-loading code, append all the
2802 * existing statements in the block to those statements,
2803 * and make the resulting list the list of statements
2807 sappend(s
, b
->stmts
);
2812 static struct block
*
2813 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2815 struct slist
*s_load_dlt
;
2818 if (cstate
->linktype
== DLT_PPI
)
2820 /* Create the statements that check for the DLT
2822 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2823 s_load_dlt
->s
.k
= 4;
2825 b
= new_block(cstate
, JMP(BPF_JEQ
));
2827 b
->stmts
= s_load_dlt
;
2828 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2839 * Take an absolute offset, and:
2841 * if it has no variable part, return NULL;
2843 * if it has a variable part, generate code to load the register
2844 * containing that variable part into the X register, returning
2845 * a pointer to that code - if no register for that offset has
2846 * been allocated, allocate it first.
2848 * (The code to set that register will be generated later, but will
2849 * be placed earlier in the code sequence.)
2851 static struct slist
*
2852 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2856 if (off
->is_variable
) {
2857 if (off
->reg
== -1) {
2859 * We haven't yet assigned a register for the
2860 * variable part of the offset of the link-layer
2861 * header; allocate one.
2863 off
->reg
= alloc_reg(cstate
);
2867 * Load the register containing the variable part of the
2868 * offset of the link-layer header into the X register.
2870 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2875 * That offset isn't variable, there's no variable part,
2876 * so we don't need to generate any code.
2883 * Map an Ethernet type to the equivalent PPP type.
2886 ethertype_to_ppptype(proto
)
2895 case ETHERTYPE_IPV6
:
2903 case ETHERTYPE_ATALK
:
2917 * I'm assuming the "Bridging PDU"s that go
2918 * over PPP are Spanning Tree Protocol
2932 * Generate any tests that, for encapsulation of a link-layer packet
2933 * inside another protocol stack, need to be done to check for those
2934 * link-layer packets (and that haven't already been done by a check
2935 * for that encapsulation).
2937 static struct block
*
2938 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
2942 if (cstate
->is_geneve
)
2943 return gen_geneve_ll_check(cstate
);
2945 switch (cstate
->prevlinktype
) {
2949 * This is LANE-encapsulated Ethernet; check that the LANE
2950 * packet doesn't begin with an LE Control marker, i.e.
2951 * that it's data, not a control message.
2953 * (We've already generated a test for LANE.)
2955 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2961 * No such tests are necessary.
2969 * The three different values we should check for when checking for an
2970 * IPv6 packet with DLT_NULL.
2972 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
2973 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
2974 #define BSD_AFNUM_INET6_DARWIN 30 /* OS X, iOS, other Darwin-based OSes */
2977 * Generate code to match a particular packet type by matching the
2978 * link-layer type field or fields in the 802.2 LLC header.
2980 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2981 * value, if <= ETHERMTU.
2983 static struct block
*
2984 gen_linktype(compiler_state_t
*cstate
, int proto
)
2986 struct block
*b0
, *b1
, *b2
;
2987 const char *description
;
2989 /* are we checking MPLS-encapsulated packets? */
2990 if (cstate
->label_stack_depth
> 0) {
2994 /* FIXME add other L3 proto IDs */
2995 return gen_mpls_linktype(cstate
, Q_IP
);
2997 case ETHERTYPE_IPV6
:
2999 /* FIXME add other L3 proto IDs */
3000 return gen_mpls_linktype(cstate
, Q_IPV6
);
3003 bpf_error(cstate
, "unsupported protocol over mpls");
3008 switch (cstate
->linktype
) {
3011 case DLT_NETANALYZER
:
3012 case DLT_NETANALYZER_TRANSPARENT
:
3013 /* Geneve has an EtherType regardless of whether there is an
3015 if (!cstate
->is_geneve
)
3016 b0
= gen_prevlinkhdr_check(cstate
);
3020 b1
= gen_ether_linktype(cstate
, proto
);
3031 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3035 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3041 case DLT_IEEE802_11
:
3042 case DLT_PRISM_HEADER
:
3043 case DLT_IEEE802_11_RADIO_AVS
:
3044 case DLT_IEEE802_11_RADIO
:
3047 * Check that we have a data frame.
3049 b0
= gen_check_802_11_data_frame(cstate
);
3052 * Now check for the specified link-layer type.
3054 b1
= gen_llc_linktype(cstate
, proto
);
3062 * XXX - check for LLC frames.
3064 return gen_llc_linktype(cstate
, proto
);
3070 * XXX - check for LLC PDUs, as per IEEE 802.5.
3072 return gen_llc_linktype(cstate
, proto
);
3076 case DLT_ATM_RFC1483
:
3078 case DLT_IP_OVER_FC
:
3079 return gen_llc_linktype(cstate
, proto
);
3085 * Check for an LLC-encapsulated version of this protocol;
3086 * if we were checking for LANE, linktype would no longer
3089 * Check for LLC encapsulation and then check the protocol.
3091 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3092 b1
= gen_llc_linktype(cstate
, proto
);
3099 return gen_linux_sll_linktype(cstate
, proto
);
3104 case DLT_SLIP_BSDOS
:
3107 * These types don't provide any type field; packets
3108 * are always IPv4 or IPv6.
3110 * XXX - for IPv4, check for a version number of 4, and,
3111 * for IPv6, check for a version number of 6?
3116 /* Check for a version number of 4. */
3117 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3119 case ETHERTYPE_IPV6
:
3120 /* Check for a version number of 6. */
3121 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3124 return gen_false(cstate
); /* always false */
3131 * Raw IPv4, so no type field.
3133 if (proto
== ETHERTYPE_IP
)
3134 return gen_true(cstate
); /* always true */
3136 /* Checking for something other than IPv4; always false */
3137 return gen_false(cstate
);
3143 * Raw IPv6, so no type field.
3145 if (proto
== ETHERTYPE_IPV6
)
3146 return gen_true(cstate
); /* always true */
3148 /* Checking for something other than IPv6; always false */
3149 return gen_false(cstate
);
3155 case DLT_PPP_SERIAL
:
3158 * We use Ethernet protocol types inside libpcap;
3159 * map them to the corresponding PPP protocol types.
3161 proto
= ethertype_to_ppptype(proto
);
3162 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3168 * We use Ethernet protocol types inside libpcap;
3169 * map them to the corresponding PPP protocol types.
3175 * Also check for Van Jacobson-compressed IP.
3176 * XXX - do this for other forms of PPP?
3178 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3179 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3181 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3186 proto
= ethertype_to_ppptype(proto
);
3187 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3199 return (gen_loopback_linktype(cstate
, AF_INET
));
3201 case ETHERTYPE_IPV6
:
3203 * AF_ values may, unfortunately, be platform-
3204 * dependent; AF_INET isn't, because everybody
3205 * used 4.2BSD's value, but AF_INET6 is, because
3206 * 4.2BSD didn't have a value for it (given that
3207 * IPv6 didn't exist back in the early 1980's),
3208 * and they all picked their own values.
3210 * This means that, if we're reading from a
3211 * savefile, we need to check for all the
3214 * If we're doing a live capture, we only need
3215 * to check for this platform's value; however,
3216 * Npcap uses 24, which isn't Windows's AF_INET6
3217 * value. (Given the multiple different values,
3218 * programs that read pcap files shouldn't be
3219 * checking for their platform's AF_INET6 value
3220 * anyway, they should check for all of the
3221 * possible values. and they might as well do
3222 * that even for live captures.)
3224 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3226 * Savefile - check for all three
3227 * possible IPv6 values.
3229 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3230 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3232 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3237 * Live capture, so we only need to
3238 * check for the value used on this
3243 * Npcap doesn't use Windows's AF_INET6,
3244 * as that collides with AF_IPX on
3245 * some BSDs (both have the value 23).
3246 * Instead, it uses 24.
3248 return (gen_loopback_linktype(cstate
, 24));
3251 return (gen_loopback_linktype(cstate
, AF_INET6
));
3252 #else /* AF_INET6 */
3254 * I guess this platform doesn't support
3255 * IPv6, so we just reject all packets.
3257 return gen_false(cstate
);
3258 #endif /* AF_INET6 */
3264 * Not a type on which we support filtering.
3265 * XXX - support those that have AF_ values
3266 * #defined on this platform, at least?
3268 return gen_false(cstate
);
3271 #ifdef HAVE_NET_PFVAR_H
3274 * af field is host byte order in contrast to the rest of
3277 if (proto
== ETHERTYPE_IP
)
3278 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3279 BPF_B
, (bpf_int32
)AF_INET
));
3280 else if (proto
== ETHERTYPE_IPV6
)
3281 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3282 BPF_B
, (bpf_int32
)AF_INET6
));
3284 return gen_false(cstate
);
3287 #endif /* HAVE_NET_PFVAR_H */
3290 case DLT_ARCNET_LINUX
:
3292 * XXX should we check for first fragment if the protocol
3298 return gen_false(cstate
);
3300 case ETHERTYPE_IPV6
:
3301 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3302 (bpf_int32
)ARCTYPE_INET6
));
3305 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3306 (bpf_int32
)ARCTYPE_IP
);
3307 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3308 (bpf_int32
)ARCTYPE_IP_OLD
);
3313 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3314 (bpf_int32
)ARCTYPE_ARP
);
3315 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3316 (bpf_int32
)ARCTYPE_ARP_OLD
);
3320 case ETHERTYPE_REVARP
:
3321 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3322 (bpf_int32
)ARCTYPE_REVARP
));
3324 case ETHERTYPE_ATALK
:
3325 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3326 (bpf_int32
)ARCTYPE_ATALK
));
3333 case ETHERTYPE_ATALK
:
3334 return gen_true(cstate
);
3336 return gen_false(cstate
);
3343 * XXX - assumes a 2-byte Frame Relay header with
3344 * DLCI and flags. What if the address is longer?
3350 * Check for the special NLPID for IP.
3352 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3354 case ETHERTYPE_IPV6
:
3356 * Check for the special NLPID for IPv6.
3358 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3362 * Check for several OSI protocols.
3364 * Frame Relay packets typically have an OSI
3365 * NLPID at the beginning; we check for each
3368 * What we check for is the NLPID and a frame
3369 * control field of UI, i.e. 0x03 followed
3372 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3373 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3374 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3380 return gen_false(cstate
);
3386 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3388 case DLT_JUNIPER_MFR
:
3389 case DLT_JUNIPER_MLFR
:
3390 case DLT_JUNIPER_MLPPP
:
3391 case DLT_JUNIPER_ATM1
:
3392 case DLT_JUNIPER_ATM2
:
3393 case DLT_JUNIPER_PPPOE
:
3394 case DLT_JUNIPER_PPPOE_ATM
:
3395 case DLT_JUNIPER_GGSN
:
3396 case DLT_JUNIPER_ES
:
3397 case DLT_JUNIPER_MONITOR
:
3398 case DLT_JUNIPER_SERVICES
:
3399 case DLT_JUNIPER_ETHER
:
3400 case DLT_JUNIPER_PPP
:
3401 case DLT_JUNIPER_FRELAY
:
3402 case DLT_JUNIPER_CHDLC
:
3403 case DLT_JUNIPER_VP
:
3404 case DLT_JUNIPER_ST
:
3405 case DLT_JUNIPER_ISM
:
3406 case DLT_JUNIPER_VS
:
3407 case DLT_JUNIPER_SRX_E2E
:
3408 case DLT_JUNIPER_FIBRECHANNEL
:
3409 case DLT_JUNIPER_ATM_CEMIC
:
3411 /* just lets verify the magic number for now -
3412 * on ATM we may have up to 6 different encapsulations on the wire
3413 * and need a lot of heuristics to figure out that the payload
3416 * FIXME encapsulation specific BPF_ filters
3418 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3420 case DLT_BACNET_MS_TP
:
3421 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3424 return gen_ipnet_linktype(cstate
, proto
);
3426 case DLT_LINUX_IRDA
:
3427 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3430 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3433 case DLT_MTP2_WITH_PHDR
:
3434 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3437 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3440 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3442 case DLT_LINUX_LAPD
:
3443 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3445 case DLT_USB_FREEBSD
:
3447 case DLT_USB_LINUX_MMAPPED
:
3449 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3451 case DLT_BLUETOOTH_HCI_H4
:
3452 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3453 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3456 case DLT_CAN_SOCKETCAN
:
3457 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3459 case DLT_IEEE802_15_4
:
3460 case DLT_IEEE802_15_4_LINUX
:
3461 case DLT_IEEE802_15_4_NONASK_PHY
:
3462 case DLT_IEEE802_15_4_NOFCS
:
3463 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3465 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3466 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3469 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3472 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3475 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3478 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3481 /* Using the fixed-size NFLOG header it is possible to tell only
3482 * the address family of the packet, other meaningful data is
3483 * either missing or behind TLVs.
3485 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3489 * Does this link-layer header type have a field
3490 * indicating the type of the next protocol? If
3491 * so, off_linktype.constant_part will be the offset of that
3492 * field in the packet; if not, it will be OFFSET_NOT_SET.
3494 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3496 * Yes; assume it's an Ethernet type. (If
3497 * it's not, it needs to be handled specially
3500 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3503 * No; report an error.
3505 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3506 if (description
!= NULL
) {
3507 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3510 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3519 * Check for an LLC SNAP packet with a given organization code and
3520 * protocol type; we check the entire contents of the 802.2 LLC and
3521 * snap headers, checking for DSAP and SSAP of SNAP and a control
3522 * field of 0x03 in the LLC header, and for the specified organization
3523 * code and protocol type in the SNAP header.
3525 static struct block
*
3526 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3528 u_char snapblock
[8];
3530 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3531 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3532 snapblock
[2] = 0x03; /* control = UI */
3533 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3534 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3535 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3536 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3537 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3538 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3542 * Generate code to match frames with an LLC header.
3545 gen_llc(compiler_state_t
*cstate
)
3547 struct block
*b0
, *b1
;
3549 switch (cstate
->linktype
) {
3553 * We check for an Ethernet type field less than
3554 * 1500, which means it's an 802.3 length field.
3556 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3560 * Now check for the purported DSAP and SSAP not being
3561 * 0xFF, to rule out NetWare-over-802.3.
3563 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3570 * We check for LLC traffic.
3572 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3575 case DLT_IEEE802
: /* Token Ring */
3577 * XXX - check for LLC frames.
3579 return gen_true(cstate
);
3583 * XXX - check for LLC frames.
3585 return gen_true(cstate
);
3587 case DLT_ATM_RFC1483
:
3589 * For LLC encapsulation, these are defined to have an
3592 * For VC encapsulation, they don't, but there's no
3593 * way to check for that; the protocol used on the VC
3594 * is negotiated out of band.
3596 return gen_true(cstate
);
3598 case DLT_IEEE802_11
:
3599 case DLT_PRISM_HEADER
:
3600 case DLT_IEEE802_11_RADIO
:
3601 case DLT_IEEE802_11_RADIO_AVS
:
3604 * Check that we have a data frame.
3606 b0
= gen_check_802_11_data_frame(cstate
);
3610 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3616 gen_llc_i(compiler_state_t
*cstate
)
3618 struct block
*b0
, *b1
;
3622 * Check whether this is an LLC frame.
3624 b0
= gen_llc(cstate
);
3627 * Load the control byte and test the low-order bit; it must
3628 * be clear for I frames.
3630 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3631 b1
= new_block(cstate
, JMP(BPF_JSET
));
3640 gen_llc_s(compiler_state_t
*cstate
)
3642 struct block
*b0
, *b1
;
3645 * Check whether this is an LLC frame.
3647 b0
= gen_llc(cstate
);
3650 * Now compare the low-order 2 bit of the control byte against
3651 * the appropriate value for S frames.
3653 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3659 gen_llc_u(compiler_state_t
*cstate
)
3661 struct block
*b0
, *b1
;
3664 * Check whether this is an LLC frame.
3666 b0
= gen_llc(cstate
);
3669 * Now compare the low-order 2 bit of the control byte against
3670 * the appropriate value for U frames.
3672 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3678 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3680 struct block
*b0
, *b1
;
3683 * Check whether this is an LLC frame.
3685 b0
= gen_llc(cstate
);
3688 * Now check for an S frame with the appropriate type.
3690 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3696 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3698 struct block
*b0
, *b1
;
3701 * Check whether this is an LLC frame.
3703 b0
= gen_llc(cstate
);
3706 * Now check for a U frame with the appropriate type.
3708 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3714 * Generate code to match a particular packet type, for link-layer types
3715 * using 802.2 LLC headers.
3717 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3718 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3720 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3721 * value, if <= ETHERMTU. We use that to determine whether to
3722 * match the DSAP or both DSAP and LSAP or to check the OUI and
3723 * protocol ID in a SNAP header.
3725 static struct block
*
3726 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3729 * XXX - handle token-ring variable-length header.
3735 case LLCSAP_NETBEUI
:
3737 * XXX - should we check both the DSAP and the
3738 * SSAP, like this, or should we check just the
3739 * DSAP, as we do for other SAP values?
3741 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3742 ((proto
<< 8) | proto
));
3746 * XXX - are there ever SNAP frames for IPX on
3747 * non-Ethernet 802.x networks?
3749 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3750 (bpf_int32
)LLCSAP_IPX
);
3752 case ETHERTYPE_ATALK
:
3754 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3755 * SNAP packets with an organization code of
3756 * 0x080007 (Apple, for Appletalk) and a protocol
3757 * type of ETHERTYPE_ATALK (Appletalk).
3759 * XXX - check for an organization code of
3760 * encapsulated Ethernet as well?
3762 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3766 * XXX - we don't have to check for IPX 802.3
3767 * here, but should we check for the IPX Ethertype?
3769 if (proto
<= ETHERMTU
) {
3771 * This is an LLC SAP value, so check
3774 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3777 * This is an Ethernet type; we assume that it's
3778 * unlikely that it'll appear in the right place
3779 * at random, and therefore check only the
3780 * location that would hold the Ethernet type
3781 * in a SNAP frame with an organization code of
3782 * 0x000000 (encapsulated Ethernet).
3784 * XXX - if we were to check for the SNAP DSAP and
3785 * LSAP, as per XXX, and were also to check for an
3786 * organization code of 0x000000 (encapsulated
3787 * Ethernet), we'd do
3789 * return gen_snap(cstate, 0x000000, proto);
3791 * here; for now, we don't, as per the above.
3792 * I don't know whether it's worth the extra CPU
3793 * time to do the right check or not.
3795 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3800 static struct block
*
3801 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3802 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3804 struct block
*b0
, *b1
;
3818 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3819 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3825 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3826 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3833 b0
= gen_linktype(cstate
, proto
);
3834 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3840 static struct block
*
3841 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3842 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3844 struct block
*b0
, *b1
;
3859 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3860 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3866 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3867 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3874 /* this order is important */
3875 a
= (u_int32_t
*)addr
;
3876 m
= (u_int32_t
*)mask
;
3877 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3878 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3880 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3882 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3884 b0
= gen_linktype(cstate
, proto
);
3890 static struct block
*
3891 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3893 register struct block
*b0
, *b1
;
3897 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
3900 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
3903 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3904 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3910 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3911 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3916 bpf_error(cstate
, "'addr1' is only supported on 802.11 with 802.11 headers");
3920 bpf_error(cstate
, "'addr2' is only supported on 802.11 with 802.11 headers");
3924 bpf_error(cstate
, "'addr3' is only supported on 802.11 with 802.11 headers");
3928 bpf_error(cstate
, "'addr4' is only supported on 802.11 with 802.11 headers");
3932 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
3936 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
3944 * Like gen_ehostop, but for DLT_FDDI
3946 static struct block
*
3947 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3949 struct block
*b0
, *b1
;
3953 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
3956 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
3959 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
3960 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
3966 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
3967 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
3972 bpf_error(cstate
, "'addr1' is only supported on 802.11");
3976 bpf_error(cstate
, "'addr2' is only supported on 802.11");
3980 bpf_error(cstate
, "'addr3' is only supported on 802.11");
3984 bpf_error(cstate
, "'addr4' is only supported on 802.11");
3988 bpf_error(cstate
, "'ra' is only supported on 802.11");
3992 bpf_error(cstate
, "'ta' is only supported on 802.11");
4000 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4002 static struct block
*
4003 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4005 register struct block
*b0
, *b1
;
4009 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4012 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4015 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4016 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4022 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4023 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4028 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4032 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4036 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4040 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4044 bpf_error(cstate
, "'ra' is only supported on 802.11");
4048 bpf_error(cstate
, "'ta' is only supported on 802.11");
4056 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4057 * various 802.11 + radio headers.
4059 static struct block
*
4060 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4062 register struct block
*b0
, *b1
, *b2
;
4063 register struct slist
*s
;
4065 #ifdef ENABLE_WLAN_FILTERING_PATCH
4068 * We need to disable the optimizer because the optimizer is buggy
4069 * and wipes out some LD instructions generated by the below
4070 * code to validate the Frame Control bits
4072 cstate
->no_optimize
= 1;
4073 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4080 * For control frames, there is no SA.
4082 * For management frames, SA is at an
4083 * offset of 10 from the beginning of
4086 * For data frames, SA is at an offset
4087 * of 10 from the beginning of the packet
4088 * if From DS is clear, at an offset of
4089 * 16 from the beginning of the packet
4090 * if From DS is set and To DS is clear,
4091 * and an offset of 24 from the beginning
4092 * of the packet if From DS is set and To DS
4097 * Generate the tests to be done for data frames
4100 * First, check for To DS set, i.e. check "link[1] & 0x01".
4102 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4103 b1
= new_block(cstate
, JMP(BPF_JSET
));
4104 b1
->s
.k
= 0x01; /* To DS */
4108 * If To DS is set, the SA is at 24.
4110 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4114 * Now, check for To DS not set, i.e. check
4115 * "!(link[1] & 0x01)".
4117 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4118 b2
= new_block(cstate
, JMP(BPF_JSET
));
4119 b2
->s
.k
= 0x01; /* To DS */
4124 * If To DS is not set, the SA is at 16.
4126 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4130 * Now OR together the last two checks. That gives
4131 * the complete set of checks for data frames with
4137 * Now check for From DS being set, and AND that with
4138 * the ORed-together checks.
4140 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4141 b1
= new_block(cstate
, JMP(BPF_JSET
));
4142 b1
->s
.k
= 0x02; /* From DS */
4147 * Now check for data frames with From DS not set.
4149 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4150 b2
= new_block(cstate
, JMP(BPF_JSET
));
4151 b2
->s
.k
= 0x02; /* From DS */
4156 * If From DS isn't set, the SA is at 10.
4158 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4162 * Now OR together the checks for data frames with
4163 * From DS not set and for data frames with From DS
4164 * set; that gives the checks done for data frames.
4169 * Now check for a data frame.
4170 * I.e, check "link[0] & 0x08".
4172 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4173 b1
= new_block(cstate
, JMP(BPF_JSET
));
4178 * AND that with the checks done for data frames.
4183 * If the high-order bit of the type value is 0, this
4184 * is a management frame.
4185 * I.e, check "!(link[0] & 0x08)".
4187 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4188 b2
= new_block(cstate
, JMP(BPF_JSET
));
4194 * For management frames, the SA is at 10.
4196 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4200 * OR that with the checks done for data frames.
4201 * That gives the checks done for management and
4207 * If the low-order bit of the type value is 1,
4208 * this is either a control frame or a frame
4209 * with a reserved type, and thus not a
4212 * I.e., check "!(link[0] & 0x04)".
4214 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4215 b1
= new_block(cstate
, JMP(BPF_JSET
));
4221 * AND that with the checks for data and management
4231 * For control frames, there is no DA.
4233 * For management frames, DA is at an
4234 * offset of 4 from the beginning of
4237 * For data frames, DA is at an offset
4238 * of 4 from the beginning of the packet
4239 * if To DS is clear and at an offset of
4240 * 16 from the beginning of the packet
4245 * Generate the tests to be done for data frames.
4247 * First, check for To DS set, i.e. "link[1] & 0x01".
4249 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4250 b1
= new_block(cstate
, JMP(BPF_JSET
));
4251 b1
->s
.k
= 0x01; /* To DS */
4255 * If To DS is set, the DA is at 16.
4257 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4261 * Now, check for To DS not set, i.e. check
4262 * "!(link[1] & 0x01)".
4264 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4265 b2
= new_block(cstate
, JMP(BPF_JSET
));
4266 b2
->s
.k
= 0x01; /* To DS */
4271 * If To DS is not set, the DA is at 4.
4273 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4277 * Now OR together the last two checks. That gives
4278 * the complete set of checks for data frames.
4283 * Now check for a data frame.
4284 * I.e, check "link[0] & 0x08".
4286 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4287 b1
= new_block(cstate
, JMP(BPF_JSET
));
4292 * AND that with the checks done for data frames.
4297 * If the high-order bit of the type value is 0, this
4298 * is a management frame.
4299 * I.e, check "!(link[0] & 0x08)".
4301 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4302 b2
= new_block(cstate
, JMP(BPF_JSET
));
4308 * For management frames, the DA is at 4.
4310 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4314 * OR that with the checks done for data frames.
4315 * That gives the checks done for management and
4321 * If the low-order bit of the type value is 1,
4322 * this is either a control frame or a frame
4323 * with a reserved type, and thus not a
4326 * I.e., check "!(link[0] & 0x04)".
4328 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4329 b1
= new_block(cstate
, JMP(BPF_JSET
));
4335 * AND that with the checks for data and management
4343 * Not present in management frames; addr1 in other
4348 * If the high-order bit of the type value is 0, this
4349 * is a management frame.
4350 * I.e, check "(link[0] & 0x08)".
4352 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4353 b1
= new_block(cstate
, JMP(BPF_JSET
));
4360 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4363 * AND that with the check of addr1.
4370 * Not present in management frames; addr2, if present,
4375 * Not present in CTS or ACK control frames.
4377 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4378 IEEE80211_FC0_TYPE_MASK
);
4380 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4381 IEEE80211_FC0_SUBTYPE_MASK
);
4383 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4384 IEEE80211_FC0_SUBTYPE_MASK
);
4390 * If the high-order bit of the type value is 0, this
4391 * is a management frame.
4392 * I.e, check "(link[0] & 0x08)".
4394 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4395 b1
= new_block(cstate
, JMP(BPF_JSET
));
4400 * AND that with the check for frames other than
4401 * CTS and ACK frames.
4408 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4413 * XXX - add BSSID keyword?
4416 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4420 * Not present in CTS or ACK control frames.
4422 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4423 IEEE80211_FC0_TYPE_MASK
);
4425 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4426 IEEE80211_FC0_SUBTYPE_MASK
);
4428 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4429 IEEE80211_FC0_SUBTYPE_MASK
);
4433 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4439 * Not present in control frames.
4441 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4442 IEEE80211_FC0_TYPE_MASK
);
4444 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4450 * Present only if the direction mask has both "From DS"
4451 * and "To DS" set. Neither control frames nor management
4452 * frames should have both of those set, so we don't
4453 * check the frame type.
4455 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4456 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4457 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4462 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4463 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4469 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4470 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4479 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4480 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4481 * as the RFC states.)
4483 static struct block
*
4484 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4486 register struct block
*b0
, *b1
;
4490 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4493 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4496 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4497 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4503 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4504 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4509 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4513 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4517 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4521 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4525 bpf_error(cstate
, "'ra' is only supported on 802.11");
4529 bpf_error(cstate
, "'ta' is only supported on 802.11");
4537 * This is quite tricky because there may be pad bytes in front of the
4538 * DECNET header, and then there are two possible data packet formats that
4539 * carry both src and dst addresses, plus 5 packet types in a format that
4540 * carries only the src node, plus 2 types that use a different format and
4541 * also carry just the src node.
4545 * Instead of doing those all right, we just look for data packets with
4546 * 0 or 1 bytes of padding. If you want to look at other packets, that
4547 * will require a lot more hacking.
4549 * To add support for filtering on DECNET "areas" (network numbers)
4550 * one would want to add a "mask" argument to this routine. That would
4551 * make the filter even more inefficient, although one could be clever
4552 * and not generate masking instructions if the mask is 0xFFFF.
4554 static struct block
*
4555 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4557 struct block
*b0
, *b1
, *b2
, *tmp
;
4558 u_int offset_lh
; /* offset if long header is received */
4559 u_int offset_sh
; /* offset if short header is received */
4564 offset_sh
= 1; /* follows flags */
4565 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4569 offset_sh
= 3; /* follows flags, dstnode */
4570 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4574 /* Inefficient because we do our Calvinball dance twice */
4575 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4576 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4582 /* Inefficient because we do our Calvinball dance twice */
4583 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4584 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4589 bpf_error(cstate
, "ISO host filtering not implemented");
4594 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4595 /* Check for pad = 1, long header case */
4596 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4597 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4598 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4599 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4601 /* Check for pad = 0, long header case */
4602 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4603 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4606 /* Check for pad = 1, short header case */
4607 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4608 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4609 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4612 /* Check for pad = 0, short header case */
4613 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4614 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4618 /* Combine with test for cstate->linktype */
4624 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4625 * test the bottom-of-stack bit, and then check the version number
4626 * field in the IP header.
4628 static struct block
*
4629 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4631 struct block
*b0
, *b1
;
4636 /* match the bottom-of-stack bit */
4637 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4638 /* match the IPv4 version number */
4639 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4644 /* match the bottom-of-stack bit */
4645 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4646 /* match the IPv4 version number */
4647 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4656 static struct block
*
4657 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4658 int proto
, int dir
, int type
)
4660 struct block
*b0
, *b1
;
4661 const char *typestr
;
4671 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4673 * Only check for non-IPv4 addresses if we're not
4674 * checking MPLS-encapsulated packets.
4676 if (cstate
->label_stack_depth
== 0) {
4677 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4679 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4685 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4688 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4691 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4694 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4697 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4700 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4703 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4706 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4709 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4712 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4715 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4718 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4721 bpf_error(cstate
, "ATALK host filtering not implemented");
4724 bpf_error(cstate
, "AARP host filtering not implemented");
4727 return gen_dnhostop(cstate
, addr
, dir
);
4730 bpf_error(cstate
, "SCA host filtering not implemented");
4733 bpf_error(cstate
, "LAT host filtering not implemented");
4736 bpf_error(cstate
, "MOPDL host filtering not implemented");
4739 bpf_error(cstate
, "MOPRC host filtering not implemented");
4742 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4745 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4748 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4751 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4754 bpf_error(cstate
, "ISO host filtering not implemented");
4757 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4760 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4763 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4766 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4769 bpf_error(cstate
, "IPX host filtering not implemented");
4772 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4775 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4784 static struct block
*
4785 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4786 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4788 const char *typestr
;
4798 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4801 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4804 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4807 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4810 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4813 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4816 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4819 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4822 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4825 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4828 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4831 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4834 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4837 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4840 bpf_error(cstate
, "ATALK host filtering not implemented");
4843 bpf_error(cstate
, "AARP host filtering not implemented");
4846 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
4849 bpf_error(cstate
, "SCA host filtering not implemented");
4852 bpf_error(cstate
, "LAT host filtering not implemented");
4855 bpf_error(cstate
, "MOPDL host filtering not implemented");
4858 bpf_error(cstate
, "MOPRC host filtering not implemented");
4861 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4864 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4867 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4870 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4873 bpf_error(cstate
, "ISO host filtering not implemented");
4876 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4879 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4882 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4885 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4888 bpf_error(cstate
, "IPX host filtering not implemented");
4891 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4894 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4904 static struct block
*
4905 gen_gateway(eaddr
, alist
, proto
, dir
)
4906 const u_char
*eaddr
;
4907 bpf_u_int32
**alist
;
4911 struct block
*b0
, *b1
, *tmp
;
4914 bpf_error(cstate
, "direction applied to 'gateway'");
4921 switch (cstate
->linktype
) {
4923 case DLT_NETANALYZER
:
4924 case DLT_NETANALYZER_TRANSPARENT
:
4925 b1
= gen_prevlinkhdr_check(cstate
);
4926 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
4931 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
4934 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
4936 case DLT_IEEE802_11
:
4937 case DLT_PRISM_HEADER
:
4938 case DLT_IEEE802_11_RADIO_AVS
:
4939 case DLT_IEEE802_11_RADIO
:
4941 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
4945 * This is LLC-multiplexed traffic; if it were
4946 * LANE, cstate->linktype would have been set to
4950 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4952 case DLT_IP_OVER_FC
:
4953 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
4957 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4959 b1
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4961 tmp
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
,
4970 bpf_error(cstate
, "illegal modifier of 'gateway'");
4976 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
4984 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4985 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4990 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4991 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4996 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4997 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5002 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5005 #ifndef IPPROTO_IGMP
5006 #define IPPROTO_IGMP 2
5010 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5013 #ifndef IPPROTO_IGRP
5014 #define IPPROTO_IGRP 9
5017 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5021 #define IPPROTO_PIM 103
5025 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5026 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5030 #ifndef IPPROTO_VRRP
5031 #define IPPROTO_VRRP 112
5035 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5038 #ifndef IPPROTO_CARP
5039 #define IPPROTO_CARP 112
5043 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5047 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5051 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5055 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5059 bpf_error(cstate
, "link layer applied in wrong context");
5062 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5066 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5070 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5074 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5078 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5082 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5086 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5090 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5093 #ifndef IPPROTO_ICMPV6
5094 #define IPPROTO_ICMPV6 58
5097 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5101 #define IPPROTO_AH 51
5104 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5105 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5110 #define IPPROTO_ESP 50
5113 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5114 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5119 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5123 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5127 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5130 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5131 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5132 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5134 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5136 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5138 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5142 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5143 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5144 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5146 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5148 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5150 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5154 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5155 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5156 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5158 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5163 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5164 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5169 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5170 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5172 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5174 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5179 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5180 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5185 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5186 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5191 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5195 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5199 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5203 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5207 bpf_error(cstate
, "'radio' is not a valid protocol type");
5215 static struct block
*
5216 gen_ipfrag(compiler_state_t
*cstate
)
5221 /* not IPv4 frag other than the first frag */
5222 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5223 b
= new_block(cstate
, JMP(BPF_JSET
));
5232 * Generate a comparison to a port value in the transport-layer header
5233 * at the specified offset from the beginning of that header.
5235 * XXX - this handles a variable-length prefix preceding the link-layer
5236 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5237 * variable-length link-layer headers (such as Token Ring or 802.11
5240 static struct block
*
5241 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5243 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5246 static struct block
*
5247 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5249 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5253 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5255 struct block
*b0
, *b1
, *tmp
;
5257 /* ip proto 'proto' and not a fragment other than the first fragment */
5258 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5259 b0
= gen_ipfrag(cstate
);
5264 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5268 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5273 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5274 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5279 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5280 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5292 static struct block
*
5293 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5295 struct block
*b0
, *b1
, *tmp
;
5300 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5301 * not LLC encapsulation with LLCSAP_IP.
5303 * For IEEE 802 networks - which includes 802.5 token ring
5304 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5305 * says that SNAP encapsulation is used, not LLC encapsulation
5308 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5309 * RFC 2225 say that SNAP encapsulation is used, not LLC
5310 * encapsulation with LLCSAP_IP.
5312 * So we always check for ETHERTYPE_IP.
5314 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5320 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5324 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5325 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5327 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5339 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5341 struct block
*b0
, *b1
, *tmp
;
5343 /* ip6 proto 'proto' */
5344 /* XXX - catch the first fragment of a fragmented packet? */
5345 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5349 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5353 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5358 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5359 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5364 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5365 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5377 static struct block
*
5378 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5380 struct block
*b0
, *b1
, *tmp
;
5382 /* link proto ip6 */
5383 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5389 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5393 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5394 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5396 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5407 /* gen_portrange code */
5408 static struct block
*
5409 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5412 struct block
*b1
, *b2
;
5416 * Reverse the order of the ports, so v1 is the lower one.
5425 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5426 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5434 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5437 struct block
*b0
, *b1
, *tmp
;
5439 /* ip proto 'proto' and not a fragment other than the first fragment */
5440 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5441 b0
= gen_ipfrag(cstate
);
5446 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5450 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5455 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5456 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5461 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5462 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5474 static struct block
*
5475 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5478 struct block
*b0
, *b1
, *tmp
;
5481 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5487 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5491 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5492 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5494 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5505 static struct block
*
5506 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5509 struct block
*b1
, *b2
;
5513 * Reverse the order of the ports, so v1 is the lower one.
5522 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5523 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5531 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5534 struct block
*b0
, *b1
, *tmp
;
5536 /* ip6 proto 'proto' */
5537 /* XXX - catch the first fragment of a fragmented packet? */
5538 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5542 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5546 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5551 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5552 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5557 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5558 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5570 static struct block
*
5571 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5574 struct block
*b0
, *b1
, *tmp
;
5576 /* link proto ip6 */
5577 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5583 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5587 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5588 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5590 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5602 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5611 v
= pcap_nametoproto(name
);
5612 if (v
== PROTO_UNDEF
)
5613 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5617 /* XXX should look up h/w protocol type based on cstate->linktype */
5618 v
= pcap_nametoeproto(name
);
5619 if (v
== PROTO_UNDEF
) {
5620 v
= pcap_nametollc(name
);
5621 if (v
== PROTO_UNDEF
)
5622 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5627 if (strcmp(name
, "esis") == 0)
5629 else if (strcmp(name
, "isis") == 0)
5631 else if (strcmp(name
, "clnp") == 0)
5634 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5654 static struct block
*
5655 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5657 #ifdef NO_PROTOCHAIN
5658 return gen_proto(cstate
, v
, proto
, dir
);
5660 struct block
*b0
, *b
;
5661 struct slist
*s
[100];
5662 int fix2
, fix3
, fix4
, fix5
;
5663 int ahcheck
, again
, end
;
5665 int reg2
= alloc_reg(cstate
);
5667 memset(s
, 0, sizeof(s
));
5668 fix3
= fix4
= fix5
= 0;
5675 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5676 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5680 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5685 * We don't handle variable-length prefixes before the link-layer
5686 * header, or variable-length link-layer headers, here yet.
5687 * We might want to add BPF instructions to do the protochain
5688 * work, to simplify that and, on platforms that have a BPF
5689 * interpreter with the new instructions, let the filtering
5690 * be done in the kernel. (We already require a modified BPF
5691 * engine to do the protochain stuff, to support backward
5692 * branches, and backward branch support is unlikely to appear
5693 * in kernel BPF engines.)
5695 if (cstate
->off_linkpl
.is_variable
)
5696 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5698 cstate
->no_optimize
= 1; /*this code is not compatible with optimzer yet */
5701 * s[0] is a dummy entry to protect other BPF insn from damage
5702 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5703 * hard to find interdependency made by jump table fixup.
5706 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5711 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5714 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5715 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5717 /* X = ip->ip_hl << 2 */
5718 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5719 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5724 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5726 /* A = ip6->ip_nxt */
5727 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5728 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
5730 /* X = sizeof(struct ip6_hdr) */
5731 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
5737 bpf_error(cstate
, "unsupported proto to gen_protochain");
5741 /* again: if (A == v) goto end; else fall through; */
5743 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5745 s
[i
]->s
.jt
= NULL
; /*later*/
5746 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5750 #ifndef IPPROTO_NONE
5751 #define IPPROTO_NONE 59
5753 /* if (A == IPPROTO_NONE) goto end */
5754 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5755 s
[i
]->s
.jt
= NULL
; /*later*/
5756 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5757 s
[i
]->s
.k
= IPPROTO_NONE
;
5758 s
[fix5
]->s
.jf
= s
[i
];
5762 if (proto
== Q_IPV6
) {
5763 int v6start
, v6end
, v6advance
, j
;
5766 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5767 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5768 s
[i
]->s
.jt
= NULL
; /*later*/
5769 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5770 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5771 s
[fix2
]->s
.jf
= s
[i
];
5773 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5774 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5775 s
[i
]->s
.jt
= NULL
; /*later*/
5776 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5777 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5779 /* if (A == IPPROTO_ROUTING) goto v6advance */
5780 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5781 s
[i
]->s
.jt
= NULL
; /*later*/
5782 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5783 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5785 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5786 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5787 s
[i
]->s
.jt
= NULL
; /*later*/
5788 s
[i
]->s
.jf
= NULL
; /*later*/
5789 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5799 * A = P[X + packet head];
5800 * X = X + (P[X + packet head + 1] + 1) * 8;
5802 /* A = P[X + packet head] */
5803 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5804 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5807 s
[i
] = new_stmt(cstate
, BPF_ST
);
5810 /* A = P[X + packet head + 1]; */
5811 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5812 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
5815 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5819 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5823 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
5827 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5830 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5834 /* goto again; (must use BPF_JA for backward jump) */
5835 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5836 s
[i
]->s
.k
= again
- i
- 1;
5837 s
[i
- 1]->s
.jf
= s
[i
];
5841 for (j
= v6start
; j
<= v6end
; j
++)
5842 s
[j
]->s
.jt
= s
[v6advance
];
5845 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5847 s
[fix2
]->s
.jf
= s
[i
];
5853 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5854 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5855 s
[i
]->s
.jt
= NULL
; /*later*/
5856 s
[i
]->s
.jf
= NULL
; /*later*/
5857 s
[i
]->s
.k
= IPPROTO_AH
;
5859 s
[fix3
]->s
.jf
= s
[ahcheck
];
5866 * X = X + (P[X + 1] + 2) * 4;
5869 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5871 /* A = P[X + packet head]; */
5872 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5873 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5876 s
[i
] = new_stmt(cstate
, BPF_ST
);
5880 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5883 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5887 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5889 /* A = P[X + packet head] */
5890 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5891 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5894 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5898 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5902 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5905 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5909 /* goto again; (must use BPF_JA for backward jump) */
5910 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5911 s
[i
]->s
.k
= again
- i
- 1;
5916 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5918 s
[fix2
]->s
.jt
= s
[end
];
5919 s
[fix4
]->s
.jf
= s
[end
];
5920 s
[fix5
]->s
.jt
= s
[end
];
5927 for (i
= 0; i
< max
- 1; i
++)
5928 s
[i
]->next
= s
[i
+ 1];
5929 s
[max
- 1]->next
= NULL
;
5934 b
= new_block(cstate
, JMP(BPF_JEQ
));
5935 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5938 free_reg(cstate
, reg2
);
5945 static struct block
*
5946 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
5949 struct block
*b0
, *b1
;
5952 * A data frame has the 0x08 bit (b3) in the frame control field set
5953 * and the 0x04 bit (b2) clear.
5955 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
5956 b0
= new_block(cstate
, JMP(BPF_JSET
));
5960 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
5961 b1
= new_block(cstate
, JMP(BPF_JSET
));
5972 * Generate code that checks whether the packet is a packet for protocol
5973 * <proto> and whether the type field in that protocol's header has
5974 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5975 * IP packet and checks the protocol number in the IP header against <v>.
5977 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5978 * against Q_IP and Q_IPV6.
5980 static struct block
*
5981 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5983 struct block
*b0
, *b1
;
5988 if (dir
!= Q_DEFAULT
)
5989 bpf_error(cstate
, "direction applied to 'proto'");
5993 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
5994 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6000 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6001 * not LLC encapsulation with LLCSAP_IP.
6003 * For IEEE 802 networks - which includes 802.5 token ring
6004 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6005 * says that SNAP encapsulation is used, not LLC encapsulation
6008 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6009 * RFC 2225 say that SNAP encapsulation is used, not LLC
6010 * encapsulation with LLCSAP_IP.
6012 * So we always check for ETHERTYPE_IP.
6014 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6016 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6018 b1
= gen_protochain(cstate
, v
, Q_IP
);
6024 switch (cstate
->linktype
) {
6028 * Frame Relay packets typically have an OSI
6029 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6030 * generates code to check for all the OSI
6031 * NLPIDs, so calling it and then adding a check
6032 * for the particular NLPID for which we're
6033 * looking is bogus, as we can just check for
6036 * What we check for is the NLPID and a frame
6037 * control field value of UI, i.e. 0x03 followed
6040 * XXX - assumes a 2-byte Frame Relay header with
6041 * DLCI and flags. What if the address is longer?
6043 * XXX - what about SNAP-encapsulated frames?
6045 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6051 * Cisco uses an Ethertype lookalike - for OSI,
6054 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6055 /* OSI in C-HDLC is stuffed with a fudge byte */
6056 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6061 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6062 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6068 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6070 * 4 is the offset of the PDU type relative to the IS-IS
6073 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6078 bpf_error(cstate
, "arp does not encapsulate another protocol");
6082 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6086 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6090 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6094 bpf_error(cstate
, "sca does not encapsulate another protocol");
6098 bpf_error(cstate
, "lat does not encapsulate another protocol");
6102 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6106 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6110 return gen_linktype(cstate
, v
);
6113 bpf_error(cstate
, "'udp proto' is bogus");
6117 bpf_error(cstate
, "'tcp proto' is bogus");
6121 bpf_error(cstate
, "'sctp proto' is bogus");
6125 bpf_error(cstate
, "'icmp proto' is bogus");
6129 bpf_error(cstate
, "'igmp proto' is bogus");
6133 bpf_error(cstate
, "'igrp proto' is bogus");
6137 bpf_error(cstate
, "'pim proto' is bogus");
6141 bpf_error(cstate
, "'vrrp proto' is bogus");
6145 bpf_error(cstate
, "'carp proto' is bogus");
6149 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6152 * Also check for a fragment header before the final
6155 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6156 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6158 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6161 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6167 bpf_error(cstate
, "'icmp6 proto' is bogus");
6170 bpf_error(cstate
, "'ah proto' is bogus");
6173 bpf_error(cstate
, "'ah proto' is bogus");
6176 bpf_error(cstate
, "'stp proto' is bogus");
6179 bpf_error(cstate
, "'ipx proto' is bogus");
6182 bpf_error(cstate
, "'netbeui proto' is bogus");
6185 bpf_error(cstate
, "'radio proto' is bogus");
6195 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6197 int proto
= q
.proto
;
6201 bpf_u_int32 mask
, addr
;
6203 bpf_u_int32
**alist
;
6206 struct sockaddr_in
*sin4
;
6207 struct sockaddr_in6
*sin6
;
6208 struct addrinfo
*res
, *res0
;
6209 struct in6_addr mask128
;
6211 struct block
*b
, *tmp
;
6212 int port
, real_proto
;
6218 addr
= pcap_nametonetaddr(name
);
6220 bpf_error(cstate
, "unknown network '%s'", name
);
6221 /* Left justify network addr and calculate its network mask */
6223 while (addr
&& (addr
& 0xff000000) == 0) {
6227 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6231 if (proto
== Q_LINK
) {
6232 switch (cstate
->linktype
) {
6235 case DLT_NETANALYZER
:
6236 case DLT_NETANALYZER_TRANSPARENT
:
6237 eaddr
= pcap_ether_hostton(name
);
6240 "unknown ether host '%s'", name
);
6241 tmp
= gen_prevlinkhdr_check(cstate
);
6242 b
= gen_ehostop(cstate
, eaddr
, dir
);
6249 eaddr
= pcap_ether_hostton(name
);
6252 "unknown FDDI host '%s'", name
);
6253 b
= gen_fhostop(cstate
, eaddr
, dir
);
6258 eaddr
= pcap_ether_hostton(name
);
6261 "unknown token ring host '%s'", name
);
6262 b
= gen_thostop(cstate
, eaddr
, dir
);
6266 case DLT_IEEE802_11
:
6267 case DLT_PRISM_HEADER
:
6268 case DLT_IEEE802_11_RADIO_AVS
:
6269 case DLT_IEEE802_11_RADIO
:
6271 eaddr
= pcap_ether_hostton(name
);
6274 "unknown 802.11 host '%s'", name
);
6275 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6279 case DLT_IP_OVER_FC
:
6280 eaddr
= pcap_ether_hostton(name
);
6283 "unknown Fibre Channel host '%s'", name
);
6284 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6289 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6290 } else if (proto
== Q_DECNET
) {
6291 unsigned short dn_addr
;
6293 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6295 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6297 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6302 * I don't think DECNET hosts can be multihomed, so
6303 * there is no need to build up a list of addresses
6305 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6308 alist
= pcap_nametoaddr(name
);
6309 if (alist
== NULL
|| *alist
== NULL
)
6310 bpf_error(cstate
, "unknown host '%s'", name
);
6312 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6313 tproto
== Q_DEFAULT
)
6315 b
= gen_host(cstate
, **alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6317 tmp
= gen_host(cstate
, **alist
++, 0xffffffff,
6318 tproto
, dir
, q
.addr
);
6324 memset(&mask128
, 0xff, sizeof(mask128
));
6325 res0
= res
= pcap_nametoaddrinfo(name
);
6327 bpf_error(cstate
, "unknown host '%s'", name
);
6330 tproto
= tproto6
= proto
;
6331 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6332 tproto
== Q_DEFAULT
) {
6336 for (res
= res0
; res
; res
= res
->ai_next
) {
6337 switch (res
->ai_family
) {
6339 if (tproto
== Q_IPV6
)
6342 sin4
= (struct sockaddr_in
*)
6344 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6345 0xffffffff, tproto
, dir
, q
.addr
);
6348 if (tproto6
== Q_IP
)
6351 sin6
= (struct sockaddr_in6
*)
6353 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6354 &mask128
, tproto6
, dir
, q
.addr
);
6366 bpf_error(cstate
, "unknown host '%s'%s", name
,
6367 (proto
== Q_DEFAULT
)
6369 : " for specified address family");
6376 if (proto
!= Q_DEFAULT
&&
6377 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6378 bpf_error(cstate
, "illegal qualifier of 'port'");
6379 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6380 bpf_error(cstate
, "unknown port '%s'", name
);
6381 if (proto
== Q_UDP
) {
6382 if (real_proto
== IPPROTO_TCP
)
6383 bpf_error(cstate
, "port '%s' is tcp", name
);
6384 else if (real_proto
== IPPROTO_SCTP
)
6385 bpf_error(cstate
, "port '%s' is sctp", name
);
6387 /* override PROTO_UNDEF */
6388 real_proto
= IPPROTO_UDP
;
6390 if (proto
== Q_TCP
) {
6391 if (real_proto
== IPPROTO_UDP
)
6392 bpf_error(cstate
, "port '%s' is udp", name
);
6394 else if (real_proto
== IPPROTO_SCTP
)
6395 bpf_error(cstate
, "port '%s' is sctp", name
);
6397 /* override PROTO_UNDEF */
6398 real_proto
= IPPROTO_TCP
;
6400 if (proto
== Q_SCTP
) {
6401 if (real_proto
== IPPROTO_UDP
)
6402 bpf_error(cstate
, "port '%s' is udp", name
);
6404 else if (real_proto
== IPPROTO_TCP
)
6405 bpf_error(cstate
, "port '%s' is tcp", name
);
6407 /* override PROTO_UNDEF */
6408 real_proto
= IPPROTO_SCTP
;
6411 bpf_error(cstate
, "illegal port number %d < 0", port
);
6413 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6414 b
= gen_port(cstate
, port
, real_proto
, dir
);
6415 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6419 if (proto
!= Q_DEFAULT
&&
6420 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6421 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6422 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6423 bpf_error(cstate
, "unknown port in range '%s'", name
);
6424 if (proto
== Q_UDP
) {
6425 if (real_proto
== IPPROTO_TCP
)
6426 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6427 else if (real_proto
== IPPROTO_SCTP
)
6428 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6430 /* override PROTO_UNDEF */
6431 real_proto
= IPPROTO_UDP
;
6433 if (proto
== Q_TCP
) {
6434 if (real_proto
== IPPROTO_UDP
)
6435 bpf_error(cstate
, "port in range '%s' is udp", name
);
6436 else if (real_proto
== IPPROTO_SCTP
)
6437 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6439 /* override PROTO_UNDEF */
6440 real_proto
= IPPROTO_TCP
;
6442 if (proto
== Q_SCTP
) {
6443 if (real_proto
== IPPROTO_UDP
)
6444 bpf_error(cstate
, "port in range '%s' is udp", name
);
6445 else if (real_proto
== IPPROTO_TCP
)
6446 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6448 /* override PROTO_UNDEF */
6449 real_proto
= IPPROTO_SCTP
;
6452 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6454 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6456 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6458 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6460 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6461 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6466 eaddr
= pcap_ether_hostton(name
);
6468 bpf_error(cstate
, "unknown ether host: %s", name
);
6470 alist
= pcap_nametoaddr(name
);
6471 if (alist
== NULL
|| *alist
== NULL
)
6472 bpf_error(cstate
, "unknown host '%s'", name
);
6473 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6477 bpf_error(cstate
, "'gateway' not supported in this configuration");
6481 real_proto
= lookup_proto(cstate
, name
, proto
);
6482 if (real_proto
>= 0)
6483 return gen_proto(cstate
, real_proto
, proto
, dir
);
6485 bpf_error(cstate
, "unknown protocol: %s", name
);
6488 real_proto
= lookup_proto(cstate
, name
, proto
);
6489 if (real_proto
>= 0)
6490 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6492 bpf_error(cstate
, "unknown protocol: %s", name
);
6503 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6504 unsigned int masklen
, struct qual q
)
6506 register int nlen
, mlen
;
6509 nlen
= __pcap_atoin(s1
, &n
);
6510 /* Promote short ipaddr */
6514 mlen
= __pcap_atoin(s2
, &m
);
6515 /* Promote short ipaddr */
6518 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6521 /* Convert mask len to mask */
6523 bpf_error(cstate
, "mask length must be <= 32");
6526 * X << 32 is not guaranteed by C to be 0; it's
6531 m
= 0xffffffff << (32 - masklen
);
6533 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6540 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6543 bpf_error(cstate
, "Mask syntax for networks only");
6551 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6554 int proto
= q
.proto
;
6560 else if (q
.proto
== Q_DECNET
) {
6561 vlen
= __pcap_atodn(s
, &v
);
6563 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6565 vlen
= __pcap_atoin(s
, &v
);
6572 if (proto
== Q_DECNET
)
6573 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6574 else if (proto
== Q_LINK
) {
6575 bpf_error(cstate
, "illegal link layer address");
6578 if (s
== NULL
&& q
.addr
== Q_NET
) {
6579 /* Promote short net number */
6580 while (v
&& (v
& 0xff000000) == 0) {
6585 /* Promote short ipaddr */
6587 mask
<<= 32 - vlen
;
6589 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6594 proto
= IPPROTO_UDP
;
6595 else if (proto
== Q_TCP
)
6596 proto
= IPPROTO_TCP
;
6597 else if (proto
== Q_SCTP
)
6598 proto
= IPPROTO_SCTP
;
6599 else if (proto
== Q_DEFAULT
)
6600 proto
= PROTO_UNDEF
;
6602 bpf_error(cstate
, "illegal qualifier of 'port'");
6605 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6609 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6610 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6616 proto
= IPPROTO_UDP
;
6617 else if (proto
== Q_TCP
)
6618 proto
= IPPROTO_TCP
;
6619 else if (proto
== Q_SCTP
)
6620 proto
= IPPROTO_SCTP
;
6621 else if (proto
== Q_DEFAULT
)
6622 proto
= PROTO_UNDEF
;
6624 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6627 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6631 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6632 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6637 bpf_error(cstate
, "'gateway' requires a name");
6641 return gen_proto(cstate
, (int)v
, proto
, dir
);
6644 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6659 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6660 unsigned int masklen
, struct qual q
)
6662 struct addrinfo
*res
;
6663 struct in6_addr
*addr
;
6664 struct in6_addr mask
;
6669 bpf_error(cstate
, "no mask %s supported", s2
);
6671 res
= pcap_nametoaddrinfo(s1
);
6673 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6676 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6677 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6679 if (sizeof(mask
) * 8 < masklen
)
6680 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6681 memset(&mask
, 0, sizeof(mask
));
6682 memset(&mask
, 0xff, masklen
/ 8);
6684 mask
.s6_addr
[masklen
/ 8] =
6685 (0xff << (8 - masklen
% 8)) & 0xff;
6688 a
= (u_int32_t
*)addr
;
6689 m
= (u_int32_t
*)&mask
;
6690 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6691 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6692 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6700 bpf_error(cstate
, "Mask syntax for networks only");
6704 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6710 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6718 gen_ecode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
6720 struct block
*b
, *tmp
;
6722 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6723 switch (cstate
->linktype
) {
6725 case DLT_NETANALYZER
:
6726 case DLT_NETANALYZER_TRANSPARENT
:
6727 tmp
= gen_prevlinkhdr_check(cstate
);
6728 b
= gen_ehostop(cstate
, eaddr
, (int)q
.dir
);
6733 return gen_fhostop(cstate
, eaddr
, (int)q
.dir
);
6735 return gen_thostop(cstate
, eaddr
, (int)q
.dir
);
6736 case DLT_IEEE802_11
:
6737 case DLT_PRISM_HEADER
:
6738 case DLT_IEEE802_11_RADIO_AVS
:
6739 case DLT_IEEE802_11_RADIO
:
6741 return gen_wlanhostop(cstate
, eaddr
, (int)q
.dir
);
6742 case DLT_IP_OVER_FC
:
6743 return gen_ipfchostop(cstate
, eaddr
, (int)q
.dir
);
6745 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6749 bpf_error(cstate
, "ethernet address used in non-ether expression");
6756 struct slist
*s0
, *s1
;
6759 * This is definitely not the best way to do this, but the
6760 * lists will rarely get long.
6767 static struct slist
*
6768 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
6772 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
6777 static struct slist
*
6778 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
6782 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6788 * Modify "index" to use the value stored into its register as an
6789 * offset relative to the beginning of the header for the protocol
6790 * "proto", and allocate a register and put an item "size" bytes long
6791 * (1, 2, or 4) at that offset into that register, making it the register
6795 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
6797 struct slist
*s
, *tmp
;
6799 int regno
= alloc_reg(cstate
);
6801 free_reg(cstate
, inst
->regno
);
6805 bpf_error(cstate
, "data size must be 1, 2, or 4");
6821 bpf_error(cstate
, "unsupported index operation");
6825 * The offset is relative to the beginning of the packet
6826 * data, if we have a radio header. (If we don't, this
6829 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6830 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
6831 cstate
->linktype
!= DLT_PRISM_HEADER
)
6832 bpf_error(cstate
, "radio information not present in capture");
6835 * Load into the X register the offset computed into the
6836 * register specified by "index".
6838 s
= xfer_to_x(cstate
, inst
);
6841 * Load the item at that offset.
6843 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6845 sappend(inst
->s
, s
);
6850 * The offset is relative to the beginning of
6851 * the link-layer header.
6853 * XXX - what about ATM LANE? Should the index be
6854 * relative to the beginning of the AAL5 frame, so
6855 * that 0 refers to the beginning of the LE Control
6856 * field, or relative to the beginning of the LAN
6857 * frame, so that 0 refers, for Ethernet LANE, to
6858 * the beginning of the destination address?
6860 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
6863 * If "s" is non-null, it has code to arrange that the
6864 * X register contains the length of the prefix preceding
6865 * the link-layer header. Add to it the offset computed
6866 * into the register specified by "index", and move that
6867 * into the X register. Otherwise, just load into the X
6868 * register the offset computed into the register specified
6872 sappend(s
, xfer_to_a(cstate
, inst
));
6873 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6874 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6876 s
= xfer_to_x(cstate
, inst
);
6879 * Load the item at the sum of the offset we've put in the
6880 * X register and the offset of the start of the link
6881 * layer header (which is 0 if the radio header is
6882 * variable-length; that header length is what we put
6883 * into the X register and then added to the index).
6885 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6886 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
6888 sappend(inst
->s
, s
);
6902 * The offset is relative to the beginning of
6903 * the network-layer header.
6904 * XXX - are there any cases where we want
6905 * cstate->off_nl_nosnap?
6907 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
6910 * If "s" is non-null, it has code to arrange that the
6911 * X register contains the variable part of the offset
6912 * of the link-layer payload. Add to it the offset
6913 * computed into the register specified by "index",
6914 * and move that into the X register. Otherwise, just
6915 * load into the X register the offset computed into
6916 * the register specified by "index".
6919 sappend(s
, xfer_to_a(cstate
, inst
));
6920 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6921 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6923 s
= xfer_to_x(cstate
, inst
);
6926 * Load the item at the sum of the offset we've put in the
6927 * X register, the offset of the start of the network
6928 * layer header from the beginning of the link-layer
6929 * payload, and the constant part of the offset of the
6930 * start of the link-layer payload.
6932 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6933 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6935 sappend(inst
->s
, s
);
6938 * Do the computation only if the packet contains
6939 * the protocol in question.
6941 b
= gen_proto_abbrev(cstate
, proto
);
6943 gen_and(inst
->b
, b
);
6957 * The offset is relative to the beginning of
6958 * the transport-layer header.
6960 * Load the X register with the length of the IPv4 header
6961 * (plus the offset of the link-layer header, if it's
6962 * a variable-length header), in bytes.
6964 * XXX - are there any cases where we want
6965 * cstate->off_nl_nosnap?
6966 * XXX - we should, if we're built with
6967 * IPv6 support, generate code to load either
6968 * IPv4, IPv6, or both, as appropriate.
6970 s
= gen_loadx_iphdrlen(cstate
);
6973 * The X register now contains the sum of the variable
6974 * part of the offset of the link-layer payload and the
6975 * length of the network-layer header.
6977 * Load into the A register the offset relative to
6978 * the beginning of the transport layer header,
6979 * add the X register to that, move that to the
6980 * X register, and load with an offset from the
6981 * X register equal to the sum of the constant part of
6982 * the offset of the link-layer payload and the offset,
6983 * relative to the beginning of the link-layer payload,
6984 * of the network-layer header.
6986 sappend(s
, xfer_to_a(cstate
, inst
));
6987 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6988 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6989 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
6990 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6991 sappend(inst
->s
, s
);
6994 * Do the computation only if the packet contains
6995 * the protocol in question - which is true only
6996 * if this is an IP datagram and is the first or
6997 * only fragment of that datagram.
6999 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7001 gen_and(inst
->b
, b
);
7002 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7006 bpf_error(cstate
, "IPv6 upper-layer protocol is not supported by proto[x]");
7009 inst
->regno
= regno
;
7010 s
= new_stmt(cstate
, BPF_ST
);
7012 sappend(inst
->s
, s
);
7018 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7019 struct arth
*a1
, int reversed
)
7021 struct slist
*s0
, *s1
, *s2
;
7022 struct block
*b
, *tmp
;
7024 s0
= xfer_to_x(cstate
, a1
);
7025 s1
= xfer_to_a(cstate
, a0
);
7026 if (code
== BPF_JEQ
) {
7027 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7028 b
= new_block(cstate
, JMP(code
));
7032 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7038 sappend(a0
->s
, a1
->s
);
7042 free_reg(cstate
, a0
->regno
);
7043 free_reg(cstate
, a1
->regno
);
7045 /* 'and' together protocol checks */
7048 gen_and(a0
->b
, tmp
= a1
->b
);
7062 gen_loadlen(compiler_state_t
*cstate
)
7064 int regno
= alloc_reg(cstate
);
7065 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7068 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7069 s
->next
= new_stmt(cstate
, BPF_ST
);
7070 s
->next
->s
.k
= regno
;
7078 gen_loadi(compiler_state_t
*cstate
, int val
)
7084 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7086 reg
= alloc_reg(cstate
);
7088 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7090 s
->next
= new_stmt(cstate
, BPF_ST
);
7099 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7103 s
= xfer_to_a(cstate
, a
);
7105 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7108 s
= new_stmt(cstate
, BPF_ST
);
7116 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7119 struct slist
*s0
, *s1
, *s2
;
7122 * Disallow division by, or modulus by, zero; we do this here
7123 * so that it gets done even if the optimizer is disabled.
7125 if (code
== BPF_DIV
) {
7126 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7127 bpf_error(cstate
, "division by zero");
7128 } else if (code
== BPF_MOD
) {
7129 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7130 bpf_error(cstate
, "modulus by zero");
7132 s0
= xfer_to_x(cstate
, a1
);
7133 s1
= xfer_to_a(cstate
, a0
);
7134 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7139 sappend(a0
->s
, a1
->s
);
7141 free_reg(cstate
, a0
->regno
);
7142 free_reg(cstate
, a1
->regno
);
7144 s0
= new_stmt(cstate
, BPF_ST
);
7145 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7152 * Initialize the table of used registers and the current register.
7155 init_regs(compiler_state_t
*cstate
)
7158 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7162 * Return the next free register.
7165 alloc_reg(compiler_state_t
*cstate
)
7167 int n
= BPF_MEMWORDS
;
7170 if (cstate
->regused
[cstate
->curreg
])
7171 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7173 cstate
->regused
[cstate
->curreg
] = 1;
7174 return cstate
->curreg
;
7177 bpf_error(cstate
, "too many registers needed to evaluate expression");
7183 * Return a register to the table so it can
7187 free_reg(compiler_state_t
*cstate
, int n
)
7189 cstate
->regused
[n
] = 0;
7192 static struct block
*
7193 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7198 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7199 b
= new_block(cstate
, JMP(jmp
));
7207 gen_greater(compiler_state_t
*cstate
, int n
)
7209 return gen_len(cstate
, BPF_JGE
, n
);
7213 * Actually, this is less than or equal.
7216 gen_less(compiler_state_t
*cstate
, int n
)
7220 b
= gen_len(cstate
, BPF_JGT
, n
);
7227 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7228 * the beginning of the link-layer header.
7229 * XXX - that means you can't test values in the radiotap header, but
7230 * as that header is difficult if not impossible to parse generally
7231 * without a loop, that might not be a severe problem. A new keyword
7232 * "radio" could be added for that, although what you'd really want
7233 * would be a way of testing particular radio header values, which
7234 * would generate code appropriate to the radio header in question.
7237 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7247 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7250 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7254 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7258 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7262 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7266 b
= new_block(cstate
, JMP(BPF_JEQ
));
7273 static const u_char abroadcast
[] = { 0x0 };
7276 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7278 bpf_u_int32 hostmask
;
7279 struct block
*b0
, *b1
, *b2
;
7280 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7286 switch (cstate
->linktype
) {
7288 case DLT_ARCNET_LINUX
:
7289 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7291 case DLT_NETANALYZER
:
7292 case DLT_NETANALYZER_TRANSPARENT
:
7293 b1
= gen_prevlinkhdr_check(cstate
);
7294 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7299 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7301 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7302 case DLT_IEEE802_11
:
7303 case DLT_PRISM_HEADER
:
7304 case DLT_IEEE802_11_RADIO_AVS
:
7305 case DLT_IEEE802_11_RADIO
:
7307 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7308 case DLT_IP_OVER_FC
:
7309 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7311 bpf_error(cstate
, "not a broadcast link");
7317 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7318 * as an indication that we don't know the netmask, and fail
7321 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7322 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7323 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7324 hostmask
= ~cstate
->netmask
;
7325 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7326 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7327 (bpf_int32
)(~0 & hostmask
), hostmask
);
7332 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7338 * Generate code to test the low-order bit of a MAC address (that's
7339 * the bottom bit of the *first* byte).
7341 static struct block
*
7342 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7344 register struct block
*b0
;
7345 register struct slist
*s
;
7347 /* link[offset] & 1 != 0 */
7348 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7349 b0
= new_block(cstate
, JMP(BPF_JSET
));
7356 gen_multicast(compiler_state_t
*cstate
, int proto
)
7358 register struct block
*b0
, *b1
, *b2
;
7359 register struct slist
*s
;
7365 switch (cstate
->linktype
) {
7367 case DLT_ARCNET_LINUX
:
7368 /* all ARCnet multicasts use the same address */
7369 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7371 case DLT_NETANALYZER
:
7372 case DLT_NETANALYZER_TRANSPARENT
:
7373 b1
= gen_prevlinkhdr_check(cstate
);
7374 /* ether[0] & 1 != 0 */
7375 b0
= gen_mac_multicast(cstate
, 0);
7381 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7383 * XXX - was that referring to bit-order issues?
7385 /* fddi[1] & 1 != 0 */
7386 return gen_mac_multicast(cstate
, 1);
7388 /* tr[2] & 1 != 0 */
7389 return gen_mac_multicast(cstate
, 2);
7390 case DLT_IEEE802_11
:
7391 case DLT_PRISM_HEADER
:
7392 case DLT_IEEE802_11_RADIO_AVS
:
7393 case DLT_IEEE802_11_RADIO
:
7398 * For control frames, there is no DA.
7400 * For management frames, DA is at an
7401 * offset of 4 from the beginning of
7404 * For data frames, DA is at an offset
7405 * of 4 from the beginning of the packet
7406 * if To DS is clear and at an offset of
7407 * 16 from the beginning of the packet
7412 * Generate the tests to be done for data frames.
7414 * First, check for To DS set, i.e. "link[1] & 0x01".
7416 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7417 b1
= new_block(cstate
, JMP(BPF_JSET
));
7418 b1
->s
.k
= 0x01; /* To DS */
7422 * If To DS is set, the DA is at 16.
7424 b0
= gen_mac_multicast(cstate
, 16);
7428 * Now, check for To DS not set, i.e. check
7429 * "!(link[1] & 0x01)".
7431 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7432 b2
= new_block(cstate
, JMP(BPF_JSET
));
7433 b2
->s
.k
= 0x01; /* To DS */
7438 * If To DS is not set, the DA is at 4.
7440 b1
= gen_mac_multicast(cstate
, 4);
7444 * Now OR together the last two checks. That gives
7445 * the complete set of checks for data frames.
7450 * Now check for a data frame.
7451 * I.e, check "link[0] & 0x08".
7453 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7454 b1
= new_block(cstate
, JMP(BPF_JSET
));
7459 * AND that with the checks done for data frames.
7464 * If the high-order bit of the type value is 0, this
7465 * is a management frame.
7466 * I.e, check "!(link[0] & 0x08)".
7468 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7469 b2
= new_block(cstate
, JMP(BPF_JSET
));
7475 * For management frames, the DA is at 4.
7477 b1
= gen_mac_multicast(cstate
, 4);
7481 * OR that with the checks done for data frames.
7482 * That gives the checks done for management and
7488 * If the low-order bit of the type value is 1,
7489 * this is either a control frame or a frame
7490 * with a reserved type, and thus not a
7493 * I.e., check "!(link[0] & 0x04)".
7495 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7496 b1
= new_block(cstate
, JMP(BPF_JSET
));
7502 * AND that with the checks for data and management
7507 case DLT_IP_OVER_FC
:
7508 b0
= gen_mac_multicast(cstate
, 2);
7513 /* Link not known to support multicasts */
7517 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7518 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7523 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7524 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7528 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7534 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7535 * Outbound traffic is sent by this machine, while inbound traffic is
7536 * sent by a remote machine (and may include packets destined for a
7537 * unicast or multicast link-layer address we are not subscribing to).
7538 * These are the same definitions implemented by pcap_setdirection().
7539 * Capturing only unicast traffic destined for this host is probably
7540 * better accomplished using a higher-layer filter.
7543 gen_inbound(compiler_state_t
*cstate
, int dir
)
7545 register struct block
*b0
;
7548 * Only some data link types support inbound/outbound qualifiers.
7550 switch (cstate
->linktype
) {
7552 b0
= gen_relation(cstate
, BPF_JEQ
,
7553 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7554 gen_loadi(cstate
, 0),
7560 /* match outgoing packets */
7561 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7563 /* match incoming packets */
7564 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7569 /* match outgoing packets */
7570 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7572 /* to filter on inbound traffic, invert the match */
7577 #ifdef HAVE_NET_PFVAR_H
7579 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7580 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7586 /* match outgoing packets */
7587 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7589 /* match incoming packets */
7590 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7594 case DLT_JUNIPER_MFR
:
7595 case DLT_JUNIPER_MLFR
:
7596 case DLT_JUNIPER_MLPPP
:
7597 case DLT_JUNIPER_ATM1
:
7598 case DLT_JUNIPER_ATM2
:
7599 case DLT_JUNIPER_PPPOE
:
7600 case DLT_JUNIPER_PPPOE_ATM
:
7601 case DLT_JUNIPER_GGSN
:
7602 case DLT_JUNIPER_ES
:
7603 case DLT_JUNIPER_MONITOR
:
7604 case DLT_JUNIPER_SERVICES
:
7605 case DLT_JUNIPER_ETHER
:
7606 case DLT_JUNIPER_PPP
:
7607 case DLT_JUNIPER_FRELAY
:
7608 case DLT_JUNIPER_CHDLC
:
7609 case DLT_JUNIPER_VP
:
7610 case DLT_JUNIPER_ST
:
7611 case DLT_JUNIPER_ISM
:
7612 case DLT_JUNIPER_VS
:
7613 case DLT_JUNIPER_SRX_E2E
:
7614 case DLT_JUNIPER_FIBRECHANNEL
:
7615 case DLT_JUNIPER_ATM_CEMIC
:
7617 /* juniper flags (including direction) are stored
7618 * the byte after the 3-byte magic number */
7620 /* match outgoing packets */
7621 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7623 /* match incoming packets */
7624 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7630 * If we have packet meta-data indicating a direction,
7631 * check it, otherwise give up as this link-layer type
7632 * has nothing in the packet data.
7634 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7636 * This is Linux with PF_PACKET support.
7637 * If this is a *live* capture, we can look at
7638 * special meta-data in the filter expression;
7639 * if it's a savefile, we can't.
7641 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7642 /* We have a FILE *, so this is a savefile */
7643 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
7648 /* match outgoing packets */
7649 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7652 /* to filter on inbound traffic, invert the match */
7655 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7656 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
7660 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7665 #ifdef HAVE_NET_PFVAR_H
7666 /* PF firewall log matched interface */
7668 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7673 if (cstate
->linktype
!= DLT_PFLOG
) {
7674 bpf_error(cstate
, "ifname supported only on PF linktype");
7677 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7678 off
= offsetof(struct pfloghdr
, ifname
);
7679 if (strlen(ifname
) >= len
) {
7680 bpf_error(cstate
, "ifname interface names can only be %d characters",
7684 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7688 /* PF firewall log ruleset name */
7690 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7694 if (cstate
->linktype
!= DLT_PFLOG
) {
7695 bpf_error(cstate
, "ruleset supported only on PF linktype");
7699 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7700 bpf_error(cstate
, "ruleset names can only be %ld characters",
7701 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7705 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7706 strlen(ruleset
), (const u_char
*)ruleset
);
7710 /* PF firewall log rule number */
7712 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7716 if (cstate
->linktype
!= DLT_PFLOG
) {
7717 bpf_error(cstate
, "rnr supported only on PF linktype");
7721 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7726 /* PF firewall log sub-rule number */
7728 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7732 if (cstate
->linktype
!= DLT_PFLOG
) {
7733 bpf_error(cstate
, "srnr supported only on PF linktype");
7737 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7742 /* PF firewall log reason code */
7744 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7748 if (cstate
->linktype
!= DLT_PFLOG
) {
7749 bpf_error(cstate
, "reason supported only on PF linktype");
7753 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7758 /* PF firewall log action */
7760 gen_pf_action(compiler_state_t
*cstate
, int action
)
7764 if (cstate
->linktype
!= DLT_PFLOG
) {
7765 bpf_error(cstate
, "action supported only on PF linktype");
7769 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7773 #else /* !HAVE_NET_PFVAR_H */
7775 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7777 bpf_error(cstate
, "libpcap was compiled without pf support");
7783 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7785 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7791 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7793 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7799 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7801 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7807 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7809 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7815 gen_pf_action(compiler_state_t
*cstate
, int action
)
7817 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7821 #endif /* HAVE_NET_PFVAR_H */
7823 /* IEEE 802.11 wireless header */
7825 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
7829 switch (cstate
->linktype
) {
7831 case DLT_IEEE802_11
:
7832 case DLT_PRISM_HEADER
:
7833 case DLT_IEEE802_11_RADIO_AVS
:
7834 case DLT_IEEE802_11_RADIO
:
7835 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7840 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
7848 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
7852 switch (cstate
->linktype
) {
7854 case DLT_IEEE802_11
:
7855 case DLT_PRISM_HEADER
:
7856 case DLT_IEEE802_11_RADIO_AVS
:
7857 case DLT_IEEE802_11_RADIO
:
7861 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
7865 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7866 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7872 gen_acode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
7874 switch (cstate
->linktype
) {
7877 case DLT_ARCNET_LINUX
:
7878 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7880 return (gen_ahostop(cstate
, eaddr
, (int)q
.dir
));
7882 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7888 bpf_error(cstate
, "aid supported only on ARCnet");
7891 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7896 static struct block
*
7897 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
7899 register struct block
*b0
, *b1
;
7902 /* src comes first, different from Ethernet */
7904 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
7907 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
7910 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
7911 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
7917 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
7918 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
7923 bpf_error(cstate
, "'addr1' is only supported on 802.11");
7927 bpf_error(cstate
, "'addr2' is only supported on 802.11");
7931 bpf_error(cstate
, "'addr3' is only supported on 802.11");
7935 bpf_error(cstate
, "'addr4' is only supported on 802.11");
7939 bpf_error(cstate
, "'ra' is only supported on 802.11");
7943 bpf_error(cstate
, "'ta' is only supported on 802.11");
7950 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7951 static struct block
*
7952 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
7954 struct block
*b0
, *b1
;
7957 /* generate new filter code based on extracting packet
7959 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
7960 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7962 b0
= new_block(cstate
, JMP(BPF_JEQ
));
7966 if (vlan_num
>= 0) {
7967 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
7968 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7970 b1
= new_block(cstate
, JMP(BPF_JEQ
));
7972 b1
->s
.k
= (bpf_int32
) vlan_num
;
7982 static struct block
*
7983 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
7985 struct block
*b0
, *b1
;
7987 /* check for VLAN, including QinQ */
7988 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
7989 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
7992 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
7996 /* If a specific VLAN is requested, check VLAN id */
7997 if (vlan_num
>= 0) {
7998 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
,
7999 (bpf_int32
)vlan_num
, 0x0fff);
8005 * The payload follows the full header, including the
8006 * VLAN tags, so skip past this VLAN tag.
8008 cstate
->off_linkpl
.constant_part
+= 4;
8011 * The link-layer type information follows the VLAN tags, so
8012 * skip past this VLAN tag.
8014 cstate
->off_linktype
.constant_part
+= 4;
8020 * support IEEE 802.1Q VLAN trunk over ethernet
8023 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
8027 /* can't check for VLAN-encapsulated packets inside MPLS */
8028 if (cstate
->label_stack_depth
> 0)
8029 bpf_error(cstate
, "no VLAN match after MPLS");
8032 * Check for a VLAN packet, and then change the offsets to point
8033 * to the type and data fields within the VLAN packet. Just
8034 * increment the offsets, so that we can support a hierarchy, e.g.
8035 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8038 * XXX - this is a bit of a kludge. If we were to split the
8039 * compiler into a parser that parses an expression and
8040 * generates an expression tree, and a code generator that
8041 * takes an expression tree (which could come from our
8042 * parser or from some other parser) and generates BPF code,
8043 * we could perhaps make the offsets parameters of routines
8044 * and, in the handler for an "AND" node, pass to subnodes
8045 * other than the VLAN node the adjusted offsets.
8047 * This would mean that "vlan" would, instead of changing the
8048 * behavior of *all* tests after it, change only the behavior
8049 * of tests ANDed with it. That would change the documented
8050 * semantics of "vlan", which might break some expressions.
8051 * However, it would mean that "(vlan and ip) or ip" would check
8052 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8053 * checking only for VLAN-encapsulated IP, so that could still
8054 * be considered worth doing; it wouldn't break expressions
8055 * that are of the form "vlan and ..." or "vlan N and ...",
8056 * which I suspect are the most common expressions involving
8057 * "vlan". "vlan or ..." doesn't necessarily do what the user
8058 * would really want, now, as all the "or ..." tests would
8059 * be done assuming a VLAN, even though the "or" could be viewed
8060 * as meaning "or, if this isn't a VLAN packet...".
8062 switch (cstate
->linktype
) {
8065 case DLT_NETANALYZER
:
8066 case DLT_NETANALYZER_TRANSPARENT
:
8067 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8068 /* Verify that this is the outer part of the packet and
8069 * not encapsulated somehow. */
8070 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8071 cstate
->off_linkhdr
.constant_part
==
8072 cstate
->off_outermostlinkhdr
.constant_part
) {
8074 * Do we need special VLAN handling?
8076 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8077 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8079 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8082 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8085 case DLT_IEEE802_11
:
8086 case DLT_PRISM_HEADER
:
8087 case DLT_IEEE802_11_RADIO_AVS
:
8088 case DLT_IEEE802_11_RADIO
:
8089 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8093 bpf_error(cstate
, "no VLAN support for data link type %d",
8098 cstate
->vlan_stack_depth
++;
8107 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8109 struct block
*b0
, *b1
;
8111 if (cstate
->label_stack_depth
> 0) {
8112 /* just match the bottom-of-stack bit clear */
8113 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8116 * We're not in an MPLS stack yet, so check the link-layer
8117 * type against MPLS.
8119 switch (cstate
->linktype
) {
8121 case DLT_C_HDLC
: /* fall through */
8123 case DLT_NETANALYZER
:
8124 case DLT_NETANALYZER_TRANSPARENT
:
8125 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8129 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8132 /* FIXME add other DLT_s ...
8133 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8134 * leave it for now */
8137 bpf_error(cstate
, "no MPLS support for data link type %d",
8145 /* If a specific MPLS label is requested, check it */
8146 if (label_num
>= 0) {
8147 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8148 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8149 0xfffff000); /* only compare the first 20 bits */
8155 * Change the offsets to point to the type and data fields within
8156 * the MPLS packet. Just increment the offsets, so that we
8157 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8158 * capture packets with an outer label of 100000 and an inner
8161 * Increment the MPLS stack depth as well; this indicates that
8162 * we're checking MPLS-encapsulated headers, to make sure higher
8163 * level code generators don't try to match against IP-related
8164 * protocols such as Q_ARP, Q_RARP etc.
8166 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8168 cstate
->off_nl_nosnap
+= 4;
8169 cstate
->off_nl
+= 4;
8170 cstate
->label_stack_depth
++;
8175 * Support PPPOE discovery and session.
8178 gen_pppoed(compiler_state_t
*cstate
)
8180 /* check for PPPoE discovery */
8181 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8185 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8187 struct block
*b0
, *b1
;
8190 * Test against the PPPoE session link-layer type.
8192 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8194 /* If a specific session is requested, check PPPoE session id */
8195 if (sess_num
>= 0) {
8196 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8197 (bpf_int32
)sess_num
, 0x0000ffff);
8203 * Change the offsets to point to the type and data fields within
8204 * the PPP packet, and note that this is PPPoE rather than
8207 * XXX - this is a bit of a kludge. If we were to split the
8208 * compiler into a parser that parses an expression and
8209 * generates an expression tree, and a code generator that
8210 * takes an expression tree (which could come from our
8211 * parser or from some other parser) and generates BPF code,
8212 * we could perhaps make the offsets parameters of routines
8213 * and, in the handler for an "AND" node, pass to subnodes
8214 * other than the PPPoE node the adjusted offsets.
8216 * This would mean that "pppoes" would, instead of changing the
8217 * behavior of *all* tests after it, change only the behavior
8218 * of tests ANDed with it. That would change the documented
8219 * semantics of "pppoes", which might break some expressions.
8220 * However, it would mean that "(pppoes and ip) or ip" would check
8221 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8222 * checking only for VLAN-encapsulated IP, so that could still
8223 * be considered worth doing; it wouldn't break expressions
8224 * that are of the form "pppoes and ..." which I suspect are the
8225 * most common expressions involving "pppoes". "pppoes or ..."
8226 * doesn't necessarily do what the user would really want, now,
8227 * as all the "or ..." tests would be done assuming PPPoE, even
8228 * though the "or" could be viewed as meaning "or, if this isn't
8229 * a PPPoE packet...".
8231 * The "network-layer" protocol is PPPoE, which has a 6-byte
8232 * PPPoE header, followed by a PPP packet.
8234 * There is no HDLC encapsulation for the PPP packet (it's
8235 * encapsulated in PPPoES instead), so the link-layer type
8236 * starts at the first byte of the PPP packet. For PPPoE,
8237 * that offset is relative to the beginning of the total
8238 * link-layer payload, including any 802.2 LLC header, so
8239 * it's 6 bytes past cstate->off_nl.
8241 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8242 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8243 cstate
->off_linkpl
.reg
);
8245 cstate
->off_linktype
= cstate
->off_linkhdr
;
8246 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8249 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8254 /* Check that this is Geneve and the VNI is correct if
8255 * specified. Parameterized to handle both IPv4 and IPv6. */
8256 static struct block
*
8257 gen_geneve_check(compiler_state_t
*cstate
,
8258 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8259 enum e_offrel offrel
, int vni
)
8261 struct block
*b0
, *b1
;
8263 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8265 /* Check that we are operating on version 0. Otherwise, we
8266 * can't decode the rest of the fields. The version is 2 bits
8267 * in the first byte of the Geneve header. */
8268 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8273 vni
<<= 8; /* VNI is in the upper 3 bytes */
8274 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8283 /* The IPv4 and IPv6 Geneve checks need to do two things:
8284 * - Verify that this actually is Geneve with the right VNI.
8285 * - Place the IP header length (plus variable link prefix if
8286 * needed) into register A to be used later to compute
8287 * the inner packet offsets. */
8288 static struct block
*
8289 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8291 struct block
*b0
, *b1
;
8292 struct slist
*s
, *s1
;
8294 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8296 /* Load the IP header length into A. */
8297 s
= gen_loadx_iphdrlen(cstate
);
8299 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8302 /* Forcibly append these statements to the true condition
8303 * of the protocol check by creating a new block that is
8304 * always true and ANDing them. */
8305 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8314 static struct block
*
8315 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8317 struct block
*b0
, *b1
;
8318 struct slist
*s
, *s1
;
8320 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8322 /* Load the IP header length. We need to account for a
8323 * variable length link prefix if there is one. */
8324 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8326 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8330 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8334 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8338 /* Forcibly append these statements to the true condition
8339 * of the protocol check by creating a new block that is
8340 * always true and ANDing them. */
8341 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8344 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8353 /* We need to store three values based on the Geneve header::
8354 * - The offset of the linktype.
8355 * - The offset of the end of the Geneve header.
8356 * - The offset of the end of the encapsulated MAC header. */
8357 static struct slist
*
8358 gen_geneve_offsets(compiler_state_t
*cstate
)
8360 struct slist
*s
, *s1
, *s_proto
;
8362 /* First we need to calculate the offset of the Geneve header
8363 * itself. This is composed of the IP header previously calculated
8364 * (include any variable link prefix) and stored in A plus the
8365 * fixed sized headers (fixed link prefix, MAC length, and UDP
8367 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8368 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8370 /* Stash this in X since we'll need it later. */
8371 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8374 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8376 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8380 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8381 cstate
->off_linktype
.is_variable
= 1;
8382 cstate
->off_linktype
.constant_part
= 0;
8384 s1
= new_stmt(cstate
, BPF_ST
);
8385 s1
->s
.k
= cstate
->off_linktype
.reg
;
8388 /* Load the Geneve option length and mask and shift to get the
8389 * number of bytes. It is stored in the first byte of the Geneve
8391 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8395 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8399 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8403 /* Add in the rest of the Geneve base header. */
8404 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8408 /* Add the Geneve header length to its offset and store. */
8409 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8413 /* Set the encapsulated type as Ethernet. Even though we may
8414 * not actually have Ethernet inside there are two reasons this
8416 * - The linktype field is always in EtherType format regardless
8417 * of whether it is in Geneve or an inner Ethernet frame.
8418 * - The only link layer that we have specific support for is
8419 * Ethernet. We will confirm that the packet actually is
8420 * Ethernet at runtime before executing these checks. */
8421 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8423 s1
= new_stmt(cstate
, BPF_ST
);
8424 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8427 /* Calculate whether we have an Ethernet header or just raw IP/
8428 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8429 * and linktype by 14 bytes so that the network header can be found
8430 * seamlessly. Otherwise, keep what we've calculated already. */
8432 /* We have a bare jmp so we can't use the optimizer. */
8433 cstate
->no_optimize
= 1;
8435 /* Load the EtherType in the Geneve header, 2 bytes in. */
8436 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8440 /* Load X with the end of the Geneve header. */
8441 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8442 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8445 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8446 * end of this check, we should have the total length in X. In
8447 * the non-Ethernet case, it's already there. */
8448 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8449 s_proto
->s
.k
= ETHERTYPE_TEB
;
8450 sappend(s
, s_proto
);
8452 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8456 /* Since this is Ethernet, use the EtherType of the payload
8457 * directly as the linktype. Overwrite what we already have. */
8458 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8462 s1
= new_stmt(cstate
, BPF_ST
);
8463 s1
->s
.k
= cstate
->off_linktype
.reg
;
8466 /* Advance two bytes further to get the end of the Ethernet
8468 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8472 /* Move the result to X. */
8473 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8476 /* Store the final result of our linkpl calculation. */
8477 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8478 cstate
->off_linkpl
.is_variable
= 1;
8479 cstate
->off_linkpl
.constant_part
= 0;
8481 s1
= new_stmt(cstate
, BPF_STX
);
8482 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8491 /* Check to see if this is a Geneve packet. */
8493 gen_geneve(compiler_state_t
*cstate
, int vni
)
8495 struct block
*b0
, *b1
;
8498 b0
= gen_geneve4(cstate
, vni
);
8499 b1
= gen_geneve6(cstate
, vni
);
8504 /* Later filters should act on the payload of the Geneve frame,
8505 * update all of the header pointers. Attach this code so that
8506 * it gets executed in the event that the Geneve filter matches. */
8507 s
= gen_geneve_offsets(cstate
);
8509 b1
= gen_true(cstate
);
8510 sappend(s
, b1
->stmts
);
8515 cstate
->is_geneve
= 1;
8520 /* Check that the encapsulated frame has a link layer header
8521 * for Ethernet filters. */
8522 static struct block
*
8523 gen_geneve_ll_check(compiler_state_t
*cstate
)
8526 struct slist
*s
, *s1
;
8528 /* The easiest way to see if there is a link layer present
8529 * is to check if the link layer header and payload are not
8532 /* Geneve always generates pure variable offsets so we can
8533 * compare only the registers. */
8534 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8535 s
->s
.k
= cstate
->off_linkhdr
.reg
;
8537 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8538 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8541 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8550 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
8551 bpf_u_int32 jtype
, int reverse
)
8558 if (!cstate
->is_atm
)
8559 bpf_error(cstate
, "'vpi' supported only on raw ATM");
8560 if (cstate
->off_vpi
== (u_int
)-1)
8562 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
8567 if (!cstate
->is_atm
)
8568 bpf_error(cstate
, "'vci' supported only on raw ATM");
8569 if (cstate
->off_vci
== (u_int
)-1)
8571 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
8576 if (cstate
->off_proto
== (u_int
)-1)
8577 abort(); /* XXX - this isn't on FreeBSD */
8578 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
8583 if (cstate
->off_payload
== (u_int
)-1)
8585 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
8586 0xffffffff, jtype
, reverse
, jvalue
);
8590 if (!cstate
->is_atm
)
8591 bpf_error(cstate
, "'callref' supported only on raw ATM");
8592 if (cstate
->off_proto
== (u_int
)-1)
8594 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
8595 jtype
, reverse
, jvalue
);
8605 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
8607 struct block
*b0
, *b1
;
8612 /* Get all packets in Meta signalling Circuit */
8613 if (!cstate
->is_atm
)
8614 bpf_error(cstate
, "'metac' supported only on raw ATM");
8615 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8616 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
8621 /* Get all packets in Broadcast Circuit*/
8622 if (!cstate
->is_atm
)
8623 bpf_error(cstate
, "'bcc' supported only on raw ATM");
8624 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8625 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
8630 /* Get all cells in Segment OAM F4 circuit*/
8631 if (!cstate
->is_atm
)
8632 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
8633 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8634 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8639 /* Get all cells in End-to-End OAM F4 Circuit*/
8640 if (!cstate
->is_atm
)
8641 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
8642 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8643 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8648 /* Get all packets in connection Signalling Circuit */
8649 if (!cstate
->is_atm
)
8650 bpf_error(cstate
, "'sc' supported only on raw ATM");
8651 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8652 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
8657 /* Get all packets in ILMI Circuit */
8658 if (!cstate
->is_atm
)
8659 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
8660 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8661 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
8666 /* Get all LANE packets */
8667 if (!cstate
->is_atm
)
8668 bpf_error(cstate
, "'lane' supported only on raw ATM");
8669 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8672 * Arrange that all subsequent tests assume LANE
8673 * rather than LLC-encapsulated packets, and set
8674 * the offsets appropriately for LANE-encapsulated
8677 * We assume LANE means Ethernet, not Token Ring.
8679 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
8680 cstate
->off_payload
+ 2, /* Ethernet header */
8682 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
8683 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
8684 cstate
->off_nl
= 0; /* Ethernet II */
8685 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
8689 /* Get all LLC-encapsulated packets */
8690 if (!cstate
->is_atm
)
8691 bpf_error(cstate
, "'llc' supported only on raw ATM");
8692 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8693 cstate
->linktype
= cstate
->prevlinktype
;
8703 * Filtering for MTP2 messages based on li value
8704 * FISU, length is null
8705 * LSSU, length is 1 or 2
8706 * MSU, length is 3 or more
8707 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8710 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
8712 struct block
*b0
, *b1
;
8717 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8718 (cstate
->linktype
!= DLT_ERF
) &&
8719 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8720 bpf_error(cstate
, "'fisu' supported only on MTP2");
8721 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8722 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8726 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8727 (cstate
->linktype
!= DLT_ERF
) &&
8728 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8729 bpf_error(cstate
, "'lssu' supported only on MTP2");
8730 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8731 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8736 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8737 (cstate
->linktype
!= DLT_ERF
) &&
8738 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8739 bpf_error(cstate
, "'msu' supported only on MTP2");
8740 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8744 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8745 (cstate
->linktype
!= DLT_ERF
) &&
8746 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8747 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
8748 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8749 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8753 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8754 (cstate
->linktype
!= DLT_ERF
) &&
8755 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8756 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
8757 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8758 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8763 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8764 (cstate
->linktype
!= DLT_ERF
) &&
8765 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8766 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
8767 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8777 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
8778 bpf_u_int32 jtype
, int reverse
)
8781 bpf_u_int32 val1
, val2
, val3
;
8782 u_int newoff_sio
= cstate
->off_sio
;
8783 u_int newoff_opc
= cstate
->off_opc
;
8784 u_int newoff_dpc
= cstate
->off_dpc
;
8785 u_int newoff_sls
= cstate
->off_sls
;
8787 switch (mtp3field
) {
8790 newoff_sio
+= 3; /* offset for MTP2_HSL */
8794 if (cstate
->off_sio
== (u_int
)-1)
8795 bpf_error(cstate
, "'sio' supported only on SS7");
8796 /* sio coded on 1 byte so max value 255 */
8798 bpf_error(cstate
, "sio value %u too big; max value = 255",
8800 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8801 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8807 if (cstate
->off_opc
== (u_int
)-1)
8808 bpf_error(cstate
, "'opc' supported only on SS7");
8809 /* opc coded on 14 bits so max value 16383 */
8811 bpf_error(cstate
, "opc value %u too big; max value = 16383",
8813 /* the following instructions are made to convert jvalue
8814 * to the form used to write opc in an ss7 message*/
8815 val1
= jvalue
& 0x00003c00;
8817 val2
= jvalue
& 0x000003fc;
8819 val3
= jvalue
& 0x00000003;
8821 jvalue
= val1
+ val2
+ val3
;
8822 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8823 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8831 if (cstate
->off_dpc
== (u_int
)-1)
8832 bpf_error(cstate
, "'dpc' supported only on SS7");
8833 /* dpc coded on 14 bits so max value 16383 */
8835 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
8837 /* the following instructions are made to convert jvalue
8838 * to the forme used to write dpc in an ss7 message*/
8839 val1
= jvalue
& 0x000000ff;
8841 val2
= jvalue
& 0x00003f00;
8843 jvalue
= val1
+ val2
;
8844 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8845 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8851 if (cstate
->off_sls
== (u_int
)-1)
8852 bpf_error(cstate
, "'sls' supported only on SS7");
8853 /* sls coded on 4 bits so max value 15 */
8855 bpf_error(cstate
, "sls value %u too big; max value = 15",
8857 /* the following instruction is made to convert jvalue
8858 * to the forme used to write sls in an ss7 message*/
8859 jvalue
= jvalue
<< 4;
8860 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8861 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8870 static struct block
*
8871 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
8876 * Q.2931 signalling protocol messages for handling virtual circuits
8877 * establishment and teardown
8882 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8886 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8890 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8894 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8898 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8901 case A_RELEASE_DONE
:
8902 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8912 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
8914 struct block
*b0
, *b1
;
8919 if (!cstate
->is_atm
)
8920 bpf_error(cstate
, "'oam' supported only on raw ATM");
8921 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
8925 if (!cstate
->is_atm
)
8926 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
8928 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8929 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8931 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8937 * Get Q.2931 signalling messages for switched
8938 * virtual connection
8940 if (!cstate
->is_atm
)
8941 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
8942 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
8943 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
8945 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
8947 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
8949 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
8951 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
8953 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
8958 if (!cstate
->is_atm
)
8959 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
8960 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
8961 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
8963 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
8965 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
8967 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
8969 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);