1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <pcap-types.h>
31 #include <sys/socket.h>
34 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
53 #include "ethertype.h"
57 #include "ieee80211.h"
59 #include "sunatmpos.h"
62 #include "pcap/ipnet.h"
68 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
69 #include <linux/types.h>
70 #include <linux/if_packet.h>
71 #include <linux/filter.h>
74 #ifdef HAVE_NET_PFVAR_H
75 #include <sys/socket.h>
77 #include <net/pfvar.h>
78 #include <net/if_pflog.h>
82 #define offsetof(s, e) ((size_t)&((s *)0)->e)
87 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
94 uint16_t u6_addr16
[8];
95 uint32_t u6_addr32
[4];
97 #define s6_addr in6_u.u6_addr8
98 #define s6_addr16 in6_u.u6_addr16
99 #define s6_addr32 in6_u.u6_addr32
100 #define s6_addr64 in6_u.u6_addr64
103 typedef unsigned short sa_family_t
;
105 #define __SOCKADDR_COMMON(sa_prefix) \
106 sa_family_t sa_prefix##family
108 /* Ditto, for IPv6. */
111 __SOCKADDR_COMMON (sin6_
);
112 uint16_t sin6_port
; /* Transport layer port # */
113 uint32_t sin6_flowinfo
; /* IPv6 flow information */
114 struct in6_addr sin6_addr
; /* IPv6 address */
117 #ifndef EAI_ADDRFAMILY
119 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
120 int ai_family
; /* PF_xxx */
121 int ai_socktype
; /* SOCK_xxx */
122 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
123 size_t ai_addrlen
; /* length of ai_addr */
124 char *ai_canonname
; /* canonical name for hostname */
125 struct sockaddr
*ai_addr
; /* binary address */
126 struct addrinfo
*ai_next
; /* next structure in linked list */
128 #endif /* EAI_ADDRFAMILY */
129 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
132 #include <netdb.h> /* for "struct addrinfo" */
134 #include <pcap/namedb.h>
136 #include "nametoaddr.h"
138 #define ETHERMTU 1500
140 #ifndef ETHERTYPE_TEB
141 #define ETHERTYPE_TEB 0x6558
144 #ifndef IPPROTO_HOPOPTS
145 #define IPPROTO_HOPOPTS 0
147 #ifndef IPPROTO_ROUTING
148 #define IPPROTO_ROUTING 43
150 #ifndef IPPROTO_FRAGMENT
151 #define IPPROTO_FRAGMENT 44
153 #ifndef IPPROTO_DSTOPTS
154 #define IPPROTO_DSTOPTS 60
157 #define IPPROTO_SCTP 132
160 #define GENEVE_PORT 6081
162 #ifdef HAVE_OS_PROTO_H
163 #include "os-proto.h"
166 #define JMP(c) ((c)|BPF_JMP|BPF_K)
169 * "Push" the current value of the link-layer header type and link-layer
170 * header offset onto a "stack", and set a new value. (It's not a
171 * full-blown stack; we keep only the top two items.)
173 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
175 (cs)->prevlinktype = (cs)->linktype; \
176 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
177 (cs)->linktype = (new_linktype); \
178 (cs)->off_linkhdr.is_variable = (new_is_variable); \
179 (cs)->off_linkhdr.constant_part = (new_constant_part); \
180 (cs)->off_linkhdr.reg = (new_reg); \
181 (cs)->is_geneve = 0; \
185 * Offset "not set" value.
187 #define OFFSET_NOT_SET 0xffffffffU
190 * Absolute offsets, which are offsets from the beginning of the raw
191 * packet data, are, in the general case, the sum of a variable value
192 * and a constant value; the variable value may be absent, in which
193 * case the offset is only the constant value, and the constant value
194 * may be zero, in which case the offset is only the variable value.
196 * bpf_abs_offset is a structure containing all that information:
198 * is_variable is 1 if there's a variable part.
200 * constant_part is the constant part of the value, possibly zero;
202 * if is_variable is 1, reg is the register number for a register
203 * containing the variable value if the register has been assigned,
213 * Value passed to gen_load_a() to indicate what the offset argument
214 * is relative to the beginning of.
217 OR_PACKET
, /* full packet data */
218 OR_LINKHDR
, /* link-layer header */
219 OR_PREVLINKHDR
, /* previous link-layer header */
220 OR_LLC
, /* 802.2 LLC header */
221 OR_PREVMPLSHDR
, /* previous MPLS header */
222 OR_LINKTYPE
, /* link-layer type */
223 OR_LINKPL
, /* link-layer payload */
224 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
225 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
226 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
230 * We divy out chunks of memory rather than call malloc each time so
231 * we don't have to worry about leaking memory. It's probably
232 * not a big deal if all this memory was wasted but if this ever
233 * goes into a library that would probably not be a good idea.
235 * XXX - this *is* in a library....
238 #define CHUNK0SIZE 1024
244 /* Code generator state */
246 struct _compiler_state
{
256 int outermostlinktype
;
261 /* Hack for handling VLAN and MPLS stacks. */
262 u_int label_stack_depth
;
263 u_int vlan_stack_depth
;
269 * As errors are handled by a longjmp, anything allocated must
270 * be freed in the longjmp handler, so it must be reachable
273 * One thing that's allocated is the result of pcap_nametoaddrinfo();
274 * it must be freed with freeaddrinfo(). This variable points to
275 * any addrinfo structure that would need to be freed.
280 * Various code constructs need to know the layout of the packet.
281 * These values give the necessary offsets from the beginning
282 * of the packet data.
286 * Absolute offset of the beginning of the link-layer header.
288 bpf_abs_offset off_linkhdr
;
291 * If we're checking a link-layer header for a packet encapsulated
292 * in another protocol layer, this is the equivalent information
293 * for the previous layers' link-layer header from the beginning
294 * of the raw packet data.
296 bpf_abs_offset off_prevlinkhdr
;
299 * This is the equivalent information for the outermost layers'
302 bpf_abs_offset off_outermostlinkhdr
;
305 * Absolute offset of the beginning of the link-layer payload.
307 bpf_abs_offset off_linkpl
;
310 * "off_linktype" is the offset to information in the link-layer
311 * header giving the packet type. This is an absolute offset
312 * from the beginning of the packet.
314 * For Ethernet, it's the offset of the Ethernet type field; this
315 * means that it must have a value that skips VLAN tags.
317 * For link-layer types that always use 802.2 headers, it's the
318 * offset of the LLC header; this means that it must have a value
319 * that skips VLAN tags.
321 * For PPP, it's the offset of the PPP type field.
323 * For Cisco HDLC, it's the offset of the CHDLC type field.
325 * For BSD loopback, it's the offset of the AF_ value.
327 * For Linux cooked sockets, it's the offset of the type field.
329 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
330 * encapsulation, in which case, IP is assumed.
332 bpf_abs_offset off_linktype
;
335 * TRUE if the link layer includes an ATM pseudo-header.
340 * TRUE if "geneve" appeared in the filter; it causes us to
341 * generate code that checks for a Geneve header and assume
342 * that later filters apply to the encapsulated payload.
347 * TRUE if we need variable length part of VLAN offset
349 int is_vlan_vloffset
;
352 * These are offsets for the ATM pseudo-header.
359 * These are offsets for the MTP2 fields.
365 * These are offsets for the MTP3 fields.
373 * This is the offset of the first byte after the ATM pseudo_header,
374 * or -1 if there is no ATM pseudo-header.
379 * These are offsets to the beginning of the network-layer header.
380 * They are relative to the beginning of the link-layer payload
381 * (i.e., they don't include off_linkhdr.constant_part or
382 * off_linkpl.constant_part).
384 * If the link layer never uses 802.2 LLC:
386 * "off_nl" and "off_nl_nosnap" are the same.
388 * If the link layer always uses 802.2 LLC:
390 * "off_nl" is the offset if there's a SNAP header following
393 * "off_nl_nosnap" is the offset if there's no SNAP header.
395 * If the link layer is Ethernet:
397 * "off_nl" is the offset if the packet is an Ethernet II packet
398 * (we assume no 802.3+802.2+SNAP);
400 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
401 * with an 802.2 header following it.
407 * Here we handle simple allocation of the scratch registers.
408 * If too many registers are alloc'd, the allocator punts.
410 int regused
[BPF_MEMWORDS
];
416 struct chunk chunks
[NCHUNKS
];
421 bpf_syntax_error(compiler_state_t
*cstate
, const char *msg
)
423 bpf_error(cstate
, "syntax error in filter expression: %s", msg
);
429 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
434 if (cstate
->bpf_pcap
!= NULL
)
435 (void)pcap_vsnprintf(pcap_geterr(cstate
->bpf_pcap
),
436 PCAP_ERRBUF_SIZE
, fmt
, ap
);
438 longjmp(cstate
->top_ctx
, 1);
442 static void init_linktype(compiler_state_t
*, pcap_t
*);
444 static void init_regs(compiler_state_t
*);
445 static int alloc_reg(compiler_state_t
*);
446 static void free_reg(compiler_state_t
*, int);
448 static void initchunks(compiler_state_t
*cstate
);
449 static void *newchunk(compiler_state_t
*cstate
, size_t);
450 static void freechunks(compiler_state_t
*cstate
);
451 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
452 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
453 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
454 static inline void syntax(compiler_state_t
*cstate
);
456 static void backpatch(struct block
*, struct block
*);
457 static void merge(struct block
*, struct block
*);
458 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
460 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
462 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
464 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
466 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
468 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
469 u_int
, bpf_int32
, bpf_u_int32
);
470 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
471 u_int
, const u_char
*);
472 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
473 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
474 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
476 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
478 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
479 static struct block
*gen_uncond(compiler_state_t
*, int);
480 static inline struct block
*gen_true(compiler_state_t
*);
481 static inline struct block
*gen_false(compiler_state_t
*);
482 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
483 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
484 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
485 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
486 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
487 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
488 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
489 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
490 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
492 static int ethertype_to_ppptype(int);
493 static struct block
*gen_linktype(compiler_state_t
*, int);
494 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
495 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
496 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
497 int, int, u_int
, u_int
);
499 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
500 struct in6_addr
*, int, int, u_int
, u_int
);
502 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
503 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
504 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
505 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
506 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
507 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
508 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
509 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
510 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
513 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
514 struct in6_addr
*, int, int, int);
517 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
518 struct addrinfo
*, int, int);
520 static struct block
*gen_ipfrag(compiler_state_t
*);
521 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
522 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
524 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
525 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
527 struct block
*gen_portop(compiler_state_t
*, int, int, int);
528 static struct block
*gen_port(compiler_state_t
*, int, int, int);
529 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
530 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
531 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
532 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
533 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
534 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
535 static int lookup_proto(compiler_state_t
*, const char *, int);
536 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
537 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
538 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
539 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
540 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
541 static struct block
*gen_len(compiler_state_t
*, int, int);
542 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
543 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
545 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
546 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
549 initchunks(compiler_state_t
*cstate
)
553 for (i
= 0; i
< NCHUNKS
; i
++) {
554 cstate
->chunks
[i
].n_left
= 0;
555 cstate
->chunks
[i
].m
= NULL
;
557 cstate
->cur_chunk
= 0;
561 newchunk(compiler_state_t
*cstate
, size_t n
)
568 /* XXX Round up to nearest long. */
569 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
571 /* XXX Round up to structure boundary. */
575 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
576 if (n
> cp
->n_left
) {
577 ++cp
, k
= ++cstate
->cur_chunk
;
579 bpf_error(cstate
, "out of memory");
580 size
= CHUNK0SIZE
<< k
;
581 cp
->m
= (void *)malloc(size
);
583 bpf_error(cstate
, "out of memory");
584 memset((char *)cp
->m
, 0, size
);
587 bpf_error(cstate
, "out of memory");
590 return (void *)((char *)cp
->m
+ cp
->n_left
);
594 freechunks(compiler_state_t
*cstate
)
598 for (i
= 0; i
< NCHUNKS
; ++i
)
599 if (cstate
->chunks
[i
].m
!= NULL
)
600 free(cstate
->chunks
[i
].m
);
604 * A strdup whose allocations are freed after code generation is over.
607 sdup(compiler_state_t
*cstate
, const char *s
)
609 size_t n
= strlen(s
) + 1;
610 char *cp
= newchunk(cstate
, n
);
616 static inline struct block
*
617 new_block(compiler_state_t
*cstate
, int code
)
621 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
628 static inline struct slist
*
629 new_stmt(compiler_state_t
*cstate
, int code
)
633 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
639 static struct block
*
640 gen_retblk(compiler_state_t
*cstate
, int v
)
642 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
649 syntax(compiler_state_t
*cstate
)
651 bpf_error(cstate
, "syntax error in filter expression");
655 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
656 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
;
669 * If this pcap_t hasn't been activated, it doesn't have a
670 * link-layer type, so we can't use it.
673 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
674 "not-yet-activated pcap_t passed to pcap_compile");
686 * If the device on which we're capturing need to be notified
687 * that a new filter is being compiled, do so.
689 * This allows them to save a copy of it, in case, for example,
690 * they're implementing a form of remote packet capture, and
691 * want the remote machine to filter out the packets in which
692 * it's sending the packets it's captured.
694 * XXX - the fact that we happen to be compiling a filter
695 * doesn't necessarily mean we'll be installing it as the
696 * filter for this pcap_t; we might be running it from userland
697 * on captured packets to do packet classification. We really
698 * need a better way of handling this, but this is all that
699 * the WinPcap code did.
701 if (p
->save_current_filter_op
!= NULL
)
702 (p
->save_current_filter_op
)(p
, buf
);
706 cstate
.no_optimize
= 0;
710 cstate
.ic
.root
= NULL
;
711 cstate
.ic
.cur_mark
= 0;
715 if (setjmp(cstate
.top_ctx
)) {
717 if (cstate
.ai
!= NULL
)
718 freeaddrinfo(cstate
.ai
);
724 cstate
.netmask
= mask
;
726 cstate
.snaplen
= pcap_snapshot(p
);
727 if (cstate
.snaplen
== 0) {
728 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
729 "snaplen of 0 rejects all packets");
734 if (pcap_lex_init(&scanner
) != 0)
735 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
736 errno
, "can't initialize scanner");
737 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
740 * Associate the compiler state with the lexical analyzer
743 pcap_set_extra(&cstate
, scanner
);
745 init_linktype(&cstate
, p
);
746 (void)pcap_parse(scanner
, &cstate
);
748 if (cstate
.ic
.root
== NULL
)
749 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
751 if (optimize
&& !cstate
.no_optimize
) {
752 bpf_optimize(&cstate
, &cstate
.ic
);
753 if (cstate
.ic
.root
== NULL
||
754 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
755 bpf_error(&cstate
, "expression rejects all packets");
757 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
758 program
->bf_len
= len
;
760 rc
= 0; /* We're all okay */
764 * Clean up everything for the lexical analyzer.
766 if (in_buffer
!= NULL
)
767 pcap__delete_buffer(in_buffer
, scanner
);
769 pcap_lex_destroy(scanner
);
772 * Clean up our own allocated memory.
780 * entry point for using the compiler with no pcap open
781 * pass in all the stuff that is needed explicitly instead.
784 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
785 struct bpf_program
*program
,
786 const char *buf
, int optimize
, bpf_u_int32 mask
)
791 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
794 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
800 * Clean up a "struct bpf_program" by freeing all the memory allocated
804 pcap_freecode(struct bpf_program
*program
)
807 if (program
->bf_insns
!= NULL
) {
808 free((char *)program
->bf_insns
);
809 program
->bf_insns
= NULL
;
814 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
815 * which of the jt and jf fields has been resolved and which is a pointer
816 * back to another unresolved block (or nil). At least one of the fields
817 * in each block is already resolved.
820 backpatch(struct block
*list
, struct block
*target
)
837 * Merge the lists in b0 and b1, using the 'sense' field to indicate
838 * which of jt and jf is the link.
841 merge(struct block
*b0
, struct block
*b1
)
843 register struct block
**p
= &b0
;
845 /* Find end of list. */
847 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
849 /* Concatenate the lists. */
854 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
856 struct block
*ppi_dlt_check
;
859 * Insert before the statements of the first (root) block any
860 * statements needed to load the lengths of any variable-length
861 * headers into registers.
863 * XXX - a fancier strategy would be to insert those before the
864 * statements of all blocks that use those lengths and that
865 * have no predecessors that use them, so that we only compute
866 * the lengths if we need them. There might be even better
867 * approaches than that.
869 * However, those strategies would be more complicated, and
870 * as we don't generate code to compute a length if the
871 * program has no tests that use the length, and as most
872 * tests will probably use those lengths, we would just
873 * postpone computing the lengths so that it's not done
874 * for tests that fail early, and it's not clear that's
877 insert_compute_vloffsets(cstate
, p
->head
);
880 * For DLT_PPI captures, generate a check of the per-packet
881 * DLT value to make sure it's DLT_IEEE802_11.
883 * XXX - TurboCap cards use DLT_PPI for Ethernet.
884 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
885 * with appropriate Ethernet information and use that rather
886 * than using something such as DLT_PPI where you don't know
887 * the link-layer header type until runtime, which, in the
888 * general case, would force us to generate both Ethernet *and*
889 * 802.11 code (*and* anything else for which PPI is used)
890 * and choose between them early in the BPF program?
892 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
893 if (ppi_dlt_check
!= NULL
)
894 gen_and(ppi_dlt_check
, p
);
896 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
897 p
->sense
= !p
->sense
;
898 backpatch(p
, gen_retblk(cstate
, 0));
899 cstate
->ic
.root
= p
->head
;
903 gen_and(struct block
*b0
, struct block
*b1
)
905 backpatch(b0
, b1
->head
);
906 b0
->sense
= !b0
->sense
;
907 b1
->sense
= !b1
->sense
;
909 b1
->sense
= !b1
->sense
;
914 gen_or(struct block
*b0
, struct block
*b1
)
916 b0
->sense
= !b0
->sense
;
917 backpatch(b0
, b1
->head
);
918 b0
->sense
= !b0
->sense
;
924 gen_not(struct block
*b
)
926 b
->sense
= !b
->sense
;
929 static struct block
*
930 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
931 u_int size
, bpf_int32 v
)
933 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
936 static struct block
*
937 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
938 u_int size
, bpf_int32 v
)
940 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
943 static struct block
*
944 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
945 u_int size
, bpf_int32 v
)
947 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
950 static struct block
*
951 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
952 u_int size
, bpf_int32 v
)
954 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
957 static struct block
*
958 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
959 u_int size
, bpf_int32 v
)
961 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
964 static struct block
*
965 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
966 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
968 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
971 static struct block
*
972 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
973 u_int size
, const u_char
*v
)
975 register struct block
*b
, *tmp
;
979 register const u_char
*p
= &v
[size
- 4];
980 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
981 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
983 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
, w
);
990 register const u_char
*p
= &v
[size
- 2];
991 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
993 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
, w
);
1000 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1009 * AND the field of size "size" at offset "offset" relative to the header
1010 * specified by "offrel" with "mask", and compare it with the value "v"
1011 * with the test specified by "jtype"; if "reverse" is true, the test
1012 * should test the opposite of "jtype".
1014 static struct block
*
1015 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1016 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1019 struct slist
*s
, *s2
;
1022 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1024 if (mask
!= 0xffffffff) {
1025 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1030 b
= new_block(cstate
, JMP(jtype
));
1033 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1039 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1041 cstate
->pcap_fddipad
= p
->fddipad
;
1044 * We start out with only one link-layer header.
1046 cstate
->outermostlinktype
= pcap_datalink(p
);
1047 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1048 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1049 cstate
->off_outermostlinkhdr
.reg
= -1;
1051 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1052 cstate
->off_prevlinkhdr
.constant_part
= 0;
1053 cstate
->off_prevlinkhdr
.is_variable
= 0;
1054 cstate
->off_prevlinkhdr
.reg
= -1;
1056 cstate
->linktype
= cstate
->outermostlinktype
;
1057 cstate
->off_linkhdr
.constant_part
= 0;
1058 cstate
->off_linkhdr
.is_variable
= 0;
1059 cstate
->off_linkhdr
.reg
= -1;
1064 cstate
->off_linkpl
.constant_part
= 0;
1065 cstate
->off_linkpl
.is_variable
= 0;
1066 cstate
->off_linkpl
.reg
= -1;
1068 cstate
->off_linktype
.constant_part
= 0;
1069 cstate
->off_linktype
.is_variable
= 0;
1070 cstate
->off_linktype
.reg
= -1;
1073 * Assume it's not raw ATM with a pseudo-header, for now.
1076 cstate
->off_vpi
= -1;
1077 cstate
->off_vci
= -1;
1078 cstate
->off_proto
= -1;
1079 cstate
->off_payload
= -1;
1084 cstate
->is_geneve
= 0;
1087 * No variable length VLAN offset by default
1089 cstate
->is_vlan_vloffset
= 0;
1092 * And assume we're not doing SS7.
1094 cstate
->off_li
= -1;
1095 cstate
->off_li_hsl
= -1;
1096 cstate
->off_sio
= -1;
1097 cstate
->off_opc
= -1;
1098 cstate
->off_dpc
= -1;
1099 cstate
->off_sls
= -1;
1101 cstate
->label_stack_depth
= 0;
1102 cstate
->vlan_stack_depth
= 0;
1104 switch (cstate
->linktype
) {
1107 cstate
->off_linktype
.constant_part
= 2;
1108 cstate
->off_linkpl
.constant_part
= 6;
1109 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1110 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1113 case DLT_ARCNET_LINUX
:
1114 cstate
->off_linktype
.constant_part
= 4;
1115 cstate
->off_linkpl
.constant_part
= 8;
1116 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1117 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1121 cstate
->off_linktype
.constant_part
= 12;
1122 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1123 cstate
->off_nl
= 0; /* Ethernet II */
1124 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1129 * SLIP doesn't have a link level type. The 16 byte
1130 * header is hacked into our SLIP driver.
1132 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1133 cstate
->off_linkpl
.constant_part
= 16;
1135 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1138 case DLT_SLIP_BSDOS
:
1139 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1140 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1142 cstate
->off_linkpl
.constant_part
= 24;
1144 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1149 cstate
->off_linktype
.constant_part
= 0;
1150 cstate
->off_linkpl
.constant_part
= 4;
1152 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1156 cstate
->off_linktype
.constant_part
= 0;
1157 cstate
->off_linkpl
.constant_part
= 12;
1159 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1164 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1165 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1166 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1167 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1169 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1174 * This does no include the Ethernet header, and
1175 * only covers session state.
1177 cstate
->off_linktype
.constant_part
= 6;
1178 cstate
->off_linkpl
.constant_part
= 8;
1180 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1184 cstate
->off_linktype
.constant_part
= 5;
1185 cstate
->off_linkpl
.constant_part
= 24;
1187 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1192 * FDDI doesn't really have a link-level type field.
1193 * We set "off_linktype" to the offset of the LLC header.
1195 * To check for Ethernet types, we assume that SSAP = SNAP
1196 * is being used and pick out the encapsulated Ethernet type.
1197 * XXX - should we generate code to check for SNAP?
1199 cstate
->off_linktype
.constant_part
= 13;
1200 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1201 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1202 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1203 cstate
->off_nl
= 8; /* 802.2+SNAP */
1204 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1209 * Token Ring doesn't really have a link-level type field.
1210 * We set "off_linktype" to the offset of the LLC header.
1212 * To check for Ethernet types, we assume that SSAP = SNAP
1213 * is being used and pick out the encapsulated Ethernet type.
1214 * XXX - should we generate code to check for SNAP?
1216 * XXX - the header is actually variable-length.
1217 * Some various Linux patched versions gave 38
1218 * as "off_linktype" and 40 as "off_nl"; however,
1219 * if a token ring packet has *no* routing
1220 * information, i.e. is not source-routed, the correct
1221 * values are 20 and 22, as they are in the vanilla code.
1223 * A packet is source-routed iff the uppermost bit
1224 * of the first byte of the source address, at an
1225 * offset of 8, has the uppermost bit set. If the
1226 * packet is source-routed, the total number of bytes
1227 * of routing information is 2 plus bits 0x1F00 of
1228 * the 16-bit value at an offset of 14 (shifted right
1229 * 8 - figure out which byte that is).
1231 cstate
->off_linktype
.constant_part
= 14;
1232 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1233 cstate
->off_nl
= 8; /* 802.2+SNAP */
1234 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1237 case DLT_PRISM_HEADER
:
1238 case DLT_IEEE802_11_RADIO_AVS
:
1239 case DLT_IEEE802_11_RADIO
:
1240 cstate
->off_linkhdr
.is_variable
= 1;
1241 /* Fall through, 802.11 doesn't have a variable link
1242 * prefix but is otherwise the same. */
1244 case DLT_IEEE802_11
:
1246 * 802.11 doesn't really have a link-level type field.
1247 * We set "off_linktype.constant_part" to the offset of
1250 * To check for Ethernet types, we assume that SSAP = SNAP
1251 * is being used and pick out the encapsulated Ethernet type.
1252 * XXX - should we generate code to check for SNAP?
1254 * We also handle variable-length radio headers here.
1255 * The Prism header is in theory variable-length, but in
1256 * practice it's always 144 bytes long. However, some
1257 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1258 * sometimes or always supply an AVS header, so we
1259 * have to check whether the radio header is a Prism
1260 * header or an AVS header, so, in practice, it's
1263 cstate
->off_linktype
.constant_part
= 24;
1264 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1265 cstate
->off_linkpl
.is_variable
= 1;
1266 cstate
->off_nl
= 8; /* 802.2+SNAP */
1267 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1272 * At the moment we treat PPI the same way that we treat
1273 * normal Radiotap encoded packets. The difference is in
1274 * the function that generates the code at the beginning
1275 * to compute the header length. Since this code generator
1276 * of PPI supports bare 802.11 encapsulation only (i.e.
1277 * the encapsulated DLT should be DLT_IEEE802_11) we
1278 * generate code to check for this too.
1280 cstate
->off_linktype
.constant_part
= 24;
1281 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1282 cstate
->off_linkpl
.is_variable
= 1;
1283 cstate
->off_linkhdr
.is_variable
= 1;
1284 cstate
->off_nl
= 8; /* 802.2+SNAP */
1285 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1288 case DLT_ATM_RFC1483
:
1289 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1291 * assume routed, non-ISO PDUs
1292 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1294 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1295 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1296 * latter would presumably be treated the way PPPoE
1297 * should be, so you can do "pppoe and udp port 2049"
1298 * or "pppoa and tcp port 80" and have it check for
1299 * PPPo{A,E} and a PPP protocol of IP and....
1301 cstate
->off_linktype
.constant_part
= 0;
1302 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1303 cstate
->off_nl
= 8; /* 802.2+SNAP */
1304 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1309 * Full Frontal ATM; you get AALn PDUs with an ATM
1313 cstate
->off_vpi
= SUNATM_VPI_POS
;
1314 cstate
->off_vci
= SUNATM_VCI_POS
;
1315 cstate
->off_proto
= PROTO_POS
;
1316 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1317 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1318 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1319 cstate
->off_nl
= 8; /* 802.2+SNAP */
1320 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1326 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1327 cstate
->off_linkpl
.constant_part
= 0;
1329 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1332 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1333 cstate
->off_linktype
.constant_part
= 14;
1334 cstate
->off_linkpl
.constant_part
= 16;
1336 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1341 * LocalTalk does have a 1-byte type field in the LLAP header,
1342 * but really it just indicates whether there is a "short" or
1343 * "long" DDP packet following.
1345 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1346 cstate
->off_linkpl
.constant_part
= 0;
1348 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1351 case DLT_IP_OVER_FC
:
1353 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1354 * link-level type field. We set "off_linktype" to the
1355 * offset of the LLC header.
1357 * To check for Ethernet types, we assume that SSAP = SNAP
1358 * is being used and pick out the encapsulated Ethernet type.
1359 * XXX - should we generate code to check for SNAP? RFC
1360 * 2625 says SNAP should be used.
1362 cstate
->off_linktype
.constant_part
= 16;
1363 cstate
->off_linkpl
.constant_part
= 16;
1364 cstate
->off_nl
= 8; /* 802.2+SNAP */
1365 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1370 * XXX - we should set this to handle SNAP-encapsulated
1371 * frames (NLPID of 0x80).
1373 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1374 cstate
->off_linkpl
.constant_part
= 0;
1376 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1380 * the only BPF-interesting FRF.16 frames are non-control frames;
1381 * Frame Relay has a variable length link-layer
1382 * so lets start with offset 4 for now and increments later on (FIXME);
1385 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1386 cstate
->off_linkpl
.constant_part
= 0;
1388 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1391 case DLT_APPLE_IP_OVER_IEEE1394
:
1392 cstate
->off_linktype
.constant_part
= 16;
1393 cstate
->off_linkpl
.constant_part
= 18;
1395 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1398 case DLT_SYMANTEC_FIREWALL
:
1399 cstate
->off_linktype
.constant_part
= 6;
1400 cstate
->off_linkpl
.constant_part
= 44;
1401 cstate
->off_nl
= 0; /* Ethernet II */
1402 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1405 #ifdef HAVE_NET_PFVAR_H
1407 cstate
->off_linktype
.constant_part
= 0;
1408 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1410 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1414 case DLT_JUNIPER_MFR
:
1415 case DLT_JUNIPER_MLFR
:
1416 case DLT_JUNIPER_MLPPP
:
1417 case DLT_JUNIPER_PPP
:
1418 case DLT_JUNIPER_CHDLC
:
1419 case DLT_JUNIPER_FRELAY
:
1420 cstate
->off_linktype
.constant_part
= 4;
1421 cstate
->off_linkpl
.constant_part
= 4;
1423 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1426 case DLT_JUNIPER_ATM1
:
1427 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1428 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1430 cstate
->off_nl_nosnap
= 10;
1433 case DLT_JUNIPER_ATM2
:
1434 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1435 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1437 cstate
->off_nl_nosnap
= 10;
1440 /* frames captured on a Juniper PPPoE service PIC
1441 * contain raw ethernet frames */
1442 case DLT_JUNIPER_PPPOE
:
1443 case DLT_JUNIPER_ETHER
:
1444 cstate
->off_linkpl
.constant_part
= 14;
1445 cstate
->off_linktype
.constant_part
= 16;
1446 cstate
->off_nl
= 18; /* Ethernet II */
1447 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1450 case DLT_JUNIPER_PPPOE_ATM
:
1451 cstate
->off_linktype
.constant_part
= 4;
1452 cstate
->off_linkpl
.constant_part
= 6;
1454 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1457 case DLT_JUNIPER_GGSN
:
1458 cstate
->off_linktype
.constant_part
= 6;
1459 cstate
->off_linkpl
.constant_part
= 12;
1461 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1464 case DLT_JUNIPER_ES
:
1465 cstate
->off_linktype
.constant_part
= 6;
1466 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1467 cstate
->off_nl
= -1; /* not really a network layer but raw IP addresses */
1468 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1471 case DLT_JUNIPER_MONITOR
:
1472 cstate
->off_linktype
.constant_part
= 12;
1473 cstate
->off_linkpl
.constant_part
= 12;
1474 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1475 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1478 case DLT_BACNET_MS_TP
:
1479 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1480 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1481 cstate
->off_nl
= -1;
1482 cstate
->off_nl_nosnap
= -1;
1485 case DLT_JUNIPER_SERVICES
:
1486 cstate
->off_linktype
.constant_part
= 12;
1487 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1488 cstate
->off_nl
= -1; /* L3 proto location dep. on cookie type */
1489 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1492 case DLT_JUNIPER_VP
:
1493 cstate
->off_linktype
.constant_part
= 18;
1494 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1495 cstate
->off_nl
= -1;
1496 cstate
->off_nl_nosnap
= -1;
1499 case DLT_JUNIPER_ST
:
1500 cstate
->off_linktype
.constant_part
= 18;
1501 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1502 cstate
->off_nl
= -1;
1503 cstate
->off_nl_nosnap
= -1;
1506 case DLT_JUNIPER_ISM
:
1507 cstate
->off_linktype
.constant_part
= 8;
1508 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1509 cstate
->off_nl
= -1;
1510 cstate
->off_nl_nosnap
= -1;
1513 case DLT_JUNIPER_VS
:
1514 case DLT_JUNIPER_SRX_E2E
:
1515 case DLT_JUNIPER_FIBRECHANNEL
:
1516 case DLT_JUNIPER_ATM_CEMIC
:
1517 cstate
->off_linktype
.constant_part
= 8;
1518 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1519 cstate
->off_nl
= -1;
1520 cstate
->off_nl_nosnap
= -1;
1525 cstate
->off_li_hsl
= 4;
1526 cstate
->off_sio
= 3;
1527 cstate
->off_opc
= 4;
1528 cstate
->off_dpc
= 4;
1529 cstate
->off_sls
= 7;
1530 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1531 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1532 cstate
->off_nl
= -1;
1533 cstate
->off_nl_nosnap
= -1;
1536 case DLT_MTP2_WITH_PHDR
:
1538 cstate
->off_li_hsl
= 8;
1539 cstate
->off_sio
= 7;
1540 cstate
->off_opc
= 8;
1541 cstate
->off_dpc
= 8;
1542 cstate
->off_sls
= 11;
1543 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1544 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1545 cstate
->off_nl
= -1;
1546 cstate
->off_nl_nosnap
= -1;
1550 cstate
->off_li
= 22;
1551 cstate
->off_li_hsl
= 24;
1552 cstate
->off_sio
= 23;
1553 cstate
->off_opc
= 24;
1554 cstate
->off_dpc
= 24;
1555 cstate
->off_sls
= 27;
1556 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1557 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1558 cstate
->off_nl
= -1;
1559 cstate
->off_nl_nosnap
= -1;
1563 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1564 cstate
->off_linkpl
.constant_part
= 4;
1566 cstate
->off_nl_nosnap
= 0;
1571 * Currently, only raw "link[N:M]" filtering is supported.
1573 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1574 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1575 cstate
->off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1576 cstate
->off_nl_nosnap
= -1; /* no 802.2 LLC */
1580 cstate
->off_linktype
.constant_part
= 1;
1581 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1583 cstate
->off_nl_nosnap
= -1;
1586 case DLT_NETANALYZER
:
1587 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1588 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1589 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1590 cstate
->off_nl
= 0; /* Ethernet II */
1591 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1594 case DLT_NETANALYZER_TRANSPARENT
:
1595 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1596 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1597 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1598 cstate
->off_nl
= 0; /* Ethernet II */
1599 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1604 * For values in the range in which we've assigned new
1605 * DLT_ values, only raw "link[N:M]" filtering is supported.
1607 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1608 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1609 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1610 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1611 cstate
->off_nl
= -1;
1612 cstate
->off_nl_nosnap
= -1;
1614 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1619 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1623 * Load a value relative to the specified absolute offset.
1625 static struct slist
*
1626 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1627 u_int offset
, u_int size
)
1629 struct slist
*s
, *s2
;
1631 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1634 * If "s" is non-null, it has code to arrange that the X register
1635 * contains the variable part of the absolute offset, so we
1636 * generate a load relative to that, with an offset of
1637 * abs_offset->constant_part + offset.
1639 * Otherwise, we can do an absolute load with an offset of
1640 * abs_offset->constant_part + offset.
1644 * "s" points to a list of statements that puts the
1645 * variable part of the absolute offset into the X register.
1646 * Do an indirect load, to use the X register as an offset.
1648 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1649 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1653 * There is no variable part of the absolute offset, so
1654 * just do an absolute load.
1656 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1657 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1663 * Load a value relative to the beginning of the specified header.
1665 static struct slist
*
1666 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1669 struct slist
*s
, *s2
;
1674 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1679 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1682 case OR_PREVLINKHDR
:
1683 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1687 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1690 case OR_PREVMPLSHDR
:
1691 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1695 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1698 case OR_LINKPL_NOSNAP
:
1699 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1703 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1708 * Load the X register with the length of the IPv4 header
1709 * (plus the offset of the link-layer header, if it's
1710 * preceded by a variable-length header such as a radio
1711 * header), in bytes.
1713 s
= gen_loadx_iphdrlen(cstate
);
1716 * Load the item at {offset of the link-layer payload} +
1717 * {offset, relative to the start of the link-layer
1718 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1719 * {specified offset}.
1721 * If the offset of the link-layer payload is variable,
1722 * the variable part of that offset is included in the
1723 * value in the X register, and we include the constant
1724 * part in the offset of the load.
1726 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1727 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1732 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1743 * Generate code to load into the X register the sum of the length of
1744 * the IPv4 header and the variable part of the offset of the link-layer
1747 static struct slist
*
1748 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1750 struct slist
*s
, *s2
;
1752 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1755 * The offset of the link-layer payload has a variable
1756 * part. "s" points to a list of statements that put
1757 * the variable part of that offset into the X register.
1759 * The 4*([k]&0xf) addressing mode can't be used, as we
1760 * don't have a constant offset, so we have to load the
1761 * value in question into the A register and add to it
1762 * the value from the X register.
1764 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1765 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1767 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1770 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1775 * The A register now contains the length of the IP header.
1776 * We need to add to it the variable part of the offset of
1777 * the link-layer payload, which is still in the X
1778 * register, and move the result into the X register.
1780 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1781 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1784 * The offset of the link-layer payload is a constant,
1785 * so no code was generated to load the (non-existent)
1786 * variable part of that offset.
1788 * This means we can use the 4*([k]&0xf) addressing
1789 * mode. Load the length of the IPv4 header, which
1790 * is at an offset of cstate->off_nl from the beginning of
1791 * the link-layer payload, and thus at an offset of
1792 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1793 * of the raw packet data, using that addressing mode.
1795 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1796 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1802 static struct block
*
1803 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1808 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1810 b
= new_block(cstate
, JMP(BPF_JEQ
));
1816 static inline struct block
*
1817 gen_true(compiler_state_t
*cstate
)
1819 return gen_uncond(cstate
, 1);
1822 static inline struct block
*
1823 gen_false(compiler_state_t
*cstate
)
1825 return gen_uncond(cstate
, 0);
1829 * Byte-swap a 32-bit number.
1830 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1831 * big-endian platforms.)
1833 #define SWAPLONG(y) \
1834 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1837 * Generate code to match a particular packet type.
1839 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1840 * value, if <= ETHERMTU. We use that to determine whether to
1841 * match the type/length field or to check the type/length field for
1842 * a value <= ETHERMTU to see whether it's a type field and then do
1843 * the appropriate test.
1845 static struct block
*
1846 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1848 struct block
*b0
, *b1
;
1854 case LLCSAP_NETBEUI
:
1856 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1857 * so we check the DSAP and SSAP.
1859 * LLCSAP_IP checks for IP-over-802.2, rather
1860 * than IP-over-Ethernet or IP-over-SNAP.
1862 * XXX - should we check both the DSAP and the
1863 * SSAP, like this, or should we check just the
1864 * DSAP, as we do for other types <= ETHERMTU
1865 * (i.e., other SAP values)?
1867 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1869 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1870 ((proto
<< 8) | proto
));
1878 * Ethernet_II frames, which are Ethernet
1879 * frames with a frame type of ETHERTYPE_IPX;
1881 * Ethernet_802.3 frames, which are 802.3
1882 * frames (i.e., the type/length field is
1883 * a length field, <= ETHERMTU, rather than
1884 * a type field) with the first two bytes
1885 * after the Ethernet/802.3 header being
1888 * Ethernet_802.2 frames, which are 802.3
1889 * frames with an 802.2 LLC header and
1890 * with the IPX LSAP as the DSAP in the LLC
1893 * Ethernet_SNAP frames, which are 802.3
1894 * frames with an LLC header and a SNAP
1895 * header and with an OUI of 0x000000
1896 * (encapsulated Ethernet) and a protocol
1897 * ID of ETHERTYPE_IPX in the SNAP header.
1899 * XXX - should we generate the same code both
1900 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1904 * This generates code to check both for the
1905 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1907 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1908 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1912 * Now we add code to check for SNAP frames with
1913 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1915 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1919 * Now we generate code to check for 802.3
1920 * frames in general.
1922 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1926 * Now add the check for 802.3 frames before the
1927 * check for Ethernet_802.2 and Ethernet_802.3,
1928 * as those checks should only be done on 802.3
1929 * frames, not on Ethernet frames.
1934 * Now add the check for Ethernet_II frames, and
1935 * do that before checking for the other frame
1938 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1942 case ETHERTYPE_ATALK
:
1943 case ETHERTYPE_AARP
:
1945 * EtherTalk (AppleTalk protocols on Ethernet link
1946 * layer) may use 802.2 encapsulation.
1950 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1951 * we check for an Ethernet type field less than
1952 * 1500, which means it's an 802.3 length field.
1954 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1958 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1959 * SNAP packets with an organization code of
1960 * 0x080007 (Apple, for Appletalk) and a protocol
1961 * type of ETHERTYPE_ATALK (Appletalk).
1963 * 802.2-encapsulated ETHERTYPE_AARP packets are
1964 * SNAP packets with an organization code of
1965 * 0x000000 (encapsulated Ethernet) and a protocol
1966 * type of ETHERTYPE_AARP (Appletalk ARP).
1968 if (proto
== ETHERTYPE_ATALK
)
1969 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
1970 else /* proto == ETHERTYPE_AARP */
1971 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
1975 * Check for Ethernet encapsulation (Ethertalk
1976 * phase 1?); we just check for the Ethernet
1979 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1985 if (proto
<= ETHERMTU
) {
1987 * This is an LLC SAP value, so the frames
1988 * that match would be 802.2 frames.
1989 * Check that the frame is an 802.2 frame
1990 * (i.e., that the length/type field is
1991 * a length field, <= ETHERMTU) and
1992 * then check the DSAP.
1994 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1996 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2001 * This is an Ethernet type, so compare
2002 * the length/type field with it (if
2003 * the frame is an 802.2 frame, the length
2004 * field will be <= ETHERMTU, and, as
2005 * "proto" is > ETHERMTU, this test
2006 * will fail and the frame won't match,
2007 * which is what we want).
2009 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2015 static struct block
*
2016 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2019 * For DLT_NULL, the link-layer header is a 32-bit word
2020 * containing an AF_ value in *host* byte order, and for
2021 * DLT_ENC, the link-layer header begins with a 32-bit
2022 * word containing an AF_ value in host byte order.
2024 * In addition, if we're reading a saved capture file,
2025 * the host byte order in the capture may not be the
2026 * same as the host byte order on this machine.
2028 * For DLT_LOOP, the link-layer header is a 32-bit
2029 * word containing an AF_ value in *network* byte order.
2031 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2033 * The AF_ value is in host byte order, but the BPF
2034 * interpreter will convert it to network byte order.
2036 * If this is a save file, and it's from a machine
2037 * with the opposite byte order to ours, we byte-swap
2040 * Then we run it through "htonl()", and generate
2041 * code to compare against the result.
2043 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2044 proto
= SWAPLONG(proto
);
2045 proto
= htonl(proto
);
2047 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2051 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2052 * or IPv6 then we have an error.
2054 static struct block
*
2055 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2060 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2063 case ETHERTYPE_IPV6
:
2064 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2065 (bpf_int32
)IPH_AF_INET6
);
2072 return gen_false(cstate
);
2076 * Generate code to match a particular packet type.
2078 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2079 * value, if <= ETHERMTU. We use that to determine whether to
2080 * match the type field or to check the type field for the special
2081 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2083 static struct block
*
2084 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2086 struct block
*b0
, *b1
;
2092 case LLCSAP_NETBEUI
:
2094 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2095 * so we check the DSAP and SSAP.
2097 * LLCSAP_IP checks for IP-over-802.2, rather
2098 * than IP-over-Ethernet or IP-over-SNAP.
2100 * XXX - should we check both the DSAP and the
2101 * SSAP, like this, or should we check just the
2102 * DSAP, as we do for other types <= ETHERMTU
2103 * (i.e., other SAP values)?
2105 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2106 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2107 ((proto
<< 8) | proto
));
2113 * Ethernet_II frames, which are Ethernet
2114 * frames with a frame type of ETHERTYPE_IPX;
2116 * Ethernet_802.3 frames, which have a frame
2117 * type of LINUX_SLL_P_802_3;
2119 * Ethernet_802.2 frames, which are 802.3
2120 * frames with an 802.2 LLC header (i.e, have
2121 * a frame type of LINUX_SLL_P_802_2) and
2122 * with the IPX LSAP as the DSAP in the LLC
2125 * Ethernet_SNAP frames, which are 802.3
2126 * frames with an LLC header and a SNAP
2127 * header and with an OUI of 0x000000
2128 * (encapsulated Ethernet) and a protocol
2129 * ID of ETHERTYPE_IPX in the SNAP header.
2131 * First, do the checks on LINUX_SLL_P_802_2
2132 * frames; generate the check for either
2133 * Ethernet_802.2 or Ethernet_SNAP frames, and
2134 * then put a check for LINUX_SLL_P_802_2 frames
2137 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2138 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2140 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2144 * Now check for 802.3 frames and OR that with
2145 * the previous test.
2147 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2151 * Now add the check for Ethernet_II frames, and
2152 * do that before checking for the other frame
2155 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2159 case ETHERTYPE_ATALK
:
2160 case ETHERTYPE_AARP
:
2162 * EtherTalk (AppleTalk protocols on Ethernet link
2163 * layer) may use 802.2 encapsulation.
2167 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2168 * we check for the 802.2 protocol type in the
2169 * "Ethernet type" field.
2171 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2174 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2175 * SNAP packets with an organization code of
2176 * 0x080007 (Apple, for Appletalk) and a protocol
2177 * type of ETHERTYPE_ATALK (Appletalk).
2179 * 802.2-encapsulated ETHERTYPE_AARP packets are
2180 * SNAP packets with an organization code of
2181 * 0x000000 (encapsulated Ethernet) and a protocol
2182 * type of ETHERTYPE_AARP (Appletalk ARP).
2184 if (proto
== ETHERTYPE_ATALK
)
2185 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2186 else /* proto == ETHERTYPE_AARP */
2187 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2191 * Check for Ethernet encapsulation (Ethertalk
2192 * phase 1?); we just check for the Ethernet
2195 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2201 if (proto
<= ETHERMTU
) {
2203 * This is an LLC SAP value, so the frames
2204 * that match would be 802.2 frames.
2205 * Check for the 802.2 protocol type
2206 * in the "Ethernet type" field, and
2207 * then check the DSAP.
2209 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2210 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2216 * This is an Ethernet type, so compare
2217 * the length/type field with it (if
2218 * the frame is an 802.2 frame, the length
2219 * field will be <= ETHERMTU, and, as
2220 * "proto" is > ETHERMTU, this test
2221 * will fail and the frame won't match,
2222 * which is what we want).
2224 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2229 static struct slist
*
2230 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2232 struct slist
*s1
, *s2
;
2233 struct slist
*sjeq_avs_cookie
;
2234 struct slist
*sjcommon
;
2237 * This code is not compatible with the optimizer, as
2238 * we are generating jmp instructions within a normal
2239 * slist of instructions
2241 cstate
->no_optimize
= 1;
2244 * Generate code to load the length of the radio header into
2245 * the register assigned to hold that length, if one has been
2246 * assigned. (If one hasn't been assigned, no code we've
2247 * generated uses that prefix, so we don't need to generate any
2250 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2251 * or always use the AVS header rather than the Prism header.
2252 * We load a 4-byte big-endian value at the beginning of the
2253 * raw packet data, and see whether, when masked with 0xFFFFF000,
2254 * it's equal to 0x80211000. If so, that indicates that it's
2255 * an AVS header (the masked-out bits are the version number).
2256 * Otherwise, it's a Prism header.
2258 * XXX - the Prism header is also, in theory, variable-length,
2259 * but no known software generates headers that aren't 144
2262 if (cstate
->off_linkhdr
.reg
!= -1) {
2266 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2270 * AND it with 0xFFFFF000.
2272 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2273 s2
->s
.k
= 0xFFFFF000;
2277 * Compare with 0x80211000.
2279 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2280 sjeq_avs_cookie
->s
.k
= 0x80211000;
2281 sappend(s1
, sjeq_avs_cookie
);
2286 * The 4 bytes at an offset of 4 from the beginning of
2287 * the AVS header are the length of the AVS header.
2288 * That field is big-endian.
2290 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2293 sjeq_avs_cookie
->s
.jt
= s2
;
2296 * Now jump to the code to allocate a register
2297 * into which to save the header length and
2298 * store the length there. (The "jump always"
2299 * instruction needs to have the k field set;
2300 * it's added to the PC, so, as we're jumping
2301 * over a single instruction, it should be 1.)
2303 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2305 sappend(s1
, sjcommon
);
2308 * Now for the code that handles the Prism header.
2309 * Just load the length of the Prism header (144)
2310 * into the A register. Have the test for an AVS
2311 * header branch here if we don't have an AVS header.
2313 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2316 sjeq_avs_cookie
->s
.jf
= s2
;
2319 * Now allocate a register to hold that value and store
2320 * it. The code for the AVS header will jump here after
2321 * loading the length of the AVS header.
2323 s2
= new_stmt(cstate
, BPF_ST
);
2324 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2326 sjcommon
->s
.jf
= s2
;
2329 * Now move it into the X register.
2331 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2339 static struct slist
*
2340 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2342 struct slist
*s1
, *s2
;
2345 * Generate code to load the length of the AVS header into
2346 * the register assigned to hold that length, if one has been
2347 * assigned. (If one hasn't been assigned, no code we've
2348 * generated uses that prefix, so we don't need to generate any
2351 if (cstate
->off_linkhdr
.reg
!= -1) {
2353 * The 4 bytes at an offset of 4 from the beginning of
2354 * the AVS header are the length of the AVS header.
2355 * That field is big-endian.
2357 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2361 * Now allocate a register to hold that value and store
2364 s2
= new_stmt(cstate
, BPF_ST
);
2365 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2369 * Now move it into the X register.
2371 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2379 static struct slist
*
2380 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2382 struct slist
*s1
, *s2
;
2385 * Generate code to load the length of the radiotap header into
2386 * the register assigned to hold that length, if one has been
2387 * assigned. (If one hasn't been assigned, no code we've
2388 * generated uses that prefix, so we don't need to generate any
2391 if (cstate
->off_linkhdr
.reg
!= -1) {
2393 * The 2 bytes at offsets of 2 and 3 from the beginning
2394 * of the radiotap header are the length of the radiotap
2395 * header; unfortunately, it's little-endian, so we have
2396 * to load it a byte at a time and construct the value.
2400 * Load the high-order byte, at an offset of 3, shift it
2401 * left a byte, and put the result in the X register.
2403 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2405 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2408 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2412 * Load the next byte, at an offset of 2, and OR the
2413 * value from the X register into it.
2415 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2418 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2422 * Now allocate a register to hold that value and store
2425 s2
= new_stmt(cstate
, BPF_ST
);
2426 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2430 * Now move it into the X register.
2432 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2441 * At the moment we treat PPI as normal Radiotap encoded
2442 * packets. The difference is in the function that generates
2443 * the code at the beginning to compute the header length.
2444 * Since this code generator of PPI supports bare 802.11
2445 * encapsulation only (i.e. the encapsulated DLT should be
2446 * DLT_IEEE802_11) we generate code to check for this too;
2447 * that's done in finish_parse().
2449 static struct slist
*
2450 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2452 struct slist
*s1
, *s2
;
2455 * Generate code to load the length of the radiotap header
2456 * into the register assigned to hold that length, if one has
2459 if (cstate
->off_linkhdr
.reg
!= -1) {
2461 * The 2 bytes at offsets of 2 and 3 from the beginning
2462 * of the radiotap header are the length of the radiotap
2463 * header; unfortunately, it's little-endian, so we have
2464 * to load it a byte at a time and construct the value.
2468 * Load the high-order byte, at an offset of 3, shift it
2469 * left a byte, and put the result in the X register.
2471 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2473 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2476 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2480 * Load the next byte, at an offset of 2, and OR the
2481 * value from the X register into it.
2483 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2486 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2490 * Now allocate a register to hold that value and store
2493 s2
= new_stmt(cstate
, BPF_ST
);
2494 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2498 * Now move it into the X register.
2500 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2509 * Load a value relative to the beginning of the link-layer header after the 802.11
2510 * header, i.e. LLC_SNAP.
2511 * The link-layer header doesn't necessarily begin at the beginning
2512 * of the packet data; there might be a variable-length prefix containing
2513 * radio information.
2515 static struct slist
*
2516 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2519 struct slist
*sjset_data_frame_1
;
2520 struct slist
*sjset_data_frame_2
;
2521 struct slist
*sjset_qos
;
2522 struct slist
*sjset_radiotap_flags_present
;
2523 struct slist
*sjset_radiotap_ext_present
;
2524 struct slist
*sjset_radiotap_tsft_present
;
2525 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2526 struct slist
*s_roundup
;
2528 if (cstate
->off_linkpl
.reg
== -1) {
2530 * No register has been assigned to the offset of
2531 * the link-layer payload, which means nobody needs
2532 * it; don't bother computing it - just return
2533 * what we already have.
2539 * This code is not compatible with the optimizer, as
2540 * we are generating jmp instructions within a normal
2541 * slist of instructions
2543 cstate
->no_optimize
= 1;
2546 * If "s" is non-null, it has code to arrange that the X register
2547 * contains the length of the prefix preceding the link-layer
2550 * Otherwise, the length of the prefix preceding the link-layer
2551 * header is "off_outermostlinkhdr.constant_part".
2555 * There is no variable-length header preceding the
2556 * link-layer header.
2558 * Load the length of the fixed-length prefix preceding
2559 * the link-layer header (if any) into the X register,
2560 * and store it in the cstate->off_linkpl.reg register.
2561 * That length is off_outermostlinkhdr.constant_part.
2563 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2564 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2568 * The X register contains the offset of the beginning of the
2569 * link-layer header; add 24, which is the minimum length
2570 * of the MAC header for a data frame, to that, and store it
2571 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2572 * which is at the offset in the X register, with an indexed load.
2574 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2576 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2579 s2
= new_stmt(cstate
, BPF_ST
);
2580 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2583 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2588 * Check the Frame Control field to see if this is a data frame;
2589 * a data frame has the 0x08 bit (b3) in that field set and the
2590 * 0x04 bit (b2) clear.
2592 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2593 sjset_data_frame_1
->s
.k
= 0x08;
2594 sappend(s
, sjset_data_frame_1
);
2597 * If b3 is set, test b2, otherwise go to the first statement of
2598 * the rest of the program.
2600 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2601 sjset_data_frame_2
->s
.k
= 0x04;
2602 sappend(s
, sjset_data_frame_2
);
2603 sjset_data_frame_1
->s
.jf
= snext
;
2606 * If b2 is not set, this is a data frame; test the QoS bit.
2607 * Otherwise, go to the first statement of the rest of the
2610 sjset_data_frame_2
->s
.jt
= snext
;
2611 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2612 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2613 sappend(s
, sjset_qos
);
2616 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2618 * Otherwise, go to the first statement of the rest of the
2621 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2622 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2624 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2627 s2
= new_stmt(cstate
, BPF_ST
);
2628 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2632 * If we have a radiotap header, look at it to see whether
2633 * there's Atheros padding between the MAC-layer header
2636 * Note: all of the fields in the radiotap header are
2637 * little-endian, so we byte-swap all of the values
2638 * we test against, as they will be loaded as big-endian
2641 * XXX - in the general case, we would have to scan through
2642 * *all* the presence bits, if there's more than one word of
2643 * presence bits. That would require a loop, meaning that
2644 * we wouldn't be able to run the filter in the kernel.
2646 * We assume here that the Atheros adapters that insert the
2647 * annoying padding don't have multiple antennae and therefore
2648 * do not generate radiotap headers with multiple presence words.
2650 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2652 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2653 * in the first presence flag word?
2655 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2659 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2660 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2661 sappend(s
, sjset_radiotap_flags_present
);
2664 * If not, skip all of this.
2666 sjset_radiotap_flags_present
->s
.jf
= snext
;
2669 * Otherwise, is the "extension" bit set in that word?
2671 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2672 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2673 sappend(s
, sjset_radiotap_ext_present
);
2674 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2677 * If so, skip all of this.
2679 sjset_radiotap_ext_present
->s
.jt
= snext
;
2682 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2684 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2685 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2686 sappend(s
, sjset_radiotap_tsft_present
);
2687 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2690 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2691 * at an offset of 16 from the beginning of the raw packet
2692 * data (8 bytes for the radiotap header and 8 bytes for
2695 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2698 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2701 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2703 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2704 sjset_tsft_datapad
->s
.k
= 0x20;
2705 sappend(s
, sjset_tsft_datapad
);
2708 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2709 * at an offset of 8 from the beginning of the raw packet
2710 * data (8 bytes for the radiotap header).
2712 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2715 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2718 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2720 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2721 sjset_notsft_datapad
->s
.k
= 0x20;
2722 sappend(s
, sjset_notsft_datapad
);
2725 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2726 * set, round the length of the 802.11 header to
2727 * a multiple of 4. Do that by adding 3 and then
2728 * dividing by and multiplying by 4, which we do by
2731 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2732 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2733 sappend(s
, s_roundup
);
2734 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2737 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2740 s2
= new_stmt(cstate
, BPF_ST
);
2741 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2744 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2745 sjset_tsft_datapad
->s
.jf
= snext
;
2746 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2747 sjset_notsft_datapad
->s
.jf
= snext
;
2749 sjset_qos
->s
.jf
= snext
;
2755 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2759 /* There is an implicit dependency between the link
2760 * payload and link header since the payload computation
2761 * includes the variable part of the header. Therefore,
2762 * if nobody else has allocated a register for the link
2763 * header and we need it, do it now. */
2764 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2765 cstate
->off_linkhdr
.reg
== -1)
2766 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2769 * For link-layer types that have a variable-length header
2770 * preceding the link-layer header, generate code to load
2771 * the offset of the link-layer header into the register
2772 * assigned to that offset, if any.
2774 * XXX - this, and the next switch statement, won't handle
2775 * encapsulation of 802.11 or 802.11+radio information in
2776 * some other protocol stack. That's significantly more
2779 switch (cstate
->outermostlinktype
) {
2781 case DLT_PRISM_HEADER
:
2782 s
= gen_load_prism_llprefixlen(cstate
);
2785 case DLT_IEEE802_11_RADIO_AVS
:
2786 s
= gen_load_avs_llprefixlen(cstate
);
2789 case DLT_IEEE802_11_RADIO
:
2790 s
= gen_load_radiotap_llprefixlen(cstate
);
2794 s
= gen_load_ppi_llprefixlen(cstate
);
2803 * For link-layer types that have a variable-length link-layer
2804 * header, generate code to load the offset of the link-layer
2805 * payload into the register assigned to that offset, if any.
2807 switch (cstate
->outermostlinktype
) {
2809 case DLT_IEEE802_11
:
2810 case DLT_PRISM_HEADER
:
2811 case DLT_IEEE802_11_RADIO_AVS
:
2812 case DLT_IEEE802_11_RADIO
:
2814 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2819 * If there there is no initialization yet and we need variable
2820 * length offsets for VLAN, initialize them to zero
2822 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2825 if (cstate
->off_linkpl
.reg
== -1)
2826 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2827 if (cstate
->off_linktype
.reg
== -1)
2828 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2830 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2832 s2
= new_stmt(cstate
, BPF_ST
);
2833 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2835 s2
= new_stmt(cstate
, BPF_ST
);
2836 s2
->s
.k
= cstate
->off_linktype
.reg
;
2841 * If we have any offset-loading code, append all the
2842 * existing statements in the block to those statements,
2843 * and make the resulting list the list of statements
2847 sappend(s
, b
->stmts
);
2852 static struct block
*
2853 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2855 struct slist
*s_load_dlt
;
2858 if (cstate
->linktype
== DLT_PPI
)
2860 /* Create the statements that check for the DLT
2862 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2863 s_load_dlt
->s
.k
= 4;
2865 b
= new_block(cstate
, JMP(BPF_JEQ
));
2867 b
->stmts
= s_load_dlt
;
2868 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2879 * Take an absolute offset, and:
2881 * if it has no variable part, return NULL;
2883 * if it has a variable part, generate code to load the register
2884 * containing that variable part into the X register, returning
2885 * a pointer to that code - if no register for that offset has
2886 * been allocated, allocate it first.
2888 * (The code to set that register will be generated later, but will
2889 * be placed earlier in the code sequence.)
2891 static struct slist
*
2892 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2896 if (off
->is_variable
) {
2897 if (off
->reg
== -1) {
2899 * We haven't yet assigned a register for the
2900 * variable part of the offset of the link-layer
2901 * header; allocate one.
2903 off
->reg
= alloc_reg(cstate
);
2907 * Load the register containing the variable part of the
2908 * offset of the link-layer header into the X register.
2910 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2915 * That offset isn't variable, there's no variable part,
2916 * so we don't need to generate any code.
2923 * Map an Ethernet type to the equivalent PPP type.
2926 ethertype_to_ppptype(int proto
)
2934 case ETHERTYPE_IPV6
:
2942 case ETHERTYPE_ATALK
:
2956 * I'm assuming the "Bridging PDU"s that go
2957 * over PPP are Spanning Tree Protocol
2971 * Generate any tests that, for encapsulation of a link-layer packet
2972 * inside another protocol stack, need to be done to check for those
2973 * link-layer packets (and that haven't already been done by a check
2974 * for that encapsulation).
2976 static struct block
*
2977 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
2981 if (cstate
->is_geneve
)
2982 return gen_geneve_ll_check(cstate
);
2984 switch (cstate
->prevlinktype
) {
2988 * This is LANE-encapsulated Ethernet; check that the LANE
2989 * packet doesn't begin with an LE Control marker, i.e.
2990 * that it's data, not a control message.
2992 * (We've already generated a test for LANE.)
2994 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3000 * No such tests are necessary.
3008 * The three different values we should check for when checking for an
3009 * IPv6 packet with DLT_NULL.
3011 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3012 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3013 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3016 * Generate code to match a particular packet type by matching the
3017 * link-layer type field or fields in the 802.2 LLC header.
3019 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3020 * value, if <= ETHERMTU.
3022 static struct block
*
3023 gen_linktype(compiler_state_t
*cstate
, int proto
)
3025 struct block
*b0
, *b1
, *b2
;
3026 const char *description
;
3028 /* are we checking MPLS-encapsulated packets? */
3029 if (cstate
->label_stack_depth
> 0) {
3033 /* FIXME add other L3 proto IDs */
3034 return gen_mpls_linktype(cstate
, Q_IP
);
3036 case ETHERTYPE_IPV6
:
3038 /* FIXME add other L3 proto IDs */
3039 return gen_mpls_linktype(cstate
, Q_IPV6
);
3042 bpf_error(cstate
, "unsupported protocol over mpls");
3047 switch (cstate
->linktype
) {
3050 case DLT_NETANALYZER
:
3051 case DLT_NETANALYZER_TRANSPARENT
:
3052 /* Geneve has an EtherType regardless of whether there is an
3054 if (!cstate
->is_geneve
)
3055 b0
= gen_prevlinkhdr_check(cstate
);
3059 b1
= gen_ether_linktype(cstate
, proto
);
3070 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3074 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3080 case DLT_IEEE802_11
:
3081 case DLT_PRISM_HEADER
:
3082 case DLT_IEEE802_11_RADIO_AVS
:
3083 case DLT_IEEE802_11_RADIO
:
3086 * Check that we have a data frame.
3088 b0
= gen_check_802_11_data_frame(cstate
);
3091 * Now check for the specified link-layer type.
3093 b1
= gen_llc_linktype(cstate
, proto
);
3101 * XXX - check for LLC frames.
3103 return gen_llc_linktype(cstate
, proto
);
3109 * XXX - check for LLC PDUs, as per IEEE 802.5.
3111 return gen_llc_linktype(cstate
, proto
);
3115 case DLT_ATM_RFC1483
:
3117 case DLT_IP_OVER_FC
:
3118 return gen_llc_linktype(cstate
, proto
);
3124 * Check for an LLC-encapsulated version of this protocol;
3125 * if we were checking for LANE, linktype would no longer
3128 * Check for LLC encapsulation and then check the protocol.
3130 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3131 b1
= gen_llc_linktype(cstate
, proto
);
3138 return gen_linux_sll_linktype(cstate
, proto
);
3143 case DLT_SLIP_BSDOS
:
3146 * These types don't provide any type field; packets
3147 * are always IPv4 or IPv6.
3149 * XXX - for IPv4, check for a version number of 4, and,
3150 * for IPv6, check for a version number of 6?
3155 /* Check for a version number of 4. */
3156 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3158 case ETHERTYPE_IPV6
:
3159 /* Check for a version number of 6. */
3160 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3163 return gen_false(cstate
); /* always false */
3170 * Raw IPv4, so no type field.
3172 if (proto
== ETHERTYPE_IP
)
3173 return gen_true(cstate
); /* always true */
3175 /* Checking for something other than IPv4; always false */
3176 return gen_false(cstate
);
3182 * Raw IPv6, so no type field.
3184 if (proto
== ETHERTYPE_IPV6
)
3185 return gen_true(cstate
); /* always true */
3187 /* Checking for something other than IPv6; always false */
3188 return gen_false(cstate
);
3194 case DLT_PPP_SERIAL
:
3197 * We use Ethernet protocol types inside libpcap;
3198 * map them to the corresponding PPP protocol types.
3200 proto
= ethertype_to_ppptype(proto
);
3201 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3207 * We use Ethernet protocol types inside libpcap;
3208 * map them to the corresponding PPP protocol types.
3214 * Also check for Van Jacobson-compressed IP.
3215 * XXX - do this for other forms of PPP?
3217 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3218 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3220 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3225 proto
= ethertype_to_ppptype(proto
);
3226 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3238 return (gen_loopback_linktype(cstate
, AF_INET
));
3240 case ETHERTYPE_IPV6
:
3242 * AF_ values may, unfortunately, be platform-
3243 * dependent; AF_INET isn't, because everybody
3244 * used 4.2BSD's value, but AF_INET6 is, because
3245 * 4.2BSD didn't have a value for it (given that
3246 * IPv6 didn't exist back in the early 1980's),
3247 * and they all picked their own values.
3249 * This means that, if we're reading from a
3250 * savefile, we need to check for all the
3253 * If we're doing a live capture, we only need
3254 * to check for this platform's value; however,
3255 * Npcap uses 24, which isn't Windows's AF_INET6
3256 * value. (Given the multiple different values,
3257 * programs that read pcap files shouldn't be
3258 * checking for their platform's AF_INET6 value
3259 * anyway, they should check for all of the
3260 * possible values. and they might as well do
3261 * that even for live captures.)
3263 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3265 * Savefile - check for all three
3266 * possible IPv6 values.
3268 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3269 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3271 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3276 * Live capture, so we only need to
3277 * check for the value used on this
3282 * Npcap doesn't use Windows's AF_INET6,
3283 * as that collides with AF_IPX on
3284 * some BSDs (both have the value 23).
3285 * Instead, it uses 24.
3287 return (gen_loopback_linktype(cstate
, 24));
3290 return (gen_loopback_linktype(cstate
, AF_INET6
));
3291 #else /* AF_INET6 */
3293 * I guess this platform doesn't support
3294 * IPv6, so we just reject all packets.
3296 return gen_false(cstate
);
3297 #endif /* AF_INET6 */
3303 * Not a type on which we support filtering.
3304 * XXX - support those that have AF_ values
3305 * #defined on this platform, at least?
3307 return gen_false(cstate
);
3310 #ifdef HAVE_NET_PFVAR_H
3313 * af field is host byte order in contrast to the rest of
3316 if (proto
== ETHERTYPE_IP
)
3317 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3318 BPF_B
, (bpf_int32
)AF_INET
));
3319 else if (proto
== ETHERTYPE_IPV6
)
3320 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3321 BPF_B
, (bpf_int32
)AF_INET6
));
3323 return gen_false(cstate
);
3326 #endif /* HAVE_NET_PFVAR_H */
3329 case DLT_ARCNET_LINUX
:
3331 * XXX should we check for first fragment if the protocol
3337 return gen_false(cstate
);
3339 case ETHERTYPE_IPV6
:
3340 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3341 (bpf_int32
)ARCTYPE_INET6
));
3344 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3345 (bpf_int32
)ARCTYPE_IP
);
3346 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3347 (bpf_int32
)ARCTYPE_IP_OLD
);
3352 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3353 (bpf_int32
)ARCTYPE_ARP
);
3354 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3355 (bpf_int32
)ARCTYPE_ARP_OLD
);
3359 case ETHERTYPE_REVARP
:
3360 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3361 (bpf_int32
)ARCTYPE_REVARP
));
3363 case ETHERTYPE_ATALK
:
3364 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3365 (bpf_int32
)ARCTYPE_ATALK
));
3372 case ETHERTYPE_ATALK
:
3373 return gen_true(cstate
);
3375 return gen_false(cstate
);
3382 * XXX - assumes a 2-byte Frame Relay header with
3383 * DLCI and flags. What if the address is longer?
3389 * Check for the special NLPID for IP.
3391 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3393 case ETHERTYPE_IPV6
:
3395 * Check for the special NLPID for IPv6.
3397 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3401 * Check for several OSI protocols.
3403 * Frame Relay packets typically have an OSI
3404 * NLPID at the beginning; we check for each
3407 * What we check for is the NLPID and a frame
3408 * control field of UI, i.e. 0x03 followed
3411 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3412 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3413 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3419 return gen_false(cstate
);
3425 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3427 case DLT_JUNIPER_MFR
:
3428 case DLT_JUNIPER_MLFR
:
3429 case DLT_JUNIPER_MLPPP
:
3430 case DLT_JUNIPER_ATM1
:
3431 case DLT_JUNIPER_ATM2
:
3432 case DLT_JUNIPER_PPPOE
:
3433 case DLT_JUNIPER_PPPOE_ATM
:
3434 case DLT_JUNIPER_GGSN
:
3435 case DLT_JUNIPER_ES
:
3436 case DLT_JUNIPER_MONITOR
:
3437 case DLT_JUNIPER_SERVICES
:
3438 case DLT_JUNIPER_ETHER
:
3439 case DLT_JUNIPER_PPP
:
3440 case DLT_JUNIPER_FRELAY
:
3441 case DLT_JUNIPER_CHDLC
:
3442 case DLT_JUNIPER_VP
:
3443 case DLT_JUNIPER_ST
:
3444 case DLT_JUNIPER_ISM
:
3445 case DLT_JUNIPER_VS
:
3446 case DLT_JUNIPER_SRX_E2E
:
3447 case DLT_JUNIPER_FIBRECHANNEL
:
3448 case DLT_JUNIPER_ATM_CEMIC
:
3450 /* just lets verify the magic number for now -
3451 * on ATM we may have up to 6 different encapsulations on the wire
3452 * and need a lot of heuristics to figure out that the payload
3455 * FIXME encapsulation specific BPF_ filters
3457 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3459 case DLT_BACNET_MS_TP
:
3460 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3463 return gen_ipnet_linktype(cstate
, proto
);
3465 case DLT_LINUX_IRDA
:
3466 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3469 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3472 case DLT_MTP2_WITH_PHDR
:
3473 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3476 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3479 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3481 case DLT_LINUX_LAPD
:
3482 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3484 case DLT_USB_FREEBSD
:
3486 case DLT_USB_LINUX_MMAPPED
:
3488 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3490 case DLT_BLUETOOTH_HCI_H4
:
3491 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3492 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3495 case DLT_CAN_SOCKETCAN
:
3496 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3498 case DLT_IEEE802_15_4
:
3499 case DLT_IEEE802_15_4_LINUX
:
3500 case DLT_IEEE802_15_4_NONASK_PHY
:
3501 case DLT_IEEE802_15_4_NOFCS
:
3502 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3504 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3505 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3508 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3511 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3514 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3517 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3520 /* Using the fixed-size NFLOG header it is possible to tell only
3521 * the address family of the packet, other meaningful data is
3522 * either missing or behind TLVs.
3524 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3528 * Does this link-layer header type have a field
3529 * indicating the type of the next protocol? If
3530 * so, off_linktype.constant_part will be the offset of that
3531 * field in the packet; if not, it will be OFFSET_NOT_SET.
3533 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3535 * Yes; assume it's an Ethernet type. (If
3536 * it's not, it needs to be handled specially
3539 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3542 * No; report an error.
3544 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3545 if (description
!= NULL
) {
3546 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3549 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3558 * Check for an LLC SNAP packet with a given organization code and
3559 * protocol type; we check the entire contents of the 802.2 LLC and
3560 * snap headers, checking for DSAP and SSAP of SNAP and a control
3561 * field of 0x03 in the LLC header, and for the specified organization
3562 * code and protocol type in the SNAP header.
3564 static struct block
*
3565 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3567 u_char snapblock
[8];
3569 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3570 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3571 snapblock
[2] = 0x03; /* control = UI */
3572 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3573 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3574 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3575 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3576 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3577 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3581 * Generate code to match frames with an LLC header.
3584 gen_llc(compiler_state_t
*cstate
)
3586 struct block
*b0
, *b1
;
3588 switch (cstate
->linktype
) {
3592 * We check for an Ethernet type field less than
3593 * 1500, which means it's an 802.3 length field.
3595 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3599 * Now check for the purported DSAP and SSAP not being
3600 * 0xFF, to rule out NetWare-over-802.3.
3602 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3609 * We check for LLC traffic.
3611 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3614 case DLT_IEEE802
: /* Token Ring */
3616 * XXX - check for LLC frames.
3618 return gen_true(cstate
);
3622 * XXX - check for LLC frames.
3624 return gen_true(cstate
);
3626 case DLT_ATM_RFC1483
:
3628 * For LLC encapsulation, these are defined to have an
3631 * For VC encapsulation, they don't, but there's no
3632 * way to check for that; the protocol used on the VC
3633 * is negotiated out of band.
3635 return gen_true(cstate
);
3637 case DLT_IEEE802_11
:
3638 case DLT_PRISM_HEADER
:
3639 case DLT_IEEE802_11_RADIO
:
3640 case DLT_IEEE802_11_RADIO_AVS
:
3643 * Check that we have a data frame.
3645 b0
= gen_check_802_11_data_frame(cstate
);
3649 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3655 gen_llc_i(compiler_state_t
*cstate
)
3657 struct block
*b0
, *b1
;
3661 * Check whether this is an LLC frame.
3663 b0
= gen_llc(cstate
);
3666 * Load the control byte and test the low-order bit; it must
3667 * be clear for I frames.
3669 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3670 b1
= new_block(cstate
, JMP(BPF_JSET
));
3679 gen_llc_s(compiler_state_t
*cstate
)
3681 struct block
*b0
, *b1
;
3684 * Check whether this is an LLC frame.
3686 b0
= gen_llc(cstate
);
3689 * Now compare the low-order 2 bit of the control byte against
3690 * the appropriate value for S frames.
3692 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3698 gen_llc_u(compiler_state_t
*cstate
)
3700 struct block
*b0
, *b1
;
3703 * Check whether this is an LLC frame.
3705 b0
= gen_llc(cstate
);
3708 * Now compare the low-order 2 bit of the control byte against
3709 * the appropriate value for U frames.
3711 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3717 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3719 struct block
*b0
, *b1
;
3722 * Check whether this is an LLC frame.
3724 b0
= gen_llc(cstate
);
3727 * Now check for an S frame with the appropriate type.
3729 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3735 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3737 struct block
*b0
, *b1
;
3740 * Check whether this is an LLC frame.
3742 b0
= gen_llc(cstate
);
3745 * Now check for a U frame with the appropriate type.
3747 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3753 * Generate code to match a particular packet type, for link-layer types
3754 * using 802.2 LLC headers.
3756 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3757 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3759 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3760 * value, if <= ETHERMTU. We use that to determine whether to
3761 * match the DSAP or both DSAP and LSAP or to check the OUI and
3762 * protocol ID in a SNAP header.
3764 static struct block
*
3765 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3768 * XXX - handle token-ring variable-length header.
3774 case LLCSAP_NETBEUI
:
3776 * XXX - should we check both the DSAP and the
3777 * SSAP, like this, or should we check just the
3778 * DSAP, as we do for other SAP values?
3780 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3781 ((proto
<< 8) | proto
));
3785 * XXX - are there ever SNAP frames for IPX on
3786 * non-Ethernet 802.x networks?
3788 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3789 (bpf_int32
)LLCSAP_IPX
);
3791 case ETHERTYPE_ATALK
:
3793 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3794 * SNAP packets with an organization code of
3795 * 0x080007 (Apple, for Appletalk) and a protocol
3796 * type of ETHERTYPE_ATALK (Appletalk).
3798 * XXX - check for an organization code of
3799 * encapsulated Ethernet as well?
3801 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3805 * XXX - we don't have to check for IPX 802.3
3806 * here, but should we check for the IPX Ethertype?
3808 if (proto
<= ETHERMTU
) {
3810 * This is an LLC SAP value, so check
3813 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3816 * This is an Ethernet type; we assume that it's
3817 * unlikely that it'll appear in the right place
3818 * at random, and therefore check only the
3819 * location that would hold the Ethernet type
3820 * in a SNAP frame with an organization code of
3821 * 0x000000 (encapsulated Ethernet).
3823 * XXX - if we were to check for the SNAP DSAP and
3824 * LSAP, as per XXX, and were also to check for an
3825 * organization code of 0x000000 (encapsulated
3826 * Ethernet), we'd do
3828 * return gen_snap(cstate, 0x000000, proto);
3830 * here; for now, we don't, as per the above.
3831 * I don't know whether it's worth the extra CPU
3832 * time to do the right check or not.
3834 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3839 static struct block
*
3840 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3841 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3843 struct block
*b0
, *b1
;
3857 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3858 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3864 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3865 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3870 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3874 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3878 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3882 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3886 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3890 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3896 b0
= gen_linktype(cstate
, proto
);
3897 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3903 static struct block
*
3904 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3905 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3907 struct block
*b0
, *b1
;
3922 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3923 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3929 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3930 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3935 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3939 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3943 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3947 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3951 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3955 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3961 /* this order is important */
3962 a
= (uint32_t *)addr
;
3963 m
= (uint32_t *)mask
;
3964 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3965 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3967 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3969 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3971 b0
= gen_linktype(cstate
, proto
);
3977 static struct block
*
3978 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
3980 register struct block
*b0
, *b1
;
3984 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
3987 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
3990 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3991 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
3997 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
3998 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4003 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4007 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4011 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4015 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4019 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4023 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4031 * Like gen_ehostop, but for DLT_FDDI
4033 static struct block
*
4034 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4036 struct block
*b0
, *b1
;
4040 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4043 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4046 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4047 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4053 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4054 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4059 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4063 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4067 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4071 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4075 bpf_error(cstate
, "'ra' is only supported on 802.11");
4079 bpf_error(cstate
, "'ta' is only supported on 802.11");
4087 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4089 static struct block
*
4090 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4092 register struct block
*b0
, *b1
;
4096 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4099 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4102 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4103 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4109 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4110 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4115 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4119 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4123 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4127 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4131 bpf_error(cstate
, "'ra' is only supported on 802.11");
4135 bpf_error(cstate
, "'ta' is only supported on 802.11");
4143 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4144 * various 802.11 + radio headers.
4146 static struct block
*
4147 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4149 register struct block
*b0
, *b1
, *b2
;
4150 register struct slist
*s
;
4152 #ifdef ENABLE_WLAN_FILTERING_PATCH
4155 * We need to disable the optimizer because the optimizer is buggy
4156 * and wipes out some LD instructions generated by the below
4157 * code to validate the Frame Control bits
4159 cstate
->no_optimize
= 1;
4160 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4167 * For control frames, there is no SA.
4169 * For management frames, SA is at an
4170 * offset of 10 from the beginning of
4173 * For data frames, SA is at an offset
4174 * of 10 from the beginning of the packet
4175 * if From DS is clear, at an offset of
4176 * 16 from the beginning of the packet
4177 * if From DS is set and To DS is clear,
4178 * and an offset of 24 from the beginning
4179 * of the packet if From DS is set and To DS
4184 * Generate the tests to be done for data frames
4187 * First, check for To DS set, i.e. check "link[1] & 0x01".
4189 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4190 b1
= new_block(cstate
, JMP(BPF_JSET
));
4191 b1
->s
.k
= 0x01; /* To DS */
4195 * If To DS is set, the SA is at 24.
4197 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4201 * Now, check for To DS not set, i.e. check
4202 * "!(link[1] & 0x01)".
4204 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4205 b2
= new_block(cstate
, JMP(BPF_JSET
));
4206 b2
->s
.k
= 0x01; /* To DS */
4211 * If To DS is not set, the SA is at 16.
4213 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4217 * Now OR together the last two checks. That gives
4218 * the complete set of checks for data frames with
4224 * Now check for From DS being set, and AND that with
4225 * the ORed-together checks.
4227 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4228 b1
= new_block(cstate
, JMP(BPF_JSET
));
4229 b1
->s
.k
= 0x02; /* From DS */
4234 * Now check for data frames with From DS not set.
4236 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4237 b2
= new_block(cstate
, JMP(BPF_JSET
));
4238 b2
->s
.k
= 0x02; /* From DS */
4243 * If From DS isn't set, the SA is at 10.
4245 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4249 * Now OR together the checks for data frames with
4250 * From DS not set and for data frames with From DS
4251 * set; that gives the checks done for data frames.
4256 * Now check for a data frame.
4257 * I.e, check "link[0] & 0x08".
4259 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4260 b1
= new_block(cstate
, JMP(BPF_JSET
));
4265 * AND that with the checks done for data frames.
4270 * If the high-order bit of the type value is 0, this
4271 * is a management frame.
4272 * I.e, check "!(link[0] & 0x08)".
4274 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4275 b2
= new_block(cstate
, JMP(BPF_JSET
));
4281 * For management frames, the SA is at 10.
4283 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4287 * OR that with the checks done for data frames.
4288 * That gives the checks done for management and
4294 * If the low-order bit of the type value is 1,
4295 * this is either a control frame or a frame
4296 * with a reserved type, and thus not a
4299 * I.e., check "!(link[0] & 0x04)".
4301 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4302 b1
= new_block(cstate
, JMP(BPF_JSET
));
4308 * AND that with the checks for data and management
4318 * For control frames, there is no DA.
4320 * For management frames, DA is at an
4321 * offset of 4 from the beginning of
4324 * For data frames, DA is at an offset
4325 * of 4 from the beginning of the packet
4326 * if To DS is clear and at an offset of
4327 * 16 from the beginning of the packet
4332 * Generate the tests to be done for data frames.
4334 * First, check for To DS set, i.e. "link[1] & 0x01".
4336 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4337 b1
= new_block(cstate
, JMP(BPF_JSET
));
4338 b1
->s
.k
= 0x01; /* To DS */
4342 * If To DS is set, the DA is at 16.
4344 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4348 * Now, check for To DS not set, i.e. check
4349 * "!(link[1] & 0x01)".
4351 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4352 b2
= new_block(cstate
, JMP(BPF_JSET
));
4353 b2
->s
.k
= 0x01; /* To DS */
4358 * If To DS is not set, the DA is at 4.
4360 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4364 * Now OR together the last two checks. That gives
4365 * the complete set of checks for data frames.
4370 * Now check for a data frame.
4371 * I.e, check "link[0] & 0x08".
4373 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4374 b1
= new_block(cstate
, JMP(BPF_JSET
));
4379 * AND that with the checks done for data frames.
4384 * If the high-order bit of the type value is 0, this
4385 * is a management frame.
4386 * I.e, check "!(link[0] & 0x08)".
4388 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4389 b2
= new_block(cstate
, JMP(BPF_JSET
));
4395 * For management frames, the DA is at 4.
4397 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4401 * OR that with the checks done for data frames.
4402 * That gives the checks done for management and
4408 * If the low-order bit of the type value is 1,
4409 * this is either a control frame or a frame
4410 * with a reserved type, and thus not a
4413 * I.e., check "!(link[0] & 0x04)".
4415 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4416 b1
= new_block(cstate
, JMP(BPF_JSET
));
4422 * AND that with the checks for data and management
4430 * Not present in management frames; addr1 in other
4435 * If the high-order bit of the type value is 0, this
4436 * is a management frame.
4437 * I.e, check "(link[0] & 0x08)".
4439 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4440 b1
= new_block(cstate
, JMP(BPF_JSET
));
4447 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4450 * AND that with the check of addr1.
4457 * Not present in management frames; addr2, if present,
4462 * Not present in CTS or ACK control frames.
4464 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4465 IEEE80211_FC0_TYPE_MASK
);
4467 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4468 IEEE80211_FC0_SUBTYPE_MASK
);
4470 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4471 IEEE80211_FC0_SUBTYPE_MASK
);
4477 * If the high-order bit of the type value is 0, this
4478 * is a management frame.
4479 * I.e, check "(link[0] & 0x08)".
4481 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4482 b1
= new_block(cstate
, JMP(BPF_JSET
));
4487 * AND that with the check for frames other than
4488 * CTS and ACK frames.
4495 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4500 * XXX - add BSSID keyword?
4503 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4507 * Not present in CTS or ACK control frames.
4509 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4510 IEEE80211_FC0_TYPE_MASK
);
4512 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4513 IEEE80211_FC0_SUBTYPE_MASK
);
4515 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4516 IEEE80211_FC0_SUBTYPE_MASK
);
4520 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4526 * Not present in control frames.
4528 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4529 IEEE80211_FC0_TYPE_MASK
);
4531 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4537 * Present only if the direction mask has both "From DS"
4538 * and "To DS" set. Neither control frames nor management
4539 * frames should have both of those set, so we don't
4540 * check the frame type.
4542 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4543 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4544 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4549 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4550 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4556 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4557 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4566 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4567 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4568 * as the RFC states.)
4570 static struct block
*
4571 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4573 register struct block
*b0
, *b1
;
4577 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4580 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4583 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4584 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4590 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4591 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4596 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4600 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4604 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4608 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4612 bpf_error(cstate
, "'ra' is only supported on 802.11");
4616 bpf_error(cstate
, "'ta' is only supported on 802.11");
4624 * This is quite tricky because there may be pad bytes in front of the
4625 * DECNET header, and then there are two possible data packet formats that
4626 * carry both src and dst addresses, plus 5 packet types in a format that
4627 * carries only the src node, plus 2 types that use a different format and
4628 * also carry just the src node.
4632 * Instead of doing those all right, we just look for data packets with
4633 * 0 or 1 bytes of padding. If you want to look at other packets, that
4634 * will require a lot more hacking.
4636 * To add support for filtering on DECNET "areas" (network numbers)
4637 * one would want to add a "mask" argument to this routine. That would
4638 * make the filter even more inefficient, although one could be clever
4639 * and not generate masking instructions if the mask is 0xFFFF.
4641 static struct block
*
4642 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4644 struct block
*b0
, *b1
, *b2
, *tmp
;
4645 u_int offset_lh
; /* offset if long header is received */
4646 u_int offset_sh
; /* offset if short header is received */
4651 offset_sh
= 1; /* follows flags */
4652 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4656 offset_sh
= 3; /* follows flags, dstnode */
4657 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4661 /* Inefficient because we do our Calvinball dance twice */
4662 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4663 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4669 /* Inefficient because we do our Calvinball dance twice */
4670 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4671 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4676 bpf_error(cstate
, "ISO host filtering not implemented");
4681 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4682 /* Check for pad = 1, long header case */
4683 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4684 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4685 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4686 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4688 /* Check for pad = 0, long header case */
4689 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4690 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4693 /* Check for pad = 1, short header case */
4694 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4695 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4696 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4699 /* Check for pad = 0, short header case */
4700 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4701 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4705 /* Combine with test for cstate->linktype */
4711 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4712 * test the bottom-of-stack bit, and then check the version number
4713 * field in the IP header.
4715 static struct block
*
4716 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4718 struct block
*b0
, *b1
;
4723 /* match the bottom-of-stack bit */
4724 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4725 /* match the IPv4 version number */
4726 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4731 /* match the bottom-of-stack bit */
4732 b0
= gen_mcmp(cstate
, OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4733 /* match the IPv4 version number */
4734 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4743 static struct block
*
4744 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4745 int proto
, int dir
, int type
)
4747 struct block
*b0
, *b1
;
4748 const char *typestr
;
4758 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4760 * Only check for non-IPv4 addresses if we're not
4761 * checking MPLS-encapsulated packets.
4763 if (cstate
->label_stack_depth
== 0) {
4764 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4766 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4772 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4775 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4778 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4781 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4784 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4787 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4790 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4793 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4796 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4799 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4802 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4805 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4808 bpf_error(cstate
, "ATALK host filtering not implemented");
4811 bpf_error(cstate
, "AARP host filtering not implemented");
4814 return gen_dnhostop(cstate
, addr
, dir
);
4817 bpf_error(cstate
, "SCA host filtering not implemented");
4820 bpf_error(cstate
, "LAT host filtering not implemented");
4823 bpf_error(cstate
, "MOPDL host filtering not implemented");
4826 bpf_error(cstate
, "MOPRC host filtering not implemented");
4829 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4832 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4835 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4838 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4841 bpf_error(cstate
, "ISO host filtering not implemented");
4844 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4847 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4850 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4853 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4856 bpf_error(cstate
, "IPX host filtering not implemented");
4859 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4862 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4871 static struct block
*
4872 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4873 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4875 const char *typestr
;
4885 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4888 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4891 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4894 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4897 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4900 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4903 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4906 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4909 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4912 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4915 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4918 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4921 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4924 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4927 bpf_error(cstate
, "ATALK host filtering not implemented");
4930 bpf_error(cstate
, "AARP host filtering not implemented");
4933 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
4936 bpf_error(cstate
, "SCA host filtering not implemented");
4939 bpf_error(cstate
, "LAT host filtering not implemented");
4942 bpf_error(cstate
, "MOPDL host filtering not implemented");
4945 bpf_error(cstate
, "MOPRC host filtering not implemented");
4948 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4951 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4954 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4957 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4960 bpf_error(cstate
, "ISO host filtering not implemented");
4963 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4966 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4969 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4972 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4975 bpf_error(cstate
, "IPX host filtering not implemented");
4978 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4981 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4991 static struct block
*
4992 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
4993 struct addrinfo
*alist
, int proto
, int dir
)
4995 struct block
*b0
, *b1
, *tmp
;
4996 struct addrinfo
*ai
;
4997 struct sockaddr_in
*sin
;
5000 bpf_error(cstate
, "direction applied to 'gateway'");
5007 switch (cstate
->linktype
) {
5009 case DLT_NETANALYZER
:
5010 case DLT_NETANALYZER_TRANSPARENT
:
5011 b1
= gen_prevlinkhdr_check(cstate
);
5012 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5017 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5020 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5022 case DLT_IEEE802_11
:
5023 case DLT_PRISM_HEADER
:
5024 case DLT_IEEE802_11_RADIO_AVS
:
5025 case DLT_IEEE802_11_RADIO
:
5027 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5031 * This is LLC-multiplexed traffic; if it were
5032 * LANE, cstate->linktype would have been set to
5036 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5038 case DLT_IP_OVER_FC
:
5039 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5043 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5046 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5048 * Does it have an address?
5050 if (ai
->ai_addr
!= NULL
) {
5052 * Yes. Is it an IPv4 address?
5054 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5056 * Generate an entry for it.
5058 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5059 tmp
= gen_host(cstate
,
5060 ntohl(sin
->sin_addr
.s_addr
),
5061 0xffffffff, proto
, Q_OR
, Q_HOST
);
5063 * Is it the *first* IPv4 address?
5067 * Yes, so start with it.
5072 * No, so OR it into the
5084 * No IPv4 addresses found.
5092 bpf_error(cstate
, "illegal modifier of 'gateway'");
5098 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5106 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5107 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5112 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5113 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5118 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5119 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5124 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5127 #ifndef IPPROTO_IGMP
5128 #define IPPROTO_IGMP 2
5132 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5135 #ifndef IPPROTO_IGRP
5136 #define IPPROTO_IGRP 9
5139 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5143 #define IPPROTO_PIM 103
5147 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5148 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5152 #ifndef IPPROTO_VRRP
5153 #define IPPROTO_VRRP 112
5157 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5160 #ifndef IPPROTO_CARP
5161 #define IPPROTO_CARP 112
5165 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5169 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5173 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5177 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5181 bpf_error(cstate
, "link layer applied in wrong context");
5184 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5188 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5192 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5196 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5200 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5204 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5208 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5212 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5215 #ifndef IPPROTO_ICMPV6
5216 #define IPPROTO_ICMPV6 58
5219 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5223 #define IPPROTO_AH 51
5226 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5227 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5232 #define IPPROTO_ESP 50
5235 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5236 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5241 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5245 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5249 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5252 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5253 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5254 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5256 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5258 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5260 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5264 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5265 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5266 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5268 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5270 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5272 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5276 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5277 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5278 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5280 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5285 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5286 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5291 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5292 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5294 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5296 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5301 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5302 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5307 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5308 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5313 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5317 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5321 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5325 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5329 bpf_error(cstate
, "'radio' is not a valid protocol type");
5337 static struct block
*
5338 gen_ipfrag(compiler_state_t
*cstate
)
5343 /* not IPv4 frag other than the first frag */
5344 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5345 b
= new_block(cstate
, JMP(BPF_JSET
));
5354 * Generate a comparison to a port value in the transport-layer header
5355 * at the specified offset from the beginning of that header.
5357 * XXX - this handles a variable-length prefix preceding the link-layer
5358 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5359 * variable-length link-layer headers (such as Token Ring or 802.11
5362 static struct block
*
5363 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5365 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5368 static struct block
*
5369 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5371 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5375 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5377 struct block
*b0
, *b1
, *tmp
;
5379 /* ip proto 'proto' and not a fragment other than the first fragment */
5380 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5381 b0
= gen_ipfrag(cstate
);
5386 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5390 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5395 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5396 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5401 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5402 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5414 static struct block
*
5415 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5417 struct block
*b0
, *b1
, *tmp
;
5422 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5423 * not LLC encapsulation with LLCSAP_IP.
5425 * For IEEE 802 networks - which includes 802.5 token ring
5426 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5427 * says that SNAP encapsulation is used, not LLC encapsulation
5430 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5431 * RFC 2225 say that SNAP encapsulation is used, not LLC
5432 * encapsulation with LLCSAP_IP.
5434 * So we always check for ETHERTYPE_IP.
5436 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5442 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5446 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5447 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5449 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5461 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5463 struct block
*b0
, *b1
, *tmp
;
5465 /* ip6 proto 'proto' */
5466 /* XXX - catch the first fragment of a fragmented packet? */
5467 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5471 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5475 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5480 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5481 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5486 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5487 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5499 static struct block
*
5500 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5502 struct block
*b0
, *b1
, *tmp
;
5504 /* link proto ip6 */
5505 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5511 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5515 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5516 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5518 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5529 /* gen_portrange code */
5530 static struct block
*
5531 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5534 struct block
*b1
, *b2
;
5538 * Reverse the order of the ports, so v1 is the lower one.
5547 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5548 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5556 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5559 struct block
*b0
, *b1
, *tmp
;
5561 /* ip proto 'proto' and not a fragment other than the first fragment */
5562 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5563 b0
= gen_ipfrag(cstate
);
5568 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5572 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5577 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5578 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5583 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5584 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5596 static struct block
*
5597 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5600 struct block
*b0
, *b1
, *tmp
;
5603 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5609 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5613 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5614 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5616 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5627 static struct block
*
5628 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5631 struct block
*b1
, *b2
;
5635 * Reverse the order of the ports, so v1 is the lower one.
5644 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5645 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5653 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5656 struct block
*b0
, *b1
, *tmp
;
5658 /* ip6 proto 'proto' */
5659 /* XXX - catch the first fragment of a fragmented packet? */
5660 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5664 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5668 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5673 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5674 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5679 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5680 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5692 static struct block
*
5693 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5696 struct block
*b0
, *b1
, *tmp
;
5698 /* link proto ip6 */
5699 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5705 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5709 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5710 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5712 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5724 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5733 v
= pcap_nametoproto(name
);
5734 if (v
== PROTO_UNDEF
)
5735 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5739 /* XXX should look up h/w protocol type based on cstate->linktype */
5740 v
= pcap_nametoeproto(name
);
5741 if (v
== PROTO_UNDEF
) {
5742 v
= pcap_nametollc(name
);
5743 if (v
== PROTO_UNDEF
)
5744 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5749 if (strcmp(name
, "esis") == 0)
5751 else if (strcmp(name
, "isis") == 0)
5753 else if (strcmp(name
, "clnp") == 0)
5756 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5768 gen_joinsp(struct stmt
**s
, int n
)
5774 static struct block
*
5775 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5777 #ifdef NO_PROTOCHAIN
5778 return gen_proto(cstate
, v
, proto
, dir
);
5780 struct block
*b0
, *b
;
5781 struct slist
*s
[100];
5782 int fix2
, fix3
, fix4
, fix5
;
5783 int ahcheck
, again
, end
;
5785 int reg2
= alloc_reg(cstate
);
5787 memset(s
, 0, sizeof(s
));
5788 fix3
= fix4
= fix5
= 0;
5795 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5796 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5800 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5805 * We don't handle variable-length prefixes before the link-layer
5806 * header, or variable-length link-layer headers, here yet.
5807 * We might want to add BPF instructions to do the protochain
5808 * work, to simplify that and, on platforms that have a BPF
5809 * interpreter with the new instructions, let the filtering
5810 * be done in the kernel. (We already require a modified BPF
5811 * engine to do the protochain stuff, to support backward
5812 * branches, and backward branch support is unlikely to appear
5813 * in kernel BPF engines.)
5815 if (cstate
->off_linkpl
.is_variable
)
5816 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5818 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
5821 * s[0] is a dummy entry to protect other BPF insn from damage
5822 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5823 * hard to find interdependency made by jump table fixup.
5826 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5831 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5834 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5835 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5837 /* X = ip->ip_hl << 2 */
5838 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5839 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5844 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5846 /* A = ip6->ip_nxt */
5847 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5848 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
5850 /* X = sizeof(struct ip6_hdr) */
5851 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
5857 bpf_error(cstate
, "unsupported proto to gen_protochain");
5861 /* again: if (A == v) goto end; else fall through; */
5863 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5865 s
[i
]->s
.jt
= NULL
; /*later*/
5866 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5870 #ifndef IPPROTO_NONE
5871 #define IPPROTO_NONE 59
5873 /* if (A == IPPROTO_NONE) goto end */
5874 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5875 s
[i
]->s
.jt
= NULL
; /*later*/
5876 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5877 s
[i
]->s
.k
= IPPROTO_NONE
;
5878 s
[fix5
]->s
.jf
= s
[i
];
5882 if (proto
== Q_IPV6
) {
5883 int v6start
, v6end
, v6advance
, j
;
5886 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5887 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5888 s
[i
]->s
.jt
= NULL
; /*later*/
5889 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5890 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5891 s
[fix2
]->s
.jf
= s
[i
];
5893 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5894 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5895 s
[i
]->s
.jt
= NULL
; /*later*/
5896 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5897 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5899 /* if (A == IPPROTO_ROUTING) goto v6advance */
5900 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5901 s
[i
]->s
.jt
= NULL
; /*later*/
5902 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5903 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5905 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5906 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5907 s
[i
]->s
.jt
= NULL
; /*later*/
5908 s
[i
]->s
.jf
= NULL
; /*later*/
5909 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5919 * A = P[X + packet head];
5920 * X = X + (P[X + packet head + 1] + 1) * 8;
5922 /* A = P[X + packet head] */
5923 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5924 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5927 s
[i
] = new_stmt(cstate
, BPF_ST
);
5930 /* A = P[X + packet head + 1]; */
5931 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5932 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
5935 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5939 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
5943 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
5947 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
5950 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
5954 /* goto again; (must use BPF_JA for backward jump) */
5955 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
5956 s
[i
]->s
.k
= again
- i
- 1;
5957 s
[i
- 1]->s
.jf
= s
[i
];
5961 for (j
= v6start
; j
<= v6end
; j
++)
5962 s
[j
]->s
.jt
= s
[v6advance
];
5965 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
5967 s
[fix2
]->s
.jf
= s
[i
];
5973 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5974 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5975 s
[i
]->s
.jt
= NULL
; /*later*/
5976 s
[i
]->s
.jf
= NULL
; /*later*/
5977 s
[i
]->s
.k
= IPPROTO_AH
;
5979 s
[fix3
]->s
.jf
= s
[ahcheck
];
5986 * X = X + (P[X + 1] + 2) * 4;
5989 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
5991 /* A = P[X + packet head]; */
5992 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
5993 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5996 s
[i
] = new_stmt(cstate
, BPF_ST
);
6000 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6003 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6007 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6009 /* A = P[X + packet head] */
6010 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6011 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6014 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6018 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6022 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6025 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6029 /* goto again; (must use BPF_JA for backward jump) */
6030 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6031 s
[i
]->s
.k
= again
- i
- 1;
6036 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6038 s
[fix2
]->s
.jt
= s
[end
];
6039 s
[fix4
]->s
.jf
= s
[end
];
6040 s
[fix5
]->s
.jt
= s
[end
];
6047 for (i
= 0; i
< max
- 1; i
++)
6048 s
[i
]->next
= s
[i
+ 1];
6049 s
[max
- 1]->next
= NULL
;
6054 b
= new_block(cstate
, JMP(BPF_JEQ
));
6055 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6058 free_reg(cstate
, reg2
);
6065 static struct block
*
6066 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6069 struct block
*b0
, *b1
;
6072 * A data frame has the 0x08 bit (b3) in the frame control field set
6073 * and the 0x04 bit (b2) clear.
6075 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6076 b0
= new_block(cstate
, JMP(BPF_JSET
));
6080 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6081 b1
= new_block(cstate
, JMP(BPF_JSET
));
6092 * Generate code that checks whether the packet is a packet for protocol
6093 * <proto> and whether the type field in that protocol's header has
6094 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6095 * IP packet and checks the protocol number in the IP header against <v>.
6097 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6098 * against Q_IP and Q_IPV6.
6100 static struct block
*
6101 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6103 struct block
*b0
, *b1
;
6108 if (dir
!= Q_DEFAULT
)
6109 bpf_error(cstate
, "direction applied to 'proto'");
6113 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6114 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6120 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6121 * not LLC encapsulation with LLCSAP_IP.
6123 * For IEEE 802 networks - which includes 802.5 token ring
6124 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6125 * says that SNAP encapsulation is used, not LLC encapsulation
6128 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6129 * RFC 2225 say that SNAP encapsulation is used, not LLC
6130 * encapsulation with LLCSAP_IP.
6132 * So we always check for ETHERTYPE_IP.
6134 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6136 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6138 b1
= gen_protochain(cstate
, v
, Q_IP
);
6144 switch (cstate
->linktype
) {
6148 * Frame Relay packets typically have an OSI
6149 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6150 * generates code to check for all the OSI
6151 * NLPIDs, so calling it and then adding a check
6152 * for the particular NLPID for which we're
6153 * looking is bogus, as we can just check for
6156 * What we check for is the NLPID and a frame
6157 * control field value of UI, i.e. 0x03 followed
6160 * XXX - assumes a 2-byte Frame Relay header with
6161 * DLCI and flags. What if the address is longer?
6163 * XXX - what about SNAP-encapsulated frames?
6165 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6171 * Cisco uses an Ethertype lookalike - for OSI,
6174 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6175 /* OSI in C-HDLC is stuffed with a fudge byte */
6176 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6181 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6182 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6188 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6190 * 4 is the offset of the PDU type relative to the IS-IS
6193 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6198 bpf_error(cstate
, "arp does not encapsulate another protocol");
6202 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6206 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6210 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6214 bpf_error(cstate
, "sca does not encapsulate another protocol");
6218 bpf_error(cstate
, "lat does not encapsulate another protocol");
6222 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6226 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6230 return gen_linktype(cstate
, v
);
6233 bpf_error(cstate
, "'udp proto' is bogus");
6237 bpf_error(cstate
, "'tcp proto' is bogus");
6241 bpf_error(cstate
, "'sctp proto' is bogus");
6245 bpf_error(cstate
, "'icmp proto' is bogus");
6249 bpf_error(cstate
, "'igmp proto' is bogus");
6253 bpf_error(cstate
, "'igrp proto' is bogus");
6257 bpf_error(cstate
, "'pim proto' is bogus");
6261 bpf_error(cstate
, "'vrrp proto' is bogus");
6265 bpf_error(cstate
, "'carp proto' is bogus");
6269 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6272 * Also check for a fragment header before the final
6275 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6276 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6278 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6281 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6287 bpf_error(cstate
, "'icmp6 proto' is bogus");
6290 bpf_error(cstate
, "'ah proto' is bogus");
6293 bpf_error(cstate
, "'ah proto' is bogus");
6296 bpf_error(cstate
, "'stp proto' is bogus");
6299 bpf_error(cstate
, "'ipx proto' is bogus");
6302 bpf_error(cstate
, "'netbeui proto' is bogus");
6305 bpf_error(cstate
, "'radio proto' is bogus");
6315 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6317 int proto
= q
.proto
;
6321 bpf_u_int32 mask
, addr
;
6322 struct addrinfo
*res
, *res0
;
6323 struct sockaddr_in
*sin4
;
6326 struct sockaddr_in6
*sin6
;
6327 struct in6_addr mask128
;
6329 struct block
*b
, *tmp
;
6330 int port
, real_proto
;
6336 addr
= pcap_nametonetaddr(name
);
6338 bpf_error(cstate
, "unknown network '%s'", name
);
6339 /* Left justify network addr and calculate its network mask */
6341 while (addr
&& (addr
& 0xff000000) == 0) {
6345 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6349 if (proto
== Q_LINK
) {
6350 switch (cstate
->linktype
) {
6353 case DLT_NETANALYZER
:
6354 case DLT_NETANALYZER_TRANSPARENT
:
6355 eaddr
= pcap_ether_hostton(name
);
6358 "unknown ether host '%s'", name
);
6359 tmp
= gen_prevlinkhdr_check(cstate
);
6360 b
= gen_ehostop(cstate
, eaddr
, dir
);
6367 eaddr
= pcap_ether_hostton(name
);
6370 "unknown FDDI host '%s'", name
);
6371 b
= gen_fhostop(cstate
, eaddr
, dir
);
6376 eaddr
= pcap_ether_hostton(name
);
6379 "unknown token ring host '%s'", name
);
6380 b
= gen_thostop(cstate
, eaddr
, dir
);
6384 case DLT_IEEE802_11
:
6385 case DLT_PRISM_HEADER
:
6386 case DLT_IEEE802_11_RADIO_AVS
:
6387 case DLT_IEEE802_11_RADIO
:
6389 eaddr
= pcap_ether_hostton(name
);
6392 "unknown 802.11 host '%s'", name
);
6393 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6397 case DLT_IP_OVER_FC
:
6398 eaddr
= pcap_ether_hostton(name
);
6401 "unknown Fibre Channel host '%s'", name
);
6402 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6407 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6408 } else if (proto
== Q_DECNET
) {
6409 unsigned short dn_addr
;
6411 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6413 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6415 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6420 * I don't think DECNET hosts can be multihomed, so
6421 * there is no need to build up a list of addresses
6423 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6426 memset(&mask128
, 0xff, sizeof(mask128
));
6428 res0
= res
= pcap_nametoaddrinfo(name
);
6430 bpf_error(cstate
, "unknown host '%s'", name
);
6437 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6438 tproto
== Q_DEFAULT
) {
6444 for (res
= res0
; res
; res
= res
->ai_next
) {
6445 switch (res
->ai_family
) {
6448 if (tproto
== Q_IPV6
)
6452 sin4
= (struct sockaddr_in
*)
6454 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6455 0xffffffff, tproto
, dir
, q
.addr
);
6459 if (tproto6
== Q_IP
)
6462 sin6
= (struct sockaddr_in6
*)
6464 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6465 &mask128
, tproto6
, dir
, q
.addr
);
6478 bpf_error(cstate
, "unknown host '%s'%s", name
,
6479 (proto
== Q_DEFAULT
)
6481 : " for specified address family");
6487 if (proto
!= Q_DEFAULT
&&
6488 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6489 bpf_error(cstate
, "illegal qualifier of 'port'");
6490 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6491 bpf_error(cstate
, "unknown port '%s'", name
);
6492 if (proto
== Q_UDP
) {
6493 if (real_proto
== IPPROTO_TCP
)
6494 bpf_error(cstate
, "port '%s' is tcp", name
);
6495 else if (real_proto
== IPPROTO_SCTP
)
6496 bpf_error(cstate
, "port '%s' is sctp", name
);
6498 /* override PROTO_UNDEF */
6499 real_proto
= IPPROTO_UDP
;
6501 if (proto
== Q_TCP
) {
6502 if (real_proto
== IPPROTO_UDP
)
6503 bpf_error(cstate
, "port '%s' is udp", name
);
6505 else if (real_proto
== IPPROTO_SCTP
)
6506 bpf_error(cstate
, "port '%s' is sctp", name
);
6508 /* override PROTO_UNDEF */
6509 real_proto
= IPPROTO_TCP
;
6511 if (proto
== Q_SCTP
) {
6512 if (real_proto
== IPPROTO_UDP
)
6513 bpf_error(cstate
, "port '%s' is udp", name
);
6515 else if (real_proto
== IPPROTO_TCP
)
6516 bpf_error(cstate
, "port '%s' is tcp", name
);
6518 /* override PROTO_UNDEF */
6519 real_proto
= IPPROTO_SCTP
;
6522 bpf_error(cstate
, "illegal port number %d < 0", port
);
6524 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6525 b
= gen_port(cstate
, port
, real_proto
, dir
);
6526 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6530 if (proto
!= Q_DEFAULT
&&
6531 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6532 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6533 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6534 bpf_error(cstate
, "unknown port in range '%s'", name
);
6535 if (proto
== Q_UDP
) {
6536 if (real_proto
== IPPROTO_TCP
)
6537 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6538 else if (real_proto
== IPPROTO_SCTP
)
6539 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6541 /* override PROTO_UNDEF */
6542 real_proto
= IPPROTO_UDP
;
6544 if (proto
== Q_TCP
) {
6545 if (real_proto
== IPPROTO_UDP
)
6546 bpf_error(cstate
, "port in range '%s' is udp", name
);
6547 else if (real_proto
== IPPROTO_SCTP
)
6548 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6550 /* override PROTO_UNDEF */
6551 real_proto
= IPPROTO_TCP
;
6553 if (proto
== Q_SCTP
) {
6554 if (real_proto
== IPPROTO_UDP
)
6555 bpf_error(cstate
, "port in range '%s' is udp", name
);
6556 else if (real_proto
== IPPROTO_TCP
)
6557 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6559 /* override PROTO_UNDEF */
6560 real_proto
= IPPROTO_SCTP
;
6563 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6565 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6567 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6569 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6571 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6572 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6577 eaddr
= pcap_ether_hostton(name
);
6579 bpf_error(cstate
, "unknown ether host: %s", name
);
6581 res
= pcap_nametoaddrinfo(name
);
6584 bpf_error(cstate
, "unknown host '%s'", name
);
6585 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6589 bpf_error(cstate
, "unknown host '%s'", name
);
6592 bpf_error(cstate
, "'gateway' not supported in this configuration");
6596 real_proto
= lookup_proto(cstate
, name
, proto
);
6597 if (real_proto
>= 0)
6598 return gen_proto(cstate
, real_proto
, proto
, dir
);
6600 bpf_error(cstate
, "unknown protocol: %s", name
);
6603 real_proto
= lookup_proto(cstate
, name
, proto
);
6604 if (real_proto
>= 0)
6605 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6607 bpf_error(cstate
, "unknown protocol: %s", name
);
6618 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6619 unsigned int masklen
, struct qual q
)
6621 register int nlen
, mlen
;
6624 nlen
= __pcap_atoin(s1
, &n
);
6625 /* Promote short ipaddr */
6629 mlen
= __pcap_atoin(s2
, &m
);
6630 /* Promote short ipaddr */
6633 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6636 /* Convert mask len to mask */
6638 bpf_error(cstate
, "mask length must be <= 32");
6641 * X << 32 is not guaranteed by C to be 0; it's
6646 m
= 0xffffffff << (32 - masklen
);
6648 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6655 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6658 bpf_error(cstate
, "Mask syntax for networks only");
6665 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6668 int proto
= q
.proto
;
6674 else if (q
.proto
== Q_DECNET
) {
6675 vlen
= __pcap_atodn(s
, &v
);
6677 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6679 vlen
= __pcap_atoin(s
, &v
);
6686 if (proto
== Q_DECNET
)
6687 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6688 else if (proto
== Q_LINK
) {
6689 bpf_error(cstate
, "illegal link layer address");
6692 if (s
== NULL
&& q
.addr
== Q_NET
) {
6693 /* Promote short net number */
6694 while (v
&& (v
& 0xff000000) == 0) {
6699 /* Promote short ipaddr */
6701 mask
<<= 32 - vlen
;
6703 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6708 proto
= IPPROTO_UDP
;
6709 else if (proto
== Q_TCP
)
6710 proto
= IPPROTO_TCP
;
6711 else if (proto
== Q_SCTP
)
6712 proto
= IPPROTO_SCTP
;
6713 else if (proto
== Q_DEFAULT
)
6714 proto
= PROTO_UNDEF
;
6716 bpf_error(cstate
, "illegal qualifier of 'port'");
6719 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6723 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6724 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6730 proto
= IPPROTO_UDP
;
6731 else if (proto
== Q_TCP
)
6732 proto
= IPPROTO_TCP
;
6733 else if (proto
== Q_SCTP
)
6734 proto
= IPPROTO_SCTP
;
6735 else if (proto
== Q_DEFAULT
)
6736 proto
= PROTO_UNDEF
;
6738 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6741 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6745 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6746 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6751 bpf_error(cstate
, "'gateway' requires a name");
6755 return gen_proto(cstate
, (int)v
, proto
, dir
);
6758 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6773 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6774 unsigned int masklen
, struct qual q
)
6776 struct addrinfo
*res
;
6777 struct in6_addr
*addr
;
6778 struct in6_addr mask
;
6783 bpf_error(cstate
, "no mask %s supported", s2
);
6785 res
= pcap_nametoaddrinfo(s1
);
6787 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6790 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6791 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6793 if (sizeof(mask
) * 8 < masklen
)
6794 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6795 memset(&mask
, 0, sizeof(mask
));
6796 memset(&mask
, 0xff, masklen
/ 8);
6798 mask
.s6_addr
[masklen
/ 8] =
6799 (0xff << (8 - masklen
% 8)) & 0xff;
6802 a
= (uint32_t *)addr
;
6803 m
= (uint32_t *)&mask
;
6804 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6805 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6806 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6814 bpf_error(cstate
, "Mask syntax for networks only");
6818 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6824 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6831 gen_ecode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
6833 struct block
*b
, *tmp
;
6835 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6836 switch (cstate
->linktype
) {
6838 case DLT_NETANALYZER
:
6839 case DLT_NETANALYZER_TRANSPARENT
:
6840 tmp
= gen_prevlinkhdr_check(cstate
);
6841 b
= gen_ehostop(cstate
, eaddr
, (int)q
.dir
);
6846 return gen_fhostop(cstate
, eaddr
, (int)q
.dir
);
6848 return gen_thostop(cstate
, eaddr
, (int)q
.dir
);
6849 case DLT_IEEE802_11
:
6850 case DLT_PRISM_HEADER
:
6851 case DLT_IEEE802_11_RADIO_AVS
:
6852 case DLT_IEEE802_11_RADIO
:
6854 return gen_wlanhostop(cstate
, eaddr
, (int)q
.dir
);
6855 case DLT_IP_OVER_FC
:
6856 return gen_ipfchostop(cstate
, eaddr
, (int)q
.dir
);
6858 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6862 bpf_error(cstate
, "ethernet address used in non-ether expression");
6867 sappend(struct slist
*s0
, struct slist
*s1
)
6870 * This is definitely not the best way to do this, but the
6871 * lists will rarely get long.
6878 static struct slist
*
6879 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
6883 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
6888 static struct slist
*
6889 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
6893 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6899 * Modify "index" to use the value stored into its register as an
6900 * offset relative to the beginning of the header for the protocol
6901 * "proto", and allocate a register and put an item "size" bytes long
6902 * (1, 2, or 4) at that offset into that register, making it the register
6906 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
6908 struct slist
*s
, *tmp
;
6910 int regno
= alloc_reg(cstate
);
6912 free_reg(cstate
, inst
->regno
);
6916 bpf_error(cstate
, "data size must be 1, 2, or 4");
6932 bpf_error(cstate
, "unsupported index operation");
6936 * The offset is relative to the beginning of the packet
6937 * data, if we have a radio header. (If we don't, this
6940 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6941 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
6942 cstate
->linktype
!= DLT_PRISM_HEADER
)
6943 bpf_error(cstate
, "radio information not present in capture");
6946 * Load into the X register the offset computed into the
6947 * register specified by "index".
6949 s
= xfer_to_x(cstate
, inst
);
6952 * Load the item at that offset.
6954 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6956 sappend(inst
->s
, s
);
6961 * The offset is relative to the beginning of
6962 * the link-layer header.
6964 * XXX - what about ATM LANE? Should the index be
6965 * relative to the beginning of the AAL5 frame, so
6966 * that 0 refers to the beginning of the LE Control
6967 * field, or relative to the beginning of the LAN
6968 * frame, so that 0 refers, for Ethernet LANE, to
6969 * the beginning of the destination address?
6971 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
6974 * If "s" is non-null, it has code to arrange that the
6975 * X register contains the length of the prefix preceding
6976 * the link-layer header. Add to it the offset computed
6977 * into the register specified by "index", and move that
6978 * into the X register. Otherwise, just load into the X
6979 * register the offset computed into the register specified
6983 sappend(s
, xfer_to_a(cstate
, inst
));
6984 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
6985 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
6987 s
= xfer_to_x(cstate
, inst
);
6990 * Load the item at the sum of the offset we've put in the
6991 * X register and the offset of the start of the link
6992 * layer header (which is 0 if the radio header is
6993 * variable-length; that header length is what we put
6994 * into the X register and then added to the index).
6996 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
6997 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
6999 sappend(inst
->s
, s
);
7013 * The offset is relative to the beginning of
7014 * the network-layer header.
7015 * XXX - are there any cases where we want
7016 * cstate->off_nl_nosnap?
7018 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7021 * If "s" is non-null, it has code to arrange that the
7022 * X register contains the variable part of the offset
7023 * of the link-layer payload. Add to it the offset
7024 * computed into the register specified by "index",
7025 * and move that into the X register. Otherwise, just
7026 * load into the X register the offset computed into
7027 * the register specified by "index".
7030 sappend(s
, xfer_to_a(cstate
, inst
));
7031 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7032 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7034 s
= xfer_to_x(cstate
, inst
);
7037 * Load the item at the sum of the offset we've put in the
7038 * X register, the offset of the start of the network
7039 * layer header from the beginning of the link-layer
7040 * payload, and the constant part of the offset of the
7041 * start of the link-layer payload.
7043 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7044 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7046 sappend(inst
->s
, s
);
7049 * Do the computation only if the packet contains
7050 * the protocol in question.
7052 b
= gen_proto_abbrev(cstate
, proto
);
7054 gen_and(inst
->b
, b
);
7068 * The offset is relative to the beginning of
7069 * the transport-layer header.
7071 * Load the X register with the length of the IPv4 header
7072 * (plus the offset of the link-layer header, if it's
7073 * a variable-length header), in bytes.
7075 * XXX - are there any cases where we want
7076 * cstate->off_nl_nosnap?
7077 * XXX - we should, if we're built with
7078 * IPv6 support, generate code to load either
7079 * IPv4, IPv6, or both, as appropriate.
7081 s
= gen_loadx_iphdrlen(cstate
);
7084 * The X register now contains the sum of the variable
7085 * part of the offset of the link-layer payload and the
7086 * length of the network-layer header.
7088 * Load into the A register the offset relative to
7089 * the beginning of the transport layer header,
7090 * add the X register to that, move that to the
7091 * X register, and load with an offset from the
7092 * X register equal to the sum of the constant part of
7093 * the offset of the link-layer payload and the offset,
7094 * relative to the beginning of the link-layer payload,
7095 * of the network-layer header.
7097 sappend(s
, xfer_to_a(cstate
, inst
));
7098 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7099 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7100 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7101 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7102 sappend(inst
->s
, s
);
7105 * Do the computation only if the packet contains
7106 * the protocol in question - which is true only
7107 * if this is an IP datagram and is the first or
7108 * only fragment of that datagram.
7110 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7112 gen_and(inst
->b
, b
);
7113 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7118 * Do the computation only if the packet contains
7119 * the protocol in question.
7121 b
= gen_proto_abbrev(cstate
, Q_IPV6
);
7123 gen_and(inst
->b
, b
);
7128 * Check if we have an icmp6 next header
7130 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7132 gen_and(inst
->b
, b
);
7137 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7139 * If "s" is non-null, it has code to arrange that the
7140 * X register contains the variable part of the offset
7141 * of the link-layer payload. Add to it the offset
7142 * computed into the register specified by "index",
7143 * and move that into the X register. Otherwise, just
7144 * load into the X register the offset computed into
7145 * the register specified by "index".
7148 sappend(s
, xfer_to_a(cstate
, inst
));
7149 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7150 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7152 s
= xfer_to_x(cstate
, inst
);
7156 * Load the item at the sum of the offset we've put in the
7157 * X register, the offset of the start of the network
7158 * layer header from the beginning of the link-layer
7159 * payload, and the constant part of the offset of the
7160 * start of the link-layer payload.
7162 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7163 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7166 sappend(inst
->s
, s
);
7170 inst
->regno
= regno
;
7171 s
= new_stmt(cstate
, BPF_ST
);
7173 sappend(inst
->s
, s
);
7179 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7180 struct arth
*a1
, int reversed
)
7182 struct slist
*s0
, *s1
, *s2
;
7183 struct block
*b
, *tmp
;
7185 s0
= xfer_to_x(cstate
, a1
);
7186 s1
= xfer_to_a(cstate
, a0
);
7187 if (code
== BPF_JEQ
) {
7188 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7189 b
= new_block(cstate
, JMP(code
));
7193 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7199 sappend(a0
->s
, a1
->s
);
7203 free_reg(cstate
, a0
->regno
);
7204 free_reg(cstate
, a1
->regno
);
7206 /* 'and' together protocol checks */
7209 gen_and(a0
->b
, tmp
= a1
->b
);
7223 gen_loadlen(compiler_state_t
*cstate
)
7225 int regno
= alloc_reg(cstate
);
7226 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7229 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7230 s
->next
= new_stmt(cstate
, BPF_ST
);
7231 s
->next
->s
.k
= regno
;
7239 gen_loadi(compiler_state_t
*cstate
, int val
)
7245 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7247 reg
= alloc_reg(cstate
);
7249 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7251 s
->next
= new_stmt(cstate
, BPF_ST
);
7260 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7264 s
= xfer_to_a(cstate
, a
);
7266 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7269 s
= new_stmt(cstate
, BPF_ST
);
7277 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7280 struct slist
*s0
, *s1
, *s2
;
7283 * Disallow division by, or modulus by, zero; we do this here
7284 * so that it gets done even if the optimizer is disabled.
7286 if (code
== BPF_DIV
) {
7287 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7288 bpf_error(cstate
, "division by zero");
7289 } else if (code
== BPF_MOD
) {
7290 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7291 bpf_error(cstate
, "modulus by zero");
7293 s0
= xfer_to_x(cstate
, a1
);
7294 s1
= xfer_to_a(cstate
, a0
);
7295 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7300 sappend(a0
->s
, a1
->s
);
7302 free_reg(cstate
, a0
->regno
);
7303 free_reg(cstate
, a1
->regno
);
7305 s0
= new_stmt(cstate
, BPF_ST
);
7306 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7313 * Initialize the table of used registers and the current register.
7316 init_regs(compiler_state_t
*cstate
)
7319 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7323 * Return the next free register.
7326 alloc_reg(compiler_state_t
*cstate
)
7328 int n
= BPF_MEMWORDS
;
7331 if (cstate
->regused
[cstate
->curreg
])
7332 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7334 cstate
->regused
[cstate
->curreg
] = 1;
7335 return cstate
->curreg
;
7338 bpf_error(cstate
, "too many registers needed to evaluate expression");
7343 * Return a register to the table so it can
7347 free_reg(compiler_state_t
*cstate
, int n
)
7349 cstate
->regused
[n
] = 0;
7352 static struct block
*
7353 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7358 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7359 b
= new_block(cstate
, JMP(jmp
));
7367 gen_greater(compiler_state_t
*cstate
, int n
)
7369 return gen_len(cstate
, BPF_JGE
, n
);
7373 * Actually, this is less than or equal.
7376 gen_less(compiler_state_t
*cstate
, int n
)
7380 b
= gen_len(cstate
, BPF_JGT
, n
);
7387 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7388 * the beginning of the link-layer header.
7389 * XXX - that means you can't test values in the radiotap header, but
7390 * as that header is difficult if not impossible to parse generally
7391 * without a loop, that might not be a severe problem. A new keyword
7392 * "radio" could be added for that, although what you'd really want
7393 * would be a way of testing particular radio header values, which
7394 * would generate code appropriate to the radio header in question.
7397 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7407 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7410 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7414 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7418 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7422 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7426 b
= new_block(cstate
, JMP(BPF_JEQ
));
7433 static const u_char abroadcast
[] = { 0x0 };
7436 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7438 bpf_u_int32 hostmask
;
7439 struct block
*b0
, *b1
, *b2
;
7440 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7446 switch (cstate
->linktype
) {
7448 case DLT_ARCNET_LINUX
:
7449 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7451 case DLT_NETANALYZER
:
7452 case DLT_NETANALYZER_TRANSPARENT
:
7453 b1
= gen_prevlinkhdr_check(cstate
);
7454 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7459 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7461 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7462 case DLT_IEEE802_11
:
7463 case DLT_PRISM_HEADER
:
7464 case DLT_IEEE802_11_RADIO_AVS
:
7465 case DLT_IEEE802_11_RADIO
:
7467 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7468 case DLT_IP_OVER_FC
:
7469 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7471 bpf_error(cstate
, "not a broadcast link");
7477 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7478 * as an indication that we don't know the netmask, and fail
7481 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7482 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7483 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7484 hostmask
= ~cstate
->netmask
;
7485 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7486 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7487 (bpf_int32
)(~0 & hostmask
), hostmask
);
7492 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7497 * Generate code to test the low-order bit of a MAC address (that's
7498 * the bottom bit of the *first* byte).
7500 static struct block
*
7501 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7503 register struct block
*b0
;
7504 register struct slist
*s
;
7506 /* link[offset] & 1 != 0 */
7507 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7508 b0
= new_block(cstate
, JMP(BPF_JSET
));
7515 gen_multicast(compiler_state_t
*cstate
, int proto
)
7517 register struct block
*b0
, *b1
, *b2
;
7518 register struct slist
*s
;
7524 switch (cstate
->linktype
) {
7526 case DLT_ARCNET_LINUX
:
7527 /* all ARCnet multicasts use the same address */
7528 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7530 case DLT_NETANALYZER
:
7531 case DLT_NETANALYZER_TRANSPARENT
:
7532 b1
= gen_prevlinkhdr_check(cstate
);
7533 /* ether[0] & 1 != 0 */
7534 b0
= gen_mac_multicast(cstate
, 0);
7540 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7542 * XXX - was that referring to bit-order issues?
7544 /* fddi[1] & 1 != 0 */
7545 return gen_mac_multicast(cstate
, 1);
7547 /* tr[2] & 1 != 0 */
7548 return gen_mac_multicast(cstate
, 2);
7549 case DLT_IEEE802_11
:
7550 case DLT_PRISM_HEADER
:
7551 case DLT_IEEE802_11_RADIO_AVS
:
7552 case DLT_IEEE802_11_RADIO
:
7557 * For control frames, there is no DA.
7559 * For management frames, DA is at an
7560 * offset of 4 from the beginning of
7563 * For data frames, DA is at an offset
7564 * of 4 from the beginning of the packet
7565 * if To DS is clear and at an offset of
7566 * 16 from the beginning of the packet
7571 * Generate the tests to be done for data frames.
7573 * First, check for To DS set, i.e. "link[1] & 0x01".
7575 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7576 b1
= new_block(cstate
, JMP(BPF_JSET
));
7577 b1
->s
.k
= 0x01; /* To DS */
7581 * If To DS is set, the DA is at 16.
7583 b0
= gen_mac_multicast(cstate
, 16);
7587 * Now, check for To DS not set, i.e. check
7588 * "!(link[1] & 0x01)".
7590 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7591 b2
= new_block(cstate
, JMP(BPF_JSET
));
7592 b2
->s
.k
= 0x01; /* To DS */
7597 * If To DS is not set, the DA is at 4.
7599 b1
= gen_mac_multicast(cstate
, 4);
7603 * Now OR together the last two checks. That gives
7604 * the complete set of checks for data frames.
7609 * Now check for a data frame.
7610 * I.e, check "link[0] & 0x08".
7612 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7613 b1
= new_block(cstate
, JMP(BPF_JSET
));
7618 * AND that with the checks done for data frames.
7623 * If the high-order bit of the type value is 0, this
7624 * is a management frame.
7625 * I.e, check "!(link[0] & 0x08)".
7627 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7628 b2
= new_block(cstate
, JMP(BPF_JSET
));
7634 * For management frames, the DA is at 4.
7636 b1
= gen_mac_multicast(cstate
, 4);
7640 * OR that with the checks done for data frames.
7641 * That gives the checks done for management and
7647 * If the low-order bit of the type value is 1,
7648 * this is either a control frame or a frame
7649 * with a reserved type, and thus not a
7652 * I.e., check "!(link[0] & 0x04)".
7654 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7655 b1
= new_block(cstate
, JMP(BPF_JSET
));
7661 * AND that with the checks for data and management
7666 case DLT_IP_OVER_FC
:
7667 b0
= gen_mac_multicast(cstate
, 2);
7672 /* Link not known to support multicasts */
7676 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7677 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7682 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7683 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7687 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7692 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7693 * Outbound traffic is sent by this machine, while inbound traffic is
7694 * sent by a remote machine (and may include packets destined for a
7695 * unicast or multicast link-layer address we are not subscribing to).
7696 * These are the same definitions implemented by pcap_setdirection().
7697 * Capturing only unicast traffic destined for this host is probably
7698 * better accomplished using a higher-layer filter.
7701 gen_inbound(compiler_state_t
*cstate
, int dir
)
7703 register struct block
*b0
;
7706 * Only some data link types support inbound/outbound qualifiers.
7708 switch (cstate
->linktype
) {
7710 b0
= gen_relation(cstate
, BPF_JEQ
,
7711 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7712 gen_loadi(cstate
, 0),
7718 /* match outgoing packets */
7719 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7721 /* match incoming packets */
7722 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7727 /* match outgoing packets */
7728 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7730 /* to filter on inbound traffic, invert the match */
7735 #ifdef HAVE_NET_PFVAR_H
7737 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7738 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7744 /* match outgoing packets */
7745 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7747 /* match incoming packets */
7748 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7752 case DLT_JUNIPER_MFR
:
7753 case DLT_JUNIPER_MLFR
:
7754 case DLT_JUNIPER_MLPPP
:
7755 case DLT_JUNIPER_ATM1
:
7756 case DLT_JUNIPER_ATM2
:
7757 case DLT_JUNIPER_PPPOE
:
7758 case DLT_JUNIPER_PPPOE_ATM
:
7759 case DLT_JUNIPER_GGSN
:
7760 case DLT_JUNIPER_ES
:
7761 case DLT_JUNIPER_MONITOR
:
7762 case DLT_JUNIPER_SERVICES
:
7763 case DLT_JUNIPER_ETHER
:
7764 case DLT_JUNIPER_PPP
:
7765 case DLT_JUNIPER_FRELAY
:
7766 case DLT_JUNIPER_CHDLC
:
7767 case DLT_JUNIPER_VP
:
7768 case DLT_JUNIPER_ST
:
7769 case DLT_JUNIPER_ISM
:
7770 case DLT_JUNIPER_VS
:
7771 case DLT_JUNIPER_SRX_E2E
:
7772 case DLT_JUNIPER_FIBRECHANNEL
:
7773 case DLT_JUNIPER_ATM_CEMIC
:
7775 /* juniper flags (including direction) are stored
7776 * the byte after the 3-byte magic number */
7778 /* match outgoing packets */
7779 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7781 /* match incoming packets */
7782 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7788 * If we have packet meta-data indicating a direction,
7789 * and that metadata can be checked by BPF code, check
7790 * it. Otherwise, give up, as this link-layer type has
7791 * nothing in the packet data.
7793 * Currently, the only platform where a BPF filter can
7794 * check that metadata is Linux with the in-kernel
7795 * BPF interpreter. If other packet capture mechanisms
7796 * and BPF filters also supported this, it would be
7797 * nice. It would be even better if they made that
7798 * metadata available so that we could provide it
7799 * with newer capture APIs, allowing it to be saved
7802 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7804 * This is Linux with PF_PACKET support.
7805 * If this is a *live* capture, we can look at
7806 * special meta-data in the filter expression;
7807 * if it's a savefile, we can't.
7809 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7810 /* We have a FILE *, so this is a savefile */
7811 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
7816 /* match outgoing packets */
7817 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7820 /* to filter on inbound traffic, invert the match */
7823 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7824 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
7827 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7832 #ifdef HAVE_NET_PFVAR_H
7833 /* PF firewall log matched interface */
7835 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7840 if (cstate
->linktype
!= DLT_PFLOG
) {
7841 bpf_error(cstate
, "ifname supported only on PF linktype");
7844 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7845 off
= offsetof(struct pfloghdr
, ifname
);
7846 if (strlen(ifname
) >= len
) {
7847 bpf_error(cstate
, "ifname interface names can only be %d characters",
7851 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7855 /* PF firewall log ruleset name */
7857 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7861 if (cstate
->linktype
!= DLT_PFLOG
) {
7862 bpf_error(cstate
, "ruleset supported only on PF linktype");
7866 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7867 bpf_error(cstate
, "ruleset names can only be %ld characters",
7868 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7872 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7873 strlen(ruleset
), (const u_char
*)ruleset
);
7877 /* PF firewall log rule number */
7879 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7883 if (cstate
->linktype
!= DLT_PFLOG
) {
7884 bpf_error(cstate
, "rnr supported only on PF linktype");
7888 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7893 /* PF firewall log sub-rule number */
7895 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7899 if (cstate
->linktype
!= DLT_PFLOG
) {
7900 bpf_error(cstate
, "srnr supported only on PF linktype");
7904 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7909 /* PF firewall log reason code */
7911 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7915 if (cstate
->linktype
!= DLT_PFLOG
) {
7916 bpf_error(cstate
, "reason supported only on PF linktype");
7920 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7925 /* PF firewall log action */
7927 gen_pf_action(compiler_state_t
*cstate
, int action
)
7931 if (cstate
->linktype
!= DLT_PFLOG
) {
7932 bpf_error(cstate
, "action supported only on PF linktype");
7936 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7940 #else /* !HAVE_NET_PFVAR_H */
7942 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7944 bpf_error(cstate
, "libpcap was compiled without pf support");
7949 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7951 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7956 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
7958 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7963 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
7965 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7970 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
7972 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7977 gen_pf_action(compiler_state_t
*cstate
, int action
)
7979 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
7982 #endif /* HAVE_NET_PFVAR_H */
7984 /* IEEE 802.11 wireless header */
7986 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
7990 switch (cstate
->linktype
) {
7992 case DLT_IEEE802_11
:
7993 case DLT_PRISM_HEADER
:
7994 case DLT_IEEE802_11_RADIO_AVS
:
7995 case DLT_IEEE802_11_RADIO
:
7996 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8001 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8009 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8013 switch (cstate
->linktype
) {
8015 case DLT_IEEE802_11
:
8016 case DLT_PRISM_HEADER
:
8017 case DLT_IEEE802_11_RADIO_AVS
:
8018 case DLT_IEEE802_11_RADIO
:
8022 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8026 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8027 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8033 gen_acode(compiler_state_t
*cstate
, const u_char
*eaddr
, struct qual q
)
8035 switch (cstate
->linktype
) {
8038 case DLT_ARCNET_LINUX
:
8039 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8041 return (gen_ahostop(cstate
, eaddr
, (int)q
.dir
));
8043 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8049 bpf_error(cstate
, "aid supported only on ARCnet");
8054 static struct block
*
8055 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8057 register struct block
*b0
, *b1
;
8060 /* src comes first, different from Ethernet */
8062 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8065 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8068 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8069 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8075 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8076 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8081 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8085 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8089 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8093 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8097 bpf_error(cstate
, "'ra' is only supported on 802.11");
8101 bpf_error(cstate
, "'ta' is only supported on 802.11");
8108 static struct block
*
8109 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8111 struct block
*b0
, *b1
;
8113 /* check for VLAN, including QinQ */
8114 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8115 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8118 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8124 static struct block
*
8125 gen_vlan_vid_test(compiler_state_t
*cstate
, int vlan_num
)
8127 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8130 static struct block
*
8131 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8133 struct block
*b0
, *b1
;
8135 b0
= gen_vlan_tpid_test(cstate
);
8137 if (vlan_num
>= 0) {
8138 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8144 * Both payload and link header type follow the VLAN tags so that
8145 * both need to be updated.
8147 cstate
->off_linkpl
.constant_part
+= 4;
8148 cstate
->off_linktype
.constant_part
+= 4;
8153 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8154 /* add v to variable part of off */
8156 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8160 if (!off
->is_variable
)
8161 off
->is_variable
= 1;
8163 off
->reg
= alloc_reg(cstate
);
8165 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8168 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8171 s2
= new_stmt(cstate
, BPF_ST
);
8177 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8178 * and link type offsets first
8181 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8185 /* offset determined at run time, shift variable part */
8187 cstate
->is_vlan_vloffset
= 1;
8188 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8189 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8191 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8192 sappend(s
.next
, b_tpid
->head
->stmts
);
8193 b_tpid
->head
->stmts
= s
.next
;
8197 * patch block b_vid (VLAN id test) to load VID value either from packet
8198 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8201 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8203 struct slist
*s
, *s2
, *sjeq
;
8206 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8207 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8209 /* true -> next instructions, false -> beginning of b_vid */
8210 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8212 sjeq
->s
.jf
= b_vid
->stmts
;
8215 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8216 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8220 /* jump to the test in b_vid (bypass loading VID from packet data) */
8222 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8224 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8228 /* insert our statements at the beginning of b_vid */
8229 sappend(s
, b_vid
->stmts
);
8234 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8235 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8236 * tag can be either in metadata or in packet data; therefore if the
8237 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8238 * header for VLAN tag. As the decision is done at run time, we need
8239 * update variable part of the offsets
8241 static struct block
*
8242 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8244 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8247 /* generate new filter code based on extracting packet
8249 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8250 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8252 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8257 * This is tricky. We need to insert the statements updating variable
8258 * parts of offsets before the the traditional TPID and VID tests so
8259 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8260 * we do not want this update to affect those checks. That's why we
8261 * generate both test blocks first and insert the statements updating
8262 * variable parts of both offsets after that. This wouldn't work if
8263 * there already were variable length link header when entering this
8264 * function but gen_vlan_bpf_extensions() isn't called in that case.
8266 b_tpid
= gen_vlan_tpid_test(cstate
);
8268 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8270 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8274 if (vlan_num
>= 0) {
8275 gen_vlan_patch_vid_test(cstate
, b_vid
);
8285 * support IEEE 802.1Q VLAN trunk over ethernet
8288 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
8292 /* can't check for VLAN-encapsulated packets inside MPLS */
8293 if (cstate
->label_stack_depth
> 0)
8294 bpf_error(cstate
, "no VLAN match after MPLS");
8297 * Check for a VLAN packet, and then change the offsets to point
8298 * to the type and data fields within the VLAN packet. Just
8299 * increment the offsets, so that we can support a hierarchy, e.g.
8300 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8303 * XXX - this is a bit of a kludge. If we were to split the
8304 * compiler into a parser that parses an expression and
8305 * generates an expression tree, and a code generator that
8306 * takes an expression tree (which could come from our
8307 * parser or from some other parser) and generates BPF code,
8308 * we could perhaps make the offsets parameters of routines
8309 * and, in the handler for an "AND" node, pass to subnodes
8310 * other than the VLAN node the adjusted offsets.
8312 * This would mean that "vlan" would, instead of changing the
8313 * behavior of *all* tests after it, change only the behavior
8314 * of tests ANDed with it. That would change the documented
8315 * semantics of "vlan", which might break some expressions.
8316 * However, it would mean that "(vlan and ip) or ip" would check
8317 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8318 * checking only for VLAN-encapsulated IP, so that could still
8319 * be considered worth doing; it wouldn't break expressions
8320 * that are of the form "vlan and ..." or "vlan N and ...",
8321 * which I suspect are the most common expressions involving
8322 * "vlan". "vlan or ..." doesn't necessarily do what the user
8323 * would really want, now, as all the "or ..." tests would
8324 * be done assuming a VLAN, even though the "or" could be viewed
8325 * as meaning "or, if this isn't a VLAN packet...".
8327 switch (cstate
->linktype
) {
8330 case DLT_NETANALYZER
:
8331 case DLT_NETANALYZER_TRANSPARENT
:
8332 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8333 /* Verify that this is the outer part of the packet and
8334 * not encapsulated somehow. */
8335 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8336 cstate
->off_linkhdr
.constant_part
==
8337 cstate
->off_outermostlinkhdr
.constant_part
) {
8339 * Do we need special VLAN handling?
8341 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8342 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8344 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8347 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8350 case DLT_IEEE802_11
:
8351 case DLT_PRISM_HEADER
:
8352 case DLT_IEEE802_11_RADIO_AVS
:
8353 case DLT_IEEE802_11_RADIO
:
8354 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8358 bpf_error(cstate
, "no VLAN support for data link type %d",
8363 cstate
->vlan_stack_depth
++;
8372 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8374 struct block
*b0
, *b1
;
8376 if (cstate
->label_stack_depth
> 0) {
8377 /* just match the bottom-of-stack bit clear */
8378 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8381 * We're not in an MPLS stack yet, so check the link-layer
8382 * type against MPLS.
8384 switch (cstate
->linktype
) {
8386 case DLT_C_HDLC
: /* fall through */
8388 case DLT_NETANALYZER
:
8389 case DLT_NETANALYZER_TRANSPARENT
:
8390 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8394 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8397 /* FIXME add other DLT_s ...
8398 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8399 * leave it for now */
8402 bpf_error(cstate
, "no MPLS support for data link type %d",
8409 /* If a specific MPLS label is requested, check it */
8410 if (label_num
>= 0) {
8411 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8412 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8413 0xfffff000); /* only compare the first 20 bits */
8419 * Change the offsets to point to the type and data fields within
8420 * the MPLS packet. Just increment the offsets, so that we
8421 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8422 * capture packets with an outer label of 100000 and an inner
8425 * Increment the MPLS stack depth as well; this indicates that
8426 * we're checking MPLS-encapsulated headers, to make sure higher
8427 * level code generators don't try to match against IP-related
8428 * protocols such as Q_ARP, Q_RARP etc.
8430 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8432 cstate
->off_nl_nosnap
+= 4;
8433 cstate
->off_nl
+= 4;
8434 cstate
->label_stack_depth
++;
8439 * Support PPPOE discovery and session.
8442 gen_pppoed(compiler_state_t
*cstate
)
8444 /* check for PPPoE discovery */
8445 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8449 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8451 struct block
*b0
, *b1
;
8454 * Test against the PPPoE session link-layer type.
8456 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8458 /* If a specific session is requested, check PPPoE session id */
8459 if (sess_num
>= 0) {
8460 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8461 (bpf_int32
)sess_num
, 0x0000ffff);
8467 * Change the offsets to point to the type and data fields within
8468 * the PPP packet, and note that this is PPPoE rather than
8471 * XXX - this is a bit of a kludge. If we were to split the
8472 * compiler into a parser that parses an expression and
8473 * generates an expression tree, and a code generator that
8474 * takes an expression tree (which could come from our
8475 * parser or from some other parser) and generates BPF code,
8476 * we could perhaps make the offsets parameters of routines
8477 * and, in the handler for an "AND" node, pass to subnodes
8478 * other than the PPPoE node the adjusted offsets.
8480 * This would mean that "pppoes" would, instead of changing the
8481 * behavior of *all* tests after it, change only the behavior
8482 * of tests ANDed with it. That would change the documented
8483 * semantics of "pppoes", which might break some expressions.
8484 * However, it would mean that "(pppoes and ip) or ip" would check
8485 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8486 * checking only for VLAN-encapsulated IP, so that could still
8487 * be considered worth doing; it wouldn't break expressions
8488 * that are of the form "pppoes and ..." which I suspect are the
8489 * most common expressions involving "pppoes". "pppoes or ..."
8490 * doesn't necessarily do what the user would really want, now,
8491 * as all the "or ..." tests would be done assuming PPPoE, even
8492 * though the "or" could be viewed as meaning "or, if this isn't
8493 * a PPPoE packet...".
8495 * The "network-layer" protocol is PPPoE, which has a 6-byte
8496 * PPPoE header, followed by a PPP packet.
8498 * There is no HDLC encapsulation for the PPP packet (it's
8499 * encapsulated in PPPoES instead), so the link-layer type
8500 * starts at the first byte of the PPP packet. For PPPoE,
8501 * that offset is relative to the beginning of the total
8502 * link-layer payload, including any 802.2 LLC header, so
8503 * it's 6 bytes past cstate->off_nl.
8505 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8506 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8507 cstate
->off_linkpl
.reg
);
8509 cstate
->off_linktype
= cstate
->off_linkhdr
;
8510 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8513 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8518 /* Check that this is Geneve and the VNI is correct if
8519 * specified. Parameterized to handle both IPv4 and IPv6. */
8520 static struct block
*
8521 gen_geneve_check(compiler_state_t
*cstate
,
8522 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8523 enum e_offrel offrel
, int vni
)
8525 struct block
*b0
, *b1
;
8527 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8529 /* Check that we are operating on version 0. Otherwise, we
8530 * can't decode the rest of the fields. The version is 2 bits
8531 * in the first byte of the Geneve header. */
8532 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8537 vni
<<= 8; /* VNI is in the upper 3 bytes */
8538 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8547 /* The IPv4 and IPv6 Geneve checks need to do two things:
8548 * - Verify that this actually is Geneve with the right VNI.
8549 * - Place the IP header length (plus variable link prefix if
8550 * needed) into register A to be used later to compute
8551 * the inner packet offsets. */
8552 static struct block
*
8553 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8555 struct block
*b0
, *b1
;
8556 struct slist
*s
, *s1
;
8558 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8560 /* Load the IP header length into A. */
8561 s
= gen_loadx_iphdrlen(cstate
);
8563 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8566 /* Forcibly append these statements to the true condition
8567 * of the protocol check by creating a new block that is
8568 * always true and ANDing them. */
8569 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8578 static struct block
*
8579 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8581 struct block
*b0
, *b1
;
8582 struct slist
*s
, *s1
;
8584 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8586 /* Load the IP header length. We need to account for a
8587 * variable length link prefix if there is one. */
8588 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8590 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8594 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8598 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8602 /* Forcibly append these statements to the true condition
8603 * of the protocol check by creating a new block that is
8604 * always true and ANDing them. */
8605 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8608 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8617 /* We need to store three values based on the Geneve header::
8618 * - The offset of the linktype.
8619 * - The offset of the end of the Geneve header.
8620 * - The offset of the end of the encapsulated MAC header. */
8621 static struct slist
*
8622 gen_geneve_offsets(compiler_state_t
*cstate
)
8624 struct slist
*s
, *s1
, *s_proto
;
8626 /* First we need to calculate the offset of the Geneve header
8627 * itself. This is composed of the IP header previously calculated
8628 * (include any variable link prefix) and stored in A plus the
8629 * fixed sized headers (fixed link prefix, MAC length, and UDP
8631 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8632 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8634 /* Stash this in X since we'll need it later. */
8635 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8638 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8640 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8644 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8645 cstate
->off_linktype
.is_variable
= 1;
8646 cstate
->off_linktype
.constant_part
= 0;
8648 s1
= new_stmt(cstate
, BPF_ST
);
8649 s1
->s
.k
= cstate
->off_linktype
.reg
;
8652 /* Load the Geneve option length and mask and shift to get the
8653 * number of bytes. It is stored in the first byte of the Geneve
8655 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8659 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8663 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8667 /* Add in the rest of the Geneve base header. */
8668 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8672 /* Add the Geneve header length to its offset and store. */
8673 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8677 /* Set the encapsulated type as Ethernet. Even though we may
8678 * not actually have Ethernet inside there are two reasons this
8680 * - The linktype field is always in EtherType format regardless
8681 * of whether it is in Geneve or an inner Ethernet frame.
8682 * - The only link layer that we have specific support for is
8683 * Ethernet. We will confirm that the packet actually is
8684 * Ethernet at runtime before executing these checks. */
8685 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8687 s1
= new_stmt(cstate
, BPF_ST
);
8688 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8691 /* Calculate whether we have an Ethernet header or just raw IP/
8692 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8693 * and linktype by 14 bytes so that the network header can be found
8694 * seamlessly. Otherwise, keep what we've calculated already. */
8696 /* We have a bare jmp so we can't use the optimizer. */
8697 cstate
->no_optimize
= 1;
8699 /* Load the EtherType in the Geneve header, 2 bytes in. */
8700 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8704 /* Load X with the end of the Geneve header. */
8705 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8706 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8709 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8710 * end of this check, we should have the total length in X. In
8711 * the non-Ethernet case, it's already there. */
8712 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8713 s_proto
->s
.k
= ETHERTYPE_TEB
;
8714 sappend(s
, s_proto
);
8716 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8720 /* Since this is Ethernet, use the EtherType of the payload
8721 * directly as the linktype. Overwrite what we already have. */
8722 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8726 s1
= new_stmt(cstate
, BPF_ST
);
8727 s1
->s
.k
= cstate
->off_linktype
.reg
;
8730 /* Advance two bytes further to get the end of the Ethernet
8732 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8736 /* Move the result to X. */
8737 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8740 /* Store the final result of our linkpl calculation. */
8741 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8742 cstate
->off_linkpl
.is_variable
= 1;
8743 cstate
->off_linkpl
.constant_part
= 0;
8745 s1
= new_stmt(cstate
, BPF_STX
);
8746 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8755 /* Check to see if this is a Geneve packet. */
8757 gen_geneve(compiler_state_t
*cstate
, int vni
)
8759 struct block
*b0
, *b1
;
8762 b0
= gen_geneve4(cstate
, vni
);
8763 b1
= gen_geneve6(cstate
, vni
);
8768 /* Later filters should act on the payload of the Geneve frame,
8769 * update all of the header pointers. Attach this code so that
8770 * it gets executed in the event that the Geneve filter matches. */
8771 s
= gen_geneve_offsets(cstate
);
8773 b1
= gen_true(cstate
);
8774 sappend(s
, b1
->stmts
);
8779 cstate
->is_geneve
= 1;
8784 /* Check that the encapsulated frame has a link layer header
8785 * for Ethernet filters. */
8786 static struct block
*
8787 gen_geneve_ll_check(compiler_state_t
*cstate
)
8790 struct slist
*s
, *s1
;
8792 /* The easiest way to see if there is a link layer present
8793 * is to check if the link layer header and payload are not
8796 /* Geneve always generates pure variable offsets so we can
8797 * compare only the registers. */
8798 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8799 s
->s
.k
= cstate
->off_linkhdr
.reg
;
8801 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8802 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8805 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8814 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
8815 bpf_u_int32 jtype
, int reverse
)
8822 if (!cstate
->is_atm
)
8823 bpf_error(cstate
, "'vpi' supported only on raw ATM");
8824 if (cstate
->off_vpi
== (u_int
)-1)
8826 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
8831 if (!cstate
->is_atm
)
8832 bpf_error(cstate
, "'vci' supported only on raw ATM");
8833 if (cstate
->off_vci
== (u_int
)-1)
8835 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
8840 if (cstate
->off_proto
== (u_int
)-1)
8841 abort(); /* XXX - this isn't on FreeBSD */
8842 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
8847 if (cstate
->off_payload
== (u_int
)-1)
8849 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
8850 0xffffffff, jtype
, reverse
, jvalue
);
8854 if (!cstate
->is_atm
)
8855 bpf_error(cstate
, "'callref' supported only on raw ATM");
8856 if (cstate
->off_proto
== (u_int
)-1)
8858 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
8859 jtype
, reverse
, jvalue
);
8869 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
8871 struct block
*b0
, *b1
;
8876 /* Get all packets in Meta signalling Circuit */
8877 if (!cstate
->is_atm
)
8878 bpf_error(cstate
, "'metac' supported only on raw ATM");
8879 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8880 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
8885 /* Get all packets in Broadcast Circuit*/
8886 if (!cstate
->is_atm
)
8887 bpf_error(cstate
, "'bcc' supported only on raw ATM");
8888 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8889 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
8894 /* Get all cells in Segment OAM F4 circuit*/
8895 if (!cstate
->is_atm
)
8896 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
8897 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8898 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
8903 /* Get all cells in End-to-End OAM F4 Circuit*/
8904 if (!cstate
->is_atm
)
8905 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
8906 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8907 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
8912 /* Get all packets in connection Signalling Circuit */
8913 if (!cstate
->is_atm
)
8914 bpf_error(cstate
, "'sc' supported only on raw ATM");
8915 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8916 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
8921 /* Get all packets in ILMI Circuit */
8922 if (!cstate
->is_atm
)
8923 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
8924 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
8925 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
8930 /* Get all LANE packets */
8931 if (!cstate
->is_atm
)
8932 bpf_error(cstate
, "'lane' supported only on raw ATM");
8933 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8936 * Arrange that all subsequent tests assume LANE
8937 * rather than LLC-encapsulated packets, and set
8938 * the offsets appropriately for LANE-encapsulated
8941 * We assume LANE means Ethernet, not Token Ring.
8943 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
8944 cstate
->off_payload
+ 2, /* Ethernet header */
8946 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
8947 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
8948 cstate
->off_nl
= 0; /* Ethernet II */
8949 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
8953 /* Get all LLC-encapsulated packets */
8954 if (!cstate
->is_atm
)
8955 bpf_error(cstate
, "'llc' supported only on raw ATM");
8956 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8957 cstate
->linktype
= cstate
->prevlinktype
;
8967 * Filtering for MTP2 messages based on li value
8968 * FISU, length is null
8969 * LSSU, length is 1 or 2
8970 * MSU, length is 3 or more
8971 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8974 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
8976 struct block
*b0
, *b1
;
8981 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8982 (cstate
->linktype
!= DLT_ERF
) &&
8983 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8984 bpf_error(cstate
, "'fisu' supported only on MTP2");
8985 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
8986 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8990 if ( (cstate
->linktype
!= DLT_MTP2
) &&
8991 (cstate
->linktype
!= DLT_ERF
) &&
8992 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
8993 bpf_error(cstate
, "'lssu' supported only on MTP2");
8994 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8995 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9000 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9001 (cstate
->linktype
!= DLT_ERF
) &&
9002 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9003 bpf_error(cstate
, "'msu' supported only on MTP2");
9004 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9008 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9009 (cstate
->linktype
!= DLT_ERF
) &&
9010 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9011 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9012 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9013 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9017 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9018 (cstate
->linktype
!= DLT_ERF
) &&
9019 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9020 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9021 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9022 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9027 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9028 (cstate
->linktype
!= DLT_ERF
) &&
9029 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9030 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9031 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9041 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
9042 bpf_u_int32 jtype
, int reverse
)
9045 bpf_u_int32 val1
, val2
, val3
;
9046 u_int newoff_sio
= cstate
->off_sio
;
9047 u_int newoff_opc
= cstate
->off_opc
;
9048 u_int newoff_dpc
= cstate
->off_dpc
;
9049 u_int newoff_sls
= cstate
->off_sls
;
9051 switch (mtp3field
) {
9054 newoff_sio
+= 3; /* offset for MTP2_HSL */
9058 if (cstate
->off_sio
== (u_int
)-1)
9059 bpf_error(cstate
, "'sio' supported only on SS7");
9060 /* sio coded on 1 byte so max value 255 */
9062 bpf_error(cstate
, "sio value %u too big; max value = 255",
9064 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9065 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9071 if (cstate
->off_opc
== (u_int
)-1)
9072 bpf_error(cstate
, "'opc' supported only on SS7");
9073 /* opc coded on 14 bits so max value 16383 */
9075 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9077 /* the following instructions are made to convert jvalue
9078 * to the form used to write opc in an ss7 message*/
9079 val1
= jvalue
& 0x00003c00;
9081 val2
= jvalue
& 0x000003fc;
9083 val3
= jvalue
& 0x00000003;
9085 jvalue
= val1
+ val2
+ val3
;
9086 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9087 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9095 if (cstate
->off_dpc
== (u_int
)-1)
9096 bpf_error(cstate
, "'dpc' supported only on SS7");
9097 /* dpc coded on 14 bits so max value 16383 */
9099 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9101 /* the following instructions are made to convert jvalue
9102 * to the forme used to write dpc in an ss7 message*/
9103 val1
= jvalue
& 0x000000ff;
9105 val2
= jvalue
& 0x00003f00;
9107 jvalue
= val1
+ val2
;
9108 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9109 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9115 if (cstate
->off_sls
== (u_int
)-1)
9116 bpf_error(cstate
, "'sls' supported only on SS7");
9117 /* sls coded on 4 bits so max value 15 */
9119 bpf_error(cstate
, "sls value %u too big; max value = 15",
9121 /* the following instruction is made to convert jvalue
9122 * to the forme used to write sls in an ss7 message*/
9123 jvalue
= jvalue
<< 4;
9124 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9125 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9134 static struct block
*
9135 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9140 * Q.2931 signalling protocol messages for handling virtual circuits
9141 * establishment and teardown
9146 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9150 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9154 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9158 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9162 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9165 case A_RELEASE_DONE
:
9166 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9176 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9178 struct block
*b0
, *b1
;
9183 if (!cstate
->is_atm
)
9184 bpf_error(cstate
, "'oam' supported only on raw ATM");
9185 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
9189 if (!cstate
->is_atm
)
9190 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9192 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9193 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9195 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9201 * Get Q.2931 signalling messages for switched
9202 * virtual connection
9204 if (!cstate
->is_atm
)
9205 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9206 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9207 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9209 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9211 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9213 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9215 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9217 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9222 if (!cstate
->is_atm
)
9223 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9224 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9225 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9227 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9229 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9231 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9233 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);