1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 #include <pcap-stdinc.h>
35 #ifdef HAVE_SYS_BITYPES_H
36 #include <sys/bitypes.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
45 #include <sys/param.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
65 #include "ethertype.h"
69 #include "ieee80211.h"
71 #include "sunatmpos.h"
74 #include "pcap/ipnet.h"
80 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
81 #include <linux/types.h>
82 #include <linux/if_packet.h>
83 #include <linux/filter.h>
86 #ifdef HAVE_NET_PFVAR_H
87 #include <sys/socket.h>
89 #include <net/pfvar.h>
90 #include <net/if_pflog.h>
94 #define offsetof(s, e) ((size_t)&((s *)0)->e)
99 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
105 u_int8_t u6_addr8
[16];
106 u_int16_t u6_addr16
[8];
107 u_int32_t u6_addr32
[4];
109 #define s6_addr in6_u.u6_addr8
110 #define s6_addr16 in6_u.u6_addr16
111 #define s6_addr32 in6_u.u6_addr32
112 #define s6_addr64 in6_u.u6_addr64
115 typedef unsigned short sa_family_t
;
117 #define __SOCKADDR_COMMON(sa_prefix) \
118 sa_family_t sa_prefix##family
120 /* Ditto, for IPv6. */
123 __SOCKADDR_COMMON (sin6_
);
124 u_int16_t sin6_port
; /* Transport layer port # */
125 u_int32_t sin6_flowinfo
; /* IPv6 flow information */
126 struct in6_addr sin6_addr
; /* IPv6 address */
129 #ifndef EAI_ADDRFAMILY
131 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
132 int ai_family
; /* PF_xxx */
133 int ai_socktype
; /* SOCK_xxx */
134 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
135 size_t ai_addrlen
; /* length of ai_addr */
136 char *ai_canonname
; /* canonical name for hostname */
137 struct sockaddr
*ai_addr
; /* binary address */
138 struct addrinfo
*ai_next
; /* next structure in linked list */
140 #endif /* EAI_ADDRFAMILY */
141 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
143 #include <netdb.h> /* for "struct addrinfo" */
146 #include <pcap/namedb.h>
148 #include "nametoaddr.h"
150 #define ETHERMTU 1500
152 #ifndef ETHERTYPE_TEB
153 #define ETHERTYPE_TEB 0x6558
156 #ifndef IPPROTO_HOPOPTS
157 #define IPPROTO_HOPOPTS 0
159 #ifndef IPPROTO_ROUTING
160 #define IPPROTO_ROUTING 43
162 #ifndef IPPROTO_FRAGMENT
163 #define IPPROTO_FRAGMENT 44
165 #ifndef IPPROTO_DSTOPTS
166 #define IPPROTO_DSTOPTS 60
169 #define IPPROTO_SCTP 132
172 #define GENEVE_PORT 6081
174 #ifdef HAVE_OS_PROTO_H
175 #include "os-proto.h"
178 #define JMP(c) ((c)|BPF_JMP|BPF_K)
181 * "Push" the current value of the link-layer header type and link-layer
182 * header offset onto a "stack", and set a new value. (It's not a
183 * full-blown stack; we keep only the top two items.)
185 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
187 (cs)->prevlinktype = (cs)->linktype; \
188 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
189 (cs)->linktype = (new_linktype); \
190 (cs)->off_linkhdr.is_variable = (new_is_variable); \
191 (cs)->off_linkhdr.constant_part = (new_constant_part); \
192 (cs)->off_linkhdr.reg = (new_reg); \
193 (cs)->is_geneve = 0; \
197 * Absolute offsets, which are offsets from the beginning of the raw
198 * packet data, are, in the general case, the sum of a variable value
199 * and a constant value; the variable value may be absent, in which
200 * case the offset is only the constant value, and the constant value
201 * may be zero, in which case the offset is only the variable value.
203 * bpf_abs_offset is a structure containing all that information:
205 * is_variable is 1 if there's a variable part.
207 * constant_part is the constant part of the value, possibly zero;
209 * if is_variable is 1, reg is the register number for a register
210 * containing the variable value if the register has been assigned,
220 * Value passed to gen_load_a() to indicate what the offset argument
221 * is relative to the beginning of.
224 OR_PACKET
, /* full packet data */
225 OR_LINKHDR
, /* link-layer header */
226 OR_PREVLINKHDR
, /* previous link-layer header */
227 OR_LLC
, /* 802.2 LLC header */
228 OR_PREVMPLSHDR
, /* previous MPLS header */
229 OR_LINKTYPE
, /* link-layer type */
230 OR_LINKPL
, /* link-layer payload */
231 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
232 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
233 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
237 * We divy out chunks of memory rather than call malloc each time so
238 * we don't have to worry about leaking memory. It's probably
239 * not a big deal if all this memory was wasted but if this ever
240 * goes into a library that would probably not be a good idea.
242 * XXX - this *is* in a library....
245 #define CHUNK0SIZE 1024
251 /* Code generator state */
253 struct _compiler_state
{
263 int outermostlinktype
;
268 /* Hack for handling VLAN and MPLS stacks. */
269 u_int label_stack_depth
;
270 u_int vlan_stack_depth
;
277 * As errors are handled by a longjmp, anything allocated must
278 * be freed in the longjmp handler, so it must be reachable
281 * One thing that's allocated is the result of pcap_nametoaddrinfo();
282 * it must be freed with freeaddrinfo(). This variable points to
283 * any addrinfo structure that would need to be freed.
289 * Various code constructs need to know the layout of the packet.
290 * These values give the necessary offsets from the beginning
291 * of the packet data.
295 * Absolute offset of the beginning of the link-layer header.
297 bpf_abs_offset off_linkhdr
;
300 * If we're checking a link-layer header for a packet encapsulated
301 * in another protocol layer, this is the equivalent information
302 * for the previous layers' link-layer header from the beginning
303 * of the raw packet data.
305 bpf_abs_offset off_prevlinkhdr
;
308 * This is the equivalent information for the outermost layers'
311 bpf_abs_offset off_outermostlinkhdr
;
314 * Absolute offset of the beginning of the link-layer payload.
316 bpf_abs_offset off_linkpl
;
319 * "off_linktype" is the offset to information in the link-layer
320 * header giving the packet type. This is an absolute offset
321 * from the beginning of the packet.
323 * For Ethernet, it's the offset of the Ethernet type field; this
324 * means that it must have a value that skips VLAN tags.
326 * For link-layer types that always use 802.2 headers, it's the
327 * offset of the LLC header; this means that it must have a value
328 * that skips VLAN tags.
330 * For PPP, it's the offset of the PPP type field.
332 * For Cisco HDLC, it's the offset of the CHDLC type field.
334 * For BSD loopback, it's the offset of the AF_ value.
336 * For Linux cooked sockets, it's the offset of the type field.
338 * off_linktype.constant_part is set to -1 for no encapsulation,
339 * in which case, IP is assumed.
341 bpf_abs_offset off_linktype
;
344 * TRUE if the link layer includes an ATM pseudo-header.
349 * TRUE if "geneve" appeared in the filter; it causes us to
350 * generate code that checks for a Geneve header and assume
351 * that later filters apply to the encapsulated payload.
356 * These are offsets for the ATM pseudo-header.
363 * These are offsets for the MTP2 fields.
369 * These are offsets for the MTP3 fields.
377 * This is the offset of the first byte after the ATM pseudo_header,
378 * or -1 if there is no ATM pseudo-header.
383 * These are offsets to the beginning of the network-layer header.
384 * They are relative to the beginning of the link-layer payload
385 * (i.e., they don't include off_linkhdr.constant_part or
386 * off_linkpl.constant_part).
388 * If the link layer never uses 802.2 LLC:
390 * "off_nl" and "off_nl_nosnap" are the same.
392 * If the link layer always uses 802.2 LLC:
394 * "off_nl" is the offset if there's a SNAP header following
397 * "off_nl_nosnap" is the offset if there's no SNAP header.
399 * If the link layer is Ethernet:
401 * "off_nl" is the offset if the packet is an Ethernet II packet
402 * (we assume no 802.3+802.2+SNAP);
404 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
405 * with an 802.2 header following it.
411 * Here we handle simple allocation of the scratch registers.
412 * If too many registers are alloc'd, the allocator punts.
414 int regused
[BPF_MEMWORDS
];
420 struct chunk chunks
[NCHUNKS
];
425 bpf_syntax_error(compiler_state_t
*cstate
, const char *msg
)
427 bpf_error(cstate
, "syntax error in filter expression: %s", msg
);
433 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
438 if (cstate
->bpf_pcap
!= NULL
)
439 (void)pcap_vsnprintf(pcap_geterr(cstate
->bpf_pcap
),
440 PCAP_ERRBUF_SIZE
, fmt
, ap
);
442 longjmp(cstate
->top_ctx
, 1);
446 static void init_linktype(compiler_state_t
*, pcap_t
*);
448 static void init_regs(compiler_state_t
*);
449 static int alloc_reg(compiler_state_t
*);
450 static void free_reg(compiler_state_t
*, int);
452 static void initchunks(compiler_state_t
*cstate
);
453 static void *newchunk(compiler_state_t
*cstate
, size_t);
454 static void freechunks(compiler_state_t
*cstate
);
455 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
456 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
457 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
458 static inline void syntax(compiler_state_t
*cstate
);
460 static void backpatch(struct block
*, struct block
*);
461 static void merge(struct block
*, struct block
*);
462 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
464 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
466 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
468 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
470 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
472 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
473 u_int
, bpf_int32
, bpf_u_int32
);
474 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
475 u_int
, const u_char
*);
476 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
477 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
478 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
480 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
482 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
483 static struct block
*gen_uncond(compiler_state_t
*, int);
484 static inline struct block
*gen_true(compiler_state_t
*);
485 static inline struct block
*gen_false(compiler_state_t
*);
486 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
487 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
488 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
489 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
490 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
491 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
492 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
493 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
494 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
496 static int ethertype_to_ppptype(int);
497 static struct block
*gen_linktype(compiler_state_t
*, int);
498 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
499 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
500 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
501 int, int, u_int
, u_int
);
503 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
504 struct in6_addr
*, int, int, u_int
, u_int
);
506 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
507 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
508 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
509 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
510 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
511 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
512 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
513 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
514 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
517 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
518 struct in6_addr
*, int, int, int);
521 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
523 static struct block
*gen_ipfrag(compiler_state_t
*);
524 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
525 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
527 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
528 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
530 struct block
*gen_portop(compiler_state_t
*, int, int, int);
531 static struct block
*gen_port(compiler_state_t
*, int, int, int);
532 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
533 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
534 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
535 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
536 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
537 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
538 static int lookup_proto(compiler_state_t
*, const char *, int);
539 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
540 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
541 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
542 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
543 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
544 static struct block
*gen_len(compiler_state_t
*, int, int);
545 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
546 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
548 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
549 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
552 initchunks(compiler_state_t
*cstate
)
556 for (i
= 0; i
< NCHUNKS
; i
++) {
557 cstate
->chunks
[i
].n_left
= 0;
558 cstate
->chunks
[i
].m
= NULL
;
560 cstate
->cur_chunk
= 0;
564 newchunk(compiler_state_t
*cstate
, size_t n
)
571 /* XXX Round up to nearest long. */
572 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
574 /* XXX Round up to structure boundary. */
578 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
579 if (n
> cp
->n_left
) {
580 ++cp
, k
= ++cstate
->cur_chunk
;
582 bpf_error(cstate
, "out of memory");
583 size
= CHUNK0SIZE
<< k
;
584 cp
->m
= (void *)malloc(size
);
586 bpf_error(cstate
, "out of memory");
587 memset((char *)cp
->m
, 0, size
);
590 bpf_error(cstate
, "out of memory");
593 return (void *)((char *)cp
->m
+ cp
->n_left
);
597 freechunks(compiler_state_t
*cstate
)
601 for (i
= 0; i
< NCHUNKS
; ++i
)
602 if (cstate
->chunks
[i
].m
!= NULL
)
603 free(cstate
->chunks
[i
].m
);
607 * A strdup whose allocations are freed after code generation is over.
610 sdup(compiler_state_t
*cstate
, const char *s
)
612 size_t n
= strlen(s
) + 1;
613 char *cp
= newchunk(cstate
, n
);
619 static inline struct block
*
620 new_block(compiler_state_t
*cstate
, int code
)
624 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
631 static inline struct slist
*
632 new_stmt(compiler_state_t
*cstate
, int code
)
636 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
642 static struct block
*
643 gen_retblk(compiler_state_t
*cstate
, int v
)
645 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
652 syntax(compiler_state_t
*cstate
)
654 bpf_error(cstate
, "syntax error in filter expression");
658 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
659 const char *buf
, int optimize
, bpf_u_int32 mask
)
661 compiler_state_t cstate
;
662 const char * volatile xbuf
= buf
;
663 yyscan_t scanner
= NULL
;
664 YY_BUFFER_STATE in_buffer
= NULL
;
677 * If this pcap_t hasn't been activated, it doesn't have a
678 * link-layer type, so we can't use it.
681 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
682 "not-yet-activated pcap_t passed to pcap_compile");
687 cstate
.no_optimize
= 0;
689 cstate
.ic
.root
= NULL
;
690 cstate
.ic
.cur_mark
= 0;
694 if (setjmp(cstate
.top_ctx
)) {
696 if (cstate
.ai
!= NULL
)
697 freeaddrinfo(cstate
.ai
);
703 cstate
.netmask
= mask
;
705 cstate
.snaplen
= pcap_snapshot(p
);
706 if (cstate
.snaplen
== 0) {
707 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
708 "snaplen of 0 rejects all packets");
713 if (pcap_lex_init(&scanner
) != 0)
714 bpf_error(&cstate
, "can't initialize scanner: %s", pcap_strerror(errno
));
715 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
718 * Associate the compiler state with the lexical analyzer
721 pcap_set_extra(&cstate
, scanner
);
723 init_linktype(&cstate
, p
);
724 (void)pcap_parse(scanner
, &cstate
);
726 if (cstate
.ic
.root
== NULL
)
727 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
729 if (optimize
&& !cstate
.no_optimize
) {
730 bpf_optimize(&cstate
, &cstate
.ic
);
731 if (cstate
.ic
.root
== NULL
||
732 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
733 bpf_error(&cstate
, "expression rejects all packets");
735 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
736 program
->bf_len
= len
;
738 rc
= 0; /* We're all okay */
742 * Clean up everything for the lexical analyzer.
744 if (in_buffer
!= NULL
)
745 pcap__delete_buffer(in_buffer
, scanner
);
747 pcap_lex_destroy(scanner
);
750 * Clean up our own allocated memory.
758 * entry point for using the compiler with no pcap open
759 * pass in all the stuff that is needed explicitly instead.
762 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
763 struct bpf_program
*program
,
764 const char *buf
, int optimize
, bpf_u_int32 mask
)
769 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
772 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
778 * Clean up a "struct bpf_program" by freeing all the memory allocated
782 pcap_freecode(struct bpf_program
*program
)
785 if (program
->bf_insns
!= NULL
) {
786 free((char *)program
->bf_insns
);
787 program
->bf_insns
= NULL
;
792 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
793 * which of the jt and jf fields has been resolved and which is a pointer
794 * back to another unresolved block (or nil). At least one of the fields
795 * in each block is already resolved.
798 backpatch(list
, target
)
799 struct block
*list
, *target
;
816 * Merge the lists in b0 and b1, using the 'sense' field to indicate
817 * which of jt and jf is the link.
821 struct block
*b0
, *b1
;
823 register struct block
**p
= &b0
;
825 /* Find end of list. */
827 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
829 /* Concatenate the lists. */
834 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
836 struct block
*ppi_dlt_check
;
839 * Insert before the statements of the first (root) block any
840 * statements needed to load the lengths of any variable-length
841 * headers into registers.
843 * XXX - a fancier strategy would be to insert those before the
844 * statements of all blocks that use those lengths and that
845 * have no predecessors that use them, so that we only compute
846 * the lengths if we need them. There might be even better
847 * approaches than that.
849 * However, those strategies would be more complicated, and
850 * as we don't generate code to compute a length if the
851 * program has no tests that use the length, and as most
852 * tests will probably use those lengths, we would just
853 * postpone computing the lengths so that it's not done
854 * for tests that fail early, and it's not clear that's
857 insert_compute_vloffsets(cstate
, p
->head
);
860 * For DLT_PPI captures, generate a check of the per-packet
861 * DLT value to make sure it's DLT_IEEE802_11.
863 * XXX - TurboCap cards use DLT_PPI for Ethernet.
864 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
865 * with appropriate Ethernet information and use that rather
866 * than using something such as DLT_PPI where you don't know
867 * the link-layer header type until runtime, which, in the
868 * general case, would force us to generate both Ethernet *and*
869 * 802.11 code (*and* anything else for which PPI is used)
870 * and choose between them early in the BPF program?
872 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
873 if (ppi_dlt_check
!= NULL
)
874 gen_and(ppi_dlt_check
, p
);
876 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
877 p
->sense
= !p
->sense
;
878 backpatch(p
, gen_retblk(cstate
, 0));
879 cstate
->ic
.root
= p
->head
;
884 struct block
*b0
, *b1
;
886 backpatch(b0
, b1
->head
);
887 b0
->sense
= !b0
->sense
;
888 b1
->sense
= !b1
->sense
;
890 b1
->sense
= !b1
->sense
;
896 struct block
*b0
, *b1
;
898 b0
->sense
= !b0
->sense
;
899 backpatch(b0
, b1
->head
);
900 b0
->sense
= !b0
->sense
;
909 b
->sense
= !b
->sense
;
912 static struct block
*
913 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
914 u_int size
, bpf_int32 v
)
916 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
919 static struct block
*
920 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
921 u_int size
, bpf_int32 v
)
923 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
926 static struct block
*
927 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
928 u_int size
, bpf_int32 v
)
930 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
933 static struct block
*
934 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
935 u_int size
, bpf_int32 v
)
937 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
940 static struct block
*
941 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
942 u_int size
, bpf_int32 v
)
944 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
947 static struct block
*
948 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
949 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
951 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
954 static struct block
*
955 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
956 u_int size
, const u_char
*v
)
958 register struct block
*b
, *tmp
;
962 register const u_char
*p
= &v
[size
- 4];
963 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
964 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
966 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
, w
);
973 register const u_char
*p
= &v
[size
- 2];
974 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
976 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
, w
);
983 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
992 * AND the field of size "size" at offset "offset" relative to the header
993 * specified by "offrel" with "mask", and compare it with the value "v"
994 * with the test specified by "jtype"; if "reverse" is true, the test
995 * should test the opposite of "jtype".
997 static struct block
*
998 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
999 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1002 struct slist
*s
, *s2
;
1005 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1007 if (mask
!= 0xffffffff) {
1008 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1013 b
= new_block(cstate
, JMP(jtype
));
1016 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1022 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1024 cstate
->pcap_fddipad
= p
->fddipad
;
1027 * We start out with only one link-layer header.
1029 cstate
->outermostlinktype
= pcap_datalink(p
);
1030 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1031 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1032 cstate
->off_outermostlinkhdr
.reg
= -1;
1034 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1035 cstate
->off_prevlinkhdr
.constant_part
= 0;
1036 cstate
->off_prevlinkhdr
.is_variable
= 0;
1037 cstate
->off_prevlinkhdr
.reg
= -1;
1039 cstate
->linktype
= cstate
->outermostlinktype
;
1040 cstate
->off_linkhdr
.constant_part
= 0;
1041 cstate
->off_linkhdr
.is_variable
= 0;
1042 cstate
->off_linkhdr
.reg
= -1;
1047 cstate
->off_linkpl
.constant_part
= 0;
1048 cstate
->off_linkpl
.is_variable
= 0;
1049 cstate
->off_linkpl
.reg
= -1;
1051 cstate
->off_linktype
.constant_part
= 0;
1052 cstate
->off_linktype
.is_variable
= 0;
1053 cstate
->off_linktype
.reg
= -1;
1056 * Assume it's not raw ATM with a pseudo-header, for now.
1059 cstate
->off_vpi
= -1;
1060 cstate
->off_vci
= -1;
1061 cstate
->off_proto
= -1;
1062 cstate
->off_payload
= -1;
1067 cstate
->is_geneve
= 0;
1070 * And assume we're not doing SS7.
1072 cstate
->off_li
= -1;
1073 cstate
->off_li_hsl
= -1;
1074 cstate
->off_sio
= -1;
1075 cstate
->off_opc
= -1;
1076 cstate
->off_dpc
= -1;
1077 cstate
->off_sls
= -1;
1079 cstate
->label_stack_depth
= 0;
1080 cstate
->vlan_stack_depth
= 0;
1082 switch (cstate
->linktype
) {
1085 cstate
->off_linktype
.constant_part
= 2;
1086 cstate
->off_linkpl
.constant_part
= 6;
1087 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1088 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1091 case DLT_ARCNET_LINUX
:
1092 cstate
->off_linktype
.constant_part
= 4;
1093 cstate
->off_linkpl
.constant_part
= 8;
1094 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1095 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1099 cstate
->off_linktype
.constant_part
= 12;
1100 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1101 cstate
->off_nl
= 0; /* Ethernet II */
1102 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1107 * SLIP doesn't have a link level type. The 16 byte
1108 * header is hacked into our SLIP driver.
1110 cstate
->off_linktype
.constant_part
= -1;
1111 cstate
->off_linkpl
.constant_part
= 16;
1113 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1116 case DLT_SLIP_BSDOS
:
1117 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1118 cstate
->off_linktype
.constant_part
= -1;
1120 cstate
->off_linkpl
.constant_part
= 24;
1122 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1127 cstate
->off_linktype
.constant_part
= 0;
1128 cstate
->off_linkpl
.constant_part
= 4;
1130 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1134 cstate
->off_linktype
.constant_part
= 0;
1135 cstate
->off_linkpl
.constant_part
= 12;
1137 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1142 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1143 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1144 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1145 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1147 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1152 * This does no include the Ethernet header, and
1153 * only covers session state.
1155 cstate
->off_linktype
.constant_part
= 6;
1156 cstate
->off_linkpl
.constant_part
= 8;
1158 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1162 cstate
->off_linktype
.constant_part
= 5;
1163 cstate
->off_linkpl
.constant_part
= 24;
1165 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1170 * FDDI doesn't really have a link-level type field.
1171 * We set "off_linktype" to the offset of the LLC header.
1173 * To check for Ethernet types, we assume that SSAP = SNAP
1174 * is being used and pick out the encapsulated Ethernet type.
1175 * XXX - should we generate code to check for SNAP?
1177 cstate
->off_linktype
.constant_part
= 13;
1178 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1179 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1180 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1181 cstate
->off_nl
= 8; /* 802.2+SNAP */
1182 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1187 * Token Ring doesn't really have a link-level type field.
1188 * We set "off_linktype" to the offset of the LLC header.
1190 * To check for Ethernet types, we assume that SSAP = SNAP
1191 * is being used and pick out the encapsulated Ethernet type.
1192 * XXX - should we generate code to check for SNAP?
1194 * XXX - the header is actually variable-length.
1195 * Some various Linux patched versions gave 38
1196 * as "off_linktype" and 40 as "off_nl"; however,
1197 * if a token ring packet has *no* routing
1198 * information, i.e. is not source-routed, the correct
1199 * values are 20 and 22, as they are in the vanilla code.
1201 * A packet is source-routed iff the uppermost bit
1202 * of the first byte of the source address, at an
1203 * offset of 8, has the uppermost bit set. If the
1204 * packet is source-routed, the total number of bytes
1205 * of routing information is 2 plus bits 0x1F00 of
1206 * the 16-bit value at an offset of 14 (shifted right
1207 * 8 - figure out which byte that is).
1209 cstate
->off_linktype
.constant_part
= 14;
1210 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1211 cstate
->off_nl
= 8; /* 802.2+SNAP */
1212 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1215 case DLT_PRISM_HEADER
:
1216 case DLT_IEEE802_11_RADIO_AVS
:
1217 case DLT_IEEE802_11_RADIO
:
1218 cstate
->off_linkhdr
.is_variable
= 1;
1219 /* Fall through, 802.11 doesn't have a variable link
1220 * prefix but is otherwise the same. */
1222 case DLT_IEEE802_11
:
1224 * 802.11 doesn't really have a link-level type field.
1225 * We set "off_linktype.constant_part" to the offset of
1228 * To check for Ethernet types, we assume that SSAP = SNAP
1229 * is being used and pick out the encapsulated Ethernet type.
1230 * XXX - should we generate code to check for SNAP?
1232 * We also handle variable-length radio headers here.
1233 * The Prism header is in theory variable-length, but in
1234 * practice it's always 144 bytes long. However, some
1235 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1236 * sometimes or always supply an AVS header, so we
1237 * have to check whether the radio header is a Prism
1238 * header or an AVS header, so, in practice, it's
1241 cstate
->off_linktype
.constant_part
= 24;
1242 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1243 cstate
->off_linkpl
.is_variable
= 1;
1244 cstate
->off_nl
= 8; /* 802.2+SNAP */
1245 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1250 * At the moment we treat PPI the same way that we treat
1251 * normal Radiotap encoded packets. The difference is in
1252 * the function that generates the code at the beginning
1253 * to compute the header length. Since this code generator
1254 * of PPI supports bare 802.11 encapsulation only (i.e.
1255 * the encapsulated DLT should be DLT_IEEE802_11) we
1256 * generate code to check for this too.
1258 cstate
->off_linktype
.constant_part
= 24;
1259 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1260 cstate
->off_linkpl
.is_variable
= 1;
1261 cstate
->off_linkhdr
.is_variable
= 1;
1262 cstate
->off_nl
= 8; /* 802.2+SNAP */
1263 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1266 case DLT_ATM_RFC1483
:
1267 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1269 * assume routed, non-ISO PDUs
1270 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1272 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1273 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1274 * latter would presumably be treated the way PPPoE
1275 * should be, so you can do "pppoe and udp port 2049"
1276 * or "pppoa and tcp port 80" and have it check for
1277 * PPPo{A,E} and a PPP protocol of IP and....
1279 cstate
->off_linktype
.constant_part
= 0;
1280 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1281 cstate
->off_nl
= 8; /* 802.2+SNAP */
1282 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1287 * Full Frontal ATM; you get AALn PDUs with an ATM
1291 cstate
->off_vpi
= SUNATM_VPI_POS
;
1292 cstate
->off_vci
= SUNATM_VCI_POS
;
1293 cstate
->off_proto
= PROTO_POS
;
1294 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1295 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1296 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1297 cstate
->off_nl
= 8; /* 802.2+SNAP */
1298 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1304 cstate
->off_linktype
.constant_part
= -1;
1305 cstate
->off_linkpl
.constant_part
= 0;
1307 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1310 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1311 cstate
->off_linktype
.constant_part
= 14;
1312 cstate
->off_linkpl
.constant_part
= 16;
1314 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1319 * LocalTalk does have a 1-byte type field in the LLAP header,
1320 * but really it just indicates whether there is a "short" or
1321 * "long" DDP packet following.
1323 cstate
->off_linktype
.constant_part
= -1;
1324 cstate
->off_linkpl
.constant_part
= 0;
1326 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1329 case DLT_IP_OVER_FC
:
1331 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1332 * link-level type field. We set "off_linktype" to the
1333 * offset of the LLC header.
1335 * To check for Ethernet types, we assume that SSAP = SNAP
1336 * is being used and pick out the encapsulated Ethernet type.
1337 * XXX - should we generate code to check for SNAP? RFC
1338 * 2625 says SNAP should be used.
1340 cstate
->off_linktype
.constant_part
= 16;
1341 cstate
->off_linkpl
.constant_part
= 16;
1342 cstate
->off_nl
= 8; /* 802.2+SNAP */
1343 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1348 * XXX - we should set this to handle SNAP-encapsulated
1349 * frames (NLPID of 0x80).
1351 cstate
->off_linktype
.constant_part
= -1;
1352 cstate
->off_linkpl
.constant_part
= 0;
1354 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1358 * the only BPF-interesting FRF.16 frames are non-control frames;
1359 * Frame Relay has a variable length link-layer
1360 * so lets start with offset 4 for now and increments later on (FIXME);
1363 cstate
->off_linktype
.constant_part
= -1;
1364 cstate
->off_linkpl
.constant_part
= 0;
1366 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1369 case DLT_APPLE_IP_OVER_IEEE1394
:
1370 cstate
->off_linktype
.constant_part
= 16;
1371 cstate
->off_linkpl
.constant_part
= 18;
1373 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1376 case DLT_SYMANTEC_FIREWALL
:
1377 cstate
->off_linktype
.constant_part
= 6;
1378 cstate
->off_linkpl
.constant_part
= 44;
1379 cstate
->off_nl
= 0; /* Ethernet II */
1380 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1383 #ifdef HAVE_NET_PFVAR_H
1385 cstate
->off_linktype
.constant_part
= 0;
1386 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1388 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1392 case DLT_JUNIPER_MFR
:
1393 case DLT_JUNIPER_MLFR
:
1394 case DLT_JUNIPER_MLPPP
:
1395 case DLT_JUNIPER_PPP
:
1396 case DLT_JUNIPER_CHDLC
:
1397 case DLT_JUNIPER_FRELAY
:
1398 cstate
->off_linktype
.constant_part
= 4;
1399 cstate
->off_linkpl
.constant_part
= 4;
1401 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1404 case DLT_JUNIPER_ATM1
:
1405 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1406 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1408 cstate
->off_nl_nosnap
= 10;
1411 case DLT_JUNIPER_ATM2
:
1412 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1413 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1415 cstate
->off_nl_nosnap
= 10;
1418 /* frames captured on a Juniper PPPoE service PIC
1419 * contain raw ethernet frames */
1420 case DLT_JUNIPER_PPPOE
:
1421 case DLT_JUNIPER_ETHER
:
1422 cstate
->off_linkpl
.constant_part
= 14;
1423 cstate
->off_linktype
.constant_part
= 16;
1424 cstate
->off_nl
= 18; /* Ethernet II */
1425 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1428 case DLT_JUNIPER_PPPOE_ATM
:
1429 cstate
->off_linktype
.constant_part
= 4;
1430 cstate
->off_linkpl
.constant_part
= 6;
1432 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1435 case DLT_JUNIPER_GGSN
:
1436 cstate
->off_linktype
.constant_part
= 6;
1437 cstate
->off_linkpl
.constant_part
= 12;
1439 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1442 case DLT_JUNIPER_ES
:
1443 cstate
->off_linktype
.constant_part
= 6;
1444 cstate
->off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1445 cstate
->off_nl
= -1; /* not really a network layer but raw IP addresses */
1446 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1449 case DLT_JUNIPER_MONITOR
:
1450 cstate
->off_linktype
.constant_part
= 12;
1451 cstate
->off_linkpl
.constant_part
= 12;
1452 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1453 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1456 case DLT_BACNET_MS_TP
:
1457 cstate
->off_linktype
.constant_part
= -1;
1458 cstate
->off_linkpl
.constant_part
= -1;
1459 cstate
->off_nl
= -1;
1460 cstate
->off_nl_nosnap
= -1;
1463 case DLT_JUNIPER_SERVICES
:
1464 cstate
->off_linktype
.constant_part
= 12;
1465 cstate
->off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1466 cstate
->off_nl
= -1; /* L3 proto location dep. on cookie type */
1467 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1470 case DLT_JUNIPER_VP
:
1471 cstate
->off_linktype
.constant_part
= 18;
1472 cstate
->off_linkpl
.constant_part
= -1;
1473 cstate
->off_nl
= -1;
1474 cstate
->off_nl_nosnap
= -1;
1477 case DLT_JUNIPER_ST
:
1478 cstate
->off_linktype
.constant_part
= 18;
1479 cstate
->off_linkpl
.constant_part
= -1;
1480 cstate
->off_nl
= -1;
1481 cstate
->off_nl_nosnap
= -1;
1484 case DLT_JUNIPER_ISM
:
1485 cstate
->off_linktype
.constant_part
= 8;
1486 cstate
->off_linkpl
.constant_part
= -1;
1487 cstate
->off_nl
= -1;
1488 cstate
->off_nl_nosnap
= -1;
1491 case DLT_JUNIPER_VS
:
1492 case DLT_JUNIPER_SRX_E2E
:
1493 case DLT_JUNIPER_FIBRECHANNEL
:
1494 case DLT_JUNIPER_ATM_CEMIC
:
1495 cstate
->off_linktype
.constant_part
= 8;
1496 cstate
->off_linkpl
.constant_part
= -1;
1497 cstate
->off_nl
= -1;
1498 cstate
->off_nl_nosnap
= -1;
1503 cstate
->off_li_hsl
= 4;
1504 cstate
->off_sio
= 3;
1505 cstate
->off_opc
= 4;
1506 cstate
->off_dpc
= 4;
1507 cstate
->off_sls
= 7;
1508 cstate
->off_linktype
.constant_part
= -1;
1509 cstate
->off_linkpl
.constant_part
= -1;
1510 cstate
->off_nl
= -1;
1511 cstate
->off_nl_nosnap
= -1;
1514 case DLT_MTP2_WITH_PHDR
:
1516 cstate
->off_li_hsl
= 8;
1517 cstate
->off_sio
= 7;
1518 cstate
->off_opc
= 8;
1519 cstate
->off_dpc
= 8;
1520 cstate
->off_sls
= 11;
1521 cstate
->off_linktype
.constant_part
= -1;
1522 cstate
->off_linkpl
.constant_part
= -1;
1523 cstate
->off_nl
= -1;
1524 cstate
->off_nl_nosnap
= -1;
1528 cstate
->off_li
= 22;
1529 cstate
->off_li_hsl
= 24;
1530 cstate
->off_sio
= 23;
1531 cstate
->off_opc
= 24;
1532 cstate
->off_dpc
= 24;
1533 cstate
->off_sls
= 27;
1534 cstate
->off_linktype
.constant_part
= -1;
1535 cstate
->off_linkpl
.constant_part
= -1;
1536 cstate
->off_nl
= -1;
1537 cstate
->off_nl_nosnap
= -1;
1541 cstate
->off_linktype
.constant_part
= -1;
1542 cstate
->off_linkpl
.constant_part
= 4;
1544 cstate
->off_nl_nosnap
= 0;
1549 * Currently, only raw "link[N:M]" filtering is supported.
1551 cstate
->off_linktype
.constant_part
= -1; /* variable, min 15, max 71 steps of 7 */
1552 cstate
->off_linkpl
.constant_part
= -1;
1553 cstate
->off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1554 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1558 cstate
->off_linktype
.constant_part
= 1;
1559 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1561 cstate
->off_nl_nosnap
= -1;
1564 case DLT_NETANALYZER
:
1565 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1566 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1567 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1568 cstate
->off_nl
= 0; /* Ethernet II */
1569 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1572 case DLT_NETANALYZER_TRANSPARENT
:
1573 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1574 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1575 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1576 cstate
->off_nl
= 0; /* Ethernet II */
1577 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1582 * For values in the range in which we've assigned new
1583 * DLT_ values, only raw "link[N:M]" filtering is supported.
1585 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1586 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1587 cstate
->off_linktype
.constant_part
= -1;
1588 cstate
->off_linkpl
.constant_part
= -1;
1589 cstate
->off_nl
= -1;
1590 cstate
->off_nl_nosnap
= -1;
1592 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1597 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1601 * Load a value relative to the specified absolute offset.
1603 static struct slist
*
1604 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1605 u_int offset
, u_int size
)
1607 struct slist
*s
, *s2
;
1609 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1612 * If "s" is non-null, it has code to arrange that the X register
1613 * contains the variable part of the absolute offset, so we
1614 * generate a load relative to that, with an offset of
1615 * abs_offset->constant_part + offset.
1617 * Otherwise, we can do an absolute load with an offset of
1618 * abs_offset->constant_part + offset.
1622 * "s" points to a list of statements that puts the
1623 * variable part of the absolute offset into the X register.
1624 * Do an indirect load, to use the X register as an offset.
1626 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1627 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1631 * There is no variable part of the absolute offset, so
1632 * just do an absolute load.
1634 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1635 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1641 * Load a value relative to the beginning of the specified header.
1643 static struct slist
*
1644 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1647 struct slist
*s
, *s2
;
1652 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1657 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1660 case OR_PREVLINKHDR
:
1661 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1665 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1668 case OR_PREVMPLSHDR
:
1669 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1673 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1676 case OR_LINKPL_NOSNAP
:
1677 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1681 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1686 * Load the X register with the length of the IPv4 header
1687 * (plus the offset of the link-layer header, if it's
1688 * preceded by a variable-length header such as a radio
1689 * header), in bytes.
1691 s
= gen_loadx_iphdrlen(cstate
);
1694 * Load the item at {offset of the link-layer payload} +
1695 * {offset, relative to the start of the link-layer
1696 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1697 * {specified offset}.
1699 * If the offset of the link-layer payload is variable,
1700 * the variable part of that offset is included in the
1701 * value in the X register, and we include the constant
1702 * part in the offset of the load.
1704 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1705 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1710 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1721 * Generate code to load into the X register the sum of the length of
1722 * the IPv4 header and the variable part of the offset of the link-layer
1725 static struct slist
*
1726 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1728 struct slist
*s
, *s2
;
1730 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1733 * The offset of the link-layer payload has a variable
1734 * part. "s" points to a list of statements that put
1735 * the variable part of that offset into the X register.
1737 * The 4*([k]&0xf) addressing mode can't be used, as we
1738 * don't have a constant offset, so we have to load the
1739 * value in question into the A register and add to it
1740 * the value from the X register.
1742 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1743 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1745 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1748 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1753 * The A register now contains the length of the IP header.
1754 * We need to add to it the variable part of the offset of
1755 * the link-layer payload, which is still in the X
1756 * register, and move the result into the X register.
1758 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1759 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1762 * The offset of the link-layer payload is a constant,
1763 * so no code was generated to load the (non-existent)
1764 * variable part of that offset.
1766 * This means we can use the 4*([k]&0xf) addressing
1767 * mode. Load the length of the IPv4 header, which
1768 * is at an offset of cstate->off_nl from the beginning of
1769 * the link-layer payload, and thus at an offset of
1770 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1771 * of the raw packet data, using that addressing mode.
1773 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1774 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1779 static struct block
*
1780 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1785 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1787 b
= new_block(cstate
, JMP(BPF_JEQ
));
1793 static inline struct block
*
1794 gen_true(compiler_state_t
*cstate
)
1796 return gen_uncond(cstate
, 1);
1799 static inline struct block
*
1800 gen_false(compiler_state_t
*cstate
)
1802 return gen_uncond(cstate
, 0);
1806 * Byte-swap a 32-bit number.
1807 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1808 * big-endian platforms.)
1810 #define SWAPLONG(y) \
1811 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1814 * Generate code to match a particular packet type.
1816 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1817 * value, if <= ETHERMTU. We use that to determine whether to
1818 * match the type/length field or to check the type/length field for
1819 * a value <= ETHERMTU to see whether it's a type field and then do
1820 * the appropriate test.
1822 static struct block
*
1823 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1825 struct block
*b0
, *b1
;
1831 case LLCSAP_NETBEUI
:
1833 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1834 * so we check the DSAP and SSAP.
1836 * LLCSAP_IP checks for IP-over-802.2, rather
1837 * than IP-over-Ethernet or IP-over-SNAP.
1839 * XXX - should we check both the DSAP and the
1840 * SSAP, like this, or should we check just the
1841 * DSAP, as we do for other types <= ETHERMTU
1842 * (i.e., other SAP values)?
1844 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1846 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1847 ((proto
<< 8) | proto
));
1855 * Ethernet_II frames, which are Ethernet
1856 * frames with a frame type of ETHERTYPE_IPX;
1858 * Ethernet_802.3 frames, which are 802.3
1859 * frames (i.e., the type/length field is
1860 * a length field, <= ETHERMTU, rather than
1861 * a type field) with the first two bytes
1862 * after the Ethernet/802.3 header being
1865 * Ethernet_802.2 frames, which are 802.3
1866 * frames with an 802.2 LLC header and
1867 * with the IPX LSAP as the DSAP in the LLC
1870 * Ethernet_SNAP frames, which are 802.3
1871 * frames with an LLC header and a SNAP
1872 * header and with an OUI of 0x000000
1873 * (encapsulated Ethernet) and a protocol
1874 * ID of ETHERTYPE_IPX in the SNAP header.
1876 * XXX - should we generate the same code both
1877 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1881 * This generates code to check both for the
1882 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1884 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1885 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1889 * Now we add code to check for SNAP frames with
1890 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1892 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1896 * Now we generate code to check for 802.3
1897 * frames in general.
1899 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1903 * Now add the check for 802.3 frames before the
1904 * check for Ethernet_802.2 and Ethernet_802.3,
1905 * as those checks should only be done on 802.3
1906 * frames, not on Ethernet frames.
1911 * Now add the check for Ethernet_II frames, and
1912 * do that before checking for the other frame
1915 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1919 case ETHERTYPE_ATALK
:
1920 case ETHERTYPE_AARP
:
1922 * EtherTalk (AppleTalk protocols on Ethernet link
1923 * layer) may use 802.2 encapsulation.
1927 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1928 * we check for an Ethernet type field less than
1929 * 1500, which means it's an 802.3 length field.
1931 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1935 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1936 * SNAP packets with an organization code of
1937 * 0x080007 (Apple, for Appletalk) and a protocol
1938 * type of ETHERTYPE_ATALK (Appletalk).
1940 * 802.2-encapsulated ETHERTYPE_AARP packets are
1941 * SNAP packets with an organization code of
1942 * 0x000000 (encapsulated Ethernet) and a protocol
1943 * type of ETHERTYPE_AARP (Appletalk ARP).
1945 if (proto
== ETHERTYPE_ATALK
)
1946 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
1947 else /* proto == ETHERTYPE_AARP */
1948 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
1952 * Check for Ethernet encapsulation (Ethertalk
1953 * phase 1?); we just check for the Ethernet
1956 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1962 if (proto
<= ETHERMTU
) {
1964 * This is an LLC SAP value, so the frames
1965 * that match would be 802.2 frames.
1966 * Check that the frame is an 802.2 frame
1967 * (i.e., that the length/type field is
1968 * a length field, <= ETHERMTU) and
1969 * then check the DSAP.
1971 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1973 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1978 * This is an Ethernet type, so compare
1979 * the length/type field with it (if
1980 * the frame is an 802.2 frame, the length
1981 * field will be <= ETHERMTU, and, as
1982 * "proto" is > ETHERMTU, this test
1983 * will fail and the frame won't match,
1984 * which is what we want).
1986 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
1993 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1994 * or IPv6 then we have an error.
1996 static struct block
*
1997 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2002 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2005 case ETHERTYPE_IPV6
:
2006 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2007 (bpf_int32
)IPH_AF_INET6
);
2014 return gen_false(cstate
);
2018 * Generate code to match a particular packet type.
2020 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2021 * value, if <= ETHERMTU. We use that to determine whether to
2022 * match the type field or to check the type field for the special
2023 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2025 static struct block
*
2026 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2028 struct block
*b0
, *b1
;
2034 case LLCSAP_NETBEUI
:
2036 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2037 * so we check the DSAP and SSAP.
2039 * LLCSAP_IP checks for IP-over-802.2, rather
2040 * than IP-over-Ethernet or IP-over-SNAP.
2042 * XXX - should we check both the DSAP and the
2043 * SSAP, like this, or should we check just the
2044 * DSAP, as we do for other types <= ETHERMTU
2045 * (i.e., other SAP values)?
2047 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2048 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2049 ((proto
<< 8) | proto
));
2055 * Ethernet_II frames, which are Ethernet
2056 * frames with a frame type of ETHERTYPE_IPX;
2058 * Ethernet_802.3 frames, which have a frame
2059 * type of LINUX_SLL_P_802_3;
2061 * Ethernet_802.2 frames, which are 802.3
2062 * frames with an 802.2 LLC header (i.e, have
2063 * a frame type of LINUX_SLL_P_802_2) and
2064 * with the IPX LSAP as the DSAP in the LLC
2067 * Ethernet_SNAP frames, which are 802.3
2068 * frames with an LLC header and a SNAP
2069 * header and with an OUI of 0x000000
2070 * (encapsulated Ethernet) and a protocol
2071 * ID of ETHERTYPE_IPX in the SNAP header.
2073 * First, do the checks on LINUX_SLL_P_802_2
2074 * frames; generate the check for either
2075 * Ethernet_802.2 or Ethernet_SNAP frames, and
2076 * then put a check for LINUX_SLL_P_802_2 frames
2079 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2080 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2082 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2086 * Now check for 802.3 frames and OR that with
2087 * the previous test.
2089 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2093 * Now add the check for Ethernet_II frames, and
2094 * do that before checking for the other frame
2097 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2101 case ETHERTYPE_ATALK
:
2102 case ETHERTYPE_AARP
:
2104 * EtherTalk (AppleTalk protocols on Ethernet link
2105 * layer) may use 802.2 encapsulation.
2109 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2110 * we check for the 802.2 protocol type in the
2111 * "Ethernet type" field.
2113 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2116 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2117 * SNAP packets with an organization code of
2118 * 0x080007 (Apple, for Appletalk) and a protocol
2119 * type of ETHERTYPE_ATALK (Appletalk).
2121 * 802.2-encapsulated ETHERTYPE_AARP packets are
2122 * SNAP packets with an organization code of
2123 * 0x000000 (encapsulated Ethernet) and a protocol
2124 * type of ETHERTYPE_AARP (Appletalk ARP).
2126 if (proto
== ETHERTYPE_ATALK
)
2127 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2128 else /* proto == ETHERTYPE_AARP */
2129 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2133 * Check for Ethernet encapsulation (Ethertalk
2134 * phase 1?); we just check for the Ethernet
2137 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2143 if (proto
<= ETHERMTU
) {
2145 * This is an LLC SAP value, so the frames
2146 * that match would be 802.2 frames.
2147 * Check for the 802.2 protocol type
2148 * in the "Ethernet type" field, and
2149 * then check the DSAP.
2151 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2152 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2158 * This is an Ethernet type, so compare
2159 * the length/type field with it (if
2160 * the frame is an 802.2 frame, the length
2161 * field will be <= ETHERMTU, and, as
2162 * "proto" is > ETHERMTU, this test
2163 * will fail and the frame won't match,
2164 * which is what we want).
2166 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2171 static struct slist
*
2172 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2174 struct slist
*s1
, *s2
;
2175 struct slist
*sjeq_avs_cookie
;
2176 struct slist
*sjcommon
;
2179 * This code is not compatible with the optimizer, as
2180 * we are generating jmp instructions within a normal
2181 * slist of instructions
2183 cstate
->no_optimize
= 1;
2186 * Generate code to load the length of the radio header into
2187 * the register assigned to hold that length, if one has been
2188 * assigned. (If one hasn't been assigned, no code we've
2189 * generated uses that prefix, so we don't need to generate any
2192 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2193 * or always use the AVS header rather than the Prism header.
2194 * We load a 4-byte big-endian value at the beginning of the
2195 * raw packet data, and see whether, when masked with 0xFFFFF000,
2196 * it's equal to 0x80211000. If so, that indicates that it's
2197 * an AVS header (the masked-out bits are the version number).
2198 * Otherwise, it's a Prism header.
2200 * XXX - the Prism header is also, in theory, variable-length,
2201 * but no known software generates headers that aren't 144
2204 if (cstate
->off_linkhdr
.reg
!= -1) {
2208 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2212 * AND it with 0xFFFFF000.
2214 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2215 s2
->s
.k
= 0xFFFFF000;
2219 * Compare with 0x80211000.
2221 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2222 sjeq_avs_cookie
->s
.k
= 0x80211000;
2223 sappend(s1
, sjeq_avs_cookie
);
2228 * The 4 bytes at an offset of 4 from the beginning of
2229 * the AVS header are the length of the AVS header.
2230 * That field is big-endian.
2232 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2235 sjeq_avs_cookie
->s
.jt
= s2
;
2238 * Now jump to the code to allocate a register
2239 * into which to save the header length and
2240 * store the length there. (The "jump always"
2241 * instruction needs to have the k field set;
2242 * it's added to the PC, so, as we're jumping
2243 * over a single instruction, it should be 1.)
2245 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2247 sappend(s1
, sjcommon
);
2250 * Now for the code that handles the Prism header.
2251 * Just load the length of the Prism header (144)
2252 * into the A register. Have the test for an AVS
2253 * header branch here if we don't have an AVS header.
2255 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2258 sjeq_avs_cookie
->s
.jf
= s2
;
2261 * Now allocate a register to hold that value and store
2262 * it. The code for the AVS header will jump here after
2263 * loading the length of the AVS header.
2265 s2
= new_stmt(cstate
, BPF_ST
);
2266 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2268 sjcommon
->s
.jf
= s2
;
2271 * Now move it into the X register.
2273 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2281 static struct slist
*
2282 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2284 struct slist
*s1
, *s2
;
2287 * Generate code to load the length of the AVS header into
2288 * the register assigned to hold that length, if one has been
2289 * assigned. (If one hasn't been assigned, no code we've
2290 * generated uses that prefix, so we don't need to generate any
2293 if (cstate
->off_linkhdr
.reg
!= -1) {
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 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2303 * Now allocate a register to hold that value and store
2306 s2
= new_stmt(cstate
, BPF_ST
);
2307 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2311 * Now move it into the X register.
2313 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2321 static struct slist
*
2322 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2324 struct slist
*s1
, *s2
;
2327 * Generate code to load the length of the radiotap header into
2328 * the register assigned to hold that length, if one has been
2329 * assigned. (If one hasn't been assigned, no code we've
2330 * generated uses that prefix, so we don't need to generate any
2333 if (cstate
->off_linkhdr
.reg
!= -1) {
2335 * The 2 bytes at offsets of 2 and 3 from the beginning
2336 * of the radiotap header are the length of the radiotap
2337 * header; unfortunately, it's little-endian, so we have
2338 * to load it a byte at a time and construct the value.
2342 * Load the high-order byte, at an offset of 3, shift it
2343 * left a byte, and put the result in the X register.
2345 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2347 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2350 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2354 * Load the next byte, at an offset of 2, and OR the
2355 * value from the X register into it.
2357 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2360 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2364 * Now allocate a register to hold that value and store
2367 s2
= new_stmt(cstate
, BPF_ST
);
2368 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2372 * Now move it into the X register.
2374 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2383 * At the moment we treat PPI as normal Radiotap encoded
2384 * packets. The difference is in the function that generates
2385 * the code at the beginning to compute the header length.
2386 * Since this code generator of PPI supports bare 802.11
2387 * encapsulation only (i.e. the encapsulated DLT should be
2388 * DLT_IEEE802_11) we generate code to check for this too;
2389 * that's done in finish_parse().
2391 static struct slist
*
2392 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2394 struct slist
*s1
, *s2
;
2397 * Generate code to load the length of the radiotap header
2398 * into the register assigned to hold that length, if one has
2401 if (cstate
->off_linkhdr
.reg
!= -1) {
2403 * The 2 bytes at offsets of 2 and 3 from the beginning
2404 * of the radiotap header are the length of the radiotap
2405 * header; unfortunately, it's little-endian, so we have
2406 * to load it a byte at a time and construct the value.
2410 * Load the high-order byte, at an offset of 3, shift it
2411 * left a byte, and put the result in the X register.
2413 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2415 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2418 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2422 * Load the next byte, at an offset of 2, and OR the
2423 * value from the X register into it.
2425 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2428 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2432 * Now allocate a register to hold that value and store
2435 s2
= new_stmt(cstate
, BPF_ST
);
2436 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2440 * Now move it into the X register.
2442 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2451 * Load a value relative to the beginning of the link-layer header after the 802.11
2452 * header, i.e. LLC_SNAP.
2453 * The link-layer header doesn't necessarily begin at the beginning
2454 * of the packet data; there might be a variable-length prefix containing
2455 * radio information.
2457 static struct slist
*
2458 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2461 struct slist
*sjset_data_frame_1
;
2462 struct slist
*sjset_data_frame_2
;
2463 struct slist
*sjset_qos
;
2464 struct slist
*sjset_radiotap_flags_present
;
2465 struct slist
*sjset_radiotap_ext_present
;
2466 struct slist
*sjset_radiotap_tsft_present
;
2467 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2468 struct slist
*s_roundup
;
2470 if (cstate
->off_linkpl
.reg
== -1) {
2472 * No register has been assigned to the offset of
2473 * the link-layer payload, which means nobody needs
2474 * it; don't bother computing it - just return
2475 * what we already have.
2481 * This code is not compatible with the optimizer, as
2482 * we are generating jmp instructions within a normal
2483 * slist of instructions
2485 cstate
->no_optimize
= 1;
2488 * If "s" is non-null, it has code to arrange that the X register
2489 * contains the length of the prefix preceding the link-layer
2492 * Otherwise, the length of the prefix preceding the link-layer
2493 * header is "off_outermostlinkhdr.constant_part".
2497 * There is no variable-length header preceding the
2498 * link-layer header.
2500 * Load the length of the fixed-length prefix preceding
2501 * the link-layer header (if any) into the X register,
2502 * and store it in the cstate->off_linkpl.reg register.
2503 * That length is off_outermostlinkhdr.constant_part.
2505 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2506 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2510 * The X register contains the offset of the beginning of the
2511 * link-layer header; add 24, which is the minimum length
2512 * of the MAC header for a data frame, to that, and store it
2513 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2514 * which is at the offset in the X register, with an indexed load.
2516 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2518 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2521 s2
= new_stmt(cstate
, BPF_ST
);
2522 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2525 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2530 * Check the Frame Control field to see if this is a data frame;
2531 * a data frame has the 0x08 bit (b3) in that field set and the
2532 * 0x04 bit (b2) clear.
2534 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2535 sjset_data_frame_1
->s
.k
= 0x08;
2536 sappend(s
, sjset_data_frame_1
);
2539 * If b3 is set, test b2, otherwise go to the first statement of
2540 * the rest of the program.
2542 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2543 sjset_data_frame_2
->s
.k
= 0x04;
2544 sappend(s
, sjset_data_frame_2
);
2545 sjset_data_frame_1
->s
.jf
= snext
;
2548 * If b2 is not set, this is a data frame; test the QoS bit.
2549 * Otherwise, go to the first statement of the rest of the
2552 sjset_data_frame_2
->s
.jt
= snext
;
2553 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2554 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2555 sappend(s
, sjset_qos
);
2558 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2560 * Otherwise, go to the first statement of the rest of the
2563 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2564 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2566 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2569 s2
= new_stmt(cstate
, BPF_ST
);
2570 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2574 * If we have a radiotap header, look at it to see whether
2575 * there's Atheros padding between the MAC-layer header
2578 * Note: all of the fields in the radiotap header are
2579 * little-endian, so we byte-swap all of the values
2580 * we test against, as they will be loaded as big-endian
2583 * XXX - in the general case, we would have to scan through
2584 * *all* the presence bits, if there's more than one word of
2585 * presence bits. That would require a loop, meaning that
2586 * we wouldn't be able to run the filter in the kernel.
2588 * We assume here that the Atheros adapters that insert the
2589 * annoying padding don't have multiple antennae and therefore
2590 * do not generate radiotap headers with multiple presence words.
2592 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2594 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2595 * in the first presence flag word?
2597 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2601 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2602 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2603 sappend(s
, sjset_radiotap_flags_present
);
2606 * If not, skip all of this.
2608 sjset_radiotap_flags_present
->s
.jf
= snext
;
2611 * Otherwise, is the "extension" bit set in that word?
2613 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2614 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2615 sappend(s
, sjset_radiotap_ext_present
);
2616 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2619 * If so, skip all of this.
2621 sjset_radiotap_ext_present
->s
.jt
= snext
;
2624 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2626 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2627 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2628 sappend(s
, sjset_radiotap_tsft_present
);
2629 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2632 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2633 * at an offset of 16 from the beginning of the raw packet
2634 * data (8 bytes for the radiotap header and 8 bytes for
2637 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2640 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2643 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2645 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2646 sjset_tsft_datapad
->s
.k
= 0x20;
2647 sappend(s
, sjset_tsft_datapad
);
2650 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2651 * at an offset of 8 from the beginning of the raw packet
2652 * data (8 bytes for the radiotap header).
2654 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2657 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2660 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2662 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2663 sjset_notsft_datapad
->s
.k
= 0x20;
2664 sappend(s
, sjset_notsft_datapad
);
2667 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2668 * set, round the length of the 802.11 header to
2669 * a multiple of 4. Do that by adding 3 and then
2670 * dividing by and multiplying by 4, which we do by
2673 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2674 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2675 sappend(s
, s_roundup
);
2676 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2679 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2682 s2
= new_stmt(cstate
, BPF_ST
);
2683 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2686 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2687 sjset_tsft_datapad
->s
.jf
= snext
;
2688 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2689 sjset_notsft_datapad
->s
.jf
= snext
;
2691 sjset_qos
->s
.jf
= snext
;
2697 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2701 /* There is an implicit dependency between the link
2702 * payload and link header since the payload computation
2703 * includes the variable part of the header. Therefore,
2704 * if nobody else has allocated a register for the link
2705 * header and we need it, do it now. */
2706 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2707 cstate
->off_linkhdr
.reg
== -1)
2708 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2711 * For link-layer types that have a variable-length header
2712 * preceding the link-layer header, generate code to load
2713 * the offset of the link-layer header into the register
2714 * assigned to that offset, if any.
2716 * XXX - this, and the next switch statement, won't handle
2717 * encapsulation of 802.11 or 802.11+radio information in
2718 * some other protocol stack. That's significantly more
2721 switch (cstate
->outermostlinktype
) {
2723 case DLT_PRISM_HEADER
:
2724 s
= gen_load_prism_llprefixlen(cstate
);
2727 case DLT_IEEE802_11_RADIO_AVS
:
2728 s
= gen_load_avs_llprefixlen(cstate
);
2731 case DLT_IEEE802_11_RADIO
:
2732 s
= gen_load_radiotap_llprefixlen(cstate
);
2736 s
= gen_load_ppi_llprefixlen(cstate
);
2745 * For link-layer types that have a variable-length link-layer
2746 * header, generate code to load the offset of the link-layer
2747 * payload into the register assigned to that offset, if any.
2749 switch (cstate
->outermostlinktype
) {
2751 case DLT_IEEE802_11
:
2752 case DLT_PRISM_HEADER
:
2753 case DLT_IEEE802_11_RADIO_AVS
:
2754 case DLT_IEEE802_11_RADIO
:
2756 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2761 * If we have any offset-loading code, append all the
2762 * existing statements in the block to those statements,
2763 * and make the resulting list the list of statements
2767 sappend(s
, b
->stmts
);
2772 static struct block
*
2773 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2775 struct slist
*s_load_dlt
;
2778 if (cstate
->linktype
== DLT_PPI
)
2780 /* Create the statements that check for the DLT
2782 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2783 s_load_dlt
->s
.k
= 4;
2785 b
= new_block(cstate
, JMP(BPF_JEQ
));
2787 b
->stmts
= s_load_dlt
;
2788 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2799 * Take an absolute offset, and:
2801 * if it has no variable part, return NULL;
2803 * if it has a variable part, generate code to load the register
2804 * containing that variable part into the X register, returning
2805 * a pointer to that code - if no register for that offset has
2806 * been allocated, allocate it first.
2808 * (The code to set that register will be generated later, but will
2809 * be placed earlier in the code sequence.)
2811 static struct slist
*
2812 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2816 if (off
->is_variable
) {
2817 if (off
->reg
== -1) {
2819 * We haven't yet assigned a register for the
2820 * variable part of the offset of the link-layer
2821 * header; allocate one.
2823 off
->reg
= alloc_reg(cstate
);
2827 * Load the register containing the variable part of the
2828 * offset of the link-layer header into the X register.
2830 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2835 * That offset isn't variable, there's no variable part,
2836 * so we don't need to generate any code.
2843 * Map an Ethernet type to the equivalent PPP type.
2846 ethertype_to_ppptype(proto
)
2855 case ETHERTYPE_IPV6
:
2863 case ETHERTYPE_ATALK
:
2877 * I'm assuming the "Bridging PDU"s that go
2878 * over PPP are Spanning Tree Protocol
2892 * Generate any tests that, for encapsulation of a link-layer packet
2893 * inside another protocol stack, need to be done to check for those
2894 * link-layer packets (and that haven't already been done by a check
2895 * for that encapsulation).
2897 static struct block
*
2898 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
2902 if (cstate
->is_geneve
)
2903 return gen_geneve_ll_check(cstate
);
2905 switch (cstate
->prevlinktype
) {
2909 * This is LANE-encapsulated Ethernet; check that the LANE
2910 * packet doesn't begin with an LE Control marker, i.e.
2911 * that it's data, not a control message.
2913 * (We've already generated a test for LANE.)
2915 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2921 * No such tests are necessary.
2929 * Generate code to match a particular packet type by matching the
2930 * link-layer type field or fields in the 802.2 LLC header.
2932 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2933 * value, if <= ETHERMTU.
2935 static struct block
*
2936 gen_linktype(compiler_state_t
*cstate
, int proto
)
2938 struct block
*b0
, *b1
, *b2
;
2939 const char *description
;
2941 /* are we checking MPLS-encapsulated packets? */
2942 if (cstate
->label_stack_depth
> 0) {
2946 /* FIXME add other L3 proto IDs */
2947 return gen_mpls_linktype(cstate
, Q_IP
);
2949 case ETHERTYPE_IPV6
:
2951 /* FIXME add other L3 proto IDs */
2952 return gen_mpls_linktype(cstate
, Q_IPV6
);
2955 bpf_error(cstate
, "unsupported protocol over mpls");
2960 switch (cstate
->linktype
) {
2963 case DLT_NETANALYZER
:
2964 case DLT_NETANALYZER_TRANSPARENT
:
2965 /* Geneve has an EtherType regardless of whether there is an
2967 if (!cstate
->is_geneve
)
2968 b0
= gen_prevlinkhdr_check(cstate
);
2972 b1
= gen_ether_linktype(cstate
, proto
);
2983 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2987 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2993 case DLT_IEEE802_11
:
2994 case DLT_PRISM_HEADER
:
2995 case DLT_IEEE802_11_RADIO_AVS
:
2996 case DLT_IEEE802_11_RADIO
:
2999 * Check that we have a data frame.
3001 b0
= gen_check_802_11_data_frame(cstate
);
3004 * Now check for the specified link-layer type.
3006 b1
= gen_llc_linktype(cstate
, proto
);
3014 * XXX - check for LLC frames.
3016 return gen_llc_linktype(cstate
, proto
);
3022 * XXX - check for LLC PDUs, as per IEEE 802.5.
3024 return gen_llc_linktype(cstate
, proto
);
3028 case DLT_ATM_RFC1483
:
3030 case DLT_IP_OVER_FC
:
3031 return gen_llc_linktype(cstate
, proto
);
3037 * Check for an LLC-encapsulated version of this protocol;
3038 * if we were checking for LANE, linktype would no longer
3041 * Check for LLC encapsulation and then check the protocol.
3043 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3044 b1
= gen_llc_linktype(cstate
, proto
);
3051 return gen_linux_sll_linktype(cstate
, proto
);
3056 case DLT_SLIP_BSDOS
:
3059 * These types don't provide any type field; packets
3060 * are always IPv4 or IPv6.
3062 * XXX - for IPv4, check for a version number of 4, and,
3063 * for IPv6, check for a version number of 6?
3068 /* Check for a version number of 4. */
3069 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3071 case ETHERTYPE_IPV6
:
3072 /* Check for a version number of 6. */
3073 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3076 return gen_false(cstate
); /* always false */
3083 * Raw IPv4, so no type field.
3085 if (proto
== ETHERTYPE_IP
)
3086 return gen_true(cstate
); /* always true */
3088 /* Checking for something other than IPv4; always false */
3089 return gen_false(cstate
);
3095 * Raw IPv6, so no type field.
3097 if (proto
== ETHERTYPE_IPV6
)
3098 return gen_true(cstate
); /* always true */
3100 /* Checking for something other than IPv6; always false */
3101 return gen_false(cstate
);
3107 case DLT_PPP_SERIAL
:
3110 * We use Ethernet protocol types inside libpcap;
3111 * map them to the corresponding PPP protocol types.
3113 proto
= ethertype_to_ppptype(proto
);
3114 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3120 * We use Ethernet protocol types inside libpcap;
3121 * map them to the corresponding PPP protocol types.
3127 * Also check for Van Jacobson-compressed IP.
3128 * XXX - do this for other forms of PPP?
3130 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3131 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3133 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3138 proto
= ethertype_to_ppptype(proto
);
3139 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3149 * For DLT_NULL, the link-layer header is a 32-bit
3150 * word containing an AF_ value in *host* byte order,
3151 * and for DLT_ENC, the link-layer header begins
3152 * with a 32-bit work containing an AF_ value in
3155 * In addition, if we're reading a saved capture file,
3156 * the host byte order in the capture may not be the
3157 * same as the host byte order on this machine.
3159 * For DLT_LOOP, the link-layer header is a 32-bit
3160 * word containing an AF_ value in *network* byte order.
3162 * XXX - AF_ values may, unfortunately, be platform-
3163 * dependent; for example, FreeBSD's AF_INET6 is 24
3164 * whilst NetBSD's and OpenBSD's is 26.
3166 * This means that, when reading a capture file, just
3167 * checking for our AF_INET6 value won't work if the
3168 * capture file came from another OS.
3177 case ETHERTYPE_IPV6
:
3184 * Not a type on which we support filtering.
3185 * XXX - support those that have AF_ values
3186 * #defined on this platform, at least?
3188 return gen_false(cstate
);
3191 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
3193 * The AF_ value is in host byte order, but
3194 * the BPF interpreter will convert it to
3195 * network byte order.
3197 * If this is a save file, and it's from a
3198 * machine with the opposite byte order to
3199 * ours, we byte-swap the AF_ value.
3201 * Then we run it through "htonl()", and
3202 * generate code to compare against the result.
3204 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
3205 proto
= SWAPLONG(proto
);
3206 proto
= htonl(proto
);
3208 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3210 #ifdef HAVE_NET_PFVAR_H
3213 * af field is host byte order in contrast to the rest of
3216 if (proto
== ETHERTYPE_IP
)
3217 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3218 BPF_B
, (bpf_int32
)AF_INET
));
3219 else if (proto
== ETHERTYPE_IPV6
)
3220 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3221 BPF_B
, (bpf_int32
)AF_INET6
));
3223 return gen_false(cstate
);
3226 #endif /* HAVE_NET_PFVAR_H */
3229 case DLT_ARCNET_LINUX
:
3231 * XXX should we check for first fragment if the protocol
3237 return gen_false(cstate
);
3239 case ETHERTYPE_IPV6
:
3240 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3241 (bpf_int32
)ARCTYPE_INET6
));
3244 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3245 (bpf_int32
)ARCTYPE_IP
);
3246 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3247 (bpf_int32
)ARCTYPE_IP_OLD
);
3252 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3253 (bpf_int32
)ARCTYPE_ARP
);
3254 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3255 (bpf_int32
)ARCTYPE_ARP_OLD
);
3259 case ETHERTYPE_REVARP
:
3260 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3261 (bpf_int32
)ARCTYPE_REVARP
));
3263 case ETHERTYPE_ATALK
:
3264 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3265 (bpf_int32
)ARCTYPE_ATALK
));
3272 case ETHERTYPE_ATALK
:
3273 return gen_true(cstate
);
3275 return gen_false(cstate
);
3282 * XXX - assumes a 2-byte Frame Relay header with
3283 * DLCI and flags. What if the address is longer?
3289 * Check for the special NLPID for IP.
3291 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3293 case ETHERTYPE_IPV6
:
3295 * Check for the special NLPID for IPv6.
3297 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3301 * Check for several OSI protocols.
3303 * Frame Relay packets typically have an OSI
3304 * NLPID at the beginning; we check for each
3307 * What we check for is the NLPID and a frame
3308 * control field of UI, i.e. 0x03 followed
3311 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3312 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3313 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3319 return gen_false(cstate
);
3325 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3327 case DLT_JUNIPER_MFR
:
3328 case DLT_JUNIPER_MLFR
:
3329 case DLT_JUNIPER_MLPPP
:
3330 case DLT_JUNIPER_ATM1
:
3331 case DLT_JUNIPER_ATM2
:
3332 case DLT_JUNIPER_PPPOE
:
3333 case DLT_JUNIPER_PPPOE_ATM
:
3334 case DLT_JUNIPER_GGSN
:
3335 case DLT_JUNIPER_ES
:
3336 case DLT_JUNIPER_MONITOR
:
3337 case DLT_JUNIPER_SERVICES
:
3338 case DLT_JUNIPER_ETHER
:
3339 case DLT_JUNIPER_PPP
:
3340 case DLT_JUNIPER_FRELAY
:
3341 case DLT_JUNIPER_CHDLC
:
3342 case DLT_JUNIPER_VP
:
3343 case DLT_JUNIPER_ST
:
3344 case DLT_JUNIPER_ISM
:
3345 case DLT_JUNIPER_VS
:
3346 case DLT_JUNIPER_SRX_E2E
:
3347 case DLT_JUNIPER_FIBRECHANNEL
:
3348 case DLT_JUNIPER_ATM_CEMIC
:
3350 /* just lets verify the magic number for now -
3351 * on ATM we may have up to 6 different encapsulations on the wire
3352 * and need a lot of heuristics to figure out that the payload
3355 * FIXME encapsulation specific BPF_ filters
3357 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3359 case DLT_BACNET_MS_TP
:
3360 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3363 return gen_ipnet_linktype(cstate
, proto
);
3365 case DLT_LINUX_IRDA
:
3366 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3369 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3372 case DLT_MTP2_WITH_PHDR
:
3373 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3376 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3379 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3381 case DLT_LINUX_LAPD
:
3382 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3384 case DLT_USB_FREEBSD
:
3386 case DLT_USB_LINUX_MMAPPED
:
3388 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3390 case DLT_BLUETOOTH_HCI_H4
:
3391 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3392 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3395 case DLT_CAN_SOCKETCAN
:
3396 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3398 case DLT_IEEE802_15_4
:
3399 case DLT_IEEE802_15_4_LINUX
:
3400 case DLT_IEEE802_15_4_NONASK_PHY
:
3401 case DLT_IEEE802_15_4_NOFCS
:
3402 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3404 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3405 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3408 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3411 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3414 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3417 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3420 /* Using the fixed-size NFLOG header it is possible to tell only
3421 * the address family of the packet, other meaningful data is
3422 * either missing or behind TLVs.
3424 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3428 * Does this link-layer header type have a field
3429 * indicating the type of the next protocol? If
3430 * so, off_linktype.constant_part will be the offset of that
3431 * field in the packet; if not, it will be -1.
3433 if (cstate
->off_linktype
.constant_part
!= (u_int
)-1) {
3435 * Yes; assume it's an Ethernet type. (If
3436 * it's not, it needs to be handled specially
3439 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3442 * No; report an error.
3444 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3445 if (description
!= NULL
) {
3446 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3449 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3458 * Check for an LLC SNAP packet with a given organization code and
3459 * protocol type; we check the entire contents of the 802.2 LLC and
3460 * snap headers, checking for DSAP and SSAP of SNAP and a control
3461 * field of 0x03 in the LLC header, and for the specified organization
3462 * code and protocol type in the SNAP header.
3464 static struct block
*
3465 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3467 u_char snapblock
[8];
3469 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3470 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3471 snapblock
[2] = 0x03; /* control = UI */
3472 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3473 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3474 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3475 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3476 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3477 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3481 * Generate code to match frames with an LLC header.
3484 gen_llc(compiler_state_t
*cstate
)
3486 struct block
*b0
, *b1
;
3488 switch (cstate
->linktype
) {
3492 * We check for an Ethernet type field less than
3493 * 1500, which means it's an 802.3 length field.
3495 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3499 * Now check for the purported DSAP and SSAP not being
3500 * 0xFF, to rule out NetWare-over-802.3.
3502 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3509 * We check for LLC traffic.
3511 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3514 case DLT_IEEE802
: /* Token Ring */
3516 * XXX - check for LLC frames.
3518 return gen_true(cstate
);
3522 * XXX - check for LLC frames.
3524 return gen_true(cstate
);
3526 case DLT_ATM_RFC1483
:
3528 * For LLC encapsulation, these are defined to have an
3531 * For VC encapsulation, they don't, but there's no
3532 * way to check for that; the protocol used on the VC
3533 * is negotiated out of band.
3535 return gen_true(cstate
);
3537 case DLT_IEEE802_11
:
3538 case DLT_PRISM_HEADER
:
3539 case DLT_IEEE802_11_RADIO
:
3540 case DLT_IEEE802_11_RADIO_AVS
:
3543 * Check that we have a data frame.
3545 b0
= gen_check_802_11_data_frame(cstate
);
3549 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3555 gen_llc_i(compiler_state_t
*cstate
)
3557 struct block
*b0
, *b1
;
3561 * Check whether this is an LLC frame.
3563 b0
= gen_llc(cstate
);
3566 * Load the control byte and test the low-order bit; it must
3567 * be clear for I frames.
3569 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3570 b1
= new_block(cstate
, JMP(BPF_JSET
));
3579 gen_llc_s(compiler_state_t
*cstate
)
3581 struct block
*b0
, *b1
;
3584 * Check whether this is an LLC frame.
3586 b0
= gen_llc(cstate
);
3589 * Now compare the low-order 2 bit of the control byte against
3590 * the appropriate value for S frames.
3592 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3598 gen_llc_u(compiler_state_t
*cstate
)
3600 struct block
*b0
, *b1
;
3603 * Check whether this is an LLC frame.
3605 b0
= gen_llc(cstate
);
3608 * Now compare the low-order 2 bit of the control byte against
3609 * the appropriate value for U frames.
3611 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3617 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3619 struct block
*b0
, *b1
;
3622 * Check whether this is an LLC frame.
3624 b0
= gen_llc(cstate
);
3627 * Now check for an S frame with the appropriate type.
3629 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3635 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3637 struct block
*b0
, *b1
;
3640 * Check whether this is an LLC frame.
3642 b0
= gen_llc(cstate
);
3645 * Now check for a U frame with the appropriate type.
3647 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3653 * Generate code to match a particular packet type, for link-layer types
3654 * using 802.2 LLC headers.
3656 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3657 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3659 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3660 * value, if <= ETHERMTU. We use that to determine whether to
3661 * match the DSAP or both DSAP and LSAP or to check the OUI and
3662 * protocol ID in a SNAP header.
3664 static struct block
*
3665 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3668 * XXX - handle token-ring variable-length header.
3674 case LLCSAP_NETBEUI
:
3676 * XXX - should we check both the DSAP and the
3677 * SSAP, like this, or should we check just the
3678 * DSAP, as we do for other SAP values?
3680 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3681 ((proto
<< 8) | proto
));
3685 * XXX - are there ever SNAP frames for IPX on
3686 * non-Ethernet 802.x networks?
3688 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3689 (bpf_int32
)LLCSAP_IPX
);
3691 case ETHERTYPE_ATALK
:
3693 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3694 * SNAP packets with an organization code of
3695 * 0x080007 (Apple, for Appletalk) and a protocol
3696 * type of ETHERTYPE_ATALK (Appletalk).
3698 * XXX - check for an organization code of
3699 * encapsulated Ethernet as well?
3701 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3705 * XXX - we don't have to check for IPX 802.3
3706 * here, but should we check for the IPX Ethertype?
3708 if (proto
<= ETHERMTU
) {
3710 * This is an LLC SAP value, so check
3713 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3716 * This is an Ethernet type; we assume that it's
3717 * unlikely that it'll appear in the right place
3718 * at random, and therefore check only the
3719 * location that would hold the Ethernet type
3720 * in a SNAP frame with an organization code of
3721 * 0x000000 (encapsulated Ethernet).
3723 * XXX - if we were to check for the SNAP DSAP and
3724 * LSAP, as per XXX, and were also to check for an
3725 * organization code of 0x000000 (encapsulated
3726 * Ethernet), we'd do
3728 * return gen_snap(cstate, 0x000000, proto);
3730 * here; for now, we don't, as per the above.
3731 * I don't know whether it's worth the extra CPU
3732 * time to do the right check or not.
3734 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3739 static struct block
*
3740 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3741 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3743 struct block
*b0
, *b1
;
3757 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3758 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3764 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3765 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3772 b0
= gen_linktype(cstate
, proto
);
3773 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3779 static struct block
*
3780 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3781 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3783 struct block
*b0
, *b1
;
3798 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3799 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3805 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3806 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3813 /* this order is important */
3814 a
= (u_int32_t
*)addr
;
3815 m
= (u_int32_t
*)mask
;
3816 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3817 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3819 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3821 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3823 b0
= gen_linktype(cstate
, proto
);
3829 static struct block
*
3830 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3832 register struct block
*b0
, *b1
;
3836 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
3839 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
3842 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3843 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3849 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3850 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3855 bpf_error(cstate
, "'addr1' is only supported on 802.11 with 802.11 headers");
3859 bpf_error(cstate
, "'addr2' is only supported on 802.11 with 802.11 headers");
3863 bpf_error(cstate
, "'addr3' is only supported on 802.11 with 802.11 headers");
3867 bpf_error(cstate
, "'addr4' is only supported on 802.11 with 802.11 headers");
3871 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
3875 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
3883 * Like gen_ehostop, but for DLT_FDDI
3885 static struct block
*
3886 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3888 struct block
*b0
, *b1
;
3892 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
3895 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
3898 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
3899 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
3905 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
3906 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
3911 bpf_error(cstate
, "'addr1' is only supported on 802.11");
3915 bpf_error(cstate
, "'addr2' is only supported on 802.11");
3919 bpf_error(cstate
, "'addr3' is only supported on 802.11");
3923 bpf_error(cstate
, "'addr4' is only supported on 802.11");
3927 bpf_error(cstate
, "'ra' is only supported on 802.11");
3931 bpf_error(cstate
, "'ta' is only supported on 802.11");
3939 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3941 static struct block
*
3942 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3944 register struct block
*b0
, *b1
;
3948 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
3951 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
3954 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
3955 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
3961 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
3962 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
3967 bpf_error(cstate
, "'addr1' is only supported on 802.11");
3971 bpf_error(cstate
, "'addr2' is only supported on 802.11");
3975 bpf_error(cstate
, "'addr3' is only supported on 802.11");
3979 bpf_error(cstate
, "'addr4' is only supported on 802.11");
3983 bpf_error(cstate
, "'ra' is only supported on 802.11");
3987 bpf_error(cstate
, "'ta' is only supported on 802.11");
3995 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3996 * various 802.11 + radio headers.
3998 static struct block
*
3999 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4001 register struct block
*b0
, *b1
, *b2
;
4002 register struct slist
*s
;
4004 #ifdef ENABLE_WLAN_FILTERING_PATCH
4007 * We need to disable the optimizer because the optimizer is buggy
4008 * and wipes out some LD instructions generated by the below
4009 * code to validate the Frame Control bits
4011 cstate
->no_optimize
= 1;
4012 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4019 * For control frames, there is no SA.
4021 * For management frames, SA is at an
4022 * offset of 10 from the beginning of
4025 * For data frames, SA is at an offset
4026 * of 10 from the beginning of the packet
4027 * if From DS is clear, at an offset of
4028 * 16 from the beginning of the packet
4029 * if From DS is set and To DS is clear,
4030 * and an offset of 24 from the beginning
4031 * of the packet if From DS is set and To DS
4036 * Generate the tests to be done for data frames
4039 * First, check for To DS set, i.e. check "link[1] & 0x01".
4041 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4042 b1
= new_block(cstate
, JMP(BPF_JSET
));
4043 b1
->s
.k
= 0x01; /* To DS */
4047 * If To DS is set, the SA is at 24.
4049 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4053 * Now, check for To DS not set, i.e. check
4054 * "!(link[1] & 0x01)".
4056 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4057 b2
= new_block(cstate
, JMP(BPF_JSET
));
4058 b2
->s
.k
= 0x01; /* To DS */
4063 * If To DS is not set, the SA is at 16.
4065 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4069 * Now OR together the last two checks. That gives
4070 * the complete set of checks for data frames with
4076 * Now check for From DS being set, and AND that with
4077 * the ORed-together checks.
4079 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4080 b1
= new_block(cstate
, JMP(BPF_JSET
));
4081 b1
->s
.k
= 0x02; /* From DS */
4086 * Now check for data frames with From DS not set.
4088 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4089 b2
= new_block(cstate
, JMP(BPF_JSET
));
4090 b2
->s
.k
= 0x02; /* From DS */
4095 * If From DS isn't set, the SA is at 10.
4097 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4101 * Now OR together the checks for data frames with
4102 * From DS not set and for data frames with From DS
4103 * set; that gives the checks done for data frames.
4108 * Now check for a data frame.
4109 * I.e, check "link[0] & 0x08".
4111 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4112 b1
= new_block(cstate
, JMP(BPF_JSET
));
4117 * AND that with the checks done for data frames.
4122 * If the high-order bit of the type value is 0, this
4123 * is a management frame.
4124 * I.e, check "!(link[0] & 0x08)".
4126 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4127 b2
= new_block(cstate
, JMP(BPF_JSET
));
4133 * For management frames, the SA is at 10.
4135 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4139 * OR that with the checks done for data frames.
4140 * That gives the checks done for management and
4146 * If the low-order bit of the type value is 1,
4147 * this is either a control frame or a frame
4148 * with a reserved type, and thus not a
4151 * I.e., check "!(link[0] & 0x04)".
4153 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4154 b1
= new_block(cstate
, JMP(BPF_JSET
));
4160 * AND that with the checks for data and management
4170 * For control frames, there is no DA.
4172 * For management frames, DA is at an
4173 * offset of 4 from the beginning of
4176 * For data frames, DA is at an offset
4177 * of 4 from the beginning of the packet
4178 * if To DS is clear and at an offset of
4179 * 16 from the beginning of the packet
4184 * Generate the tests to be done for data frames.
4186 * First, check for To DS set, i.e. "link[1] & 0x01".
4188 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4189 b1
= new_block(cstate
, JMP(BPF_JSET
));
4190 b1
->s
.k
= 0x01; /* To DS */
4194 * If To DS is set, the DA is at 16.
4196 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4200 * Now, check for To DS not set, i.e. check
4201 * "!(link[1] & 0x01)".
4203 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4204 b2
= new_block(cstate
, JMP(BPF_JSET
));
4205 b2
->s
.k
= 0x01; /* To DS */
4210 * If To DS is not set, the DA is at 4.
4212 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4216 * Now OR together the last two checks. That gives
4217 * the complete set of checks for data frames.
4222 * Now check for a data frame.
4223 * I.e, check "link[0] & 0x08".
4225 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4226 b1
= new_block(cstate
, JMP(BPF_JSET
));
4231 * AND that with the checks done for data frames.
4236 * If the high-order bit of the type value is 0, this
4237 * is a management frame.
4238 * I.e, check "!(link[0] & 0x08)".
4240 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4241 b2
= new_block(cstate
, JMP(BPF_JSET
));
4247 * For management frames, the DA is at 4.
4249 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4253 * OR that with the checks done for data frames.
4254 * That gives the checks done for management and
4260 * If the low-order bit of the type value is 1,
4261 * this is either a control frame or a frame
4262 * with a reserved type, and thus not a
4265 * I.e., check "!(link[0] & 0x04)".
4267 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4268 b1
= new_block(cstate
, JMP(BPF_JSET
));
4274 * AND that with the checks for data and management
4282 * Not present in management frames; addr1 in other
4287 * If the high-order bit of the type value is 0, this
4288 * is a management frame.
4289 * I.e, check "(link[0] & 0x08)".
4291 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4292 b1
= new_block(cstate
, JMP(BPF_JSET
));
4299 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4302 * AND that with the check of addr1.
4309 * Not present in management frames; addr2, if present,
4314 * Not present in CTS or ACK control frames.
4316 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4317 IEEE80211_FC0_TYPE_MASK
);
4319 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4320 IEEE80211_FC0_SUBTYPE_MASK
);
4322 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4323 IEEE80211_FC0_SUBTYPE_MASK
);
4329 * If the high-order bit of the type value is 0, this
4330 * is a management frame.
4331 * I.e, check "(link[0] & 0x08)".
4333 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4334 b1
= new_block(cstate
, JMP(BPF_JSET
));
4339 * AND that with the check for frames other than
4340 * CTS and ACK frames.
4347 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4352 * XXX - add BSSID keyword?
4355 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4359 * Not present in CTS or ACK control frames.
4361 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4362 IEEE80211_FC0_TYPE_MASK
);
4364 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4365 IEEE80211_FC0_SUBTYPE_MASK
);
4367 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4368 IEEE80211_FC0_SUBTYPE_MASK
);
4372 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4378 * Not present in control frames.
4380 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4381 IEEE80211_FC0_TYPE_MASK
);
4383 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4389 * Present only if the direction mask has both "From DS"
4390 * and "To DS" set. Neither control frames nor management
4391 * frames should have both of those set, so we don't
4392 * check the frame type.
4394 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4395 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4396 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4401 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4402 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4408 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4409 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4418 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4419 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4420 * as the RFC states.)
4422 static struct block
*
4423 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4425 register struct block
*b0
, *b1
;
4429 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4432 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4435 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4436 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4442 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4443 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4448 bpf_error(cstate
, "'addr1' is only supported on 802.11");
4452 bpf_error(cstate
, "'addr2' is only supported on 802.11");
4456 bpf_error(cstate
, "'addr3' is only supported on 802.11");
4460 bpf_error(cstate
, "'addr4' is only supported on 802.11");
4464 bpf_error(cstate
, "'ra' is only supported on 802.11");
4468 bpf_error(cstate
, "'ta' is only supported on 802.11");
4476 * This is quite tricky because there may be pad bytes in front of the
4477 * DECNET header, and then there are two possible data packet formats that
4478 * carry both src and dst addresses, plus 5 packet types in a format that
4479 * carries only the src node, plus 2 types that use a different format and
4480 * also carry just the src node.
4484 * Instead of doing those all right, we just look for data packets with
4485 * 0 or 1 bytes of padding. If you want to look at other packets, that
4486 * will require a lot more hacking.
4488 * To add support for filtering on DECNET "areas" (network numbers)
4489 * one would want to add a "mask" argument to this routine. That would
4490 * make the filter even more inefficient, although one could be clever
4491 * and not generate masking instructions if the mask is 0xFFFF.
4493 static struct block
*
4494 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4496 struct block
*b0
, *b1
, *b2
, *tmp
;
4497 u_int offset_lh
; /* offset if long header is received */
4498 u_int offset_sh
; /* offset if short header is received */
4503 offset_sh
= 1; /* follows flags */
4504 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4508 offset_sh
= 3; /* follows flags, dstnode */
4509 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4513 /* Inefficient because we do our Calvinball dance twice */
4514 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4515 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4521 /* Inefficient because we do our Calvinball dance twice */
4522 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4523 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4528 bpf_error(cstate
, "ISO host filtering not implemented");
4533 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4534 /* Check for pad = 1, long header case */
4535 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4536 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4537 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4538 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4540 /* Check for pad = 0, long header case */
4541 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4542 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4545 /* Check for pad = 1, short header case */
4546 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4547 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4548 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4551 /* Check for pad = 0, short header case */
4552 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4553 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4557 /* Combine with test for cstate->linktype */
4563 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4564 * test the bottom-of-stack bit, and then check the version number
4565 * field in the IP header.
4567 static struct block
*
4568 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4570 struct block
*b0
, *b1
;
4575 /* match the bottom-of-stack bit */
4576 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4577 /* match the IPv4 version number */
4578 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4583 /* match the bottom-of-stack bit */
4584 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4585 /* match the IPv4 version number */
4586 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4595 static struct block
*
4596 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4597 int proto
, int dir
, int type
)
4599 struct block
*b0
, *b1
;
4600 const char *typestr
;
4610 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4612 * Only check for non-IPv4 addresses if we're not
4613 * checking MPLS-encapsulated packets.
4615 if (cstate
->label_stack_depth
== 0) {
4616 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4618 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4624 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4627 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4630 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4633 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4636 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4639 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4642 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4645 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4648 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4651 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4654 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4657 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4660 bpf_error(cstate
, "ATALK host filtering not implemented");
4663 bpf_error(cstate
, "AARP host filtering not implemented");
4666 return gen_dnhostop(cstate
, addr
, dir
);
4669 bpf_error(cstate
, "SCA host filtering not implemented");
4672 bpf_error(cstate
, "LAT host filtering not implemented");
4675 bpf_error(cstate
, "MOPDL host filtering not implemented");
4678 bpf_error(cstate
, "MOPRC host filtering not implemented");
4681 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4684 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4687 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4690 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4693 bpf_error(cstate
, "ISO host filtering not implemented");
4696 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4699 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4702 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4705 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4708 bpf_error(cstate
, "IPX host filtering not implemented");
4711 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4714 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4723 static struct block
*
4724 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4725 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4727 const char *typestr
;
4737 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4740 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4743 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4746 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4749 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4752 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4755 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4758 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4761 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4764 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4767 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4770 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4773 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4776 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4779 bpf_error(cstate
, "ATALK host filtering not implemented");
4782 bpf_error(cstate
, "AARP host filtering not implemented");
4785 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
4788 bpf_error(cstate
, "SCA host filtering not implemented");
4791 bpf_error(cstate
, "LAT host filtering not implemented");
4794 bpf_error(cstate
, "MOPDL host filtering not implemented");
4797 bpf_error(cstate
, "MOPRC host filtering not implemented");
4800 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4803 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4806 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4809 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4812 bpf_error(cstate
, "ISO host filtering not implemented");
4815 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4818 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4821 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4824 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4827 bpf_error(cstate
, "IPX host filtering not implemented");
4830 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4833 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4843 static struct block
*
4844 gen_gateway(eaddr
, alist
, proto
, dir
)
4845 const u_char
*eaddr
;
4846 bpf_u_int32
**alist
;
4850 struct block
*b0
, *b1
, *tmp
;
4853 bpf_error(cstate
, "direction applied to 'gateway'");
4860 switch (cstate
->linktype
) {
4862 case DLT_NETANALYZER
:
4863 case DLT_NETANALYZER_TRANSPARENT
:
4864 b1
= gen_prevlinkhdr_check(cstate
);
4865 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
4870 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
4873 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
4875 case DLT_IEEE802_11
:
4876 case DLT_PRISM_HEADER
:
4877 case DLT_IEEE802_11_RADIO_AVS
:
4878 case DLT_IEEE802_11_RADIO
:
4880 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
4884 * This is LLC-multiplexed traffic; if it were
4885 * LANE, cstate->linktype would have been set to
4889 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4891 case DLT_IP_OVER_FC
:
4892 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
4896 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4898 b1
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4900 tmp
= gen_host(cstate
, **alist
++, 0xffffffff, proto
, Q_OR
,
4909 bpf_error(cstate
, "illegal modifier of 'gateway'");
4915 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
4923 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4924 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4929 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4930 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4935 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4936 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4941 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4944 #ifndef IPPROTO_IGMP
4945 #define IPPROTO_IGMP 2
4949 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4952 #ifndef IPPROTO_IGRP
4953 #define IPPROTO_IGRP 9
4956 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4960 #define IPPROTO_PIM 103
4964 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4965 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4969 #ifndef IPPROTO_VRRP
4970 #define IPPROTO_VRRP 112
4974 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4977 #ifndef IPPROTO_CARP
4978 #define IPPROTO_CARP 112
4982 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4986 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
4990 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
4994 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
4998 bpf_error(cstate
, "link layer applied in wrong context");
5001 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5005 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5009 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5013 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5017 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5021 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5025 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5029 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5032 #ifndef IPPROTO_ICMPV6
5033 #define IPPROTO_ICMPV6 58
5036 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5040 #define IPPROTO_AH 51
5043 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5044 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5049 #define IPPROTO_ESP 50
5052 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5053 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5058 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5062 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5066 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5069 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5070 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5071 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5073 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5075 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5077 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5081 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5082 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5083 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5085 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5087 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5089 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5093 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5094 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5095 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5097 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5102 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5103 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5108 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5109 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5111 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5113 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5118 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5119 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5124 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5125 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5130 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5134 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5138 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5142 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5146 bpf_error(cstate
, "'radio' is not a valid protocol type");
5154 static struct block
*
5155 gen_ipfrag(compiler_state_t
*cstate
)
5160 /* not IPv4 frag other than the first frag */
5161 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5162 b
= new_block(cstate
, JMP(BPF_JSET
));
5171 * Generate a comparison to a port value in the transport-layer header
5172 * at the specified offset from the beginning of that header.
5174 * XXX - this handles a variable-length prefix preceding the link-layer
5175 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5176 * variable-length link-layer headers (such as Token Ring or 802.11
5179 static struct block
*
5180 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5182 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5185 static struct block
*
5186 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5188 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5192 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5194 struct block
*b0
, *b1
, *tmp
;
5196 /* ip proto 'proto' and not a fragment other than the first fragment */
5197 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5198 b0
= gen_ipfrag(cstate
);
5203 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5207 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5212 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5213 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5218 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5219 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5231 static struct block
*
5232 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5234 struct block
*b0
, *b1
, *tmp
;
5239 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5240 * not LLC encapsulation with LLCSAP_IP.
5242 * For IEEE 802 networks - which includes 802.5 token ring
5243 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5244 * says that SNAP encapsulation is used, not LLC encapsulation
5247 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5248 * RFC 2225 say that SNAP encapsulation is used, not LLC
5249 * encapsulation with LLCSAP_IP.
5251 * So we always check for ETHERTYPE_IP.
5253 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5259 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5263 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5264 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5266 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5278 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5280 struct block
*b0
, *b1
, *tmp
;
5282 /* ip6 proto 'proto' */
5283 /* XXX - catch the first fragment of a fragmented packet? */
5284 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5288 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5292 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5297 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5298 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5303 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5304 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5316 static struct block
*
5317 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5319 struct block
*b0
, *b1
, *tmp
;
5321 /* link proto ip6 */
5322 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5328 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5332 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5333 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5335 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5346 /* gen_portrange code */
5347 static struct block
*
5348 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5351 struct block
*b1
, *b2
;
5355 * Reverse the order of the ports, so v1 is the lower one.
5364 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5365 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5373 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5376 struct block
*b0
, *b1
, *tmp
;
5378 /* ip proto 'proto' and not a fragment other than the first fragment */
5379 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5380 b0
= gen_ipfrag(cstate
);
5385 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5389 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5394 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5395 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5400 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5401 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5413 static struct block
*
5414 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5417 struct block
*b0
, *b1
, *tmp
;
5420 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5426 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5430 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5431 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5433 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5444 static struct block
*
5445 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5448 struct block
*b1
, *b2
;
5452 * Reverse the order of the ports, so v1 is the lower one.
5461 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5462 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5470 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5473 struct block
*b0
, *b1
, *tmp
;
5475 /* ip6 proto 'proto' */
5476 /* XXX - catch the first fragment of a fragmented packet? */
5477 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5481 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5485 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5490 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5491 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5496 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5497 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5509 static struct block
*
5510 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5513 struct block
*b0
, *b1
, *tmp
;
5515 /* link proto ip6 */
5516 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5522 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5526 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5527 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5529 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5541 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5550 v
= pcap_nametoproto(name
);
5551 if (v
== PROTO_UNDEF
)
5552 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5556 /* XXX should look up h/w protocol type based on cstate->linktype */
5557 v
= pcap_nametoeproto(name
);
5558 if (v
== PROTO_UNDEF
) {
5559 v
= pcap_nametollc(name
);
5560 if (v
== PROTO_UNDEF
)
5561 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5566 if (strcmp(name
, "esis") == 0)
5568 else if (strcmp(name
, "isis") == 0)
5570 else if (strcmp(name
, "clnp") == 0)
5573 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5593 static struct block
*
5594 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5596 #ifdef NO_PROTOCHAIN
5597 return gen_proto(cstate
, v
, proto
, dir
);
5599 struct block
*b0
, *b
;
5600 struct slist
*s
[100];
5601 int fix2
, fix3
, fix4
, fix5
;
5602 int ahcheck
, again
, end
;
5604 int reg2
= alloc_reg(cstate
);
5606 memset(s
, 0, sizeof(s
));
5607 fix3
= fix4
= fix5
= 0;
5614 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5615 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5619 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5624 * We don't handle variable-length prefixes before the link-layer
5625 * header, or variable-length link-layer headers, here yet.
5626 * We might want to add BPF instructions to do the protochain
5627 * work, to simplify that and, on platforms that have a BPF
5628 * interpreter with the new instructions, let the filtering
5629 * be done in the kernel. (We already require a modified BPF
5630 * engine to do the protochain stuff, to support backward
5631 * branches, and backward branch support is unlikely to appear
5632 * in kernel BPF engines.)
5634 if (cstate
->off_linkpl
.is_variable
)
5635 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5637 cstate
->no_optimize
= 1; /*this code is not compatible with optimzer yet */
5640 * s[0] is a dummy entry to protect other BPF insn from damage
5641 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5642 * hard to find interdependency made by jump table fixup.
5645 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5650 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5653 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5654 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5656 /* X = ip->ip_hl << 2 */
5657 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5658 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5663 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5665 /* A = ip6->ip_nxt */
5666 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5667 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
5669 /* X = sizeof(struct ip6_hdr) */
5670 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
5676 bpf_error(cstate
, "unsupported proto to gen_protochain");
5680 /* again: if (A == v) goto end; else fall through; */
5682 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5684 s
[i
]->s
.jt
= NULL
; /*later*/
5685 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5689 #ifndef IPPROTO_NONE
5690 #define IPPROTO_NONE 59
5692 /* if (A == IPPROTO_NONE) goto end */
5693 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5694 s
[i
]->s
.jt
= NULL
; /*later*/
5695 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5696 s
[i
]->s
.k
= IPPROTO_NONE
;
5697 s
[fix5
]->s
.jf
= s
[i
];
5701 if (proto
== Q_IPV6
) {
5702 int v6start
, v6end
, v6advance
, j
;
5705 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5706 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5707 s
[i
]->s
.jt
= NULL
; /*later*/
5708 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5709 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5710 s
[fix2
]->s
.jf
= s
[i
];
5712 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5713 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5714 s
[i
]->s
.jt
= NULL
; /*later*/
5715 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5716 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5718 /* if (A == IPPROTO_ROUTING) goto v6advance */
5719 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5720 s
[i
]->s
.jt
= NULL
; /*later*/
5721 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5722 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5724 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5725 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5726 s
[i
]->s
.jt
= NULL
; /*later*/
5727 s
[i
]->s
.jf
= NULL
; /*later*/
5728 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5738 * A = P[X + packet head];
5739 * X = X + (P[X + packet head + 1] + 1) * 8;
5741 /* A = P[X + packet head] */
5742 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5743 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5746 s
[i
] = new_stmt(cstate
, BPF_ST
);
5749 /* A = P[X + packet head + 1]; */
5750 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5751 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
5754 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5758 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5762 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
5766 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5769 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5773 /* goto again; (must use BPF_JA for backward jump) */
5774 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5775 s
[i
]->s
.k
= again
- i
- 1;
5776 s
[i
- 1]->s
.jf
= s
[i
];
5780 for (j
= v6start
; j
<= v6end
; j
++)
5781 s
[j
]->s
.jt
= s
[v6advance
];
5784 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5786 s
[fix2
]->s
.jf
= s
[i
];
5792 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5793 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5794 s
[i
]->s
.jt
= NULL
; /*later*/
5795 s
[i
]->s
.jf
= NULL
; /*later*/
5796 s
[i
]->s
.k
= IPPROTO_AH
;
5798 s
[fix3
]->s
.jf
= s
[ahcheck
];
5805 * X = X + (P[X + 1] + 2) * 4;
5808 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5810 /* A = P[X + packet head]; */
5811 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5812 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5815 s
[i
] = new_stmt(cstate
, BPF_ST
);
5819 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5822 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5826 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5828 /* A = P[X + packet head] */
5829 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5830 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5833 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5837 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5841 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5844 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5848 /* goto again; (must use BPF_JA for backward jump) */
5849 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5850 s
[i
]->s
.k
= again
- i
- 1;
5855 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5857 s
[fix2
]->s
.jt
= s
[end
];
5858 s
[fix4
]->s
.jf
= s
[end
];
5859 s
[fix5
]->s
.jt
= s
[end
];
5866 for (i
= 0; i
< max
- 1; i
++)
5867 s
[i
]->next
= s
[i
+ 1];
5868 s
[max
- 1]->next
= NULL
;
5873 b
= new_block(cstate
, JMP(BPF_JEQ
));
5874 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5877 free_reg(cstate
, reg2
);
5884 static struct block
*
5885 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
5888 struct block
*b0
, *b1
;
5891 * A data frame has the 0x08 bit (b3) in the frame control field set
5892 * and the 0x04 bit (b2) clear.
5894 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
5895 b0
= new_block(cstate
, JMP(BPF_JSET
));
5899 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
5900 b1
= new_block(cstate
, JMP(BPF_JSET
));
5911 * Generate code that checks whether the packet is a packet for protocol
5912 * <proto> and whether the type field in that protocol's header has
5913 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5914 * IP packet and checks the protocol number in the IP header against <v>.
5916 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5917 * against Q_IP and Q_IPV6.
5919 static struct block
*
5920 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5922 struct block
*b0
, *b1
;
5927 if (dir
!= Q_DEFAULT
)
5928 bpf_error(cstate
, "direction applied to 'proto'");
5932 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
5933 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
5939 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5940 * not LLC encapsulation with LLCSAP_IP.
5942 * For IEEE 802 networks - which includes 802.5 token ring
5943 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5944 * says that SNAP encapsulation is used, not LLC encapsulation
5947 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5948 * RFC 2225 say that SNAP encapsulation is used, not LLC
5949 * encapsulation with LLCSAP_IP.
5951 * So we always check for ETHERTYPE_IP.
5953 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5955 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5957 b1
= gen_protochain(cstate
, v
, Q_IP
);
5963 switch (cstate
->linktype
) {
5967 * Frame Relay packets typically have an OSI
5968 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
5969 * generates code to check for all the OSI
5970 * NLPIDs, so calling it and then adding a check
5971 * for the particular NLPID for which we're
5972 * looking is bogus, as we can just check for
5975 * What we check for is the NLPID and a frame
5976 * control field value of UI, i.e. 0x03 followed
5979 * XXX - assumes a 2-byte Frame Relay header with
5980 * DLCI and flags. What if the address is longer?
5982 * XXX - what about SNAP-encapsulated frames?
5984 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5990 * Cisco uses an Ethertype lookalike - for OSI,
5993 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5994 /* OSI in C-HDLC is stuffed with a fudge byte */
5995 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6000 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6001 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6007 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6009 * 4 is the offset of the PDU type relative to the IS-IS
6012 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6017 bpf_error(cstate
, "arp does not encapsulate another protocol");
6021 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6025 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6029 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6033 bpf_error(cstate
, "sca does not encapsulate another protocol");
6037 bpf_error(cstate
, "lat does not encapsulate another protocol");
6041 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6045 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6049 return gen_linktype(cstate
, v
);
6052 bpf_error(cstate
, "'udp proto' is bogus");
6056 bpf_error(cstate
, "'tcp proto' is bogus");
6060 bpf_error(cstate
, "'sctp proto' is bogus");
6064 bpf_error(cstate
, "'icmp proto' is bogus");
6068 bpf_error(cstate
, "'igmp proto' is bogus");
6072 bpf_error(cstate
, "'igrp proto' is bogus");
6076 bpf_error(cstate
, "'pim proto' is bogus");
6080 bpf_error(cstate
, "'vrrp proto' is bogus");
6084 bpf_error(cstate
, "'carp proto' is bogus");
6088 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6091 * Also check for a fragment header before the final
6094 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6095 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6097 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6100 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6106 bpf_error(cstate
, "'icmp6 proto' is bogus");
6109 bpf_error(cstate
, "'ah proto' is bogus");
6112 bpf_error(cstate
, "'ah proto' is bogus");
6115 bpf_error(cstate
, "'stp proto' is bogus");
6118 bpf_error(cstate
, "'ipx proto' is bogus");
6121 bpf_error(cstate
, "'netbeui proto' is bogus");
6124 bpf_error(cstate
, "'radio proto' is bogus");
6134 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6136 int proto
= q
.proto
;
6140 bpf_u_int32 mask
, addr
;
6142 bpf_u_int32
**alist
;
6145 struct sockaddr_in
*sin4
;
6146 struct sockaddr_in6
*sin6
;
6147 struct addrinfo
*res
, *res0
;
6148 struct in6_addr mask128
;
6150 struct block
*b
, *tmp
;
6151 int port
, real_proto
;
6157 addr
= pcap_nametonetaddr(name
);
6159 bpf_error(cstate
, "unknown network '%s'", name
);
6160 /* Left justify network addr and calculate its network mask */
6162 while (addr
&& (addr
& 0xff000000) == 0) {
6166 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6170 if (proto
== Q_LINK
) {
6171 switch (cstate
->linktype
) {
6174 case DLT_NETANALYZER
:
6175 case DLT_NETANALYZER_TRANSPARENT
:
6176 eaddr
= pcap_ether_hostton(name
);
6179 "unknown ether host '%s'", name
);
6180 tmp
= gen_prevlinkhdr_check(cstate
);
6181 b
= gen_ehostop(cstate
, eaddr
, dir
);
6188 eaddr
= pcap_ether_hostton(name
);
6191 "unknown FDDI host '%s'", name
);
6192 b
= gen_fhostop(cstate
, eaddr
, dir
);
6197 eaddr
= pcap_ether_hostton(name
);
6200 "unknown token ring host '%s'", name
);
6201 b
= gen_thostop(cstate
, eaddr
, dir
);
6205 case DLT_IEEE802_11
:
6206 case DLT_PRISM_HEADER
:
6207 case DLT_IEEE802_11_RADIO_AVS
:
6208 case DLT_IEEE802_11_RADIO
:
6210 eaddr
= pcap_ether_hostton(name
);
6213 "unknown 802.11 host '%s'", name
);
6214 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6218 case DLT_IP_OVER_FC
:
6219 eaddr
= pcap_ether_hostton(name
);
6222 "unknown Fibre Channel host '%s'", name
);
6223 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6228 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6229 } else if (proto
== Q_DECNET
) {
6230 unsigned short dn_addr
;
6232 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6234 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6236 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6241 * I don't think DECNET hosts can be multihomed, so
6242 * there is no need to build up a list of addresses
6244 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6247 alist
= pcap_nametoaddr(name
);
6248 if (alist
== NULL
|| *alist
== NULL
)
6249 bpf_error(cstate
, "unknown host '%s'", name
);
6251 if (cstate
->off_linktype
.constant_part
== (u_int
)-1 &&
6252 tproto
== Q_DEFAULT
)
6254 b
= gen_host(cstate
, **alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6256 tmp
= gen_host(cstate
, **alist
++, 0xffffffff,
6257 tproto
, dir
, q
.addr
);
6263 memset(&mask128
, 0xff, sizeof(mask128
));
6264 res0
= res
= pcap_nametoaddrinfo(name
);
6266 bpf_error(cstate
, "unknown host '%s'", name
);
6269 tproto
= tproto6
= proto
;
6270 if (cstate
->off_linktype
.constant_part
== -1 &&
6271 tproto
== Q_DEFAULT
) {
6275 for (res
= res0
; res
; res
= res
->ai_next
) {
6276 switch (res
->ai_family
) {
6278 if (tproto
== Q_IPV6
)
6281 sin4
= (struct sockaddr_in
*)
6283 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6284 0xffffffff, tproto
, dir
, q
.addr
);
6287 if (tproto6
== Q_IP
)
6290 sin6
= (struct sockaddr_in6
*)
6292 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6293 &mask128
, tproto6
, dir
, q
.addr
);
6305 bpf_error(cstate
, "unknown host '%s'%s", name
,
6306 (proto
== Q_DEFAULT
)
6308 : " for specified address family");
6315 if (proto
!= Q_DEFAULT
&&
6316 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6317 bpf_error(cstate
, "illegal qualifier of 'port'");
6318 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6319 bpf_error(cstate
, "unknown port '%s'", name
);
6320 if (proto
== Q_UDP
) {
6321 if (real_proto
== IPPROTO_TCP
)
6322 bpf_error(cstate
, "port '%s' is tcp", name
);
6323 else if (real_proto
== IPPROTO_SCTP
)
6324 bpf_error(cstate
, "port '%s' is sctp", name
);
6326 /* override PROTO_UNDEF */
6327 real_proto
= IPPROTO_UDP
;
6329 if (proto
== Q_TCP
) {
6330 if (real_proto
== IPPROTO_UDP
)
6331 bpf_error(cstate
, "port '%s' is udp", name
);
6333 else if (real_proto
== IPPROTO_SCTP
)
6334 bpf_error(cstate
, "port '%s' is sctp", name
);
6336 /* override PROTO_UNDEF */
6337 real_proto
= IPPROTO_TCP
;
6339 if (proto
== Q_SCTP
) {
6340 if (real_proto
== IPPROTO_UDP
)
6341 bpf_error(cstate
, "port '%s' is udp", name
);
6343 else if (real_proto
== IPPROTO_TCP
)
6344 bpf_error(cstate
, "port '%s' is tcp", name
);
6346 /* override PROTO_UNDEF */
6347 real_proto
= IPPROTO_SCTP
;
6350 bpf_error(cstate
, "illegal port number %d < 0", port
);
6352 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6353 b
= gen_port(cstate
, port
, real_proto
, dir
);
6354 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6358 if (proto
!= Q_DEFAULT
&&
6359 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6360 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6361 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6362 bpf_error(cstate
, "unknown port in range '%s'", name
);
6363 if (proto
== Q_UDP
) {
6364 if (real_proto
== IPPROTO_TCP
)
6365 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6366 else if (real_proto
== IPPROTO_SCTP
)
6367 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6369 /* override PROTO_UNDEF */
6370 real_proto
= IPPROTO_UDP
;
6372 if (proto
== Q_TCP
) {
6373 if (real_proto
== IPPROTO_UDP
)
6374 bpf_error(cstate
, "port in range '%s' is udp", name
);
6375 else if (real_proto
== IPPROTO_SCTP
)
6376 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6378 /* override PROTO_UNDEF */
6379 real_proto
= IPPROTO_TCP
;
6381 if (proto
== Q_SCTP
) {
6382 if (real_proto
== IPPROTO_UDP
)
6383 bpf_error(cstate
, "port in range '%s' is udp", name
);
6384 else if (real_proto
== IPPROTO_TCP
)
6385 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6387 /* override PROTO_UNDEF */
6388 real_proto
= IPPROTO_SCTP
;
6391 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6393 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6395 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6397 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6399 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6400 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6405 eaddr
= pcap_ether_hostton(name
);
6407 bpf_error(cstate
, "unknown ether host: %s", name
);
6409 alist
= pcap_nametoaddr(name
);
6410 if (alist
== NULL
|| *alist
== NULL
)
6411 bpf_error(cstate
, "unknown host '%s'", name
);
6412 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6416 bpf_error(cstate
, "'gateway' not supported in this configuration");
6420 real_proto
= lookup_proto(cstate
, name
, proto
);
6421 if (real_proto
>= 0)
6422 return gen_proto(cstate
, real_proto
, proto
, dir
);
6424 bpf_error(cstate
, "unknown protocol: %s", name
);
6427 real_proto
= lookup_proto(cstate
, name
, proto
);
6428 if (real_proto
>= 0)
6429 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6431 bpf_error(cstate
, "unknown protocol: %s", name
);
6442 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6443 unsigned int masklen
, struct qual q
)
6445 register int nlen
, mlen
;
6448 nlen
= __pcap_atoin(s1
, &n
);
6449 /* Promote short ipaddr */
6453 mlen
= __pcap_atoin(s2
, &m
);
6454 /* Promote short ipaddr */
6457 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6460 /* Convert mask len to mask */
6462 bpf_error(cstate
, "mask length must be <= 32");
6465 * X << 32 is not guaranteed by C to be 0; it's
6470 m
= 0xffffffff << (32 - masklen
);
6472 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6479 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6482 bpf_error(cstate
, "Mask syntax for networks only");
6490 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6493 int proto
= q
.proto
;
6499 else if (q
.proto
== Q_DECNET
) {
6500 vlen
= __pcap_atodn(s
, &v
);
6502 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6504 vlen
= __pcap_atoin(s
, &v
);
6511 if (proto
== Q_DECNET
)
6512 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6513 else if (proto
== Q_LINK
) {
6514 bpf_error(cstate
, "illegal link layer address");
6517 if (s
== NULL
&& q
.addr
== Q_NET
) {
6518 /* Promote short net number */
6519 while (v
&& (v
& 0xff000000) == 0) {
6524 /* Promote short ipaddr */
6526 mask
<<= 32 - vlen
;
6528 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6533 proto
= IPPROTO_UDP
;
6534 else if (proto
== Q_TCP
)
6535 proto
= IPPROTO_TCP
;
6536 else if (proto
== Q_SCTP
)
6537 proto
= IPPROTO_SCTP
;
6538 else if (proto
== Q_DEFAULT
)
6539 proto
= PROTO_UNDEF
;
6541 bpf_error(cstate
, "illegal qualifier of 'port'");
6544 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6548 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6549 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6555 proto
= IPPROTO_UDP
;
6556 else if (proto
== Q_TCP
)
6557 proto
= IPPROTO_TCP
;
6558 else if (proto
== Q_SCTP
)
6559 proto
= IPPROTO_SCTP
;
6560 else if (proto
== Q_DEFAULT
)
6561 proto
= PROTO_UNDEF
;
6563 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6566 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6570 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6571 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6576 bpf_error(cstate
, "'gateway' requires a name");
6580 return gen_proto(cstate
, (int)v
, proto
, dir
);
6583 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6598 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6599 unsigned int masklen
, struct qual q
)
6601 struct addrinfo
*res
;
6602 struct in6_addr
*addr
;
6603 struct in6_addr mask
;
6608 bpf_error(cstate
, "no mask %s supported", s2
);
6610 res
= pcap_nametoaddrinfo(s1
);
6612 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6615 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6616 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6618 if (sizeof(mask
) * 8 < masklen
)
6619 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6620 memset(&mask
, 0, sizeof(mask
));
6621 memset(&mask
, 0xff, masklen
/ 8);
6623 mask
.s6_addr
[masklen
/ 8] =
6624 (0xff << (8 - masklen
% 8)) & 0xff;
6627 a
= (u_int32_t
*)addr
;
6628 m
= (u_int32_t
*)&mask
;
6629 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6630 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6631 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6639 bpf_error(cstate
, "Mask syntax for networks only");
6643 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6649 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6657 gen_ecode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
6659 struct block
*b
, *tmp
;
6661 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6662 switch (cstate
->linktype
) {
6664 case DLT_NETANALYZER
:
6665 case DLT_NETANALYZER_TRANSPARENT
:
6666 tmp
= gen_prevlinkhdr_check(cstate
);
6667 b
= gen_ehostop(cstate
, eaddr
, (int)q
.dir
);
6672 return gen_fhostop(cstate
, eaddr
, (int)q
.dir
);
6674 return gen_thostop(cstate
, eaddr
, (int)q
.dir
);
6675 case DLT_IEEE802_11
:
6676 case DLT_PRISM_HEADER
:
6677 case DLT_IEEE802_11_RADIO_AVS
:
6678 case DLT_IEEE802_11_RADIO
:
6680 return gen_wlanhostop(cstate
, eaddr
, (int)q
.dir
);
6681 case DLT_IP_OVER_FC
:
6682 return gen_ipfchostop(cstate
, eaddr
, (int)q
.dir
);
6684 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6688 bpf_error(cstate
, "ethernet address used in non-ether expression");
6695 struct slist
*s0
, *s1
;
6698 * This is definitely not the best way to do this, but the
6699 * lists will rarely get long.
6706 static struct slist
*
6707 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
6711 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
6716 static struct slist
*
6717 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
6721 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6727 * Modify "index" to use the value stored into its register as an
6728 * offset relative to the beginning of the header for the protocol
6729 * "proto", and allocate a register and put an item "size" bytes long
6730 * (1, 2, or 4) at that offset into that register, making it the register
6734 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
6736 struct slist
*s
, *tmp
;
6738 int regno
= alloc_reg(cstate
);
6740 free_reg(cstate
, inst
->regno
);
6744 bpf_error(cstate
, "data size must be 1, 2, or 4");
6760 bpf_error(cstate
, "unsupported index operation");
6764 * The offset is relative to the beginning of the packet
6765 * data, if we have a radio header. (If we don't, this
6768 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6769 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
6770 cstate
->linktype
!= DLT_PRISM_HEADER
)
6771 bpf_error(cstate
, "radio information not present in capture");
6774 * Load into the X register the offset computed into the
6775 * register specified by "index".
6777 s
= xfer_to_x(cstate
, inst
);
6780 * Load the item at that offset.
6782 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6784 sappend(inst
->s
, s
);
6789 * The offset is relative to the beginning of
6790 * the link-layer header.
6792 * XXX - what about ATM LANE? Should the index be
6793 * relative to the beginning of the AAL5 frame, so
6794 * that 0 refers to the beginning of the LE Control
6795 * field, or relative to the beginning of the LAN
6796 * frame, so that 0 refers, for Ethernet LANE, to
6797 * the beginning of the destination address?
6799 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
6802 * If "s" is non-null, it has code to arrange that the
6803 * X register contains the length of the prefix preceding
6804 * the link-layer header. Add to it the offset computed
6805 * into the register specified by "index", and move that
6806 * into the X register. Otherwise, just load into the X
6807 * register the offset computed into the register specified
6811 sappend(s
, xfer_to_a(cstate
, inst
));
6812 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6813 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6815 s
= xfer_to_x(cstate
, inst
);
6818 * Load the item at the sum of the offset we've put in the
6819 * X register and the offset of the start of the link
6820 * layer header (which is 0 if the radio header is
6821 * variable-length; that header length is what we put
6822 * into the X register and then added to the index).
6824 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6825 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
6827 sappend(inst
->s
, s
);
6841 * The offset is relative to the beginning of
6842 * the network-layer header.
6843 * XXX - are there any cases where we want
6844 * cstate->off_nl_nosnap?
6846 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
6849 * If "s" is non-null, it has code to arrange that the
6850 * X register contains the variable part of the offset
6851 * of the link-layer payload. Add to it the offset
6852 * computed into the register specified by "index",
6853 * and move that into the X register. Otherwise, just
6854 * load into the X register the offset computed into
6855 * the register specified by "index".
6858 sappend(s
, xfer_to_a(cstate
, inst
));
6859 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6860 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6862 s
= xfer_to_x(cstate
, inst
);
6865 * Load the item at the sum of the offset we've put in the
6866 * X register, the offset of the start of the network
6867 * layer header from the beginning of the link-layer
6868 * payload, and the constant part of the offset of the
6869 * start of the link-layer payload.
6871 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6872 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6874 sappend(inst
->s
, s
);
6877 * Do the computation only if the packet contains
6878 * the protocol in question.
6880 b
= gen_proto_abbrev(cstate
, proto
);
6882 gen_and(inst
->b
, b
);
6896 * The offset is relative to the beginning of
6897 * the transport-layer header.
6899 * Load the X register with the length of the IPv4 header
6900 * (plus the offset of the link-layer header, if it's
6901 * a variable-length header), in bytes.
6903 * XXX - are there any cases where we want
6904 * cstate->off_nl_nosnap?
6905 * XXX - we should, if we're built with
6906 * IPv6 support, generate code to load either
6907 * IPv4, IPv6, or both, as appropriate.
6909 s
= gen_loadx_iphdrlen(cstate
);
6912 * The X register now contains the sum of the variable
6913 * part of the offset of the link-layer payload and the
6914 * length of the network-layer header.
6916 * Load into the A register the offset relative to
6917 * the beginning of the transport layer header,
6918 * add the X register to that, move that to the
6919 * X register, and load with an offset from the
6920 * X register equal to the sum of the constant part of
6921 * the offset of the link-layer payload and the offset,
6922 * relative to the beginning of the link-layer payload,
6923 * of the network-layer header.
6925 sappend(s
, xfer_to_a(cstate
, inst
));
6926 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6927 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6928 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
6929 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6930 sappend(inst
->s
, s
);
6933 * Do the computation only if the packet contains
6934 * the protocol in question - which is true only
6935 * if this is an IP datagram and is the first or
6936 * only fragment of that datagram.
6938 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
6940 gen_and(inst
->b
, b
);
6941 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
6945 bpf_error(cstate
, "IPv6 upper-layer protocol is not supported by proto[x]");
6948 inst
->regno
= regno
;
6949 s
= new_stmt(cstate
, BPF_ST
);
6951 sappend(inst
->s
, s
);
6957 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
6958 struct arth
*a1
, int reversed
)
6960 struct slist
*s0
, *s1
, *s2
;
6961 struct block
*b
, *tmp
;
6963 s0
= xfer_to_x(cstate
, a1
);
6964 s1
= xfer_to_a(cstate
, a0
);
6965 if (code
== BPF_JEQ
) {
6966 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
6967 b
= new_block(cstate
, JMP(code
));
6971 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
6977 sappend(a0
->s
, a1
->s
);
6981 free_reg(cstate
, a0
->regno
);
6982 free_reg(cstate
, a1
->regno
);
6984 /* 'and' together protocol checks */
6987 gen_and(a0
->b
, tmp
= a1
->b
);
7001 gen_loadlen(compiler_state_t
*cstate
)
7003 int regno
= alloc_reg(cstate
);
7004 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7007 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7008 s
->next
= new_stmt(cstate
, BPF_ST
);
7009 s
->next
->s
.k
= regno
;
7017 gen_loadi(compiler_state_t
*cstate
, int val
)
7023 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7025 reg
= alloc_reg(cstate
);
7027 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7029 s
->next
= new_stmt(cstate
, BPF_ST
);
7038 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7042 s
= xfer_to_a(cstate
, a
);
7044 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7047 s
= new_stmt(cstate
, BPF_ST
);
7055 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7058 struct slist
*s0
, *s1
, *s2
;
7061 * Disallow division by, or modulus by, zero; we do this here
7062 * so that it gets done even if the optimizer is disabled.
7064 if (code
== BPF_DIV
) {
7065 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7066 bpf_error(cstate
, "division by zero");
7067 } else if (code
== BPF_MOD
) {
7068 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7069 bpf_error(cstate
, "modulus by zero");
7071 s0
= xfer_to_x(cstate
, a1
);
7072 s1
= xfer_to_a(cstate
, a0
);
7073 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7078 sappend(a0
->s
, a1
->s
);
7080 free_reg(cstate
, a0
->regno
);
7081 free_reg(cstate
, a1
->regno
);
7083 s0
= new_stmt(cstate
, BPF_ST
);
7084 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7091 * Initialize the table of used registers and the current register.
7094 init_regs(compiler_state_t
*cstate
)
7097 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7101 * Return the next free register.
7104 alloc_reg(compiler_state_t
*cstate
)
7106 int n
= BPF_MEMWORDS
;
7109 if (cstate
->regused
[cstate
->curreg
])
7110 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7112 cstate
->regused
[cstate
->curreg
] = 1;
7113 return cstate
->curreg
;
7116 bpf_error(cstate
, "too many registers needed to evaluate expression");
7122 * Return a register to the table so it can
7126 free_reg(compiler_state_t
*cstate
, int n
)
7128 cstate
->regused
[n
] = 0;
7131 static struct block
*
7132 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7137 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7138 b
= new_block(cstate
, JMP(jmp
));
7146 gen_greater(compiler_state_t
*cstate
, int n
)
7148 return gen_len(cstate
, BPF_JGE
, n
);
7152 * Actually, this is less than or equal.
7155 gen_less(compiler_state_t
*cstate
, int n
)
7159 b
= gen_len(cstate
, BPF_JGT
, n
);
7166 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7167 * the beginning of the link-layer header.
7168 * XXX - that means you can't test values in the radiotap header, but
7169 * as that header is difficult if not impossible to parse generally
7170 * without a loop, that might not be a severe problem. A new keyword
7171 * "radio" could be added for that, although what you'd really want
7172 * would be a way of testing particular radio header values, which
7173 * would generate code appropriate to the radio header in question.
7176 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7186 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7189 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7193 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7197 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7201 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7205 b
= new_block(cstate
, JMP(BPF_JEQ
));
7212 static const u_char abroadcast
[] = { 0x0 };
7215 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7217 bpf_u_int32 hostmask
;
7218 struct block
*b0
, *b1
, *b2
;
7219 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7225 switch (cstate
->linktype
) {
7227 case DLT_ARCNET_LINUX
:
7228 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7230 case DLT_NETANALYZER
:
7231 case DLT_NETANALYZER_TRANSPARENT
:
7232 b1
= gen_prevlinkhdr_check(cstate
);
7233 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7238 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7240 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7241 case DLT_IEEE802_11
:
7242 case DLT_PRISM_HEADER
:
7243 case DLT_IEEE802_11_RADIO_AVS
:
7244 case DLT_IEEE802_11_RADIO
:
7246 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7247 case DLT_IP_OVER_FC
:
7248 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7250 bpf_error(cstate
, "not a broadcast link");
7256 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7257 * as an indication that we don't know the netmask, and fail
7260 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7261 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7262 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7263 hostmask
= ~cstate
->netmask
;
7264 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7265 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7266 (bpf_int32
)(~0 & hostmask
), hostmask
);
7271 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7277 * Generate code to test the low-order bit of a MAC address (that's
7278 * the bottom bit of the *first* byte).
7280 static struct block
*
7281 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7283 register struct block
*b0
;
7284 register struct slist
*s
;
7286 /* link[offset] & 1 != 0 */
7287 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7288 b0
= new_block(cstate
, JMP(BPF_JSET
));
7295 gen_multicast(compiler_state_t
*cstate
, int proto
)
7297 register struct block
*b0
, *b1
, *b2
;
7298 register struct slist
*s
;
7304 switch (cstate
->linktype
) {
7306 case DLT_ARCNET_LINUX
:
7307 /* all ARCnet multicasts use the same address */
7308 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7310 case DLT_NETANALYZER
:
7311 case DLT_NETANALYZER_TRANSPARENT
:
7312 b1
= gen_prevlinkhdr_check(cstate
);
7313 /* ether[0] & 1 != 0 */
7314 b0
= gen_mac_multicast(cstate
, 0);
7320 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7322 * XXX - was that referring to bit-order issues?
7324 /* fddi[1] & 1 != 0 */
7325 return gen_mac_multicast(cstate
, 1);
7327 /* tr[2] & 1 != 0 */
7328 return gen_mac_multicast(cstate
, 2);
7329 case DLT_IEEE802_11
:
7330 case DLT_PRISM_HEADER
:
7331 case DLT_IEEE802_11_RADIO_AVS
:
7332 case DLT_IEEE802_11_RADIO
:
7337 * For control frames, there is no DA.
7339 * For management frames, DA is at an
7340 * offset of 4 from the beginning of
7343 * For data frames, DA is at an offset
7344 * of 4 from the beginning of the packet
7345 * if To DS is clear and at an offset of
7346 * 16 from the beginning of the packet
7351 * Generate the tests to be done for data frames.
7353 * First, check for To DS set, i.e. "link[1] & 0x01".
7355 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7356 b1
= new_block(cstate
, JMP(BPF_JSET
));
7357 b1
->s
.k
= 0x01; /* To DS */
7361 * If To DS is set, the DA is at 16.
7363 b0
= gen_mac_multicast(cstate
, 16);
7367 * Now, check for To DS not set, i.e. check
7368 * "!(link[1] & 0x01)".
7370 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7371 b2
= new_block(cstate
, JMP(BPF_JSET
));
7372 b2
->s
.k
= 0x01; /* To DS */
7377 * If To DS is not set, the DA is at 4.
7379 b1
= gen_mac_multicast(cstate
, 4);
7383 * Now OR together the last two checks. That gives
7384 * the complete set of checks for data frames.
7389 * Now check for a data frame.
7390 * I.e, check "link[0] & 0x08".
7392 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7393 b1
= new_block(cstate
, JMP(BPF_JSET
));
7398 * AND that with the checks done for data frames.
7403 * If the high-order bit of the type value is 0, this
7404 * is a management frame.
7405 * I.e, check "!(link[0] & 0x08)".
7407 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7408 b2
= new_block(cstate
, JMP(BPF_JSET
));
7414 * For management frames, the DA is at 4.
7416 b1
= gen_mac_multicast(cstate
, 4);
7420 * OR that with the checks done for data frames.
7421 * That gives the checks done for management and
7427 * If the low-order bit of the type value is 1,
7428 * this is either a control frame or a frame
7429 * with a reserved type, and thus not a
7432 * I.e., check "!(link[0] & 0x04)".
7434 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7435 b1
= new_block(cstate
, JMP(BPF_JSET
));
7441 * AND that with the checks for data and management
7446 case DLT_IP_OVER_FC
:
7447 b0
= gen_mac_multicast(cstate
, 2);
7452 /* Link not known to support multicasts */
7456 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7457 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7462 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7463 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7467 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7473 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7474 * Outbound traffic is sent by this machine, while inbound traffic is
7475 * sent by a remote machine (and may include packets destined for a
7476 * unicast or multicast link-layer address we are not subscribing to).
7477 * These are the same definitions implemented by pcap_setdirection().
7478 * Capturing only unicast traffic destined for this host is probably
7479 * better accomplished using a higher-layer filter.
7482 gen_inbound(compiler_state_t
*cstate
, int dir
)
7484 register struct block
*b0
;
7487 * Only some data link types support inbound/outbound qualifiers.
7489 switch (cstate
->linktype
) {
7491 b0
= gen_relation(cstate
, BPF_JEQ
,
7492 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7493 gen_loadi(cstate
, 0),
7499 /* match outgoing packets */
7500 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7502 /* match incoming packets */
7503 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7508 /* match outgoing packets */
7509 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7511 /* to filter on inbound traffic, invert the match */
7516 #ifdef HAVE_NET_PFVAR_H
7518 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7519 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7525 /* match outgoing packets */
7526 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7528 /* match incoming packets */
7529 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7533 case DLT_JUNIPER_MFR
:
7534 case DLT_JUNIPER_MLFR
:
7535 case DLT_JUNIPER_MLPPP
:
7536 case DLT_JUNIPER_ATM1
:
7537 case DLT_JUNIPER_ATM2
:
7538 case DLT_JUNIPER_PPPOE
:
7539 case DLT_JUNIPER_PPPOE_ATM
:
7540 case DLT_JUNIPER_GGSN
:
7541 case DLT_JUNIPER_ES
:
7542 case DLT_JUNIPER_MONITOR
:
7543 case DLT_JUNIPER_SERVICES
:
7544 case DLT_JUNIPER_ETHER
:
7545 case DLT_JUNIPER_PPP
:
7546 case DLT_JUNIPER_FRELAY
:
7547 case DLT_JUNIPER_CHDLC
:
7548 case DLT_JUNIPER_VP
:
7549 case DLT_JUNIPER_ST
:
7550 case DLT_JUNIPER_ISM
:
7551 case DLT_JUNIPER_VS
:
7552 case DLT_JUNIPER_SRX_E2E
:
7553 case DLT_JUNIPER_FIBRECHANNEL
:
7554 case DLT_JUNIPER_ATM_CEMIC
:
7556 /* juniper flags (including direction) are stored
7557 * the byte after the 3-byte magic number */
7559 /* match outgoing packets */
7560 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7562 /* match incoming packets */
7563 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7569 * If we have packet meta-data indicating a direction,
7570 * check it, otherwise give up as this link-layer type
7571 * has nothing in the packet data.
7573 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7575 * This is Linux with PF_PACKET support.
7576 * If this is a *live* capture, we can look at
7577 * special meta-data in the filter expression;
7578 * if it's a savefile, we can't.
7580 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7581 /* We have a FILE *, so this is a savefile */
7582 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
7587 /* match outgoing packets */
7588 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7591 /* to filter on inbound traffic, invert the match */
7594 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7595 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
7599 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7604 #ifdef HAVE_NET_PFVAR_H
7605 /* PF firewall log matched interface */
7607 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7612 if (cstate
->linktype
!= DLT_PFLOG
) {
7613 bpf_error(cstate
, "ifname supported only on PF linktype");
7616 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7617 off
= offsetof(struct pfloghdr
, ifname
);
7618 if (strlen(ifname
) >= len
) {
7619 bpf_error(cstate
, "ifname interface names can only be %d characters",
7623 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7627 /* PF firewall log ruleset name */
7629 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7633 if (cstate
->linktype
!= DLT_PFLOG
) {
7634 bpf_error(cstate
, "ruleset supported only on PF linktype");
7638 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7639 bpf_error(cstate
, "ruleset names can only be %ld characters",
7640 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7644 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7645 strlen(ruleset
), (const u_char
*)ruleset
);
7649 /* PF firewall log rule number */
7651 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7655 if (cstate
->linktype
!= DLT_PFLOG
) {
7656 bpf_error(cstate
, "rnr supported only on PF linktype");
7660 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7665 /* PF firewall log sub-rule number */
7667 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7671 if (cstate
->linktype
!= DLT_PFLOG
) {
7672 bpf_error(cstate
, "srnr supported only on PF linktype");
7676 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7681 /* PF firewall log reason code */
7683 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7687 if (cstate
->linktype
!= DLT_PFLOG
) {
7688 bpf_error(cstate
, "reason supported only on PF linktype");
7692 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7697 /* PF firewall log action */
7699 gen_pf_action(compiler_state_t
*cstate
, int action
)
7703 if (cstate
->linktype
!= DLT_PFLOG
) {
7704 bpf_error(cstate
, "action supported only on PF linktype");
7708 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7712 #else /* !HAVE_NET_PFVAR_H */
7714 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7716 bpf_error(cstate
, "libpcap was compiled without pf support");
7722 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7724 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7730 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7732 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7738 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7740 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7746 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7748 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7754 gen_pf_action(compiler_state_t
*cstate
, int action
)
7756 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7760 #endif /* HAVE_NET_PFVAR_H */
7762 /* IEEE 802.11 wireless header */
7764 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
7768 switch (cstate
->linktype
) {
7770 case DLT_IEEE802_11
:
7771 case DLT_PRISM_HEADER
:
7772 case DLT_IEEE802_11_RADIO_AVS
:
7773 case DLT_IEEE802_11_RADIO
:
7774 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7779 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
7787 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
7791 switch (cstate
->linktype
) {
7793 case DLT_IEEE802_11
:
7794 case DLT_PRISM_HEADER
:
7795 case DLT_IEEE802_11_RADIO_AVS
:
7796 case DLT_IEEE802_11_RADIO
:
7800 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
7804 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7805 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7811 gen_acode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
7813 switch (cstate
->linktype
) {
7816 case DLT_ARCNET_LINUX
:
7817 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7819 return (gen_ahostop(cstate
, eaddr
, (int)q
.dir
));
7821 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7827 bpf_error(cstate
, "aid supported only on ARCnet");
7830 bpf_error(cstate
, "ARCnet address used in non-arc expression");
7835 static struct block
*
7836 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
7838 register struct block
*b0
, *b1
;
7841 /* src comes first, different from Ethernet */
7843 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
7846 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
7849 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
7850 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
7856 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
7857 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
7862 bpf_error(cstate
, "'addr1' is only supported on 802.11");
7866 bpf_error(cstate
, "'addr2' is only supported on 802.11");
7870 bpf_error(cstate
, "'addr3' is only supported on 802.11");
7874 bpf_error(cstate
, "'addr4' is only supported on 802.11");
7878 bpf_error(cstate
, "'ra' is only supported on 802.11");
7882 bpf_error(cstate
, "'ta' is only supported on 802.11");
7889 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7890 static struct block
*
7891 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
7893 struct block
*b0
, *b1
;
7896 /* generate new filter code based on extracting packet
7898 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
7899 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7901 b0
= new_block(cstate
, JMP(BPF_JEQ
));
7905 if (vlan_num
>= 0) {
7906 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
7907 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7909 b1
= new_block(cstate
, JMP(BPF_JEQ
));
7911 b1
->s
.k
= (bpf_int32
) vlan_num
;
7921 static struct block
*
7922 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
7924 struct block
*b0
, *b1
;
7926 /* check for VLAN, including QinQ */
7927 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
7928 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
7931 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
7935 /* If a specific VLAN is requested, check VLAN id */
7936 if (vlan_num
>= 0) {
7937 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
,
7938 (bpf_int32
)vlan_num
, 0x0fff);
7944 * The payload follows the full header, including the
7945 * VLAN tags, so skip past this VLAN tag.
7947 cstate
->off_linkpl
.constant_part
+= 4;
7950 * The link-layer type information follows the VLAN tags, so
7951 * skip past this VLAN tag.
7953 cstate
->off_linktype
.constant_part
+= 4;
7959 * support IEEE 802.1Q VLAN trunk over ethernet
7962 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
7966 /* can't check for VLAN-encapsulated packets inside MPLS */
7967 if (cstate
->label_stack_depth
> 0)
7968 bpf_error(cstate
, "no VLAN match after MPLS");
7971 * Check for a VLAN packet, and then change the offsets to point
7972 * to the type and data fields within the VLAN packet. Just
7973 * increment the offsets, so that we can support a hierarchy, e.g.
7974 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7977 * XXX - this is a bit of a kludge. If we were to split the
7978 * compiler into a parser that parses an expression and
7979 * generates an expression tree, and a code generator that
7980 * takes an expression tree (which could come from our
7981 * parser or from some other parser) and generates BPF code,
7982 * we could perhaps make the offsets parameters of routines
7983 * and, in the handler for an "AND" node, pass to subnodes
7984 * other than the VLAN node the adjusted offsets.
7986 * This would mean that "vlan" would, instead of changing the
7987 * behavior of *all* tests after it, change only the behavior
7988 * of tests ANDed with it. That would change the documented
7989 * semantics of "vlan", which might break some expressions.
7990 * However, it would mean that "(vlan and ip) or ip" would check
7991 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7992 * checking only for VLAN-encapsulated IP, so that could still
7993 * be considered worth doing; it wouldn't break expressions
7994 * that are of the form "vlan and ..." or "vlan N and ...",
7995 * which I suspect are the most common expressions involving
7996 * "vlan". "vlan or ..." doesn't necessarily do what the user
7997 * would really want, now, as all the "or ..." tests would
7998 * be done assuming a VLAN, even though the "or" could be viewed
7999 * as meaning "or, if this isn't a VLAN packet...".
8001 switch (cstate
->linktype
) {
8004 case DLT_NETANALYZER
:
8005 case DLT_NETANALYZER_TRANSPARENT
:
8006 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8007 /* Verify that this is the outer part of the packet and
8008 * not encapsulated somehow. */
8009 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8010 cstate
->off_linkhdr
.constant_part
==
8011 cstate
->off_outermostlinkhdr
.constant_part
) {
8013 * Do we need special VLAN handling?
8015 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8016 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8018 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8021 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8024 case DLT_IEEE802_11
:
8025 case DLT_PRISM_HEADER
:
8026 case DLT_IEEE802_11_RADIO_AVS
:
8027 case DLT_IEEE802_11_RADIO
:
8028 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8032 bpf_error(cstate
, "no VLAN support for data link type %d",
8037 cstate
->vlan_stack_depth
++;
8046 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8048 struct block
*b0
, *b1
;
8050 if (cstate
->label_stack_depth
> 0) {
8051 /* just match the bottom-of-stack bit clear */
8052 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8055 * We're not in an MPLS stack yet, so check the link-layer
8056 * type against MPLS.
8058 switch (cstate
->linktype
) {
8060 case DLT_C_HDLC
: /* fall through */
8062 case DLT_NETANALYZER
:
8063 case DLT_NETANALYZER_TRANSPARENT
:
8064 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8068 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8071 /* FIXME add other DLT_s ...
8072 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8073 * leave it for now */
8076 bpf_error(cstate
, "no MPLS support for data link type %d",
8084 /* If a specific MPLS label is requested, check it */
8085 if (label_num
>= 0) {
8086 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8087 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8088 0xfffff000); /* only compare the first 20 bits */
8094 * Change the offsets to point to the type and data fields within
8095 * the MPLS packet. Just increment the offsets, so that we
8096 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8097 * capture packets with an outer label of 100000 and an inner
8100 * Increment the MPLS stack depth as well; this indicates that
8101 * we're checking MPLS-encapsulated headers, to make sure higher
8102 * level code generators don't try to match against IP-related
8103 * protocols such as Q_ARP, Q_RARP etc.
8105 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8107 cstate
->off_nl_nosnap
+= 4;
8108 cstate
->off_nl
+= 4;
8109 cstate
->label_stack_depth
++;
8114 * Support PPPOE discovery and session.
8117 gen_pppoed(compiler_state_t
*cstate
)
8119 /* check for PPPoE discovery */
8120 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8124 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8126 struct block
*b0
, *b1
;
8129 * Test against the PPPoE session link-layer type.
8131 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8133 /* If a specific session is requested, check PPPoE session id */
8134 if (sess_num
>= 0) {
8135 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8136 (bpf_int32
)sess_num
, 0x0000ffff);
8142 * Change the offsets to point to the type and data fields within
8143 * the PPP packet, and note that this is PPPoE rather than
8146 * XXX - this is a bit of a kludge. If we were to split the
8147 * compiler into a parser that parses an expression and
8148 * generates an expression tree, and a code generator that
8149 * takes an expression tree (which could come from our
8150 * parser or from some other parser) and generates BPF code,
8151 * we could perhaps make the offsets parameters of routines
8152 * and, in the handler for an "AND" node, pass to subnodes
8153 * other than the PPPoE node the adjusted offsets.
8155 * This would mean that "pppoes" would, instead of changing the
8156 * behavior of *all* tests after it, change only the behavior
8157 * of tests ANDed with it. That would change the documented
8158 * semantics of "pppoes", which might break some expressions.
8159 * However, it would mean that "(pppoes and ip) or ip" would check
8160 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8161 * checking only for VLAN-encapsulated IP, so that could still
8162 * be considered worth doing; it wouldn't break expressions
8163 * that are of the form "pppoes and ..." which I suspect are the
8164 * most common expressions involving "pppoes". "pppoes or ..."
8165 * doesn't necessarily do what the user would really want, now,
8166 * as all the "or ..." tests would be done assuming PPPoE, even
8167 * though the "or" could be viewed as meaning "or, if this isn't
8168 * a PPPoE packet...".
8170 * The "network-layer" protocol is PPPoE, which has a 6-byte
8171 * PPPoE header, followed by a PPP packet.
8173 * There is no HDLC encapsulation for the PPP packet (it's
8174 * encapsulated in PPPoES instead), so the link-layer type
8175 * starts at the first byte of the PPP packet. For PPPoE,
8176 * that offset is relative to the beginning of the total
8177 * link-layer payload, including any 802.2 LLC header, so
8178 * it's 6 bytes past cstate->off_nl.
8180 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8181 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8182 cstate
->off_linkpl
.reg
);
8184 cstate
->off_linktype
= cstate
->off_linkhdr
;
8185 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8188 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8193 /* Check that this is Geneve and the VNI is correct if
8194 * specified. Parameterized to handle both IPv4 and IPv6. */
8195 static struct block
*
8196 gen_geneve_check(compiler_state_t
*cstate
,
8197 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8198 enum e_offrel offrel
, int vni
)
8200 struct block
*b0
, *b1
;
8202 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8204 /* Check that we are operating on version 0. Otherwise, we
8205 * can't decode the rest of the fields. The version is 2 bits
8206 * in the first byte of the Geneve header. */
8207 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8212 vni
<<= 8; /* VNI is in the upper 3 bytes */
8213 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8222 /* The IPv4 and IPv6 Geneve checks need to do two things:
8223 * - Verify that this actually is Geneve with the right VNI.
8224 * - Place the IP header length (plus variable link prefix if
8225 * needed) into register A to be used later to compute
8226 * the inner packet offsets. */
8227 static struct block
*
8228 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8230 struct block
*b0
, *b1
;
8231 struct slist
*s
, *s1
;
8233 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8235 /* Load the IP header length into A. */
8236 s
= gen_loadx_iphdrlen(cstate
);
8238 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8241 /* Forcibly append these statements to the true condition
8242 * of the protocol check by creating a new block that is
8243 * always true and ANDing them. */
8244 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8253 static struct block
*
8254 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8256 struct block
*b0
, *b1
;
8257 struct slist
*s
, *s1
;
8259 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8261 /* Load the IP header length. We need to account for a
8262 * variable length link prefix if there is one. */
8263 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8265 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8269 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8273 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8277 /* Forcibly append these statements to the true condition
8278 * of the protocol check by creating a new block that is
8279 * always true and ANDing them. */
8280 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8283 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8292 /* We need to store three values based on the Geneve header::
8293 * - The offset of the linktype.
8294 * - The offset of the end of the Geneve header.
8295 * - The offset of the end of the encapsulated MAC header. */
8296 static struct slist
*
8297 gen_geneve_offsets(compiler_state_t
*cstate
)
8299 struct slist
*s
, *s1
, *s_proto
;
8301 /* First we need to calculate the offset of the Geneve header
8302 * itself. This is composed of the IP header previously calculated
8303 * (include any variable link prefix) and stored in A plus the
8304 * fixed sized headers (fixed link prefix, MAC length, and UDP
8306 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8307 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8309 /* Stash this in X since we'll need it later. */
8310 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8313 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8315 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8319 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8320 cstate
->off_linktype
.is_variable
= 1;
8321 cstate
->off_linktype
.constant_part
= 0;
8323 s1
= new_stmt(cstate
, BPF_ST
);
8324 s1
->s
.k
= cstate
->off_linktype
.reg
;
8327 /* Load the Geneve option length and mask and shift to get the
8328 * number of bytes. It is stored in the first byte of the Geneve
8330 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8334 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8338 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8342 /* Add in the rest of the Geneve base header. */
8343 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8347 /* Add the Geneve header length to its offset and store. */
8348 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8352 /* Set the encapsulated type as Ethernet. Even though we may
8353 * not actually have Ethernet inside there are two reasons this
8355 * - The linktype field is always in EtherType format regardless
8356 * of whether it is in Geneve or an inner Ethernet frame.
8357 * - The only link layer that we have specific support for is
8358 * Ethernet. We will confirm that the packet actually is
8359 * Ethernet at runtime before executing these checks. */
8360 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8362 s1
= new_stmt(cstate
, BPF_ST
);
8363 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8366 /* Calculate whether we have an Ethernet header or just raw IP/
8367 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8368 * and linktype by 14 bytes so that the network header can be found
8369 * seamlessly. Otherwise, keep what we've calculated already. */
8371 /* We have a bare jmp so we can't use the optimizer. */
8372 cstate
->no_optimize
= 1;
8374 /* Load the EtherType in the Geneve header, 2 bytes in. */
8375 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8379 /* Load X with the end of the Geneve header. */
8380 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8381 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8384 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8385 * end of this check, we should have the total length in X. In
8386 * the non-Ethernet case, it's already there. */
8387 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8388 s_proto
->s
.k
= ETHERTYPE_TEB
;
8389 sappend(s
, s_proto
);
8391 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8395 /* Since this is Ethernet, use the EtherType of the payload
8396 * directly as the linktype. Overwrite what we already have. */
8397 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8401 s1
= new_stmt(cstate
, BPF_ST
);
8402 s1
->s
.k
= cstate
->off_linktype
.reg
;
8405 /* Advance two bytes further to get the end of the Ethernet
8407 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8411 /* Move the result to X. */
8412 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8415 /* Store the final result of our linkpl calculation. */
8416 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8417 cstate
->off_linkpl
.is_variable
= 1;
8418 cstate
->off_linkpl
.constant_part
= 0;
8420 s1
= new_stmt(cstate
, BPF_STX
);
8421 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8430 /* Check to see if this is a Geneve packet. */
8432 gen_geneve(compiler_state_t
*cstate
, int vni
)
8434 struct block
*b0
, *b1
;
8437 b0
= gen_geneve4(cstate
, vni
);
8438 b1
= gen_geneve6(cstate
, vni
);
8443 /* Later filters should act on the payload of the Geneve frame,
8444 * update all of the header pointers. Attach this code so that
8445 * it gets executed in the event that the Geneve filter matches. */
8446 s
= gen_geneve_offsets(cstate
);
8448 b1
= gen_true(cstate
);
8449 sappend(s
, b1
->stmts
);
8454 cstate
->is_geneve
= 1;
8459 /* Check that the encapsulated frame has a link layer header
8460 * for Ethernet filters. */
8461 static struct block
*
8462 gen_geneve_ll_check(compiler_state_t
*cstate
)
8465 struct slist
*s
, *s1
;
8467 /* The easiest way to see if there is a link layer present
8468 * is to check if the link layer header and payload are not
8471 /* Geneve always generates pure variable offsets so we can
8472 * compare only the registers. */
8473 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8474 s
->s
.k
= cstate
->off_linkhdr
.reg
;
8476 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8477 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8480 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8489 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
8490 bpf_u_int32 jtype
, int reverse
)
8497 if (!cstate
->is_atm
)
8498 bpf_error(cstate
, "'vpi' supported only on raw ATM");
8499 if (cstate
->off_vpi
== (u_int
)-1)
8501 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
8506 if (!cstate
->is_atm
)
8507 bpf_error(cstate
, "'vci' supported only on raw ATM");
8508 if (cstate
->off_vci
== (u_int
)-1)
8510 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
8515 if (cstate
->off_proto
== (u_int
)-1)
8516 abort(); /* XXX - this isn't on FreeBSD */
8517 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
8522 if (cstate
->off_payload
== (u_int
)-1)
8524 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
8525 0xffffffff, jtype
, reverse
, jvalue
);
8529 if (!cstate
->is_atm
)
8530 bpf_error(cstate
, "'callref' supported only on raw ATM");
8531 if (cstate
->off_proto
== (u_int
)-1)
8533 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
8534 jtype
, reverse
, jvalue
);
8544 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
8546 struct block
*b0
, *b1
;
8551 /* Get all packets in Meta signalling Circuit */
8552 if (!cstate
->is_atm
)
8553 bpf_error(cstate
, "'metac' supported only on raw ATM");
8554 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8555 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
8560 /* Get all packets in Broadcast Circuit*/
8561 if (!cstate
->is_atm
)
8562 bpf_error(cstate
, "'bcc' supported only on raw ATM");
8563 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8564 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
8569 /* Get all cells in Segment OAM F4 circuit*/
8570 if (!cstate
->is_atm
)
8571 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
8572 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8573 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8578 /* Get all cells in End-to-End OAM F4 Circuit*/
8579 if (!cstate
->is_atm
)
8580 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
8581 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8582 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8587 /* Get all packets in connection Signalling Circuit */
8588 if (!cstate
->is_atm
)
8589 bpf_error(cstate
, "'sc' supported only on raw ATM");
8590 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8591 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
8596 /* Get all packets in ILMI Circuit */
8597 if (!cstate
->is_atm
)
8598 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
8599 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8600 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
8605 /* Get all LANE packets */
8606 if (!cstate
->is_atm
)
8607 bpf_error(cstate
, "'lane' supported only on raw ATM");
8608 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8611 * Arrange that all subsequent tests assume LANE
8612 * rather than LLC-encapsulated packets, and set
8613 * the offsets appropriately for LANE-encapsulated
8616 * We assume LANE means Ethernet, not Token Ring.
8618 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
8619 cstate
->off_payload
+ 2, /* Ethernet header */
8621 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
8622 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
8623 cstate
->off_nl
= 0; /* Ethernet II */
8624 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
8628 /* Get all LLC-encapsulated packets */
8629 if (!cstate
->is_atm
)
8630 bpf_error(cstate
, "'llc' supported only on raw ATM");
8631 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8632 cstate
->linktype
= cstate
->prevlinktype
;
8642 * Filtering for MTP2 messages based on li value
8643 * FISU, length is null
8644 * LSSU, length is 1 or 2
8645 * MSU, length is 3 or more
8646 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8649 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
8651 struct block
*b0
, *b1
;
8656 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8657 (cstate
->linktype
!= DLT_ERF
) &&
8658 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8659 bpf_error(cstate
, "'fisu' supported only on MTP2");
8660 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8661 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8665 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8666 (cstate
->linktype
!= DLT_ERF
) &&
8667 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8668 bpf_error(cstate
, "'lssu' supported only on MTP2");
8669 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8670 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8675 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8676 (cstate
->linktype
!= DLT_ERF
) &&
8677 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8678 bpf_error(cstate
, "'msu' supported only on MTP2");
8679 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8683 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8684 (cstate
->linktype
!= DLT_ERF
) &&
8685 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8686 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
8687 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8688 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8692 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8693 (cstate
->linktype
!= DLT_ERF
) &&
8694 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8695 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
8696 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8697 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8702 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8703 (cstate
->linktype
!= DLT_ERF
) &&
8704 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8705 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
8706 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8716 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
8717 bpf_u_int32 jtype
, int reverse
)
8720 bpf_u_int32 val1
, val2
, val3
;
8721 u_int newoff_sio
= cstate
->off_sio
;
8722 u_int newoff_opc
= cstate
->off_opc
;
8723 u_int newoff_dpc
= cstate
->off_dpc
;
8724 u_int newoff_sls
= cstate
->off_sls
;
8726 switch (mtp3field
) {
8729 newoff_sio
+= 3; /* offset for MTP2_HSL */
8733 if (cstate
->off_sio
== (u_int
)-1)
8734 bpf_error(cstate
, "'sio' supported only on SS7");
8735 /* sio coded on 1 byte so max value 255 */
8737 bpf_error(cstate
, "sio value %u too big; max value = 255",
8739 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8740 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8746 if (cstate
->off_opc
== (u_int
)-1)
8747 bpf_error(cstate
, "'opc' supported only on SS7");
8748 /* opc coded on 14 bits so max value 16383 */
8750 bpf_error(cstate
, "opc value %u too big; max value = 16383",
8752 /* the following instructions are made to convert jvalue
8753 * to the form used to write opc in an ss7 message*/
8754 val1
= jvalue
& 0x00003c00;
8756 val2
= jvalue
& 0x000003fc;
8758 val3
= jvalue
& 0x00000003;
8760 jvalue
= val1
+ val2
+ val3
;
8761 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8762 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8770 if (cstate
->off_dpc
== (u_int
)-1)
8771 bpf_error(cstate
, "'dpc' supported only on SS7");
8772 /* dpc coded on 14 bits so max value 16383 */
8774 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
8776 /* the following instructions are made to convert jvalue
8777 * to the forme used to write dpc in an ss7 message*/
8778 val1
= jvalue
& 0x000000ff;
8780 val2
= jvalue
& 0x00003f00;
8782 jvalue
= val1
+ val2
;
8783 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8784 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8790 if (cstate
->off_sls
== (u_int
)-1)
8791 bpf_error(cstate
, "'sls' supported only on SS7");
8792 /* sls coded on 4 bits so max value 15 */
8794 bpf_error(cstate
, "sls value %u too big; max value = 15",
8796 /* the following instruction is made to convert jvalue
8797 * to the forme used to write sls in an ss7 message*/
8798 jvalue
= jvalue
<< 4;
8799 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8800 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8809 static struct block
*
8810 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
8815 * Q.2931 signalling protocol messages for handling virtual circuits
8816 * establishment and teardown
8821 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8825 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8829 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8833 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8837 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8840 case A_RELEASE_DONE
:
8841 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8851 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
8853 struct block
*b0
, *b1
;
8858 if (!cstate
->is_atm
)
8859 bpf_error(cstate
, "'oam' supported only on raw ATM");
8860 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
8864 if (!cstate
->is_atm
)
8865 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
8867 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8868 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8870 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8876 * Get Q.2931 signalling messages for switched
8877 * virtual connection
8879 if (!cstate
->is_atm
)
8880 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
8881 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
8882 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
8884 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
8886 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
8888 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
8890 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
8892 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
8897 if (!cstate
->is_atm
)
8898 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
8899 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
8900 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
8902 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
8904 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
8906 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
8908 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);