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>
37 #include <sys/param.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
57 #include "ethertype.h"
61 #include "ieee80211.h"
63 #include "sunatmpos.h"
66 #include "pcap/ipnet.h"
72 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
73 #include <linux/types.h>
74 #include <linux/if_packet.h>
75 #include <linux/filter.h>
78 #ifdef HAVE_NET_PFVAR_H
79 #include <sys/socket.h>
81 #include <net/pfvar.h>
82 #include <net/if_pflog.h>
86 #define offsetof(s, e) ((size_t)&((s *)0)->e)
91 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
97 u_int8_t u6_addr8
[16];
98 u_int16_t u6_addr16
[8];
99 u_int32_t u6_addr32
[4];
101 #define s6_addr in6_u.u6_addr8
102 #define s6_addr16 in6_u.u6_addr16
103 #define s6_addr32 in6_u.u6_addr32
104 #define s6_addr64 in6_u.u6_addr64
107 typedef unsigned short sa_family_t
;
109 #define __SOCKADDR_COMMON(sa_prefix) \
110 sa_family_t sa_prefix##family
112 /* Ditto, for IPv6. */
115 __SOCKADDR_COMMON (sin6_
);
116 u_int16_t sin6_port
; /* Transport layer port # */
117 u_int32_t sin6_flowinfo
; /* IPv6 flow information */
118 struct in6_addr sin6_addr
; /* IPv6 address */
121 #ifndef EAI_ADDRFAMILY
123 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
124 int ai_family
; /* PF_xxx */
125 int ai_socktype
; /* SOCK_xxx */
126 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
127 size_t ai_addrlen
; /* length of ai_addr */
128 char *ai_canonname
; /* canonical name for hostname */
129 struct sockaddr
*ai_addr
; /* binary address */
130 struct addrinfo
*ai_next
; /* next structure in linked list */
132 #endif /* EAI_ADDRFAMILY */
133 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
135 #include <netdb.h> /* for "struct addrinfo" */
138 #include <pcap/namedb.h>
140 #include "nametoaddr.h"
142 #define ETHERMTU 1500
144 #ifndef ETHERTYPE_TEB
145 #define ETHERTYPE_TEB 0x6558
148 #ifndef IPPROTO_HOPOPTS
149 #define IPPROTO_HOPOPTS 0
151 #ifndef IPPROTO_ROUTING
152 #define IPPROTO_ROUTING 43
154 #ifndef IPPROTO_FRAGMENT
155 #define IPPROTO_FRAGMENT 44
157 #ifndef IPPROTO_DSTOPTS
158 #define IPPROTO_DSTOPTS 60
161 #define IPPROTO_SCTP 132
164 #define GENEVE_PORT 6081
166 #ifdef HAVE_OS_PROTO_H
167 #include "os-proto.h"
170 #define JMP(c) ((c)|BPF_JMP|BPF_K)
173 * "Push" the current value of the link-layer header type and link-layer
174 * header offset onto a "stack", and set a new value. (It's not a
175 * full-blown stack; we keep only the top two items.)
177 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
179 (cs)->prevlinktype = (cs)->linktype; \
180 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
181 (cs)->linktype = (new_linktype); \
182 (cs)->off_linkhdr.is_variable = (new_is_variable); \
183 (cs)->off_linkhdr.constant_part = (new_constant_part); \
184 (cs)->off_linkhdr.reg = (new_reg); \
185 (cs)->is_geneve = 0; \
189 * Offset "not set" value.
191 #define OFFSET_NOT_SET 0xffffffffU
194 * Absolute offsets, which are offsets from the beginning of the raw
195 * packet data, are, in the general case, the sum of a variable value
196 * and a constant value; the variable value may be absent, in which
197 * case the offset is only the constant value, and the constant value
198 * may be zero, in which case the offset is only the variable value.
200 * bpf_abs_offset is a structure containing all that information:
202 * is_variable is 1 if there's a variable part.
204 * constant_part is the constant part of the value, possibly zero;
206 * if is_variable is 1, reg is the register number for a register
207 * containing the variable value if the register has been assigned,
217 * Value passed to gen_load_a() to indicate what the offset argument
218 * is relative to the beginning of.
221 OR_PACKET
, /* full packet data */
222 OR_LINKHDR
, /* link-layer header */
223 OR_PREVLINKHDR
, /* previous link-layer header */
224 OR_LLC
, /* 802.2 LLC header */
225 OR_PREVMPLSHDR
, /* previous MPLS header */
226 OR_LINKTYPE
, /* link-layer type */
227 OR_LINKPL
, /* link-layer payload */
228 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
229 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
230 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
234 * We divy out chunks of memory rather than call malloc each time so
235 * we don't have to worry about leaking memory. It's probably
236 * not a big deal if all this memory was wasted but if this ever
237 * goes into a library that would probably not be a good idea.
239 * XXX - this *is* in a library....
242 #define CHUNK0SIZE 1024
248 /* Code generator state */
250 struct _compiler_state
{
260 int outermostlinktype
;
265 /* Hack for handling VLAN and MPLS stacks. */
266 u_int label_stack_depth
;
267 u_int vlan_stack_depth
;
274 * As errors are handled by a longjmp, anything allocated must
275 * be freed in the longjmp handler, so it must be reachable
278 * One thing that's allocated is the result of pcap_nametoaddrinfo();
279 * it must be freed with freeaddrinfo(). This variable points to
280 * any addrinfo structure that would need to be freed.
286 * Various code constructs need to know the layout of the packet.
287 * These values give the necessary offsets from the beginning
288 * of the packet data.
292 * Absolute offset of the beginning of the link-layer header.
294 bpf_abs_offset off_linkhdr
;
297 * If we're checking a link-layer header for a packet encapsulated
298 * in another protocol layer, this is the equivalent information
299 * for the previous layers' link-layer header from the beginning
300 * of the raw packet data.
302 bpf_abs_offset off_prevlinkhdr
;
305 * This is the equivalent information for the outermost layers'
308 bpf_abs_offset off_outermostlinkhdr
;
311 * Absolute offset of the beginning of the link-layer payload.
313 bpf_abs_offset off_linkpl
;
316 * "off_linktype" is the offset to information in the link-layer
317 * header giving the packet type. This is an absolute offset
318 * from the beginning of the packet.
320 * For Ethernet, it's the offset of the Ethernet type field; this
321 * means that it must have a value that skips VLAN tags.
323 * For link-layer types that always use 802.2 headers, it's the
324 * offset of the LLC header; this means that it must have a value
325 * that skips VLAN tags.
327 * For PPP, it's the offset of the PPP type field.
329 * For Cisco HDLC, it's the offset of the CHDLC type field.
331 * For BSD loopback, it's the offset of the AF_ value.
333 * For Linux cooked sockets, it's the offset of the type field.
335 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
336 * encapsulation, in which case, IP is assumed.
338 bpf_abs_offset off_linktype
;
341 * TRUE if the link layer includes an ATM pseudo-header.
346 * TRUE if "geneve" appeared in the filter; it causes us to
347 * generate code that checks for a Geneve header and assume
348 * that later filters apply to the encapsulated payload.
353 * TRUE if we need variable length part of VLAN offset
355 int is_vlan_vloffset
;
358 * These are offsets for the ATM pseudo-header.
365 * These are offsets for the MTP2 fields.
371 * These are offsets for the MTP3 fields.
379 * This is the offset of the first byte after the ATM pseudo_header,
380 * or -1 if there is no ATM pseudo-header.
385 * These are offsets to the beginning of the network-layer header.
386 * They are relative to the beginning of the link-layer payload
387 * (i.e., they don't include off_linkhdr.constant_part or
388 * off_linkpl.constant_part).
390 * If the link layer never uses 802.2 LLC:
392 * "off_nl" and "off_nl_nosnap" are the same.
394 * If the link layer always uses 802.2 LLC:
396 * "off_nl" is the offset if there's a SNAP header following
399 * "off_nl_nosnap" is the offset if there's no SNAP header.
401 * If the link layer is Ethernet:
403 * "off_nl" is the offset if the packet is an Ethernet II packet
404 * (we assume no 802.3+802.2+SNAP);
406 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
407 * with an 802.2 header following it.
413 * Here we handle simple allocation of the scratch registers.
414 * If too many registers are alloc'd, the allocator punts.
416 int regused
[BPF_MEMWORDS
];
422 struct chunk chunks
[NCHUNKS
];
427 bpf_syntax_error(compiler_state_t
*cstate
, const char *msg
)
429 bpf_error(cstate
, "syntax error in filter expression: %s", msg
);
435 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
440 if (cstate
->bpf_pcap
!= NULL
)
441 (void)pcap_vsnprintf(pcap_geterr(cstate
->bpf_pcap
),
442 PCAP_ERRBUF_SIZE
, fmt
, ap
);
444 longjmp(cstate
->top_ctx
, 1);
448 static void init_linktype(compiler_state_t
*, pcap_t
*);
450 static void init_regs(compiler_state_t
*);
451 static int alloc_reg(compiler_state_t
*);
452 static void free_reg(compiler_state_t
*, int);
454 static void initchunks(compiler_state_t
*cstate
);
455 static void *newchunk(compiler_state_t
*cstate
, size_t);
456 static void freechunks(compiler_state_t
*cstate
);
457 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
458 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
459 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
460 static inline void syntax(compiler_state_t
*cstate
);
462 static void backpatch(struct block
*, struct block
*);
463 static void merge(struct block
*, struct block
*);
464 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
466 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
468 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
470 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
472 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
474 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
475 u_int
, bpf_int32
, bpf_u_int32
);
476 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
477 u_int
, const u_char
*);
478 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
479 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
480 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
482 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
484 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
485 static struct block
*gen_uncond(compiler_state_t
*, int);
486 static inline struct block
*gen_true(compiler_state_t
*);
487 static inline struct block
*gen_false(compiler_state_t
*);
488 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
489 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
490 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
491 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
492 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
493 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
494 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
495 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
496 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
498 static int ethertype_to_ppptype(int);
499 static struct block
*gen_linktype(compiler_state_t
*, int);
500 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
501 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
502 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
503 int, int, u_int
, u_int
);
505 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
506 struct in6_addr
*, int, int, u_int
, u_int
);
508 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
509 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
510 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
511 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
512 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
513 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
514 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
515 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
516 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
519 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
520 struct in6_addr
*, int, int, int);
523 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
524 bpf_u_int32
**, int, int);
526 static struct block
*gen_ipfrag(compiler_state_t
*);
527 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
528 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
530 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
531 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
533 struct block
*gen_portop(compiler_state_t
*, int, int, int);
534 static struct block
*gen_port(compiler_state_t
*, int, int, int);
535 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
536 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
537 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
538 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
539 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
540 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
541 static int lookup_proto(compiler_state_t
*, const char *, int);
542 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
543 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
544 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
545 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
546 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
547 static struct block
*gen_len(compiler_state_t
*, int, int);
548 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
549 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
551 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
552 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
555 initchunks(compiler_state_t
*cstate
)
559 for (i
= 0; i
< NCHUNKS
; i
++) {
560 cstate
->chunks
[i
].n_left
= 0;
561 cstate
->chunks
[i
].m
= NULL
;
563 cstate
->cur_chunk
= 0;
567 newchunk(compiler_state_t
*cstate
, size_t n
)
574 /* XXX Round up to nearest long. */
575 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
577 /* XXX Round up to structure boundary. */
581 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
582 if (n
> cp
->n_left
) {
583 ++cp
, k
= ++cstate
->cur_chunk
;
585 bpf_error(cstate
, "out of memory");
586 size
= CHUNK0SIZE
<< k
;
587 cp
->m
= (void *)malloc(size
);
589 bpf_error(cstate
, "out of memory");
590 memset((char *)cp
->m
, 0, size
);
593 bpf_error(cstate
, "out of memory");
596 return (void *)((char *)cp
->m
+ cp
->n_left
);
600 freechunks(compiler_state_t
*cstate
)
604 for (i
= 0; i
< NCHUNKS
; ++i
)
605 if (cstate
->chunks
[i
].m
!= NULL
)
606 free(cstate
->chunks
[i
].m
);
610 * A strdup whose allocations are freed after code generation is over.
613 sdup(compiler_state_t
*cstate
, const char *s
)
615 size_t n
= strlen(s
) + 1;
616 char *cp
= newchunk(cstate
, n
);
622 static inline struct block
*
623 new_block(compiler_state_t
*cstate
, int code
)
627 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
634 static inline struct slist
*
635 new_stmt(compiler_state_t
*cstate
, int code
)
639 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
645 static struct block
*
646 gen_retblk(compiler_state_t
*cstate
, int v
)
648 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
655 syntax(compiler_state_t
*cstate
)
657 bpf_error(cstate
, "syntax error in filter expression");
661 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
662 const char *buf
, int optimize
, bpf_u_int32 mask
)
667 compiler_state_t cstate
;
668 const char * volatile xbuf
= buf
;
669 yyscan_t scanner
= NULL
;
670 YY_BUFFER_STATE in_buffer
= NULL
;
675 * If this pcap_t hasn't been activated, it doesn't have a
676 * link-layer type, so we can't use it.
679 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
680 "not-yet-activated pcap_t passed to pcap_compile");
692 * If the device on which we're capturing need to be notified
693 * that a new filter is being compiled, do so.
695 * This allows them to save a copy of it, in case, for example,
696 * they're implementing a form of remote packet capture, and
697 * want the remote machine to filter out the packets in which
698 * it's sending the packets it's captured.
700 * XXX - the fact that we happen to be compiling a filter
701 * doesn't necessarily mean we'll be installing it as the
702 * filter for this pcap_t; we might be running it from userland
703 * on captured packets to do packet classification. We really
704 * need a better way of handling this, but this is all that
705 * the WinPcap code did.
707 if (p
->save_current_filter_op
!= NULL
)
708 (p
->save_current_filter_op
)(p
, buf
);
712 cstate
.no_optimize
= 0;
716 cstate
.ic
.root
= NULL
;
717 cstate
.ic
.cur_mark
= 0;
721 if (setjmp(cstate
.top_ctx
)) {
723 if (cstate
.ai
!= NULL
)
724 freeaddrinfo(cstate
.ai
);
730 cstate
.netmask
= mask
;
732 cstate
.snaplen
= pcap_snapshot(p
);
733 if (cstate
.snaplen
== 0) {
734 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
735 "snaplen of 0 rejects all packets");
740 if (pcap_lex_init(&scanner
) != 0)
741 bpf_error(&cstate
, "can't initialize scanner: %s", pcap_strerror(errno
));
742 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
745 * Associate the compiler state with the lexical analyzer
748 pcap_set_extra(&cstate
, scanner
);
750 init_linktype(&cstate
, p
);
751 (void)pcap_parse(scanner
, &cstate
);
753 if (cstate
.ic
.root
== NULL
)
754 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
756 if (optimize
&& !cstate
.no_optimize
) {
757 bpf_optimize(&cstate
, &cstate
.ic
);
758 if (cstate
.ic
.root
== NULL
||
759 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
760 bpf_error(&cstate
, "expression rejects all packets");
762 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
763 program
->bf_len
= len
;
765 rc
= 0; /* We're all okay */
769 * Clean up everything for the lexical analyzer.
771 if (in_buffer
!= NULL
)
772 pcap__delete_buffer(in_buffer
, scanner
);
774 pcap_lex_destroy(scanner
);
777 * Clean up our own allocated memory.
785 * entry point for using the compiler with no pcap open
786 * pass in all the stuff that is needed explicitly instead.
789 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
790 struct bpf_program
*program
,
791 const char *buf
, int optimize
, bpf_u_int32 mask
)
796 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
799 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
805 * Clean up a "struct bpf_program" by freeing all the memory allocated
809 pcap_freecode(struct bpf_program
*program
)
812 if (program
->bf_insns
!= NULL
) {
813 free((char *)program
->bf_insns
);
814 program
->bf_insns
= NULL
;
819 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
820 * which of the jt and jf fields has been resolved and which is a pointer
821 * back to another unresolved block (or nil). At least one of the fields
822 * in each block is already resolved.
825 backpatch(list
, target
)
826 struct block
*list
, *target
;
843 * Merge the lists in b0 and b1, using the 'sense' field to indicate
844 * which of jt and jf is the link.
848 struct block
*b0
, *b1
;
850 register struct block
**p
= &b0
;
852 /* Find end of list. */
854 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
856 /* Concatenate the lists. */
861 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
863 struct block
*ppi_dlt_check
;
866 * Insert before the statements of the first (root) block any
867 * statements needed to load the lengths of any variable-length
868 * headers into registers.
870 * XXX - a fancier strategy would be to insert those before the
871 * statements of all blocks that use those lengths and that
872 * have no predecessors that use them, so that we only compute
873 * the lengths if we need them. There might be even better
874 * approaches than that.
876 * However, those strategies would be more complicated, and
877 * as we don't generate code to compute a length if the
878 * program has no tests that use the length, and as most
879 * tests will probably use those lengths, we would just
880 * postpone computing the lengths so that it's not done
881 * for tests that fail early, and it's not clear that's
884 insert_compute_vloffsets(cstate
, p
->head
);
887 * For DLT_PPI captures, generate a check of the per-packet
888 * DLT value to make sure it's DLT_IEEE802_11.
890 * XXX - TurboCap cards use DLT_PPI for Ethernet.
891 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
892 * with appropriate Ethernet information and use that rather
893 * than using something such as DLT_PPI where you don't know
894 * the link-layer header type until runtime, which, in the
895 * general case, would force us to generate both Ethernet *and*
896 * 802.11 code (*and* anything else for which PPI is used)
897 * and choose between them early in the BPF program?
899 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
900 if (ppi_dlt_check
!= NULL
)
901 gen_and(ppi_dlt_check
, p
);
903 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
904 p
->sense
= !p
->sense
;
905 backpatch(p
, gen_retblk(cstate
, 0));
906 cstate
->ic
.root
= p
->head
;
911 struct block
*b0
, *b1
;
913 backpatch(b0
, b1
->head
);
914 b0
->sense
= !b0
->sense
;
915 b1
->sense
= !b1
->sense
;
917 b1
->sense
= !b1
->sense
;
923 struct block
*b0
, *b1
;
925 b0
->sense
= !b0
->sense
;
926 backpatch(b0
, b1
->head
);
927 b0
->sense
= !b0
->sense
;
936 b
->sense
= !b
->sense
;
939 static struct block
*
940 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
941 u_int size
, bpf_int32 v
)
943 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
946 static struct block
*
947 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
948 u_int size
, bpf_int32 v
)
950 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
953 static struct block
*
954 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
955 u_int size
, bpf_int32 v
)
957 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
960 static struct block
*
961 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
962 u_int size
, bpf_int32 v
)
964 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
967 static struct block
*
968 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
969 u_int size
, bpf_int32 v
)
971 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
974 static struct block
*
975 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
976 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
978 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
981 static struct block
*
982 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
983 u_int size
, const u_char
*v
)
985 register struct block
*b
, *tmp
;
989 register const u_char
*p
= &v
[size
- 4];
990 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
991 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
993 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
, w
);
1000 register const u_char
*p
= &v
[size
- 2];
1001 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
1003 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
, w
);
1010 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1019 * AND the field of size "size" at offset "offset" relative to the header
1020 * specified by "offrel" with "mask", and compare it with the value "v"
1021 * with the test specified by "jtype"; if "reverse" is true, the test
1022 * should test the opposite of "jtype".
1024 static struct block
*
1025 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1026 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1029 struct slist
*s
, *s2
;
1032 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1034 if (mask
!= 0xffffffff) {
1035 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1040 b
= new_block(cstate
, JMP(jtype
));
1043 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1049 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1051 cstate
->pcap_fddipad
= p
->fddipad
;
1054 * We start out with only one link-layer header.
1056 cstate
->outermostlinktype
= pcap_datalink(p
);
1057 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1058 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1059 cstate
->off_outermostlinkhdr
.reg
= -1;
1061 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1062 cstate
->off_prevlinkhdr
.constant_part
= 0;
1063 cstate
->off_prevlinkhdr
.is_variable
= 0;
1064 cstate
->off_prevlinkhdr
.reg
= -1;
1066 cstate
->linktype
= cstate
->outermostlinktype
;
1067 cstate
->off_linkhdr
.constant_part
= 0;
1068 cstate
->off_linkhdr
.is_variable
= 0;
1069 cstate
->off_linkhdr
.reg
= -1;
1074 cstate
->off_linkpl
.constant_part
= 0;
1075 cstate
->off_linkpl
.is_variable
= 0;
1076 cstate
->off_linkpl
.reg
= -1;
1078 cstate
->off_linktype
.constant_part
= 0;
1079 cstate
->off_linktype
.is_variable
= 0;
1080 cstate
->off_linktype
.reg
= -1;
1083 * Assume it's not raw ATM with a pseudo-header, for now.
1086 cstate
->off_vpi
= -1;
1087 cstate
->off_vci
= -1;
1088 cstate
->off_proto
= -1;
1089 cstate
->off_payload
= -1;
1094 cstate
->is_geneve
= 0;
1097 * No variable length VLAN offset by default
1099 cstate
->is_vlan_vloffset
= 0;
1102 * And assume we're not doing SS7.
1104 cstate
->off_li
= -1;
1105 cstate
->off_li_hsl
= -1;
1106 cstate
->off_sio
= -1;
1107 cstate
->off_opc
= -1;
1108 cstate
->off_dpc
= -1;
1109 cstate
->off_sls
= -1;
1111 cstate
->label_stack_depth
= 0;
1112 cstate
->vlan_stack_depth
= 0;
1114 switch (cstate
->linktype
) {
1117 cstate
->off_linktype
.constant_part
= 2;
1118 cstate
->off_linkpl
.constant_part
= 6;
1119 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1120 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1123 case DLT_ARCNET_LINUX
:
1124 cstate
->off_linktype
.constant_part
= 4;
1125 cstate
->off_linkpl
.constant_part
= 8;
1126 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1127 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1131 cstate
->off_linktype
.constant_part
= 12;
1132 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1133 cstate
->off_nl
= 0; /* Ethernet II */
1134 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1139 * SLIP doesn't have a link level type. The 16 byte
1140 * header is hacked into our SLIP driver.
1142 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1143 cstate
->off_linkpl
.constant_part
= 16;
1145 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1148 case DLT_SLIP_BSDOS
:
1149 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1150 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1152 cstate
->off_linkpl
.constant_part
= 24;
1154 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1159 cstate
->off_linktype
.constant_part
= 0;
1160 cstate
->off_linkpl
.constant_part
= 4;
1162 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1166 cstate
->off_linktype
.constant_part
= 0;
1167 cstate
->off_linkpl
.constant_part
= 12;
1169 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1174 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1175 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1176 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1177 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1179 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1184 * This does no include the Ethernet header, and
1185 * only covers session state.
1187 cstate
->off_linktype
.constant_part
= 6;
1188 cstate
->off_linkpl
.constant_part
= 8;
1190 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1194 cstate
->off_linktype
.constant_part
= 5;
1195 cstate
->off_linkpl
.constant_part
= 24;
1197 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1202 * FDDI doesn't really have a link-level type field.
1203 * We set "off_linktype" to the offset of the LLC header.
1205 * To check for Ethernet types, we assume that SSAP = SNAP
1206 * is being used and pick out the encapsulated Ethernet type.
1207 * XXX - should we generate code to check for SNAP?
1209 cstate
->off_linktype
.constant_part
= 13;
1210 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1211 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1212 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1213 cstate
->off_nl
= 8; /* 802.2+SNAP */
1214 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1219 * Token Ring doesn't really have a link-level type field.
1220 * We set "off_linktype" to the offset of the LLC header.
1222 * To check for Ethernet types, we assume that SSAP = SNAP
1223 * is being used and pick out the encapsulated Ethernet type.
1224 * XXX - should we generate code to check for SNAP?
1226 * XXX - the header is actually variable-length.
1227 * Some various Linux patched versions gave 38
1228 * as "off_linktype" and 40 as "off_nl"; however,
1229 * if a token ring packet has *no* routing
1230 * information, i.e. is not source-routed, the correct
1231 * values are 20 and 22, as they are in the vanilla code.
1233 * A packet is source-routed iff the uppermost bit
1234 * of the first byte of the source address, at an
1235 * offset of 8, has the uppermost bit set. If the
1236 * packet is source-routed, the total number of bytes
1237 * of routing information is 2 plus bits 0x1F00 of
1238 * the 16-bit value at an offset of 14 (shifted right
1239 * 8 - figure out which byte that is).
1241 cstate
->off_linktype
.constant_part
= 14;
1242 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1243 cstate
->off_nl
= 8; /* 802.2+SNAP */
1244 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1247 case DLT_PRISM_HEADER
:
1248 case DLT_IEEE802_11_RADIO_AVS
:
1249 case DLT_IEEE802_11_RADIO
:
1250 cstate
->off_linkhdr
.is_variable
= 1;
1251 /* Fall through, 802.11 doesn't have a variable link
1252 * prefix but is otherwise the same. */
1254 case DLT_IEEE802_11
:
1256 * 802.11 doesn't really have a link-level type field.
1257 * We set "off_linktype.constant_part" to the offset of
1260 * To check for Ethernet types, we assume that SSAP = SNAP
1261 * is being used and pick out the encapsulated Ethernet type.
1262 * XXX - should we generate code to check for SNAP?
1264 * We also handle variable-length radio headers here.
1265 * The Prism header is in theory variable-length, but in
1266 * practice it's always 144 bytes long. However, some
1267 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1268 * sometimes or always supply an AVS header, so we
1269 * have to check whether the radio header is a Prism
1270 * header or an AVS header, so, in practice, it's
1273 cstate
->off_linktype
.constant_part
= 24;
1274 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1275 cstate
->off_linkpl
.is_variable
= 1;
1276 cstate
->off_nl
= 8; /* 802.2+SNAP */
1277 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1282 * At the moment we treat PPI the same way that we treat
1283 * normal Radiotap encoded packets. The difference is in
1284 * the function that generates the code at the beginning
1285 * to compute the header length. Since this code generator
1286 * of PPI supports bare 802.11 encapsulation only (i.e.
1287 * the encapsulated DLT should be DLT_IEEE802_11) we
1288 * generate code to check for this too.
1290 cstate
->off_linktype
.constant_part
= 24;
1291 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1292 cstate
->off_linkpl
.is_variable
= 1;
1293 cstate
->off_linkhdr
.is_variable
= 1;
1294 cstate
->off_nl
= 8; /* 802.2+SNAP */
1295 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1298 case DLT_ATM_RFC1483
:
1299 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1301 * assume routed, non-ISO PDUs
1302 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1304 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1305 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1306 * latter would presumably be treated the way PPPoE
1307 * should be, so you can do "pppoe and udp port 2049"
1308 * or "pppoa and tcp port 80" and have it check for
1309 * PPPo{A,E} and a PPP protocol of IP and....
1311 cstate
->off_linktype
.constant_part
= 0;
1312 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1313 cstate
->off_nl
= 8; /* 802.2+SNAP */
1314 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1319 * Full Frontal ATM; you get AALn PDUs with an ATM
1323 cstate
->off_vpi
= SUNATM_VPI_POS
;
1324 cstate
->off_vci
= SUNATM_VCI_POS
;
1325 cstate
->off_proto
= PROTO_POS
;
1326 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1327 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1328 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1329 cstate
->off_nl
= 8; /* 802.2+SNAP */
1330 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1336 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1337 cstate
->off_linkpl
.constant_part
= 0;
1339 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1342 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1343 cstate
->off_linktype
.constant_part
= 14;
1344 cstate
->off_linkpl
.constant_part
= 16;
1346 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1351 * LocalTalk does have a 1-byte type field in the LLAP header,
1352 * but really it just indicates whether there is a "short" or
1353 * "long" DDP packet following.
1355 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1356 cstate
->off_linkpl
.constant_part
= 0;
1358 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1361 case DLT_IP_OVER_FC
:
1363 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1364 * link-level type field. We set "off_linktype" to the
1365 * offset of the LLC header.
1367 * To check for Ethernet types, we assume that SSAP = SNAP
1368 * is being used and pick out the encapsulated Ethernet type.
1369 * XXX - should we generate code to check for SNAP? RFC
1370 * 2625 says SNAP should be used.
1372 cstate
->off_linktype
.constant_part
= 16;
1373 cstate
->off_linkpl
.constant_part
= 16;
1374 cstate
->off_nl
= 8; /* 802.2+SNAP */
1375 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1380 * XXX - we should set this to handle SNAP-encapsulated
1381 * frames (NLPID of 0x80).
1383 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1384 cstate
->off_linkpl
.constant_part
= 0;
1386 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1390 * the only BPF-interesting FRF.16 frames are non-control frames;
1391 * Frame Relay has a variable length link-layer
1392 * so lets start with offset 4 for now and increments later on (FIXME);
1395 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1396 cstate
->off_linkpl
.constant_part
= 0;
1398 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1401 case DLT_APPLE_IP_OVER_IEEE1394
:
1402 cstate
->off_linktype
.constant_part
= 16;
1403 cstate
->off_linkpl
.constant_part
= 18;
1405 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1408 case DLT_SYMANTEC_FIREWALL
:
1409 cstate
->off_linktype
.constant_part
= 6;
1410 cstate
->off_linkpl
.constant_part
= 44;
1411 cstate
->off_nl
= 0; /* Ethernet II */
1412 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1415 #ifdef HAVE_NET_PFVAR_H
1417 cstate
->off_linktype
.constant_part
= 0;
1418 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1420 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1424 case DLT_JUNIPER_MFR
:
1425 case DLT_JUNIPER_MLFR
:
1426 case DLT_JUNIPER_MLPPP
:
1427 case DLT_JUNIPER_PPP
:
1428 case DLT_JUNIPER_CHDLC
:
1429 case DLT_JUNIPER_FRELAY
:
1430 cstate
->off_linktype
.constant_part
= 4;
1431 cstate
->off_linkpl
.constant_part
= 4;
1433 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1436 case DLT_JUNIPER_ATM1
:
1437 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1438 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1440 cstate
->off_nl_nosnap
= 10;
1443 case DLT_JUNIPER_ATM2
:
1444 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1445 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1447 cstate
->off_nl_nosnap
= 10;
1450 /* frames captured on a Juniper PPPoE service PIC
1451 * contain raw ethernet frames */
1452 case DLT_JUNIPER_PPPOE
:
1453 case DLT_JUNIPER_ETHER
:
1454 cstate
->off_linkpl
.constant_part
= 14;
1455 cstate
->off_linktype
.constant_part
= 16;
1456 cstate
->off_nl
= 18; /* Ethernet II */
1457 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1460 case DLT_JUNIPER_PPPOE_ATM
:
1461 cstate
->off_linktype
.constant_part
= 4;
1462 cstate
->off_linkpl
.constant_part
= 6;
1464 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1467 case DLT_JUNIPER_GGSN
:
1468 cstate
->off_linktype
.constant_part
= 6;
1469 cstate
->off_linkpl
.constant_part
= 12;
1471 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1474 case DLT_JUNIPER_ES
:
1475 cstate
->off_linktype
.constant_part
= 6;
1476 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1477 cstate
->off_nl
= -1; /* not really a network layer but raw IP addresses */
1478 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1481 case DLT_JUNIPER_MONITOR
:
1482 cstate
->off_linktype
.constant_part
= 12;
1483 cstate
->off_linkpl
.constant_part
= 12;
1484 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1485 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1488 case DLT_BACNET_MS_TP
:
1489 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1490 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1491 cstate
->off_nl
= -1;
1492 cstate
->off_nl_nosnap
= -1;
1495 case DLT_JUNIPER_SERVICES
:
1496 cstate
->off_linktype
.constant_part
= 12;
1497 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1498 cstate
->off_nl
= -1; /* L3 proto location dep. on cookie type */
1499 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1502 case DLT_JUNIPER_VP
:
1503 cstate
->off_linktype
.constant_part
= 18;
1504 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1505 cstate
->off_nl
= -1;
1506 cstate
->off_nl_nosnap
= -1;
1509 case DLT_JUNIPER_ST
:
1510 cstate
->off_linktype
.constant_part
= 18;
1511 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1512 cstate
->off_nl
= -1;
1513 cstate
->off_nl_nosnap
= -1;
1516 case DLT_JUNIPER_ISM
:
1517 cstate
->off_linktype
.constant_part
= 8;
1518 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1519 cstate
->off_nl
= -1;
1520 cstate
->off_nl_nosnap
= -1;
1523 case DLT_JUNIPER_VS
:
1524 case DLT_JUNIPER_SRX_E2E
:
1525 case DLT_JUNIPER_FIBRECHANNEL
:
1526 case DLT_JUNIPER_ATM_CEMIC
:
1527 cstate
->off_linktype
.constant_part
= 8;
1528 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1529 cstate
->off_nl
= -1;
1530 cstate
->off_nl_nosnap
= -1;
1535 cstate
->off_li_hsl
= 4;
1536 cstate
->off_sio
= 3;
1537 cstate
->off_opc
= 4;
1538 cstate
->off_dpc
= 4;
1539 cstate
->off_sls
= 7;
1540 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1541 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1542 cstate
->off_nl
= -1;
1543 cstate
->off_nl_nosnap
= -1;
1546 case DLT_MTP2_WITH_PHDR
:
1548 cstate
->off_li_hsl
= 8;
1549 cstate
->off_sio
= 7;
1550 cstate
->off_opc
= 8;
1551 cstate
->off_dpc
= 8;
1552 cstate
->off_sls
= 11;
1553 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1554 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1555 cstate
->off_nl
= -1;
1556 cstate
->off_nl_nosnap
= -1;
1560 cstate
->off_li
= 22;
1561 cstate
->off_li_hsl
= 24;
1562 cstate
->off_sio
= 23;
1563 cstate
->off_opc
= 24;
1564 cstate
->off_dpc
= 24;
1565 cstate
->off_sls
= 27;
1566 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1567 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1568 cstate
->off_nl
= -1;
1569 cstate
->off_nl_nosnap
= -1;
1573 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1574 cstate
->off_linkpl
.constant_part
= 4;
1576 cstate
->off_nl_nosnap
= 0;
1581 * Currently, only raw "link[N:M]" filtering is supported.
1583 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1584 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1585 cstate
->off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1586 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1590 cstate
->off_linktype
.constant_part
= 1;
1591 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1593 cstate
->off_nl_nosnap
= -1;
1596 case DLT_NETANALYZER
:
1597 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1598 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1599 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1600 cstate
->off_nl
= 0; /* Ethernet II */
1601 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1604 case DLT_NETANALYZER_TRANSPARENT
:
1605 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1606 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1607 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1608 cstate
->off_nl
= 0; /* Ethernet II */
1609 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1614 * For values in the range in which we've assigned new
1615 * DLT_ values, only raw "link[N:M]" filtering is supported.
1617 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1618 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1619 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1620 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1621 cstate
->off_nl
= -1;
1622 cstate
->off_nl_nosnap
= -1;
1624 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1629 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1633 * Load a value relative to the specified absolute offset.
1635 static struct slist
*
1636 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1637 u_int offset
, u_int size
)
1639 struct slist
*s
, *s2
;
1641 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1644 * If "s" is non-null, it has code to arrange that the X register
1645 * contains the variable part of the absolute offset, so we
1646 * generate a load relative to that, with an offset of
1647 * abs_offset->constant_part + offset.
1649 * Otherwise, we can do an absolute load with an offset of
1650 * abs_offset->constant_part + offset.
1654 * "s" points to a list of statements that puts the
1655 * variable part of the absolute offset into the X register.
1656 * Do an indirect load, to use the X register as an offset.
1658 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1659 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1663 * There is no variable part of the absolute offset, so
1664 * just do an absolute load.
1666 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1667 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1673 * Load a value relative to the beginning of the specified header.
1675 static struct slist
*
1676 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1679 struct slist
*s
, *s2
;
1684 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1689 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1692 case OR_PREVLINKHDR
:
1693 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1697 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1700 case OR_PREVMPLSHDR
:
1701 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1705 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1708 case OR_LINKPL_NOSNAP
:
1709 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1713 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1718 * Load the X register with the length of the IPv4 header
1719 * (plus the offset of the link-layer header, if it's
1720 * preceded by a variable-length header such as a radio
1721 * header), in bytes.
1723 s
= gen_loadx_iphdrlen(cstate
);
1726 * Load the item at {offset of the link-layer payload} +
1727 * {offset, relative to the start of the link-layer
1728 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1729 * {specified offset}.
1731 * If the offset of the link-layer payload is variable,
1732 * the variable part of that offset is included in the
1733 * value in the X register, and we include the constant
1734 * part in the offset of the load.
1736 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1737 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1742 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1753 * Generate code to load into the X register the sum of the length of
1754 * the IPv4 header and the variable part of the offset of the link-layer
1757 static struct slist
*
1758 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1760 struct slist
*s
, *s2
;
1762 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1765 * The offset of the link-layer payload has a variable
1766 * part. "s" points to a list of statements that put
1767 * the variable part of that offset into the X register.
1769 * The 4*([k]&0xf) addressing mode can't be used, as we
1770 * don't have a constant offset, so we have to load the
1771 * value in question into the A register and add to it
1772 * the value from the X register.
1774 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1775 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1777 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1780 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1785 * The A register now contains the length of the IP header.
1786 * We need to add to it the variable part of the offset of
1787 * the link-layer payload, which is still in the X
1788 * register, and move the result into the X register.
1790 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1791 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1794 * The offset of the link-layer payload is a constant,
1795 * so no code was generated to load the (non-existent)
1796 * variable part of that offset.
1798 * This means we can use the 4*([k]&0xf) addressing
1799 * mode. Load the length of the IPv4 header, which
1800 * is at an offset of cstate->off_nl from the beginning of
1801 * the link-layer payload, and thus at an offset of
1802 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1803 * of the raw packet data, using that addressing mode.
1805 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1806 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1811 static struct block
*
1812 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1817 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1819 b
= new_block(cstate
, JMP(BPF_JEQ
));
1825 static inline struct block
*
1826 gen_true(compiler_state_t
*cstate
)
1828 return gen_uncond(cstate
, 1);
1831 static inline struct block
*
1832 gen_false(compiler_state_t
*cstate
)
1834 return gen_uncond(cstate
, 0);
1838 * Byte-swap a 32-bit number.
1839 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1840 * big-endian platforms.)
1842 #define SWAPLONG(y) \
1843 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1846 * Generate code to match a particular packet type.
1848 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1849 * value, if <= ETHERMTU. We use that to determine whether to
1850 * match the type/length field or to check the type/length field for
1851 * a value <= ETHERMTU to see whether it's a type field and then do
1852 * the appropriate test.
1854 static struct block
*
1855 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1857 struct block
*b0
, *b1
;
1863 case LLCSAP_NETBEUI
:
1865 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1866 * so we check the DSAP and SSAP.
1868 * LLCSAP_IP checks for IP-over-802.2, rather
1869 * than IP-over-Ethernet or IP-over-SNAP.
1871 * XXX - should we check both the DSAP and the
1872 * SSAP, like this, or should we check just the
1873 * DSAP, as we do for other types <= ETHERMTU
1874 * (i.e., other SAP values)?
1876 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1878 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1879 ((proto
<< 8) | proto
));
1887 * Ethernet_II frames, which are Ethernet
1888 * frames with a frame type of ETHERTYPE_IPX;
1890 * Ethernet_802.3 frames, which are 802.3
1891 * frames (i.e., the type/length field is
1892 * a length field, <= ETHERMTU, rather than
1893 * a type field) with the first two bytes
1894 * after the Ethernet/802.3 header being
1897 * Ethernet_802.2 frames, which are 802.3
1898 * frames with an 802.2 LLC header and
1899 * with the IPX LSAP as the DSAP in the LLC
1902 * Ethernet_SNAP frames, which are 802.3
1903 * frames with an LLC header and a SNAP
1904 * header and with an OUI of 0x000000
1905 * (encapsulated Ethernet) and a protocol
1906 * ID of ETHERTYPE_IPX in the SNAP header.
1908 * XXX - should we generate the same code both
1909 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1913 * This generates code to check both for the
1914 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1916 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1917 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1921 * Now we add code to check for SNAP frames with
1922 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1924 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1928 * Now we generate code to check for 802.3
1929 * frames in general.
1931 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1935 * Now add the check for 802.3 frames before the
1936 * check for Ethernet_802.2 and Ethernet_802.3,
1937 * as those checks should only be done on 802.3
1938 * frames, not on Ethernet frames.
1943 * Now add the check for Ethernet_II frames, and
1944 * do that before checking for the other frame
1947 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1951 case ETHERTYPE_ATALK
:
1952 case ETHERTYPE_AARP
:
1954 * EtherTalk (AppleTalk protocols on Ethernet link
1955 * layer) may use 802.2 encapsulation.
1959 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1960 * we check for an Ethernet type field less than
1961 * 1500, which means it's an 802.3 length field.
1963 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1967 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1968 * SNAP packets with an organization code of
1969 * 0x080007 (Apple, for Appletalk) and a protocol
1970 * type of ETHERTYPE_ATALK (Appletalk).
1972 * 802.2-encapsulated ETHERTYPE_AARP packets are
1973 * SNAP packets with an organization code of
1974 * 0x000000 (encapsulated Ethernet) and a protocol
1975 * type of ETHERTYPE_AARP (Appletalk ARP).
1977 if (proto
== ETHERTYPE_ATALK
)
1978 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
1979 else /* proto == ETHERTYPE_AARP */
1980 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
1984 * Check for Ethernet encapsulation (Ethertalk
1985 * phase 1?); we just check for the Ethernet
1988 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1994 if (proto
<= ETHERMTU
) {
1996 * This is an LLC SAP value, so the frames
1997 * that match would be 802.2 frames.
1998 * Check that the frame is an 802.2 frame
1999 * (i.e., that the length/type field is
2000 * a length field, <= ETHERMTU) and
2001 * then check the DSAP.
2003 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2005 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2010 * This is an Ethernet type, so compare
2011 * the length/type field with it (if
2012 * the frame is an 802.2 frame, the length
2013 * field will be <= ETHERMTU, and, as
2014 * "proto" is > ETHERMTU, this test
2015 * will fail and the frame won't match,
2016 * which is what we want).
2018 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2024 static struct block
*
2025 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2028 * For DLT_NULL, the link-layer header is a 32-bit word
2029 * containing an AF_ value in *host* byte order, and for
2030 * DLT_ENC, the link-layer header begins with a 32-bit
2031 * word containing an AF_ value in host byte order.
2033 * In addition, if we're reading a saved capture file,
2034 * the host byte order in the capture may not be the
2035 * same as the host byte order on this machine.
2037 * For DLT_LOOP, the link-layer header is a 32-bit
2038 * word containing an AF_ value in *network* byte order.
2040 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2042 * The AF_ value is in host byte order, but the BPF
2043 * interpreter will convert it to network byte order.
2045 * If this is a save file, and it's from a machine
2046 * with the opposite byte order to ours, we byte-swap
2049 * Then we run it through "htonl()", and generate
2050 * code to compare against the result.
2052 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2053 proto
= SWAPLONG(proto
);
2054 proto
= htonl(proto
);
2056 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2060 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2061 * or IPv6 then we have an error.
2063 static struct block
*
2064 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2069 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2072 case ETHERTYPE_IPV6
:
2073 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2074 (bpf_int32
)IPH_AF_INET6
);
2081 return gen_false(cstate
);
2085 * Generate code to match a particular packet type.
2087 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2088 * value, if <= ETHERMTU. We use that to determine whether to
2089 * match the type field or to check the type field for the special
2090 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2092 static struct block
*
2093 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2095 struct block
*b0
, *b1
;
2101 case LLCSAP_NETBEUI
:
2103 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2104 * so we check the DSAP and SSAP.
2106 * LLCSAP_IP checks for IP-over-802.2, rather
2107 * than IP-over-Ethernet or IP-over-SNAP.
2109 * XXX - should we check both the DSAP and the
2110 * SSAP, like this, or should we check just the
2111 * DSAP, as we do for other types <= ETHERMTU
2112 * (i.e., other SAP values)?
2114 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2115 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2116 ((proto
<< 8) | proto
));
2122 * Ethernet_II frames, which are Ethernet
2123 * frames with a frame type of ETHERTYPE_IPX;
2125 * Ethernet_802.3 frames, which have a frame
2126 * type of LINUX_SLL_P_802_3;
2128 * Ethernet_802.2 frames, which are 802.3
2129 * frames with an 802.2 LLC header (i.e, have
2130 * a frame type of LINUX_SLL_P_802_2) and
2131 * with the IPX LSAP as the DSAP in the LLC
2134 * Ethernet_SNAP frames, which are 802.3
2135 * frames with an LLC header and a SNAP
2136 * header and with an OUI of 0x000000
2137 * (encapsulated Ethernet) and a protocol
2138 * ID of ETHERTYPE_IPX in the SNAP header.
2140 * First, do the checks on LINUX_SLL_P_802_2
2141 * frames; generate the check for either
2142 * Ethernet_802.2 or Ethernet_SNAP frames, and
2143 * then put a check for LINUX_SLL_P_802_2 frames
2146 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2147 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2149 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2153 * Now check for 802.3 frames and OR that with
2154 * the previous test.
2156 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2160 * Now add the check for Ethernet_II frames, and
2161 * do that before checking for the other frame
2164 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2168 case ETHERTYPE_ATALK
:
2169 case ETHERTYPE_AARP
:
2171 * EtherTalk (AppleTalk protocols on Ethernet link
2172 * layer) may use 802.2 encapsulation.
2176 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2177 * we check for the 802.2 protocol type in the
2178 * "Ethernet type" field.
2180 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2183 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2184 * SNAP packets with an organization code of
2185 * 0x080007 (Apple, for Appletalk) and a protocol
2186 * type of ETHERTYPE_ATALK (Appletalk).
2188 * 802.2-encapsulated ETHERTYPE_AARP packets are
2189 * SNAP packets with an organization code of
2190 * 0x000000 (encapsulated Ethernet) and a protocol
2191 * type of ETHERTYPE_AARP (Appletalk ARP).
2193 if (proto
== ETHERTYPE_ATALK
)
2194 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2195 else /* proto == ETHERTYPE_AARP */
2196 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2200 * Check for Ethernet encapsulation (Ethertalk
2201 * phase 1?); we just check for the Ethernet
2204 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2210 if (proto
<= ETHERMTU
) {
2212 * This is an LLC SAP value, so the frames
2213 * that match would be 802.2 frames.
2214 * Check for the 802.2 protocol type
2215 * in the "Ethernet type" field, and
2216 * then check the DSAP.
2218 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2219 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2225 * This is an Ethernet type, so compare
2226 * the length/type field with it (if
2227 * the frame is an 802.2 frame, the length
2228 * field will be <= ETHERMTU, and, as
2229 * "proto" is > ETHERMTU, this test
2230 * will fail and the frame won't match,
2231 * which is what we want).
2233 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2238 static struct slist
*
2239 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2241 struct slist
*s1
, *s2
;
2242 struct slist
*sjeq_avs_cookie
;
2243 struct slist
*sjcommon
;
2246 * This code is not compatible with the optimizer, as
2247 * we are generating jmp instructions within a normal
2248 * slist of instructions
2250 cstate
->no_optimize
= 1;
2253 * Generate code to load the length of the radio header into
2254 * the register assigned to hold that length, if one has been
2255 * assigned. (If one hasn't been assigned, no code we've
2256 * generated uses that prefix, so we don't need to generate any
2259 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2260 * or always use the AVS header rather than the Prism header.
2261 * We load a 4-byte big-endian value at the beginning of the
2262 * raw packet data, and see whether, when masked with 0xFFFFF000,
2263 * it's equal to 0x80211000. If so, that indicates that it's
2264 * an AVS header (the masked-out bits are the version number).
2265 * Otherwise, it's a Prism header.
2267 * XXX - the Prism header is also, in theory, variable-length,
2268 * but no known software generates headers that aren't 144
2271 if (cstate
->off_linkhdr
.reg
!= -1) {
2275 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2279 * AND it with 0xFFFFF000.
2281 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2282 s2
->s
.k
= 0xFFFFF000;
2286 * Compare with 0x80211000.
2288 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2289 sjeq_avs_cookie
->s
.k
= 0x80211000;
2290 sappend(s1
, sjeq_avs_cookie
);
2295 * The 4 bytes at an offset of 4 from the beginning of
2296 * the AVS header are the length of the AVS header.
2297 * That field is big-endian.
2299 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2302 sjeq_avs_cookie
->s
.jt
= s2
;
2305 * Now jump to the code to allocate a register
2306 * into which to save the header length and
2307 * store the length there. (The "jump always"
2308 * instruction needs to have the k field set;
2309 * it's added to the PC, so, as we're jumping
2310 * over a single instruction, it should be 1.)
2312 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2314 sappend(s1
, sjcommon
);
2317 * Now for the code that handles the Prism header.
2318 * Just load the length of the Prism header (144)
2319 * into the A register. Have the test for an AVS
2320 * header branch here if we don't have an AVS header.
2322 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2325 sjeq_avs_cookie
->s
.jf
= s2
;
2328 * Now allocate a register to hold that value and store
2329 * it. The code for the AVS header will jump here after
2330 * loading the length of the AVS header.
2332 s2
= new_stmt(cstate
, BPF_ST
);
2333 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2335 sjcommon
->s
.jf
= s2
;
2338 * Now move it into the X register.
2340 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2348 static struct slist
*
2349 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2351 struct slist
*s1
, *s2
;
2354 * Generate code to load the length of the AVS header into
2355 * the register assigned to hold that length, if one has been
2356 * assigned. (If one hasn't been assigned, no code we've
2357 * generated uses that prefix, so we don't need to generate any
2360 if (cstate
->off_linkhdr
.reg
!= -1) {
2362 * The 4 bytes at an offset of 4 from the beginning of
2363 * the AVS header are the length of the AVS header.
2364 * That field is big-endian.
2366 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2370 * Now allocate a register to hold that value and store
2373 s2
= new_stmt(cstate
, BPF_ST
);
2374 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2378 * Now move it into the X register.
2380 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2388 static struct slist
*
2389 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2391 struct slist
*s1
, *s2
;
2394 * Generate code to load the length of the radiotap header into
2395 * the register assigned to hold that length, if one has been
2396 * assigned. (If one hasn't been assigned, no code we've
2397 * generated uses that prefix, so we don't need to generate any
2400 if (cstate
->off_linkhdr
.reg
!= -1) {
2402 * The 2 bytes at offsets of 2 and 3 from the beginning
2403 * of the radiotap header are the length of the radiotap
2404 * header; unfortunately, it's little-endian, so we have
2405 * to load it a byte at a time and construct the value.
2409 * Load the high-order byte, at an offset of 3, shift it
2410 * left a byte, and put the result in the X register.
2412 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2414 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2417 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2421 * Load the next byte, at an offset of 2, and OR the
2422 * value from the X register into it.
2424 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2427 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2431 * Now allocate a register to hold that value and store
2434 s2
= new_stmt(cstate
, BPF_ST
);
2435 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2439 * Now move it into the X register.
2441 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2450 * At the moment we treat PPI as normal Radiotap encoded
2451 * packets. The difference is in the function that generates
2452 * the code at the beginning to compute the header length.
2453 * Since this code generator of PPI supports bare 802.11
2454 * encapsulation only (i.e. the encapsulated DLT should be
2455 * DLT_IEEE802_11) we generate code to check for this too;
2456 * that's done in finish_parse().
2458 static struct slist
*
2459 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2461 struct slist
*s1
, *s2
;
2464 * Generate code to load the length of the radiotap header
2465 * into the register assigned to hold that length, if one has
2468 if (cstate
->off_linkhdr
.reg
!= -1) {
2470 * The 2 bytes at offsets of 2 and 3 from the beginning
2471 * of the radiotap header are the length of the radiotap
2472 * header; unfortunately, it's little-endian, so we have
2473 * to load it a byte at a time and construct the value.
2477 * Load the high-order byte, at an offset of 3, shift it
2478 * left a byte, and put the result in the X register.
2480 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2482 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2485 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2489 * Load the next byte, at an offset of 2, and OR the
2490 * value from the X register into it.
2492 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2495 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2499 * Now allocate a register to hold that value and store
2502 s2
= new_stmt(cstate
, BPF_ST
);
2503 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2507 * Now move it into the X register.
2509 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2518 * Load a value relative to the beginning of the link-layer header after the 802.11
2519 * header, i.e. LLC_SNAP.
2520 * The link-layer header doesn't necessarily begin at the beginning
2521 * of the packet data; there might be a variable-length prefix containing
2522 * radio information.
2524 static struct slist
*
2525 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2528 struct slist
*sjset_data_frame_1
;
2529 struct slist
*sjset_data_frame_2
;
2530 struct slist
*sjset_qos
;
2531 struct slist
*sjset_radiotap_flags_present
;
2532 struct slist
*sjset_radiotap_ext_present
;
2533 struct slist
*sjset_radiotap_tsft_present
;
2534 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2535 struct slist
*s_roundup
;
2537 if (cstate
->off_linkpl
.reg
== -1) {
2539 * No register has been assigned to the offset of
2540 * the link-layer payload, which means nobody needs
2541 * it; don't bother computing it - just return
2542 * what we already have.
2548 * This code is not compatible with the optimizer, as
2549 * we are generating jmp instructions within a normal
2550 * slist of instructions
2552 cstate
->no_optimize
= 1;
2555 * If "s" is non-null, it has code to arrange that the X register
2556 * contains the length of the prefix preceding the link-layer
2559 * Otherwise, the length of the prefix preceding the link-layer
2560 * header is "off_outermostlinkhdr.constant_part".
2564 * There is no variable-length header preceding the
2565 * link-layer header.
2567 * Load the length of the fixed-length prefix preceding
2568 * the link-layer header (if any) into the X register,
2569 * and store it in the cstate->off_linkpl.reg register.
2570 * That length is off_outermostlinkhdr.constant_part.
2572 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2573 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2577 * The X register contains the offset of the beginning of the
2578 * link-layer header; add 24, which is the minimum length
2579 * of the MAC header for a data frame, to that, and store it
2580 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2581 * which is at the offset in the X register, with an indexed load.
2583 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2585 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2588 s2
= new_stmt(cstate
, BPF_ST
);
2589 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2592 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2597 * Check the Frame Control field to see if this is a data frame;
2598 * a data frame has the 0x08 bit (b3) in that field set and the
2599 * 0x04 bit (b2) clear.
2601 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2602 sjset_data_frame_1
->s
.k
= 0x08;
2603 sappend(s
, sjset_data_frame_1
);
2606 * If b3 is set, test b2, otherwise go to the first statement of
2607 * the rest of the program.
2609 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2610 sjset_data_frame_2
->s
.k
= 0x04;
2611 sappend(s
, sjset_data_frame_2
);
2612 sjset_data_frame_1
->s
.jf
= snext
;
2615 * If b2 is not set, this is a data frame; test the QoS bit.
2616 * Otherwise, go to the first statement of the rest of the
2619 sjset_data_frame_2
->s
.jt
= snext
;
2620 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2621 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2622 sappend(s
, sjset_qos
);
2625 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2627 * Otherwise, go to the first statement of the rest of the
2630 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2631 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2633 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2636 s2
= new_stmt(cstate
, BPF_ST
);
2637 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2641 * If we have a radiotap header, look at it to see whether
2642 * there's Atheros padding between the MAC-layer header
2645 * Note: all of the fields in the radiotap header are
2646 * little-endian, so we byte-swap all of the values
2647 * we test against, as they will be loaded as big-endian
2650 * XXX - in the general case, we would have to scan through
2651 * *all* the presence bits, if there's more than one word of
2652 * presence bits. That would require a loop, meaning that
2653 * we wouldn't be able to run the filter in the kernel.
2655 * We assume here that the Atheros adapters that insert the
2656 * annoying padding don't have multiple antennae and therefore
2657 * do not generate radiotap headers with multiple presence words.
2659 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2661 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2662 * in the first presence flag word?
2664 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2668 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2669 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2670 sappend(s
, sjset_radiotap_flags_present
);
2673 * If not, skip all of this.
2675 sjset_radiotap_flags_present
->s
.jf
= snext
;
2678 * Otherwise, is the "extension" bit set in that word?
2680 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2681 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2682 sappend(s
, sjset_radiotap_ext_present
);
2683 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2686 * If so, skip all of this.
2688 sjset_radiotap_ext_present
->s
.jt
= snext
;
2691 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2693 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2694 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2695 sappend(s
, sjset_radiotap_tsft_present
);
2696 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2699 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2700 * at an offset of 16 from the beginning of the raw packet
2701 * data (8 bytes for the radiotap header and 8 bytes for
2704 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2707 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2710 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2712 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2713 sjset_tsft_datapad
->s
.k
= 0x20;
2714 sappend(s
, sjset_tsft_datapad
);
2717 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2718 * at an offset of 8 from the beginning of the raw packet
2719 * data (8 bytes for the radiotap header).
2721 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2724 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2727 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2729 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2730 sjset_notsft_datapad
->s
.k
= 0x20;
2731 sappend(s
, sjset_notsft_datapad
);
2734 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2735 * set, round the length of the 802.11 header to
2736 * a multiple of 4. Do that by adding 3 and then
2737 * dividing by and multiplying by 4, which we do by
2740 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2741 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2742 sappend(s
, s_roundup
);
2743 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2746 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2749 s2
= new_stmt(cstate
, BPF_ST
);
2750 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2753 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2754 sjset_tsft_datapad
->s
.jf
= snext
;
2755 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2756 sjset_notsft_datapad
->s
.jf
= snext
;
2758 sjset_qos
->s
.jf
= snext
;
2764 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2768 /* There is an implicit dependency between the link
2769 * payload and link header since the payload computation
2770 * includes the variable part of the header. Therefore,
2771 * if nobody else has allocated a register for the link
2772 * header and we need it, do it now. */
2773 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2774 cstate
->off_linkhdr
.reg
== -1)
2775 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2778 * For link-layer types that have a variable-length header
2779 * preceding the link-layer header, generate code to load
2780 * the offset of the link-layer header into the register
2781 * assigned to that offset, if any.
2783 * XXX - this, and the next switch statement, won't handle
2784 * encapsulation of 802.11 or 802.11+radio information in
2785 * some other protocol stack. That's significantly more
2788 switch (cstate
->outermostlinktype
) {
2790 case DLT_PRISM_HEADER
:
2791 s
= gen_load_prism_llprefixlen(cstate
);
2794 case DLT_IEEE802_11_RADIO_AVS
:
2795 s
= gen_load_avs_llprefixlen(cstate
);
2798 case DLT_IEEE802_11_RADIO
:
2799 s
= gen_load_radiotap_llprefixlen(cstate
);
2803 s
= gen_load_ppi_llprefixlen(cstate
);
2812 * For link-layer types that have a variable-length link-layer
2813 * header, generate code to load the offset of the link-layer
2814 * payload into the register assigned to that offset, if any.
2816 switch (cstate
->outermostlinktype
) {
2818 case DLT_IEEE802_11
:
2819 case DLT_PRISM_HEADER
:
2820 case DLT_IEEE802_11_RADIO_AVS
:
2821 case DLT_IEEE802_11_RADIO
:
2823 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2828 * If there there is no initialization yet and we need variable
2829 * length offsets for VLAN, initialize them to zero
2831 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2834 if (cstate
->off_linkpl
.reg
== -1)
2835 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2836 if (cstate
->off_linktype
.reg
== -1)
2837 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2839 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2841 s2
= new_stmt(cstate
, BPF_ST
);
2842 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2844 s2
= new_stmt(cstate
, BPF_ST
);
2845 s2
->s
.k
= cstate
->off_linktype
.reg
;
2850 * If we have any offset-loading code, append all the
2851 * existing statements in the block to those statements,
2852 * and make the resulting list the list of statements
2856 sappend(s
, b
->stmts
);
2861 static struct block
*
2862 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2864 struct slist
*s_load_dlt
;
2867 if (cstate
->linktype
== DLT_PPI
)
2869 /* Create the statements that check for the DLT
2871 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2872 s_load_dlt
->s
.k
= 4;
2874 b
= new_block(cstate
, JMP(BPF_JEQ
));
2876 b
->stmts
= s_load_dlt
;
2877 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2888 * Take an absolute offset, and:
2890 * if it has no variable part, return NULL;
2892 * if it has a variable part, generate code to load the register
2893 * containing that variable part into the X register, returning
2894 * a pointer to that code - if no register for that offset has
2895 * been allocated, allocate it first.
2897 * (The code to set that register will be generated later, but will
2898 * be placed earlier in the code sequence.)
2900 static struct slist
*
2901 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2905 if (off
->is_variable
) {
2906 if (off
->reg
== -1) {
2908 * We haven't yet assigned a register for the
2909 * variable part of the offset of the link-layer
2910 * header; allocate one.
2912 off
->reg
= alloc_reg(cstate
);
2916 * Load the register containing the variable part of the
2917 * offset of the link-layer header into the X register.
2919 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2924 * That offset isn't variable, there's no variable part,
2925 * so we don't need to generate any code.
2932 * Map an Ethernet type to the equivalent PPP type.
2935 ethertype_to_ppptype(proto
)
2944 case ETHERTYPE_IPV6
:
2952 case ETHERTYPE_ATALK
:
2966 * I'm assuming the "Bridging PDU"s that go
2967 * over PPP are Spanning Tree Protocol
2981 * Generate any tests that, for encapsulation of a link-layer packet
2982 * inside another protocol stack, need to be done to check for those
2983 * link-layer packets (and that haven't already been done by a check
2984 * for that encapsulation).
2986 static struct block
*
2987 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
2991 if (cstate
->is_geneve
)
2992 return gen_geneve_ll_check(cstate
);
2994 switch (cstate
->prevlinktype
) {
2998 * This is LANE-encapsulated Ethernet; check that the LANE
2999 * packet doesn't begin with an LE Control marker, i.e.
3000 * that it's data, not a control message.
3002 * (We've already generated a test for LANE.)
3004 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3010 * No such tests are necessary.
3018 * The three different values we should check for when checking for an
3019 * IPv6 packet with DLT_NULL.
3021 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3022 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3023 #define BSD_AFNUM_INET6_DARWIN 30 /* OS X, iOS, other Darwin-based OSes */
3026 * Generate code to match a particular packet type by matching the
3027 * link-layer type field or fields in the 802.2 LLC header.
3029 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3030 * value, if <= ETHERMTU.
3032 static struct block
*
3033 gen_linktype(compiler_state_t
*cstate
, int proto
)
3035 struct block
*b0
, *b1
, *b2
;
3036 const char *description
;
3038 /* are we checking MPLS-encapsulated packets? */
3039 if (cstate
->label_stack_depth
> 0) {
3043 /* FIXME add other L3 proto IDs */
3044 return gen_mpls_linktype(cstate
, Q_IP
);
3046 case ETHERTYPE_IPV6
:
3048 /* FIXME add other L3 proto IDs */
3049 return gen_mpls_linktype(cstate
, Q_IPV6
);
3052 bpf_error(cstate
, "unsupported protocol over mpls");
3057 switch (cstate
->linktype
) {
3060 case DLT_NETANALYZER
:
3061 case DLT_NETANALYZER_TRANSPARENT
:
3062 /* Geneve has an EtherType regardless of whether there is an
3064 if (!cstate
->is_geneve
)
3065 b0
= gen_prevlinkhdr_check(cstate
);
3069 b1
= gen_ether_linktype(cstate
, proto
);
3080 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3084 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3090 case DLT_IEEE802_11
:
3091 case DLT_PRISM_HEADER
:
3092 case DLT_IEEE802_11_RADIO_AVS
:
3093 case DLT_IEEE802_11_RADIO
:
3096 * Check that we have a data frame.
3098 b0
= gen_check_802_11_data_frame(cstate
);
3101 * Now check for the specified link-layer type.
3103 b1
= gen_llc_linktype(cstate
, proto
);
3111 * XXX - check for LLC frames.
3113 return gen_llc_linktype(cstate
, proto
);
3119 * XXX - check for LLC PDUs, as per IEEE 802.5.
3121 return gen_llc_linktype(cstate
, proto
);
3125 case DLT_ATM_RFC1483
:
3127 case DLT_IP_OVER_FC
:
3128 return gen_llc_linktype(cstate
, proto
);
3134 * Check for an LLC-encapsulated version of this protocol;
3135 * if we were checking for LANE, linktype would no longer
3138 * Check for LLC encapsulation and then check the protocol.
3140 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3141 b1
= gen_llc_linktype(cstate
, proto
);
3148 return gen_linux_sll_linktype(cstate
, proto
);
3153 case DLT_SLIP_BSDOS
:
3156 * These types don't provide any type field; packets
3157 * are always IPv4 or IPv6.
3159 * XXX - for IPv4, check for a version number of 4, and,
3160 * for IPv6, check for a version number of 6?
3165 /* Check for a version number of 4. */
3166 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3168 case ETHERTYPE_IPV6
:
3169 /* Check for a version number of 6. */
3170 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3173 return gen_false(cstate
); /* always false */
3180 * Raw IPv4, so no type field.
3182 if (proto
== ETHERTYPE_IP
)
3183 return gen_true(cstate
); /* always true */
3185 /* Checking for something other than IPv4; always false */
3186 return gen_false(cstate
);
3192 * Raw IPv6, so no type field.
3194 if (proto
== ETHERTYPE_IPV6
)
3195 return gen_true(cstate
); /* always true */
3197 /* Checking for something other than IPv6; always false */
3198 return gen_false(cstate
);
3204 case DLT_PPP_SERIAL
:
3207 * We use Ethernet protocol types inside libpcap;
3208 * map them to the corresponding PPP protocol types.
3210 proto
= ethertype_to_ppptype(proto
);
3211 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3217 * We use Ethernet protocol types inside libpcap;
3218 * map them to the corresponding PPP protocol types.
3224 * Also check for Van Jacobson-compressed IP.
3225 * XXX - do this for other forms of PPP?
3227 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3228 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3230 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3235 proto
= ethertype_to_ppptype(proto
);
3236 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3248 return (gen_loopback_linktype(cstate
, AF_INET
));
3250 case ETHERTYPE_IPV6
:
3252 * AF_ values may, unfortunately, be platform-
3253 * dependent; AF_INET isn't, because everybody
3254 * used 4.2BSD's value, but AF_INET6 is, because
3255 * 4.2BSD didn't have a value for it (given that
3256 * IPv6 didn't exist back in the early 1980's),
3257 * and they all picked their own values.
3259 * This means that, if we're reading from a
3260 * savefile, we need to check for all the
3263 * If we're doing a live capture, we only need
3264 * to check for this platform's value; however,
3265 * Npcap uses 24, which isn't Windows's AF_INET6
3266 * value. (Given the multiple different values,
3267 * programs that read pcap files shouldn't be
3268 * checking for their platform's AF_INET6 value
3269 * anyway, they should check for all of the
3270 * possible values. and they might as well do
3271 * that even for live captures.)
3273 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3275 * Savefile - check for all three
3276 * possible IPv6 values.
3278 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3279 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3281 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3286 * Live capture, so we only need to
3287 * check for the value used on this
3292 * Npcap doesn't use Windows's AF_INET6,
3293 * as that collides with AF_IPX on
3294 * some BSDs (both have the value 23).
3295 * Instead, it uses 24.
3297 return (gen_loopback_linktype(cstate
, 24));
3300 return (gen_loopback_linktype(cstate
, AF_INET6
));
3301 #else /* AF_INET6 */
3303 * I guess this platform doesn't support
3304 * IPv6, so we just reject all packets.
3306 return gen_false(cstate
);
3307 #endif /* AF_INET6 */
3313 * Not a type on which we support filtering.
3314 * XXX - support those that have AF_ values
3315 * #defined on this platform, at least?
3317 return gen_false(cstate
);
3320 #ifdef HAVE_NET_PFVAR_H
3323 * af field is host byte order in contrast to the rest of
3326 if (proto
== ETHERTYPE_IP
)
3327 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3328 BPF_B
, (bpf_int32
)AF_INET
));
3329 else if (proto
== ETHERTYPE_IPV6
)
3330 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3331 BPF_B
, (bpf_int32
)AF_INET6
));
3333 return gen_false(cstate
);
3336 #endif /* HAVE_NET_PFVAR_H */
3339 case DLT_ARCNET_LINUX
:
3341 * XXX should we check for first fragment if the protocol
3347 return gen_false(cstate
);
3349 case ETHERTYPE_IPV6
:
3350 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3351 (bpf_int32
)ARCTYPE_INET6
));
3354 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3355 (bpf_int32
)ARCTYPE_IP
);
3356 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3357 (bpf_int32
)ARCTYPE_IP_OLD
);
3362 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3363 (bpf_int32
)ARCTYPE_ARP
);
3364 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3365 (bpf_int32
)ARCTYPE_ARP_OLD
);
3369 case ETHERTYPE_REVARP
:
3370 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3371 (bpf_int32
)ARCTYPE_REVARP
));
3373 case ETHERTYPE_ATALK
:
3374 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3375 (bpf_int32
)ARCTYPE_ATALK
));
3382 case ETHERTYPE_ATALK
:
3383 return gen_true(cstate
);
3385 return gen_false(cstate
);
3392 * XXX - assumes a 2-byte Frame Relay header with
3393 * DLCI and flags. What if the address is longer?
3399 * Check for the special NLPID for IP.
3401 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3403 case ETHERTYPE_IPV6
:
3405 * Check for the special NLPID for IPv6.
3407 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3411 * Check for several OSI protocols.
3413 * Frame Relay packets typically have an OSI
3414 * NLPID at the beginning; we check for each
3417 * What we check for is the NLPID and a frame
3418 * control field of UI, i.e. 0x03 followed
3421 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3422 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3423 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3429 return gen_false(cstate
);
3435 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3437 case DLT_JUNIPER_MFR
:
3438 case DLT_JUNIPER_MLFR
:
3439 case DLT_JUNIPER_MLPPP
:
3440 case DLT_JUNIPER_ATM1
:
3441 case DLT_JUNIPER_ATM2
:
3442 case DLT_JUNIPER_PPPOE
:
3443 case DLT_JUNIPER_PPPOE_ATM
:
3444 case DLT_JUNIPER_GGSN
:
3445 case DLT_JUNIPER_ES
:
3446 case DLT_JUNIPER_MONITOR
:
3447 case DLT_JUNIPER_SERVICES
:
3448 case DLT_JUNIPER_ETHER
:
3449 case DLT_JUNIPER_PPP
:
3450 case DLT_JUNIPER_FRELAY
:
3451 case DLT_JUNIPER_CHDLC
:
3452 case DLT_JUNIPER_VP
:
3453 case DLT_JUNIPER_ST
:
3454 case DLT_JUNIPER_ISM
:
3455 case DLT_JUNIPER_VS
:
3456 case DLT_JUNIPER_SRX_E2E
:
3457 case DLT_JUNIPER_FIBRECHANNEL
:
3458 case DLT_JUNIPER_ATM_CEMIC
:
3460 /* just lets verify the magic number for now -
3461 * on ATM we may have up to 6 different encapsulations on the wire
3462 * and need a lot of heuristics to figure out that the payload
3465 * FIXME encapsulation specific BPF_ filters
3467 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3469 case DLT_BACNET_MS_TP
:
3470 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3473 return gen_ipnet_linktype(cstate
, proto
);
3475 case DLT_LINUX_IRDA
:
3476 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3479 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3482 case DLT_MTP2_WITH_PHDR
:
3483 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3486 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3489 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3491 case DLT_LINUX_LAPD
:
3492 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3494 case DLT_USB_FREEBSD
:
3496 case DLT_USB_LINUX_MMAPPED
:
3498 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3500 case DLT_BLUETOOTH_HCI_H4
:
3501 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3502 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3505 case DLT_CAN_SOCKETCAN
:
3506 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3508 case DLT_IEEE802_15_4
:
3509 case DLT_IEEE802_15_4_LINUX
:
3510 case DLT_IEEE802_15_4_NONASK_PHY
:
3511 case DLT_IEEE802_15_4_NOFCS
:
3512 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3514 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3515 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3518 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3521 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3524 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3527 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3530 /* Using the fixed-size NFLOG header it is possible to tell only
3531 * the address family of the packet, other meaningful data is
3532 * either missing or behind TLVs.
3534 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3538 * Does this link-layer header type have a field
3539 * indicating the type of the next protocol? If
3540 * so, off_linktype.constant_part will be the offset of that
3541 * field in the packet; if not, it will be OFFSET_NOT_SET.
3543 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3545 * Yes; assume it's an Ethernet type. (If
3546 * it's not, it needs to be handled specially
3549 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3552 * No; report an error.
3554 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3555 if (description
!= NULL
) {
3556 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3559 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3568 * Check for an LLC SNAP packet with a given organization code and
3569 * protocol type; we check the entire contents of the 802.2 LLC and
3570 * snap headers, checking for DSAP and SSAP of SNAP and a control
3571 * field of 0x03 in the LLC header, and for the specified organization
3572 * code and protocol type in the SNAP header.
3574 static struct block
*
3575 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3577 u_char snapblock
[8];
3579 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3580 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3581 snapblock
[2] = 0x03; /* control = UI */
3582 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3583 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3584 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3585 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3586 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3587 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3591 * Generate code to match frames with an LLC header.
3594 gen_llc(compiler_state_t
*cstate
)
3596 struct block
*b0
, *b1
;
3598 switch (cstate
->linktype
) {
3602 * We check for an Ethernet type field less than
3603 * 1500, which means it's an 802.3 length field.
3605 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3609 * Now check for the purported DSAP and SSAP not being
3610 * 0xFF, to rule out NetWare-over-802.3.
3612 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3619 * We check for LLC traffic.
3621 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3624 case DLT_IEEE802
: /* Token Ring */
3626 * XXX - check for LLC frames.
3628 return gen_true(cstate
);
3632 * XXX - check for LLC frames.
3634 return gen_true(cstate
);
3636 case DLT_ATM_RFC1483
:
3638 * For LLC encapsulation, these are defined to have an
3641 * For VC encapsulation, they don't, but there's no
3642 * way to check for that; the protocol used on the VC
3643 * is negotiated out of band.
3645 return gen_true(cstate
);
3647 case DLT_IEEE802_11
:
3648 case DLT_PRISM_HEADER
:
3649 case DLT_IEEE802_11_RADIO
:
3650 case DLT_IEEE802_11_RADIO_AVS
:
3653 * Check that we have a data frame.
3655 b0
= gen_check_802_11_data_frame(cstate
);
3659 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3665 gen_llc_i(compiler_state_t
*cstate
)
3667 struct block
*b0
, *b1
;
3671 * Check whether this is an LLC frame.
3673 b0
= gen_llc(cstate
);
3676 * Load the control byte and test the low-order bit; it must
3677 * be clear for I frames.
3679 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3680 b1
= new_block(cstate
, JMP(BPF_JSET
));
3689 gen_llc_s(compiler_state_t
*cstate
)
3691 struct block
*b0
, *b1
;
3694 * Check whether this is an LLC frame.
3696 b0
= gen_llc(cstate
);
3699 * Now compare the low-order 2 bit of the control byte against
3700 * the appropriate value for S frames.
3702 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3708 gen_llc_u(compiler_state_t
*cstate
)
3710 struct block
*b0
, *b1
;
3713 * Check whether this is an LLC frame.
3715 b0
= gen_llc(cstate
);
3718 * Now compare the low-order 2 bit of the control byte against
3719 * the appropriate value for U frames.
3721 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3727 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3729 struct block
*b0
, *b1
;
3732 * Check whether this is an LLC frame.
3734 b0
= gen_llc(cstate
);
3737 * Now check for an S frame with the appropriate type.
3739 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3745 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3747 struct block
*b0
, *b1
;
3750 * Check whether this is an LLC frame.
3752 b0
= gen_llc(cstate
);
3755 * Now check for a U frame with the appropriate type.
3757 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3763 * Generate code to match a particular packet type, for link-layer types
3764 * using 802.2 LLC headers.
3766 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3767 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3769 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3770 * value, if <= ETHERMTU. We use that to determine whether to
3771 * match the DSAP or both DSAP and LSAP or to check the OUI and
3772 * protocol ID in a SNAP header.
3774 static struct block
*
3775 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3778 * XXX - handle token-ring variable-length header.
3784 case LLCSAP_NETBEUI
:
3786 * XXX - should we check both the DSAP and the
3787 * SSAP, like this, or should we check just the
3788 * DSAP, as we do for other SAP values?
3790 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3791 ((proto
<< 8) | proto
));
3795 * XXX - are there ever SNAP frames for IPX on
3796 * non-Ethernet 802.x networks?
3798 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3799 (bpf_int32
)LLCSAP_IPX
);
3801 case ETHERTYPE_ATALK
:
3803 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3804 * SNAP packets with an organization code of
3805 * 0x080007 (Apple, for Appletalk) and a protocol
3806 * type of ETHERTYPE_ATALK (Appletalk).
3808 * XXX - check for an organization code of
3809 * encapsulated Ethernet as well?
3811 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3815 * XXX - we don't have to check for IPX 802.3
3816 * here, but should we check for the IPX Ethertype?
3818 if (proto
<= ETHERMTU
) {
3820 * This is an LLC SAP value, so check
3823 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3826 * This is an Ethernet type; we assume that it's
3827 * unlikely that it'll appear in the right place
3828 * at random, and therefore check only the
3829 * location that would hold the Ethernet type
3830 * in a SNAP frame with an organization code of
3831 * 0x000000 (encapsulated Ethernet).
3833 * XXX - if we were to check for the SNAP DSAP and
3834 * LSAP, as per XXX, and were also to check for an
3835 * organization code of 0x000000 (encapsulated
3836 * Ethernet), we'd do
3838 * return gen_snap(cstate, 0x000000, proto);
3840 * here; for now, we don't, as per the above.
3841 * I don't know whether it's worth the extra CPU
3842 * time to do the right check or not.
3844 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3849 static struct block
*
3850 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3851 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3853 struct block
*b0
, *b1
;
3867 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3868 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3874 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3875 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3882 b0
= gen_linktype(cstate
, proto
);
3883 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3889 static struct block
*
3890 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3891 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3893 struct block
*b0
, *b1
;
3908 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3909 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3915 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3916 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3923 /* this order is important */
3924 a
= (u_int32_t
*)addr
;
3925 m
= (u_int32_t
*)mask
;
3926 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3927 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3929 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3931 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3933 b0
= gen_linktype(cstate
, proto
);
3939 static struct block
*
3940 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3942 register struct block
*b0
, *b1
;
3946 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
3949 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
3952 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3953 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3959 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3960 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3965 bpf_error(cstate
, "'addr1' is only supported on 802.11 with 802.11 headers");
3969 bpf_error(cstate
, "'addr2' is only supported on 802.11 with 802.11 headers");
3973 bpf_error(cstate
, "'addr3' is only supported on 802.11 with 802.11 headers");
3977 bpf_error(cstate
, "'addr4' is only supported on 802.11 with 802.11 headers");
3981 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
3985 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
3993 * Like gen_ehostop, but for DLT_FDDI
3995 static struct block
*
3996 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3998 struct block
*b0
, *b1
;
4002 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4005 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4008 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4009 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4015 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4016 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4021 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4025 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4029 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4033 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4037 bpf_error(cstate
, "'ra' is only supported on 802.11");
4041 bpf_error(cstate
, "'ta' is only supported on 802.11");
4049 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4051 static struct block
*
4052 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4054 register struct block
*b0
, *b1
;
4058 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4061 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4064 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4065 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4071 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4072 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4077 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4081 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4085 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4089 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4093 bpf_error(cstate
, "'ra' is only supported on 802.11");
4097 bpf_error(cstate
, "'ta' is only supported on 802.11");
4105 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4106 * various 802.11 + radio headers.
4108 static struct block
*
4109 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4111 register struct block
*b0
, *b1
, *b2
;
4112 register struct slist
*s
;
4114 #ifdef ENABLE_WLAN_FILTERING_PATCH
4117 * We need to disable the optimizer because the optimizer is buggy
4118 * and wipes out some LD instructions generated by the below
4119 * code to validate the Frame Control bits
4121 cstate
->no_optimize
= 1;
4122 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4129 * For control frames, there is no SA.
4131 * For management frames, SA is at an
4132 * offset of 10 from the beginning of
4135 * For data frames, SA is at an offset
4136 * of 10 from the beginning of the packet
4137 * if From DS is clear, at an offset of
4138 * 16 from the beginning of the packet
4139 * if From DS is set and To DS is clear,
4140 * and an offset of 24 from the beginning
4141 * of the packet if From DS is set and To DS
4146 * Generate the tests to be done for data frames
4149 * First, check for To DS set, i.e. check "link[1] & 0x01".
4151 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4152 b1
= new_block(cstate
, JMP(BPF_JSET
));
4153 b1
->s
.k
= 0x01; /* To DS */
4157 * If To DS is set, the SA is at 24.
4159 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4163 * Now, check for To DS not set, i.e. check
4164 * "!(link[1] & 0x01)".
4166 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4167 b2
= new_block(cstate
, JMP(BPF_JSET
));
4168 b2
->s
.k
= 0x01; /* To DS */
4173 * If To DS is not set, the SA is at 16.
4175 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4179 * Now OR together the last two checks. That gives
4180 * the complete set of checks for data frames with
4186 * Now check for From DS being set, and AND that with
4187 * the ORed-together checks.
4189 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4190 b1
= new_block(cstate
, JMP(BPF_JSET
));
4191 b1
->s
.k
= 0x02; /* From DS */
4196 * Now check for data frames with From DS not set.
4198 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4199 b2
= new_block(cstate
, JMP(BPF_JSET
));
4200 b2
->s
.k
= 0x02; /* From DS */
4205 * If From DS isn't set, the SA is at 10.
4207 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4211 * Now OR together the checks for data frames with
4212 * From DS not set and for data frames with From DS
4213 * set; that gives the checks done for data frames.
4218 * Now check for a data frame.
4219 * I.e, check "link[0] & 0x08".
4221 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4222 b1
= new_block(cstate
, JMP(BPF_JSET
));
4227 * AND that with the checks done for data frames.
4232 * If the high-order bit of the type value is 0, this
4233 * is a management frame.
4234 * I.e, check "!(link[0] & 0x08)".
4236 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4237 b2
= new_block(cstate
, JMP(BPF_JSET
));
4243 * For management frames, the SA is at 10.
4245 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4249 * OR that with the checks done for data frames.
4250 * That gives the checks done for management and
4256 * If the low-order bit of the type value is 1,
4257 * this is either a control frame or a frame
4258 * with a reserved type, and thus not a
4261 * I.e., check "!(link[0] & 0x04)".
4263 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4264 b1
= new_block(cstate
, JMP(BPF_JSET
));
4270 * AND that with the checks for data and management
4280 * For control frames, there is no DA.
4282 * For management frames, DA is at an
4283 * offset of 4 from the beginning of
4286 * For data frames, DA is at an offset
4287 * of 4 from the beginning of the packet
4288 * if To DS is clear and at an offset of
4289 * 16 from the beginning of the packet
4294 * Generate the tests to be done for data frames.
4296 * First, check for To DS set, i.e. "link[1] & 0x01".
4298 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4299 b1
= new_block(cstate
, JMP(BPF_JSET
));
4300 b1
->s
.k
= 0x01; /* To DS */
4304 * If To DS is set, the DA is at 16.
4306 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4310 * Now, check for To DS not set, i.e. check
4311 * "!(link[1] & 0x01)".
4313 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4314 b2
= new_block(cstate
, JMP(BPF_JSET
));
4315 b2
->s
.k
= 0x01; /* To DS */
4320 * If To DS is not set, the DA is at 4.
4322 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4326 * Now OR together the last two checks. That gives
4327 * the complete set of checks for data frames.
4332 * Now check for a data frame.
4333 * I.e, check "link[0] & 0x08".
4335 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4336 b1
= new_block(cstate
, JMP(BPF_JSET
));
4341 * AND that with the checks done for data frames.
4346 * If the high-order bit of the type value is 0, this
4347 * is a management frame.
4348 * I.e, check "!(link[0] & 0x08)".
4350 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4351 b2
= new_block(cstate
, JMP(BPF_JSET
));
4357 * For management frames, the DA is at 4.
4359 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4363 * OR that with the checks done for data frames.
4364 * That gives the checks done for management and
4370 * If the low-order bit of the type value is 1,
4371 * this is either a control frame or a frame
4372 * with a reserved type, and thus not a
4375 * I.e., check "!(link[0] & 0x04)".
4377 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4378 b1
= new_block(cstate
, JMP(BPF_JSET
));
4384 * AND that with the checks for data and management
4392 * Not present in management frames; addr1 in other
4397 * If the high-order bit of the type value is 0, this
4398 * is a management frame.
4399 * I.e, check "(link[0] & 0x08)".
4401 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4402 b1
= new_block(cstate
, JMP(BPF_JSET
));
4409 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4412 * AND that with the check of addr1.
4419 * Not present in management frames; addr2, if present,
4424 * Not present in CTS or ACK control frames.
4426 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4427 IEEE80211_FC0_TYPE_MASK
);
4429 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4430 IEEE80211_FC0_SUBTYPE_MASK
);
4432 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4433 IEEE80211_FC0_SUBTYPE_MASK
);
4439 * If the high-order bit of the type value is 0, this
4440 * is a management frame.
4441 * I.e, check "(link[0] & 0x08)".
4443 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4444 b1
= new_block(cstate
, JMP(BPF_JSET
));
4449 * AND that with the check for frames other than
4450 * CTS and ACK frames.
4457 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4462 * XXX - add BSSID keyword?
4465 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4469 * Not present in CTS or ACK control frames.
4471 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4472 IEEE80211_FC0_TYPE_MASK
);
4474 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4475 IEEE80211_FC0_SUBTYPE_MASK
);
4477 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4478 IEEE80211_FC0_SUBTYPE_MASK
);
4482 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4488 * Not present in control frames.
4490 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4491 IEEE80211_FC0_TYPE_MASK
);
4493 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4499 * Present only if the direction mask has both "From DS"
4500 * and "To DS" set. Neither control frames nor management
4501 * frames should have both of those set, so we don't
4502 * check the frame type.
4504 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4505 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4506 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4511 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4512 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4518 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4519 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4528 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4529 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4530 * as the RFC states.)
4532 static struct block
*
4533 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4535 register struct block
*b0
, *b1
;
4539 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4542 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4545 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4546 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4552 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4553 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4558 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4562 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4566 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4570 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4574 bpf_error(cstate
, "'ra' is only supported on 802.11");
4578 bpf_error(cstate
, "'ta' is only supported on 802.11");
4586 * This is quite tricky because there may be pad bytes in front of the
4587 * DECNET header, and then there are two possible data packet formats that
4588 * carry both src and dst addresses, plus 5 packet types in a format that
4589 * carries only the src node, plus 2 types that use a different format and
4590 * also carry just the src node.
4594 * Instead of doing those all right, we just look for data packets with
4595 * 0 or 1 bytes of padding. If you want to look at other packets, that
4596 * will require a lot more hacking.
4598 * To add support for filtering on DECNET "areas" (network numbers)
4599 * one would want to add a "mask" argument to this routine. That would
4600 * make the filter even more inefficient, although one could be clever
4601 * and not generate masking instructions if the mask is 0xFFFF.
4603 static struct block
*
4604 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4606 struct block
*b0
, *b1
, *b2
, *tmp
;
4607 u_int offset_lh
; /* offset if long header is received */
4608 u_int offset_sh
; /* offset if short header is received */
4613 offset_sh
= 1; /* follows flags */
4614 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4618 offset_sh
= 3; /* follows flags, dstnode */
4619 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4623 /* Inefficient because we do our Calvinball dance twice */
4624 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4625 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4631 /* Inefficient because we do our Calvinball dance twice */
4632 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4633 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4638 bpf_error(cstate
, "ISO host filtering not implemented");
4643 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4644 /* Check for pad = 1, long header case */
4645 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4646 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4647 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4648 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4650 /* Check for pad = 0, long header case */
4651 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4652 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4655 /* Check for pad = 1, short header case */
4656 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4657 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4658 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4661 /* Check for pad = 0, short header case */
4662 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4663 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4667 /* Combine with test for cstate->linktype */
4673 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4674 * test the bottom-of-stack bit, and then check the version number
4675 * field in the IP header.
4677 static struct block
*
4678 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4680 struct block
*b0
, *b1
;
4685 /* match the bottom-of-stack bit */
4686 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4687 /* match the IPv4 version number */
4688 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4693 /* match the bottom-of-stack bit */
4694 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4695 /* match the IPv4 version number */
4696 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4705 static struct block
*
4706 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4707 int proto
, int dir
, int type
)
4709 struct block
*b0
, *b1
;
4710 const char *typestr
;
4720 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4722 * Only check for non-IPv4 addresses if we're not
4723 * checking MPLS-encapsulated packets.
4725 if (cstate
->label_stack_depth
== 0) {
4726 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4728 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4734 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4737 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4740 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4743 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4746 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4749 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4752 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4755 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4758 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4761 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4764 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4767 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4770 bpf_error(cstate
, "ATALK host filtering not implemented");
4773 bpf_error(cstate
, "AARP host filtering not implemented");
4776 return gen_dnhostop(cstate
, addr
, dir
);
4779 bpf_error(cstate
, "SCA host filtering not implemented");
4782 bpf_error(cstate
, "LAT host filtering not implemented");
4785 bpf_error(cstate
, "MOPDL host filtering not implemented");
4788 bpf_error(cstate
, "MOPRC host filtering not implemented");
4791 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4794 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4797 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4800 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4803 bpf_error(cstate
, "ISO host filtering not implemented");
4806 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4809 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4812 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4815 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4818 bpf_error(cstate
, "IPX host filtering not implemented");
4821 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4824 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4833 static struct block
*
4834 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4835 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4837 const char *typestr
;
4847 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4850 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4853 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4856 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4859 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4862 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4865 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4868 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4871 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4874 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4877 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4880 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4883 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4886 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4889 bpf_error(cstate
, "ATALK host filtering not implemented");
4892 bpf_error(cstate
, "AARP host filtering not implemented");
4895 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
4898 bpf_error(cstate
, "SCA host filtering not implemented");
4901 bpf_error(cstate
, "LAT host filtering not implemented");
4904 bpf_error(cstate
, "MOPDL host filtering not implemented");
4907 bpf_error(cstate
, "MOPRC host filtering not implemented");
4910 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4913 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4916 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4919 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4922 bpf_error(cstate
, "ISO host filtering not implemented");
4925 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4928 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4931 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4934 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4937 bpf_error(cstate
, "IPX host filtering not implemented");
4940 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4943 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4953 static struct block
*
4954 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
, bpf_u_int32
**alist
,
4957 struct block
*b0
, *b1
, *tmp
;
4960 bpf_error(cstate
, "direction applied to 'gateway'");
4967 switch (cstate
->linktype
) {
4969 case DLT_NETANALYZER
:
4970 case DLT_NETANALYZER_TRANSPARENT
:
4971 b1
= gen_prevlinkhdr_check(cstate
);
4972 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
4977 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
4980 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
4982 case DLT_IEEE802_11
:
4983 case DLT_PRISM_HEADER
:
4984 case DLT_IEEE802_11_RADIO_AVS
:
4985 case DLT_IEEE802_11_RADIO
:
4987 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
4991 * This is LLC-multiplexed traffic; if it were
4992 * LANE, cstate->linktype would have been set to
4996 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4998 case DLT_IP_OVER_FC
:
4999 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5003 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5005 b1
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
5007 tmp
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
,
5016 bpf_error(cstate
, "illegal modifier of 'gateway'");
5022 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5030 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5031 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5036 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5037 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5042 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5043 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5048 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5051 #ifndef IPPROTO_IGMP
5052 #define IPPROTO_IGMP 2
5056 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5059 #ifndef IPPROTO_IGRP
5060 #define IPPROTO_IGRP 9
5063 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5067 #define IPPROTO_PIM 103
5071 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5072 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5076 #ifndef IPPROTO_VRRP
5077 #define IPPROTO_VRRP 112
5081 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5084 #ifndef IPPROTO_CARP
5085 #define IPPROTO_CARP 112
5089 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5093 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5097 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5101 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5105 bpf_error(cstate
, "link layer applied in wrong context");
5108 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5112 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5116 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5120 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5124 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5128 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5132 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5136 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5139 #ifndef IPPROTO_ICMPV6
5140 #define IPPROTO_ICMPV6 58
5143 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5147 #define IPPROTO_AH 51
5150 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5151 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5156 #define IPPROTO_ESP 50
5159 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5160 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5165 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5169 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5173 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5176 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5177 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5178 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5180 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5182 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5184 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5188 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5189 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5190 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5192 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5194 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5196 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5200 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5201 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5202 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5204 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5209 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5210 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5215 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5216 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5218 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5220 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5225 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5226 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5231 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5232 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5237 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5241 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5245 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5249 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5253 bpf_error(cstate
, "'radio' is not a valid protocol type");
5261 static struct block
*
5262 gen_ipfrag(compiler_state_t
*cstate
)
5267 /* not IPv4 frag other than the first frag */
5268 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5269 b
= new_block(cstate
, JMP(BPF_JSET
));
5278 * Generate a comparison to a port value in the transport-layer header
5279 * at the specified offset from the beginning of that header.
5281 * XXX - this handles a variable-length prefix preceding the link-layer
5282 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5283 * variable-length link-layer headers (such as Token Ring or 802.11
5286 static struct block
*
5287 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5289 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5292 static struct block
*
5293 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5295 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5299 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5301 struct block
*b0
, *b1
, *tmp
;
5303 /* ip proto 'proto' and not a fragment other than the first fragment */
5304 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5305 b0
= gen_ipfrag(cstate
);
5310 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5314 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5319 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5320 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5325 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5326 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5338 static struct block
*
5339 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5341 struct block
*b0
, *b1
, *tmp
;
5346 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5347 * not LLC encapsulation with LLCSAP_IP.
5349 * For IEEE 802 networks - which includes 802.5 token ring
5350 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5351 * says that SNAP encapsulation is used, not LLC encapsulation
5354 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5355 * RFC 2225 say that SNAP encapsulation is used, not LLC
5356 * encapsulation with LLCSAP_IP.
5358 * So we always check for ETHERTYPE_IP.
5360 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5366 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5370 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5371 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5373 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5385 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5387 struct block
*b0
, *b1
, *tmp
;
5389 /* ip6 proto 'proto' */
5390 /* XXX - catch the first fragment of a fragmented packet? */
5391 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5395 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5399 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5404 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5405 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5410 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5411 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5423 static struct block
*
5424 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5426 struct block
*b0
, *b1
, *tmp
;
5428 /* link proto ip6 */
5429 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5435 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5439 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5440 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5442 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5453 /* gen_portrange code */
5454 static struct block
*
5455 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5458 struct block
*b1
, *b2
;
5462 * Reverse the order of the ports, so v1 is the lower one.
5471 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5472 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5480 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5483 struct block
*b0
, *b1
, *tmp
;
5485 /* ip proto 'proto' and not a fragment other than the first fragment */
5486 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5487 b0
= gen_ipfrag(cstate
);
5492 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5496 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5501 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5502 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5507 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5508 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5520 static struct block
*
5521 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5524 struct block
*b0
, *b1
, *tmp
;
5527 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5533 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5537 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5538 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5540 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5551 static struct block
*
5552 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5555 struct block
*b1
, *b2
;
5559 * Reverse the order of the ports, so v1 is the lower one.
5568 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5569 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5577 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5580 struct block
*b0
, *b1
, *tmp
;
5582 /* ip6 proto 'proto' */
5583 /* XXX - catch the first fragment of a fragmented packet? */
5584 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5588 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5592 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5597 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5598 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5603 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5604 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5616 static struct block
*
5617 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5620 struct block
*b0
, *b1
, *tmp
;
5622 /* link proto ip6 */
5623 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5629 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5633 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5634 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5636 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5648 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5657 v
= pcap_nametoproto(name
);
5658 if (v
== PROTO_UNDEF
)
5659 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5663 /* XXX should look up h/w protocol type based on cstate->linktype */
5664 v
= pcap_nametoeproto(name
);
5665 if (v
== PROTO_UNDEF
) {
5666 v
= pcap_nametollc(name
);
5667 if (v
== PROTO_UNDEF
)
5668 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5673 if (strcmp(name
, "esis") == 0)
5675 else if (strcmp(name
, "isis") == 0)
5677 else if (strcmp(name
, "clnp") == 0)
5680 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5700 static struct block
*
5701 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5703 #ifdef NO_PROTOCHAIN
5704 return gen_proto(cstate
, v
, proto
, dir
);
5706 struct block
*b0
, *b
;
5707 struct slist
*s
[100];
5708 int fix2
, fix3
, fix4
, fix5
;
5709 int ahcheck
, again
, end
;
5711 int reg2
= alloc_reg(cstate
);
5713 memset(s
, 0, sizeof(s
));
5714 fix3
= fix4
= fix5
= 0;
5721 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5722 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5726 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5731 * We don't handle variable-length prefixes before the link-layer
5732 * header, or variable-length link-layer headers, here yet.
5733 * We might want to add BPF instructions to do the protochain
5734 * work, to simplify that and, on platforms that have a BPF
5735 * interpreter with the new instructions, let the filtering
5736 * be done in the kernel. (We already require a modified BPF
5737 * engine to do the protochain stuff, to support backward
5738 * branches, and backward branch support is unlikely to appear
5739 * in kernel BPF engines.)
5741 if (cstate
->off_linkpl
.is_variable
)
5742 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5744 cstate
->no_optimize
= 1; /*this code is not compatible with optimzer yet */
5747 * s[0] is a dummy entry to protect other BPF insn from damage
5748 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5749 * hard to find interdependency made by jump table fixup.
5752 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5757 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5760 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5761 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5763 /* X = ip->ip_hl << 2 */
5764 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5765 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5770 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5772 /* A = ip6->ip_nxt */
5773 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5774 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
5776 /* X = sizeof(struct ip6_hdr) */
5777 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
5783 bpf_error(cstate
, "unsupported proto to gen_protochain");
5787 /* again: if (A == v) goto end; else fall through; */
5789 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5791 s
[i
]->s
.jt
= NULL
; /*later*/
5792 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5796 #ifndef IPPROTO_NONE
5797 #define IPPROTO_NONE 59
5799 /* if (A == IPPROTO_NONE) goto end */
5800 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5801 s
[i
]->s
.jt
= NULL
; /*later*/
5802 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5803 s
[i
]->s
.k
= IPPROTO_NONE
;
5804 s
[fix5
]->s
.jf
= s
[i
];
5808 if (proto
== Q_IPV6
) {
5809 int v6start
, v6end
, v6advance
, j
;
5812 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5813 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5814 s
[i
]->s
.jt
= NULL
; /*later*/
5815 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5816 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5817 s
[fix2
]->s
.jf
= s
[i
];
5819 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5820 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5821 s
[i
]->s
.jt
= NULL
; /*later*/
5822 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5823 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5825 /* if (A == IPPROTO_ROUTING) goto v6advance */
5826 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5827 s
[i
]->s
.jt
= NULL
; /*later*/
5828 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5829 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5831 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5832 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5833 s
[i
]->s
.jt
= NULL
; /*later*/
5834 s
[i
]->s
.jf
= NULL
; /*later*/
5835 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5845 * A = P[X + packet head];
5846 * X = X + (P[X + packet head + 1] + 1) * 8;
5848 /* A = P[X + packet head] */
5849 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5850 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5853 s
[i
] = new_stmt(cstate
, BPF_ST
);
5856 /* A = P[X + packet head + 1]; */
5857 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5858 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
5861 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5865 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5869 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
5873 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5876 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5880 /* goto again; (must use BPF_JA for backward jump) */
5881 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5882 s
[i
]->s
.k
= again
- i
- 1;
5883 s
[i
- 1]->s
.jf
= s
[i
];
5887 for (j
= v6start
; j
<= v6end
; j
++)
5888 s
[j
]->s
.jt
= s
[v6advance
];
5891 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5893 s
[fix2
]->s
.jf
= s
[i
];
5899 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5900 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5901 s
[i
]->s
.jt
= NULL
; /*later*/
5902 s
[i
]->s
.jf
= NULL
; /*later*/
5903 s
[i
]->s
.k
= IPPROTO_AH
;
5905 s
[fix3
]->s
.jf
= s
[ahcheck
];
5912 * X = X + (P[X + 1] + 2) * 4;
5915 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5917 /* A = P[X + packet head]; */
5918 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5919 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5922 s
[i
] = new_stmt(cstate
, BPF_ST
);
5926 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5929 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5933 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5935 /* A = P[X + packet head] */
5936 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5937 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5940 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5944 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5948 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5951 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5955 /* goto again; (must use BPF_JA for backward jump) */
5956 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5957 s
[i
]->s
.k
= again
- i
- 1;
5962 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5964 s
[fix2
]->s
.jt
= s
[end
];
5965 s
[fix4
]->s
.jf
= s
[end
];
5966 s
[fix5
]->s
.jt
= s
[end
];
5973 for (i
= 0; i
< max
- 1; i
++)
5974 s
[i
]->next
= s
[i
+ 1];
5975 s
[max
- 1]->next
= NULL
;
5980 b
= new_block(cstate
, JMP(BPF_JEQ
));
5981 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5984 free_reg(cstate
, reg2
);
5991 static struct block
*
5992 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
5995 struct block
*b0
, *b1
;
5998 * A data frame has the 0x08 bit (b3) in the frame control field set
5999 * and the 0x04 bit (b2) clear.
6001 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6002 b0
= new_block(cstate
, JMP(BPF_JSET
));
6006 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6007 b1
= new_block(cstate
, JMP(BPF_JSET
));
6018 * Generate code that checks whether the packet is a packet for protocol
6019 * <proto> and whether the type field in that protocol's header has
6020 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6021 * IP packet and checks the protocol number in the IP header against <v>.
6023 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6024 * against Q_IP and Q_IPV6.
6026 static struct block
*
6027 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6029 struct block
*b0
, *b1
;
6034 if (dir
!= Q_DEFAULT
)
6035 bpf_error(cstate
, "direction applied to 'proto'");
6039 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6040 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6046 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6047 * not LLC encapsulation with LLCSAP_IP.
6049 * For IEEE 802 networks - which includes 802.5 token ring
6050 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6051 * says that SNAP encapsulation is used, not LLC encapsulation
6054 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6055 * RFC 2225 say that SNAP encapsulation is used, not LLC
6056 * encapsulation with LLCSAP_IP.
6058 * So we always check for ETHERTYPE_IP.
6060 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6062 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6064 b1
= gen_protochain(cstate
, v
, Q_IP
);
6070 switch (cstate
->linktype
) {
6074 * Frame Relay packets typically have an OSI
6075 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6076 * generates code to check for all the OSI
6077 * NLPIDs, so calling it and then adding a check
6078 * for the particular NLPID for which we're
6079 * looking is bogus, as we can just check for
6082 * What we check for is the NLPID and a frame
6083 * control field value of UI, i.e. 0x03 followed
6086 * XXX - assumes a 2-byte Frame Relay header with
6087 * DLCI and flags. What if the address is longer?
6089 * XXX - what about SNAP-encapsulated frames?
6091 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6097 * Cisco uses an Ethertype lookalike - for OSI,
6100 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6101 /* OSI in C-HDLC is stuffed with a fudge byte */
6102 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6107 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6108 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6114 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6116 * 4 is the offset of the PDU type relative to the IS-IS
6119 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6124 bpf_error(cstate
, "arp does not encapsulate another protocol");
6128 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6132 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6136 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6140 bpf_error(cstate
, "sca does not encapsulate another protocol");
6144 bpf_error(cstate
, "lat does not encapsulate another protocol");
6148 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6152 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6156 return gen_linktype(cstate
, v
);
6159 bpf_error(cstate
, "'udp proto' is bogus");
6163 bpf_error(cstate
, "'tcp proto' is bogus");
6167 bpf_error(cstate
, "'sctp proto' is bogus");
6171 bpf_error(cstate
, "'icmp proto' is bogus");
6175 bpf_error(cstate
, "'igmp proto' is bogus");
6179 bpf_error(cstate
, "'igrp proto' is bogus");
6183 bpf_error(cstate
, "'pim proto' is bogus");
6187 bpf_error(cstate
, "'vrrp proto' is bogus");
6191 bpf_error(cstate
, "'carp proto' is bogus");
6195 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6198 * Also check for a fragment header before the final
6201 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6202 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6204 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6207 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6213 bpf_error(cstate
, "'icmp6 proto' is bogus");
6216 bpf_error(cstate
, "'ah proto' is bogus");
6219 bpf_error(cstate
, "'ah proto' is bogus");
6222 bpf_error(cstate
, "'stp proto' is bogus");
6225 bpf_error(cstate
, "'ipx proto' is bogus");
6228 bpf_error(cstate
, "'netbeui proto' is bogus");
6231 bpf_error(cstate
, "'radio proto' is bogus");
6241 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6243 int proto
= q
.proto
;
6247 bpf_u_int32 mask
, addr
;
6249 bpf_u_int32
**alist
;
6252 struct sockaddr_in
*sin4
;
6253 struct sockaddr_in6
*sin6
;
6254 struct addrinfo
*res
, *res0
;
6255 struct in6_addr mask128
;
6257 struct block
*b
, *tmp
;
6258 int port
, real_proto
;
6264 addr
= pcap_nametonetaddr(name
);
6266 bpf_error(cstate
, "unknown network '%s'", name
);
6267 /* Left justify network addr and calculate its network mask */
6269 while (addr
&& (addr
& 0xff000000) == 0) {
6273 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6277 if (proto
== Q_LINK
) {
6278 switch (cstate
->linktype
) {
6281 case DLT_NETANALYZER
:
6282 case DLT_NETANALYZER_TRANSPARENT
:
6283 eaddr
= pcap_ether_hostton(name
);
6286 "unknown ether host '%s'", name
);
6287 tmp
= gen_prevlinkhdr_check(cstate
);
6288 b
= gen_ehostop(cstate
, eaddr
, dir
);
6295 eaddr
= pcap_ether_hostton(name
);
6298 "unknown FDDI host '%s'", name
);
6299 b
= gen_fhostop(cstate
, eaddr
, dir
);
6304 eaddr
= pcap_ether_hostton(name
);
6307 "unknown token ring host '%s'", name
);
6308 b
= gen_thostop(cstate
, eaddr
, dir
);
6312 case DLT_IEEE802_11
:
6313 case DLT_PRISM_HEADER
:
6314 case DLT_IEEE802_11_RADIO_AVS
:
6315 case DLT_IEEE802_11_RADIO
:
6317 eaddr
= pcap_ether_hostton(name
);
6320 "unknown 802.11 host '%s'", name
);
6321 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6325 case DLT_IP_OVER_FC
:
6326 eaddr
= pcap_ether_hostton(name
);
6329 "unknown Fibre Channel host '%s'", name
);
6330 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6335 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6336 } else if (proto
== Q_DECNET
) {
6337 unsigned short dn_addr
;
6339 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6341 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6343 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6348 * I don't think DECNET hosts can be multihomed, so
6349 * there is no need to build up a list of addresses
6351 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6354 alist
= pcap_nametoaddr(name
);
6355 if (alist
== NULL
|| *alist
== NULL
)
6356 bpf_error(cstate
, "unknown host '%s'", name
);
6358 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6359 tproto
== Q_DEFAULT
)
6361 b
= gen_host(cstate
, **alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6363 tmp
= gen_host(cstate
, **alist
++, 0xffffffff,
6364 tproto
, dir
, q
.addr
);
6370 memset(&mask128
, 0xff, sizeof(mask128
));
6371 res0
= res
= pcap_nametoaddrinfo(name
);
6373 bpf_error(cstate
, "unknown host '%s'", name
);
6376 tproto
= tproto6
= proto
;
6377 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6378 tproto
== Q_DEFAULT
) {
6382 for (res
= res0
; res
; res
= res
->ai_next
) {
6383 switch (res
->ai_family
) {
6385 if (tproto
== Q_IPV6
)
6388 sin4
= (struct sockaddr_in
*)
6390 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6391 0xffffffff, tproto
, dir
, q
.addr
);
6394 if (tproto6
== Q_IP
)
6397 sin6
= (struct sockaddr_in6
*)
6399 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6400 &mask128
, tproto6
, dir
, q
.addr
);
6412 bpf_error(cstate
, "unknown host '%s'%s", name
,
6413 (proto
== Q_DEFAULT
)
6415 : " for specified address family");
6422 if (proto
!= Q_DEFAULT
&&
6423 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6424 bpf_error(cstate
, "illegal qualifier of 'port'");
6425 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6426 bpf_error(cstate
, "unknown port '%s'", name
);
6427 if (proto
== Q_UDP
) {
6428 if (real_proto
== IPPROTO_TCP
)
6429 bpf_error(cstate
, "port '%s' is tcp", name
);
6430 else if (real_proto
== IPPROTO_SCTP
)
6431 bpf_error(cstate
, "port '%s' is sctp", name
);
6433 /* override PROTO_UNDEF */
6434 real_proto
= IPPROTO_UDP
;
6436 if (proto
== Q_TCP
) {
6437 if (real_proto
== IPPROTO_UDP
)
6438 bpf_error(cstate
, "port '%s' is udp", name
);
6440 else if (real_proto
== IPPROTO_SCTP
)
6441 bpf_error(cstate
, "port '%s' is sctp", name
);
6443 /* override PROTO_UNDEF */
6444 real_proto
= IPPROTO_TCP
;
6446 if (proto
== Q_SCTP
) {
6447 if (real_proto
== IPPROTO_UDP
)
6448 bpf_error(cstate
, "port '%s' is udp", name
);
6450 else if (real_proto
== IPPROTO_TCP
)
6451 bpf_error(cstate
, "port '%s' is tcp", name
);
6453 /* override PROTO_UNDEF */
6454 real_proto
= IPPROTO_SCTP
;
6457 bpf_error(cstate
, "illegal port number %d < 0", port
);
6459 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6460 b
= gen_port(cstate
, port
, real_proto
, dir
);
6461 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6465 if (proto
!= Q_DEFAULT
&&
6466 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6467 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6468 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6469 bpf_error(cstate
, "unknown port in range '%s'", name
);
6470 if (proto
== Q_UDP
) {
6471 if (real_proto
== IPPROTO_TCP
)
6472 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6473 else if (real_proto
== IPPROTO_SCTP
)
6474 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6476 /* override PROTO_UNDEF */
6477 real_proto
= IPPROTO_UDP
;
6479 if (proto
== Q_TCP
) {
6480 if (real_proto
== IPPROTO_UDP
)
6481 bpf_error(cstate
, "port in range '%s' is udp", name
);
6482 else if (real_proto
== IPPROTO_SCTP
)
6483 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6485 /* override PROTO_UNDEF */
6486 real_proto
= IPPROTO_TCP
;
6488 if (proto
== Q_SCTP
) {
6489 if (real_proto
== IPPROTO_UDP
)
6490 bpf_error(cstate
, "port in range '%s' is udp", name
);
6491 else if (real_proto
== IPPROTO_TCP
)
6492 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6494 /* override PROTO_UNDEF */
6495 real_proto
= IPPROTO_SCTP
;
6498 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6500 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6502 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6504 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6506 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6507 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6512 eaddr
= pcap_ether_hostton(name
);
6514 bpf_error(cstate
, "unknown ether host: %s", name
);
6516 alist
= pcap_nametoaddr(name
);
6517 if (alist
== NULL
|| *alist
== NULL
)
6518 bpf_error(cstate
, "unknown host '%s'", name
);
6519 b
= gen_gateway(cstate
, eaddr
, alist
, proto
, dir
);
6523 bpf_error(cstate
, "'gateway' not supported in this configuration");
6527 real_proto
= lookup_proto(cstate
, name
, proto
);
6528 if (real_proto
>= 0)
6529 return gen_proto(cstate
, real_proto
, proto
, dir
);
6531 bpf_error(cstate
, "unknown protocol: %s", name
);
6534 real_proto
= lookup_proto(cstate
, name
, proto
);
6535 if (real_proto
>= 0)
6536 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6538 bpf_error(cstate
, "unknown protocol: %s", name
);
6549 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6550 unsigned int masklen
, struct qual q
)
6552 register int nlen
, mlen
;
6555 nlen
= __pcap_atoin(s1
, &n
);
6556 /* Promote short ipaddr */
6560 mlen
= __pcap_atoin(s2
, &m
);
6561 /* Promote short ipaddr */
6564 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6567 /* Convert mask len to mask */
6569 bpf_error(cstate
, "mask length must be <= 32");
6572 * X << 32 is not guaranteed by C to be 0; it's
6577 m
= 0xffffffff << (32 - masklen
);
6579 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6586 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6589 bpf_error(cstate
, "Mask syntax for networks only");
6597 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6600 int proto
= q
.proto
;
6606 else if (q
.proto
== Q_DECNET
) {
6607 vlen
= __pcap_atodn(s
, &v
);
6609 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6611 vlen
= __pcap_atoin(s
, &v
);
6618 if (proto
== Q_DECNET
)
6619 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6620 else if (proto
== Q_LINK
) {
6621 bpf_error(cstate
, "illegal link layer address");
6624 if (s
== NULL
&& q
.addr
== Q_NET
) {
6625 /* Promote short net number */
6626 while (v
&& (v
& 0xff000000) == 0) {
6631 /* Promote short ipaddr */
6633 mask
<<= 32 - vlen
;
6635 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6640 proto
= IPPROTO_UDP
;
6641 else if (proto
== Q_TCP
)
6642 proto
= IPPROTO_TCP
;
6643 else if (proto
== Q_SCTP
)
6644 proto
= IPPROTO_SCTP
;
6645 else if (proto
== Q_DEFAULT
)
6646 proto
= PROTO_UNDEF
;
6648 bpf_error(cstate
, "illegal qualifier of 'port'");
6651 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6655 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6656 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6662 proto
= IPPROTO_UDP
;
6663 else if (proto
== Q_TCP
)
6664 proto
= IPPROTO_TCP
;
6665 else if (proto
== Q_SCTP
)
6666 proto
= IPPROTO_SCTP
;
6667 else if (proto
== Q_DEFAULT
)
6668 proto
= PROTO_UNDEF
;
6670 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6673 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6677 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6678 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6683 bpf_error(cstate
, "'gateway' requires a name");
6687 return gen_proto(cstate
, (int)v
, proto
, dir
);
6690 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6705 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6706 unsigned int masklen
, struct qual q
)
6708 struct addrinfo
*res
;
6709 struct in6_addr
*addr
;
6710 struct in6_addr mask
;
6715 bpf_error(cstate
, "no mask %s supported", s2
);
6717 res
= pcap_nametoaddrinfo(s1
);
6719 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6722 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6723 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6725 if (sizeof(mask
) * 8 < masklen
)
6726 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6727 memset(&mask
, 0, sizeof(mask
));
6728 memset(&mask
, 0xff, masklen
/ 8);
6730 mask
.s6_addr
[masklen
/ 8] =
6731 (0xff << (8 - masklen
% 8)) & 0xff;
6734 a
= (u_int32_t
*)addr
;
6735 m
= (u_int32_t
*)&mask
;
6736 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6737 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6738 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6746 bpf_error(cstate
, "Mask syntax for networks only");
6750 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6756 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6764 gen_ecode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
6766 struct block
*b
, *tmp
;
6768 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6769 switch (cstate
->linktype
) {
6771 case DLT_NETANALYZER
:
6772 case DLT_NETANALYZER_TRANSPARENT
:
6773 tmp
= gen_prevlinkhdr_check(cstate
);
6774 b
= gen_ehostop(cstate
, eaddr
, (int)q
.dir
);
6779 return gen_fhostop(cstate
, eaddr
, (int)q
.dir
);
6781 return gen_thostop(cstate
, eaddr
, (int)q
.dir
);
6782 case DLT_IEEE802_11
:
6783 case DLT_PRISM_HEADER
:
6784 case DLT_IEEE802_11_RADIO_AVS
:
6785 case DLT_IEEE802_11_RADIO
:
6787 return gen_wlanhostop(cstate
, eaddr
, (int)q
.dir
);
6788 case DLT_IP_OVER_FC
:
6789 return gen_ipfchostop(cstate
, eaddr
, (int)q
.dir
);
6791 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6795 bpf_error(cstate
, "ethernet address used in non-ether expression");
6802 struct slist
*s0
, *s1
;
6805 * This is definitely not the best way to do this, but the
6806 * lists will rarely get long.
6813 static struct slist
*
6814 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
6818 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
6823 static struct slist
*
6824 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
6828 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6834 * Modify "index" to use the value stored into its register as an
6835 * offset relative to the beginning of the header for the protocol
6836 * "proto", and allocate a register and put an item "size" bytes long
6837 * (1, 2, or 4) at that offset into that register, making it the register
6841 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
6843 struct slist
*s
, *tmp
;
6845 int regno
= alloc_reg(cstate
);
6847 free_reg(cstate
, inst
->regno
);
6851 bpf_error(cstate
, "data size must be 1, 2, or 4");
6867 bpf_error(cstate
, "unsupported index operation");
6871 * The offset is relative to the beginning of the packet
6872 * data, if we have a radio header. (If we don't, this
6875 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6876 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
6877 cstate
->linktype
!= DLT_PRISM_HEADER
)
6878 bpf_error(cstate
, "radio information not present in capture");
6881 * Load into the X register the offset computed into the
6882 * register specified by "index".
6884 s
= xfer_to_x(cstate
, inst
);
6887 * Load the item at that offset.
6889 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6891 sappend(inst
->s
, s
);
6896 * The offset is relative to the beginning of
6897 * the link-layer header.
6899 * XXX - what about ATM LANE? Should the index be
6900 * relative to the beginning of the AAL5 frame, so
6901 * that 0 refers to the beginning of the LE Control
6902 * field, or relative to the beginning of the LAN
6903 * frame, so that 0 refers, for Ethernet LANE, to
6904 * the beginning of the destination address?
6906 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
6909 * If "s" is non-null, it has code to arrange that the
6910 * X register contains the length of the prefix preceding
6911 * the link-layer header. Add to it the offset computed
6912 * into the register specified by "index", and move that
6913 * into the X register. Otherwise, just load into the X
6914 * register the offset computed into the register specified
6918 sappend(s
, xfer_to_a(cstate
, inst
));
6919 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6920 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6922 s
= xfer_to_x(cstate
, inst
);
6925 * Load the item at the sum of the offset we've put in the
6926 * X register and the offset of the start of the link
6927 * layer header (which is 0 if the radio header is
6928 * variable-length; that header length is what we put
6929 * into the X register and then added to the index).
6931 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6932 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
6934 sappend(inst
->s
, s
);
6948 * The offset is relative to the beginning of
6949 * the network-layer header.
6950 * XXX - are there any cases where we want
6951 * cstate->off_nl_nosnap?
6953 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
6956 * If "s" is non-null, it has code to arrange that the
6957 * X register contains the variable part of the offset
6958 * of the link-layer payload. Add to it the offset
6959 * computed into the register specified by "index",
6960 * and move that into the X register. Otherwise, just
6961 * load into the X register the offset computed into
6962 * the register specified by "index".
6965 sappend(s
, xfer_to_a(cstate
, inst
));
6966 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6967 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6969 s
= xfer_to_x(cstate
, inst
);
6972 * Load the item at the sum of the offset we've put in the
6973 * X register, the offset of the start of the network
6974 * layer header from the beginning of the link-layer
6975 * payload, and the constant part of the offset of the
6976 * start of the link-layer payload.
6978 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6979 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6981 sappend(inst
->s
, s
);
6984 * Do the computation only if the packet contains
6985 * the protocol in question.
6987 b
= gen_proto_abbrev(cstate
, proto
);
6989 gen_and(inst
->b
, b
);
7003 * The offset is relative to the beginning of
7004 * the transport-layer header.
7006 * Load the X register with the length of the IPv4 header
7007 * (plus the offset of the link-layer header, if it's
7008 * a variable-length header), in bytes.
7010 * XXX - are there any cases where we want
7011 * cstate->off_nl_nosnap?
7012 * XXX - we should, if we're built with
7013 * IPv6 support, generate code to load either
7014 * IPv4, IPv6, or both, as appropriate.
7016 s
= gen_loadx_iphdrlen(cstate
);
7019 * The X register now contains the sum of the variable
7020 * part of the offset of the link-layer payload and the
7021 * length of the network-layer header.
7023 * Load into the A register the offset relative to
7024 * the beginning of the transport layer header,
7025 * add the X register to that, move that to the
7026 * X register, and load with an offset from the
7027 * X register equal to the sum of the constant part of
7028 * the offset of the link-layer payload and the offset,
7029 * relative to the beginning of the link-layer payload,
7030 * of the network-layer header.
7032 sappend(s
, xfer_to_a(cstate
, inst
));
7033 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7034 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7035 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7036 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7037 sappend(inst
->s
, s
);
7040 * Do the computation only if the packet contains
7041 * the protocol in question - which is true only
7042 * if this is an IP datagram and is the first or
7043 * only fragment of that datagram.
7045 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7047 gen_and(inst
->b
, b
);
7048 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7052 bpf_error(cstate
, "IPv6 upper-layer protocol is not supported by proto[x]");
7055 inst
->regno
= regno
;
7056 s
= new_stmt(cstate
, BPF_ST
);
7058 sappend(inst
->s
, s
);
7064 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7065 struct arth
*a1
, int reversed
)
7067 struct slist
*s0
, *s1
, *s2
;
7068 struct block
*b
, *tmp
;
7070 s0
= xfer_to_x(cstate
, a1
);
7071 s1
= xfer_to_a(cstate
, a0
);
7072 if (code
== BPF_JEQ
) {
7073 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7074 b
= new_block(cstate
, JMP(code
));
7078 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7084 sappend(a0
->s
, a1
->s
);
7088 free_reg(cstate
, a0
->regno
);
7089 free_reg(cstate
, a1
->regno
);
7091 /* 'and' together protocol checks */
7094 gen_and(a0
->b
, tmp
= a1
->b
);
7108 gen_loadlen(compiler_state_t
*cstate
)
7110 int regno
= alloc_reg(cstate
);
7111 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7114 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7115 s
->next
= new_stmt(cstate
, BPF_ST
);
7116 s
->next
->s
.k
= regno
;
7124 gen_loadi(compiler_state_t
*cstate
, int val
)
7130 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7132 reg
= alloc_reg(cstate
);
7134 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7136 s
->next
= new_stmt(cstate
, BPF_ST
);
7145 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7149 s
= xfer_to_a(cstate
, a
);
7151 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7154 s
= new_stmt(cstate
, BPF_ST
);
7162 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7165 struct slist
*s0
, *s1
, *s2
;
7168 * Disallow division by, or modulus by, zero; we do this here
7169 * so that it gets done even if the optimizer is disabled.
7171 if (code
== BPF_DIV
) {
7172 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7173 bpf_error(cstate
, "division by zero");
7174 } else if (code
== BPF_MOD
) {
7175 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7176 bpf_error(cstate
, "modulus by zero");
7178 s0
= xfer_to_x(cstate
, a1
);
7179 s1
= xfer_to_a(cstate
, a0
);
7180 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7185 sappend(a0
->s
, a1
->s
);
7187 free_reg(cstate
, a0
->regno
);
7188 free_reg(cstate
, a1
->regno
);
7190 s0
= new_stmt(cstate
, BPF_ST
);
7191 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7198 * Initialize the table of used registers and the current register.
7201 init_regs(compiler_state_t
*cstate
)
7204 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7208 * Return the next free register.
7211 alloc_reg(compiler_state_t
*cstate
)
7213 int n
= BPF_MEMWORDS
;
7216 if (cstate
->regused
[cstate
->curreg
])
7217 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7219 cstate
->regused
[cstate
->curreg
] = 1;
7220 return cstate
->curreg
;
7223 bpf_error(cstate
, "too many registers needed to evaluate expression");
7229 * Return a register to the table so it can
7233 free_reg(compiler_state_t
*cstate
, int n
)
7235 cstate
->regused
[n
] = 0;
7238 static struct block
*
7239 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7244 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7245 b
= new_block(cstate
, JMP(jmp
));
7253 gen_greater(compiler_state_t
*cstate
, int n
)
7255 return gen_len(cstate
, BPF_JGE
, n
);
7259 * Actually, this is less than or equal.
7262 gen_less(compiler_state_t
*cstate
, int n
)
7266 b
= gen_len(cstate
, BPF_JGT
, n
);
7273 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7274 * the beginning of the link-layer header.
7275 * XXX - that means you can't test values in the radiotap header, but
7276 * as that header is difficult if not impossible to parse generally
7277 * without a loop, that might not be a severe problem. A new keyword
7278 * "radio" could be added for that, although what you'd really want
7279 * would be a way of testing particular radio header values, which
7280 * would generate code appropriate to the radio header in question.
7283 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7293 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7296 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7300 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7304 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7308 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7312 b
= new_block(cstate
, JMP(BPF_JEQ
));
7319 static const u_char abroadcast
[] = { 0x0 };
7322 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7324 bpf_u_int32 hostmask
;
7325 struct block
*b0
, *b1
, *b2
;
7326 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7332 switch (cstate
->linktype
) {
7334 case DLT_ARCNET_LINUX
:
7335 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7337 case DLT_NETANALYZER
:
7338 case DLT_NETANALYZER_TRANSPARENT
:
7339 b1
= gen_prevlinkhdr_check(cstate
);
7340 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7345 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7347 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7348 case DLT_IEEE802_11
:
7349 case DLT_PRISM_HEADER
:
7350 case DLT_IEEE802_11_RADIO_AVS
:
7351 case DLT_IEEE802_11_RADIO
:
7353 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7354 case DLT_IP_OVER_FC
:
7355 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7357 bpf_error(cstate
, "not a broadcast link");
7363 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7364 * as an indication that we don't know the netmask, and fail
7367 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7368 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7369 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7370 hostmask
= ~cstate
->netmask
;
7371 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7372 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7373 (bpf_int32
)(~0 & hostmask
), hostmask
);
7378 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7384 * Generate code to test the low-order bit of a MAC address (that's
7385 * the bottom bit of the *first* byte).
7387 static struct block
*
7388 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7390 register struct block
*b0
;
7391 register struct slist
*s
;
7393 /* link[offset] & 1 != 0 */
7394 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7395 b0
= new_block(cstate
, JMP(BPF_JSET
));
7402 gen_multicast(compiler_state_t
*cstate
, int proto
)
7404 register struct block
*b0
, *b1
, *b2
;
7405 register struct slist
*s
;
7411 switch (cstate
->linktype
) {
7413 case DLT_ARCNET_LINUX
:
7414 /* all ARCnet multicasts use the same address */
7415 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7417 case DLT_NETANALYZER
:
7418 case DLT_NETANALYZER_TRANSPARENT
:
7419 b1
= gen_prevlinkhdr_check(cstate
);
7420 /* ether[0] & 1 != 0 */
7421 b0
= gen_mac_multicast(cstate
, 0);
7427 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7429 * XXX - was that referring to bit-order issues?
7431 /* fddi[1] & 1 != 0 */
7432 return gen_mac_multicast(cstate
, 1);
7434 /* tr[2] & 1 != 0 */
7435 return gen_mac_multicast(cstate
, 2);
7436 case DLT_IEEE802_11
:
7437 case DLT_PRISM_HEADER
:
7438 case DLT_IEEE802_11_RADIO_AVS
:
7439 case DLT_IEEE802_11_RADIO
:
7444 * For control frames, there is no DA.
7446 * For management frames, DA is at an
7447 * offset of 4 from the beginning of
7450 * For data frames, DA is at an offset
7451 * of 4 from the beginning of the packet
7452 * if To DS is clear and at an offset of
7453 * 16 from the beginning of the packet
7458 * Generate the tests to be done for data frames.
7460 * First, check for To DS set, i.e. "link[1] & 0x01".
7462 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7463 b1
= new_block(cstate
, JMP(BPF_JSET
));
7464 b1
->s
.k
= 0x01; /* To DS */
7468 * If To DS is set, the DA is at 16.
7470 b0
= gen_mac_multicast(cstate
, 16);
7474 * Now, check for To DS not set, i.e. check
7475 * "!(link[1] & 0x01)".
7477 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7478 b2
= new_block(cstate
, JMP(BPF_JSET
));
7479 b2
->s
.k
= 0x01; /* To DS */
7484 * If To DS is not set, the DA is at 4.
7486 b1
= gen_mac_multicast(cstate
, 4);
7490 * Now OR together the last two checks. That gives
7491 * the complete set of checks for data frames.
7496 * Now check for a data frame.
7497 * I.e, check "link[0] & 0x08".
7499 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7500 b1
= new_block(cstate
, JMP(BPF_JSET
));
7505 * AND that with the checks done for data frames.
7510 * If the high-order bit of the type value is 0, this
7511 * is a management frame.
7512 * I.e, check "!(link[0] & 0x08)".
7514 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7515 b2
= new_block(cstate
, JMP(BPF_JSET
));
7521 * For management frames, the DA is at 4.
7523 b1
= gen_mac_multicast(cstate
, 4);
7527 * OR that with the checks done for data frames.
7528 * That gives the checks done for management and
7534 * If the low-order bit of the type value is 1,
7535 * this is either a control frame or a frame
7536 * with a reserved type, and thus not a
7539 * I.e., check "!(link[0] & 0x04)".
7541 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7542 b1
= new_block(cstate
, JMP(BPF_JSET
));
7548 * AND that with the checks for data and management
7553 case DLT_IP_OVER_FC
:
7554 b0
= gen_mac_multicast(cstate
, 2);
7559 /* Link not known to support multicasts */
7563 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7564 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7569 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7570 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7574 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7580 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7581 * Outbound traffic is sent by this machine, while inbound traffic is
7582 * sent by a remote machine (and may include packets destined for a
7583 * unicast or multicast link-layer address we are not subscribing to).
7584 * These are the same definitions implemented by pcap_setdirection().
7585 * Capturing only unicast traffic destined for this host is probably
7586 * better accomplished using a higher-layer filter.
7589 gen_inbound(compiler_state_t
*cstate
, int dir
)
7591 register struct block
*b0
;
7594 * Only some data link types support inbound/outbound qualifiers.
7596 switch (cstate
->linktype
) {
7598 b0
= gen_relation(cstate
, BPF_JEQ
,
7599 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7600 gen_loadi(cstate
, 0),
7606 /* match outgoing packets */
7607 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7609 /* match incoming packets */
7610 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7615 /* match outgoing packets */
7616 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7618 /* to filter on inbound traffic, invert the match */
7623 #ifdef HAVE_NET_PFVAR_H
7625 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7626 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7632 /* match outgoing packets */
7633 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7635 /* match incoming packets */
7636 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7640 case DLT_JUNIPER_MFR
:
7641 case DLT_JUNIPER_MLFR
:
7642 case DLT_JUNIPER_MLPPP
:
7643 case DLT_JUNIPER_ATM1
:
7644 case DLT_JUNIPER_ATM2
:
7645 case DLT_JUNIPER_PPPOE
:
7646 case DLT_JUNIPER_PPPOE_ATM
:
7647 case DLT_JUNIPER_GGSN
:
7648 case DLT_JUNIPER_ES
:
7649 case DLT_JUNIPER_MONITOR
:
7650 case DLT_JUNIPER_SERVICES
:
7651 case DLT_JUNIPER_ETHER
:
7652 case DLT_JUNIPER_PPP
:
7653 case DLT_JUNIPER_FRELAY
:
7654 case DLT_JUNIPER_CHDLC
:
7655 case DLT_JUNIPER_VP
:
7656 case DLT_JUNIPER_ST
:
7657 case DLT_JUNIPER_ISM
:
7658 case DLT_JUNIPER_VS
:
7659 case DLT_JUNIPER_SRX_E2E
:
7660 case DLT_JUNIPER_FIBRECHANNEL
:
7661 case DLT_JUNIPER_ATM_CEMIC
:
7663 /* juniper flags (including direction) are stored
7664 * the byte after the 3-byte magic number */
7666 /* match outgoing packets */
7667 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7669 /* match incoming packets */
7670 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7676 * If we have packet meta-data indicating a direction,
7677 * check it, otherwise give up as this link-layer type
7678 * has nothing in the packet data.
7680 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7682 * This is Linux with PF_PACKET support.
7683 * If this is a *live* capture, we can look at
7684 * special meta-data in the filter expression;
7685 * if it's a savefile, we can't.
7687 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7688 /* We have a FILE *, so this is a savefile */
7689 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
7694 /* match outgoing packets */
7695 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7698 /* to filter on inbound traffic, invert the match */
7701 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7702 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
7706 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7711 #ifdef HAVE_NET_PFVAR_H
7712 /* PF firewall log matched interface */
7714 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7719 if (cstate
->linktype
!= DLT_PFLOG
) {
7720 bpf_error(cstate
, "ifname supported only on PF linktype");
7723 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7724 off
= offsetof(struct pfloghdr
, ifname
);
7725 if (strlen(ifname
) >= len
) {
7726 bpf_error(cstate
, "ifname interface names can only be %d characters",
7730 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7734 /* PF firewall log ruleset name */
7736 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7740 if (cstate
->linktype
!= DLT_PFLOG
) {
7741 bpf_error(cstate
, "ruleset supported only on PF linktype");
7745 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7746 bpf_error(cstate
, "ruleset names can only be %ld characters",
7747 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7751 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7752 strlen(ruleset
), (const u_char
*)ruleset
);
7756 /* PF firewall log rule number */
7758 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7762 if (cstate
->linktype
!= DLT_PFLOG
) {
7763 bpf_error(cstate
, "rnr supported only on PF linktype");
7767 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7772 /* PF firewall log sub-rule number */
7774 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7778 if (cstate
->linktype
!= DLT_PFLOG
) {
7779 bpf_error(cstate
, "srnr supported only on PF linktype");
7783 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7788 /* PF firewall log reason code */
7790 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7794 if (cstate
->linktype
!= DLT_PFLOG
) {
7795 bpf_error(cstate
, "reason supported only on PF linktype");
7799 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7804 /* PF firewall log action */
7806 gen_pf_action(compiler_state_t
*cstate
, int action
)
7810 if (cstate
->linktype
!= DLT_PFLOG
) {
7811 bpf_error(cstate
, "action supported only on PF linktype");
7815 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7819 #else /* !HAVE_NET_PFVAR_H */
7821 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7823 bpf_error(cstate
, "libpcap was compiled without pf support");
7829 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7831 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7837 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7839 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7845 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7847 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7853 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7855 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7861 gen_pf_action(compiler_state_t
*cstate
, int action
)
7863 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7867 #endif /* HAVE_NET_PFVAR_H */
7869 /* IEEE 802.11 wireless header */
7871 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
7875 switch (cstate
->linktype
) {
7877 case DLT_IEEE802_11
:
7878 case DLT_PRISM_HEADER
:
7879 case DLT_IEEE802_11_RADIO_AVS
:
7880 case DLT_IEEE802_11_RADIO
:
7881 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7886 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
7894 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
7898 switch (cstate
->linktype
) {
7900 case DLT_IEEE802_11
:
7901 case DLT_PRISM_HEADER
:
7902 case DLT_IEEE802_11_RADIO_AVS
:
7903 case DLT_IEEE802_11_RADIO
:
7907 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
7911 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7912 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7918 gen_acode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
7920 switch (cstate
->linktype
) {
7923 case DLT_ARCNET_LINUX
:
7924 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7926 return (gen_ahostop(cstate
, eaddr
, (int)q
.dir
));
7928 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7934 bpf_error(cstate
, "aid supported only on ARCnet");
7937 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7942 static struct block
*
7943 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
7945 register struct block
*b0
, *b1
;
7948 /* src comes first, different from Ethernet */
7950 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
7953 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
7956 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
7957 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
7963 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
7964 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
7969 bpf_error(cstate
, "'addr1' is only supported on 802.11");
7973 bpf_error(cstate
, "'addr2' is only supported on 802.11");
7977 bpf_error(cstate
, "'addr3' is only supported on 802.11");
7981 bpf_error(cstate
, "'addr4' is only supported on 802.11");
7985 bpf_error(cstate
, "'ra' is only supported on 802.11");
7989 bpf_error(cstate
, "'ta' is only supported on 802.11");
7996 static struct block
*
7997 gen_vlan_tpid_test(compiler_state_t
*cstate
)
7999 struct block
*b0
, *b1
;
8001 /* check for VLAN, including QinQ */
8002 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8003 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8006 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8012 static struct block
*
8013 gen_vlan_vid_test(compiler_state_t
*cstate
, int vlan_num
)
8015 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8018 static struct block
*
8019 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8021 struct block
*b0
, *b1
;
8023 b0
= gen_vlan_tpid_test(cstate
);
8025 if (vlan_num
>= 0) {
8026 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8032 * Both payload and link header type follow the VLAN tags so that
8033 * both need to be updated.
8035 cstate
->off_linkpl
.constant_part
+= 4;
8036 cstate
->off_linktype
.constant_part
+= 4;
8041 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8042 /* add v to variable part of off */
8044 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8048 if (!off
->is_variable
)
8049 off
->is_variable
= 1;
8051 off
->reg
= alloc_reg(cstate
);
8053 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8056 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8059 s2
= new_stmt(cstate
, BPF_ST
);
8065 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8066 * and link type offsets first
8069 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8073 /* offset determined at run time, shift variable part */
8075 cstate
->is_vlan_vloffset
= 1;
8076 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8077 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8079 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8080 sappend(s
.next
, b_tpid
->head
->stmts
);
8081 b_tpid
->head
->stmts
= s
.next
;
8085 * patch block b_vid (VLAN id test) to load VID value either from packet
8086 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8089 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8091 struct slist
*s
, *s2
, *sjeq
;
8094 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8095 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8097 /* true -> next instructions, false -> beginning of b_vid */
8098 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8100 sjeq
->s
.jf
= b_vid
->stmts
;
8103 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8104 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8108 /* jump to the test in b_vid (bypass loading VID from packet data) */
8110 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8112 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8116 /* insert our statements at the beginning of b_vid */
8117 sappend(s
, b_vid
->stmts
);
8122 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8123 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8124 * tag can be either in metadata or in packet data; therefore if the
8125 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8126 * header for VLAN tag. As the decision is done at run time, we need
8127 * update variable part of the offsets
8129 static struct block
*
8130 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8132 struct block
*b0
, *b_tpid
, *b_vid
;
8135 /* generate new filter code based on extracting packet
8137 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8138 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8140 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8145 * This is tricky. We need to insert the statements updating variable
8146 * parts of offsets before the the traditional TPID and VID tests so
8147 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8148 * we do not want this update to affect those checks. That's why we
8149 * generate both test blocks first and insert the statements updating
8150 * variable parts of both offsets after that. This wouldn't work if
8151 * there already were variable length link header when entering this
8152 * function but gen_vlan_bpf_extensions() isn't called in that case.
8154 b_tpid
= gen_vlan_tpid_test(cstate
);
8156 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8158 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8162 if (vlan_num
>= 0) {
8163 gen_vlan_patch_vid_test(cstate
, b_vid
);
8173 * support IEEE 802.1Q VLAN trunk over ethernet
8176 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
8180 /* can't check for VLAN-encapsulated packets inside MPLS */
8181 if (cstate
->label_stack_depth
> 0)
8182 bpf_error(cstate
, "no VLAN match after MPLS");
8185 * Check for a VLAN packet, and then change the offsets to point
8186 * to the type and data fields within the VLAN packet. Just
8187 * increment the offsets, so that we can support a hierarchy, e.g.
8188 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8191 * XXX - this is a bit of a kludge. If we were to split the
8192 * compiler into a parser that parses an expression and
8193 * generates an expression tree, and a code generator that
8194 * takes an expression tree (which could come from our
8195 * parser or from some other parser) and generates BPF code,
8196 * we could perhaps make the offsets parameters of routines
8197 * and, in the handler for an "AND" node, pass to subnodes
8198 * other than the VLAN node the adjusted offsets.
8200 * This would mean that "vlan" would, instead of changing the
8201 * behavior of *all* tests after it, change only the behavior
8202 * of tests ANDed with it. That would change the documented
8203 * semantics of "vlan", which might break some expressions.
8204 * However, it would mean that "(vlan and ip) or ip" would check
8205 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8206 * checking only for VLAN-encapsulated IP, so that could still
8207 * be considered worth doing; it wouldn't break expressions
8208 * that are of the form "vlan and ..." or "vlan N and ...",
8209 * which I suspect are the most common expressions involving
8210 * "vlan". "vlan or ..." doesn't necessarily do what the user
8211 * would really want, now, as all the "or ..." tests would
8212 * be done assuming a VLAN, even though the "or" could be viewed
8213 * as meaning "or, if this isn't a VLAN packet...".
8215 switch (cstate
->linktype
) {
8218 case DLT_NETANALYZER
:
8219 case DLT_NETANALYZER_TRANSPARENT
:
8220 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8221 /* Verify that this is the outer part of the packet and
8222 * not encapsulated somehow. */
8223 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8224 cstate
->off_linkhdr
.constant_part
==
8225 cstate
->off_outermostlinkhdr
.constant_part
) {
8227 * Do we need special VLAN handling?
8229 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8230 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8232 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8235 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8238 case DLT_IEEE802_11
:
8239 case DLT_PRISM_HEADER
:
8240 case DLT_IEEE802_11_RADIO_AVS
:
8241 case DLT_IEEE802_11_RADIO
:
8242 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8246 bpf_error(cstate
, "no VLAN support for data link type %d",
8251 cstate
->vlan_stack_depth
++;
8260 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8262 struct block
*b0
, *b1
;
8264 if (cstate
->label_stack_depth
> 0) {
8265 /* just match the bottom-of-stack bit clear */
8266 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8269 * We're not in an MPLS stack yet, so check the link-layer
8270 * type against MPLS.
8272 switch (cstate
->linktype
) {
8274 case DLT_C_HDLC
: /* fall through */
8276 case DLT_NETANALYZER
:
8277 case DLT_NETANALYZER_TRANSPARENT
:
8278 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8282 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8285 /* FIXME add other DLT_s ...
8286 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8287 * leave it for now */
8290 bpf_error(cstate
, "no MPLS support for data link type %d",
8298 /* If a specific MPLS label is requested, check it */
8299 if (label_num
>= 0) {
8300 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8301 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8302 0xfffff000); /* only compare the first 20 bits */
8308 * Change the offsets to point to the type and data fields within
8309 * the MPLS packet. Just increment the offsets, so that we
8310 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8311 * capture packets with an outer label of 100000 and an inner
8314 * Increment the MPLS stack depth as well; this indicates that
8315 * we're checking MPLS-encapsulated headers, to make sure higher
8316 * level code generators don't try to match against IP-related
8317 * protocols such as Q_ARP, Q_RARP etc.
8319 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8321 cstate
->off_nl_nosnap
+= 4;
8322 cstate
->off_nl
+= 4;
8323 cstate
->label_stack_depth
++;
8328 * Support PPPOE discovery and session.
8331 gen_pppoed(compiler_state_t
*cstate
)
8333 /* check for PPPoE discovery */
8334 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8338 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8340 struct block
*b0
, *b1
;
8343 * Test against the PPPoE session link-layer type.
8345 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8347 /* If a specific session is requested, check PPPoE session id */
8348 if (sess_num
>= 0) {
8349 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8350 (bpf_int32
)sess_num
, 0x0000ffff);
8356 * Change the offsets to point to the type and data fields within
8357 * the PPP packet, and note that this is PPPoE rather than
8360 * XXX - this is a bit of a kludge. If we were to split the
8361 * compiler into a parser that parses an expression and
8362 * generates an expression tree, and a code generator that
8363 * takes an expression tree (which could come from our
8364 * parser or from some other parser) and generates BPF code,
8365 * we could perhaps make the offsets parameters of routines
8366 * and, in the handler for an "AND" node, pass to subnodes
8367 * other than the PPPoE node the adjusted offsets.
8369 * This would mean that "pppoes" would, instead of changing the
8370 * behavior of *all* tests after it, change only the behavior
8371 * of tests ANDed with it. That would change the documented
8372 * semantics of "pppoes", which might break some expressions.
8373 * However, it would mean that "(pppoes and ip) or ip" would check
8374 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8375 * checking only for VLAN-encapsulated IP, so that could still
8376 * be considered worth doing; it wouldn't break expressions
8377 * that are of the form "pppoes and ..." which I suspect are the
8378 * most common expressions involving "pppoes". "pppoes or ..."
8379 * doesn't necessarily do what the user would really want, now,
8380 * as all the "or ..." tests would be done assuming PPPoE, even
8381 * though the "or" could be viewed as meaning "or, if this isn't
8382 * a PPPoE packet...".
8384 * The "network-layer" protocol is PPPoE, which has a 6-byte
8385 * PPPoE header, followed by a PPP packet.
8387 * There is no HDLC encapsulation for the PPP packet (it's
8388 * encapsulated in PPPoES instead), so the link-layer type
8389 * starts at the first byte of the PPP packet. For PPPoE,
8390 * that offset is relative to the beginning of the total
8391 * link-layer payload, including any 802.2 LLC header, so
8392 * it's 6 bytes past cstate->off_nl.
8394 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8395 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8396 cstate
->off_linkpl
.reg
);
8398 cstate
->off_linktype
= cstate
->off_linkhdr
;
8399 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8402 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8407 /* Check that this is Geneve and the VNI is correct if
8408 * specified. Parameterized to handle both IPv4 and IPv6. */
8409 static struct block
*
8410 gen_geneve_check(compiler_state_t
*cstate
,
8411 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8412 enum e_offrel offrel
, int vni
)
8414 struct block
*b0
, *b1
;
8416 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8418 /* Check that we are operating on version 0. Otherwise, we
8419 * can't decode the rest of the fields. The version is 2 bits
8420 * in the first byte of the Geneve header. */
8421 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8426 vni
<<= 8; /* VNI is in the upper 3 bytes */
8427 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8436 /* The IPv4 and IPv6 Geneve checks need to do two things:
8437 * - Verify that this actually is Geneve with the right VNI.
8438 * - Place the IP header length (plus variable link prefix if
8439 * needed) into register A to be used later to compute
8440 * the inner packet offsets. */
8441 static struct block
*
8442 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8444 struct block
*b0
, *b1
;
8445 struct slist
*s
, *s1
;
8447 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8449 /* Load the IP header length into A. */
8450 s
= gen_loadx_iphdrlen(cstate
);
8452 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8455 /* Forcibly append these statements to the true condition
8456 * of the protocol check by creating a new block that is
8457 * always true and ANDing them. */
8458 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8467 static struct block
*
8468 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8470 struct block
*b0
, *b1
;
8471 struct slist
*s
, *s1
;
8473 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8475 /* Load the IP header length. We need to account for a
8476 * variable length link prefix if there is one. */
8477 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8479 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8483 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8487 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8491 /* Forcibly append these statements to the true condition
8492 * of the protocol check by creating a new block that is
8493 * always true and ANDing them. */
8494 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8497 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8506 /* We need to store three values based on the Geneve header::
8507 * - The offset of the linktype.
8508 * - The offset of the end of the Geneve header.
8509 * - The offset of the end of the encapsulated MAC header. */
8510 static struct slist
*
8511 gen_geneve_offsets(compiler_state_t
*cstate
)
8513 struct slist
*s
, *s1
, *s_proto
;
8515 /* First we need to calculate the offset of the Geneve header
8516 * itself. This is composed of the IP header previously calculated
8517 * (include any variable link prefix) and stored in A plus the
8518 * fixed sized headers (fixed link prefix, MAC length, and UDP
8520 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8521 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8523 /* Stash this in X since we'll need it later. */
8524 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8527 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8529 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8533 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8534 cstate
->off_linktype
.is_variable
= 1;
8535 cstate
->off_linktype
.constant_part
= 0;
8537 s1
= new_stmt(cstate
, BPF_ST
);
8538 s1
->s
.k
= cstate
->off_linktype
.reg
;
8541 /* Load the Geneve option length and mask and shift to get the
8542 * number of bytes. It is stored in the first byte of the Geneve
8544 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8548 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8552 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8556 /* Add in the rest of the Geneve base header. */
8557 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8561 /* Add the Geneve header length to its offset and store. */
8562 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8566 /* Set the encapsulated type as Ethernet. Even though we may
8567 * not actually have Ethernet inside there are two reasons this
8569 * - The linktype field is always in EtherType format regardless
8570 * of whether it is in Geneve or an inner Ethernet frame.
8571 * - The only link layer that we have specific support for is
8572 * Ethernet. We will confirm that the packet actually is
8573 * Ethernet at runtime before executing these checks. */
8574 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8576 s1
= new_stmt(cstate
, BPF_ST
);
8577 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8580 /* Calculate whether we have an Ethernet header or just raw IP/
8581 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8582 * and linktype by 14 bytes so that the network header can be found
8583 * seamlessly. Otherwise, keep what we've calculated already. */
8585 /* We have a bare jmp so we can't use the optimizer. */
8586 cstate
->no_optimize
= 1;
8588 /* Load the EtherType in the Geneve header, 2 bytes in. */
8589 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8593 /* Load X with the end of the Geneve header. */
8594 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8595 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8598 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8599 * end of this check, we should have the total length in X. In
8600 * the non-Ethernet case, it's already there. */
8601 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8602 s_proto
->s
.k
= ETHERTYPE_TEB
;
8603 sappend(s
, s_proto
);
8605 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8609 /* Since this is Ethernet, use the EtherType of the payload
8610 * directly as the linktype. Overwrite what we already have. */
8611 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8615 s1
= new_stmt(cstate
, BPF_ST
);
8616 s1
->s
.k
= cstate
->off_linktype
.reg
;
8619 /* Advance two bytes further to get the end of the Ethernet
8621 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8625 /* Move the result to X. */
8626 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8629 /* Store the final result of our linkpl calculation. */
8630 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8631 cstate
->off_linkpl
.is_variable
= 1;
8632 cstate
->off_linkpl
.constant_part
= 0;
8634 s1
= new_stmt(cstate
, BPF_STX
);
8635 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8644 /* Check to see if this is a Geneve packet. */
8646 gen_geneve(compiler_state_t
*cstate
, int vni
)
8648 struct block
*b0
, *b1
;
8651 b0
= gen_geneve4(cstate
, vni
);
8652 b1
= gen_geneve6(cstate
, vni
);
8657 /* Later filters should act on the payload of the Geneve frame,
8658 * update all of the header pointers. Attach this code so that
8659 * it gets executed in the event that the Geneve filter matches. */
8660 s
= gen_geneve_offsets(cstate
);
8662 b1
= gen_true(cstate
);
8663 sappend(s
, b1
->stmts
);
8668 cstate
->is_geneve
= 1;
8673 /* Check that the encapsulated frame has a link layer header
8674 * for Ethernet filters. */
8675 static struct block
*
8676 gen_geneve_ll_check(compiler_state_t
*cstate
)
8679 struct slist
*s
, *s1
;
8681 /* The easiest way to see if there is a link layer present
8682 * is to check if the link layer header and payload are not
8685 /* Geneve always generates pure variable offsets so we can
8686 * compare only the registers. */
8687 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8688 s
->s
.k
= cstate
->off_linkhdr
.reg
;
8690 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8691 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8694 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8703 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
8704 bpf_u_int32 jtype
, int reverse
)
8711 if (!cstate
->is_atm
)
8712 bpf_error(cstate
, "'vpi' supported only on raw ATM");
8713 if (cstate
->off_vpi
== (u_int
)-1)
8715 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
8720 if (!cstate
->is_atm
)
8721 bpf_error(cstate
, "'vci' supported only on raw ATM");
8722 if (cstate
->off_vci
== (u_int
)-1)
8724 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
8729 if (cstate
->off_proto
== (u_int
)-1)
8730 abort(); /* XXX - this isn't on FreeBSD */
8731 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
8736 if (cstate
->off_payload
== (u_int
)-1)
8738 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
8739 0xffffffff, jtype
, reverse
, jvalue
);
8743 if (!cstate
->is_atm
)
8744 bpf_error(cstate
, "'callref' supported only on raw ATM");
8745 if (cstate
->off_proto
== (u_int
)-1)
8747 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
8748 jtype
, reverse
, jvalue
);
8758 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
8760 struct block
*b0
, *b1
;
8765 /* Get all packets in Meta signalling Circuit */
8766 if (!cstate
->is_atm
)
8767 bpf_error(cstate
, "'metac' supported only on raw ATM");
8768 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8769 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
8774 /* Get all packets in Broadcast Circuit*/
8775 if (!cstate
->is_atm
)
8776 bpf_error(cstate
, "'bcc' supported only on raw ATM");
8777 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8778 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
8783 /* Get all cells in Segment OAM F4 circuit*/
8784 if (!cstate
->is_atm
)
8785 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
8786 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8787 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8792 /* Get all cells in End-to-End OAM F4 Circuit*/
8793 if (!cstate
->is_atm
)
8794 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
8795 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8796 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8801 /* Get all packets in connection Signalling Circuit */
8802 if (!cstate
->is_atm
)
8803 bpf_error(cstate
, "'sc' supported only on raw ATM");
8804 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8805 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
8810 /* Get all packets in ILMI Circuit */
8811 if (!cstate
->is_atm
)
8812 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
8813 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8814 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
8819 /* Get all LANE packets */
8820 if (!cstate
->is_atm
)
8821 bpf_error(cstate
, "'lane' supported only on raw ATM");
8822 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8825 * Arrange that all subsequent tests assume LANE
8826 * rather than LLC-encapsulated packets, and set
8827 * the offsets appropriately for LANE-encapsulated
8830 * We assume LANE means Ethernet, not Token Ring.
8832 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
8833 cstate
->off_payload
+ 2, /* Ethernet header */
8835 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
8836 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
8837 cstate
->off_nl
= 0; /* Ethernet II */
8838 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
8842 /* Get all LLC-encapsulated packets */
8843 if (!cstate
->is_atm
)
8844 bpf_error(cstate
, "'llc' supported only on raw ATM");
8845 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8846 cstate
->linktype
= cstate
->prevlinktype
;
8856 * Filtering for MTP2 messages based on li value
8857 * FISU, length is null
8858 * LSSU, length is 1 or 2
8859 * MSU, length is 3 or more
8860 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8863 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
8865 struct block
*b0
, *b1
;
8870 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8871 (cstate
->linktype
!= DLT_ERF
) &&
8872 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8873 bpf_error(cstate
, "'fisu' supported only on MTP2");
8874 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8875 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8879 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8880 (cstate
->linktype
!= DLT_ERF
) &&
8881 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8882 bpf_error(cstate
, "'lssu' supported only on MTP2");
8883 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8884 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8889 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8890 (cstate
->linktype
!= DLT_ERF
) &&
8891 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8892 bpf_error(cstate
, "'msu' supported only on MTP2");
8893 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8897 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8898 (cstate
->linktype
!= DLT_ERF
) &&
8899 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8900 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
8901 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8902 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8906 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8907 (cstate
->linktype
!= DLT_ERF
) &&
8908 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8909 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
8910 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8911 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8916 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8917 (cstate
->linktype
!= DLT_ERF
) &&
8918 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8919 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
8920 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8930 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
8931 bpf_u_int32 jtype
, int reverse
)
8934 bpf_u_int32 val1
, val2
, val3
;
8935 u_int newoff_sio
= cstate
->off_sio
;
8936 u_int newoff_opc
= cstate
->off_opc
;
8937 u_int newoff_dpc
= cstate
->off_dpc
;
8938 u_int newoff_sls
= cstate
->off_sls
;
8940 switch (mtp3field
) {
8943 newoff_sio
+= 3; /* offset for MTP2_HSL */
8947 if (cstate
->off_sio
== (u_int
)-1)
8948 bpf_error(cstate
, "'sio' supported only on SS7");
8949 /* sio coded on 1 byte so max value 255 */
8951 bpf_error(cstate
, "sio value %u too big; max value = 255",
8953 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8954 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8960 if (cstate
->off_opc
== (u_int
)-1)
8961 bpf_error(cstate
, "'opc' supported only on SS7");
8962 /* opc coded on 14 bits so max value 16383 */
8964 bpf_error(cstate
, "opc value %u too big; max value = 16383",
8966 /* the following instructions are made to convert jvalue
8967 * to the form used to write opc in an ss7 message*/
8968 val1
= jvalue
& 0x00003c00;
8970 val2
= jvalue
& 0x000003fc;
8972 val3
= jvalue
& 0x00000003;
8974 jvalue
= val1
+ val2
+ val3
;
8975 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8976 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8984 if (cstate
->off_dpc
== (u_int
)-1)
8985 bpf_error(cstate
, "'dpc' supported only on SS7");
8986 /* dpc coded on 14 bits so max value 16383 */
8988 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
8990 /* the following instructions are made to convert jvalue
8991 * to the forme used to write dpc in an ss7 message*/
8992 val1
= jvalue
& 0x000000ff;
8994 val2
= jvalue
& 0x00003f00;
8996 jvalue
= val1
+ val2
;
8997 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8998 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9004 if (cstate
->off_sls
== (u_int
)-1)
9005 bpf_error(cstate
, "'sls' supported only on SS7");
9006 /* sls coded on 4 bits so max value 15 */
9008 bpf_error(cstate
, "sls value %u too big; max value = 15",
9010 /* the following instruction is made to convert jvalue
9011 * to the forme used to write sls in an ss7 message*/
9012 jvalue
= jvalue
<< 4;
9013 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9014 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9023 static struct block
*
9024 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9029 * Q.2931 signalling protocol messages for handling virtual circuits
9030 * establishment and teardown
9035 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9039 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9043 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9047 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9051 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9054 case A_RELEASE_DONE
:
9055 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9065 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9067 struct block
*b0
, *b1
;
9072 if (!cstate
->is_atm
)
9073 bpf_error(cstate
, "'oam' supported only on raw ATM");
9074 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
9078 if (!cstate
->is_atm
)
9079 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9081 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9082 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9084 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9090 * Get Q.2931 signalling messages for switched
9091 * virtual connection
9093 if (!cstate
->is_atm
)
9094 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9095 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9096 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9098 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9100 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9102 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9104 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9106 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9111 if (!cstate
->is_atm
)
9112 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9113 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9114 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9116 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9118 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9120 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9122 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);