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 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
738 errno
, "can't initialize scanner");
739 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
742 * Associate the compiler state with the lexical analyzer
745 pcap_set_extra(&cstate
, scanner
);
747 init_linktype(&cstate
, p
);
748 (void)pcap_parse(scanner
, &cstate
);
750 if (cstate
.ic
.root
== NULL
)
751 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
753 if (optimize
&& !cstate
.no_optimize
) {
754 bpf_optimize(&cstate
, &cstate
.ic
);
755 if (cstate
.ic
.root
== NULL
||
756 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
757 bpf_error(&cstate
, "expression rejects all packets");
759 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
760 program
->bf_len
= len
;
762 rc
= 0; /* We're all okay */
766 * Clean up everything for the lexical analyzer.
768 if (in_buffer
!= NULL
)
769 pcap__delete_buffer(in_buffer
, scanner
);
771 pcap_lex_destroy(scanner
);
774 * Clean up our own allocated memory.
782 * entry point for using the compiler with no pcap open
783 * pass in all the stuff that is needed explicitly instead.
786 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
787 struct bpf_program
*program
,
788 const char *buf
, int optimize
, bpf_u_int32 mask
)
793 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
796 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
802 * Clean up a "struct bpf_program" by freeing all the memory allocated
806 pcap_freecode(struct bpf_program
*program
)
809 if (program
->bf_insns
!= NULL
) {
810 free((char *)program
->bf_insns
);
811 program
->bf_insns
= NULL
;
816 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
817 * which of the jt and jf fields has been resolved and which is a pointer
818 * back to another unresolved block (or nil). At least one of the fields
819 * in each block is already resolved.
822 backpatch(list
, target
)
823 struct block
*list
, *target
;
840 * Merge the lists in b0 and b1, using the 'sense' field to indicate
841 * which of jt and jf is the link.
845 struct block
*b0
, *b1
;
847 register struct block
**p
= &b0
;
849 /* Find end of list. */
851 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
853 /* Concatenate the lists. */
858 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
860 struct block
*ppi_dlt_check
;
863 * Insert before the statements of the first (root) block any
864 * statements needed to load the lengths of any variable-length
865 * headers into registers.
867 * XXX - a fancier strategy would be to insert those before the
868 * statements of all blocks that use those lengths and that
869 * have no predecessors that use them, so that we only compute
870 * the lengths if we need them. There might be even better
871 * approaches than that.
873 * However, those strategies would be more complicated, and
874 * as we don't generate code to compute a length if the
875 * program has no tests that use the length, and as most
876 * tests will probably use those lengths, we would just
877 * postpone computing the lengths so that it's not done
878 * for tests that fail early, and it's not clear that's
881 insert_compute_vloffsets(cstate
, p
->head
);
884 * For DLT_PPI captures, generate a check of the per-packet
885 * DLT value to make sure it's DLT_IEEE802_11.
887 * XXX - TurboCap cards use DLT_PPI for Ethernet.
888 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
889 * with appropriate Ethernet information and use that rather
890 * than using something such as DLT_PPI where you don't know
891 * the link-layer header type until runtime, which, in the
892 * general case, would force us to generate both Ethernet *and*
893 * 802.11 code (*and* anything else for which PPI is used)
894 * and choose between them early in the BPF program?
896 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
897 if (ppi_dlt_check
!= NULL
)
898 gen_and(ppi_dlt_check
, p
);
900 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
901 p
->sense
= !p
->sense
;
902 backpatch(p
, gen_retblk(cstate
, 0));
903 cstate
->ic
.root
= p
->head
;
908 struct block
*b0
, *b1
;
910 backpatch(b0
, b1
->head
);
911 b0
->sense
= !b0
->sense
;
912 b1
->sense
= !b1
->sense
;
914 b1
->sense
= !b1
->sense
;
920 struct block
*b0
, *b1
;
922 b0
->sense
= !b0
->sense
;
923 backpatch(b0
, b1
->head
);
924 b0
->sense
= !b0
->sense
;
933 b
->sense
= !b
->sense
;
936 static struct block
*
937 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
938 u_int size
, bpf_int32 v
)
940 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
943 static struct block
*
944 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
945 u_int size
, bpf_int32 v
)
947 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
950 static struct block
*
951 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
952 u_int size
, bpf_int32 v
)
954 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
957 static struct block
*
958 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
959 u_int size
, bpf_int32 v
)
961 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
964 static struct block
*
965 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
966 u_int size
, bpf_int32 v
)
968 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
971 static struct block
*
972 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
973 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
975 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
978 static struct block
*
979 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
980 u_int size
, const u_char
*v
)
982 register struct block
*b
, *tmp
;
986 register const u_char
*p
= &v
[size
- 4];
987 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
988 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
990 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
, w
);
997 register const u_char
*p
= &v
[size
- 2];
998 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
1000 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
, w
);
1007 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1016 * AND the field of size "size" at offset "offset" relative to the header
1017 * specified by "offrel" with "mask", and compare it with the value "v"
1018 * with the test specified by "jtype"; if "reverse" is true, the test
1019 * should test the opposite of "jtype".
1021 static struct block
*
1022 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1023 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1026 struct slist
*s
, *s2
;
1029 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1031 if (mask
!= 0xffffffff) {
1032 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1037 b
= new_block(cstate
, JMP(jtype
));
1040 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1046 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1048 cstate
->pcap_fddipad
= p
->fddipad
;
1051 * We start out with only one link-layer header.
1053 cstate
->outermostlinktype
= pcap_datalink(p
);
1054 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1055 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1056 cstate
->off_outermostlinkhdr
.reg
= -1;
1058 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1059 cstate
->off_prevlinkhdr
.constant_part
= 0;
1060 cstate
->off_prevlinkhdr
.is_variable
= 0;
1061 cstate
->off_prevlinkhdr
.reg
= -1;
1063 cstate
->linktype
= cstate
->outermostlinktype
;
1064 cstate
->off_linkhdr
.constant_part
= 0;
1065 cstate
->off_linkhdr
.is_variable
= 0;
1066 cstate
->off_linkhdr
.reg
= -1;
1071 cstate
->off_linkpl
.constant_part
= 0;
1072 cstate
->off_linkpl
.is_variable
= 0;
1073 cstate
->off_linkpl
.reg
= -1;
1075 cstate
->off_linktype
.constant_part
= 0;
1076 cstate
->off_linktype
.is_variable
= 0;
1077 cstate
->off_linktype
.reg
= -1;
1080 * Assume it's not raw ATM with a pseudo-header, for now.
1083 cstate
->off_vpi
= -1;
1084 cstate
->off_vci
= -1;
1085 cstate
->off_proto
= -1;
1086 cstate
->off_payload
= -1;
1091 cstate
->is_geneve
= 0;
1094 * No variable length VLAN offset by default
1096 cstate
->is_vlan_vloffset
= 0;
1099 * And assume we're not doing SS7.
1101 cstate
->off_li
= -1;
1102 cstate
->off_li_hsl
= -1;
1103 cstate
->off_sio
= -1;
1104 cstate
->off_opc
= -1;
1105 cstate
->off_dpc
= -1;
1106 cstate
->off_sls
= -1;
1108 cstate
->label_stack_depth
= 0;
1109 cstate
->vlan_stack_depth
= 0;
1111 switch (cstate
->linktype
) {
1114 cstate
->off_linktype
.constant_part
= 2;
1115 cstate
->off_linkpl
.constant_part
= 6;
1116 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1117 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1120 case DLT_ARCNET_LINUX
:
1121 cstate
->off_linktype
.constant_part
= 4;
1122 cstate
->off_linkpl
.constant_part
= 8;
1123 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1124 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1128 cstate
->off_linktype
.constant_part
= 12;
1129 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1130 cstate
->off_nl
= 0; /* Ethernet II */
1131 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1136 * SLIP doesn't have a link level type. The 16 byte
1137 * header is hacked into our SLIP driver.
1139 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1140 cstate
->off_linkpl
.constant_part
= 16;
1142 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1145 case DLT_SLIP_BSDOS
:
1146 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1147 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1149 cstate
->off_linkpl
.constant_part
= 24;
1151 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1156 cstate
->off_linktype
.constant_part
= 0;
1157 cstate
->off_linkpl
.constant_part
= 4;
1159 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1163 cstate
->off_linktype
.constant_part
= 0;
1164 cstate
->off_linkpl
.constant_part
= 12;
1166 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1171 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1172 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1173 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1174 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1176 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1181 * This does no include the Ethernet header, and
1182 * only covers session state.
1184 cstate
->off_linktype
.constant_part
= 6;
1185 cstate
->off_linkpl
.constant_part
= 8;
1187 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1191 cstate
->off_linktype
.constant_part
= 5;
1192 cstate
->off_linkpl
.constant_part
= 24;
1194 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1199 * FDDI doesn't really have a link-level type field.
1200 * We set "off_linktype" to the offset of the LLC header.
1202 * To check for Ethernet types, we assume that SSAP = SNAP
1203 * is being used and pick out the encapsulated Ethernet type.
1204 * XXX - should we generate code to check for SNAP?
1206 cstate
->off_linktype
.constant_part
= 13;
1207 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1208 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1209 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1210 cstate
->off_nl
= 8; /* 802.2+SNAP */
1211 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1216 * Token Ring doesn't really have a link-level type field.
1217 * We set "off_linktype" to the offset of the LLC header.
1219 * To check for Ethernet types, we assume that SSAP = SNAP
1220 * is being used and pick out the encapsulated Ethernet type.
1221 * XXX - should we generate code to check for SNAP?
1223 * XXX - the header is actually variable-length.
1224 * Some various Linux patched versions gave 38
1225 * as "off_linktype" and 40 as "off_nl"; however,
1226 * if a token ring packet has *no* routing
1227 * information, i.e. is not source-routed, the correct
1228 * values are 20 and 22, as they are in the vanilla code.
1230 * A packet is source-routed iff the uppermost bit
1231 * of the first byte of the source address, at an
1232 * offset of 8, has the uppermost bit set. If the
1233 * packet is source-routed, the total number of bytes
1234 * of routing information is 2 plus bits 0x1F00 of
1235 * the 16-bit value at an offset of 14 (shifted right
1236 * 8 - figure out which byte that is).
1238 cstate
->off_linktype
.constant_part
= 14;
1239 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1240 cstate
->off_nl
= 8; /* 802.2+SNAP */
1241 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1244 case DLT_PRISM_HEADER
:
1245 case DLT_IEEE802_11_RADIO_AVS
:
1246 case DLT_IEEE802_11_RADIO
:
1247 cstate
->off_linkhdr
.is_variable
= 1;
1248 /* Fall through, 802.11 doesn't have a variable link
1249 * prefix but is otherwise the same. */
1251 case DLT_IEEE802_11
:
1253 * 802.11 doesn't really have a link-level type field.
1254 * We set "off_linktype.constant_part" to the offset of
1257 * To check for Ethernet types, we assume that SSAP = SNAP
1258 * is being used and pick out the encapsulated Ethernet type.
1259 * XXX - should we generate code to check for SNAP?
1261 * We also handle variable-length radio headers here.
1262 * The Prism header is in theory variable-length, but in
1263 * practice it's always 144 bytes long. However, some
1264 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1265 * sometimes or always supply an AVS header, so we
1266 * have to check whether the radio header is a Prism
1267 * header or an AVS header, so, in practice, it's
1270 cstate
->off_linktype
.constant_part
= 24;
1271 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1272 cstate
->off_linkpl
.is_variable
= 1;
1273 cstate
->off_nl
= 8; /* 802.2+SNAP */
1274 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1279 * At the moment we treat PPI the same way that we treat
1280 * normal Radiotap encoded packets. The difference is in
1281 * the function that generates the code at the beginning
1282 * to compute the header length. Since this code generator
1283 * of PPI supports bare 802.11 encapsulation only (i.e.
1284 * the encapsulated DLT should be DLT_IEEE802_11) we
1285 * generate code to check for this too.
1287 cstate
->off_linktype
.constant_part
= 24;
1288 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1289 cstate
->off_linkpl
.is_variable
= 1;
1290 cstate
->off_linkhdr
.is_variable
= 1;
1291 cstate
->off_nl
= 8; /* 802.2+SNAP */
1292 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1295 case DLT_ATM_RFC1483
:
1296 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1298 * assume routed, non-ISO PDUs
1299 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1301 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1302 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1303 * latter would presumably be treated the way PPPoE
1304 * should be, so you can do "pppoe and udp port 2049"
1305 * or "pppoa and tcp port 80" and have it check for
1306 * PPPo{A,E} and a PPP protocol of IP and....
1308 cstate
->off_linktype
.constant_part
= 0;
1309 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1310 cstate
->off_nl
= 8; /* 802.2+SNAP */
1311 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1316 * Full Frontal ATM; you get AALn PDUs with an ATM
1320 cstate
->off_vpi
= SUNATM_VPI_POS
;
1321 cstate
->off_vci
= SUNATM_VCI_POS
;
1322 cstate
->off_proto
= PROTO_POS
;
1323 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1324 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1325 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1326 cstate
->off_nl
= 8; /* 802.2+SNAP */
1327 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1333 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1334 cstate
->off_linkpl
.constant_part
= 0;
1336 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1339 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1340 cstate
->off_linktype
.constant_part
= 14;
1341 cstate
->off_linkpl
.constant_part
= 16;
1343 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1348 * LocalTalk does have a 1-byte type field in the LLAP header,
1349 * but really it just indicates whether there is a "short" or
1350 * "long" DDP packet following.
1352 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1353 cstate
->off_linkpl
.constant_part
= 0;
1355 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1358 case DLT_IP_OVER_FC
:
1360 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1361 * link-level type field. We set "off_linktype" to the
1362 * offset of the LLC header.
1364 * To check for Ethernet types, we assume that SSAP = SNAP
1365 * is being used and pick out the encapsulated Ethernet type.
1366 * XXX - should we generate code to check for SNAP? RFC
1367 * 2625 says SNAP should be used.
1369 cstate
->off_linktype
.constant_part
= 16;
1370 cstate
->off_linkpl
.constant_part
= 16;
1371 cstate
->off_nl
= 8; /* 802.2+SNAP */
1372 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1377 * XXX - we should set this to handle SNAP-encapsulated
1378 * frames (NLPID of 0x80).
1380 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1381 cstate
->off_linkpl
.constant_part
= 0;
1383 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1387 * the only BPF-interesting FRF.16 frames are non-control frames;
1388 * Frame Relay has a variable length link-layer
1389 * so lets start with offset 4 for now and increments later on (FIXME);
1392 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1393 cstate
->off_linkpl
.constant_part
= 0;
1395 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1398 case DLT_APPLE_IP_OVER_IEEE1394
:
1399 cstate
->off_linktype
.constant_part
= 16;
1400 cstate
->off_linkpl
.constant_part
= 18;
1402 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1405 case DLT_SYMANTEC_FIREWALL
:
1406 cstate
->off_linktype
.constant_part
= 6;
1407 cstate
->off_linkpl
.constant_part
= 44;
1408 cstate
->off_nl
= 0; /* Ethernet II */
1409 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1412 #ifdef HAVE_NET_PFVAR_H
1414 cstate
->off_linktype
.constant_part
= 0;
1415 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1417 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1421 case DLT_JUNIPER_MFR
:
1422 case DLT_JUNIPER_MLFR
:
1423 case DLT_JUNIPER_MLPPP
:
1424 case DLT_JUNIPER_PPP
:
1425 case DLT_JUNIPER_CHDLC
:
1426 case DLT_JUNIPER_FRELAY
:
1427 cstate
->off_linktype
.constant_part
= 4;
1428 cstate
->off_linkpl
.constant_part
= 4;
1430 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1433 case DLT_JUNIPER_ATM1
:
1434 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1435 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1437 cstate
->off_nl_nosnap
= 10;
1440 case DLT_JUNIPER_ATM2
:
1441 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1442 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1444 cstate
->off_nl_nosnap
= 10;
1447 /* frames captured on a Juniper PPPoE service PIC
1448 * contain raw ethernet frames */
1449 case DLT_JUNIPER_PPPOE
:
1450 case DLT_JUNIPER_ETHER
:
1451 cstate
->off_linkpl
.constant_part
= 14;
1452 cstate
->off_linktype
.constant_part
= 16;
1453 cstate
->off_nl
= 18; /* Ethernet II */
1454 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1457 case DLT_JUNIPER_PPPOE_ATM
:
1458 cstate
->off_linktype
.constant_part
= 4;
1459 cstate
->off_linkpl
.constant_part
= 6;
1461 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1464 case DLT_JUNIPER_GGSN
:
1465 cstate
->off_linktype
.constant_part
= 6;
1466 cstate
->off_linkpl
.constant_part
= 12;
1468 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1471 case DLT_JUNIPER_ES
:
1472 cstate
->off_linktype
.constant_part
= 6;
1473 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1474 cstate
->off_nl
= -1; /* not really a network layer but raw IP addresses */
1475 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1478 case DLT_JUNIPER_MONITOR
:
1479 cstate
->off_linktype
.constant_part
= 12;
1480 cstate
->off_linkpl
.constant_part
= 12;
1481 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1482 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1485 case DLT_BACNET_MS_TP
:
1486 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1487 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1488 cstate
->off_nl
= -1;
1489 cstate
->off_nl_nosnap
= -1;
1492 case DLT_JUNIPER_SERVICES
:
1493 cstate
->off_linktype
.constant_part
= 12;
1494 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1495 cstate
->off_nl
= -1; /* L3 proto location dep. on cookie type */
1496 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1499 case DLT_JUNIPER_VP
:
1500 cstate
->off_linktype
.constant_part
= 18;
1501 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1502 cstate
->off_nl
= -1;
1503 cstate
->off_nl_nosnap
= -1;
1506 case DLT_JUNIPER_ST
:
1507 cstate
->off_linktype
.constant_part
= 18;
1508 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1509 cstate
->off_nl
= -1;
1510 cstate
->off_nl_nosnap
= -1;
1513 case DLT_JUNIPER_ISM
:
1514 cstate
->off_linktype
.constant_part
= 8;
1515 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1516 cstate
->off_nl
= -1;
1517 cstate
->off_nl_nosnap
= -1;
1520 case DLT_JUNIPER_VS
:
1521 case DLT_JUNIPER_SRX_E2E
:
1522 case DLT_JUNIPER_FIBRECHANNEL
:
1523 case DLT_JUNIPER_ATM_CEMIC
:
1524 cstate
->off_linktype
.constant_part
= 8;
1525 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1526 cstate
->off_nl
= -1;
1527 cstate
->off_nl_nosnap
= -1;
1532 cstate
->off_li_hsl
= 4;
1533 cstate
->off_sio
= 3;
1534 cstate
->off_opc
= 4;
1535 cstate
->off_dpc
= 4;
1536 cstate
->off_sls
= 7;
1537 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1538 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1539 cstate
->off_nl
= -1;
1540 cstate
->off_nl_nosnap
= -1;
1543 case DLT_MTP2_WITH_PHDR
:
1545 cstate
->off_li_hsl
= 8;
1546 cstate
->off_sio
= 7;
1547 cstate
->off_opc
= 8;
1548 cstate
->off_dpc
= 8;
1549 cstate
->off_sls
= 11;
1550 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1551 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1552 cstate
->off_nl
= -1;
1553 cstate
->off_nl_nosnap
= -1;
1557 cstate
->off_li
= 22;
1558 cstate
->off_li_hsl
= 24;
1559 cstate
->off_sio
= 23;
1560 cstate
->off_opc
= 24;
1561 cstate
->off_dpc
= 24;
1562 cstate
->off_sls
= 27;
1563 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1564 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1565 cstate
->off_nl
= -1;
1566 cstate
->off_nl_nosnap
= -1;
1570 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1571 cstate
->off_linkpl
.constant_part
= 4;
1573 cstate
->off_nl_nosnap
= 0;
1578 * Currently, only raw "link[N:M]" filtering is supported.
1580 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1581 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1582 cstate
->off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1583 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1587 cstate
->off_linktype
.constant_part
= 1;
1588 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1590 cstate
->off_nl_nosnap
= -1;
1593 case DLT_NETANALYZER
:
1594 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1595 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1596 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1597 cstate
->off_nl
= 0; /* Ethernet II */
1598 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1601 case DLT_NETANALYZER_TRANSPARENT
:
1602 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1603 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1604 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1605 cstate
->off_nl
= 0; /* Ethernet II */
1606 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1611 * For values in the range in which we've assigned new
1612 * DLT_ values, only raw "link[N:M]" filtering is supported.
1614 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1615 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1616 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1617 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1618 cstate
->off_nl
= -1;
1619 cstate
->off_nl_nosnap
= -1;
1621 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1626 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1630 * Load a value relative to the specified absolute offset.
1632 static struct slist
*
1633 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1634 u_int offset
, u_int size
)
1636 struct slist
*s
, *s2
;
1638 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1641 * If "s" is non-null, it has code to arrange that the X register
1642 * contains the variable part of the absolute offset, so we
1643 * generate a load relative to that, with an offset of
1644 * abs_offset->constant_part + offset.
1646 * Otherwise, we can do an absolute load with an offset of
1647 * abs_offset->constant_part + offset.
1651 * "s" points to a list of statements that puts the
1652 * variable part of the absolute offset into the X register.
1653 * Do an indirect load, to use the X register as an offset.
1655 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1656 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1660 * There is no variable part of the absolute offset, so
1661 * just do an absolute load.
1663 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1664 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1670 * Load a value relative to the beginning of the specified header.
1672 static struct slist
*
1673 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1676 struct slist
*s
, *s2
;
1681 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1686 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1689 case OR_PREVLINKHDR
:
1690 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1694 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1697 case OR_PREVMPLSHDR
:
1698 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1702 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1705 case OR_LINKPL_NOSNAP
:
1706 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1710 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1715 * Load the X register with the length of the IPv4 header
1716 * (plus the offset of the link-layer header, if it's
1717 * preceded by a variable-length header such as a radio
1718 * header), in bytes.
1720 s
= gen_loadx_iphdrlen(cstate
);
1723 * Load the item at {offset of the link-layer payload} +
1724 * {offset, relative to the start of the link-layer
1725 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1726 * {specified offset}.
1728 * If the offset of the link-layer payload is variable,
1729 * the variable part of that offset is included in the
1730 * value in the X register, and we include the constant
1731 * part in the offset of the load.
1733 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1734 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1739 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1750 * Generate code to load into the X register the sum of the length of
1751 * the IPv4 header and the variable part of the offset of the link-layer
1754 static struct slist
*
1755 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1757 struct slist
*s
, *s2
;
1759 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1762 * The offset of the link-layer payload has a variable
1763 * part. "s" points to a list of statements that put
1764 * the variable part of that offset into the X register.
1766 * The 4*([k]&0xf) addressing mode can't be used, as we
1767 * don't have a constant offset, so we have to load the
1768 * value in question into the A register and add to it
1769 * the value from the X register.
1771 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1772 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1774 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1777 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1782 * The A register now contains the length of the IP header.
1783 * We need to add to it the variable part of the offset of
1784 * the link-layer payload, which is still in the X
1785 * register, and move the result into the X register.
1787 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1788 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1791 * The offset of the link-layer payload is a constant,
1792 * so no code was generated to load the (non-existent)
1793 * variable part of that offset.
1795 * This means we can use the 4*([k]&0xf) addressing
1796 * mode. Load the length of the IPv4 header, which
1797 * is at an offset of cstate->off_nl from the beginning of
1798 * the link-layer payload, and thus at an offset of
1799 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1800 * of the raw packet data, using that addressing mode.
1802 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1803 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1809 static struct block
*
1810 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1815 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1817 b
= new_block(cstate
, JMP(BPF_JEQ
));
1823 static inline struct block
*
1824 gen_true(compiler_state_t
*cstate
)
1826 return gen_uncond(cstate
, 1);
1829 static inline struct block
*
1830 gen_false(compiler_state_t
*cstate
)
1832 return gen_uncond(cstate
, 0);
1836 * Byte-swap a 32-bit number.
1837 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1838 * big-endian platforms.)
1840 #define SWAPLONG(y) \
1841 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1844 * Generate code to match a particular packet type.
1846 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1847 * value, if <= ETHERMTU. We use that to determine whether to
1848 * match the type/length field or to check the type/length field for
1849 * a value <= ETHERMTU to see whether it's a type field and then do
1850 * the appropriate test.
1852 static struct block
*
1853 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1855 struct block
*b0
, *b1
;
1861 case LLCSAP_NETBEUI
:
1863 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1864 * so we check the DSAP and SSAP.
1866 * LLCSAP_IP checks for IP-over-802.2, rather
1867 * than IP-over-Ethernet or IP-over-SNAP.
1869 * XXX - should we check both the DSAP and the
1870 * SSAP, like this, or should we check just the
1871 * DSAP, as we do for other types <= ETHERMTU
1872 * (i.e., other SAP values)?
1874 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1876 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1877 ((proto
<< 8) | proto
));
1885 * Ethernet_II frames, which are Ethernet
1886 * frames with a frame type of ETHERTYPE_IPX;
1888 * Ethernet_802.3 frames, which are 802.3
1889 * frames (i.e., the type/length field is
1890 * a length field, <= ETHERMTU, rather than
1891 * a type field) with the first two bytes
1892 * after the Ethernet/802.3 header being
1895 * Ethernet_802.2 frames, which are 802.3
1896 * frames with an 802.2 LLC header and
1897 * with the IPX LSAP as the DSAP in the LLC
1900 * Ethernet_SNAP frames, which are 802.3
1901 * frames with an LLC header and a SNAP
1902 * header and with an OUI of 0x000000
1903 * (encapsulated Ethernet) and a protocol
1904 * ID of ETHERTYPE_IPX in the SNAP header.
1906 * XXX - should we generate the same code both
1907 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1911 * This generates code to check both for the
1912 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1914 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1915 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1919 * Now we add code to check for SNAP frames with
1920 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1922 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1926 * Now we generate code to check for 802.3
1927 * frames in general.
1929 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1933 * Now add the check for 802.3 frames before the
1934 * check for Ethernet_802.2 and Ethernet_802.3,
1935 * as those checks should only be done on 802.3
1936 * frames, not on Ethernet frames.
1941 * Now add the check for Ethernet_II frames, and
1942 * do that before checking for the other frame
1945 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1949 case ETHERTYPE_ATALK
:
1950 case ETHERTYPE_AARP
:
1952 * EtherTalk (AppleTalk protocols on Ethernet link
1953 * layer) may use 802.2 encapsulation.
1957 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1958 * we check for an Ethernet type field less than
1959 * 1500, which means it's an 802.3 length field.
1961 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1965 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1966 * SNAP packets with an organization code of
1967 * 0x080007 (Apple, for Appletalk) and a protocol
1968 * type of ETHERTYPE_ATALK (Appletalk).
1970 * 802.2-encapsulated ETHERTYPE_AARP packets are
1971 * SNAP packets with an organization code of
1972 * 0x000000 (encapsulated Ethernet) and a protocol
1973 * type of ETHERTYPE_AARP (Appletalk ARP).
1975 if (proto
== ETHERTYPE_ATALK
)
1976 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
1977 else /* proto == ETHERTYPE_AARP */
1978 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
1982 * Check for Ethernet encapsulation (Ethertalk
1983 * phase 1?); we just check for the Ethernet
1986 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1992 if (proto
<= ETHERMTU
) {
1994 * This is an LLC SAP value, so the frames
1995 * that match would be 802.2 frames.
1996 * Check that the frame is an 802.2 frame
1997 * (i.e., that the length/type field is
1998 * a length field, <= ETHERMTU) and
1999 * then check the DSAP.
2001 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2003 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2008 * This is an Ethernet type, so compare
2009 * the length/type field with it (if
2010 * the frame is an 802.2 frame, the length
2011 * field will be <= ETHERMTU, and, as
2012 * "proto" is > ETHERMTU, this test
2013 * will fail and the frame won't match,
2014 * which is what we want).
2016 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2022 static struct block
*
2023 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2026 * For DLT_NULL, the link-layer header is a 32-bit word
2027 * containing an AF_ value in *host* byte order, and for
2028 * DLT_ENC, the link-layer header begins with a 32-bit
2029 * word containing an AF_ value in host byte order.
2031 * In addition, if we're reading a saved capture file,
2032 * the host byte order in the capture may not be the
2033 * same as the host byte order on this machine.
2035 * For DLT_LOOP, the link-layer header is a 32-bit
2036 * word containing an AF_ value in *network* byte order.
2038 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2040 * The AF_ value is in host byte order, but the BPF
2041 * interpreter will convert it to network byte order.
2043 * If this is a save file, and it's from a machine
2044 * with the opposite byte order to ours, we byte-swap
2047 * Then we run it through "htonl()", and generate
2048 * code to compare against the result.
2050 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2051 proto
= SWAPLONG(proto
);
2052 proto
= htonl(proto
);
2054 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2058 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2059 * or IPv6 then we have an error.
2061 static struct block
*
2062 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2067 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2070 case ETHERTYPE_IPV6
:
2071 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2072 (bpf_int32
)IPH_AF_INET6
);
2079 return gen_false(cstate
);
2083 * Generate code to match a particular packet type.
2085 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2086 * value, if <= ETHERMTU. We use that to determine whether to
2087 * match the type field or to check the type field for the special
2088 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2090 static struct block
*
2091 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2093 struct block
*b0
, *b1
;
2099 case LLCSAP_NETBEUI
:
2101 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2102 * so we check the DSAP and SSAP.
2104 * LLCSAP_IP checks for IP-over-802.2, rather
2105 * than IP-over-Ethernet or IP-over-SNAP.
2107 * XXX - should we check both the DSAP and the
2108 * SSAP, like this, or should we check just the
2109 * DSAP, as we do for other types <= ETHERMTU
2110 * (i.e., other SAP values)?
2112 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2113 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2114 ((proto
<< 8) | proto
));
2120 * Ethernet_II frames, which are Ethernet
2121 * frames with a frame type of ETHERTYPE_IPX;
2123 * Ethernet_802.3 frames, which have a frame
2124 * type of LINUX_SLL_P_802_3;
2126 * Ethernet_802.2 frames, which are 802.3
2127 * frames with an 802.2 LLC header (i.e, have
2128 * a frame type of LINUX_SLL_P_802_2) and
2129 * with the IPX LSAP as the DSAP in the LLC
2132 * Ethernet_SNAP frames, which are 802.3
2133 * frames with an LLC header and a SNAP
2134 * header and with an OUI of 0x000000
2135 * (encapsulated Ethernet) and a protocol
2136 * ID of ETHERTYPE_IPX in the SNAP header.
2138 * First, do the checks on LINUX_SLL_P_802_2
2139 * frames; generate the check for either
2140 * Ethernet_802.2 or Ethernet_SNAP frames, and
2141 * then put a check for LINUX_SLL_P_802_2 frames
2144 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2145 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2147 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2151 * Now check for 802.3 frames and OR that with
2152 * the previous test.
2154 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2158 * Now add the check for Ethernet_II frames, and
2159 * do that before checking for the other frame
2162 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2166 case ETHERTYPE_ATALK
:
2167 case ETHERTYPE_AARP
:
2169 * EtherTalk (AppleTalk protocols on Ethernet link
2170 * layer) may use 802.2 encapsulation.
2174 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2175 * we check for the 802.2 protocol type in the
2176 * "Ethernet type" field.
2178 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2181 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2182 * SNAP packets with an organization code of
2183 * 0x080007 (Apple, for Appletalk) and a protocol
2184 * type of ETHERTYPE_ATALK (Appletalk).
2186 * 802.2-encapsulated ETHERTYPE_AARP packets are
2187 * SNAP packets with an organization code of
2188 * 0x000000 (encapsulated Ethernet) and a protocol
2189 * type of ETHERTYPE_AARP (Appletalk ARP).
2191 if (proto
== ETHERTYPE_ATALK
)
2192 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2193 else /* proto == ETHERTYPE_AARP */
2194 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2198 * Check for Ethernet encapsulation (Ethertalk
2199 * phase 1?); we just check for the Ethernet
2202 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2208 if (proto
<= ETHERMTU
) {
2210 * This is an LLC SAP value, so the frames
2211 * that match would be 802.2 frames.
2212 * Check for the 802.2 protocol type
2213 * in the "Ethernet type" field, and
2214 * then check the DSAP.
2216 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2217 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2223 * This is an Ethernet type, so compare
2224 * the length/type field with it (if
2225 * the frame is an 802.2 frame, the length
2226 * field will be <= ETHERMTU, and, as
2227 * "proto" is > ETHERMTU, this test
2228 * will fail and the frame won't match,
2229 * which is what we want).
2231 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2236 static struct slist
*
2237 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2239 struct slist
*s1
, *s2
;
2240 struct slist
*sjeq_avs_cookie
;
2241 struct slist
*sjcommon
;
2244 * This code is not compatible with the optimizer, as
2245 * we are generating jmp instructions within a normal
2246 * slist of instructions
2248 cstate
->no_optimize
= 1;
2251 * Generate code to load the length of the radio header into
2252 * the register assigned to hold that length, if one has been
2253 * assigned. (If one hasn't been assigned, no code we've
2254 * generated uses that prefix, so we don't need to generate any
2257 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2258 * or always use the AVS header rather than the Prism header.
2259 * We load a 4-byte big-endian value at the beginning of the
2260 * raw packet data, and see whether, when masked with 0xFFFFF000,
2261 * it's equal to 0x80211000. If so, that indicates that it's
2262 * an AVS header (the masked-out bits are the version number).
2263 * Otherwise, it's a Prism header.
2265 * XXX - the Prism header is also, in theory, variable-length,
2266 * but no known software generates headers that aren't 144
2269 if (cstate
->off_linkhdr
.reg
!= -1) {
2273 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2277 * AND it with 0xFFFFF000.
2279 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2280 s2
->s
.k
= 0xFFFFF000;
2284 * Compare with 0x80211000.
2286 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2287 sjeq_avs_cookie
->s
.k
= 0x80211000;
2288 sappend(s1
, sjeq_avs_cookie
);
2293 * The 4 bytes at an offset of 4 from the beginning of
2294 * the AVS header are the length of the AVS header.
2295 * That field is big-endian.
2297 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2300 sjeq_avs_cookie
->s
.jt
= s2
;
2303 * Now jump to the code to allocate a register
2304 * into which to save the header length and
2305 * store the length there. (The "jump always"
2306 * instruction needs to have the k field set;
2307 * it's added to the PC, so, as we're jumping
2308 * over a single instruction, it should be 1.)
2310 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2312 sappend(s1
, sjcommon
);
2315 * Now for the code that handles the Prism header.
2316 * Just load the length of the Prism header (144)
2317 * into the A register. Have the test for an AVS
2318 * header branch here if we don't have an AVS header.
2320 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2323 sjeq_avs_cookie
->s
.jf
= s2
;
2326 * Now allocate a register to hold that value and store
2327 * it. The code for the AVS header will jump here after
2328 * loading the length of the AVS header.
2330 s2
= new_stmt(cstate
, BPF_ST
);
2331 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2333 sjcommon
->s
.jf
= s2
;
2336 * Now move it into the X register.
2338 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2346 static struct slist
*
2347 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2349 struct slist
*s1
, *s2
;
2352 * Generate code to load the length of the AVS header into
2353 * the register assigned to hold that length, if one has been
2354 * assigned. (If one hasn't been assigned, no code we've
2355 * generated uses that prefix, so we don't need to generate any
2358 if (cstate
->off_linkhdr
.reg
!= -1) {
2360 * The 4 bytes at an offset of 4 from the beginning of
2361 * the AVS header are the length of the AVS header.
2362 * That field is big-endian.
2364 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2368 * Now allocate a register to hold that value and store
2371 s2
= new_stmt(cstate
, BPF_ST
);
2372 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2376 * Now move it into the X register.
2378 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2386 static struct slist
*
2387 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2389 struct slist
*s1
, *s2
;
2392 * Generate code to load the length of the radiotap header into
2393 * the register assigned to hold that length, if one has been
2394 * assigned. (If one hasn't been assigned, no code we've
2395 * generated uses that prefix, so we don't need to generate any
2398 if (cstate
->off_linkhdr
.reg
!= -1) {
2400 * The 2 bytes at offsets of 2 and 3 from the beginning
2401 * of the radiotap header are the length of the radiotap
2402 * header; unfortunately, it's little-endian, so we have
2403 * to load it a byte at a time and construct the value.
2407 * Load the high-order byte, at an offset of 3, shift it
2408 * left a byte, and put the result in the X register.
2410 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2412 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2415 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2419 * Load the next byte, at an offset of 2, and OR the
2420 * value from the X register into it.
2422 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2425 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2429 * Now allocate a register to hold that value and store
2432 s2
= new_stmt(cstate
, BPF_ST
);
2433 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2437 * Now move it into the X register.
2439 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2448 * At the moment we treat PPI as normal Radiotap encoded
2449 * packets. The difference is in the function that generates
2450 * the code at the beginning to compute the header length.
2451 * Since this code generator of PPI supports bare 802.11
2452 * encapsulation only (i.e. the encapsulated DLT should be
2453 * DLT_IEEE802_11) we generate code to check for this too;
2454 * that's done in finish_parse().
2456 static struct slist
*
2457 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2459 struct slist
*s1
, *s2
;
2462 * Generate code to load the length of the radiotap header
2463 * into the register assigned to hold that length, if one has
2466 if (cstate
->off_linkhdr
.reg
!= -1) {
2468 * The 2 bytes at offsets of 2 and 3 from the beginning
2469 * of the radiotap header are the length of the radiotap
2470 * header; unfortunately, it's little-endian, so we have
2471 * to load it a byte at a time and construct the value.
2475 * Load the high-order byte, at an offset of 3, shift it
2476 * left a byte, and put the result in the X register.
2478 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2480 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2483 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2487 * Load the next byte, at an offset of 2, and OR the
2488 * value from the X register into it.
2490 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2493 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2497 * Now allocate a register to hold that value and store
2500 s2
= new_stmt(cstate
, BPF_ST
);
2501 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2505 * Now move it into the X register.
2507 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2516 * Load a value relative to the beginning of the link-layer header after the 802.11
2517 * header, i.e. LLC_SNAP.
2518 * The link-layer header doesn't necessarily begin at the beginning
2519 * of the packet data; there might be a variable-length prefix containing
2520 * radio information.
2522 static struct slist
*
2523 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2526 struct slist
*sjset_data_frame_1
;
2527 struct slist
*sjset_data_frame_2
;
2528 struct slist
*sjset_qos
;
2529 struct slist
*sjset_radiotap_flags_present
;
2530 struct slist
*sjset_radiotap_ext_present
;
2531 struct slist
*sjset_radiotap_tsft_present
;
2532 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2533 struct slist
*s_roundup
;
2535 if (cstate
->off_linkpl
.reg
== -1) {
2537 * No register has been assigned to the offset of
2538 * the link-layer payload, which means nobody needs
2539 * it; don't bother computing it - just return
2540 * what we already have.
2546 * This code is not compatible with the optimizer, as
2547 * we are generating jmp instructions within a normal
2548 * slist of instructions
2550 cstate
->no_optimize
= 1;
2553 * If "s" is non-null, it has code to arrange that the X register
2554 * contains the length of the prefix preceding the link-layer
2557 * Otherwise, the length of the prefix preceding the link-layer
2558 * header is "off_outermostlinkhdr.constant_part".
2562 * There is no variable-length header preceding the
2563 * link-layer header.
2565 * Load the length of the fixed-length prefix preceding
2566 * the link-layer header (if any) into the X register,
2567 * and store it in the cstate->off_linkpl.reg register.
2568 * That length is off_outermostlinkhdr.constant_part.
2570 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2571 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2575 * The X register contains the offset of the beginning of the
2576 * link-layer header; add 24, which is the minimum length
2577 * of the MAC header for a data frame, to that, and store it
2578 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2579 * which is at the offset in the X register, with an indexed load.
2581 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2583 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2586 s2
= new_stmt(cstate
, BPF_ST
);
2587 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2590 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2595 * Check the Frame Control field to see if this is a data frame;
2596 * a data frame has the 0x08 bit (b3) in that field set and the
2597 * 0x04 bit (b2) clear.
2599 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2600 sjset_data_frame_1
->s
.k
= 0x08;
2601 sappend(s
, sjset_data_frame_1
);
2604 * If b3 is set, test b2, otherwise go to the first statement of
2605 * the rest of the program.
2607 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2608 sjset_data_frame_2
->s
.k
= 0x04;
2609 sappend(s
, sjset_data_frame_2
);
2610 sjset_data_frame_1
->s
.jf
= snext
;
2613 * If b2 is not set, this is a data frame; test the QoS bit.
2614 * Otherwise, go to the first statement of the rest of the
2617 sjset_data_frame_2
->s
.jt
= snext
;
2618 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2619 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2620 sappend(s
, sjset_qos
);
2623 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2625 * Otherwise, go to the first statement of the rest of the
2628 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2629 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2631 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2634 s2
= new_stmt(cstate
, BPF_ST
);
2635 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2639 * If we have a radiotap header, look at it to see whether
2640 * there's Atheros padding between the MAC-layer header
2643 * Note: all of the fields in the radiotap header are
2644 * little-endian, so we byte-swap all of the values
2645 * we test against, as they will be loaded as big-endian
2648 * XXX - in the general case, we would have to scan through
2649 * *all* the presence bits, if there's more than one word of
2650 * presence bits. That would require a loop, meaning that
2651 * we wouldn't be able to run the filter in the kernel.
2653 * We assume here that the Atheros adapters that insert the
2654 * annoying padding don't have multiple antennae and therefore
2655 * do not generate radiotap headers with multiple presence words.
2657 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2659 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2660 * in the first presence flag word?
2662 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2666 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2667 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2668 sappend(s
, sjset_radiotap_flags_present
);
2671 * If not, skip all of this.
2673 sjset_radiotap_flags_present
->s
.jf
= snext
;
2676 * Otherwise, is the "extension" bit set in that word?
2678 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2679 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2680 sappend(s
, sjset_radiotap_ext_present
);
2681 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2684 * If so, skip all of this.
2686 sjset_radiotap_ext_present
->s
.jt
= snext
;
2689 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2691 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2692 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2693 sappend(s
, sjset_radiotap_tsft_present
);
2694 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2697 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2698 * at an offset of 16 from the beginning of the raw packet
2699 * data (8 bytes for the radiotap header and 8 bytes for
2702 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2705 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2708 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2710 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2711 sjset_tsft_datapad
->s
.k
= 0x20;
2712 sappend(s
, sjset_tsft_datapad
);
2715 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2716 * at an offset of 8 from the beginning of the raw packet
2717 * data (8 bytes for the radiotap header).
2719 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2722 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2725 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2727 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2728 sjset_notsft_datapad
->s
.k
= 0x20;
2729 sappend(s
, sjset_notsft_datapad
);
2732 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2733 * set, round the length of the 802.11 header to
2734 * a multiple of 4. Do that by adding 3 and then
2735 * dividing by and multiplying by 4, which we do by
2738 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2739 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2740 sappend(s
, s_roundup
);
2741 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2744 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2747 s2
= new_stmt(cstate
, BPF_ST
);
2748 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2751 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2752 sjset_tsft_datapad
->s
.jf
= snext
;
2753 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2754 sjset_notsft_datapad
->s
.jf
= snext
;
2756 sjset_qos
->s
.jf
= snext
;
2762 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2766 /* There is an implicit dependency between the link
2767 * payload and link header since the payload computation
2768 * includes the variable part of the header. Therefore,
2769 * if nobody else has allocated a register for the link
2770 * header and we need it, do it now. */
2771 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2772 cstate
->off_linkhdr
.reg
== -1)
2773 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2776 * For link-layer types that have a variable-length header
2777 * preceding the link-layer header, generate code to load
2778 * the offset of the link-layer header into the register
2779 * assigned to that offset, if any.
2781 * XXX - this, and the next switch statement, won't handle
2782 * encapsulation of 802.11 or 802.11+radio information in
2783 * some other protocol stack. That's significantly more
2786 switch (cstate
->outermostlinktype
) {
2788 case DLT_PRISM_HEADER
:
2789 s
= gen_load_prism_llprefixlen(cstate
);
2792 case DLT_IEEE802_11_RADIO_AVS
:
2793 s
= gen_load_avs_llprefixlen(cstate
);
2796 case DLT_IEEE802_11_RADIO
:
2797 s
= gen_load_radiotap_llprefixlen(cstate
);
2801 s
= gen_load_ppi_llprefixlen(cstate
);
2810 * For link-layer types that have a variable-length link-layer
2811 * header, generate code to load the offset of the link-layer
2812 * payload into the register assigned to that offset, if any.
2814 switch (cstate
->outermostlinktype
) {
2816 case DLT_IEEE802_11
:
2817 case DLT_PRISM_HEADER
:
2818 case DLT_IEEE802_11_RADIO_AVS
:
2819 case DLT_IEEE802_11_RADIO
:
2821 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2826 * If there there is no initialization yet and we need variable
2827 * length offsets for VLAN, initialize them to zero
2829 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2832 if (cstate
->off_linkpl
.reg
== -1)
2833 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2834 if (cstate
->off_linktype
.reg
== -1)
2835 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2837 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2839 s2
= new_stmt(cstate
, BPF_ST
);
2840 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2842 s2
= new_stmt(cstate
, BPF_ST
);
2843 s2
->s
.k
= cstate
->off_linktype
.reg
;
2848 * If we have any offset-loading code, append all the
2849 * existing statements in the block to those statements,
2850 * and make the resulting list the list of statements
2854 sappend(s
, b
->stmts
);
2859 static struct block
*
2860 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2862 struct slist
*s_load_dlt
;
2865 if (cstate
->linktype
== DLT_PPI
)
2867 /* Create the statements that check for the DLT
2869 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2870 s_load_dlt
->s
.k
= 4;
2872 b
= new_block(cstate
, JMP(BPF_JEQ
));
2874 b
->stmts
= s_load_dlt
;
2875 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2886 * Take an absolute offset, and:
2888 * if it has no variable part, return NULL;
2890 * if it has a variable part, generate code to load the register
2891 * containing that variable part into the X register, returning
2892 * a pointer to that code - if no register for that offset has
2893 * been allocated, allocate it first.
2895 * (The code to set that register will be generated later, but will
2896 * be placed earlier in the code sequence.)
2898 static struct slist
*
2899 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2903 if (off
->is_variable
) {
2904 if (off
->reg
== -1) {
2906 * We haven't yet assigned a register for the
2907 * variable part of the offset of the link-layer
2908 * header; allocate one.
2910 off
->reg
= alloc_reg(cstate
);
2914 * Load the register containing the variable part of the
2915 * offset of the link-layer header into the X register.
2917 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2922 * That offset isn't variable, there's no variable part,
2923 * so we don't need to generate any code.
2930 * Map an Ethernet type to the equivalent PPP type.
2933 ethertype_to_ppptype(proto
)
2942 case ETHERTYPE_IPV6
:
2950 case ETHERTYPE_ATALK
:
2964 * I'm assuming the "Bridging PDU"s that go
2965 * over PPP are Spanning Tree Protocol
2979 * Generate any tests that, for encapsulation of a link-layer packet
2980 * inside another protocol stack, need to be done to check for those
2981 * link-layer packets (and that haven't already been done by a check
2982 * for that encapsulation).
2984 static struct block
*
2985 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
2989 if (cstate
->is_geneve
)
2990 return gen_geneve_ll_check(cstate
);
2992 switch (cstate
->prevlinktype
) {
2996 * This is LANE-encapsulated Ethernet; check that the LANE
2997 * packet doesn't begin with an LE Control marker, i.e.
2998 * that it's data, not a control message.
3000 * (We've already generated a test for LANE.)
3002 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3008 * No such tests are necessary.
3016 * The three different values we should check for when checking for an
3017 * IPv6 packet with DLT_NULL.
3019 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3020 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3021 #define BSD_AFNUM_INET6_DARWIN 30 /* OS X, iOS, other Darwin-based OSes */
3024 * Generate code to match a particular packet type by matching the
3025 * link-layer type field or fields in the 802.2 LLC header.
3027 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3028 * value, if <= ETHERMTU.
3030 static struct block
*
3031 gen_linktype(compiler_state_t
*cstate
, int proto
)
3033 struct block
*b0
, *b1
, *b2
;
3034 const char *description
;
3036 /* are we checking MPLS-encapsulated packets? */
3037 if (cstate
->label_stack_depth
> 0) {
3041 /* FIXME add other L3 proto IDs */
3042 return gen_mpls_linktype(cstate
, Q_IP
);
3044 case ETHERTYPE_IPV6
:
3046 /* FIXME add other L3 proto IDs */
3047 return gen_mpls_linktype(cstate
, Q_IPV6
);
3050 bpf_error(cstate
, "unsupported protocol over mpls");
3055 switch (cstate
->linktype
) {
3058 case DLT_NETANALYZER
:
3059 case DLT_NETANALYZER_TRANSPARENT
:
3060 /* Geneve has an EtherType regardless of whether there is an
3062 if (!cstate
->is_geneve
)
3063 b0
= gen_prevlinkhdr_check(cstate
);
3067 b1
= gen_ether_linktype(cstate
, proto
);
3078 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3082 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3088 case DLT_IEEE802_11
:
3089 case DLT_PRISM_HEADER
:
3090 case DLT_IEEE802_11_RADIO_AVS
:
3091 case DLT_IEEE802_11_RADIO
:
3094 * Check that we have a data frame.
3096 b0
= gen_check_802_11_data_frame(cstate
);
3099 * Now check for the specified link-layer type.
3101 b1
= gen_llc_linktype(cstate
, proto
);
3109 * XXX - check for LLC frames.
3111 return gen_llc_linktype(cstate
, proto
);
3117 * XXX - check for LLC PDUs, as per IEEE 802.5.
3119 return gen_llc_linktype(cstate
, proto
);
3123 case DLT_ATM_RFC1483
:
3125 case DLT_IP_OVER_FC
:
3126 return gen_llc_linktype(cstate
, proto
);
3132 * Check for an LLC-encapsulated version of this protocol;
3133 * if we were checking for LANE, linktype would no longer
3136 * Check for LLC encapsulation and then check the protocol.
3138 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3139 b1
= gen_llc_linktype(cstate
, proto
);
3146 return gen_linux_sll_linktype(cstate
, proto
);
3151 case DLT_SLIP_BSDOS
:
3154 * These types don't provide any type field; packets
3155 * are always IPv4 or IPv6.
3157 * XXX - for IPv4, check for a version number of 4, and,
3158 * for IPv6, check for a version number of 6?
3163 /* Check for a version number of 4. */
3164 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3166 case ETHERTYPE_IPV6
:
3167 /* Check for a version number of 6. */
3168 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3171 return gen_false(cstate
); /* always false */
3178 * Raw IPv4, so no type field.
3180 if (proto
== ETHERTYPE_IP
)
3181 return gen_true(cstate
); /* always true */
3183 /* Checking for something other than IPv4; always false */
3184 return gen_false(cstate
);
3190 * Raw IPv6, so no type field.
3192 if (proto
== ETHERTYPE_IPV6
)
3193 return gen_true(cstate
); /* always true */
3195 /* Checking for something other than IPv6; always false */
3196 return gen_false(cstate
);
3202 case DLT_PPP_SERIAL
:
3205 * We use Ethernet protocol types inside libpcap;
3206 * map them to the corresponding PPP protocol types.
3208 proto
= ethertype_to_ppptype(proto
);
3209 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3215 * We use Ethernet protocol types inside libpcap;
3216 * map them to the corresponding PPP protocol types.
3222 * Also check for Van Jacobson-compressed IP.
3223 * XXX - do this for other forms of PPP?
3225 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3226 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3228 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3233 proto
= ethertype_to_ppptype(proto
);
3234 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3246 return (gen_loopback_linktype(cstate
, AF_INET
));
3248 case ETHERTYPE_IPV6
:
3250 * AF_ values may, unfortunately, be platform-
3251 * dependent; AF_INET isn't, because everybody
3252 * used 4.2BSD's value, but AF_INET6 is, because
3253 * 4.2BSD didn't have a value for it (given that
3254 * IPv6 didn't exist back in the early 1980's),
3255 * and they all picked their own values.
3257 * This means that, if we're reading from a
3258 * savefile, we need to check for all the
3261 * If we're doing a live capture, we only need
3262 * to check for this platform's value; however,
3263 * Npcap uses 24, which isn't Windows's AF_INET6
3264 * value. (Given the multiple different values,
3265 * programs that read pcap files shouldn't be
3266 * checking for their platform's AF_INET6 value
3267 * anyway, they should check for all of the
3268 * possible values. and they might as well do
3269 * that even for live captures.)
3271 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3273 * Savefile - check for all three
3274 * possible IPv6 values.
3276 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3277 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3279 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3284 * Live capture, so we only need to
3285 * check for the value used on this
3290 * Npcap doesn't use Windows's AF_INET6,
3291 * as that collides with AF_IPX on
3292 * some BSDs (both have the value 23).
3293 * Instead, it uses 24.
3295 return (gen_loopback_linktype(cstate
, 24));
3298 return (gen_loopback_linktype(cstate
, AF_INET6
));
3299 #else /* AF_INET6 */
3301 * I guess this platform doesn't support
3302 * IPv6, so we just reject all packets.
3304 return gen_false(cstate
);
3305 #endif /* AF_INET6 */
3311 * Not a type on which we support filtering.
3312 * XXX - support those that have AF_ values
3313 * #defined on this platform, at least?
3315 return gen_false(cstate
);
3318 #ifdef HAVE_NET_PFVAR_H
3321 * af field is host byte order in contrast to the rest of
3324 if (proto
== ETHERTYPE_IP
)
3325 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3326 BPF_B
, (bpf_int32
)AF_INET
));
3327 else if (proto
== ETHERTYPE_IPV6
)
3328 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3329 BPF_B
, (bpf_int32
)AF_INET6
));
3331 return gen_false(cstate
);
3334 #endif /* HAVE_NET_PFVAR_H */
3337 case DLT_ARCNET_LINUX
:
3339 * XXX should we check for first fragment if the protocol
3345 return gen_false(cstate
);
3347 case ETHERTYPE_IPV6
:
3348 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3349 (bpf_int32
)ARCTYPE_INET6
));
3352 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3353 (bpf_int32
)ARCTYPE_IP
);
3354 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3355 (bpf_int32
)ARCTYPE_IP_OLD
);
3360 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3361 (bpf_int32
)ARCTYPE_ARP
);
3362 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3363 (bpf_int32
)ARCTYPE_ARP_OLD
);
3367 case ETHERTYPE_REVARP
:
3368 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3369 (bpf_int32
)ARCTYPE_REVARP
));
3371 case ETHERTYPE_ATALK
:
3372 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3373 (bpf_int32
)ARCTYPE_ATALK
));
3380 case ETHERTYPE_ATALK
:
3381 return gen_true(cstate
);
3383 return gen_false(cstate
);
3390 * XXX - assumes a 2-byte Frame Relay header with
3391 * DLCI and flags. What if the address is longer?
3397 * Check for the special NLPID for IP.
3399 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3401 case ETHERTYPE_IPV6
:
3403 * Check for the special NLPID for IPv6.
3405 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3409 * Check for several OSI protocols.
3411 * Frame Relay packets typically have an OSI
3412 * NLPID at the beginning; we check for each
3415 * What we check for is the NLPID and a frame
3416 * control field of UI, i.e. 0x03 followed
3419 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3420 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3421 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3427 return gen_false(cstate
);
3433 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3435 case DLT_JUNIPER_MFR
:
3436 case DLT_JUNIPER_MLFR
:
3437 case DLT_JUNIPER_MLPPP
:
3438 case DLT_JUNIPER_ATM1
:
3439 case DLT_JUNIPER_ATM2
:
3440 case DLT_JUNIPER_PPPOE
:
3441 case DLT_JUNIPER_PPPOE_ATM
:
3442 case DLT_JUNIPER_GGSN
:
3443 case DLT_JUNIPER_ES
:
3444 case DLT_JUNIPER_MONITOR
:
3445 case DLT_JUNIPER_SERVICES
:
3446 case DLT_JUNIPER_ETHER
:
3447 case DLT_JUNIPER_PPP
:
3448 case DLT_JUNIPER_FRELAY
:
3449 case DLT_JUNIPER_CHDLC
:
3450 case DLT_JUNIPER_VP
:
3451 case DLT_JUNIPER_ST
:
3452 case DLT_JUNIPER_ISM
:
3453 case DLT_JUNIPER_VS
:
3454 case DLT_JUNIPER_SRX_E2E
:
3455 case DLT_JUNIPER_FIBRECHANNEL
:
3456 case DLT_JUNIPER_ATM_CEMIC
:
3458 /* just lets verify the magic number for now -
3459 * on ATM we may have up to 6 different encapsulations on the wire
3460 * and need a lot of heuristics to figure out that the payload
3463 * FIXME encapsulation specific BPF_ filters
3465 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3467 case DLT_BACNET_MS_TP
:
3468 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3471 return gen_ipnet_linktype(cstate
, proto
);
3473 case DLT_LINUX_IRDA
:
3474 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3477 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3480 case DLT_MTP2_WITH_PHDR
:
3481 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3484 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3487 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3489 case DLT_LINUX_LAPD
:
3490 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3492 case DLT_USB_FREEBSD
:
3494 case DLT_USB_LINUX_MMAPPED
:
3496 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3498 case DLT_BLUETOOTH_HCI_H4
:
3499 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3500 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3503 case DLT_CAN_SOCKETCAN
:
3504 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3506 case DLT_IEEE802_15_4
:
3507 case DLT_IEEE802_15_4_LINUX
:
3508 case DLT_IEEE802_15_4_NONASK_PHY
:
3509 case DLT_IEEE802_15_4_NOFCS
:
3510 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3512 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3513 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3516 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3519 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3522 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3525 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3528 /* Using the fixed-size NFLOG header it is possible to tell only
3529 * the address family of the packet, other meaningful data is
3530 * either missing or behind TLVs.
3532 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3536 * Does this link-layer header type have a field
3537 * indicating the type of the next protocol? If
3538 * so, off_linktype.constant_part will be the offset of that
3539 * field in the packet; if not, it will be OFFSET_NOT_SET.
3541 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3543 * Yes; assume it's an Ethernet type. (If
3544 * it's not, it needs to be handled specially
3547 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3550 * No; report an error.
3552 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3553 if (description
!= NULL
) {
3554 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3557 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3566 * Check for an LLC SNAP packet with a given organization code and
3567 * protocol type; we check the entire contents of the 802.2 LLC and
3568 * snap headers, checking for DSAP and SSAP of SNAP and a control
3569 * field of 0x03 in the LLC header, and for the specified organization
3570 * code and protocol type in the SNAP header.
3572 static struct block
*
3573 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3575 u_char snapblock
[8];
3577 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3578 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3579 snapblock
[2] = 0x03; /* control = UI */
3580 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3581 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3582 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3583 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3584 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3585 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3589 * Generate code to match frames with an LLC header.
3592 gen_llc(compiler_state_t
*cstate
)
3594 struct block
*b0
, *b1
;
3596 switch (cstate
->linktype
) {
3600 * We check for an Ethernet type field less than
3601 * 1500, which means it's an 802.3 length field.
3603 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3607 * Now check for the purported DSAP and SSAP not being
3608 * 0xFF, to rule out NetWare-over-802.3.
3610 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3617 * We check for LLC traffic.
3619 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3622 case DLT_IEEE802
: /* Token Ring */
3624 * XXX - check for LLC frames.
3626 return gen_true(cstate
);
3630 * XXX - check for LLC frames.
3632 return gen_true(cstate
);
3634 case DLT_ATM_RFC1483
:
3636 * For LLC encapsulation, these are defined to have an
3639 * For VC encapsulation, they don't, but there's no
3640 * way to check for that; the protocol used on the VC
3641 * is negotiated out of band.
3643 return gen_true(cstate
);
3645 case DLT_IEEE802_11
:
3646 case DLT_PRISM_HEADER
:
3647 case DLT_IEEE802_11_RADIO
:
3648 case DLT_IEEE802_11_RADIO_AVS
:
3651 * Check that we have a data frame.
3653 b0
= gen_check_802_11_data_frame(cstate
);
3657 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3663 gen_llc_i(compiler_state_t
*cstate
)
3665 struct block
*b0
, *b1
;
3669 * Check whether this is an LLC frame.
3671 b0
= gen_llc(cstate
);
3674 * Load the control byte and test the low-order bit; it must
3675 * be clear for I frames.
3677 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3678 b1
= new_block(cstate
, JMP(BPF_JSET
));
3687 gen_llc_s(compiler_state_t
*cstate
)
3689 struct block
*b0
, *b1
;
3692 * Check whether this is an LLC frame.
3694 b0
= gen_llc(cstate
);
3697 * Now compare the low-order 2 bit of the control byte against
3698 * the appropriate value for S frames.
3700 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3706 gen_llc_u(compiler_state_t
*cstate
)
3708 struct block
*b0
, *b1
;
3711 * Check whether this is an LLC frame.
3713 b0
= gen_llc(cstate
);
3716 * Now compare the low-order 2 bit of the control byte against
3717 * the appropriate value for U frames.
3719 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3725 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3727 struct block
*b0
, *b1
;
3730 * Check whether this is an LLC frame.
3732 b0
= gen_llc(cstate
);
3735 * Now check for an S frame with the appropriate type.
3737 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3743 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3745 struct block
*b0
, *b1
;
3748 * Check whether this is an LLC frame.
3750 b0
= gen_llc(cstate
);
3753 * Now check for a U frame with the appropriate type.
3755 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3761 * Generate code to match a particular packet type, for link-layer types
3762 * using 802.2 LLC headers.
3764 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3765 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3767 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3768 * value, if <= ETHERMTU. We use that to determine whether to
3769 * match the DSAP or both DSAP and LSAP or to check the OUI and
3770 * protocol ID in a SNAP header.
3772 static struct block
*
3773 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3776 * XXX - handle token-ring variable-length header.
3782 case LLCSAP_NETBEUI
:
3784 * XXX - should we check both the DSAP and the
3785 * SSAP, like this, or should we check just the
3786 * DSAP, as we do for other SAP values?
3788 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3789 ((proto
<< 8) | proto
));
3793 * XXX - are there ever SNAP frames for IPX on
3794 * non-Ethernet 802.x networks?
3796 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3797 (bpf_int32
)LLCSAP_IPX
);
3799 case ETHERTYPE_ATALK
:
3801 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3802 * SNAP packets with an organization code of
3803 * 0x080007 (Apple, for Appletalk) and a protocol
3804 * type of ETHERTYPE_ATALK (Appletalk).
3806 * XXX - check for an organization code of
3807 * encapsulated Ethernet as well?
3809 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3813 * XXX - we don't have to check for IPX 802.3
3814 * here, but should we check for the IPX Ethertype?
3816 if (proto
<= ETHERMTU
) {
3818 * This is an LLC SAP value, so check
3821 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3824 * This is an Ethernet type; we assume that it's
3825 * unlikely that it'll appear in the right place
3826 * at random, and therefore check only the
3827 * location that would hold the Ethernet type
3828 * in a SNAP frame with an organization code of
3829 * 0x000000 (encapsulated Ethernet).
3831 * XXX - if we were to check for the SNAP DSAP and
3832 * LSAP, as per XXX, and were also to check for an
3833 * organization code of 0x000000 (encapsulated
3834 * Ethernet), we'd do
3836 * return gen_snap(cstate, 0x000000, proto);
3838 * here; for now, we don't, as per the above.
3839 * I don't know whether it's worth the extra CPU
3840 * time to do the right check or not.
3842 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3847 static struct block
*
3848 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3849 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3851 struct block
*b0
, *b1
;
3865 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3866 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3872 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3873 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3880 b0
= gen_linktype(cstate
, proto
);
3881 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3887 static struct block
*
3888 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3889 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3891 struct block
*b0
, *b1
;
3906 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3907 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3913 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3914 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3921 /* this order is important */
3922 a
= (uint32_t *)addr
;
3923 m
= (uint32_t *)mask
;
3924 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3925 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3927 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3929 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3931 b0
= gen_linktype(cstate
, proto
);
3937 static struct block
*
3938 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3940 register struct block
*b0
, *b1
;
3944 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
3947 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
3950 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3951 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3957 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3958 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3963 bpf_error(cstate
, "'addr1' is only supported on 802.11 with 802.11 headers");
3967 bpf_error(cstate
, "'addr2' is only supported on 802.11 with 802.11 headers");
3971 bpf_error(cstate
, "'addr3' is only supported on 802.11 with 802.11 headers");
3975 bpf_error(cstate
, "'addr4' is only supported on 802.11 with 802.11 headers");
3979 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
3983 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
3991 * Like gen_ehostop, but for DLT_FDDI
3993 static struct block
*
3994 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3996 struct block
*b0
, *b1
;
4000 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4003 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4006 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4007 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4013 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4014 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4019 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4023 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4027 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4031 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4035 bpf_error(cstate
, "'ra' is only supported on 802.11");
4039 bpf_error(cstate
, "'ta' is only supported on 802.11");
4047 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4049 static struct block
*
4050 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4052 register struct block
*b0
, *b1
;
4056 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4059 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4062 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4063 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4069 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4070 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4075 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4079 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4083 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4087 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4091 bpf_error(cstate
, "'ra' is only supported on 802.11");
4095 bpf_error(cstate
, "'ta' is only supported on 802.11");
4103 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4104 * various 802.11 + radio headers.
4106 static struct block
*
4107 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4109 register struct block
*b0
, *b1
, *b2
;
4110 register struct slist
*s
;
4112 #ifdef ENABLE_WLAN_FILTERING_PATCH
4115 * We need to disable the optimizer because the optimizer is buggy
4116 * and wipes out some LD instructions generated by the below
4117 * code to validate the Frame Control bits
4119 cstate
->no_optimize
= 1;
4120 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4127 * For control frames, there is no SA.
4129 * For management frames, SA is at an
4130 * offset of 10 from the beginning of
4133 * For data frames, SA is at an offset
4134 * of 10 from the beginning of the packet
4135 * if From DS is clear, at an offset of
4136 * 16 from the beginning of the packet
4137 * if From DS is set and To DS is clear,
4138 * and an offset of 24 from the beginning
4139 * of the packet if From DS is set and To DS
4144 * Generate the tests to be done for data frames
4147 * First, check for To DS set, i.e. check "link[1] & 0x01".
4149 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4150 b1
= new_block(cstate
, JMP(BPF_JSET
));
4151 b1
->s
.k
= 0x01; /* To DS */
4155 * If To DS is set, the SA is at 24.
4157 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4161 * Now, check for To DS not set, i.e. check
4162 * "!(link[1] & 0x01)".
4164 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4165 b2
= new_block(cstate
, JMP(BPF_JSET
));
4166 b2
->s
.k
= 0x01; /* To DS */
4171 * If To DS is not set, the SA is at 16.
4173 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4177 * Now OR together the last two checks. That gives
4178 * the complete set of checks for data frames with
4184 * Now check for From DS being set, and AND that with
4185 * the ORed-together checks.
4187 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4188 b1
= new_block(cstate
, JMP(BPF_JSET
));
4189 b1
->s
.k
= 0x02; /* From DS */
4194 * Now check for data frames with From DS not set.
4196 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4197 b2
= new_block(cstate
, JMP(BPF_JSET
));
4198 b2
->s
.k
= 0x02; /* From DS */
4203 * If From DS isn't set, the SA is at 10.
4205 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4209 * Now OR together the checks for data frames with
4210 * From DS not set and for data frames with From DS
4211 * set; that gives the checks done for data frames.
4216 * Now check for a data frame.
4217 * I.e, check "link[0] & 0x08".
4219 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4220 b1
= new_block(cstate
, JMP(BPF_JSET
));
4225 * AND that with the checks done for data frames.
4230 * If the high-order bit of the type value is 0, this
4231 * is a management frame.
4232 * I.e, check "!(link[0] & 0x08)".
4234 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4235 b2
= new_block(cstate
, JMP(BPF_JSET
));
4241 * For management frames, the SA is at 10.
4243 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4247 * OR that with the checks done for data frames.
4248 * That gives the checks done for management and
4254 * If the low-order bit of the type value is 1,
4255 * this is either a control frame or a frame
4256 * with a reserved type, and thus not a
4259 * I.e., check "!(link[0] & 0x04)".
4261 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4262 b1
= new_block(cstate
, JMP(BPF_JSET
));
4268 * AND that with the checks for data and management
4278 * For control frames, there is no DA.
4280 * For management frames, DA is at an
4281 * offset of 4 from the beginning of
4284 * For data frames, DA is at an offset
4285 * of 4 from the beginning of the packet
4286 * if To DS is clear and at an offset of
4287 * 16 from the beginning of the packet
4292 * Generate the tests to be done for data frames.
4294 * First, check for To DS set, i.e. "link[1] & 0x01".
4296 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4297 b1
= new_block(cstate
, JMP(BPF_JSET
));
4298 b1
->s
.k
= 0x01; /* To DS */
4302 * If To DS is set, the DA is at 16.
4304 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4308 * Now, check for To DS not set, i.e. check
4309 * "!(link[1] & 0x01)".
4311 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4312 b2
= new_block(cstate
, JMP(BPF_JSET
));
4313 b2
->s
.k
= 0x01; /* To DS */
4318 * If To DS is not set, the DA is at 4.
4320 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4324 * Now OR together the last two checks. That gives
4325 * the complete set of checks for data frames.
4330 * Now check for a data frame.
4331 * I.e, check "link[0] & 0x08".
4333 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4334 b1
= new_block(cstate
, JMP(BPF_JSET
));
4339 * AND that with the checks done for data frames.
4344 * If the high-order bit of the type value is 0, this
4345 * is a management frame.
4346 * I.e, check "!(link[0] & 0x08)".
4348 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4349 b2
= new_block(cstate
, JMP(BPF_JSET
));
4355 * For management frames, the DA is at 4.
4357 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4361 * OR that with the checks done for data frames.
4362 * That gives the checks done for management and
4368 * If the low-order bit of the type value is 1,
4369 * this is either a control frame or a frame
4370 * with a reserved type, and thus not a
4373 * I.e., check "!(link[0] & 0x04)".
4375 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4376 b1
= new_block(cstate
, JMP(BPF_JSET
));
4382 * AND that with the checks for data and management
4390 * Not present in management frames; addr1 in other
4395 * If the high-order bit of the type value is 0, this
4396 * is a management frame.
4397 * I.e, check "(link[0] & 0x08)".
4399 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4400 b1
= new_block(cstate
, JMP(BPF_JSET
));
4407 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4410 * AND that with the check of addr1.
4417 * Not present in management frames; addr2, if present,
4422 * Not present in CTS or ACK control frames.
4424 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4425 IEEE80211_FC0_TYPE_MASK
);
4427 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4428 IEEE80211_FC0_SUBTYPE_MASK
);
4430 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4431 IEEE80211_FC0_SUBTYPE_MASK
);
4437 * If the high-order bit of the type value is 0, this
4438 * is a management frame.
4439 * I.e, check "(link[0] & 0x08)".
4441 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4442 b1
= new_block(cstate
, JMP(BPF_JSET
));
4447 * AND that with the check for frames other than
4448 * CTS and ACK frames.
4455 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4460 * XXX - add BSSID keyword?
4463 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4467 * Not present in CTS or ACK control frames.
4469 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4470 IEEE80211_FC0_TYPE_MASK
);
4472 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4473 IEEE80211_FC0_SUBTYPE_MASK
);
4475 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4476 IEEE80211_FC0_SUBTYPE_MASK
);
4480 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4486 * Not present in control frames.
4488 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4489 IEEE80211_FC0_TYPE_MASK
);
4491 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4497 * Present only if the direction mask has both "From DS"
4498 * and "To DS" set. Neither control frames nor management
4499 * frames should have both of those set, so we don't
4500 * check the frame type.
4502 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4503 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4504 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4509 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4510 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4516 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4517 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4526 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4527 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4528 * as the RFC states.)
4530 static struct block
*
4531 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4533 register struct block
*b0
, *b1
;
4537 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4540 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4543 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4544 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4550 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4551 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4556 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4560 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4564 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4568 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4572 bpf_error(cstate
, "'ra' is only supported on 802.11");
4576 bpf_error(cstate
, "'ta' is only supported on 802.11");
4584 * This is quite tricky because there may be pad bytes in front of the
4585 * DECNET header, and then there are two possible data packet formats that
4586 * carry both src and dst addresses, plus 5 packet types in a format that
4587 * carries only the src node, plus 2 types that use a different format and
4588 * also carry just the src node.
4592 * Instead of doing those all right, we just look for data packets with
4593 * 0 or 1 bytes of padding. If you want to look at other packets, that
4594 * will require a lot more hacking.
4596 * To add support for filtering on DECNET "areas" (network numbers)
4597 * one would want to add a "mask" argument to this routine. That would
4598 * make the filter even more inefficient, although one could be clever
4599 * and not generate masking instructions if the mask is 0xFFFF.
4601 static struct block
*
4602 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4604 struct block
*b0
, *b1
, *b2
, *tmp
;
4605 u_int offset_lh
; /* offset if long header is received */
4606 u_int offset_sh
; /* offset if short header is received */
4611 offset_sh
= 1; /* follows flags */
4612 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4616 offset_sh
= 3; /* follows flags, dstnode */
4617 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4621 /* Inefficient because we do our Calvinball dance twice */
4622 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4623 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4629 /* Inefficient because we do our Calvinball dance twice */
4630 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4631 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4636 bpf_error(cstate
, "ISO host filtering not implemented");
4641 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4642 /* Check for pad = 1, long header case */
4643 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4644 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4645 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4646 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4648 /* Check for pad = 0, long header case */
4649 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4650 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4653 /* Check for pad = 1, short header case */
4654 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4655 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4656 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4659 /* Check for pad = 0, short header case */
4660 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4661 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4665 /* Combine with test for cstate->linktype */
4671 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4672 * test the bottom-of-stack bit, and then check the version number
4673 * field in the IP header.
4675 static struct block
*
4676 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4678 struct block
*b0
, *b1
;
4683 /* match the bottom-of-stack bit */
4684 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4685 /* match the IPv4 version number */
4686 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4691 /* match the bottom-of-stack bit */
4692 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4693 /* match the IPv4 version number */
4694 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4703 static struct block
*
4704 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4705 int proto
, int dir
, int type
)
4707 struct block
*b0
, *b1
;
4708 const char *typestr
;
4718 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4720 * Only check for non-IPv4 addresses if we're not
4721 * checking MPLS-encapsulated packets.
4723 if (cstate
->label_stack_depth
== 0) {
4724 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4726 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4732 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4735 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4738 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4741 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4744 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4747 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4750 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4753 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4756 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4759 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4762 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4765 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4768 bpf_error(cstate
, "ATALK host filtering not implemented");
4771 bpf_error(cstate
, "AARP host filtering not implemented");
4774 return gen_dnhostop(cstate
, addr
, dir
);
4777 bpf_error(cstate
, "SCA host filtering not implemented");
4780 bpf_error(cstate
, "LAT host filtering not implemented");
4783 bpf_error(cstate
, "MOPDL host filtering not implemented");
4786 bpf_error(cstate
, "MOPRC host filtering not implemented");
4789 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4792 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4795 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4798 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4801 bpf_error(cstate
, "ISO host filtering not implemented");
4804 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4807 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4810 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4813 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4816 bpf_error(cstate
, "IPX host filtering not implemented");
4819 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4822 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4831 static struct block
*
4832 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4833 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4835 const char *typestr
;
4845 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4848 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4851 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4854 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4857 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4860 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4863 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4866 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4869 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4872 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4875 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4878 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4881 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4884 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4887 bpf_error(cstate
, "ATALK host filtering not implemented");
4890 bpf_error(cstate
, "AARP host filtering not implemented");
4893 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
4896 bpf_error(cstate
, "SCA host filtering not implemented");
4899 bpf_error(cstate
, "LAT host filtering not implemented");
4902 bpf_error(cstate
, "MOPDL host filtering not implemented");
4905 bpf_error(cstate
, "MOPRC host filtering not implemented");
4908 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4911 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4914 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4917 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4920 bpf_error(cstate
, "ISO host filtering not implemented");
4923 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4926 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4929 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4932 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4935 bpf_error(cstate
, "IPX host filtering not implemented");
4938 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4941 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4951 static struct block
*
4952 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
, bpf_u_int32
**alist
,
4955 struct block
*b0
, *b1
, *tmp
;
4958 bpf_error(cstate
, "direction applied to 'gateway'");
4965 switch (cstate
->linktype
) {
4967 case DLT_NETANALYZER
:
4968 case DLT_NETANALYZER_TRANSPARENT
:
4969 b1
= gen_prevlinkhdr_check(cstate
);
4970 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
4975 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
4978 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
4980 case DLT_IEEE802_11
:
4981 case DLT_PRISM_HEADER
:
4982 case DLT_IEEE802_11_RADIO_AVS
:
4983 case DLT_IEEE802_11_RADIO
:
4985 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
4989 * This is LLC-multiplexed traffic; if it were
4990 * LANE, cstate->linktype would have been set to
4994 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4996 case DLT_IP_OVER_FC
:
4997 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5001 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5003 b1
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
5005 tmp
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
,
5014 bpf_error(cstate
, "illegal modifier of 'gateway'");
5020 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5028 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5029 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5034 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5035 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5040 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5041 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5046 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5049 #ifndef IPPROTO_IGMP
5050 #define IPPROTO_IGMP 2
5054 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5057 #ifndef IPPROTO_IGRP
5058 #define IPPROTO_IGRP 9
5061 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5065 #define IPPROTO_PIM 103
5069 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5070 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5074 #ifndef IPPROTO_VRRP
5075 #define IPPROTO_VRRP 112
5079 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5082 #ifndef IPPROTO_CARP
5083 #define IPPROTO_CARP 112
5087 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5091 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5095 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5099 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5103 bpf_error(cstate
, "link layer applied in wrong context");
5106 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5110 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5114 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5118 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5122 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5126 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5130 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5134 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5137 #ifndef IPPROTO_ICMPV6
5138 #define IPPROTO_ICMPV6 58
5141 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5145 #define IPPROTO_AH 51
5148 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5149 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5154 #define IPPROTO_ESP 50
5157 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5158 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5163 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5167 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5171 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5174 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5175 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5176 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5178 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5180 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5182 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5186 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5187 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5188 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5190 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5192 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5194 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5198 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5199 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5200 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5202 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5207 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5208 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5213 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5214 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5216 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5218 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5223 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5224 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5229 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5230 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5235 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5239 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5243 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5247 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5251 bpf_error(cstate
, "'radio' is not a valid protocol type");
5259 static struct block
*
5260 gen_ipfrag(compiler_state_t
*cstate
)
5265 /* not IPv4 frag other than the first frag */
5266 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5267 b
= new_block(cstate
, JMP(BPF_JSET
));
5276 * Generate a comparison to a port value in the transport-layer header
5277 * at the specified offset from the beginning of that header.
5279 * XXX - this handles a variable-length prefix preceding the link-layer
5280 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5281 * variable-length link-layer headers (such as Token Ring or 802.11
5284 static struct block
*
5285 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5287 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5290 static struct block
*
5291 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5293 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5297 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5299 struct block
*b0
, *b1
, *tmp
;
5301 /* ip proto 'proto' and not a fragment other than the first fragment */
5302 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5303 b0
= gen_ipfrag(cstate
);
5308 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5312 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5317 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5318 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5323 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5324 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5336 static struct block
*
5337 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5339 struct block
*b0
, *b1
, *tmp
;
5344 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5345 * not LLC encapsulation with LLCSAP_IP.
5347 * For IEEE 802 networks - which includes 802.5 token ring
5348 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5349 * says that SNAP encapsulation is used, not LLC encapsulation
5352 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5353 * RFC 2225 say that SNAP encapsulation is used, not LLC
5354 * encapsulation with LLCSAP_IP.
5356 * So we always check for ETHERTYPE_IP.
5358 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5364 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5368 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5369 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5371 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5383 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5385 struct block
*b0
, *b1
, *tmp
;
5387 /* ip6 proto 'proto' */
5388 /* XXX - catch the first fragment of a fragmented packet? */
5389 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5393 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5397 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5402 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5403 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5408 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5409 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5421 static struct block
*
5422 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5424 struct block
*b0
, *b1
, *tmp
;
5426 /* link proto ip6 */
5427 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5433 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5437 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5438 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5440 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5451 /* gen_portrange code */
5452 static struct block
*
5453 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5456 struct block
*b1
, *b2
;
5460 * Reverse the order of the ports, so v1 is the lower one.
5469 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5470 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5478 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5481 struct block
*b0
, *b1
, *tmp
;
5483 /* ip proto 'proto' and not a fragment other than the first fragment */
5484 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5485 b0
= gen_ipfrag(cstate
);
5490 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5494 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5499 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5500 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5505 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5506 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5518 static struct block
*
5519 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5522 struct block
*b0
, *b1
, *tmp
;
5525 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5531 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5535 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5536 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5538 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5549 static struct block
*
5550 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5553 struct block
*b1
, *b2
;
5557 * Reverse the order of the ports, so v1 is the lower one.
5566 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5567 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5575 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5578 struct block
*b0
, *b1
, *tmp
;
5580 /* ip6 proto 'proto' */
5581 /* XXX - catch the first fragment of a fragmented packet? */
5582 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5586 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5590 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5595 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5596 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5601 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5602 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5614 static struct block
*
5615 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5618 struct block
*b0
, *b1
, *tmp
;
5620 /* link proto ip6 */
5621 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5627 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5631 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5632 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5634 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5646 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5655 v
= pcap_nametoproto(name
);
5656 if (v
== PROTO_UNDEF
)
5657 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5661 /* XXX should look up h/w protocol type based on cstate->linktype */
5662 v
= pcap_nametoeproto(name
);
5663 if (v
== PROTO_UNDEF
) {
5664 v
= pcap_nametollc(name
);
5665 if (v
== PROTO_UNDEF
)
5666 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5671 if (strcmp(name
, "esis") == 0)
5673 else if (strcmp(name
, "isis") == 0)
5675 else if (strcmp(name
, "clnp") == 0)
5678 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5698 static struct block
*
5699 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5701 #ifdef NO_PROTOCHAIN
5702 return gen_proto(cstate
, v
, proto
, dir
);
5704 struct block
*b0
, *b
;
5705 struct slist
*s
[100];
5706 int fix2
, fix3
, fix4
, fix5
;
5707 int ahcheck
, again
, end
;
5709 int reg2
= alloc_reg(cstate
);
5711 memset(s
, 0, sizeof(s
));
5712 fix3
= fix4
= fix5
= 0;
5719 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5720 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5724 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5729 * We don't handle variable-length prefixes before the link-layer
5730 * header, or variable-length link-layer headers, here yet.
5731 * We might want to add BPF instructions to do the protochain
5732 * work, to simplify that and, on platforms that have a BPF
5733 * interpreter with the new instructions, let the filtering
5734 * be done in the kernel. (We already require a modified BPF
5735 * engine to do the protochain stuff, to support backward
5736 * branches, and backward branch support is unlikely to appear
5737 * in kernel BPF engines.)
5739 if (cstate
->off_linkpl
.is_variable
)
5740 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5742 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
5745 * s[0] is a dummy entry to protect other BPF insn from damage
5746 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5747 * hard to find interdependency made by jump table fixup.
5750 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5755 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5758 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5759 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5761 /* X = ip->ip_hl << 2 */
5762 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5763 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5768 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5770 /* A = ip6->ip_nxt */
5771 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5772 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
5774 /* X = sizeof(struct ip6_hdr) */
5775 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
5781 bpf_error(cstate
, "unsupported proto to gen_protochain");
5785 /* again: if (A == v) goto end; else fall through; */
5787 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5789 s
[i
]->s
.jt
= NULL
; /*later*/
5790 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5794 #ifndef IPPROTO_NONE
5795 #define IPPROTO_NONE 59
5797 /* if (A == IPPROTO_NONE) goto end */
5798 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5799 s
[i
]->s
.jt
= NULL
; /*later*/
5800 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5801 s
[i
]->s
.k
= IPPROTO_NONE
;
5802 s
[fix5
]->s
.jf
= s
[i
];
5806 if (proto
== Q_IPV6
) {
5807 int v6start
, v6end
, v6advance
, j
;
5810 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5811 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5812 s
[i
]->s
.jt
= NULL
; /*later*/
5813 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5814 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5815 s
[fix2
]->s
.jf
= s
[i
];
5817 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5818 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5819 s
[i
]->s
.jt
= NULL
; /*later*/
5820 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5821 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5823 /* if (A == IPPROTO_ROUTING) goto v6advance */
5824 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5825 s
[i
]->s
.jt
= NULL
; /*later*/
5826 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5827 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5829 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5830 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5831 s
[i
]->s
.jt
= NULL
; /*later*/
5832 s
[i
]->s
.jf
= NULL
; /*later*/
5833 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5843 * A = P[X + packet head];
5844 * X = X + (P[X + packet head + 1] + 1) * 8;
5846 /* A = P[X + packet head] */
5847 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5848 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5851 s
[i
] = new_stmt(cstate
, BPF_ST
);
5854 /* A = P[X + packet head + 1]; */
5855 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5856 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
5859 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5863 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5867 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
5871 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5874 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5878 /* goto again; (must use BPF_JA for backward jump) */
5879 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5880 s
[i
]->s
.k
= again
- i
- 1;
5881 s
[i
- 1]->s
.jf
= s
[i
];
5885 for (j
= v6start
; j
<= v6end
; j
++)
5886 s
[j
]->s
.jt
= s
[v6advance
];
5889 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5891 s
[fix2
]->s
.jf
= s
[i
];
5897 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5898 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5899 s
[i
]->s
.jt
= NULL
; /*later*/
5900 s
[i
]->s
.jf
= NULL
; /*later*/
5901 s
[i
]->s
.k
= IPPROTO_AH
;
5903 s
[fix3
]->s
.jf
= s
[ahcheck
];
5910 * X = X + (P[X + 1] + 2) * 4;
5913 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5915 /* A = P[X + packet head]; */
5916 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5917 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5920 s
[i
] = new_stmt(cstate
, BPF_ST
);
5924 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5927 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5931 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5933 /* A = P[X + packet head] */
5934 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5935 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5938 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5942 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5946 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5949 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5953 /* goto again; (must use BPF_JA for backward jump) */
5954 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5955 s
[i
]->s
.k
= again
- i
- 1;
5960 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5962 s
[fix2
]->s
.jt
= s
[end
];
5963 s
[fix4
]->s
.jf
= s
[end
];
5964 s
[fix5
]->s
.jt
= s
[end
];
5971 for (i
= 0; i
< max
- 1; i
++)
5972 s
[i
]->next
= s
[i
+ 1];
5973 s
[max
- 1]->next
= NULL
;
5978 b
= new_block(cstate
, JMP(BPF_JEQ
));
5979 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5982 free_reg(cstate
, reg2
);
5989 static struct block
*
5990 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
5993 struct block
*b0
, *b1
;
5996 * A data frame has the 0x08 bit (b3) in the frame control field set
5997 * and the 0x04 bit (b2) clear.
5999 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6000 b0
= new_block(cstate
, JMP(BPF_JSET
));
6004 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6005 b1
= new_block(cstate
, JMP(BPF_JSET
));
6016 * Generate code that checks whether the packet is a packet for protocol
6017 * <proto> and whether the type field in that protocol's header has
6018 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6019 * IP packet and checks the protocol number in the IP header against <v>.
6021 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6022 * against Q_IP and Q_IPV6.
6024 static struct block
*
6025 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6027 struct block
*b0
, *b1
;
6032 if (dir
!= Q_DEFAULT
)
6033 bpf_error(cstate
, "direction applied to 'proto'");
6037 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6038 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6044 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6045 * not LLC encapsulation with LLCSAP_IP.
6047 * For IEEE 802 networks - which includes 802.5 token ring
6048 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6049 * says that SNAP encapsulation is used, not LLC encapsulation
6052 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6053 * RFC 2225 say that SNAP encapsulation is used, not LLC
6054 * encapsulation with LLCSAP_IP.
6056 * So we always check for ETHERTYPE_IP.
6058 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6060 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6062 b1
= gen_protochain(cstate
, v
, Q_IP
);
6068 switch (cstate
->linktype
) {
6072 * Frame Relay packets typically have an OSI
6073 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6074 * generates code to check for all the OSI
6075 * NLPIDs, so calling it and then adding a check
6076 * for the particular NLPID for which we're
6077 * looking is bogus, as we can just check for
6080 * What we check for is the NLPID and a frame
6081 * control field value of UI, i.e. 0x03 followed
6084 * XXX - assumes a 2-byte Frame Relay header with
6085 * DLCI and flags. What if the address is longer?
6087 * XXX - what about SNAP-encapsulated frames?
6089 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6095 * Cisco uses an Ethertype lookalike - for OSI,
6098 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6099 /* OSI in C-HDLC is stuffed with a fudge byte */
6100 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6105 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6106 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6112 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6114 * 4 is the offset of the PDU type relative to the IS-IS
6117 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6122 bpf_error(cstate
, "arp does not encapsulate another protocol");
6126 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6130 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6134 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6138 bpf_error(cstate
, "sca does not encapsulate another protocol");
6142 bpf_error(cstate
, "lat does not encapsulate another protocol");
6146 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6150 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6154 return gen_linktype(cstate
, v
);
6157 bpf_error(cstate
, "'udp proto' is bogus");
6161 bpf_error(cstate
, "'tcp proto' is bogus");
6165 bpf_error(cstate
, "'sctp proto' is bogus");
6169 bpf_error(cstate
, "'icmp proto' is bogus");
6173 bpf_error(cstate
, "'igmp proto' is bogus");
6177 bpf_error(cstate
, "'igrp proto' is bogus");
6181 bpf_error(cstate
, "'pim proto' is bogus");
6185 bpf_error(cstate
, "'vrrp proto' is bogus");
6189 bpf_error(cstate
, "'carp proto' is bogus");
6193 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6196 * Also check for a fragment header before the final
6199 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6200 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6202 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6205 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6211 bpf_error(cstate
, "'icmp6 proto' is bogus");
6214 bpf_error(cstate
, "'ah proto' is bogus");
6217 bpf_error(cstate
, "'ah proto' is bogus");
6220 bpf_error(cstate
, "'stp proto' is bogus");
6223 bpf_error(cstate
, "'ipx proto' is bogus");
6226 bpf_error(cstate
, "'netbeui proto' is bogus");
6229 bpf_error(cstate
, "'radio proto' is bogus");
6239 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6241 int proto
= q
.proto
;
6245 bpf_u_int32 mask
, addr
;
6247 bpf_u_int32
**alist
;
6250 struct sockaddr_in
*sin4
;
6251 struct sockaddr_in6
*sin6
;
6252 struct addrinfo
*res
, *res0
;
6253 struct in6_addr mask128
;
6255 struct block
*b
, *tmp
;
6256 int port
, real_proto
;
6262 addr
= pcap_nametonetaddr(name
);
6264 bpf_error(cstate
, "unknown network '%s'", name
);
6265 /* Left justify network addr and calculate its network mask */
6267 while (addr
&& (addr
& 0xff000000) == 0) {
6271 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6275 if (proto
== Q_LINK
) {
6276 switch (cstate
->linktype
) {
6279 case DLT_NETANALYZER
:
6280 case DLT_NETANALYZER_TRANSPARENT
:
6281 eaddr
= pcap_ether_hostton(name
);
6284 "unknown ether host '%s'", name
);
6285 tmp
= gen_prevlinkhdr_check(cstate
);
6286 b
= gen_ehostop(cstate
, eaddr
, dir
);
6293 eaddr
= pcap_ether_hostton(name
);
6296 "unknown FDDI host '%s'", name
);
6297 b
= gen_fhostop(cstate
, eaddr
, dir
);
6302 eaddr
= pcap_ether_hostton(name
);
6305 "unknown token ring host '%s'", name
);
6306 b
= gen_thostop(cstate
, eaddr
, dir
);
6310 case DLT_IEEE802_11
:
6311 case DLT_PRISM_HEADER
:
6312 case DLT_IEEE802_11_RADIO_AVS
:
6313 case DLT_IEEE802_11_RADIO
:
6315 eaddr
= pcap_ether_hostton(name
);
6318 "unknown 802.11 host '%s'", name
);
6319 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6323 case DLT_IP_OVER_FC
:
6324 eaddr
= pcap_ether_hostton(name
);
6327 "unknown Fibre Channel host '%s'", name
);
6328 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6333 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6334 } else if (proto
== Q_DECNET
) {
6335 unsigned short dn_addr
;
6337 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6339 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6341 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6346 * I don't think DECNET hosts can be multihomed, so
6347 * there is no need to build up a list of addresses
6349 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6352 alist
= pcap_nametoaddr(name
);
6353 if (alist
== NULL
|| *alist
== NULL
)
6354 bpf_error(cstate
, "unknown host '%s'", name
);
6356 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6357 tproto
== Q_DEFAULT
)
6359 b
= gen_host(cstate
, **alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6361 tmp
= gen_host(cstate
, **alist
++, 0xffffffff,
6362 tproto
, dir
, q
.addr
);
6368 memset(&mask128
, 0xff, sizeof(mask128
));
6369 res0
= res
= pcap_nametoaddrinfo(name
);
6371 bpf_error(cstate
, "unknown host '%s'", name
);
6374 tproto
= tproto6
= proto
;
6375 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6376 tproto
== Q_DEFAULT
) {
6380 for (res
= res0
; res
; res
= res
->ai_next
) {
6381 switch (res
->ai_family
) {
6383 if (tproto
== Q_IPV6
)
6386 sin4
= (struct sockaddr_in
*)
6388 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6389 0xffffffff, tproto
, dir
, q
.addr
);
6392 if (tproto6
== Q_IP
)
6395 sin6
= (struct sockaddr_in6
*)
6397 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6398 &mask128
, tproto6
, dir
, q
.addr
);
6410 bpf_error(cstate
, "unknown host '%s'%s", name
,
6411 (proto
== Q_DEFAULT
)
6413 : " for specified address family");
6420 if (proto
!= Q_DEFAULT
&&
6421 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6422 bpf_error(cstate
, "illegal qualifier of 'port'");
6423 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6424 bpf_error(cstate
, "unknown port '%s'", name
);
6425 if (proto
== Q_UDP
) {
6426 if (real_proto
== IPPROTO_TCP
)
6427 bpf_error(cstate
, "port '%s' is tcp", name
);
6428 else if (real_proto
== IPPROTO_SCTP
)
6429 bpf_error(cstate
, "port '%s' is sctp", name
);
6431 /* override PROTO_UNDEF */
6432 real_proto
= IPPROTO_UDP
;
6434 if (proto
== Q_TCP
) {
6435 if (real_proto
== IPPROTO_UDP
)
6436 bpf_error(cstate
, "port '%s' is udp", name
);
6438 else if (real_proto
== IPPROTO_SCTP
)
6439 bpf_error(cstate
, "port '%s' is sctp", name
);
6441 /* override PROTO_UNDEF */
6442 real_proto
= IPPROTO_TCP
;
6444 if (proto
== Q_SCTP
) {
6445 if (real_proto
== IPPROTO_UDP
)
6446 bpf_error(cstate
, "port '%s' is udp", name
);
6448 else if (real_proto
== IPPROTO_TCP
)
6449 bpf_error(cstate
, "port '%s' is tcp", name
);
6451 /* override PROTO_UNDEF */
6452 real_proto
= IPPROTO_SCTP
;
6455 bpf_error(cstate
, "illegal port number %d < 0", port
);
6457 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6458 b
= gen_port(cstate
, port
, real_proto
, dir
);
6459 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6463 if (proto
!= Q_DEFAULT
&&
6464 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6465 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6466 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6467 bpf_error(cstate
, "unknown port in range '%s'", name
);
6468 if (proto
== Q_UDP
) {
6469 if (real_proto
== IPPROTO_TCP
)
6470 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6471 else if (real_proto
== IPPROTO_SCTP
)
6472 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6474 /* override PROTO_UNDEF */
6475 real_proto
= IPPROTO_UDP
;
6477 if (proto
== Q_TCP
) {
6478 if (real_proto
== IPPROTO_UDP
)
6479 bpf_error(cstate
, "port in range '%s' is udp", name
);
6480 else if (real_proto
== IPPROTO_SCTP
)
6481 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6483 /* override PROTO_UNDEF */
6484 real_proto
= IPPROTO_TCP
;
6486 if (proto
== Q_SCTP
) {
6487 if (real_proto
== IPPROTO_UDP
)
6488 bpf_error(cstate
, "port in range '%s' is udp", name
);
6489 else if (real_proto
== IPPROTO_TCP
)
6490 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6492 /* override PROTO_UNDEF */
6493 real_proto
= IPPROTO_SCTP
;
6496 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6498 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6500 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6502 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6504 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6505 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6510 eaddr
= pcap_ether_hostton(name
);
6512 bpf_error(cstate
, "unknown ether host: %s", name
);
6514 alist
= pcap_nametoaddr(name
);
6515 if (alist
== NULL
|| *alist
== NULL
)
6516 bpf_error(cstate
, "unknown host '%s'", name
);
6517 b
= gen_gateway(cstate
, eaddr
, alist
, proto
, dir
);
6521 bpf_error(cstate
, "'gateway' not supported in this configuration");
6525 real_proto
= lookup_proto(cstate
, name
, proto
);
6526 if (real_proto
>= 0)
6527 return gen_proto(cstate
, real_proto
, proto
, dir
);
6529 bpf_error(cstate
, "unknown protocol: %s", name
);
6532 real_proto
= lookup_proto(cstate
, name
, proto
);
6533 if (real_proto
>= 0)
6534 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6536 bpf_error(cstate
, "unknown protocol: %s", name
);
6547 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6548 unsigned int masklen
, struct qual q
)
6550 register int nlen
, mlen
;
6553 nlen
= __pcap_atoin(s1
, &n
);
6554 /* Promote short ipaddr */
6558 mlen
= __pcap_atoin(s2
, &m
);
6559 /* Promote short ipaddr */
6562 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6565 /* Convert mask len to mask */
6567 bpf_error(cstate
, "mask length must be <= 32");
6570 * X << 32 is not guaranteed by C to be 0; it's
6575 m
= 0xffffffff << (32 - masklen
);
6577 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6584 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6587 bpf_error(cstate
, "Mask syntax for networks only");
6595 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6598 int proto
= q
.proto
;
6604 else if (q
.proto
== Q_DECNET
) {
6605 vlen
= __pcap_atodn(s
, &v
);
6607 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6609 vlen
= __pcap_atoin(s
, &v
);
6616 if (proto
== Q_DECNET
)
6617 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6618 else if (proto
== Q_LINK
) {
6619 bpf_error(cstate
, "illegal link layer address");
6622 if (s
== NULL
&& q
.addr
== Q_NET
) {
6623 /* Promote short net number */
6624 while (v
&& (v
& 0xff000000) == 0) {
6629 /* Promote short ipaddr */
6631 mask
<<= 32 - vlen
;
6633 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6638 proto
= IPPROTO_UDP
;
6639 else if (proto
== Q_TCP
)
6640 proto
= IPPROTO_TCP
;
6641 else if (proto
== Q_SCTP
)
6642 proto
= IPPROTO_SCTP
;
6643 else if (proto
== Q_DEFAULT
)
6644 proto
= PROTO_UNDEF
;
6646 bpf_error(cstate
, "illegal qualifier of 'port'");
6649 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6653 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6654 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6660 proto
= IPPROTO_UDP
;
6661 else if (proto
== Q_TCP
)
6662 proto
= IPPROTO_TCP
;
6663 else if (proto
== Q_SCTP
)
6664 proto
= IPPROTO_SCTP
;
6665 else if (proto
== Q_DEFAULT
)
6666 proto
= PROTO_UNDEF
;
6668 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6671 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6675 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6676 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6681 bpf_error(cstate
, "'gateway' requires a name");
6685 return gen_proto(cstate
, (int)v
, proto
, dir
);
6688 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6703 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6704 unsigned int masklen
, struct qual q
)
6706 struct addrinfo
*res
;
6707 struct in6_addr
*addr
;
6708 struct in6_addr mask
;
6713 bpf_error(cstate
, "no mask %s supported", s2
);
6715 res
= pcap_nametoaddrinfo(s1
);
6717 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6720 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6721 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6723 if (sizeof(mask
) * 8 < masklen
)
6724 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6725 memset(&mask
, 0, sizeof(mask
));
6726 memset(&mask
, 0xff, masklen
/ 8);
6728 mask
.s6_addr
[masklen
/ 8] =
6729 (0xff << (8 - masklen
% 8)) & 0xff;
6732 a
= (uint32_t *)addr
;
6733 m
= (uint32_t *)&mask
;
6734 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6735 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6736 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6744 bpf_error(cstate
, "Mask syntax for networks only");
6748 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6754 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6762 gen_ecode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
6764 struct block
*b
, *tmp
;
6766 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6767 switch (cstate
->linktype
) {
6769 case DLT_NETANALYZER
:
6770 case DLT_NETANALYZER_TRANSPARENT
:
6771 tmp
= gen_prevlinkhdr_check(cstate
);
6772 b
= gen_ehostop(cstate
, eaddr
, (int)q
.dir
);
6777 return gen_fhostop(cstate
, eaddr
, (int)q
.dir
);
6779 return gen_thostop(cstate
, eaddr
, (int)q
.dir
);
6780 case DLT_IEEE802_11
:
6781 case DLT_PRISM_HEADER
:
6782 case DLT_IEEE802_11_RADIO_AVS
:
6783 case DLT_IEEE802_11_RADIO
:
6785 return gen_wlanhostop(cstate
, eaddr
, (int)q
.dir
);
6786 case DLT_IP_OVER_FC
:
6787 return gen_ipfchostop(cstate
, eaddr
, (int)q
.dir
);
6789 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6793 bpf_error(cstate
, "ethernet address used in non-ether expression");
6800 struct slist
*s0
, *s1
;
6803 * This is definitely not the best way to do this, but the
6804 * lists will rarely get long.
6811 static struct slist
*
6812 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
6816 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
6821 static struct slist
*
6822 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
6826 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6832 * Modify "index" to use the value stored into its register as an
6833 * offset relative to the beginning of the header for the protocol
6834 * "proto", and allocate a register and put an item "size" bytes long
6835 * (1, 2, or 4) at that offset into that register, making it the register
6839 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
6841 struct slist
*s
, *tmp
;
6843 int regno
= alloc_reg(cstate
);
6845 free_reg(cstate
, inst
->regno
);
6849 bpf_error(cstate
, "data size must be 1, 2, or 4");
6865 bpf_error(cstate
, "unsupported index operation");
6869 * The offset is relative to the beginning of the packet
6870 * data, if we have a radio header. (If we don't, this
6873 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6874 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
6875 cstate
->linktype
!= DLT_PRISM_HEADER
)
6876 bpf_error(cstate
, "radio information not present in capture");
6879 * Load into the X register the offset computed into the
6880 * register specified by "index".
6882 s
= xfer_to_x(cstate
, inst
);
6885 * Load the item at that offset.
6887 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6889 sappend(inst
->s
, s
);
6894 * The offset is relative to the beginning of
6895 * the link-layer header.
6897 * XXX - what about ATM LANE? Should the index be
6898 * relative to the beginning of the AAL5 frame, so
6899 * that 0 refers to the beginning of the LE Control
6900 * field, or relative to the beginning of the LAN
6901 * frame, so that 0 refers, for Ethernet LANE, to
6902 * the beginning of the destination address?
6904 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
6907 * If "s" is non-null, it has code to arrange that the
6908 * X register contains the length of the prefix preceding
6909 * the link-layer header. Add to it the offset computed
6910 * into the register specified by "index", and move that
6911 * into the X register. Otherwise, just load into the X
6912 * register the offset computed into the register specified
6916 sappend(s
, xfer_to_a(cstate
, inst
));
6917 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6918 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6920 s
= xfer_to_x(cstate
, inst
);
6923 * Load the item at the sum of the offset we've put in the
6924 * X register and the offset of the start of the link
6925 * layer header (which is 0 if the radio header is
6926 * variable-length; that header length is what we put
6927 * into the X register and then added to the index).
6929 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6930 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
6932 sappend(inst
->s
, s
);
6946 * The offset is relative to the beginning of
6947 * the network-layer header.
6948 * XXX - are there any cases where we want
6949 * cstate->off_nl_nosnap?
6951 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
6954 * If "s" is non-null, it has code to arrange that the
6955 * X register contains the variable part of the offset
6956 * of the link-layer payload. Add to it the offset
6957 * computed into the register specified by "index",
6958 * and move that into the X register. Otherwise, just
6959 * load into the X register the offset computed into
6960 * the register specified by "index".
6963 sappend(s
, xfer_to_a(cstate
, inst
));
6964 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6965 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6967 s
= xfer_to_x(cstate
, inst
);
6970 * Load the item at the sum of the offset we've put in the
6971 * X register, the offset of the start of the network
6972 * layer header from the beginning of the link-layer
6973 * payload, and the constant part of the offset of the
6974 * start of the link-layer payload.
6976 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6977 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6979 sappend(inst
->s
, s
);
6982 * Do the computation only if the packet contains
6983 * the protocol in question.
6985 b
= gen_proto_abbrev(cstate
, proto
);
6987 gen_and(inst
->b
, b
);
7001 * The offset is relative to the beginning of
7002 * the transport-layer header.
7004 * Load the X register with the length of the IPv4 header
7005 * (plus the offset of the link-layer header, if it's
7006 * a variable-length header), in bytes.
7008 * XXX - are there any cases where we want
7009 * cstate->off_nl_nosnap?
7010 * XXX - we should, if we're built with
7011 * IPv6 support, generate code to load either
7012 * IPv4, IPv6, or both, as appropriate.
7014 s
= gen_loadx_iphdrlen(cstate
);
7017 * The X register now contains the sum of the variable
7018 * part of the offset of the link-layer payload and the
7019 * length of the network-layer header.
7021 * Load into the A register the offset relative to
7022 * the beginning of the transport layer header,
7023 * add the X register to that, move that to the
7024 * X register, and load with an offset from the
7025 * X register equal to the sum of the constant part of
7026 * the offset of the link-layer payload and the offset,
7027 * relative to the beginning of the link-layer payload,
7028 * of the network-layer header.
7030 sappend(s
, xfer_to_a(cstate
, inst
));
7031 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7032 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7033 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7034 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7035 sappend(inst
->s
, s
);
7038 * Do the computation only if the packet contains
7039 * the protocol in question - which is true only
7040 * if this is an IP datagram and is the first or
7041 * only fragment of that datagram.
7043 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7045 gen_and(inst
->b
, b
);
7046 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7051 * Do the computation only if the packet contains
7052 * the protocol in question.
7054 b
= gen_proto_abbrev(cstate
, Q_IPV6
);
7056 gen_and(inst
->b
, b
);
7061 * Check if we have an icmp6 next header
7063 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7065 gen_and(inst
->b
, b
);
7070 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7072 * If "s" is non-null, it has code to arrange that the
7073 * X register contains the variable part of the offset
7074 * of the link-layer payload. Add to it the offset
7075 * computed into the register specified by "index",
7076 * and move that into the X register. Otherwise, just
7077 * load into the X register the offset computed into
7078 * the register specified by "index".
7081 sappend(s
, xfer_to_a(cstate
, inst
));
7082 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7083 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7085 s
= xfer_to_x(cstate
, inst
);
7089 * Load the item at the sum of the offset we've put in the
7090 * X register, the offset of the start of the network
7091 * layer header from the beginning of the link-layer
7092 * payload, and the constant part of the offset of the
7093 * start of the link-layer payload.
7095 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7096 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7099 sappend(inst
->s
, s
);
7103 inst
->regno
= regno
;
7104 s
= new_stmt(cstate
, BPF_ST
);
7106 sappend(inst
->s
, s
);
7112 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7113 struct arth
*a1
, int reversed
)
7115 struct slist
*s0
, *s1
, *s2
;
7116 struct block
*b
, *tmp
;
7118 s0
= xfer_to_x(cstate
, a1
);
7119 s1
= xfer_to_a(cstate
, a0
);
7120 if (code
== BPF_JEQ
) {
7121 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7122 b
= new_block(cstate
, JMP(code
));
7126 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7132 sappend(a0
->s
, a1
->s
);
7136 free_reg(cstate
, a0
->regno
);
7137 free_reg(cstate
, a1
->regno
);
7139 /* 'and' together protocol checks */
7142 gen_and(a0
->b
, tmp
= a1
->b
);
7156 gen_loadlen(compiler_state_t
*cstate
)
7158 int regno
= alloc_reg(cstate
);
7159 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7162 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7163 s
->next
= new_stmt(cstate
, BPF_ST
);
7164 s
->next
->s
.k
= regno
;
7172 gen_loadi(compiler_state_t
*cstate
, int val
)
7178 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7180 reg
= alloc_reg(cstate
);
7182 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7184 s
->next
= new_stmt(cstate
, BPF_ST
);
7193 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7197 s
= xfer_to_a(cstate
, a
);
7199 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7202 s
= new_stmt(cstate
, BPF_ST
);
7210 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7213 struct slist
*s0
, *s1
, *s2
;
7216 * Disallow division by, or modulus by, zero; we do this here
7217 * so that it gets done even if the optimizer is disabled.
7219 if (code
== BPF_DIV
) {
7220 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7221 bpf_error(cstate
, "division by zero");
7222 } else if (code
== BPF_MOD
) {
7223 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7224 bpf_error(cstate
, "modulus by zero");
7226 s0
= xfer_to_x(cstate
, a1
);
7227 s1
= xfer_to_a(cstate
, a0
);
7228 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7233 sappend(a0
->s
, a1
->s
);
7235 free_reg(cstate
, a0
->regno
);
7236 free_reg(cstate
, a1
->regno
);
7238 s0
= new_stmt(cstate
, BPF_ST
);
7239 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7246 * Initialize the table of used registers and the current register.
7249 init_regs(compiler_state_t
*cstate
)
7252 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7256 * Return the next free register.
7259 alloc_reg(compiler_state_t
*cstate
)
7261 int n
= BPF_MEMWORDS
;
7264 if (cstate
->regused
[cstate
->curreg
])
7265 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7267 cstate
->regused
[cstate
->curreg
] = 1;
7268 return cstate
->curreg
;
7271 bpf_error(cstate
, "too many registers needed to evaluate expression");
7277 * Return a register to the table so it can
7281 free_reg(compiler_state_t
*cstate
, int n
)
7283 cstate
->regused
[n
] = 0;
7286 static struct block
*
7287 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7292 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7293 b
= new_block(cstate
, JMP(jmp
));
7301 gen_greater(compiler_state_t
*cstate
, int n
)
7303 return gen_len(cstate
, BPF_JGE
, n
);
7307 * Actually, this is less than or equal.
7310 gen_less(compiler_state_t
*cstate
, int n
)
7314 b
= gen_len(cstate
, BPF_JGT
, n
);
7321 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7322 * the beginning of the link-layer header.
7323 * XXX - that means you can't test values in the radiotap header, but
7324 * as that header is difficult if not impossible to parse generally
7325 * without a loop, that might not be a severe problem. A new keyword
7326 * "radio" could be added for that, although what you'd really want
7327 * would be a way of testing particular radio header values, which
7328 * would generate code appropriate to the radio header in question.
7331 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7341 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7344 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7348 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7352 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7356 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7360 b
= new_block(cstate
, JMP(BPF_JEQ
));
7367 static const u_char abroadcast
[] = { 0x0 };
7370 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7372 bpf_u_int32 hostmask
;
7373 struct block
*b0
, *b1
, *b2
;
7374 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7380 switch (cstate
->linktype
) {
7382 case DLT_ARCNET_LINUX
:
7383 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7385 case DLT_NETANALYZER
:
7386 case DLT_NETANALYZER_TRANSPARENT
:
7387 b1
= gen_prevlinkhdr_check(cstate
);
7388 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7393 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7395 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7396 case DLT_IEEE802_11
:
7397 case DLT_PRISM_HEADER
:
7398 case DLT_IEEE802_11_RADIO_AVS
:
7399 case DLT_IEEE802_11_RADIO
:
7401 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7402 case DLT_IP_OVER_FC
:
7403 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7405 bpf_error(cstate
, "not a broadcast link");
7411 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7412 * as an indication that we don't know the netmask, and fail
7415 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7416 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7417 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7418 hostmask
= ~cstate
->netmask
;
7419 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7420 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7421 (bpf_int32
)(~0 & hostmask
), hostmask
);
7426 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7432 * Generate code to test the low-order bit of a MAC address (that's
7433 * the bottom bit of the *first* byte).
7435 static struct block
*
7436 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7438 register struct block
*b0
;
7439 register struct slist
*s
;
7441 /* link[offset] & 1 != 0 */
7442 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7443 b0
= new_block(cstate
, JMP(BPF_JSET
));
7450 gen_multicast(compiler_state_t
*cstate
, int proto
)
7452 register struct block
*b0
, *b1
, *b2
;
7453 register struct slist
*s
;
7459 switch (cstate
->linktype
) {
7461 case DLT_ARCNET_LINUX
:
7462 /* all ARCnet multicasts use the same address */
7463 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7465 case DLT_NETANALYZER
:
7466 case DLT_NETANALYZER_TRANSPARENT
:
7467 b1
= gen_prevlinkhdr_check(cstate
);
7468 /* ether[0] & 1 != 0 */
7469 b0
= gen_mac_multicast(cstate
, 0);
7475 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7477 * XXX - was that referring to bit-order issues?
7479 /* fddi[1] & 1 != 0 */
7480 return gen_mac_multicast(cstate
, 1);
7482 /* tr[2] & 1 != 0 */
7483 return gen_mac_multicast(cstate
, 2);
7484 case DLT_IEEE802_11
:
7485 case DLT_PRISM_HEADER
:
7486 case DLT_IEEE802_11_RADIO_AVS
:
7487 case DLT_IEEE802_11_RADIO
:
7492 * For control frames, there is no DA.
7494 * For management frames, DA is at an
7495 * offset of 4 from the beginning of
7498 * For data frames, DA is at an offset
7499 * of 4 from the beginning of the packet
7500 * if To DS is clear and at an offset of
7501 * 16 from the beginning of the packet
7506 * Generate the tests to be done for data frames.
7508 * First, check for To DS set, i.e. "link[1] & 0x01".
7510 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7511 b1
= new_block(cstate
, JMP(BPF_JSET
));
7512 b1
->s
.k
= 0x01; /* To DS */
7516 * If To DS is set, the DA is at 16.
7518 b0
= gen_mac_multicast(cstate
, 16);
7522 * Now, check for To DS not set, i.e. check
7523 * "!(link[1] & 0x01)".
7525 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7526 b2
= new_block(cstate
, JMP(BPF_JSET
));
7527 b2
->s
.k
= 0x01; /* To DS */
7532 * If To DS is not set, the DA is at 4.
7534 b1
= gen_mac_multicast(cstate
, 4);
7538 * Now OR together the last two checks. That gives
7539 * the complete set of checks for data frames.
7544 * Now check for a data frame.
7545 * I.e, check "link[0] & 0x08".
7547 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7548 b1
= new_block(cstate
, JMP(BPF_JSET
));
7553 * AND that with the checks done for data frames.
7558 * If the high-order bit of the type value is 0, this
7559 * is a management frame.
7560 * I.e, check "!(link[0] & 0x08)".
7562 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7563 b2
= new_block(cstate
, JMP(BPF_JSET
));
7569 * For management frames, the DA is at 4.
7571 b1
= gen_mac_multicast(cstate
, 4);
7575 * OR that with the checks done for data frames.
7576 * That gives the checks done for management and
7582 * If the low-order bit of the type value is 1,
7583 * this is either a control frame or a frame
7584 * with a reserved type, and thus not a
7587 * I.e., check "!(link[0] & 0x04)".
7589 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7590 b1
= new_block(cstate
, JMP(BPF_JSET
));
7596 * AND that with the checks for data and management
7601 case DLT_IP_OVER_FC
:
7602 b0
= gen_mac_multicast(cstate
, 2);
7607 /* Link not known to support multicasts */
7611 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7612 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7617 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7618 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7622 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7628 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7629 * Outbound traffic is sent by this machine, while inbound traffic is
7630 * sent by a remote machine (and may include packets destined for a
7631 * unicast or multicast link-layer address we are not subscribing to).
7632 * These are the same definitions implemented by pcap_setdirection().
7633 * Capturing only unicast traffic destined for this host is probably
7634 * better accomplished using a higher-layer filter.
7637 gen_inbound(compiler_state_t
*cstate
, int dir
)
7639 register struct block
*b0
;
7642 * Only some data link types support inbound/outbound qualifiers.
7644 switch (cstate
->linktype
) {
7646 b0
= gen_relation(cstate
, BPF_JEQ
,
7647 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7648 gen_loadi(cstate
, 0),
7654 /* match outgoing packets */
7655 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7657 /* match incoming packets */
7658 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7663 /* match outgoing packets */
7664 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7666 /* to filter on inbound traffic, invert the match */
7671 #ifdef HAVE_NET_PFVAR_H
7673 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7674 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7680 /* match outgoing packets */
7681 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7683 /* match incoming packets */
7684 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7688 case DLT_JUNIPER_MFR
:
7689 case DLT_JUNIPER_MLFR
:
7690 case DLT_JUNIPER_MLPPP
:
7691 case DLT_JUNIPER_ATM1
:
7692 case DLT_JUNIPER_ATM2
:
7693 case DLT_JUNIPER_PPPOE
:
7694 case DLT_JUNIPER_PPPOE_ATM
:
7695 case DLT_JUNIPER_GGSN
:
7696 case DLT_JUNIPER_ES
:
7697 case DLT_JUNIPER_MONITOR
:
7698 case DLT_JUNIPER_SERVICES
:
7699 case DLT_JUNIPER_ETHER
:
7700 case DLT_JUNIPER_PPP
:
7701 case DLT_JUNIPER_FRELAY
:
7702 case DLT_JUNIPER_CHDLC
:
7703 case DLT_JUNIPER_VP
:
7704 case DLT_JUNIPER_ST
:
7705 case DLT_JUNIPER_ISM
:
7706 case DLT_JUNIPER_VS
:
7707 case DLT_JUNIPER_SRX_E2E
:
7708 case DLT_JUNIPER_FIBRECHANNEL
:
7709 case DLT_JUNIPER_ATM_CEMIC
:
7711 /* juniper flags (including direction) are stored
7712 * the byte after the 3-byte magic number */
7714 /* match outgoing packets */
7715 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7717 /* match incoming packets */
7718 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7724 * If we have packet meta-data indicating a direction,
7725 * and that metadata can be checked by BPF code, check
7726 * it. Otherwise, give up, as this link-layer type has
7727 * nothing in the packet data.
7729 * Currently, the only platform where a BPF filter can
7730 * check that metadata is Linux with the in-kernel
7731 * BPF interpreter. If other packet capture mechanisms
7732 * and BPF filters also supported this, it would be
7733 * nice. It would be even better if they made that
7734 * metadata available so that we could provide it
7735 * with newer capture APIs, allowing it to be saved
7738 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7740 * This is Linux with PF_PACKET support.
7741 * If this is a *live* capture, we can look at
7742 * special meta-data in the filter expression;
7743 * if it's a savefile, we can't.
7745 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7746 /* We have a FILE *, so this is a savefile */
7747 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
7752 /* match outgoing packets */
7753 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7756 /* to filter on inbound traffic, invert the match */
7759 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7760 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
7764 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7769 #ifdef HAVE_NET_PFVAR_H
7770 /* PF firewall log matched interface */
7772 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7777 if (cstate
->linktype
!= DLT_PFLOG
) {
7778 bpf_error(cstate
, "ifname supported only on PF linktype");
7781 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7782 off
= offsetof(struct pfloghdr
, ifname
);
7783 if (strlen(ifname
) >= len
) {
7784 bpf_error(cstate
, "ifname interface names can only be %d characters",
7788 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7792 /* PF firewall log ruleset name */
7794 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7798 if (cstate
->linktype
!= DLT_PFLOG
) {
7799 bpf_error(cstate
, "ruleset supported only on PF linktype");
7803 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7804 bpf_error(cstate
, "ruleset names can only be %ld characters",
7805 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7809 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7810 strlen(ruleset
), (const u_char
*)ruleset
);
7814 /* PF firewall log rule number */
7816 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7820 if (cstate
->linktype
!= DLT_PFLOG
) {
7821 bpf_error(cstate
, "rnr supported only on PF linktype");
7825 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7830 /* PF firewall log sub-rule number */
7832 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7836 if (cstate
->linktype
!= DLT_PFLOG
) {
7837 bpf_error(cstate
, "srnr supported only on PF linktype");
7841 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7846 /* PF firewall log reason code */
7848 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7852 if (cstate
->linktype
!= DLT_PFLOG
) {
7853 bpf_error(cstate
, "reason supported only on PF linktype");
7857 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7862 /* PF firewall log action */
7864 gen_pf_action(compiler_state_t
*cstate
, int action
)
7868 if (cstate
->linktype
!= DLT_PFLOG
) {
7869 bpf_error(cstate
, "action supported only on PF linktype");
7873 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7877 #else /* !HAVE_NET_PFVAR_H */
7879 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7881 bpf_error(cstate
, "libpcap was compiled without pf support");
7887 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7889 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7895 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7897 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7903 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7905 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7911 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7913 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7919 gen_pf_action(compiler_state_t
*cstate
, int action
)
7921 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7925 #endif /* HAVE_NET_PFVAR_H */
7927 /* IEEE 802.11 wireless header */
7929 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
7933 switch (cstate
->linktype
) {
7935 case DLT_IEEE802_11
:
7936 case DLT_PRISM_HEADER
:
7937 case DLT_IEEE802_11_RADIO_AVS
:
7938 case DLT_IEEE802_11_RADIO
:
7939 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7944 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
7952 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
7956 switch (cstate
->linktype
) {
7958 case DLT_IEEE802_11
:
7959 case DLT_PRISM_HEADER
:
7960 case DLT_IEEE802_11_RADIO_AVS
:
7961 case DLT_IEEE802_11_RADIO
:
7965 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
7969 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7970 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7976 gen_acode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
7978 switch (cstate
->linktype
) {
7981 case DLT_ARCNET_LINUX
:
7982 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7984 return (gen_ahostop(cstate
, eaddr
, (int)q
.dir
));
7986 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7992 bpf_error(cstate
, "aid supported only on ARCnet");
7995 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8000 static struct block
*
8001 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8003 register struct block
*b0
, *b1
;
8006 /* src comes first, different from Ethernet */
8008 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8011 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8014 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8015 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8021 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8022 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8027 bpf_error(cstate
, "'addr1' is only supported on 802.11");
8031 bpf_error(cstate
, "'addr2' is only supported on 802.11");
8035 bpf_error(cstate
, "'addr3' is only supported on 802.11");
8039 bpf_error(cstate
, "'addr4' is only supported on 802.11");
8043 bpf_error(cstate
, "'ra' is only supported on 802.11");
8047 bpf_error(cstate
, "'ta' is only supported on 802.11");
8054 static struct block
*
8055 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8057 struct block
*b0
, *b1
;
8059 /* check for VLAN, including QinQ */
8060 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8061 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8064 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8070 static struct block
*
8071 gen_vlan_vid_test(compiler_state_t
*cstate
, int vlan_num
)
8073 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8076 static struct block
*
8077 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8079 struct block
*b0
, *b1
;
8081 b0
= gen_vlan_tpid_test(cstate
);
8083 if (vlan_num
>= 0) {
8084 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8090 * Both payload and link header type follow the VLAN tags so that
8091 * both need to be updated.
8093 cstate
->off_linkpl
.constant_part
+= 4;
8094 cstate
->off_linktype
.constant_part
+= 4;
8099 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8100 /* add v to variable part of off */
8102 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8106 if (!off
->is_variable
)
8107 off
->is_variable
= 1;
8109 off
->reg
= alloc_reg(cstate
);
8111 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8114 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8117 s2
= new_stmt(cstate
, BPF_ST
);
8123 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8124 * and link type offsets first
8127 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8131 /* offset determined at run time, shift variable part */
8133 cstate
->is_vlan_vloffset
= 1;
8134 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8135 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8137 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8138 sappend(s
.next
, b_tpid
->head
->stmts
);
8139 b_tpid
->head
->stmts
= s
.next
;
8143 * patch block b_vid (VLAN id test) to load VID value either from packet
8144 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8147 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8149 struct slist
*s
, *s2
, *sjeq
;
8152 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8153 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8155 /* true -> next instructions, false -> beginning of b_vid */
8156 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8158 sjeq
->s
.jf
= b_vid
->stmts
;
8161 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8162 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8166 /* jump to the test in b_vid (bypass loading VID from packet data) */
8168 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8170 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8174 /* insert our statements at the beginning of b_vid */
8175 sappend(s
, b_vid
->stmts
);
8180 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8181 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8182 * tag can be either in metadata or in packet data; therefore if the
8183 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8184 * header for VLAN tag. As the decision is done at run time, we need
8185 * update variable part of the offsets
8187 static struct block
*
8188 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8190 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8193 /* generate new filter code based on extracting packet
8195 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8196 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8198 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8203 * This is tricky. We need to insert the statements updating variable
8204 * parts of offsets before the the traditional TPID and VID tests so
8205 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8206 * we do not want this update to affect those checks. That's why we
8207 * generate both test blocks first and insert the statements updating
8208 * variable parts of both offsets after that. This wouldn't work if
8209 * there already were variable length link header when entering this
8210 * function but gen_vlan_bpf_extensions() isn't called in that case.
8212 b_tpid
= gen_vlan_tpid_test(cstate
);
8214 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8216 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8220 if (vlan_num
>= 0) {
8221 gen_vlan_patch_vid_test(cstate
, b_vid
);
8231 * support IEEE 802.1Q VLAN trunk over ethernet
8234 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
8238 /* can't check for VLAN-encapsulated packets inside MPLS */
8239 if (cstate
->label_stack_depth
> 0)
8240 bpf_error(cstate
, "no VLAN match after MPLS");
8243 * Check for a VLAN packet, and then change the offsets to point
8244 * to the type and data fields within the VLAN packet. Just
8245 * increment the offsets, so that we can support a hierarchy, e.g.
8246 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8249 * XXX - this is a bit of a kludge. If we were to split the
8250 * compiler into a parser that parses an expression and
8251 * generates an expression tree, and a code generator that
8252 * takes an expression tree (which could come from our
8253 * parser or from some other parser) and generates BPF code,
8254 * we could perhaps make the offsets parameters of routines
8255 * and, in the handler for an "AND" node, pass to subnodes
8256 * other than the VLAN node the adjusted offsets.
8258 * This would mean that "vlan" would, instead of changing the
8259 * behavior of *all* tests after it, change only the behavior
8260 * of tests ANDed with it. That would change the documented
8261 * semantics of "vlan", which might break some expressions.
8262 * However, it would mean that "(vlan and ip) or ip" would check
8263 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8264 * checking only for VLAN-encapsulated IP, so that could still
8265 * be considered worth doing; it wouldn't break expressions
8266 * that are of the form "vlan and ..." or "vlan N and ...",
8267 * which I suspect are the most common expressions involving
8268 * "vlan". "vlan or ..." doesn't necessarily do what the user
8269 * would really want, now, as all the "or ..." tests would
8270 * be done assuming a VLAN, even though the "or" could be viewed
8271 * as meaning "or, if this isn't a VLAN packet...".
8273 switch (cstate
->linktype
) {
8276 case DLT_NETANALYZER
:
8277 case DLT_NETANALYZER_TRANSPARENT
:
8278 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8279 /* Verify that this is the outer part of the packet and
8280 * not encapsulated somehow. */
8281 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8282 cstate
->off_linkhdr
.constant_part
==
8283 cstate
->off_outermostlinkhdr
.constant_part
) {
8285 * Do we need special VLAN handling?
8287 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8288 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8290 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8293 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8296 case DLT_IEEE802_11
:
8297 case DLT_PRISM_HEADER
:
8298 case DLT_IEEE802_11_RADIO_AVS
:
8299 case DLT_IEEE802_11_RADIO
:
8300 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8304 bpf_error(cstate
, "no VLAN support for data link type %d",
8309 cstate
->vlan_stack_depth
++;
8318 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8320 struct block
*b0
, *b1
;
8322 if (cstate
->label_stack_depth
> 0) {
8323 /* just match the bottom-of-stack bit clear */
8324 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8327 * We're not in an MPLS stack yet, so check the link-layer
8328 * type against MPLS.
8330 switch (cstate
->linktype
) {
8332 case DLT_C_HDLC
: /* fall through */
8334 case DLT_NETANALYZER
:
8335 case DLT_NETANALYZER_TRANSPARENT
:
8336 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8340 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8343 /* FIXME add other DLT_s ...
8344 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8345 * leave it for now */
8348 bpf_error(cstate
, "no MPLS support for data link type %d",
8356 /* If a specific MPLS label is requested, check it */
8357 if (label_num
>= 0) {
8358 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8359 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8360 0xfffff000); /* only compare the first 20 bits */
8366 * Change the offsets to point to the type and data fields within
8367 * the MPLS packet. Just increment the offsets, so that we
8368 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8369 * capture packets with an outer label of 100000 and an inner
8372 * Increment the MPLS stack depth as well; this indicates that
8373 * we're checking MPLS-encapsulated headers, to make sure higher
8374 * level code generators don't try to match against IP-related
8375 * protocols such as Q_ARP, Q_RARP etc.
8377 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8379 cstate
->off_nl_nosnap
+= 4;
8380 cstate
->off_nl
+= 4;
8381 cstate
->label_stack_depth
++;
8386 * Support PPPOE discovery and session.
8389 gen_pppoed(compiler_state_t
*cstate
)
8391 /* check for PPPoE discovery */
8392 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8396 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8398 struct block
*b0
, *b1
;
8401 * Test against the PPPoE session link-layer type.
8403 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8405 /* If a specific session is requested, check PPPoE session id */
8406 if (sess_num
>= 0) {
8407 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8408 (bpf_int32
)sess_num
, 0x0000ffff);
8414 * Change the offsets to point to the type and data fields within
8415 * the PPP packet, and note that this is PPPoE rather than
8418 * XXX - this is a bit of a kludge. If we were to split the
8419 * compiler into a parser that parses an expression and
8420 * generates an expression tree, and a code generator that
8421 * takes an expression tree (which could come from our
8422 * parser or from some other parser) and generates BPF code,
8423 * we could perhaps make the offsets parameters of routines
8424 * and, in the handler for an "AND" node, pass to subnodes
8425 * other than the PPPoE node the adjusted offsets.
8427 * This would mean that "pppoes" would, instead of changing the
8428 * behavior of *all* tests after it, change only the behavior
8429 * of tests ANDed with it. That would change the documented
8430 * semantics of "pppoes", which might break some expressions.
8431 * However, it would mean that "(pppoes and ip) or ip" would check
8432 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8433 * checking only for VLAN-encapsulated IP, so that could still
8434 * be considered worth doing; it wouldn't break expressions
8435 * that are of the form "pppoes and ..." which I suspect are the
8436 * most common expressions involving "pppoes". "pppoes or ..."
8437 * doesn't necessarily do what the user would really want, now,
8438 * as all the "or ..." tests would be done assuming PPPoE, even
8439 * though the "or" could be viewed as meaning "or, if this isn't
8440 * a PPPoE packet...".
8442 * The "network-layer" protocol is PPPoE, which has a 6-byte
8443 * PPPoE header, followed by a PPP packet.
8445 * There is no HDLC encapsulation for the PPP packet (it's
8446 * encapsulated in PPPoES instead), so the link-layer type
8447 * starts at the first byte of the PPP packet. For PPPoE,
8448 * that offset is relative to the beginning of the total
8449 * link-layer payload, including any 802.2 LLC header, so
8450 * it's 6 bytes past cstate->off_nl.
8452 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8453 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8454 cstate
->off_linkpl
.reg
);
8456 cstate
->off_linktype
= cstate
->off_linkhdr
;
8457 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8460 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8465 /* Check that this is Geneve and the VNI is correct if
8466 * specified. Parameterized to handle both IPv4 and IPv6. */
8467 static struct block
*
8468 gen_geneve_check(compiler_state_t
*cstate
,
8469 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8470 enum e_offrel offrel
, int vni
)
8472 struct block
*b0
, *b1
;
8474 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8476 /* Check that we are operating on version 0. Otherwise, we
8477 * can't decode the rest of the fields. The version is 2 bits
8478 * in the first byte of the Geneve header. */
8479 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8484 vni
<<= 8; /* VNI is in the upper 3 bytes */
8485 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8494 /* The IPv4 and IPv6 Geneve checks need to do two things:
8495 * - Verify that this actually is Geneve with the right VNI.
8496 * - Place the IP header length (plus variable link prefix if
8497 * needed) into register A to be used later to compute
8498 * the inner packet offsets. */
8499 static struct block
*
8500 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8502 struct block
*b0
, *b1
;
8503 struct slist
*s
, *s1
;
8505 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8507 /* Load the IP header length into A. */
8508 s
= gen_loadx_iphdrlen(cstate
);
8510 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8513 /* Forcibly append these statements to the true condition
8514 * of the protocol check by creating a new block that is
8515 * always true and ANDing them. */
8516 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8525 static struct block
*
8526 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8528 struct block
*b0
, *b1
;
8529 struct slist
*s
, *s1
;
8531 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8533 /* Load the IP header length. We need to account for a
8534 * variable length link prefix if there is one. */
8535 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8537 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8541 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8545 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8549 /* Forcibly append these statements to the true condition
8550 * of the protocol check by creating a new block that is
8551 * always true and ANDing them. */
8552 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8555 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8564 /* We need to store three values based on the Geneve header::
8565 * - The offset of the linktype.
8566 * - The offset of the end of the Geneve header.
8567 * - The offset of the end of the encapsulated MAC header. */
8568 static struct slist
*
8569 gen_geneve_offsets(compiler_state_t
*cstate
)
8571 struct slist
*s
, *s1
, *s_proto
;
8573 /* First we need to calculate the offset of the Geneve header
8574 * itself. This is composed of the IP header previously calculated
8575 * (include any variable link prefix) and stored in A plus the
8576 * fixed sized headers (fixed link prefix, MAC length, and UDP
8578 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8579 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8581 /* Stash this in X since we'll need it later. */
8582 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8585 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8587 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8591 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8592 cstate
->off_linktype
.is_variable
= 1;
8593 cstate
->off_linktype
.constant_part
= 0;
8595 s1
= new_stmt(cstate
, BPF_ST
);
8596 s1
->s
.k
= cstate
->off_linktype
.reg
;
8599 /* Load the Geneve option length and mask and shift to get the
8600 * number of bytes. It is stored in the first byte of the Geneve
8602 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8606 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8610 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8614 /* Add in the rest of the Geneve base header. */
8615 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8619 /* Add the Geneve header length to its offset and store. */
8620 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8624 /* Set the encapsulated type as Ethernet. Even though we may
8625 * not actually have Ethernet inside there are two reasons this
8627 * - The linktype field is always in EtherType format regardless
8628 * of whether it is in Geneve or an inner Ethernet frame.
8629 * - The only link layer that we have specific support for is
8630 * Ethernet. We will confirm that the packet actually is
8631 * Ethernet at runtime before executing these checks. */
8632 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8634 s1
= new_stmt(cstate
, BPF_ST
);
8635 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8638 /* Calculate whether we have an Ethernet header or just raw IP/
8639 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8640 * and linktype by 14 bytes so that the network header can be found
8641 * seamlessly. Otherwise, keep what we've calculated already. */
8643 /* We have a bare jmp so we can't use the optimizer. */
8644 cstate
->no_optimize
= 1;
8646 /* Load the EtherType in the Geneve header, 2 bytes in. */
8647 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8651 /* Load X with the end of the Geneve header. */
8652 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8653 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8656 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8657 * end of this check, we should have the total length in X. In
8658 * the non-Ethernet case, it's already there. */
8659 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8660 s_proto
->s
.k
= ETHERTYPE_TEB
;
8661 sappend(s
, s_proto
);
8663 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8667 /* Since this is Ethernet, use the EtherType of the payload
8668 * directly as the linktype. Overwrite what we already have. */
8669 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8673 s1
= new_stmt(cstate
, BPF_ST
);
8674 s1
->s
.k
= cstate
->off_linktype
.reg
;
8677 /* Advance two bytes further to get the end of the Ethernet
8679 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8683 /* Move the result to X. */
8684 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8687 /* Store the final result of our linkpl calculation. */
8688 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8689 cstate
->off_linkpl
.is_variable
= 1;
8690 cstate
->off_linkpl
.constant_part
= 0;
8692 s1
= new_stmt(cstate
, BPF_STX
);
8693 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8702 /* Check to see if this is a Geneve packet. */
8704 gen_geneve(compiler_state_t
*cstate
, int vni
)
8706 struct block
*b0
, *b1
;
8709 b0
= gen_geneve4(cstate
, vni
);
8710 b1
= gen_geneve6(cstate
, vni
);
8715 /* Later filters should act on the payload of the Geneve frame,
8716 * update all of the header pointers. Attach this code so that
8717 * it gets executed in the event that the Geneve filter matches. */
8718 s
= gen_geneve_offsets(cstate
);
8720 b1
= gen_true(cstate
);
8721 sappend(s
, b1
->stmts
);
8726 cstate
->is_geneve
= 1;
8731 /* Check that the encapsulated frame has a link layer header
8732 * for Ethernet filters. */
8733 static struct block
*
8734 gen_geneve_ll_check(compiler_state_t
*cstate
)
8737 struct slist
*s
, *s1
;
8739 /* The easiest way to see if there is a link layer present
8740 * is to check if the link layer header and payload are not
8743 /* Geneve always generates pure variable offsets so we can
8744 * compare only the registers. */
8745 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8746 s
->s
.k
= cstate
->off_linkhdr
.reg
;
8748 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8749 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8752 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8761 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
8762 bpf_u_int32 jtype
, int reverse
)
8769 if (!cstate
->is_atm
)
8770 bpf_error(cstate
, "'vpi' supported only on raw ATM");
8771 if (cstate
->off_vpi
== (u_int
)-1)
8773 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
8778 if (!cstate
->is_atm
)
8779 bpf_error(cstate
, "'vci' supported only on raw ATM");
8780 if (cstate
->off_vci
== (u_int
)-1)
8782 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
8787 if (cstate
->off_proto
== (u_int
)-1)
8788 abort(); /* XXX - this isn't on FreeBSD */
8789 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
8794 if (cstate
->off_payload
== (u_int
)-1)
8796 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
8797 0xffffffff, jtype
, reverse
, jvalue
);
8801 if (!cstate
->is_atm
)
8802 bpf_error(cstate
, "'callref' supported only on raw ATM");
8803 if (cstate
->off_proto
== (u_int
)-1)
8805 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
8806 jtype
, reverse
, jvalue
);
8816 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
8818 struct block
*b0
, *b1
;
8823 /* Get all packets in Meta signalling Circuit */
8824 if (!cstate
->is_atm
)
8825 bpf_error(cstate
, "'metac' supported only on raw ATM");
8826 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8827 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
8832 /* Get all packets in Broadcast Circuit*/
8833 if (!cstate
->is_atm
)
8834 bpf_error(cstate
, "'bcc' supported only on raw ATM");
8835 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8836 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
8841 /* Get all cells in Segment OAM F4 circuit*/
8842 if (!cstate
->is_atm
)
8843 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
8844 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8845 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8850 /* Get all cells in End-to-End OAM F4 Circuit*/
8851 if (!cstate
->is_atm
)
8852 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
8853 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8854 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8859 /* Get all packets in connection Signalling Circuit */
8860 if (!cstate
->is_atm
)
8861 bpf_error(cstate
, "'sc' supported only on raw ATM");
8862 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8863 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
8868 /* Get all packets in ILMI Circuit */
8869 if (!cstate
->is_atm
)
8870 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
8871 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8872 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
8877 /* Get all LANE packets */
8878 if (!cstate
->is_atm
)
8879 bpf_error(cstate
, "'lane' supported only on raw ATM");
8880 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8883 * Arrange that all subsequent tests assume LANE
8884 * rather than LLC-encapsulated packets, and set
8885 * the offsets appropriately for LANE-encapsulated
8888 * We assume LANE means Ethernet, not Token Ring.
8890 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
8891 cstate
->off_payload
+ 2, /* Ethernet header */
8893 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
8894 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
8895 cstate
->off_nl
= 0; /* Ethernet II */
8896 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
8900 /* Get all LLC-encapsulated packets */
8901 if (!cstate
->is_atm
)
8902 bpf_error(cstate
, "'llc' supported only on raw ATM");
8903 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8904 cstate
->linktype
= cstate
->prevlinktype
;
8914 * Filtering for MTP2 messages based on li value
8915 * FISU, length is null
8916 * LSSU, length is 1 or 2
8917 * MSU, length is 3 or more
8918 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8921 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
8923 struct block
*b0
, *b1
;
8928 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8929 (cstate
->linktype
!= DLT_ERF
) &&
8930 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8931 bpf_error(cstate
, "'fisu' supported only on MTP2");
8932 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8933 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8937 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8938 (cstate
->linktype
!= DLT_ERF
) &&
8939 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8940 bpf_error(cstate
, "'lssu' supported only on MTP2");
8941 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8942 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8947 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8948 (cstate
->linktype
!= DLT_ERF
) &&
8949 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8950 bpf_error(cstate
, "'msu' supported only on MTP2");
8951 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8955 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8956 (cstate
->linktype
!= DLT_ERF
) &&
8957 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8958 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
8959 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8960 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8964 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8965 (cstate
->linktype
!= DLT_ERF
) &&
8966 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8967 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
8968 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8969 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8974 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8975 (cstate
->linktype
!= DLT_ERF
) &&
8976 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8977 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
8978 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8988 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
8989 bpf_u_int32 jtype
, int reverse
)
8992 bpf_u_int32 val1
, val2
, val3
;
8993 u_int newoff_sio
= cstate
->off_sio
;
8994 u_int newoff_opc
= cstate
->off_opc
;
8995 u_int newoff_dpc
= cstate
->off_dpc
;
8996 u_int newoff_sls
= cstate
->off_sls
;
8998 switch (mtp3field
) {
9001 newoff_sio
+= 3; /* offset for MTP2_HSL */
9005 if (cstate
->off_sio
== (u_int
)-1)
9006 bpf_error(cstate
, "'sio' supported only on SS7");
9007 /* sio coded on 1 byte so max value 255 */
9009 bpf_error(cstate
, "sio value %u too big; max value = 255",
9011 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9012 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9018 if (cstate
->off_opc
== (u_int
)-1)
9019 bpf_error(cstate
, "'opc' supported only on SS7");
9020 /* opc coded on 14 bits so max value 16383 */
9022 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9024 /* the following instructions are made to convert jvalue
9025 * to the form used to write opc in an ss7 message*/
9026 val1
= jvalue
& 0x00003c00;
9028 val2
= jvalue
& 0x000003fc;
9030 val3
= jvalue
& 0x00000003;
9032 jvalue
= val1
+ val2
+ val3
;
9033 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9034 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9042 if (cstate
->off_dpc
== (u_int
)-1)
9043 bpf_error(cstate
, "'dpc' supported only on SS7");
9044 /* dpc coded on 14 bits so max value 16383 */
9046 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9048 /* the following instructions are made to convert jvalue
9049 * to the forme used to write dpc in an ss7 message*/
9050 val1
= jvalue
& 0x000000ff;
9052 val2
= jvalue
& 0x00003f00;
9054 jvalue
= val1
+ val2
;
9055 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9056 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9062 if (cstate
->off_sls
== (u_int
)-1)
9063 bpf_error(cstate
, "'sls' supported only on SS7");
9064 /* sls coded on 4 bits so max value 15 */
9066 bpf_error(cstate
, "sls value %u too big; max value = 15",
9068 /* the following instruction is made to convert jvalue
9069 * to the forme used to write sls in an ss7 message*/
9070 jvalue
= jvalue
<< 4;
9071 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9072 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9081 static struct block
*
9082 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9087 * Q.2931 signalling protocol messages for handling virtual circuits
9088 * establishment and teardown
9093 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9097 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9101 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9105 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9109 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9112 case A_RELEASE_DONE
:
9113 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9123 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9125 struct block
*b0
, *b1
;
9130 if (!cstate
->is_atm
)
9131 bpf_error(cstate
, "'oam' supported only on raw ATM");
9132 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
9136 if (!cstate
->is_atm
)
9137 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9139 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9140 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9142 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9148 * Get Q.2931 signalling messages for switched
9149 * virtual connection
9151 if (!cstate
->is_atm
)
9152 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9153 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9154 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9156 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9158 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9160 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9162 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9164 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9169 if (!cstate
->is_atm
)
9170 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9171 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9172 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9174 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9176 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9178 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9180 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);