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.
27 #include <pcap-types.h>
31 #include <sys/socket.h>
34 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
53 #include "ethertype.h"
57 #include "ieee80211.h"
59 #include "sunatmpos.h"
62 #include "pcap/ipnet.h"
68 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
69 #include <linux/types.h>
70 #include <linux/if_packet.h>
71 #include <linux/filter.h>
74 #ifdef HAVE_NET_PFVAR_H
75 #include <sys/socket.h>
77 #include <net/pfvar.h>
78 #include <net/if_pflog.h>
82 #define offsetof(s, e) ((size_t)&((s *)0)->e)
87 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
94 uint16_t u6_addr16
[8];
95 uint32_t u6_addr32
[4];
97 #define s6_addr in6_u.u6_addr8
98 #define s6_addr16 in6_u.u6_addr16
99 #define s6_addr32 in6_u.u6_addr32
100 #define s6_addr64 in6_u.u6_addr64
103 typedef unsigned short sa_family_t
;
105 #define __SOCKADDR_COMMON(sa_prefix) \
106 sa_family_t sa_prefix##family
108 /* Ditto, for IPv6. */
111 __SOCKADDR_COMMON (sin6_
);
112 uint16_t sin6_port
; /* Transport layer port # */
113 uint32_t sin6_flowinfo
; /* IPv6 flow information */
114 struct in6_addr sin6_addr
; /* IPv6 address */
117 #ifndef EAI_ADDRFAMILY
119 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
120 int ai_family
; /* PF_xxx */
121 int ai_socktype
; /* SOCK_xxx */
122 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
123 size_t ai_addrlen
; /* length of ai_addr */
124 char *ai_canonname
; /* canonical name for hostname */
125 struct sockaddr
*ai_addr
; /* binary address */
126 struct addrinfo
*ai_next
; /* next structure in linked list */
128 #endif /* EAI_ADDRFAMILY */
129 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
131 #include <netdb.h> /* for "struct addrinfo" */
134 #include <pcap/namedb.h>
136 #include "nametoaddr.h"
138 #define ETHERMTU 1500
140 #ifndef ETHERTYPE_TEB
141 #define ETHERTYPE_TEB 0x6558
144 #ifndef IPPROTO_HOPOPTS
145 #define IPPROTO_HOPOPTS 0
147 #ifndef IPPROTO_ROUTING
148 #define IPPROTO_ROUTING 43
150 #ifndef IPPROTO_FRAGMENT
151 #define IPPROTO_FRAGMENT 44
153 #ifndef IPPROTO_DSTOPTS
154 #define IPPROTO_DSTOPTS 60
157 #define IPPROTO_SCTP 132
160 #define GENEVE_PORT 6081
162 #ifdef HAVE_OS_PROTO_H
163 #include "os-proto.h"
166 #define JMP(c) ((c)|BPF_JMP|BPF_K)
169 * "Push" the current value of the link-layer header type and link-layer
170 * header offset onto a "stack", and set a new value. (It's not a
171 * full-blown stack; we keep only the top two items.)
173 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
175 (cs)->prevlinktype = (cs)->linktype; \
176 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
177 (cs)->linktype = (new_linktype); \
178 (cs)->off_linkhdr.is_variable = (new_is_variable); \
179 (cs)->off_linkhdr.constant_part = (new_constant_part); \
180 (cs)->off_linkhdr.reg = (new_reg); \
181 (cs)->is_geneve = 0; \
185 * Offset "not set" value.
187 #define OFFSET_NOT_SET 0xffffffffU
190 * Absolute offsets, which are offsets from the beginning of the raw
191 * packet data, are, in the general case, the sum of a variable value
192 * and a constant value; the variable value may be absent, in which
193 * case the offset is only the constant value, and the constant value
194 * may be zero, in which case the offset is only the variable value.
196 * bpf_abs_offset is a structure containing all that information:
198 * is_variable is 1 if there's a variable part.
200 * constant_part is the constant part of the value, possibly zero;
202 * if is_variable is 1, reg is the register number for a register
203 * containing the variable value if the register has been assigned,
213 * Value passed to gen_load_a() to indicate what the offset argument
214 * is relative to the beginning of.
217 OR_PACKET
, /* full packet data */
218 OR_LINKHDR
, /* link-layer header */
219 OR_PREVLINKHDR
, /* previous link-layer header */
220 OR_LLC
, /* 802.2 LLC header */
221 OR_PREVMPLSHDR
, /* previous MPLS header */
222 OR_LINKTYPE
, /* link-layer type */
223 OR_LINKPL
, /* link-layer payload */
224 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
225 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
226 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
230 * We divy out chunks of memory rather than call malloc each time so
231 * we don't have to worry about leaking memory. It's probably
232 * not a big deal if all this memory was wasted but if this ever
233 * goes into a library that would probably not be a good idea.
235 * XXX - this *is* in a library....
238 #define CHUNK0SIZE 1024
244 /* Code generator state */
246 struct _compiler_state
{
256 int outermostlinktype
;
261 /* Hack for handling VLAN and MPLS stacks. */
262 u_int label_stack_depth
;
263 u_int vlan_stack_depth
;
270 * As errors are handled by a longjmp, anything allocated must
271 * be freed in the longjmp handler, so it must be reachable
274 * One thing that's allocated is the result of pcap_nametoaddrinfo();
275 * it must be freed with freeaddrinfo(). This variable points to
276 * any addrinfo structure that would need to be freed.
282 * Various code constructs need to know the layout of the packet.
283 * These values give the necessary offsets from the beginning
284 * of the packet data.
288 * Absolute offset of the beginning of the link-layer header.
290 bpf_abs_offset off_linkhdr
;
293 * If we're checking a link-layer header for a packet encapsulated
294 * in another protocol layer, this is the equivalent information
295 * for the previous layers' link-layer header from the beginning
296 * of the raw packet data.
298 bpf_abs_offset off_prevlinkhdr
;
301 * This is the equivalent information for the outermost layers'
304 bpf_abs_offset off_outermostlinkhdr
;
307 * Absolute offset of the beginning of the link-layer payload.
309 bpf_abs_offset off_linkpl
;
312 * "off_linktype" is the offset to information in the link-layer
313 * header giving the packet type. This is an absolute offset
314 * from the beginning of the packet.
316 * For Ethernet, it's the offset of the Ethernet type field; this
317 * means that it must have a value that skips VLAN tags.
319 * For link-layer types that always use 802.2 headers, it's the
320 * offset of the LLC header; this means that it must have a value
321 * that skips VLAN tags.
323 * For PPP, it's the offset of the PPP type field.
325 * For Cisco HDLC, it's the offset of the CHDLC type field.
327 * For BSD loopback, it's the offset of the AF_ value.
329 * For Linux cooked sockets, it's the offset of the type field.
331 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
332 * encapsulation, in which case, IP is assumed.
334 bpf_abs_offset off_linktype
;
337 * TRUE if the link layer includes an ATM pseudo-header.
342 * TRUE if "geneve" appeared in the filter; it causes us to
343 * generate code that checks for a Geneve header and assume
344 * that later filters apply to the encapsulated payload.
349 * TRUE if we need variable length part of VLAN offset
351 int is_vlan_vloffset
;
354 * These are offsets for the ATM pseudo-header.
361 * These are offsets for the MTP2 fields.
367 * These are offsets for the MTP3 fields.
375 * This is the offset of the first byte after the ATM pseudo_header,
376 * or -1 if there is no ATM pseudo-header.
381 * These are offsets to the beginning of the network-layer header.
382 * They are relative to the beginning of the link-layer payload
383 * (i.e., they don't include off_linkhdr.constant_part or
384 * off_linkpl.constant_part).
386 * If the link layer never uses 802.2 LLC:
388 * "off_nl" and "off_nl_nosnap" are the same.
390 * If the link layer always uses 802.2 LLC:
392 * "off_nl" is the offset if there's a SNAP header following
395 * "off_nl_nosnap" is the offset if there's no SNAP header.
397 * If the link layer is Ethernet:
399 * "off_nl" is the offset if the packet is an Ethernet II packet
400 * (we assume no 802.3+802.2+SNAP);
402 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
403 * with an 802.2 header following it.
409 * Here we handle simple allocation of the scratch registers.
410 * If too many registers are alloc'd, the allocator punts.
412 int regused
[BPF_MEMWORDS
];
418 struct chunk chunks
[NCHUNKS
];
423 bpf_syntax_error(compiler_state_t
*cstate
, const char *msg
)
425 bpf_error(cstate
, "syntax error in filter expression: %s", msg
);
431 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
436 if (cstate
->bpf_pcap
!= NULL
)
437 (void)pcap_vsnprintf(pcap_geterr(cstate
->bpf_pcap
),
438 PCAP_ERRBUF_SIZE
, fmt
, ap
);
440 longjmp(cstate
->top_ctx
, 1);
444 static void init_linktype(compiler_state_t
*, pcap_t
*);
446 static void init_regs(compiler_state_t
*);
447 static int alloc_reg(compiler_state_t
*);
448 static void free_reg(compiler_state_t
*, int);
450 static void initchunks(compiler_state_t
*cstate
);
451 static void *newchunk(compiler_state_t
*cstate
, size_t);
452 static void freechunks(compiler_state_t
*cstate
);
453 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
454 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
455 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
456 static inline void syntax(compiler_state_t
*cstate
);
458 static void backpatch(struct block
*, struct block
*);
459 static void merge(struct block
*, struct block
*);
460 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
462 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
464 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
466 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
468 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
470 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
471 u_int
, bpf_int32
, bpf_u_int32
);
472 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
473 u_int
, const u_char
*);
474 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
475 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
476 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
478 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
480 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
481 static struct block
*gen_uncond(compiler_state_t
*, int);
482 static inline struct block
*gen_true(compiler_state_t
*);
483 static inline struct block
*gen_false(compiler_state_t
*);
484 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
485 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
486 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
487 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
488 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
489 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
490 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
491 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
492 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
494 static int ethertype_to_ppptype(int);
495 static struct block
*gen_linktype(compiler_state_t
*, int);
496 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
497 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
498 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
499 int, int, u_int
, u_int
);
501 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
502 struct in6_addr
*, int, int, u_int
, u_int
);
504 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
505 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
506 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
507 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
508 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
509 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
510 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
511 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
512 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
515 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
516 struct in6_addr
*, int, int, int);
519 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
520 bpf_u_int32
**, int, int);
522 static struct block
*gen_ipfrag(compiler_state_t
*);
523 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
524 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
526 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
527 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
529 struct block
*gen_portop(compiler_state_t
*, int, int, int);
530 static struct block
*gen_port(compiler_state_t
*, int, int, int);
531 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
532 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
533 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
534 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
535 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
536 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
537 static int lookup_proto(compiler_state_t
*, const char *, int);
538 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
539 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
540 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
541 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
542 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
543 static struct block
*gen_len(compiler_state_t
*, int, int);
544 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
545 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
547 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
548 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
551 initchunks(compiler_state_t
*cstate
)
555 for (i
= 0; i
< NCHUNKS
; i
++) {
556 cstate
->chunks
[i
].n_left
= 0;
557 cstate
->chunks
[i
].m
= NULL
;
559 cstate
->cur_chunk
= 0;
563 newchunk(compiler_state_t
*cstate
, size_t n
)
570 /* XXX Round up to nearest long. */
571 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
573 /* XXX Round up to structure boundary. */
577 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
578 if (n
> cp
->n_left
) {
579 ++cp
, k
= ++cstate
->cur_chunk
;
581 bpf_error(cstate
, "out of memory");
582 size
= CHUNK0SIZE
<< k
;
583 cp
->m
= (void *)malloc(size
);
585 bpf_error(cstate
, "out of memory");
586 memset((char *)cp
->m
, 0, size
);
589 bpf_error(cstate
, "out of memory");
592 return (void *)((char *)cp
->m
+ cp
->n_left
);
596 freechunks(compiler_state_t
*cstate
)
600 for (i
= 0; i
< NCHUNKS
; ++i
)
601 if (cstate
->chunks
[i
].m
!= NULL
)
602 free(cstate
->chunks
[i
].m
);
606 * A strdup whose allocations are freed after code generation is over.
609 sdup(compiler_state_t
*cstate
, const char *s
)
611 size_t n
= strlen(s
) + 1;
612 char *cp
= newchunk(cstate
, n
);
618 static inline struct block
*
619 new_block(compiler_state_t
*cstate
, int code
)
623 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
630 static inline struct slist
*
631 new_stmt(compiler_state_t
*cstate
, int code
)
635 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
641 static struct block
*
642 gen_retblk(compiler_state_t
*cstate
, int v
)
644 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
651 syntax(compiler_state_t
*cstate
)
653 bpf_error(cstate
, "syntax error in filter expression");
657 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
658 const char *buf
, int optimize
, bpf_u_int32 mask
)
663 compiler_state_t cstate
;
664 const char * volatile xbuf
= buf
;
665 yyscan_t scanner
= NULL
;
666 YY_BUFFER_STATE in_buffer
= NULL
;
671 * If this pcap_t hasn't been activated, it doesn't have a
672 * link-layer type, so we can't use it.
675 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
676 "not-yet-activated pcap_t passed to pcap_compile");
688 * If the device on which we're capturing need to be notified
689 * that a new filter is being compiled, do so.
691 * This allows them to save a copy of it, in case, for example,
692 * they're implementing a form of remote packet capture, and
693 * want the remote machine to filter out the packets in which
694 * it's sending the packets it's captured.
696 * XXX - the fact that we happen to be compiling a filter
697 * doesn't necessarily mean we'll be installing it as the
698 * filter for this pcap_t; we might be running it from userland
699 * on captured packets to do packet classification. We really
700 * need a better way of handling this, but this is all that
701 * the WinPcap code did.
703 if (p
->save_current_filter_op
!= NULL
)
704 (p
->save_current_filter_op
)(p
, buf
);
708 cstate
.no_optimize
= 0;
712 cstate
.ic
.root
= NULL
;
713 cstate
.ic
.cur_mark
= 0;
717 if (setjmp(cstate
.top_ctx
)) {
719 if (cstate
.ai
!= NULL
)
720 freeaddrinfo(cstate
.ai
);
726 cstate
.netmask
= mask
;
728 cstate
.snaplen
= pcap_snapshot(p
);
729 if (cstate
.snaplen
== 0) {
730 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
731 "snaplen of 0 rejects all packets");
736 if (pcap_lex_init(&scanner
) != 0)
737 bpf_error(&cstate
, "can't initialize scanner: %s", pcap_strerror(errno
));
738 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
741 * Associate the compiler state with the lexical analyzer
744 pcap_set_extra(&cstate
, scanner
);
746 init_linktype(&cstate
, p
);
747 (void)pcap_parse(scanner
, &cstate
);
749 if (cstate
.ic
.root
== NULL
)
750 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
752 if (optimize
&& !cstate
.no_optimize
) {
753 bpf_optimize(&cstate
, &cstate
.ic
);
754 if (cstate
.ic
.root
== NULL
||
755 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
756 bpf_error(&cstate
, "expression rejects all packets");
758 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
759 program
->bf_len
= len
;
761 rc
= 0; /* We're all okay */
765 * Clean up everything for the lexical analyzer.
767 if (in_buffer
!= NULL
)
768 pcap__delete_buffer(in_buffer
, scanner
);
770 pcap_lex_destroy(scanner
);
773 * Clean up our own allocated memory.
781 * entry point for using the compiler with no pcap open
782 * pass in all the stuff that is needed explicitly instead.
785 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
786 struct bpf_program
*program
,
787 const char *buf
, int optimize
, bpf_u_int32 mask
)
792 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
795 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
801 * Clean up a "struct bpf_program" by freeing all the memory allocated
805 pcap_freecode(struct bpf_program
*program
)
808 if (program
->bf_insns
!= NULL
) {
809 free((char *)program
->bf_insns
);
810 program
->bf_insns
= NULL
;
815 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
816 * which of the jt and jf fields has been resolved and which is a pointer
817 * back to another unresolved block (or nil). At least one of the fields
818 * in each block is already resolved.
821 backpatch(list
, target
)
822 struct block
*list
, *target
;
839 * Merge the lists in b0 and b1, using the 'sense' field to indicate
840 * which of jt and jf is the link.
844 struct block
*b0
, *b1
;
846 register struct block
**p
= &b0
;
848 /* Find end of list. */
850 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
852 /* Concatenate the lists. */
857 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
859 struct block
*ppi_dlt_check
;
862 * Insert before the statements of the first (root) block any
863 * statements needed to load the lengths of any variable-length
864 * headers into registers.
866 * XXX - a fancier strategy would be to insert those before the
867 * statements of all blocks that use those lengths and that
868 * have no predecessors that use them, so that we only compute
869 * the lengths if we need them. There might be even better
870 * approaches than that.
872 * However, those strategies would be more complicated, and
873 * as we don't generate code to compute a length if the
874 * program has no tests that use the length, and as most
875 * tests will probably use those lengths, we would just
876 * postpone computing the lengths so that it's not done
877 * for tests that fail early, and it's not clear that's
880 insert_compute_vloffsets(cstate
, p
->head
);
883 * For DLT_PPI captures, generate a check of the per-packet
884 * DLT value to make sure it's DLT_IEEE802_11.
886 * XXX - TurboCap cards use DLT_PPI for Ethernet.
887 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
888 * with appropriate Ethernet information and use that rather
889 * than using something such as DLT_PPI where you don't know
890 * the link-layer header type until runtime, which, in the
891 * general case, would force us to generate both Ethernet *and*
892 * 802.11 code (*and* anything else for which PPI is used)
893 * and choose between them early in the BPF program?
895 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
896 if (ppi_dlt_check
!= NULL
)
897 gen_and(ppi_dlt_check
, p
);
899 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
900 p
->sense
= !p
->sense
;
901 backpatch(p
, gen_retblk(cstate
, 0));
902 cstate
->ic
.root
= p
->head
;
907 struct block
*b0
, *b1
;
909 backpatch(b0
, b1
->head
);
910 b0
->sense
= !b0
->sense
;
911 b1
->sense
= !b1
->sense
;
913 b1
->sense
= !b1
->sense
;
919 struct block
*b0
, *b1
;
921 b0
->sense
= !b0
->sense
;
922 backpatch(b0
, b1
->head
);
923 b0
->sense
= !b0
->sense
;
932 b
->sense
= !b
->sense
;
935 static struct block
*
936 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
937 u_int size
, bpf_int32 v
)
939 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
942 static struct block
*
943 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
944 u_int size
, bpf_int32 v
)
946 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
949 static struct block
*
950 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
951 u_int size
, bpf_int32 v
)
953 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
956 static struct block
*
957 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
958 u_int size
, bpf_int32 v
)
960 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
963 static struct block
*
964 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
965 u_int size
, bpf_int32 v
)
967 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
970 static struct block
*
971 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
972 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
974 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
977 static struct block
*
978 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
979 u_int size
, const u_char
*v
)
981 register struct block
*b
, *tmp
;
985 register const u_char
*p
= &v
[size
- 4];
986 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
987 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
989 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
, w
);
996 register const u_char
*p
= &v
[size
- 2];
997 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
999 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
, w
);
1006 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1015 * AND the field of size "size" at offset "offset" relative to the header
1016 * specified by "offrel" with "mask", and compare it with the value "v"
1017 * with the test specified by "jtype"; if "reverse" is true, the test
1018 * should test the opposite of "jtype".
1020 static struct block
*
1021 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1022 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1025 struct slist
*s
, *s2
;
1028 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1030 if (mask
!= 0xffffffff) {
1031 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1036 b
= new_block(cstate
, JMP(jtype
));
1039 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1045 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1047 cstate
->pcap_fddipad
= p
->fddipad
;
1050 * We start out with only one link-layer header.
1052 cstate
->outermostlinktype
= pcap_datalink(p
);
1053 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1054 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1055 cstate
->off_outermostlinkhdr
.reg
= -1;
1057 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1058 cstate
->off_prevlinkhdr
.constant_part
= 0;
1059 cstate
->off_prevlinkhdr
.is_variable
= 0;
1060 cstate
->off_prevlinkhdr
.reg
= -1;
1062 cstate
->linktype
= cstate
->outermostlinktype
;
1063 cstate
->off_linkhdr
.constant_part
= 0;
1064 cstate
->off_linkhdr
.is_variable
= 0;
1065 cstate
->off_linkhdr
.reg
= -1;
1070 cstate
->off_linkpl
.constant_part
= 0;
1071 cstate
->off_linkpl
.is_variable
= 0;
1072 cstate
->off_linkpl
.reg
= -1;
1074 cstate
->off_linktype
.constant_part
= 0;
1075 cstate
->off_linktype
.is_variable
= 0;
1076 cstate
->off_linktype
.reg
= -1;
1079 * Assume it's not raw ATM with a pseudo-header, for now.
1082 cstate
->off_vpi
= -1;
1083 cstate
->off_vci
= -1;
1084 cstate
->off_proto
= -1;
1085 cstate
->off_payload
= -1;
1090 cstate
->is_geneve
= 0;
1093 * No variable length VLAN offset by default
1095 cstate
->is_vlan_vloffset
= 0;
1098 * And assume we're not doing SS7.
1100 cstate
->off_li
= -1;
1101 cstate
->off_li_hsl
= -1;
1102 cstate
->off_sio
= -1;
1103 cstate
->off_opc
= -1;
1104 cstate
->off_dpc
= -1;
1105 cstate
->off_sls
= -1;
1107 cstate
->label_stack_depth
= 0;
1108 cstate
->vlan_stack_depth
= 0;
1110 switch (cstate
->linktype
) {
1113 cstate
->off_linktype
.constant_part
= 2;
1114 cstate
->off_linkpl
.constant_part
= 6;
1115 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1116 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1119 case DLT_ARCNET_LINUX
:
1120 cstate
->off_linktype
.constant_part
= 4;
1121 cstate
->off_linkpl
.constant_part
= 8;
1122 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1123 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1127 cstate
->off_linktype
.constant_part
= 12;
1128 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1129 cstate
->off_nl
= 0; /* Ethernet II */
1130 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1135 * SLIP doesn't have a link level type. The 16 byte
1136 * header is hacked into our SLIP driver.
1138 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1139 cstate
->off_linkpl
.constant_part
= 16;
1141 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1144 case DLT_SLIP_BSDOS
:
1145 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1146 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1148 cstate
->off_linkpl
.constant_part
= 24;
1150 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1155 cstate
->off_linktype
.constant_part
= 0;
1156 cstate
->off_linkpl
.constant_part
= 4;
1158 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1162 cstate
->off_linktype
.constant_part
= 0;
1163 cstate
->off_linkpl
.constant_part
= 12;
1165 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1170 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1171 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1172 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1173 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1175 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1180 * This does no include the Ethernet header, and
1181 * only covers session state.
1183 cstate
->off_linktype
.constant_part
= 6;
1184 cstate
->off_linkpl
.constant_part
= 8;
1186 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1190 cstate
->off_linktype
.constant_part
= 5;
1191 cstate
->off_linkpl
.constant_part
= 24;
1193 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1198 * FDDI doesn't really have a link-level type field.
1199 * We set "off_linktype" to the offset of the LLC header.
1201 * To check for Ethernet types, we assume that SSAP = SNAP
1202 * is being used and pick out the encapsulated Ethernet type.
1203 * XXX - should we generate code to check for SNAP?
1205 cstate
->off_linktype
.constant_part
= 13;
1206 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1207 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1208 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1209 cstate
->off_nl
= 8; /* 802.2+SNAP */
1210 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1215 * Token Ring doesn't really have a link-level type field.
1216 * We set "off_linktype" to the offset of the LLC header.
1218 * To check for Ethernet types, we assume that SSAP = SNAP
1219 * is being used and pick out the encapsulated Ethernet type.
1220 * XXX - should we generate code to check for SNAP?
1222 * XXX - the header is actually variable-length.
1223 * Some various Linux patched versions gave 38
1224 * as "off_linktype" and 40 as "off_nl"; however,
1225 * if a token ring packet has *no* routing
1226 * information, i.e. is not source-routed, the correct
1227 * values are 20 and 22, as they are in the vanilla code.
1229 * A packet is source-routed iff the uppermost bit
1230 * of the first byte of the source address, at an
1231 * offset of 8, has the uppermost bit set. If the
1232 * packet is source-routed, the total number of bytes
1233 * of routing information is 2 plus bits 0x1F00 of
1234 * the 16-bit value at an offset of 14 (shifted right
1235 * 8 - figure out which byte that is).
1237 cstate
->off_linktype
.constant_part
= 14;
1238 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1239 cstate
->off_nl
= 8; /* 802.2+SNAP */
1240 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1243 case DLT_PRISM_HEADER
:
1244 case DLT_IEEE802_11_RADIO_AVS
:
1245 case DLT_IEEE802_11_RADIO
:
1246 cstate
->off_linkhdr
.is_variable
= 1;
1247 /* Fall through, 802.11 doesn't have a variable link
1248 * prefix but is otherwise the same. */
1250 case DLT_IEEE802_11
:
1252 * 802.11 doesn't really have a link-level type field.
1253 * We set "off_linktype.constant_part" to the offset of
1256 * To check for Ethernet types, we assume that SSAP = SNAP
1257 * is being used and pick out the encapsulated Ethernet type.
1258 * XXX - should we generate code to check for SNAP?
1260 * We also handle variable-length radio headers here.
1261 * The Prism header is in theory variable-length, but in
1262 * practice it's always 144 bytes long. However, some
1263 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1264 * sometimes or always supply an AVS header, so we
1265 * have to check whether the radio header is a Prism
1266 * header or an AVS header, so, in practice, it's
1269 cstate
->off_linktype
.constant_part
= 24;
1270 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1271 cstate
->off_linkpl
.is_variable
= 1;
1272 cstate
->off_nl
= 8; /* 802.2+SNAP */
1273 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1278 * At the moment we treat PPI the same way that we treat
1279 * normal Radiotap encoded packets. The difference is in
1280 * the function that generates the code at the beginning
1281 * to compute the header length. Since this code generator
1282 * of PPI supports bare 802.11 encapsulation only (i.e.
1283 * the encapsulated DLT should be DLT_IEEE802_11) we
1284 * generate code to check for this too.
1286 cstate
->off_linktype
.constant_part
= 24;
1287 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1288 cstate
->off_linkpl
.is_variable
= 1;
1289 cstate
->off_linkhdr
.is_variable
= 1;
1290 cstate
->off_nl
= 8; /* 802.2+SNAP */
1291 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1294 case DLT_ATM_RFC1483
:
1295 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1297 * assume routed, non-ISO PDUs
1298 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1300 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1301 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1302 * latter would presumably be treated the way PPPoE
1303 * should be, so you can do "pppoe and udp port 2049"
1304 * or "pppoa and tcp port 80" and have it check for
1305 * PPPo{A,E} and a PPP protocol of IP and....
1307 cstate
->off_linktype
.constant_part
= 0;
1308 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1309 cstate
->off_nl
= 8; /* 802.2+SNAP */
1310 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1315 * Full Frontal ATM; you get AALn PDUs with an ATM
1319 cstate
->off_vpi
= SUNATM_VPI_POS
;
1320 cstate
->off_vci
= SUNATM_VCI_POS
;
1321 cstate
->off_proto
= PROTO_POS
;
1322 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1323 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1324 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1325 cstate
->off_nl
= 8; /* 802.2+SNAP */
1326 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1332 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1333 cstate
->off_linkpl
.constant_part
= 0;
1335 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1338 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1339 cstate
->off_linktype
.constant_part
= 14;
1340 cstate
->off_linkpl
.constant_part
= 16;
1342 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1347 * LocalTalk does have a 1-byte type field in the LLAP header,
1348 * but really it just indicates whether there is a "short" or
1349 * "long" DDP packet following.
1351 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1352 cstate
->off_linkpl
.constant_part
= 0;
1354 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1357 case DLT_IP_OVER_FC
:
1359 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1360 * link-level type field. We set "off_linktype" to the
1361 * offset of the LLC header.
1363 * To check for Ethernet types, we assume that SSAP = SNAP
1364 * is being used and pick out the encapsulated Ethernet type.
1365 * XXX - should we generate code to check for SNAP? RFC
1366 * 2625 says SNAP should be used.
1368 cstate
->off_linktype
.constant_part
= 16;
1369 cstate
->off_linkpl
.constant_part
= 16;
1370 cstate
->off_nl
= 8; /* 802.2+SNAP */
1371 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1376 * XXX - we should set this to handle SNAP-encapsulated
1377 * frames (NLPID of 0x80).
1379 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1380 cstate
->off_linkpl
.constant_part
= 0;
1382 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1386 * the only BPF-interesting FRF.16 frames are non-control frames;
1387 * Frame Relay has a variable length link-layer
1388 * so lets start with offset 4 for now and increments later on (FIXME);
1391 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1392 cstate
->off_linkpl
.constant_part
= 0;
1394 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1397 case DLT_APPLE_IP_OVER_IEEE1394
:
1398 cstate
->off_linktype
.constant_part
= 16;
1399 cstate
->off_linkpl
.constant_part
= 18;
1401 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1404 case DLT_SYMANTEC_FIREWALL
:
1405 cstate
->off_linktype
.constant_part
= 6;
1406 cstate
->off_linkpl
.constant_part
= 44;
1407 cstate
->off_nl
= 0; /* Ethernet II */
1408 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1411 #ifdef HAVE_NET_PFVAR_H
1413 cstate
->off_linktype
.constant_part
= 0;
1414 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1416 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1420 case DLT_JUNIPER_MFR
:
1421 case DLT_JUNIPER_MLFR
:
1422 case DLT_JUNIPER_MLPPP
:
1423 case DLT_JUNIPER_PPP
:
1424 case DLT_JUNIPER_CHDLC
:
1425 case DLT_JUNIPER_FRELAY
:
1426 cstate
->off_linktype
.constant_part
= 4;
1427 cstate
->off_linkpl
.constant_part
= 4;
1429 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1432 case DLT_JUNIPER_ATM1
:
1433 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1434 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1436 cstate
->off_nl_nosnap
= 10;
1439 case DLT_JUNIPER_ATM2
:
1440 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1441 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1443 cstate
->off_nl_nosnap
= 10;
1446 /* frames captured on a Juniper PPPoE service PIC
1447 * contain raw ethernet frames */
1448 case DLT_JUNIPER_PPPOE
:
1449 case DLT_JUNIPER_ETHER
:
1450 cstate
->off_linkpl
.constant_part
= 14;
1451 cstate
->off_linktype
.constant_part
= 16;
1452 cstate
->off_nl
= 18; /* Ethernet II */
1453 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1456 case DLT_JUNIPER_PPPOE_ATM
:
1457 cstate
->off_linktype
.constant_part
= 4;
1458 cstate
->off_linkpl
.constant_part
= 6;
1460 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1463 case DLT_JUNIPER_GGSN
:
1464 cstate
->off_linktype
.constant_part
= 6;
1465 cstate
->off_linkpl
.constant_part
= 12;
1467 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1470 case DLT_JUNIPER_ES
:
1471 cstate
->off_linktype
.constant_part
= 6;
1472 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1473 cstate
->off_nl
= -1; /* not really a network layer but raw IP addresses */
1474 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1477 case DLT_JUNIPER_MONITOR
:
1478 cstate
->off_linktype
.constant_part
= 12;
1479 cstate
->off_linkpl
.constant_part
= 12;
1480 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1481 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1484 case DLT_BACNET_MS_TP
:
1485 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1486 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1487 cstate
->off_nl
= -1;
1488 cstate
->off_nl_nosnap
= -1;
1491 case DLT_JUNIPER_SERVICES
:
1492 cstate
->off_linktype
.constant_part
= 12;
1493 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1494 cstate
->off_nl
= -1; /* L3 proto location dep. on cookie type */
1495 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1498 case DLT_JUNIPER_VP
:
1499 cstate
->off_linktype
.constant_part
= 18;
1500 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1501 cstate
->off_nl
= -1;
1502 cstate
->off_nl_nosnap
= -1;
1505 case DLT_JUNIPER_ST
:
1506 cstate
->off_linktype
.constant_part
= 18;
1507 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1508 cstate
->off_nl
= -1;
1509 cstate
->off_nl_nosnap
= -1;
1512 case DLT_JUNIPER_ISM
:
1513 cstate
->off_linktype
.constant_part
= 8;
1514 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1515 cstate
->off_nl
= -1;
1516 cstate
->off_nl_nosnap
= -1;
1519 case DLT_JUNIPER_VS
:
1520 case DLT_JUNIPER_SRX_E2E
:
1521 case DLT_JUNIPER_FIBRECHANNEL
:
1522 case DLT_JUNIPER_ATM_CEMIC
:
1523 cstate
->off_linktype
.constant_part
= 8;
1524 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1525 cstate
->off_nl
= -1;
1526 cstate
->off_nl_nosnap
= -1;
1531 cstate
->off_li_hsl
= 4;
1532 cstate
->off_sio
= 3;
1533 cstate
->off_opc
= 4;
1534 cstate
->off_dpc
= 4;
1535 cstate
->off_sls
= 7;
1536 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1537 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1538 cstate
->off_nl
= -1;
1539 cstate
->off_nl_nosnap
= -1;
1542 case DLT_MTP2_WITH_PHDR
:
1544 cstate
->off_li_hsl
= 8;
1545 cstate
->off_sio
= 7;
1546 cstate
->off_opc
= 8;
1547 cstate
->off_dpc
= 8;
1548 cstate
->off_sls
= 11;
1549 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1550 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1551 cstate
->off_nl
= -1;
1552 cstate
->off_nl_nosnap
= -1;
1556 cstate
->off_li
= 22;
1557 cstate
->off_li_hsl
= 24;
1558 cstate
->off_sio
= 23;
1559 cstate
->off_opc
= 24;
1560 cstate
->off_dpc
= 24;
1561 cstate
->off_sls
= 27;
1562 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1563 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1564 cstate
->off_nl
= -1;
1565 cstate
->off_nl_nosnap
= -1;
1569 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1570 cstate
->off_linkpl
.constant_part
= 4;
1572 cstate
->off_nl_nosnap
= 0;
1577 * Currently, only raw "link[N:M]" filtering is supported.
1579 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1580 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1581 cstate
->off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1582 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1586 cstate
->off_linktype
.constant_part
= 1;
1587 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1589 cstate
->off_nl_nosnap
= -1;
1592 case DLT_NETANALYZER
:
1593 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1594 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1595 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1596 cstate
->off_nl
= 0; /* Ethernet II */
1597 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1600 case DLT_NETANALYZER_TRANSPARENT
:
1601 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1602 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1603 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1604 cstate
->off_nl
= 0; /* Ethernet II */
1605 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1610 * For values in the range in which we've assigned new
1611 * DLT_ values, only raw "link[N:M]" filtering is supported.
1613 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1614 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1615 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1616 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1617 cstate
->off_nl
= -1;
1618 cstate
->off_nl_nosnap
= -1;
1620 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1625 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1629 * Load a value relative to the specified absolute offset.
1631 static struct slist
*
1632 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1633 u_int offset
, u_int size
)
1635 struct slist
*s
, *s2
;
1637 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1640 * If "s" is non-null, it has code to arrange that the X register
1641 * contains the variable part of the absolute offset, so we
1642 * generate a load relative to that, with an offset of
1643 * abs_offset->constant_part + offset.
1645 * Otherwise, we can do an absolute load with an offset of
1646 * abs_offset->constant_part + offset.
1650 * "s" points to a list of statements that puts the
1651 * variable part of the absolute offset into the X register.
1652 * Do an indirect load, to use the X register as an offset.
1654 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1655 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1659 * There is no variable part of the absolute offset, so
1660 * just do an absolute load.
1662 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1663 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1669 * Load a value relative to the beginning of the specified header.
1671 static struct slist
*
1672 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1675 struct slist
*s
, *s2
;
1680 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1685 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1688 case OR_PREVLINKHDR
:
1689 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1693 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1696 case OR_PREVMPLSHDR
:
1697 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1701 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1704 case OR_LINKPL_NOSNAP
:
1705 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1709 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1714 * Load the X register with the length of the IPv4 header
1715 * (plus the offset of the link-layer header, if it's
1716 * preceded by a variable-length header such as a radio
1717 * header), in bytes.
1719 s
= gen_loadx_iphdrlen(cstate
);
1722 * Load the item at {offset of the link-layer payload} +
1723 * {offset, relative to the start of the link-layer
1724 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1725 * {specified offset}.
1727 * If the offset of the link-layer payload is variable,
1728 * the variable part of that offset is included in the
1729 * value in the X register, and we include the constant
1730 * part in the offset of the load.
1732 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1733 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1738 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1749 * Generate code to load into the X register the sum of the length of
1750 * the IPv4 header and the variable part of the offset of the link-layer
1753 static struct slist
*
1754 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1756 struct slist
*s
, *s2
;
1758 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1761 * The offset of the link-layer payload has a variable
1762 * part. "s" points to a list of statements that put
1763 * the variable part of that offset into the X register.
1765 * The 4*([k]&0xf) addressing mode can't be used, as we
1766 * don't have a constant offset, so we have to load the
1767 * value in question into the A register and add to it
1768 * the value from the X register.
1770 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1771 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1773 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1776 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1781 * The A register now contains the length of the IP header.
1782 * We need to add to it the variable part of the offset of
1783 * the link-layer payload, which is still in the X
1784 * register, and move the result into the X register.
1786 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1787 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1790 * The offset of the link-layer payload is a constant,
1791 * so no code was generated to load the (non-existent)
1792 * variable part of that offset.
1794 * This means we can use the 4*([k]&0xf) addressing
1795 * mode. Load the length of the IPv4 header, which
1796 * is at an offset of cstate->off_nl from the beginning of
1797 * the link-layer payload, and thus at an offset of
1798 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1799 * of the raw packet data, using that addressing mode.
1801 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1802 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1808 static struct block
*
1809 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1814 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1816 b
= new_block(cstate
, JMP(BPF_JEQ
));
1822 static inline struct block
*
1823 gen_true(compiler_state_t
*cstate
)
1825 return gen_uncond(cstate
, 1);
1828 static inline struct block
*
1829 gen_false(compiler_state_t
*cstate
)
1831 return gen_uncond(cstate
, 0);
1835 * Byte-swap a 32-bit number.
1836 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1837 * big-endian platforms.)
1839 #define SWAPLONG(y) \
1840 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1843 * Generate code to match a particular packet type.
1845 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1846 * value, if <= ETHERMTU. We use that to determine whether to
1847 * match the type/length field or to check the type/length field for
1848 * a value <= ETHERMTU to see whether it's a type field and then do
1849 * the appropriate test.
1851 static struct block
*
1852 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1854 struct block
*b0
, *b1
;
1860 case LLCSAP_NETBEUI
:
1862 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1863 * so we check the DSAP and SSAP.
1865 * LLCSAP_IP checks for IP-over-802.2, rather
1866 * than IP-over-Ethernet or IP-over-SNAP.
1868 * XXX - should we check both the DSAP and the
1869 * SSAP, like this, or should we check just the
1870 * DSAP, as we do for other types <= ETHERMTU
1871 * (i.e., other SAP values)?
1873 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1875 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1876 ((proto
<< 8) | proto
));
1884 * Ethernet_II frames, which are Ethernet
1885 * frames with a frame type of ETHERTYPE_IPX;
1887 * Ethernet_802.3 frames, which are 802.3
1888 * frames (i.e., the type/length field is
1889 * a length field, <= ETHERMTU, rather than
1890 * a type field) with the first two bytes
1891 * after the Ethernet/802.3 header being
1894 * Ethernet_802.2 frames, which are 802.3
1895 * frames with an 802.2 LLC header and
1896 * with the IPX LSAP as the DSAP in the LLC
1899 * Ethernet_SNAP frames, which are 802.3
1900 * frames with an LLC header and a SNAP
1901 * header and with an OUI of 0x000000
1902 * (encapsulated Ethernet) and a protocol
1903 * ID of ETHERTYPE_IPX in the SNAP header.
1905 * XXX - should we generate the same code both
1906 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1910 * This generates code to check both for the
1911 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1913 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1914 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1918 * Now we add code to check for SNAP frames with
1919 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1921 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1925 * Now we generate code to check for 802.3
1926 * frames in general.
1928 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1932 * Now add the check for 802.3 frames before the
1933 * check for Ethernet_802.2 and Ethernet_802.3,
1934 * as those checks should only be done on 802.3
1935 * frames, not on Ethernet frames.
1940 * Now add the check for Ethernet_II frames, and
1941 * do that before checking for the other frame
1944 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1948 case ETHERTYPE_ATALK
:
1949 case ETHERTYPE_AARP
:
1951 * EtherTalk (AppleTalk protocols on Ethernet link
1952 * layer) may use 802.2 encapsulation.
1956 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1957 * we check for an Ethernet type field less than
1958 * 1500, which means it's an 802.3 length field.
1960 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1964 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1965 * SNAP packets with an organization code of
1966 * 0x080007 (Apple, for Appletalk) and a protocol
1967 * type of ETHERTYPE_ATALK (Appletalk).
1969 * 802.2-encapsulated ETHERTYPE_AARP packets are
1970 * SNAP packets with an organization code of
1971 * 0x000000 (encapsulated Ethernet) and a protocol
1972 * type of ETHERTYPE_AARP (Appletalk ARP).
1974 if (proto
== ETHERTYPE_ATALK
)
1975 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
1976 else /* proto == ETHERTYPE_AARP */
1977 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
1981 * Check for Ethernet encapsulation (Ethertalk
1982 * phase 1?); we just check for the Ethernet
1985 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1991 if (proto
<= ETHERMTU
) {
1993 * This is an LLC SAP value, so the frames
1994 * that match would be 802.2 frames.
1995 * Check that the frame is an 802.2 frame
1996 * (i.e., that the length/type field is
1997 * a length field, <= ETHERMTU) and
1998 * then check the DSAP.
2000 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2002 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2007 * This is an Ethernet type, so compare
2008 * the length/type field with it (if
2009 * the frame is an 802.2 frame, the length
2010 * field will be <= ETHERMTU, and, as
2011 * "proto" is > ETHERMTU, this test
2012 * will fail and the frame won't match,
2013 * which is what we want).
2015 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2021 static struct block
*
2022 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2025 * For DLT_NULL, the link-layer header is a 32-bit word
2026 * containing an AF_ value in *host* byte order, and for
2027 * DLT_ENC, the link-layer header begins with a 32-bit
2028 * word containing an AF_ value in host byte order.
2030 * In addition, if we're reading a saved capture file,
2031 * the host byte order in the capture may not be the
2032 * same as the host byte order on this machine.
2034 * For DLT_LOOP, the link-layer header is a 32-bit
2035 * word containing an AF_ value in *network* byte order.
2037 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2039 * The AF_ value is in host byte order, but the BPF
2040 * interpreter will convert it to network byte order.
2042 * If this is a save file, and it's from a machine
2043 * with the opposite byte order to ours, we byte-swap
2046 * Then we run it through "htonl()", and generate
2047 * code to compare against the result.
2049 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2050 proto
= SWAPLONG(proto
);
2051 proto
= htonl(proto
);
2053 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2057 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2058 * or IPv6 then we have an error.
2060 static struct block
*
2061 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2066 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2069 case ETHERTYPE_IPV6
:
2070 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2071 (bpf_int32
)IPH_AF_INET6
);
2078 return gen_false(cstate
);
2082 * Generate code to match a particular packet type.
2084 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2085 * value, if <= ETHERMTU. We use that to determine whether to
2086 * match the type field or to check the type field for the special
2087 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2089 static struct block
*
2090 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2092 struct block
*b0
, *b1
;
2098 case LLCSAP_NETBEUI
:
2100 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2101 * so we check the DSAP and SSAP.
2103 * LLCSAP_IP checks for IP-over-802.2, rather
2104 * than IP-over-Ethernet or IP-over-SNAP.
2106 * XXX - should we check both the DSAP and the
2107 * SSAP, like this, or should we check just the
2108 * DSAP, as we do for other types <= ETHERMTU
2109 * (i.e., other SAP values)?
2111 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2112 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2113 ((proto
<< 8) | proto
));
2119 * Ethernet_II frames, which are Ethernet
2120 * frames with a frame type of ETHERTYPE_IPX;
2122 * Ethernet_802.3 frames, which have a frame
2123 * type of LINUX_SLL_P_802_3;
2125 * Ethernet_802.2 frames, which are 802.3
2126 * frames with an 802.2 LLC header (i.e, have
2127 * a frame type of LINUX_SLL_P_802_2) and
2128 * with the IPX LSAP as the DSAP in the LLC
2131 * Ethernet_SNAP frames, which are 802.3
2132 * frames with an LLC header and a SNAP
2133 * header and with an OUI of 0x000000
2134 * (encapsulated Ethernet) and a protocol
2135 * ID of ETHERTYPE_IPX in the SNAP header.
2137 * First, do the checks on LINUX_SLL_P_802_2
2138 * frames; generate the check for either
2139 * Ethernet_802.2 or Ethernet_SNAP frames, and
2140 * then put a check for LINUX_SLL_P_802_2 frames
2143 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2144 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2146 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2150 * Now check for 802.3 frames and OR that with
2151 * the previous test.
2153 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2157 * Now add the check for Ethernet_II frames, and
2158 * do that before checking for the other frame
2161 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2165 case ETHERTYPE_ATALK
:
2166 case ETHERTYPE_AARP
:
2168 * EtherTalk (AppleTalk protocols on Ethernet link
2169 * layer) may use 802.2 encapsulation.
2173 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2174 * we check for the 802.2 protocol type in the
2175 * "Ethernet type" field.
2177 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2180 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2181 * SNAP packets with an organization code of
2182 * 0x080007 (Apple, for Appletalk) and a protocol
2183 * type of ETHERTYPE_ATALK (Appletalk).
2185 * 802.2-encapsulated ETHERTYPE_AARP packets are
2186 * SNAP packets with an organization code of
2187 * 0x000000 (encapsulated Ethernet) and a protocol
2188 * type of ETHERTYPE_AARP (Appletalk ARP).
2190 if (proto
== ETHERTYPE_ATALK
)
2191 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2192 else /* proto == ETHERTYPE_AARP */
2193 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2197 * Check for Ethernet encapsulation (Ethertalk
2198 * phase 1?); we just check for the Ethernet
2201 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2207 if (proto
<= ETHERMTU
) {
2209 * This is an LLC SAP value, so the frames
2210 * that match would be 802.2 frames.
2211 * Check for the 802.2 protocol type
2212 * in the "Ethernet type" field, and
2213 * then check the DSAP.
2215 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2216 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2222 * This is an Ethernet type, so compare
2223 * the length/type field with it (if
2224 * the frame is an 802.2 frame, the length
2225 * field will be <= ETHERMTU, and, as
2226 * "proto" is > ETHERMTU, this test
2227 * will fail and the frame won't match,
2228 * which is what we want).
2230 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2235 static struct slist
*
2236 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2238 struct slist
*s1
, *s2
;
2239 struct slist
*sjeq_avs_cookie
;
2240 struct slist
*sjcommon
;
2243 * This code is not compatible with the optimizer, as
2244 * we are generating jmp instructions within a normal
2245 * slist of instructions
2247 cstate
->no_optimize
= 1;
2250 * Generate code to load the length of the radio header into
2251 * the register assigned to hold that length, if one has been
2252 * assigned. (If one hasn't been assigned, no code we've
2253 * generated uses that prefix, so we don't need to generate any
2256 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2257 * or always use the AVS header rather than the Prism header.
2258 * We load a 4-byte big-endian value at the beginning of the
2259 * raw packet data, and see whether, when masked with 0xFFFFF000,
2260 * it's equal to 0x80211000. If so, that indicates that it's
2261 * an AVS header (the masked-out bits are the version number).
2262 * Otherwise, it's a Prism header.
2264 * XXX - the Prism header is also, in theory, variable-length,
2265 * but no known software generates headers that aren't 144
2268 if (cstate
->off_linkhdr
.reg
!= -1) {
2272 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2276 * AND it with 0xFFFFF000.
2278 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2279 s2
->s
.k
= 0xFFFFF000;
2283 * Compare with 0x80211000.
2285 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2286 sjeq_avs_cookie
->s
.k
= 0x80211000;
2287 sappend(s1
, sjeq_avs_cookie
);
2292 * The 4 bytes at an offset of 4 from the beginning of
2293 * the AVS header are the length of the AVS header.
2294 * That field is big-endian.
2296 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2299 sjeq_avs_cookie
->s
.jt
= s2
;
2302 * Now jump to the code to allocate a register
2303 * into which to save the header length and
2304 * store the length there. (The "jump always"
2305 * instruction needs to have the k field set;
2306 * it's added to the PC, so, as we're jumping
2307 * over a single instruction, it should be 1.)
2309 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2311 sappend(s1
, sjcommon
);
2314 * Now for the code that handles the Prism header.
2315 * Just load the length of the Prism header (144)
2316 * into the A register. Have the test for an AVS
2317 * header branch here if we don't have an AVS header.
2319 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2322 sjeq_avs_cookie
->s
.jf
= s2
;
2325 * Now allocate a register to hold that value and store
2326 * it. The code for the AVS header will jump here after
2327 * loading the length of the AVS header.
2329 s2
= new_stmt(cstate
, BPF_ST
);
2330 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2332 sjcommon
->s
.jf
= s2
;
2335 * Now move it into the X register.
2337 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2345 static struct slist
*
2346 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2348 struct slist
*s1
, *s2
;
2351 * Generate code to load the length of the AVS header into
2352 * the register assigned to hold that length, if one has been
2353 * assigned. (If one hasn't been assigned, no code we've
2354 * generated uses that prefix, so we don't need to generate any
2357 if (cstate
->off_linkhdr
.reg
!= -1) {
2359 * The 4 bytes at an offset of 4 from the beginning of
2360 * the AVS header are the length of the AVS header.
2361 * That field is big-endian.
2363 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2367 * Now allocate a register to hold that value and store
2370 s2
= new_stmt(cstate
, BPF_ST
);
2371 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2375 * Now move it into the X register.
2377 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2385 static struct slist
*
2386 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2388 struct slist
*s1
, *s2
;
2391 * Generate code to load the length of the radiotap header into
2392 * the register assigned to hold that length, if one has been
2393 * assigned. (If one hasn't been assigned, no code we've
2394 * generated uses that prefix, so we don't need to generate any
2397 if (cstate
->off_linkhdr
.reg
!= -1) {
2399 * The 2 bytes at offsets of 2 and 3 from the beginning
2400 * of the radiotap header are the length of the radiotap
2401 * header; unfortunately, it's little-endian, so we have
2402 * to load it a byte at a time and construct the value.
2406 * Load the high-order byte, at an offset of 3, shift it
2407 * left a byte, and put the result in the X register.
2409 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2411 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2414 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2418 * Load the next byte, at an offset of 2, and OR the
2419 * value from the X register into it.
2421 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2424 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2428 * Now allocate a register to hold that value and store
2431 s2
= new_stmt(cstate
, BPF_ST
);
2432 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2436 * Now move it into the X register.
2438 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2447 * At the moment we treat PPI as normal Radiotap encoded
2448 * packets. The difference is in the function that generates
2449 * the code at the beginning to compute the header length.
2450 * Since this code generator of PPI supports bare 802.11
2451 * encapsulation only (i.e. the encapsulated DLT should be
2452 * DLT_IEEE802_11) we generate code to check for this too;
2453 * that's done in finish_parse().
2455 static struct slist
*
2456 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2458 struct slist
*s1
, *s2
;
2461 * Generate code to load the length of the radiotap header
2462 * into the register assigned to hold that length, if one has
2465 if (cstate
->off_linkhdr
.reg
!= -1) {
2467 * The 2 bytes at offsets of 2 and 3 from the beginning
2468 * of the radiotap header are the length of the radiotap
2469 * header; unfortunately, it's little-endian, so we have
2470 * to load it a byte at a time and construct the value.
2474 * Load the high-order byte, at an offset of 3, shift it
2475 * left a byte, and put the result in the X register.
2477 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2479 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2482 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2486 * Load the next byte, at an offset of 2, and OR the
2487 * value from the X register into it.
2489 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2492 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2496 * Now allocate a register to hold that value and store
2499 s2
= new_stmt(cstate
, BPF_ST
);
2500 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2504 * Now move it into the X register.
2506 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2515 * Load a value relative to the beginning of the link-layer header after the 802.11
2516 * header, i.e. LLC_SNAP.
2517 * The link-layer header doesn't necessarily begin at the beginning
2518 * of the packet data; there might be a variable-length prefix containing
2519 * radio information.
2521 static struct slist
*
2522 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2525 struct slist
*sjset_data_frame_1
;
2526 struct slist
*sjset_data_frame_2
;
2527 struct slist
*sjset_qos
;
2528 struct slist
*sjset_radiotap_flags_present
;
2529 struct slist
*sjset_radiotap_ext_present
;
2530 struct slist
*sjset_radiotap_tsft_present
;
2531 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2532 struct slist
*s_roundup
;
2534 if (cstate
->off_linkpl
.reg
== -1) {
2536 * No register has been assigned to the offset of
2537 * the link-layer payload, which means nobody needs
2538 * it; don't bother computing it - just return
2539 * what we already have.
2545 * This code is not compatible with the optimizer, as
2546 * we are generating jmp instructions within a normal
2547 * slist of instructions
2549 cstate
->no_optimize
= 1;
2552 * If "s" is non-null, it has code to arrange that the X register
2553 * contains the length of the prefix preceding the link-layer
2556 * Otherwise, the length of the prefix preceding the link-layer
2557 * header is "off_outermostlinkhdr.constant_part".
2561 * There is no variable-length header preceding the
2562 * link-layer header.
2564 * Load the length of the fixed-length prefix preceding
2565 * the link-layer header (if any) into the X register,
2566 * and store it in the cstate->off_linkpl.reg register.
2567 * That length is off_outermostlinkhdr.constant_part.
2569 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2570 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2574 * The X register contains the offset of the beginning of the
2575 * link-layer header; add 24, which is the minimum length
2576 * of the MAC header for a data frame, to that, and store it
2577 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2578 * which is at the offset in the X register, with an indexed load.
2580 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2582 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2585 s2
= new_stmt(cstate
, BPF_ST
);
2586 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2589 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2594 * Check the Frame Control field to see if this is a data frame;
2595 * a data frame has the 0x08 bit (b3) in that field set and the
2596 * 0x04 bit (b2) clear.
2598 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2599 sjset_data_frame_1
->s
.k
= 0x08;
2600 sappend(s
, sjset_data_frame_1
);
2603 * If b3 is set, test b2, otherwise go to the first statement of
2604 * the rest of the program.
2606 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2607 sjset_data_frame_2
->s
.k
= 0x04;
2608 sappend(s
, sjset_data_frame_2
);
2609 sjset_data_frame_1
->s
.jf
= snext
;
2612 * If b2 is not set, this is a data frame; test the QoS bit.
2613 * Otherwise, go to the first statement of the rest of the
2616 sjset_data_frame_2
->s
.jt
= snext
;
2617 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2618 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2619 sappend(s
, sjset_qos
);
2622 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2624 * Otherwise, go to the first statement of the rest of the
2627 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2628 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2630 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2633 s2
= new_stmt(cstate
, BPF_ST
);
2634 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2638 * If we have a radiotap header, look at it to see whether
2639 * there's Atheros padding between the MAC-layer header
2642 * Note: all of the fields in the radiotap header are
2643 * little-endian, so we byte-swap all of the values
2644 * we test against, as they will be loaded as big-endian
2647 * XXX - in the general case, we would have to scan through
2648 * *all* the presence bits, if there's more than one word of
2649 * presence bits. That would require a loop, meaning that
2650 * we wouldn't be able to run the filter in the kernel.
2652 * We assume here that the Atheros adapters that insert the
2653 * annoying padding don't have multiple antennae and therefore
2654 * do not generate radiotap headers with multiple presence words.
2656 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2658 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2659 * in the first presence flag word?
2661 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2665 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2666 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2667 sappend(s
, sjset_radiotap_flags_present
);
2670 * If not, skip all of this.
2672 sjset_radiotap_flags_present
->s
.jf
= snext
;
2675 * Otherwise, is the "extension" bit set in that word?
2677 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2678 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2679 sappend(s
, sjset_radiotap_ext_present
);
2680 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2683 * If so, skip all of this.
2685 sjset_radiotap_ext_present
->s
.jt
= snext
;
2688 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2690 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2691 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2692 sappend(s
, sjset_radiotap_tsft_present
);
2693 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2696 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2697 * at an offset of 16 from the beginning of the raw packet
2698 * data (8 bytes for the radiotap header and 8 bytes for
2701 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2704 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2707 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2709 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2710 sjset_tsft_datapad
->s
.k
= 0x20;
2711 sappend(s
, sjset_tsft_datapad
);
2714 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2715 * at an offset of 8 from the beginning of the raw packet
2716 * data (8 bytes for the radiotap header).
2718 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2721 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2724 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2726 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2727 sjset_notsft_datapad
->s
.k
= 0x20;
2728 sappend(s
, sjset_notsft_datapad
);
2731 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2732 * set, round the length of the 802.11 header to
2733 * a multiple of 4. Do that by adding 3 and then
2734 * dividing by and multiplying by 4, which we do by
2737 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2738 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2739 sappend(s
, s_roundup
);
2740 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2743 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2746 s2
= new_stmt(cstate
, BPF_ST
);
2747 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2750 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2751 sjset_tsft_datapad
->s
.jf
= snext
;
2752 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2753 sjset_notsft_datapad
->s
.jf
= snext
;
2755 sjset_qos
->s
.jf
= snext
;
2761 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2765 /* There is an implicit dependency between the link
2766 * payload and link header since the payload computation
2767 * includes the variable part of the header. Therefore,
2768 * if nobody else has allocated a register for the link
2769 * header and we need it, do it now. */
2770 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2771 cstate
->off_linkhdr
.reg
== -1)
2772 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2775 * For link-layer types that have a variable-length header
2776 * preceding the link-layer header, generate code to load
2777 * the offset of the link-layer header into the register
2778 * assigned to that offset, if any.
2780 * XXX - this, and the next switch statement, won't handle
2781 * encapsulation of 802.11 or 802.11+radio information in
2782 * some other protocol stack. That's significantly more
2785 switch (cstate
->outermostlinktype
) {
2787 case DLT_PRISM_HEADER
:
2788 s
= gen_load_prism_llprefixlen(cstate
);
2791 case DLT_IEEE802_11_RADIO_AVS
:
2792 s
= gen_load_avs_llprefixlen(cstate
);
2795 case DLT_IEEE802_11_RADIO
:
2796 s
= gen_load_radiotap_llprefixlen(cstate
);
2800 s
= gen_load_ppi_llprefixlen(cstate
);
2809 * For link-layer types that have a variable-length link-layer
2810 * header, generate code to load the offset of the link-layer
2811 * payload into the register assigned to that offset, if any.
2813 switch (cstate
->outermostlinktype
) {
2815 case DLT_IEEE802_11
:
2816 case DLT_PRISM_HEADER
:
2817 case DLT_IEEE802_11_RADIO_AVS
:
2818 case DLT_IEEE802_11_RADIO
:
2820 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2825 * If there there is no initialization yet and we need variable
2826 * length offsets for VLAN, initialize them to zero
2828 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2831 if (cstate
->off_linkpl
.reg
== -1)
2832 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2833 if (cstate
->off_linktype
.reg
== -1)
2834 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2836 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2838 s2
= new_stmt(cstate
, BPF_ST
);
2839 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2841 s2
= new_stmt(cstate
, BPF_ST
);
2842 s2
->s
.k
= cstate
->off_linktype
.reg
;
2847 * If we have any offset-loading code, append all the
2848 * existing statements in the block to those statements,
2849 * and make the resulting list the list of statements
2853 sappend(s
, b
->stmts
);
2858 static struct block
*
2859 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2861 struct slist
*s_load_dlt
;
2864 if (cstate
->linktype
== DLT_PPI
)
2866 /* Create the statements that check for the DLT
2868 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2869 s_load_dlt
->s
.k
= 4;
2871 b
= new_block(cstate
, JMP(BPF_JEQ
));
2873 b
->stmts
= s_load_dlt
;
2874 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2885 * Take an absolute offset, and:
2887 * if it has no variable part, return NULL;
2889 * if it has a variable part, generate code to load the register
2890 * containing that variable part into the X register, returning
2891 * a pointer to that code - if no register for that offset has
2892 * been allocated, allocate it first.
2894 * (The code to set that register will be generated later, but will
2895 * be placed earlier in the code sequence.)
2897 static struct slist
*
2898 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2902 if (off
->is_variable
) {
2903 if (off
->reg
== -1) {
2905 * We haven't yet assigned a register for the
2906 * variable part of the offset of the link-layer
2907 * header; allocate one.
2909 off
->reg
= alloc_reg(cstate
);
2913 * Load the register containing the variable part of the
2914 * offset of the link-layer header into the X register.
2916 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2921 * That offset isn't variable, there's no variable part,
2922 * so we don't need to generate any code.
2929 * Map an Ethernet type to the equivalent PPP type.
2932 ethertype_to_ppptype(proto
)
2941 case ETHERTYPE_IPV6
:
2949 case ETHERTYPE_ATALK
:
2963 * I'm assuming the "Bridging PDU"s that go
2964 * over PPP are Spanning Tree Protocol
2978 * Generate any tests that, for encapsulation of a link-layer packet
2979 * inside another protocol stack, need to be done to check for those
2980 * link-layer packets (and that haven't already been done by a check
2981 * for that encapsulation).
2983 static struct block
*
2984 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
2988 if (cstate
->is_geneve
)
2989 return gen_geneve_ll_check(cstate
);
2991 switch (cstate
->prevlinktype
) {
2995 * This is LANE-encapsulated Ethernet; check that the LANE
2996 * packet doesn't begin with an LE Control marker, i.e.
2997 * that it's data, not a control message.
2999 * (We've already generated a test for LANE.)
3001 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3007 * No such tests are necessary.
3015 * The three different values we should check for when checking for an
3016 * IPv6 packet with DLT_NULL.
3018 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3019 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3020 #define BSD_AFNUM_INET6_DARWIN 30 /* OS X, iOS, other Darwin-based OSes */
3023 * Generate code to match a particular packet type by matching the
3024 * link-layer type field or fields in the 802.2 LLC header.
3026 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3027 * value, if <= ETHERMTU.
3029 static struct block
*
3030 gen_linktype(compiler_state_t
*cstate
, int proto
)
3032 struct block
*b0
, *b1
, *b2
;
3033 const char *description
;
3035 /* are we checking MPLS-encapsulated packets? */
3036 if (cstate
->label_stack_depth
> 0) {
3040 /* FIXME add other L3 proto IDs */
3041 return gen_mpls_linktype(cstate
, Q_IP
);
3043 case ETHERTYPE_IPV6
:
3045 /* FIXME add other L3 proto IDs */
3046 return gen_mpls_linktype(cstate
, Q_IPV6
);
3049 bpf_error(cstate
, "unsupported protocol over mpls");
3054 switch (cstate
->linktype
) {
3057 case DLT_NETANALYZER
:
3058 case DLT_NETANALYZER_TRANSPARENT
:
3059 /* Geneve has an EtherType regardless of whether there is an
3061 if (!cstate
->is_geneve
)
3062 b0
= gen_prevlinkhdr_check(cstate
);
3066 b1
= gen_ether_linktype(cstate
, proto
);
3077 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3081 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3087 case DLT_IEEE802_11
:
3088 case DLT_PRISM_HEADER
:
3089 case DLT_IEEE802_11_RADIO_AVS
:
3090 case DLT_IEEE802_11_RADIO
:
3093 * Check that we have a data frame.
3095 b0
= gen_check_802_11_data_frame(cstate
);
3098 * Now check for the specified link-layer type.
3100 b1
= gen_llc_linktype(cstate
, proto
);
3108 * XXX - check for LLC frames.
3110 return gen_llc_linktype(cstate
, proto
);
3116 * XXX - check for LLC PDUs, as per IEEE 802.5.
3118 return gen_llc_linktype(cstate
, proto
);
3122 case DLT_ATM_RFC1483
:
3124 case DLT_IP_OVER_FC
:
3125 return gen_llc_linktype(cstate
, proto
);
3131 * Check for an LLC-encapsulated version of this protocol;
3132 * if we were checking for LANE, linktype would no longer
3135 * Check for LLC encapsulation and then check the protocol.
3137 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3138 b1
= gen_llc_linktype(cstate
, proto
);
3145 return gen_linux_sll_linktype(cstate
, proto
);
3150 case DLT_SLIP_BSDOS
:
3153 * These types don't provide any type field; packets
3154 * are always IPv4 or IPv6.
3156 * XXX - for IPv4, check for a version number of 4, and,
3157 * for IPv6, check for a version number of 6?
3162 /* Check for a version number of 4. */
3163 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3165 case ETHERTYPE_IPV6
:
3166 /* Check for a version number of 6. */
3167 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3170 return gen_false(cstate
); /* always false */
3177 * Raw IPv4, so no type field.
3179 if (proto
== ETHERTYPE_IP
)
3180 return gen_true(cstate
); /* always true */
3182 /* Checking for something other than IPv4; always false */
3183 return gen_false(cstate
);
3189 * Raw IPv6, so no type field.
3191 if (proto
== ETHERTYPE_IPV6
)
3192 return gen_true(cstate
); /* always true */
3194 /* Checking for something other than IPv6; always false */
3195 return gen_false(cstate
);
3201 case DLT_PPP_SERIAL
:
3204 * We use Ethernet protocol types inside libpcap;
3205 * map them to the corresponding PPP protocol types.
3207 proto
= ethertype_to_ppptype(proto
);
3208 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3214 * We use Ethernet protocol types inside libpcap;
3215 * map them to the corresponding PPP protocol types.
3221 * Also check for Van Jacobson-compressed IP.
3222 * XXX - do this for other forms of PPP?
3224 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3225 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3227 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3232 proto
= ethertype_to_ppptype(proto
);
3233 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3245 return (gen_loopback_linktype(cstate
, AF_INET
));
3247 case ETHERTYPE_IPV6
:
3249 * AF_ values may, unfortunately, be platform-
3250 * dependent; AF_INET isn't, because everybody
3251 * used 4.2BSD's value, but AF_INET6 is, because
3252 * 4.2BSD didn't have a value for it (given that
3253 * IPv6 didn't exist back in the early 1980's),
3254 * and they all picked their own values.
3256 * This means that, if we're reading from a
3257 * savefile, we need to check for all the
3260 * If we're doing a live capture, we only need
3261 * to check for this platform's value; however,
3262 * Npcap uses 24, which isn't Windows's AF_INET6
3263 * value. (Given the multiple different values,
3264 * programs that read pcap files shouldn't be
3265 * checking for their platform's AF_INET6 value
3266 * anyway, they should check for all of the
3267 * possible values. and they might as well do
3268 * that even for live captures.)
3270 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3272 * Savefile - check for all three
3273 * possible IPv6 values.
3275 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3276 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3278 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3283 * Live capture, so we only need to
3284 * check for the value used on this
3289 * Npcap doesn't use Windows's AF_INET6,
3290 * as that collides with AF_IPX on
3291 * some BSDs (both have the value 23).
3292 * Instead, it uses 24.
3294 return (gen_loopback_linktype(cstate
, 24));
3297 return (gen_loopback_linktype(cstate
, AF_INET6
));
3298 #else /* AF_INET6 */
3300 * I guess this platform doesn't support
3301 * IPv6, so we just reject all packets.
3303 return gen_false(cstate
);
3304 #endif /* AF_INET6 */
3310 * Not a type on which we support filtering.
3311 * XXX - support those that have AF_ values
3312 * #defined on this platform, at least?
3314 return gen_false(cstate
);
3317 #ifdef HAVE_NET_PFVAR_H
3320 * af field is host byte order in contrast to the rest of
3323 if (proto
== ETHERTYPE_IP
)
3324 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3325 BPF_B
, (bpf_int32
)AF_INET
));
3326 else if (proto
== ETHERTYPE_IPV6
)
3327 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3328 BPF_B
, (bpf_int32
)AF_INET6
));
3330 return gen_false(cstate
);
3333 #endif /* HAVE_NET_PFVAR_H */
3336 case DLT_ARCNET_LINUX
:
3338 * XXX should we check for first fragment if the protocol
3344 return gen_false(cstate
);
3346 case ETHERTYPE_IPV6
:
3347 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3348 (bpf_int32
)ARCTYPE_INET6
));
3351 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3352 (bpf_int32
)ARCTYPE_IP
);
3353 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3354 (bpf_int32
)ARCTYPE_IP_OLD
);
3359 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3360 (bpf_int32
)ARCTYPE_ARP
);
3361 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3362 (bpf_int32
)ARCTYPE_ARP_OLD
);
3366 case ETHERTYPE_REVARP
:
3367 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3368 (bpf_int32
)ARCTYPE_REVARP
));
3370 case ETHERTYPE_ATALK
:
3371 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3372 (bpf_int32
)ARCTYPE_ATALK
));
3379 case ETHERTYPE_ATALK
:
3380 return gen_true(cstate
);
3382 return gen_false(cstate
);
3389 * XXX - assumes a 2-byte Frame Relay header with
3390 * DLCI and flags. What if the address is longer?
3396 * Check for the special NLPID for IP.
3398 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3400 case ETHERTYPE_IPV6
:
3402 * Check for the special NLPID for IPv6.
3404 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3408 * Check for several OSI protocols.
3410 * Frame Relay packets typically have an OSI
3411 * NLPID at the beginning; we check for each
3414 * What we check for is the NLPID and a frame
3415 * control field of UI, i.e. 0x03 followed
3418 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3419 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3420 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3426 return gen_false(cstate
);
3432 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3434 case DLT_JUNIPER_MFR
:
3435 case DLT_JUNIPER_MLFR
:
3436 case DLT_JUNIPER_MLPPP
:
3437 case DLT_JUNIPER_ATM1
:
3438 case DLT_JUNIPER_ATM2
:
3439 case DLT_JUNIPER_PPPOE
:
3440 case DLT_JUNIPER_PPPOE_ATM
:
3441 case DLT_JUNIPER_GGSN
:
3442 case DLT_JUNIPER_ES
:
3443 case DLT_JUNIPER_MONITOR
:
3444 case DLT_JUNIPER_SERVICES
:
3445 case DLT_JUNIPER_ETHER
:
3446 case DLT_JUNIPER_PPP
:
3447 case DLT_JUNIPER_FRELAY
:
3448 case DLT_JUNIPER_CHDLC
:
3449 case DLT_JUNIPER_VP
:
3450 case DLT_JUNIPER_ST
:
3451 case DLT_JUNIPER_ISM
:
3452 case DLT_JUNIPER_VS
:
3453 case DLT_JUNIPER_SRX_E2E
:
3454 case DLT_JUNIPER_FIBRECHANNEL
:
3455 case DLT_JUNIPER_ATM_CEMIC
:
3457 /* just lets verify the magic number for now -
3458 * on ATM we may have up to 6 different encapsulations on the wire
3459 * and need a lot of heuristics to figure out that the payload
3462 * FIXME encapsulation specific BPF_ filters
3464 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3466 case DLT_BACNET_MS_TP
:
3467 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3470 return gen_ipnet_linktype(cstate
, proto
);
3472 case DLT_LINUX_IRDA
:
3473 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3476 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3479 case DLT_MTP2_WITH_PHDR
:
3480 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3483 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3486 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3488 case DLT_LINUX_LAPD
:
3489 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3491 case DLT_USB_FREEBSD
:
3493 case DLT_USB_LINUX_MMAPPED
:
3495 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3497 case DLT_BLUETOOTH_HCI_H4
:
3498 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3499 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3502 case DLT_CAN_SOCKETCAN
:
3503 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3505 case DLT_IEEE802_15_4
:
3506 case DLT_IEEE802_15_4_LINUX
:
3507 case DLT_IEEE802_15_4_NONASK_PHY
:
3508 case DLT_IEEE802_15_4_NOFCS
:
3509 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3511 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3512 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3515 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3518 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3521 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3524 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3527 /* Using the fixed-size NFLOG header it is possible to tell only
3528 * the address family of the packet, other meaningful data is
3529 * either missing or behind TLVs.
3531 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3535 * Does this link-layer header type have a field
3536 * indicating the type of the next protocol? If
3537 * so, off_linktype.constant_part will be the offset of that
3538 * field in the packet; if not, it will be OFFSET_NOT_SET.
3540 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3542 * Yes; assume it's an Ethernet type. (If
3543 * it's not, it needs to be handled specially
3546 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3549 * No; report an error.
3551 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3552 if (description
!= NULL
) {
3553 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3556 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3565 * Check for an LLC SNAP packet with a given organization code and
3566 * protocol type; we check the entire contents of the 802.2 LLC and
3567 * snap headers, checking for DSAP and SSAP of SNAP and a control
3568 * field of 0x03 in the LLC header, and for the specified organization
3569 * code and protocol type in the SNAP header.
3571 static struct block
*
3572 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3574 u_char snapblock
[8];
3576 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3577 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3578 snapblock
[2] = 0x03; /* control = UI */
3579 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3580 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3581 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3582 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3583 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3584 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3588 * Generate code to match frames with an LLC header.
3591 gen_llc(compiler_state_t
*cstate
)
3593 struct block
*b0
, *b1
;
3595 switch (cstate
->linktype
) {
3599 * We check for an Ethernet type field less than
3600 * 1500, which means it's an 802.3 length field.
3602 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3606 * Now check for the purported DSAP and SSAP not being
3607 * 0xFF, to rule out NetWare-over-802.3.
3609 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3616 * We check for LLC traffic.
3618 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3621 case DLT_IEEE802
: /* Token Ring */
3623 * XXX - check for LLC frames.
3625 return gen_true(cstate
);
3629 * XXX - check for LLC frames.
3631 return gen_true(cstate
);
3633 case DLT_ATM_RFC1483
:
3635 * For LLC encapsulation, these are defined to have an
3638 * For VC encapsulation, they don't, but there's no
3639 * way to check for that; the protocol used on the VC
3640 * is negotiated out of band.
3642 return gen_true(cstate
);
3644 case DLT_IEEE802_11
:
3645 case DLT_PRISM_HEADER
:
3646 case DLT_IEEE802_11_RADIO
:
3647 case DLT_IEEE802_11_RADIO_AVS
:
3650 * Check that we have a data frame.
3652 b0
= gen_check_802_11_data_frame(cstate
);
3656 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3662 gen_llc_i(compiler_state_t
*cstate
)
3664 struct block
*b0
, *b1
;
3668 * Check whether this is an LLC frame.
3670 b0
= gen_llc(cstate
);
3673 * Load the control byte and test the low-order bit; it must
3674 * be clear for I frames.
3676 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3677 b1
= new_block(cstate
, JMP(BPF_JSET
));
3686 gen_llc_s(compiler_state_t
*cstate
)
3688 struct block
*b0
, *b1
;
3691 * Check whether this is an LLC frame.
3693 b0
= gen_llc(cstate
);
3696 * Now compare the low-order 2 bit of the control byte against
3697 * the appropriate value for S frames.
3699 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3705 gen_llc_u(compiler_state_t
*cstate
)
3707 struct block
*b0
, *b1
;
3710 * Check whether this is an LLC frame.
3712 b0
= gen_llc(cstate
);
3715 * Now compare the low-order 2 bit of the control byte against
3716 * the appropriate value for U frames.
3718 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3724 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3726 struct block
*b0
, *b1
;
3729 * Check whether this is an LLC frame.
3731 b0
= gen_llc(cstate
);
3734 * Now check for an S frame with the appropriate type.
3736 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3742 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3744 struct block
*b0
, *b1
;
3747 * Check whether this is an LLC frame.
3749 b0
= gen_llc(cstate
);
3752 * Now check for a U frame with the appropriate type.
3754 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3760 * Generate code to match a particular packet type, for link-layer types
3761 * using 802.2 LLC headers.
3763 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3764 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3766 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3767 * value, if <= ETHERMTU. We use that to determine whether to
3768 * match the DSAP or both DSAP and LSAP or to check the OUI and
3769 * protocol ID in a SNAP header.
3771 static struct block
*
3772 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3775 * XXX - handle token-ring variable-length header.
3781 case LLCSAP_NETBEUI
:
3783 * XXX - should we check both the DSAP and the
3784 * SSAP, like this, or should we check just the
3785 * DSAP, as we do for other SAP values?
3787 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3788 ((proto
<< 8) | proto
));
3792 * XXX - are there ever SNAP frames for IPX on
3793 * non-Ethernet 802.x networks?
3795 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3796 (bpf_int32
)LLCSAP_IPX
);
3798 case ETHERTYPE_ATALK
:
3800 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3801 * SNAP packets with an organization code of
3802 * 0x080007 (Apple, for Appletalk) and a protocol
3803 * type of ETHERTYPE_ATALK (Appletalk).
3805 * XXX - check for an organization code of
3806 * encapsulated Ethernet as well?
3808 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3812 * XXX - we don't have to check for IPX 802.3
3813 * here, but should we check for the IPX Ethertype?
3815 if (proto
<= ETHERMTU
) {
3817 * This is an LLC SAP value, so check
3820 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3823 * This is an Ethernet type; we assume that it's
3824 * unlikely that it'll appear in the right place
3825 * at random, and therefore check only the
3826 * location that would hold the Ethernet type
3827 * in a SNAP frame with an organization code of
3828 * 0x000000 (encapsulated Ethernet).
3830 * XXX - if we were to check for the SNAP DSAP and
3831 * LSAP, as per XXX, and were also to check for an
3832 * organization code of 0x000000 (encapsulated
3833 * Ethernet), we'd do
3835 * return gen_snap(cstate, 0x000000, proto);
3837 * here; for now, we don't, as per the above.
3838 * I don't know whether it's worth the extra CPU
3839 * time to do the right check or not.
3841 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3846 static struct block
*
3847 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3848 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3850 struct block
*b0
, *b1
;
3864 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3865 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3871 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3872 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3879 b0
= gen_linktype(cstate
, proto
);
3880 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3886 static struct block
*
3887 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3888 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3890 struct block
*b0
, *b1
;
3905 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3906 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3912 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3913 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3920 /* this order is important */
3921 a
= (uint32_t *)addr
;
3922 m
= (uint32_t *)mask
;
3923 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3924 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3926 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3928 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3930 b0
= gen_linktype(cstate
, proto
);
3936 static struct block
*
3937 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3939 register struct block
*b0
, *b1
;
3943 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
3946 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
3949 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3950 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3956 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3957 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3962 bpf_error(cstate
, "'addr1' is only supported on 802.11 with 802.11 headers");
3966 bpf_error(cstate
, "'addr2' is only supported on 802.11 with 802.11 headers");
3970 bpf_error(cstate
, "'addr3' is only supported on 802.11 with 802.11 headers");
3974 bpf_error(cstate
, "'addr4' is only supported on 802.11 with 802.11 headers");
3978 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
3982 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
3990 * Like gen_ehostop, but for DLT_FDDI
3992 static struct block
*
3993 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3995 struct block
*b0
, *b1
;
3999 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4002 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4005 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4006 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4012 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4013 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4018 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4022 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4026 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4030 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4034 bpf_error(cstate
, "'ra' is only supported on 802.11");
4038 bpf_error(cstate
, "'ta' is only supported on 802.11");
4046 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4048 static struct block
*
4049 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4051 register struct block
*b0
, *b1
;
4055 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4058 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4061 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4062 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4068 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4069 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4074 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4078 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4082 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4086 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4090 bpf_error(cstate
, "'ra' is only supported on 802.11");
4094 bpf_error(cstate
, "'ta' is only supported on 802.11");
4102 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4103 * various 802.11 + radio headers.
4105 static struct block
*
4106 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4108 register struct block
*b0
, *b1
, *b2
;
4109 register struct slist
*s
;
4111 #ifdef ENABLE_WLAN_FILTERING_PATCH
4114 * We need to disable the optimizer because the optimizer is buggy
4115 * and wipes out some LD instructions generated by the below
4116 * code to validate the Frame Control bits
4118 cstate
->no_optimize
= 1;
4119 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4126 * For control frames, there is no SA.
4128 * For management frames, SA is at an
4129 * offset of 10 from the beginning of
4132 * For data frames, SA is at an offset
4133 * of 10 from the beginning of the packet
4134 * if From DS is clear, at an offset of
4135 * 16 from the beginning of the packet
4136 * if From DS is set and To DS is clear,
4137 * and an offset of 24 from the beginning
4138 * of the packet if From DS is set and To DS
4143 * Generate the tests to be done for data frames
4146 * First, check for To DS set, i.e. check "link[1] & 0x01".
4148 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4149 b1
= new_block(cstate
, JMP(BPF_JSET
));
4150 b1
->s
.k
= 0x01; /* To DS */
4154 * If To DS is set, the SA is at 24.
4156 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4160 * Now, check for To DS not set, i.e. check
4161 * "!(link[1] & 0x01)".
4163 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4164 b2
= new_block(cstate
, JMP(BPF_JSET
));
4165 b2
->s
.k
= 0x01; /* To DS */
4170 * If To DS is not set, the SA is at 16.
4172 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4176 * Now OR together the last two checks. That gives
4177 * the complete set of checks for data frames with
4183 * Now check for From DS being set, and AND that with
4184 * the ORed-together checks.
4186 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4187 b1
= new_block(cstate
, JMP(BPF_JSET
));
4188 b1
->s
.k
= 0x02; /* From DS */
4193 * Now check for data frames with From DS not set.
4195 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4196 b2
= new_block(cstate
, JMP(BPF_JSET
));
4197 b2
->s
.k
= 0x02; /* From DS */
4202 * If From DS isn't set, the SA is at 10.
4204 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4208 * Now OR together the checks for data frames with
4209 * From DS not set and for data frames with From DS
4210 * set; that gives the checks done for data frames.
4215 * Now check for a data frame.
4216 * I.e, check "link[0] & 0x08".
4218 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4219 b1
= new_block(cstate
, JMP(BPF_JSET
));
4224 * AND that with the checks done for data frames.
4229 * If the high-order bit of the type value is 0, this
4230 * is a management frame.
4231 * I.e, check "!(link[0] & 0x08)".
4233 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4234 b2
= new_block(cstate
, JMP(BPF_JSET
));
4240 * For management frames, the SA is at 10.
4242 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4246 * OR that with the checks done for data frames.
4247 * That gives the checks done for management and
4253 * If the low-order bit of the type value is 1,
4254 * this is either a control frame or a frame
4255 * with a reserved type, and thus not a
4258 * I.e., check "!(link[0] & 0x04)".
4260 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4261 b1
= new_block(cstate
, JMP(BPF_JSET
));
4267 * AND that with the checks for data and management
4277 * For control frames, there is no DA.
4279 * For management frames, DA is at an
4280 * offset of 4 from the beginning of
4283 * For data frames, DA is at an offset
4284 * of 4 from the beginning of the packet
4285 * if To DS is clear and at an offset of
4286 * 16 from the beginning of the packet
4291 * Generate the tests to be done for data frames.
4293 * First, check for To DS set, i.e. "link[1] & 0x01".
4295 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4296 b1
= new_block(cstate
, JMP(BPF_JSET
));
4297 b1
->s
.k
= 0x01; /* To DS */
4301 * If To DS is set, the DA is at 16.
4303 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4307 * Now, check for To DS not set, i.e. check
4308 * "!(link[1] & 0x01)".
4310 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4311 b2
= new_block(cstate
, JMP(BPF_JSET
));
4312 b2
->s
.k
= 0x01; /* To DS */
4317 * If To DS is not set, the DA is at 4.
4319 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4323 * Now OR together the last two checks. That gives
4324 * the complete set of checks for data frames.
4329 * Now check for a data frame.
4330 * I.e, check "link[0] & 0x08".
4332 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4333 b1
= new_block(cstate
, JMP(BPF_JSET
));
4338 * AND that with the checks done for data frames.
4343 * If the high-order bit of the type value is 0, this
4344 * is a management frame.
4345 * I.e, check "!(link[0] & 0x08)".
4347 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4348 b2
= new_block(cstate
, JMP(BPF_JSET
));
4354 * For management frames, the DA is at 4.
4356 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4360 * OR that with the checks done for data frames.
4361 * That gives the checks done for management and
4367 * If the low-order bit of the type value is 1,
4368 * this is either a control frame or a frame
4369 * with a reserved type, and thus not a
4372 * I.e., check "!(link[0] & 0x04)".
4374 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4375 b1
= new_block(cstate
, JMP(BPF_JSET
));
4381 * AND that with the checks for data and management
4389 * Not present in management frames; addr1 in other
4394 * If the high-order bit of the type value is 0, this
4395 * is a management frame.
4396 * I.e, check "(link[0] & 0x08)".
4398 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4399 b1
= new_block(cstate
, JMP(BPF_JSET
));
4406 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4409 * AND that with the check of addr1.
4416 * Not present in management frames; addr2, if present,
4421 * Not present in CTS or ACK control frames.
4423 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4424 IEEE80211_FC0_TYPE_MASK
);
4426 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4427 IEEE80211_FC0_SUBTYPE_MASK
);
4429 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4430 IEEE80211_FC0_SUBTYPE_MASK
);
4436 * If the high-order bit of the type value is 0, this
4437 * is a management frame.
4438 * I.e, check "(link[0] & 0x08)".
4440 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4441 b1
= new_block(cstate
, JMP(BPF_JSET
));
4446 * AND that with the check for frames other than
4447 * CTS and ACK frames.
4454 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4459 * XXX - add BSSID keyword?
4462 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4466 * Not present in CTS or ACK control frames.
4468 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4469 IEEE80211_FC0_TYPE_MASK
);
4471 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4472 IEEE80211_FC0_SUBTYPE_MASK
);
4474 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4475 IEEE80211_FC0_SUBTYPE_MASK
);
4479 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4485 * Not present in control frames.
4487 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4488 IEEE80211_FC0_TYPE_MASK
);
4490 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4496 * Present only if the direction mask has both "From DS"
4497 * and "To DS" set. Neither control frames nor management
4498 * frames should have both of those set, so we don't
4499 * check the frame type.
4501 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4502 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4503 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4508 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4509 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4515 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4516 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4525 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4526 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4527 * as the RFC states.)
4529 static struct block
*
4530 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4532 register struct block
*b0
, *b1
;
4536 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4539 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4542 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4543 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4549 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4550 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4555 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4559 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4563 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4567 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4571 bpf_error(cstate
, "'ra' is only supported on 802.11");
4575 bpf_error(cstate
, "'ta' is only supported on 802.11");
4583 * This is quite tricky because there may be pad bytes in front of the
4584 * DECNET header, and then there are two possible data packet formats that
4585 * carry both src and dst addresses, plus 5 packet types in a format that
4586 * carries only the src node, plus 2 types that use a different format and
4587 * also carry just the src node.
4591 * Instead of doing those all right, we just look for data packets with
4592 * 0 or 1 bytes of padding. If you want to look at other packets, that
4593 * will require a lot more hacking.
4595 * To add support for filtering on DECNET "areas" (network numbers)
4596 * one would want to add a "mask" argument to this routine. That would
4597 * make the filter even more inefficient, although one could be clever
4598 * and not generate masking instructions if the mask is 0xFFFF.
4600 static struct block
*
4601 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4603 struct block
*b0
, *b1
, *b2
, *tmp
;
4604 u_int offset_lh
; /* offset if long header is received */
4605 u_int offset_sh
; /* offset if short header is received */
4610 offset_sh
= 1; /* follows flags */
4611 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4615 offset_sh
= 3; /* follows flags, dstnode */
4616 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4620 /* Inefficient because we do our Calvinball dance twice */
4621 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4622 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4628 /* Inefficient because we do our Calvinball dance twice */
4629 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4630 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4635 bpf_error(cstate
, "ISO host filtering not implemented");
4640 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4641 /* Check for pad = 1, long header case */
4642 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4643 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4644 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4645 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4647 /* Check for pad = 0, long header case */
4648 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4649 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4652 /* Check for pad = 1, short header case */
4653 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4654 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4655 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4658 /* Check for pad = 0, short header case */
4659 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4660 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4664 /* Combine with test for cstate->linktype */
4670 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4671 * test the bottom-of-stack bit, and then check the version number
4672 * field in the IP header.
4674 static struct block
*
4675 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4677 struct block
*b0
, *b1
;
4682 /* match the bottom-of-stack bit */
4683 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4684 /* match the IPv4 version number */
4685 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4690 /* match the bottom-of-stack bit */
4691 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4692 /* match the IPv4 version number */
4693 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4702 static struct block
*
4703 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4704 int proto
, int dir
, int type
)
4706 struct block
*b0
, *b1
;
4707 const char *typestr
;
4717 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4719 * Only check for non-IPv4 addresses if we're not
4720 * checking MPLS-encapsulated packets.
4722 if (cstate
->label_stack_depth
== 0) {
4723 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4725 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4731 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4734 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4737 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4740 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4743 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4746 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4749 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4752 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4755 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4758 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4761 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4764 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4767 bpf_error(cstate
, "ATALK host filtering not implemented");
4770 bpf_error(cstate
, "AARP host filtering not implemented");
4773 return gen_dnhostop(cstate
, addr
, dir
);
4776 bpf_error(cstate
, "SCA host filtering not implemented");
4779 bpf_error(cstate
, "LAT host filtering not implemented");
4782 bpf_error(cstate
, "MOPDL host filtering not implemented");
4785 bpf_error(cstate
, "MOPRC host filtering not implemented");
4788 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4791 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4794 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4797 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4800 bpf_error(cstate
, "ISO host filtering not implemented");
4803 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4806 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4809 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4812 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4815 bpf_error(cstate
, "IPX host filtering not implemented");
4818 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4821 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4830 static struct block
*
4831 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4832 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4834 const char *typestr
;
4844 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4847 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4850 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4853 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4856 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4859 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4862 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4865 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4868 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4871 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4874 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4877 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4880 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4883 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4886 bpf_error(cstate
, "ATALK host filtering not implemented");
4889 bpf_error(cstate
, "AARP host filtering not implemented");
4892 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
4895 bpf_error(cstate
, "SCA host filtering not implemented");
4898 bpf_error(cstate
, "LAT host filtering not implemented");
4901 bpf_error(cstate
, "MOPDL host filtering not implemented");
4904 bpf_error(cstate
, "MOPRC host filtering not implemented");
4907 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4910 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4913 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4916 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4919 bpf_error(cstate
, "ISO host filtering not implemented");
4922 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4925 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4928 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4931 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4934 bpf_error(cstate
, "IPX host filtering not implemented");
4937 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4940 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4950 static struct block
*
4951 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
, bpf_u_int32
**alist
,
4954 struct block
*b0
, *b1
, *tmp
;
4957 bpf_error(cstate
, "direction applied to 'gateway'");
4964 switch (cstate
->linktype
) {
4966 case DLT_NETANALYZER
:
4967 case DLT_NETANALYZER_TRANSPARENT
:
4968 b1
= gen_prevlinkhdr_check(cstate
);
4969 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
4974 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
4977 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
4979 case DLT_IEEE802_11
:
4980 case DLT_PRISM_HEADER
:
4981 case DLT_IEEE802_11_RADIO_AVS
:
4982 case DLT_IEEE802_11_RADIO
:
4984 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
4988 * This is LLC-multiplexed traffic; if it were
4989 * LANE, cstate->linktype would have been set to
4993 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4995 case DLT_IP_OVER_FC
:
4996 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5000 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5002 b1
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
5004 tmp
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
,
5013 bpf_error(cstate
, "illegal modifier of 'gateway'");
5019 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5027 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5028 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5033 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5034 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5039 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5040 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5045 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5048 #ifndef IPPROTO_IGMP
5049 #define IPPROTO_IGMP 2
5053 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5056 #ifndef IPPROTO_IGRP
5057 #define IPPROTO_IGRP 9
5060 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5064 #define IPPROTO_PIM 103
5068 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5069 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5073 #ifndef IPPROTO_VRRP
5074 #define IPPROTO_VRRP 112
5078 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5081 #ifndef IPPROTO_CARP
5082 #define IPPROTO_CARP 112
5086 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5090 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5094 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5098 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5102 bpf_error(cstate
, "link layer applied in wrong context");
5105 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5109 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5113 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5117 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5121 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5125 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5129 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5133 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5136 #ifndef IPPROTO_ICMPV6
5137 #define IPPROTO_ICMPV6 58
5140 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5144 #define IPPROTO_AH 51
5147 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5148 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5153 #define IPPROTO_ESP 50
5156 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5157 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5162 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5166 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5170 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5173 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5174 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5175 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5177 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5179 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5181 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5185 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5186 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5187 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5189 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5191 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5193 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5197 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5198 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5199 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5201 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5206 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5207 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5212 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5213 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5215 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5217 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5222 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5223 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5228 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5229 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5234 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5238 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5242 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5246 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5250 bpf_error(cstate
, "'radio' is not a valid protocol type");
5258 static struct block
*
5259 gen_ipfrag(compiler_state_t
*cstate
)
5264 /* not IPv4 frag other than the first frag */
5265 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5266 b
= new_block(cstate
, JMP(BPF_JSET
));
5275 * Generate a comparison to a port value in the transport-layer header
5276 * at the specified offset from the beginning of that header.
5278 * XXX - this handles a variable-length prefix preceding the link-layer
5279 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5280 * variable-length link-layer headers (such as Token Ring or 802.11
5283 static struct block
*
5284 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5286 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5289 static struct block
*
5290 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5292 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5296 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5298 struct block
*b0
, *b1
, *tmp
;
5300 /* ip proto 'proto' and not a fragment other than the first fragment */
5301 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5302 b0
= gen_ipfrag(cstate
);
5307 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5311 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5316 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5317 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5322 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5323 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5335 static struct block
*
5336 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5338 struct block
*b0
, *b1
, *tmp
;
5343 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5344 * not LLC encapsulation with LLCSAP_IP.
5346 * For IEEE 802 networks - which includes 802.5 token ring
5347 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5348 * says that SNAP encapsulation is used, not LLC encapsulation
5351 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5352 * RFC 2225 say that SNAP encapsulation is used, not LLC
5353 * encapsulation with LLCSAP_IP.
5355 * So we always check for ETHERTYPE_IP.
5357 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5363 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5367 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5368 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5370 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5382 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5384 struct block
*b0
, *b1
, *tmp
;
5386 /* ip6 proto 'proto' */
5387 /* XXX - catch the first fragment of a fragmented packet? */
5388 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5392 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5396 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5401 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5402 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5407 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5408 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5420 static struct block
*
5421 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5423 struct block
*b0
, *b1
, *tmp
;
5425 /* link proto ip6 */
5426 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5432 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5436 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5437 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5439 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5450 /* gen_portrange code */
5451 static struct block
*
5452 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5455 struct block
*b1
, *b2
;
5459 * Reverse the order of the ports, so v1 is the lower one.
5468 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5469 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5477 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5480 struct block
*b0
, *b1
, *tmp
;
5482 /* ip proto 'proto' and not a fragment other than the first fragment */
5483 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5484 b0
= gen_ipfrag(cstate
);
5489 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5493 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5498 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5499 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5504 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5505 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5517 static struct block
*
5518 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5521 struct block
*b0
, *b1
, *tmp
;
5524 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5530 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5534 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5535 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5537 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5548 static struct block
*
5549 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5552 struct block
*b1
, *b2
;
5556 * Reverse the order of the ports, so v1 is the lower one.
5565 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5566 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5574 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5577 struct block
*b0
, *b1
, *tmp
;
5579 /* ip6 proto 'proto' */
5580 /* XXX - catch the first fragment of a fragmented packet? */
5581 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5585 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5589 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5594 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5595 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5600 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5601 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5613 static struct block
*
5614 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5617 struct block
*b0
, *b1
, *tmp
;
5619 /* link proto ip6 */
5620 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5626 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5630 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5631 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5633 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5645 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5654 v
= pcap_nametoproto(name
);
5655 if (v
== PROTO_UNDEF
)
5656 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5660 /* XXX should look up h/w protocol type based on cstate->linktype */
5661 v
= pcap_nametoeproto(name
);
5662 if (v
== PROTO_UNDEF
) {
5663 v
= pcap_nametollc(name
);
5664 if (v
== PROTO_UNDEF
)
5665 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5670 if (strcmp(name
, "esis") == 0)
5672 else if (strcmp(name
, "isis") == 0)
5674 else if (strcmp(name
, "clnp") == 0)
5677 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5697 static struct block
*
5698 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5700 #ifdef NO_PROTOCHAIN
5701 return gen_proto(cstate
, v
, proto
, dir
);
5703 struct block
*b0
, *b
;
5704 struct slist
*s
[100];
5705 int fix2
, fix3
, fix4
, fix5
;
5706 int ahcheck
, again
, end
;
5708 int reg2
= alloc_reg(cstate
);
5710 memset(s
, 0, sizeof(s
));
5711 fix3
= fix4
= fix5
= 0;
5718 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5719 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5723 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5728 * We don't handle variable-length prefixes before the link-layer
5729 * header, or variable-length link-layer headers, here yet.
5730 * We might want to add BPF instructions to do the protochain
5731 * work, to simplify that and, on platforms that have a BPF
5732 * interpreter with the new instructions, let the filtering
5733 * be done in the kernel. (We already require a modified BPF
5734 * engine to do the protochain stuff, to support backward
5735 * branches, and backward branch support is unlikely to appear
5736 * in kernel BPF engines.)
5738 if (cstate
->off_linkpl
.is_variable
)
5739 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5741 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
5744 * s[0] is a dummy entry to protect other BPF insn from damage
5745 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5746 * hard to find interdependency made by jump table fixup.
5749 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5754 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5757 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5758 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5760 /* X = ip->ip_hl << 2 */
5761 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5762 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5767 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5769 /* A = ip6->ip_nxt */
5770 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5771 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
5773 /* X = sizeof(struct ip6_hdr) */
5774 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
5780 bpf_error(cstate
, "unsupported proto to gen_protochain");
5784 /* again: if (A == v) goto end; else fall through; */
5786 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5788 s
[i
]->s
.jt
= NULL
; /*later*/
5789 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5793 #ifndef IPPROTO_NONE
5794 #define IPPROTO_NONE 59
5796 /* if (A == IPPROTO_NONE) goto end */
5797 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5798 s
[i
]->s
.jt
= NULL
; /*later*/
5799 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5800 s
[i
]->s
.k
= IPPROTO_NONE
;
5801 s
[fix5
]->s
.jf
= s
[i
];
5805 if (proto
== Q_IPV6
) {
5806 int v6start
, v6end
, v6advance
, j
;
5809 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5810 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5811 s
[i
]->s
.jt
= NULL
; /*later*/
5812 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5813 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5814 s
[fix2
]->s
.jf
= s
[i
];
5816 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5817 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5818 s
[i
]->s
.jt
= NULL
; /*later*/
5819 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5820 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5822 /* if (A == IPPROTO_ROUTING) goto v6advance */
5823 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5824 s
[i
]->s
.jt
= NULL
; /*later*/
5825 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5826 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5828 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5829 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5830 s
[i
]->s
.jt
= NULL
; /*later*/
5831 s
[i
]->s
.jf
= NULL
; /*later*/
5832 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5842 * A = P[X + packet head];
5843 * X = X + (P[X + packet head + 1] + 1) * 8;
5845 /* A = P[X + packet head] */
5846 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5847 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5850 s
[i
] = new_stmt(cstate
, BPF_ST
);
5853 /* A = P[X + packet head + 1]; */
5854 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5855 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
5858 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5862 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5866 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
5870 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5873 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5877 /* goto again; (must use BPF_JA for backward jump) */
5878 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5879 s
[i
]->s
.k
= again
- i
- 1;
5880 s
[i
- 1]->s
.jf
= s
[i
];
5884 for (j
= v6start
; j
<= v6end
; j
++)
5885 s
[j
]->s
.jt
= s
[v6advance
];
5888 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5890 s
[fix2
]->s
.jf
= s
[i
];
5896 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5897 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5898 s
[i
]->s
.jt
= NULL
; /*later*/
5899 s
[i
]->s
.jf
= NULL
; /*later*/
5900 s
[i
]->s
.k
= IPPROTO_AH
;
5902 s
[fix3
]->s
.jf
= s
[ahcheck
];
5909 * X = X + (P[X + 1] + 2) * 4;
5912 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5914 /* A = P[X + packet head]; */
5915 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5916 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5919 s
[i
] = new_stmt(cstate
, BPF_ST
);
5923 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5926 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5930 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5932 /* A = P[X + packet head] */
5933 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5934 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5937 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5941 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5945 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5948 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5952 /* goto again; (must use BPF_JA for backward jump) */
5953 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5954 s
[i
]->s
.k
= again
- i
- 1;
5959 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5961 s
[fix2
]->s
.jt
= s
[end
];
5962 s
[fix4
]->s
.jf
= s
[end
];
5963 s
[fix5
]->s
.jt
= s
[end
];
5970 for (i
= 0; i
< max
- 1; i
++)
5971 s
[i
]->next
= s
[i
+ 1];
5972 s
[max
- 1]->next
= NULL
;
5977 b
= new_block(cstate
, JMP(BPF_JEQ
));
5978 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5981 free_reg(cstate
, reg2
);
5988 static struct block
*
5989 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
5992 struct block
*b0
, *b1
;
5995 * A data frame has the 0x08 bit (b3) in the frame control field set
5996 * and the 0x04 bit (b2) clear.
5998 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
5999 b0
= new_block(cstate
, JMP(BPF_JSET
));
6003 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6004 b1
= new_block(cstate
, JMP(BPF_JSET
));
6015 * Generate code that checks whether the packet is a packet for protocol
6016 * <proto> and whether the type field in that protocol's header has
6017 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6018 * IP packet and checks the protocol number in the IP header against <v>.
6020 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6021 * against Q_IP and Q_IPV6.
6023 static struct block
*
6024 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6026 struct block
*b0
, *b1
;
6031 if (dir
!= Q_DEFAULT
)
6032 bpf_error(cstate
, "direction applied to 'proto'");
6036 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6037 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6043 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6044 * not LLC encapsulation with LLCSAP_IP.
6046 * For IEEE 802 networks - which includes 802.5 token ring
6047 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6048 * says that SNAP encapsulation is used, not LLC encapsulation
6051 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6052 * RFC 2225 say that SNAP encapsulation is used, not LLC
6053 * encapsulation with LLCSAP_IP.
6055 * So we always check for ETHERTYPE_IP.
6057 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6059 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6061 b1
= gen_protochain(cstate
, v
, Q_IP
);
6067 switch (cstate
->linktype
) {
6071 * Frame Relay packets typically have an OSI
6072 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6073 * generates code to check for all the OSI
6074 * NLPIDs, so calling it and then adding a check
6075 * for the particular NLPID for which we're
6076 * looking is bogus, as we can just check for
6079 * What we check for is the NLPID and a frame
6080 * control field value of UI, i.e. 0x03 followed
6083 * XXX - assumes a 2-byte Frame Relay header with
6084 * DLCI and flags. What if the address is longer?
6086 * XXX - what about SNAP-encapsulated frames?
6088 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6094 * Cisco uses an Ethertype lookalike - for OSI,
6097 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6098 /* OSI in C-HDLC is stuffed with a fudge byte */
6099 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6104 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6105 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6111 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6113 * 4 is the offset of the PDU type relative to the IS-IS
6116 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6121 bpf_error(cstate
, "arp does not encapsulate another protocol");
6125 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6129 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6133 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6137 bpf_error(cstate
, "sca does not encapsulate another protocol");
6141 bpf_error(cstate
, "lat does not encapsulate another protocol");
6145 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6149 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6153 return gen_linktype(cstate
, v
);
6156 bpf_error(cstate
, "'udp proto' is bogus");
6160 bpf_error(cstate
, "'tcp proto' is bogus");
6164 bpf_error(cstate
, "'sctp proto' is bogus");
6168 bpf_error(cstate
, "'icmp proto' is bogus");
6172 bpf_error(cstate
, "'igmp proto' is bogus");
6176 bpf_error(cstate
, "'igrp proto' is bogus");
6180 bpf_error(cstate
, "'pim proto' is bogus");
6184 bpf_error(cstate
, "'vrrp proto' is bogus");
6188 bpf_error(cstate
, "'carp proto' is bogus");
6192 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6195 * Also check for a fragment header before the final
6198 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6199 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6201 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6204 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6210 bpf_error(cstate
, "'icmp6 proto' is bogus");
6213 bpf_error(cstate
, "'ah proto' is bogus");
6216 bpf_error(cstate
, "'ah proto' is bogus");
6219 bpf_error(cstate
, "'stp proto' is bogus");
6222 bpf_error(cstate
, "'ipx proto' is bogus");
6225 bpf_error(cstate
, "'netbeui proto' is bogus");
6228 bpf_error(cstate
, "'radio proto' is bogus");
6238 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6240 int proto
= q
.proto
;
6244 bpf_u_int32 mask
, addr
;
6246 bpf_u_int32
**alist
;
6249 struct sockaddr_in
*sin4
;
6250 struct sockaddr_in6
*sin6
;
6251 struct addrinfo
*res
, *res0
;
6252 struct in6_addr mask128
;
6254 struct block
*b
, *tmp
;
6255 int port
, real_proto
;
6261 addr
= pcap_nametonetaddr(name
);
6263 bpf_error(cstate
, "unknown network '%s'", name
);
6264 /* Left justify network addr and calculate its network mask */
6266 while (addr
&& (addr
& 0xff000000) == 0) {
6270 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6274 if (proto
== Q_LINK
) {
6275 switch (cstate
->linktype
) {
6278 case DLT_NETANALYZER
:
6279 case DLT_NETANALYZER_TRANSPARENT
:
6280 eaddr
= pcap_ether_hostton(name
);
6283 "unknown ether host '%s'", name
);
6284 tmp
= gen_prevlinkhdr_check(cstate
);
6285 b
= gen_ehostop(cstate
, eaddr
, dir
);
6292 eaddr
= pcap_ether_hostton(name
);
6295 "unknown FDDI host '%s'", name
);
6296 b
= gen_fhostop(cstate
, eaddr
, dir
);
6301 eaddr
= pcap_ether_hostton(name
);
6304 "unknown token ring host '%s'", name
);
6305 b
= gen_thostop(cstate
, eaddr
, dir
);
6309 case DLT_IEEE802_11
:
6310 case DLT_PRISM_HEADER
:
6311 case DLT_IEEE802_11_RADIO_AVS
:
6312 case DLT_IEEE802_11_RADIO
:
6314 eaddr
= pcap_ether_hostton(name
);
6317 "unknown 802.11 host '%s'", name
);
6318 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6322 case DLT_IP_OVER_FC
:
6323 eaddr
= pcap_ether_hostton(name
);
6326 "unknown Fibre Channel host '%s'", name
);
6327 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6332 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6333 } else if (proto
== Q_DECNET
) {
6334 unsigned short dn_addr
;
6336 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6338 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6340 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6345 * I don't think DECNET hosts can be multihomed, so
6346 * there is no need to build up a list of addresses
6348 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6351 alist
= pcap_nametoaddr(name
);
6352 if (alist
== NULL
|| *alist
== NULL
)
6353 bpf_error(cstate
, "unknown host '%s'", name
);
6355 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6356 tproto
== Q_DEFAULT
)
6358 b
= gen_host(cstate
, **alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6360 tmp
= gen_host(cstate
, **alist
++, 0xffffffff,
6361 tproto
, dir
, q
.addr
);
6367 memset(&mask128
, 0xff, sizeof(mask128
));
6368 res0
= res
= pcap_nametoaddrinfo(name
);
6370 bpf_error(cstate
, "unknown host '%s'", name
);
6373 tproto
= tproto6
= proto
;
6374 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6375 tproto
== Q_DEFAULT
) {
6379 for (res
= res0
; res
; res
= res
->ai_next
) {
6380 switch (res
->ai_family
) {
6382 if (tproto
== Q_IPV6
)
6385 sin4
= (struct sockaddr_in
*)
6387 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6388 0xffffffff, tproto
, dir
, q
.addr
);
6391 if (tproto6
== Q_IP
)
6394 sin6
= (struct sockaddr_in6
*)
6396 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6397 &mask128
, tproto6
, dir
, q
.addr
);
6409 bpf_error(cstate
, "unknown host '%s'%s", name
,
6410 (proto
== Q_DEFAULT
)
6412 : " for specified address family");
6419 if (proto
!= Q_DEFAULT
&&
6420 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6421 bpf_error(cstate
, "illegal qualifier of 'port'");
6422 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6423 bpf_error(cstate
, "unknown port '%s'", name
);
6424 if (proto
== Q_UDP
) {
6425 if (real_proto
== IPPROTO_TCP
)
6426 bpf_error(cstate
, "port '%s' is tcp", name
);
6427 else if (real_proto
== IPPROTO_SCTP
)
6428 bpf_error(cstate
, "port '%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 '%s' is udp", name
);
6437 else if (real_proto
== IPPROTO_SCTP
)
6438 bpf_error(cstate
, "port '%s' is sctp", name
);
6440 /* override PROTO_UNDEF */
6441 real_proto
= IPPROTO_TCP
;
6443 if (proto
== Q_SCTP
) {
6444 if (real_proto
== IPPROTO_UDP
)
6445 bpf_error(cstate
, "port '%s' is udp", name
);
6447 else if (real_proto
== IPPROTO_TCP
)
6448 bpf_error(cstate
, "port '%s' is tcp", name
);
6450 /* override PROTO_UNDEF */
6451 real_proto
= IPPROTO_SCTP
;
6454 bpf_error(cstate
, "illegal port number %d < 0", port
);
6456 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6457 b
= gen_port(cstate
, port
, real_proto
, dir
);
6458 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6462 if (proto
!= Q_DEFAULT
&&
6463 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6464 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6465 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6466 bpf_error(cstate
, "unknown port in range '%s'", name
);
6467 if (proto
== Q_UDP
) {
6468 if (real_proto
== IPPROTO_TCP
)
6469 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6470 else if (real_proto
== IPPROTO_SCTP
)
6471 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6473 /* override PROTO_UNDEF */
6474 real_proto
= IPPROTO_UDP
;
6476 if (proto
== Q_TCP
) {
6477 if (real_proto
== IPPROTO_UDP
)
6478 bpf_error(cstate
, "port in range '%s' is udp", name
);
6479 else if (real_proto
== IPPROTO_SCTP
)
6480 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6482 /* override PROTO_UNDEF */
6483 real_proto
= IPPROTO_TCP
;
6485 if (proto
== Q_SCTP
) {
6486 if (real_proto
== IPPROTO_UDP
)
6487 bpf_error(cstate
, "port in range '%s' is udp", name
);
6488 else if (real_proto
== IPPROTO_TCP
)
6489 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6491 /* override PROTO_UNDEF */
6492 real_proto
= IPPROTO_SCTP
;
6495 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6497 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6499 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6501 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6503 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6504 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6509 eaddr
= pcap_ether_hostton(name
);
6511 bpf_error(cstate
, "unknown ether host: %s", name
);
6513 alist
= pcap_nametoaddr(name
);
6514 if (alist
== NULL
|| *alist
== NULL
)
6515 bpf_error(cstate
, "unknown host '%s'", name
);
6516 b
= gen_gateway(cstate
, eaddr
, alist
, proto
, dir
);
6520 bpf_error(cstate
, "'gateway' not supported in this configuration");
6524 real_proto
= lookup_proto(cstate
, name
, proto
);
6525 if (real_proto
>= 0)
6526 return gen_proto(cstate
, real_proto
, proto
, dir
);
6528 bpf_error(cstate
, "unknown protocol: %s", name
);
6531 real_proto
= lookup_proto(cstate
, name
, proto
);
6532 if (real_proto
>= 0)
6533 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6535 bpf_error(cstate
, "unknown protocol: %s", name
);
6546 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6547 unsigned int masklen
, struct qual q
)
6549 register int nlen
, mlen
;
6552 nlen
= __pcap_atoin(s1
, &n
);
6553 /* Promote short ipaddr */
6557 mlen
= __pcap_atoin(s2
, &m
);
6558 /* Promote short ipaddr */
6561 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6564 /* Convert mask len to mask */
6566 bpf_error(cstate
, "mask length must be <= 32");
6569 * X << 32 is not guaranteed by C to be 0; it's
6574 m
= 0xffffffff << (32 - masklen
);
6576 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6583 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6586 bpf_error(cstate
, "Mask syntax for networks only");
6594 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6597 int proto
= q
.proto
;
6603 else if (q
.proto
== Q_DECNET
) {
6604 vlen
= __pcap_atodn(s
, &v
);
6606 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6608 vlen
= __pcap_atoin(s
, &v
);
6615 if (proto
== Q_DECNET
)
6616 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6617 else if (proto
== Q_LINK
) {
6618 bpf_error(cstate
, "illegal link layer address");
6621 if (s
== NULL
&& q
.addr
== Q_NET
) {
6622 /* Promote short net number */
6623 while (v
&& (v
& 0xff000000) == 0) {
6628 /* Promote short ipaddr */
6630 mask
<<= 32 - vlen
;
6632 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6637 proto
= IPPROTO_UDP
;
6638 else if (proto
== Q_TCP
)
6639 proto
= IPPROTO_TCP
;
6640 else if (proto
== Q_SCTP
)
6641 proto
= IPPROTO_SCTP
;
6642 else if (proto
== Q_DEFAULT
)
6643 proto
= PROTO_UNDEF
;
6645 bpf_error(cstate
, "illegal qualifier of 'port'");
6648 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6652 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6653 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6659 proto
= IPPROTO_UDP
;
6660 else if (proto
== Q_TCP
)
6661 proto
= IPPROTO_TCP
;
6662 else if (proto
== Q_SCTP
)
6663 proto
= IPPROTO_SCTP
;
6664 else if (proto
== Q_DEFAULT
)
6665 proto
= PROTO_UNDEF
;
6667 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6670 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6674 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6675 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6680 bpf_error(cstate
, "'gateway' requires a name");
6684 return gen_proto(cstate
, (int)v
, proto
, dir
);
6687 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6702 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6703 unsigned int masklen
, struct qual q
)
6705 struct addrinfo
*res
;
6706 struct in6_addr
*addr
;
6707 struct in6_addr mask
;
6712 bpf_error(cstate
, "no mask %s supported", s2
);
6714 res
= pcap_nametoaddrinfo(s1
);
6716 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6719 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6720 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6722 if (sizeof(mask
) * 8 < masklen
)
6723 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6724 memset(&mask
, 0, sizeof(mask
));
6725 memset(&mask
, 0xff, masklen
/ 8);
6727 mask
.s6_addr
[masklen
/ 8] =
6728 (0xff << (8 - masklen
% 8)) & 0xff;
6731 a
= (uint32_t *)addr
;
6732 m
= (uint32_t *)&mask
;
6733 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6734 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6735 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6743 bpf_error(cstate
, "Mask syntax for networks only");
6747 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6753 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6761 gen_ecode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
6763 struct block
*b
, *tmp
;
6765 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6766 switch (cstate
->linktype
) {
6768 case DLT_NETANALYZER
:
6769 case DLT_NETANALYZER_TRANSPARENT
:
6770 tmp
= gen_prevlinkhdr_check(cstate
);
6771 b
= gen_ehostop(cstate
, eaddr
, (int)q
.dir
);
6776 return gen_fhostop(cstate
, eaddr
, (int)q
.dir
);
6778 return gen_thostop(cstate
, eaddr
, (int)q
.dir
);
6779 case DLT_IEEE802_11
:
6780 case DLT_PRISM_HEADER
:
6781 case DLT_IEEE802_11_RADIO_AVS
:
6782 case DLT_IEEE802_11_RADIO
:
6784 return gen_wlanhostop(cstate
, eaddr
, (int)q
.dir
);
6785 case DLT_IP_OVER_FC
:
6786 return gen_ipfchostop(cstate
, eaddr
, (int)q
.dir
);
6788 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6792 bpf_error(cstate
, "ethernet address used in non-ether expression");
6799 struct slist
*s0
, *s1
;
6802 * This is definitely not the best way to do this, but the
6803 * lists will rarely get long.
6810 static struct slist
*
6811 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
6815 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
6820 static struct slist
*
6821 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
6825 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6831 * Modify "index" to use the value stored into its register as an
6832 * offset relative to the beginning of the header for the protocol
6833 * "proto", and allocate a register and put an item "size" bytes long
6834 * (1, 2, or 4) at that offset into that register, making it the register
6838 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
6840 struct slist
*s
, *tmp
;
6842 int regno
= alloc_reg(cstate
);
6844 free_reg(cstate
, inst
->regno
);
6848 bpf_error(cstate
, "data size must be 1, 2, or 4");
6864 bpf_error(cstate
, "unsupported index operation");
6868 * The offset is relative to the beginning of the packet
6869 * data, if we have a radio header. (If we don't, this
6872 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6873 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
6874 cstate
->linktype
!= DLT_PRISM_HEADER
)
6875 bpf_error(cstate
, "radio information not present in capture");
6878 * Load into the X register the offset computed into the
6879 * register specified by "index".
6881 s
= xfer_to_x(cstate
, inst
);
6884 * Load the item at that offset.
6886 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6888 sappend(inst
->s
, s
);
6893 * The offset is relative to the beginning of
6894 * the link-layer header.
6896 * XXX - what about ATM LANE? Should the index be
6897 * relative to the beginning of the AAL5 frame, so
6898 * that 0 refers to the beginning of the LE Control
6899 * field, or relative to the beginning of the LAN
6900 * frame, so that 0 refers, for Ethernet LANE, to
6901 * the beginning of the destination address?
6903 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
6906 * If "s" is non-null, it has code to arrange that the
6907 * X register contains the length of the prefix preceding
6908 * the link-layer header. Add to it the offset computed
6909 * into the register specified by "index", and move that
6910 * into the X register. Otherwise, just load into the X
6911 * register the offset computed into the register specified
6915 sappend(s
, xfer_to_a(cstate
, inst
));
6916 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6917 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6919 s
= xfer_to_x(cstate
, inst
);
6922 * Load the item at the sum of the offset we've put in the
6923 * X register and the offset of the start of the link
6924 * layer header (which is 0 if the radio header is
6925 * variable-length; that header length is what we put
6926 * into the X register and then added to the index).
6928 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6929 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
6931 sappend(inst
->s
, s
);
6945 * The offset is relative to the beginning of
6946 * the network-layer header.
6947 * XXX - are there any cases where we want
6948 * cstate->off_nl_nosnap?
6950 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
6953 * If "s" is non-null, it has code to arrange that the
6954 * X register contains the variable part of the offset
6955 * of the link-layer payload. Add to it the offset
6956 * computed into the register specified by "index",
6957 * and move that into the X register. Otherwise, just
6958 * load into the X register the offset computed into
6959 * the register specified by "index".
6962 sappend(s
, xfer_to_a(cstate
, inst
));
6963 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6964 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6966 s
= xfer_to_x(cstate
, inst
);
6969 * Load the item at the sum of the offset we've put in the
6970 * X register, the offset of the start of the network
6971 * layer header from the beginning of the link-layer
6972 * payload, and the constant part of the offset of the
6973 * start of the link-layer payload.
6975 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6976 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6978 sappend(inst
->s
, s
);
6981 * Do the computation only if the packet contains
6982 * the protocol in question.
6984 b
= gen_proto_abbrev(cstate
, proto
);
6986 gen_and(inst
->b
, b
);
7000 * The offset is relative to the beginning of
7001 * the transport-layer header.
7003 * Load the X register with the length of the IPv4 header
7004 * (plus the offset of the link-layer header, if it's
7005 * a variable-length header), in bytes.
7007 * XXX - are there any cases where we want
7008 * cstate->off_nl_nosnap?
7009 * XXX - we should, if we're built with
7010 * IPv6 support, generate code to load either
7011 * IPv4, IPv6, or both, as appropriate.
7013 s
= gen_loadx_iphdrlen(cstate
);
7016 * The X register now contains the sum of the variable
7017 * part of the offset of the link-layer payload and the
7018 * length of the network-layer header.
7020 * Load into the A register the offset relative to
7021 * the beginning of the transport layer header,
7022 * add the X register to that, move that to the
7023 * X register, and load with an offset from the
7024 * X register equal to the sum of the constant part of
7025 * the offset of the link-layer payload and the offset,
7026 * relative to the beginning of the link-layer payload,
7027 * of the network-layer header.
7029 sappend(s
, xfer_to_a(cstate
, inst
));
7030 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7031 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7032 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7033 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7034 sappend(inst
->s
, s
);
7037 * Do the computation only if the packet contains
7038 * the protocol in question - which is true only
7039 * if this is an IP datagram and is the first or
7040 * only fragment of that datagram.
7042 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7044 gen_and(inst
->b
, b
);
7045 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7050 * Do the computation only if the packet contains
7051 * the protocol in question.
7053 b
= gen_proto_abbrev(cstate
, Q_IPV6
);
7055 gen_and(inst
->b
, b
);
7060 * Check if we have an icmp6 next header
7062 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7064 gen_and(inst
->b
, b
);
7069 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7071 * If "s" is non-null, it has code to arrange that the
7072 * X register contains the variable part of the offset
7073 * of the link-layer payload. Add to it the offset
7074 * computed into the register specified by "index",
7075 * and move that into the X register. Otherwise, just
7076 * load into the X register the offset computed into
7077 * the register specified by "index".
7080 sappend(s
, xfer_to_a(cstate
, inst
));
7081 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7082 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7084 s
= xfer_to_x(cstate
, inst
);
7088 * Load the item at the sum of the offset we've put in the
7089 * X register, the offset of the start of the network
7090 * layer header from the beginning of the link-layer
7091 * payload, and the constant part of the offset of the
7092 * start of the link-layer payload.
7094 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7095 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7098 sappend(inst
->s
, s
);
7102 inst
->regno
= regno
;
7103 s
= new_stmt(cstate
, BPF_ST
);
7105 sappend(inst
->s
, s
);
7111 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7112 struct arth
*a1
, int reversed
)
7114 struct slist
*s0
, *s1
, *s2
;
7115 struct block
*b
, *tmp
;
7117 s0
= xfer_to_x(cstate
, a1
);
7118 s1
= xfer_to_a(cstate
, a0
);
7119 if (code
== BPF_JEQ
) {
7120 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7121 b
= new_block(cstate
, JMP(code
));
7125 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7131 sappend(a0
->s
, a1
->s
);
7135 free_reg(cstate
, a0
->regno
);
7136 free_reg(cstate
, a1
->regno
);
7138 /* 'and' together protocol checks */
7141 gen_and(a0
->b
, tmp
= a1
->b
);
7155 gen_loadlen(compiler_state_t
*cstate
)
7157 int regno
= alloc_reg(cstate
);
7158 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7161 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7162 s
->next
= new_stmt(cstate
, BPF_ST
);
7163 s
->next
->s
.k
= regno
;
7171 gen_loadi(compiler_state_t
*cstate
, int val
)
7177 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7179 reg
= alloc_reg(cstate
);
7181 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7183 s
->next
= new_stmt(cstate
, BPF_ST
);
7192 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7196 s
= xfer_to_a(cstate
, a
);
7198 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7201 s
= new_stmt(cstate
, BPF_ST
);
7209 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7212 struct slist
*s0
, *s1
, *s2
;
7215 * Disallow division by, or modulus by, zero; we do this here
7216 * so that it gets done even if the optimizer is disabled.
7218 if (code
== BPF_DIV
) {
7219 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7220 bpf_error(cstate
, "division by zero");
7221 } else if (code
== BPF_MOD
) {
7222 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7223 bpf_error(cstate
, "modulus by zero");
7225 s0
= xfer_to_x(cstate
, a1
);
7226 s1
= xfer_to_a(cstate
, a0
);
7227 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7232 sappend(a0
->s
, a1
->s
);
7234 free_reg(cstate
, a0
->regno
);
7235 free_reg(cstate
, a1
->regno
);
7237 s0
= new_stmt(cstate
, BPF_ST
);
7238 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7245 * Initialize the table of used registers and the current register.
7248 init_regs(compiler_state_t
*cstate
)
7251 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7255 * Return the next free register.
7258 alloc_reg(compiler_state_t
*cstate
)
7260 int n
= BPF_MEMWORDS
;
7263 if (cstate
->regused
[cstate
->curreg
])
7264 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7266 cstate
->regused
[cstate
->curreg
] = 1;
7267 return cstate
->curreg
;
7270 bpf_error(cstate
, "too many registers needed to evaluate expression");
7276 * Return a register to the table so it can
7280 free_reg(compiler_state_t
*cstate
, int n
)
7282 cstate
->regused
[n
] = 0;
7285 static struct block
*
7286 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7291 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7292 b
= new_block(cstate
, JMP(jmp
));
7300 gen_greater(compiler_state_t
*cstate
, int n
)
7302 return gen_len(cstate
, BPF_JGE
, n
);
7306 * Actually, this is less than or equal.
7309 gen_less(compiler_state_t
*cstate
, int n
)
7313 b
= gen_len(cstate
, BPF_JGT
, n
);
7320 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7321 * the beginning of the link-layer header.
7322 * XXX - that means you can't test values in the radiotap header, but
7323 * as that header is difficult if not impossible to parse generally
7324 * without a loop, that might not be a severe problem. A new keyword
7325 * "radio" could be added for that, although what you'd really want
7326 * would be a way of testing particular radio header values, which
7327 * would generate code appropriate to the radio header in question.
7330 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7340 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7343 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7347 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7351 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7355 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7359 b
= new_block(cstate
, JMP(BPF_JEQ
));
7366 static const u_char abroadcast
[] = { 0x0 };
7369 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7371 bpf_u_int32 hostmask
;
7372 struct block
*b0
, *b1
, *b2
;
7373 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7379 switch (cstate
->linktype
) {
7381 case DLT_ARCNET_LINUX
:
7382 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7384 case DLT_NETANALYZER
:
7385 case DLT_NETANALYZER_TRANSPARENT
:
7386 b1
= gen_prevlinkhdr_check(cstate
);
7387 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7392 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7394 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7395 case DLT_IEEE802_11
:
7396 case DLT_PRISM_HEADER
:
7397 case DLT_IEEE802_11_RADIO_AVS
:
7398 case DLT_IEEE802_11_RADIO
:
7400 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7401 case DLT_IP_OVER_FC
:
7402 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7404 bpf_error(cstate
, "not a broadcast link");
7410 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7411 * as an indication that we don't know the netmask, and fail
7414 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7415 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7416 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7417 hostmask
= ~cstate
->netmask
;
7418 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7419 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7420 (bpf_int32
)(~0 & hostmask
), hostmask
);
7425 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7431 * Generate code to test the low-order bit of a MAC address (that's
7432 * the bottom bit of the *first* byte).
7434 static struct block
*
7435 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7437 register struct block
*b0
;
7438 register struct slist
*s
;
7440 /* link[offset] & 1 != 0 */
7441 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7442 b0
= new_block(cstate
, JMP(BPF_JSET
));
7449 gen_multicast(compiler_state_t
*cstate
, int proto
)
7451 register struct block
*b0
, *b1
, *b2
;
7452 register struct slist
*s
;
7458 switch (cstate
->linktype
) {
7460 case DLT_ARCNET_LINUX
:
7461 /* all ARCnet multicasts use the same address */
7462 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7464 case DLT_NETANALYZER
:
7465 case DLT_NETANALYZER_TRANSPARENT
:
7466 b1
= gen_prevlinkhdr_check(cstate
);
7467 /* ether[0] & 1 != 0 */
7468 b0
= gen_mac_multicast(cstate
, 0);
7474 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7476 * XXX - was that referring to bit-order issues?
7478 /* fddi[1] & 1 != 0 */
7479 return gen_mac_multicast(cstate
, 1);
7481 /* tr[2] & 1 != 0 */
7482 return gen_mac_multicast(cstate
, 2);
7483 case DLT_IEEE802_11
:
7484 case DLT_PRISM_HEADER
:
7485 case DLT_IEEE802_11_RADIO_AVS
:
7486 case DLT_IEEE802_11_RADIO
:
7491 * For control frames, there is no DA.
7493 * For management frames, DA is at an
7494 * offset of 4 from the beginning of
7497 * For data frames, DA is at an offset
7498 * of 4 from the beginning of the packet
7499 * if To DS is clear and at an offset of
7500 * 16 from the beginning of the packet
7505 * Generate the tests to be done for data frames.
7507 * First, check for To DS set, i.e. "link[1] & 0x01".
7509 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7510 b1
= new_block(cstate
, JMP(BPF_JSET
));
7511 b1
->s
.k
= 0x01; /* To DS */
7515 * If To DS is set, the DA is at 16.
7517 b0
= gen_mac_multicast(cstate
, 16);
7521 * Now, check for To DS not set, i.e. check
7522 * "!(link[1] & 0x01)".
7524 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7525 b2
= new_block(cstate
, JMP(BPF_JSET
));
7526 b2
->s
.k
= 0x01; /* To DS */
7531 * If To DS is not set, the DA is at 4.
7533 b1
= gen_mac_multicast(cstate
, 4);
7537 * Now OR together the last two checks. That gives
7538 * the complete set of checks for data frames.
7543 * Now check for a data frame.
7544 * I.e, check "link[0] & 0x08".
7546 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7547 b1
= new_block(cstate
, JMP(BPF_JSET
));
7552 * AND that with the checks done for data frames.
7557 * If the high-order bit of the type value is 0, this
7558 * is a management frame.
7559 * I.e, check "!(link[0] & 0x08)".
7561 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7562 b2
= new_block(cstate
, JMP(BPF_JSET
));
7568 * For management frames, the DA is at 4.
7570 b1
= gen_mac_multicast(cstate
, 4);
7574 * OR that with the checks done for data frames.
7575 * That gives the checks done for management and
7581 * If the low-order bit of the type value is 1,
7582 * this is either a control frame or a frame
7583 * with a reserved type, and thus not a
7586 * I.e., check "!(link[0] & 0x04)".
7588 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7589 b1
= new_block(cstate
, JMP(BPF_JSET
));
7595 * AND that with the checks for data and management
7600 case DLT_IP_OVER_FC
:
7601 b0
= gen_mac_multicast(cstate
, 2);
7606 /* Link not known to support multicasts */
7610 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7611 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7616 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7617 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7621 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7627 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7628 * Outbound traffic is sent by this machine, while inbound traffic is
7629 * sent by a remote machine (and may include packets destined for a
7630 * unicast or multicast link-layer address we are not subscribing to).
7631 * These are the same definitions implemented by pcap_setdirection().
7632 * Capturing only unicast traffic destined for this host is probably
7633 * better accomplished using a higher-layer filter.
7636 gen_inbound(compiler_state_t
*cstate
, int dir
)
7638 register struct block
*b0
;
7641 * Only some data link types support inbound/outbound qualifiers.
7643 switch (cstate
->linktype
) {
7645 b0
= gen_relation(cstate
, BPF_JEQ
,
7646 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7647 gen_loadi(cstate
, 0),
7653 /* match outgoing packets */
7654 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7656 /* match incoming packets */
7657 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7662 /* match outgoing packets */
7663 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7665 /* to filter on inbound traffic, invert the match */
7670 #ifdef HAVE_NET_PFVAR_H
7672 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7673 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7679 /* match outgoing packets */
7680 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7682 /* match incoming packets */
7683 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7687 case DLT_JUNIPER_MFR
:
7688 case DLT_JUNIPER_MLFR
:
7689 case DLT_JUNIPER_MLPPP
:
7690 case DLT_JUNIPER_ATM1
:
7691 case DLT_JUNIPER_ATM2
:
7692 case DLT_JUNIPER_PPPOE
:
7693 case DLT_JUNIPER_PPPOE_ATM
:
7694 case DLT_JUNIPER_GGSN
:
7695 case DLT_JUNIPER_ES
:
7696 case DLT_JUNIPER_MONITOR
:
7697 case DLT_JUNIPER_SERVICES
:
7698 case DLT_JUNIPER_ETHER
:
7699 case DLT_JUNIPER_PPP
:
7700 case DLT_JUNIPER_FRELAY
:
7701 case DLT_JUNIPER_CHDLC
:
7702 case DLT_JUNIPER_VP
:
7703 case DLT_JUNIPER_ST
:
7704 case DLT_JUNIPER_ISM
:
7705 case DLT_JUNIPER_VS
:
7706 case DLT_JUNIPER_SRX_E2E
:
7707 case DLT_JUNIPER_FIBRECHANNEL
:
7708 case DLT_JUNIPER_ATM_CEMIC
:
7710 /* juniper flags (including direction) are stored
7711 * the byte after the 3-byte magic number */
7713 /* match outgoing packets */
7714 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7716 /* match incoming packets */
7717 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7723 * If we have packet meta-data indicating a direction,
7724 * check it, otherwise give up as this link-layer type
7725 * has nothing in the packet data.
7727 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7729 * This is Linux with PF_PACKET support.
7730 * If this is a *live* capture, we can look at
7731 * special meta-data in the filter expression;
7732 * if it's a savefile, we can't.
7734 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7735 /* We have a FILE *, so this is a savefile */
7736 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
7741 /* match outgoing packets */
7742 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7745 /* to filter on inbound traffic, invert the match */
7748 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7749 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
7753 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7758 #ifdef HAVE_NET_PFVAR_H
7759 /* PF firewall log matched interface */
7761 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7766 if (cstate
->linktype
!= DLT_PFLOG
) {
7767 bpf_error(cstate
, "ifname supported only on PF linktype");
7770 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7771 off
= offsetof(struct pfloghdr
, ifname
);
7772 if (strlen(ifname
) >= len
) {
7773 bpf_error(cstate
, "ifname interface names can only be %d characters",
7777 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7781 /* PF firewall log ruleset name */
7783 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7787 if (cstate
->linktype
!= DLT_PFLOG
) {
7788 bpf_error(cstate
, "ruleset supported only on PF linktype");
7792 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7793 bpf_error(cstate
, "ruleset names can only be %ld characters",
7794 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7798 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7799 strlen(ruleset
), (const u_char
*)ruleset
);
7803 /* PF firewall log rule number */
7805 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7809 if (cstate
->linktype
!= DLT_PFLOG
) {
7810 bpf_error(cstate
, "rnr supported only on PF linktype");
7814 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7819 /* PF firewall log sub-rule number */
7821 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7825 if (cstate
->linktype
!= DLT_PFLOG
) {
7826 bpf_error(cstate
, "srnr supported only on PF linktype");
7830 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7835 /* PF firewall log reason code */
7837 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7841 if (cstate
->linktype
!= DLT_PFLOG
) {
7842 bpf_error(cstate
, "reason supported only on PF linktype");
7846 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7851 /* PF firewall log action */
7853 gen_pf_action(compiler_state_t
*cstate
, int action
)
7857 if (cstate
->linktype
!= DLT_PFLOG
) {
7858 bpf_error(cstate
, "action supported only on PF linktype");
7862 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7866 #else /* !HAVE_NET_PFVAR_H */
7868 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7870 bpf_error(cstate
, "libpcap was compiled without pf support");
7876 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7878 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7884 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7886 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7892 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7894 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7900 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7902 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7908 gen_pf_action(compiler_state_t
*cstate
, int action
)
7910 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7914 #endif /* HAVE_NET_PFVAR_H */
7916 /* IEEE 802.11 wireless header */
7918 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
7922 switch (cstate
->linktype
) {
7924 case DLT_IEEE802_11
:
7925 case DLT_PRISM_HEADER
:
7926 case DLT_IEEE802_11_RADIO_AVS
:
7927 case DLT_IEEE802_11_RADIO
:
7928 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7933 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
7941 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
7945 switch (cstate
->linktype
) {
7947 case DLT_IEEE802_11
:
7948 case DLT_PRISM_HEADER
:
7949 case DLT_IEEE802_11_RADIO_AVS
:
7950 case DLT_IEEE802_11_RADIO
:
7954 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
7958 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7959 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7965 gen_acode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
7967 switch (cstate
->linktype
) {
7970 case DLT_ARCNET_LINUX
:
7971 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7973 return (gen_ahostop(cstate
, eaddr
, (int)q
.dir
));
7975 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7981 bpf_error(cstate
, "aid supported only on ARCnet");
7984 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7989 static struct block
*
7990 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
7992 register struct block
*b0
, *b1
;
7995 /* src comes first, different from Ethernet */
7997 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8000 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8003 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8004 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8010 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8011 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8016 bpf_error(cstate
, "'addr1' is only supported on 802.11");
8020 bpf_error(cstate
, "'addr2' is only supported on 802.11");
8024 bpf_error(cstate
, "'addr3' is only supported on 802.11");
8028 bpf_error(cstate
, "'addr4' is only supported on 802.11");
8032 bpf_error(cstate
, "'ra' is only supported on 802.11");
8036 bpf_error(cstate
, "'ta' is only supported on 802.11");
8043 static struct block
*
8044 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8046 struct block
*b0
, *b1
;
8048 /* check for VLAN, including QinQ */
8049 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8050 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8053 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8059 static struct block
*
8060 gen_vlan_vid_test(compiler_state_t
*cstate
, int vlan_num
)
8062 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8065 static struct block
*
8066 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8068 struct block
*b0
, *b1
;
8070 b0
= gen_vlan_tpid_test(cstate
);
8072 if (vlan_num
>= 0) {
8073 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8079 * Both payload and link header type follow the VLAN tags so that
8080 * both need to be updated.
8082 cstate
->off_linkpl
.constant_part
+= 4;
8083 cstate
->off_linktype
.constant_part
+= 4;
8088 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8089 /* add v to variable part of off */
8091 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8095 if (!off
->is_variable
)
8096 off
->is_variable
= 1;
8098 off
->reg
= alloc_reg(cstate
);
8100 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8103 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8106 s2
= new_stmt(cstate
, BPF_ST
);
8112 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8113 * and link type offsets first
8116 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8120 /* offset determined at run time, shift variable part */
8122 cstate
->is_vlan_vloffset
= 1;
8123 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8124 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8126 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8127 sappend(s
.next
, b_tpid
->head
->stmts
);
8128 b_tpid
->head
->stmts
= s
.next
;
8132 * patch block b_vid (VLAN id test) to load VID value either from packet
8133 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8136 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8138 struct slist
*s
, *s2
, *sjeq
;
8141 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8142 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8144 /* true -> next instructions, false -> beginning of b_vid */
8145 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8147 sjeq
->s
.jf
= b_vid
->stmts
;
8150 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8151 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8155 /* jump to the test in b_vid (bypass loading VID from packet data) */
8157 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8159 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8163 /* insert our statements at the beginning of b_vid */
8164 sappend(s
, b_vid
->stmts
);
8169 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8170 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8171 * tag can be either in metadata or in packet data; therefore if the
8172 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8173 * header for VLAN tag. As the decision is done at run time, we need
8174 * update variable part of the offsets
8176 static struct block
*
8177 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8179 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8182 /* generate new filter code based on extracting packet
8184 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8185 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8187 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8192 * This is tricky. We need to insert the statements updating variable
8193 * parts of offsets before the the traditional TPID and VID tests so
8194 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8195 * we do not want this update to affect those checks. That's why we
8196 * generate both test blocks first and insert the statements updating
8197 * variable parts of both offsets after that. This wouldn't work if
8198 * there already were variable length link header when entering this
8199 * function but gen_vlan_bpf_extensions() isn't called in that case.
8201 b_tpid
= gen_vlan_tpid_test(cstate
);
8203 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8205 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8209 if (vlan_num
>= 0) {
8210 gen_vlan_patch_vid_test(cstate
, b_vid
);
8220 * support IEEE 802.1Q VLAN trunk over ethernet
8223 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
8227 /* can't check for VLAN-encapsulated packets inside MPLS */
8228 if (cstate
->label_stack_depth
> 0)
8229 bpf_error(cstate
, "no VLAN match after MPLS");
8232 * Check for a VLAN packet, and then change the offsets to point
8233 * to the type and data fields within the VLAN packet. Just
8234 * increment the offsets, so that we can support a hierarchy, e.g.
8235 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8238 * XXX - this is a bit of a kludge. If we were to split the
8239 * compiler into a parser that parses an expression and
8240 * generates an expression tree, and a code generator that
8241 * takes an expression tree (which could come from our
8242 * parser or from some other parser) and generates BPF code,
8243 * we could perhaps make the offsets parameters of routines
8244 * and, in the handler for an "AND" node, pass to subnodes
8245 * other than the VLAN node the adjusted offsets.
8247 * This would mean that "vlan" would, instead of changing the
8248 * behavior of *all* tests after it, change only the behavior
8249 * of tests ANDed with it. That would change the documented
8250 * semantics of "vlan", which might break some expressions.
8251 * However, it would mean that "(vlan and ip) or ip" would check
8252 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8253 * checking only for VLAN-encapsulated IP, so that could still
8254 * be considered worth doing; it wouldn't break expressions
8255 * that are of the form "vlan and ..." or "vlan N and ...",
8256 * which I suspect are the most common expressions involving
8257 * "vlan". "vlan or ..." doesn't necessarily do what the user
8258 * would really want, now, as all the "or ..." tests would
8259 * be done assuming a VLAN, even though the "or" could be viewed
8260 * as meaning "or, if this isn't a VLAN packet...".
8262 switch (cstate
->linktype
) {
8265 case DLT_NETANALYZER
:
8266 case DLT_NETANALYZER_TRANSPARENT
:
8267 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8268 /* Verify that this is the outer part of the packet and
8269 * not encapsulated somehow. */
8270 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8271 cstate
->off_linkhdr
.constant_part
==
8272 cstate
->off_outermostlinkhdr
.constant_part
) {
8274 * Do we need special VLAN handling?
8276 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8277 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8279 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8282 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8285 case DLT_IEEE802_11
:
8286 case DLT_PRISM_HEADER
:
8287 case DLT_IEEE802_11_RADIO_AVS
:
8288 case DLT_IEEE802_11_RADIO
:
8289 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8293 bpf_error(cstate
, "no VLAN support for data link type %d",
8298 cstate
->vlan_stack_depth
++;
8307 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8309 struct block
*b0
, *b1
;
8311 if (cstate
->label_stack_depth
> 0) {
8312 /* just match the bottom-of-stack bit clear */
8313 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8316 * We're not in an MPLS stack yet, so check the link-layer
8317 * type against MPLS.
8319 switch (cstate
->linktype
) {
8321 case DLT_C_HDLC
: /* fall through */
8323 case DLT_NETANALYZER
:
8324 case DLT_NETANALYZER_TRANSPARENT
:
8325 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8329 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8332 /* FIXME add other DLT_s ...
8333 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8334 * leave it for now */
8337 bpf_error(cstate
, "no MPLS support for data link type %d",
8345 /* If a specific MPLS label is requested, check it */
8346 if (label_num
>= 0) {
8347 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8348 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8349 0xfffff000); /* only compare the first 20 bits */
8355 * Change the offsets to point to the type and data fields within
8356 * the MPLS packet. Just increment the offsets, so that we
8357 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8358 * capture packets with an outer label of 100000 and an inner
8361 * Increment the MPLS stack depth as well; this indicates that
8362 * we're checking MPLS-encapsulated headers, to make sure higher
8363 * level code generators don't try to match against IP-related
8364 * protocols such as Q_ARP, Q_RARP etc.
8366 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8368 cstate
->off_nl_nosnap
+= 4;
8369 cstate
->off_nl
+= 4;
8370 cstate
->label_stack_depth
++;
8375 * Support PPPOE discovery and session.
8378 gen_pppoed(compiler_state_t
*cstate
)
8380 /* check for PPPoE discovery */
8381 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8385 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8387 struct block
*b0
, *b1
;
8390 * Test against the PPPoE session link-layer type.
8392 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8394 /* If a specific session is requested, check PPPoE session id */
8395 if (sess_num
>= 0) {
8396 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8397 (bpf_int32
)sess_num
, 0x0000ffff);
8403 * Change the offsets to point to the type and data fields within
8404 * the PPP packet, and note that this is PPPoE rather than
8407 * XXX - this is a bit of a kludge. If we were to split the
8408 * compiler into a parser that parses an expression and
8409 * generates an expression tree, and a code generator that
8410 * takes an expression tree (which could come from our
8411 * parser or from some other parser) and generates BPF code,
8412 * we could perhaps make the offsets parameters of routines
8413 * and, in the handler for an "AND" node, pass to subnodes
8414 * other than the PPPoE node the adjusted offsets.
8416 * This would mean that "pppoes" would, instead of changing the
8417 * behavior of *all* tests after it, change only the behavior
8418 * of tests ANDed with it. That would change the documented
8419 * semantics of "pppoes", which might break some expressions.
8420 * However, it would mean that "(pppoes and ip) or ip" would check
8421 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8422 * checking only for VLAN-encapsulated IP, so that could still
8423 * be considered worth doing; it wouldn't break expressions
8424 * that are of the form "pppoes and ..." which I suspect are the
8425 * most common expressions involving "pppoes". "pppoes or ..."
8426 * doesn't necessarily do what the user would really want, now,
8427 * as all the "or ..." tests would be done assuming PPPoE, even
8428 * though the "or" could be viewed as meaning "or, if this isn't
8429 * a PPPoE packet...".
8431 * The "network-layer" protocol is PPPoE, which has a 6-byte
8432 * PPPoE header, followed by a PPP packet.
8434 * There is no HDLC encapsulation for the PPP packet (it's
8435 * encapsulated in PPPoES instead), so the link-layer type
8436 * starts at the first byte of the PPP packet. For PPPoE,
8437 * that offset is relative to the beginning of the total
8438 * link-layer payload, including any 802.2 LLC header, so
8439 * it's 6 bytes past cstate->off_nl.
8441 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8442 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8443 cstate
->off_linkpl
.reg
);
8445 cstate
->off_linktype
= cstate
->off_linkhdr
;
8446 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8449 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8454 /* Check that this is Geneve and the VNI is correct if
8455 * specified. Parameterized to handle both IPv4 and IPv6. */
8456 static struct block
*
8457 gen_geneve_check(compiler_state_t
*cstate
,
8458 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8459 enum e_offrel offrel
, int vni
)
8461 struct block
*b0
, *b1
;
8463 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8465 /* Check that we are operating on version 0. Otherwise, we
8466 * can't decode the rest of the fields. The version is 2 bits
8467 * in the first byte of the Geneve header. */
8468 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8473 vni
<<= 8; /* VNI is in the upper 3 bytes */
8474 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8483 /* The IPv4 and IPv6 Geneve checks need to do two things:
8484 * - Verify that this actually is Geneve with the right VNI.
8485 * - Place the IP header length (plus variable link prefix if
8486 * needed) into register A to be used later to compute
8487 * the inner packet offsets. */
8488 static struct block
*
8489 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8491 struct block
*b0
, *b1
;
8492 struct slist
*s
, *s1
;
8494 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8496 /* Load the IP header length into A. */
8497 s
= gen_loadx_iphdrlen(cstate
);
8499 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8502 /* Forcibly append these statements to the true condition
8503 * of the protocol check by creating a new block that is
8504 * always true and ANDing them. */
8505 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8514 static struct block
*
8515 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8517 struct block
*b0
, *b1
;
8518 struct slist
*s
, *s1
;
8520 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8522 /* Load the IP header length. We need to account for a
8523 * variable length link prefix if there is one. */
8524 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8526 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8530 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8534 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8538 /* Forcibly append these statements to the true condition
8539 * of the protocol check by creating a new block that is
8540 * always true and ANDing them. */
8541 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8544 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8553 /* We need to store three values based on the Geneve header::
8554 * - The offset of the linktype.
8555 * - The offset of the end of the Geneve header.
8556 * - The offset of the end of the encapsulated MAC header. */
8557 static struct slist
*
8558 gen_geneve_offsets(compiler_state_t
*cstate
)
8560 struct slist
*s
, *s1
, *s_proto
;
8562 /* First we need to calculate the offset of the Geneve header
8563 * itself. This is composed of the IP header previously calculated
8564 * (include any variable link prefix) and stored in A plus the
8565 * fixed sized headers (fixed link prefix, MAC length, and UDP
8567 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8568 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8570 /* Stash this in X since we'll need it later. */
8571 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8574 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8576 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8580 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8581 cstate
->off_linktype
.is_variable
= 1;
8582 cstate
->off_linktype
.constant_part
= 0;
8584 s1
= new_stmt(cstate
, BPF_ST
);
8585 s1
->s
.k
= cstate
->off_linktype
.reg
;
8588 /* Load the Geneve option length and mask and shift to get the
8589 * number of bytes. It is stored in the first byte of the Geneve
8591 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8595 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8599 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8603 /* Add in the rest of the Geneve base header. */
8604 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8608 /* Add the Geneve header length to its offset and store. */
8609 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8613 /* Set the encapsulated type as Ethernet. Even though we may
8614 * not actually have Ethernet inside there are two reasons this
8616 * - The linktype field is always in EtherType format regardless
8617 * of whether it is in Geneve or an inner Ethernet frame.
8618 * - The only link layer that we have specific support for is
8619 * Ethernet. We will confirm that the packet actually is
8620 * Ethernet at runtime before executing these checks. */
8621 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8623 s1
= new_stmt(cstate
, BPF_ST
);
8624 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8627 /* Calculate whether we have an Ethernet header or just raw IP/
8628 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8629 * and linktype by 14 bytes so that the network header can be found
8630 * seamlessly. Otherwise, keep what we've calculated already. */
8632 /* We have a bare jmp so we can't use the optimizer. */
8633 cstate
->no_optimize
= 1;
8635 /* Load the EtherType in the Geneve header, 2 bytes in. */
8636 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8640 /* Load X with the end of the Geneve header. */
8641 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8642 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8645 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8646 * end of this check, we should have the total length in X. In
8647 * the non-Ethernet case, it's already there. */
8648 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8649 s_proto
->s
.k
= ETHERTYPE_TEB
;
8650 sappend(s
, s_proto
);
8652 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8656 /* Since this is Ethernet, use the EtherType of the payload
8657 * directly as the linktype. Overwrite what we already have. */
8658 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8662 s1
= new_stmt(cstate
, BPF_ST
);
8663 s1
->s
.k
= cstate
->off_linktype
.reg
;
8666 /* Advance two bytes further to get the end of the Ethernet
8668 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8672 /* Move the result to X. */
8673 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8676 /* Store the final result of our linkpl calculation. */
8677 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8678 cstate
->off_linkpl
.is_variable
= 1;
8679 cstate
->off_linkpl
.constant_part
= 0;
8681 s1
= new_stmt(cstate
, BPF_STX
);
8682 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8691 /* Check to see if this is a Geneve packet. */
8693 gen_geneve(compiler_state_t
*cstate
, int vni
)
8695 struct block
*b0
, *b1
;
8698 b0
= gen_geneve4(cstate
, vni
);
8699 b1
= gen_geneve6(cstate
, vni
);
8704 /* Later filters should act on the payload of the Geneve frame,
8705 * update all of the header pointers. Attach this code so that
8706 * it gets executed in the event that the Geneve filter matches. */
8707 s
= gen_geneve_offsets(cstate
);
8709 b1
= gen_true(cstate
);
8710 sappend(s
, b1
->stmts
);
8715 cstate
->is_geneve
= 1;
8720 /* Check that the encapsulated frame has a link layer header
8721 * for Ethernet filters. */
8722 static struct block
*
8723 gen_geneve_ll_check(compiler_state_t
*cstate
)
8726 struct slist
*s
, *s1
;
8728 /* The easiest way to see if there is a link layer present
8729 * is to check if the link layer header and payload are not
8732 /* Geneve always generates pure variable offsets so we can
8733 * compare only the registers. */
8734 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8735 s
->s
.k
= cstate
->off_linkhdr
.reg
;
8737 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8738 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8741 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8750 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
8751 bpf_u_int32 jtype
, int reverse
)
8758 if (!cstate
->is_atm
)
8759 bpf_error(cstate
, "'vpi' supported only on raw ATM");
8760 if (cstate
->off_vpi
== (u_int
)-1)
8762 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
8767 if (!cstate
->is_atm
)
8768 bpf_error(cstate
, "'vci' supported only on raw ATM");
8769 if (cstate
->off_vci
== (u_int
)-1)
8771 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
8776 if (cstate
->off_proto
== (u_int
)-1)
8777 abort(); /* XXX - this isn't on FreeBSD */
8778 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
8783 if (cstate
->off_payload
== (u_int
)-1)
8785 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
8786 0xffffffff, jtype
, reverse
, jvalue
);
8790 if (!cstate
->is_atm
)
8791 bpf_error(cstate
, "'callref' supported only on raw ATM");
8792 if (cstate
->off_proto
== (u_int
)-1)
8794 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
8795 jtype
, reverse
, jvalue
);
8805 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
8807 struct block
*b0
, *b1
;
8812 /* Get all packets in Meta signalling Circuit */
8813 if (!cstate
->is_atm
)
8814 bpf_error(cstate
, "'metac' supported only on raw ATM");
8815 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8816 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
8821 /* Get all packets in Broadcast Circuit*/
8822 if (!cstate
->is_atm
)
8823 bpf_error(cstate
, "'bcc' supported only on raw ATM");
8824 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8825 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
8830 /* Get all cells in Segment OAM F4 circuit*/
8831 if (!cstate
->is_atm
)
8832 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
8833 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8834 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8839 /* Get all cells in End-to-End OAM F4 Circuit*/
8840 if (!cstate
->is_atm
)
8841 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
8842 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8843 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8848 /* Get all packets in connection Signalling Circuit */
8849 if (!cstate
->is_atm
)
8850 bpf_error(cstate
, "'sc' supported only on raw ATM");
8851 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8852 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
8857 /* Get all packets in ILMI Circuit */
8858 if (!cstate
->is_atm
)
8859 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
8860 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8861 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
8866 /* Get all LANE packets */
8867 if (!cstate
->is_atm
)
8868 bpf_error(cstate
, "'lane' supported only on raw ATM");
8869 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8872 * Arrange that all subsequent tests assume LANE
8873 * rather than LLC-encapsulated packets, and set
8874 * the offsets appropriately for LANE-encapsulated
8877 * We assume LANE means Ethernet, not Token Ring.
8879 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
8880 cstate
->off_payload
+ 2, /* Ethernet header */
8882 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
8883 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
8884 cstate
->off_nl
= 0; /* Ethernet II */
8885 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
8889 /* Get all LLC-encapsulated packets */
8890 if (!cstate
->is_atm
)
8891 bpf_error(cstate
, "'llc' supported only on raw ATM");
8892 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8893 cstate
->linktype
= cstate
->prevlinktype
;
8903 * Filtering for MTP2 messages based on li value
8904 * FISU, length is null
8905 * LSSU, length is 1 or 2
8906 * MSU, length is 3 or more
8907 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8910 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
8912 struct block
*b0
, *b1
;
8917 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8918 (cstate
->linktype
!= DLT_ERF
) &&
8919 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8920 bpf_error(cstate
, "'fisu' supported only on MTP2");
8921 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8922 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8926 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8927 (cstate
->linktype
!= DLT_ERF
) &&
8928 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8929 bpf_error(cstate
, "'lssu' supported only on MTP2");
8930 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8931 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8936 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8937 (cstate
->linktype
!= DLT_ERF
) &&
8938 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8939 bpf_error(cstate
, "'msu' supported only on MTP2");
8940 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8944 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8945 (cstate
->linktype
!= DLT_ERF
) &&
8946 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8947 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
8948 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8949 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8953 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8954 (cstate
->linktype
!= DLT_ERF
) &&
8955 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8956 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
8957 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8958 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8963 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8964 (cstate
->linktype
!= DLT_ERF
) &&
8965 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8966 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
8967 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8977 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
8978 bpf_u_int32 jtype
, int reverse
)
8981 bpf_u_int32 val1
, val2
, val3
;
8982 u_int newoff_sio
= cstate
->off_sio
;
8983 u_int newoff_opc
= cstate
->off_opc
;
8984 u_int newoff_dpc
= cstate
->off_dpc
;
8985 u_int newoff_sls
= cstate
->off_sls
;
8987 switch (mtp3field
) {
8990 newoff_sio
+= 3; /* offset for MTP2_HSL */
8994 if (cstate
->off_sio
== (u_int
)-1)
8995 bpf_error(cstate
, "'sio' supported only on SS7");
8996 /* sio coded on 1 byte so max value 255 */
8998 bpf_error(cstate
, "sio value %u too big; max value = 255",
9000 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9001 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9007 if (cstate
->off_opc
== (u_int
)-1)
9008 bpf_error(cstate
, "'opc' supported only on SS7");
9009 /* opc coded on 14 bits so max value 16383 */
9011 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9013 /* the following instructions are made to convert jvalue
9014 * to the form used to write opc in an ss7 message*/
9015 val1
= jvalue
& 0x00003c00;
9017 val2
= jvalue
& 0x000003fc;
9019 val3
= jvalue
& 0x00000003;
9021 jvalue
= val1
+ val2
+ val3
;
9022 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9023 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9031 if (cstate
->off_dpc
== (u_int
)-1)
9032 bpf_error(cstate
, "'dpc' supported only on SS7");
9033 /* dpc coded on 14 bits so max value 16383 */
9035 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9037 /* the following instructions are made to convert jvalue
9038 * to the forme used to write dpc in an ss7 message*/
9039 val1
= jvalue
& 0x000000ff;
9041 val2
= jvalue
& 0x00003f00;
9043 jvalue
= val1
+ val2
;
9044 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9045 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9051 if (cstate
->off_sls
== (u_int
)-1)
9052 bpf_error(cstate
, "'sls' supported only on SS7");
9053 /* sls coded on 4 bits so max value 15 */
9055 bpf_error(cstate
, "sls value %u too big; max value = 15",
9057 /* the following instruction is made to convert jvalue
9058 * to the forme used to write sls in an ss7 message*/
9059 jvalue
= jvalue
<< 4;
9060 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9061 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9070 static struct block
*
9071 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9076 * Q.2931 signalling protocol messages for handling virtual circuits
9077 * establishment and teardown
9082 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9086 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9090 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9094 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9098 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9101 case A_RELEASE_DONE
:
9102 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9112 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9114 struct block
*b0
, *b1
;
9119 if (!cstate
->is_atm
)
9120 bpf_error(cstate
, "'oam' supported only on raw ATM");
9121 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
9125 if (!cstate
->is_atm
)
9126 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9128 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9129 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9131 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9137 * Get Q.2931 signalling messages for switched
9138 * virtual connection
9140 if (!cstate
->is_atm
)
9141 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9142 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9143 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9145 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9147 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9149 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9151 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9153 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9158 if (!cstate
->is_atm
)
9159 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9160 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9161 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9163 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9165 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9167 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9169 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);