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>
55 #include "ethertype.h"
59 #include "ieee80211.h"
61 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
70 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
96 uint16_t u6_addr16
[8];
97 uint32_t u6_addr32
[4];
99 #define s6_addr in6_u.u6_addr8
100 #define s6_addr16 in6_u.u6_addr16
101 #define s6_addr32 in6_u.u6_addr32
102 #define s6_addr64 in6_u.u6_addr64
105 typedef unsigned short sa_family_t
;
107 #define __SOCKADDR_COMMON(sa_prefix) \
108 sa_family_t sa_prefix##family
110 /* Ditto, for IPv6. */
113 __SOCKADDR_COMMON (sin6_
);
114 uint16_t sin6_port
; /* Transport layer port # */
115 uint32_t sin6_flowinfo
; /* IPv6 flow information */
116 struct in6_addr sin6_addr
; /* IPv6 address */
119 #ifndef EAI_ADDRFAMILY
121 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
122 int ai_family
; /* PF_xxx */
123 int ai_socktype
; /* SOCK_xxx */
124 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
125 size_t ai_addrlen
; /* length of ai_addr */
126 char *ai_canonname
; /* canonical name for hostname */
127 struct sockaddr
*ai_addr
; /* binary address */
128 struct addrinfo
*ai_next
; /* next structure in linked list */
130 #endif /* EAI_ADDRFAMILY */
131 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
134 #include <netdb.h> /* for "struct addrinfo" */
136 #include <pcap/namedb.h>
138 #include "nametoaddr.h"
140 #define ETHERMTU 1500
142 #ifndef ETHERTYPE_TEB
143 #define ETHERTYPE_TEB 0x6558
146 #ifndef IPPROTO_HOPOPTS
147 #define IPPROTO_HOPOPTS 0
149 #ifndef IPPROTO_ROUTING
150 #define IPPROTO_ROUTING 43
152 #ifndef IPPROTO_FRAGMENT
153 #define IPPROTO_FRAGMENT 44
155 #ifndef IPPROTO_DSTOPTS
156 #define IPPROTO_DSTOPTS 60
159 #define IPPROTO_SCTP 132
162 #define GENEVE_PORT 6081
164 #ifdef HAVE_OS_PROTO_H
165 #include "os-proto.h"
168 #define JMP(c) ((c)|BPF_JMP|BPF_K)
171 * "Push" the current value of the link-layer header type and link-layer
172 * header offset onto a "stack", and set a new value. (It's not a
173 * full-blown stack; we keep only the top two items.)
175 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
177 (cs)->prevlinktype = (cs)->linktype; \
178 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
179 (cs)->linktype = (new_linktype); \
180 (cs)->off_linkhdr.is_variable = (new_is_variable); \
181 (cs)->off_linkhdr.constant_part = (new_constant_part); \
182 (cs)->off_linkhdr.reg = (new_reg); \
183 (cs)->is_geneve = 0; \
187 * Offset "not set" value.
189 #define OFFSET_NOT_SET 0xffffffffU
192 * Absolute offsets, which are offsets from the beginning of the raw
193 * packet data, are, in the general case, the sum of a variable value
194 * and a constant value; the variable value may be absent, in which
195 * case the offset is only the constant value, and the constant value
196 * may be zero, in which case the offset is only the variable value.
198 * bpf_abs_offset is a structure containing all that information:
200 * is_variable is 1 if there's a variable part.
202 * constant_part is the constant part of the value, possibly zero;
204 * if is_variable is 1, reg is the register number for a register
205 * containing the variable value if the register has been assigned,
215 * Value passed to gen_load_a() to indicate what the offset argument
216 * is relative to the beginning of.
219 OR_PACKET
, /* full packet data */
220 OR_LINKHDR
, /* link-layer header */
221 OR_PREVLINKHDR
, /* previous link-layer header */
222 OR_LLC
, /* 802.2 LLC header */
223 OR_PREVMPLSHDR
, /* previous MPLS header */
224 OR_LINKTYPE
, /* link-layer type */
225 OR_LINKPL
, /* link-layer payload */
226 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
227 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
228 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
232 * We divy out chunks of memory rather than call malloc each time so
233 * we don't have to worry about leaking memory. It's probably
234 * not a big deal if all this memory was wasted but if this ever
235 * goes into a library that would probably not be a good idea.
237 * XXX - this *is* in a library....
240 #define CHUNK0SIZE 1024
246 /* Code generator state */
248 struct _compiler_state
{
258 int outermostlinktype
;
263 /* Hack for handling VLAN and MPLS stacks. */
264 u_int label_stack_depth
;
265 u_int vlan_stack_depth
;
271 * As errors are handled by a longjmp, anything allocated must
272 * be freed in the longjmp handler, so it must be reachable
275 * One thing that's allocated is the result of pcap_nametoaddrinfo();
276 * it must be freed with freeaddrinfo(). This variable points to
277 * any addrinfo structure that would need to be freed.
282 * Another thing that's allocated is the result of pcap_ether_aton();
283 * it must be freed with free(). This variable points to any
284 * address that would need to be freed.
289 * Various code constructs need to know the layout of the packet.
290 * These values give the necessary offsets from the beginning
291 * of the packet data.
295 * Absolute offset of the beginning of the link-layer header.
297 bpf_abs_offset off_linkhdr
;
300 * If we're checking a link-layer header for a packet encapsulated
301 * in another protocol layer, this is the equivalent information
302 * for the previous layers' link-layer header from the beginning
303 * of the raw packet data.
305 bpf_abs_offset off_prevlinkhdr
;
308 * This is the equivalent information for the outermost layers'
311 bpf_abs_offset off_outermostlinkhdr
;
314 * Absolute offset of the beginning of the link-layer payload.
316 bpf_abs_offset off_linkpl
;
319 * "off_linktype" is the offset to information in the link-layer
320 * header giving the packet type. This is an absolute offset
321 * from the beginning of the packet.
323 * For Ethernet, it's the offset of the Ethernet type field; this
324 * means that it must have a value that skips VLAN tags.
326 * For link-layer types that always use 802.2 headers, it's the
327 * offset of the LLC header; this means that it must have a value
328 * that skips VLAN tags.
330 * For PPP, it's the offset of the PPP type field.
332 * For Cisco HDLC, it's the offset of the CHDLC type field.
334 * For BSD loopback, it's the offset of the AF_ value.
336 * For Linux cooked sockets, it's the offset of the type field.
338 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
339 * encapsulation, in which case, IP is assumed.
341 bpf_abs_offset off_linktype
;
344 * TRUE if the link layer includes an ATM pseudo-header.
349 * TRUE if "geneve" appeared in the filter; it causes us to
350 * generate code that checks for a Geneve header and assume
351 * that later filters apply to the encapsulated payload.
356 * TRUE if we need variable length part of VLAN offset
358 int is_vlan_vloffset
;
361 * These are offsets for the ATM pseudo-header.
368 * These are offsets for the MTP2 fields.
374 * These are offsets for the MTP3 fields.
382 * This is the offset of the first byte after the ATM pseudo_header,
383 * or -1 if there is no ATM pseudo-header.
388 * These are offsets to the beginning of the network-layer header.
389 * They are relative to the beginning of the link-layer payload
390 * (i.e., they don't include off_linkhdr.constant_part or
391 * off_linkpl.constant_part).
393 * If the link layer never uses 802.2 LLC:
395 * "off_nl" and "off_nl_nosnap" are the same.
397 * If the link layer always uses 802.2 LLC:
399 * "off_nl" is the offset if there's a SNAP header following
402 * "off_nl_nosnap" is the offset if there's no SNAP header.
404 * If the link layer is Ethernet:
406 * "off_nl" is the offset if the packet is an Ethernet II packet
407 * (we assume no 802.3+802.2+SNAP);
409 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
410 * with an 802.2 header following it.
416 * Here we handle simple allocation of the scratch registers.
417 * If too many registers are alloc'd, the allocator punts.
419 int regused
[BPF_MEMWORDS
];
425 struct chunk chunks
[NCHUNKS
];
430 bpf_parser_error(compiler_state_t
*cstate
, const char *msg
)
432 bpf_error(cstate
, "can't parse filter expression: %s", msg
);
438 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
443 if (cstate
->bpf_pcap
!= NULL
)
444 (void)pcap_vsnprintf(pcap_geterr(cstate
->bpf_pcap
),
445 PCAP_ERRBUF_SIZE
, fmt
, ap
);
447 longjmp(cstate
->top_ctx
, 1);
451 static void init_linktype(compiler_state_t
*, pcap_t
*);
453 static void init_regs(compiler_state_t
*);
454 static int alloc_reg(compiler_state_t
*);
455 static void free_reg(compiler_state_t
*, int);
457 static void initchunks(compiler_state_t
*cstate
);
458 static void *newchunk(compiler_state_t
*cstate
, size_t);
459 static void freechunks(compiler_state_t
*cstate
);
460 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
461 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
462 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
463 static inline void syntax(compiler_state_t
*cstate
);
465 static void backpatch(struct block
*, struct block
*);
466 static void merge(struct block
*, struct block
*);
467 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
469 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
471 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
473 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
475 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
477 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
478 u_int
, bpf_int32
, bpf_u_int32
);
479 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
480 u_int
, const u_char
*);
481 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
482 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
483 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
485 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
487 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
488 static struct block
*gen_uncond(compiler_state_t
*, int);
489 static inline struct block
*gen_true(compiler_state_t
*);
490 static inline struct block
*gen_false(compiler_state_t
*);
491 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
492 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
493 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
494 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
495 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
496 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
497 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
498 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
499 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
501 static int ethertype_to_ppptype(int);
502 static struct block
*gen_linktype(compiler_state_t
*, int);
503 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
504 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
505 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
506 int, int, u_int
, u_int
);
508 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
509 struct in6_addr
*, int, int, u_int
, u_int
);
511 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
512 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
513 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
514 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
515 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
516 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
517 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
518 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
519 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
522 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
523 struct in6_addr
*, int, int, int);
526 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
527 struct addrinfo
*, int, int);
529 static struct block
*gen_ipfrag(compiler_state_t
*);
530 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
531 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
533 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
534 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
536 struct block
*gen_portop(compiler_state_t
*, int, int, int);
537 static struct block
*gen_port(compiler_state_t
*, int, int, int);
538 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
539 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
540 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
541 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
542 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
543 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
544 static int lookup_proto(compiler_state_t
*, const char *, int);
545 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
546 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
547 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
548 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
549 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
550 static struct block
*gen_len(compiler_state_t
*, int, int);
551 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
552 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
554 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
555 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
558 initchunks(compiler_state_t
*cstate
)
562 for (i
= 0; i
< NCHUNKS
; i
++) {
563 cstate
->chunks
[i
].n_left
= 0;
564 cstate
->chunks
[i
].m
= NULL
;
566 cstate
->cur_chunk
= 0;
570 newchunk(compiler_state_t
*cstate
, size_t n
)
577 /* XXX Round up to nearest long. */
578 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
580 /* XXX Round up to structure boundary. */
584 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
585 if (n
> cp
->n_left
) {
587 k
= ++cstate
->cur_chunk
;
589 bpf_error(cstate
, "out of memory");
590 size
= CHUNK0SIZE
<< k
;
591 cp
->m
= (void *)malloc(size
);
593 bpf_error(cstate
, "out of memory");
594 memset((char *)cp
->m
, 0, size
);
597 bpf_error(cstate
, "out of memory");
600 return (void *)((char *)cp
->m
+ cp
->n_left
);
604 freechunks(compiler_state_t
*cstate
)
608 for (i
= 0; i
< NCHUNKS
; ++i
)
609 if (cstate
->chunks
[i
].m
!= NULL
)
610 free(cstate
->chunks
[i
].m
);
614 * A strdup whose allocations are freed after code generation is over.
617 sdup(compiler_state_t
*cstate
, const char *s
)
619 size_t n
= strlen(s
) + 1;
620 char *cp
= newchunk(cstate
, n
);
622 pcap_strlcpy(cp
, s
, n
);
626 static inline struct block
*
627 new_block(compiler_state_t
*cstate
, int code
)
631 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
638 static inline struct slist
*
639 new_stmt(compiler_state_t
*cstate
, int code
)
643 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
649 static struct block
*
650 gen_retblk(compiler_state_t
*cstate
, int v
)
652 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
658 static inline PCAP_NORETURN_DEF
void
659 syntax(compiler_state_t
*cstate
)
661 bpf_error(cstate
, "syntax error in filter expression");
665 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
666 const char *buf
, int optimize
, bpf_u_int32 mask
)
671 compiler_state_t cstate
;
672 const char * volatile xbuf
= buf
;
673 yyscan_t scanner
= NULL
;
674 volatile YY_BUFFER_STATE in_buffer
= NULL
;
679 * If this pcap_t hasn't been activated, it doesn't have a
680 * link-layer type, so we can't use it.
683 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
684 "not-yet-activated pcap_t passed to pcap_compile");
696 * If the device on which we're capturing need to be notified
697 * that a new filter is being compiled, do so.
699 * This allows them to save a copy of it, in case, for example,
700 * they're implementing a form of remote packet capture, and
701 * want the remote machine to filter out the packets in which
702 * it's sending the packets it's captured.
704 * XXX - the fact that we happen to be compiling a filter
705 * doesn't necessarily mean we'll be installing it as the
706 * filter for this pcap_t; we might be running it from userland
707 * on captured packets to do packet classification. We really
708 * need a better way of handling this, but this is all that
709 * the WinPcap code did.
711 if (p
->save_current_filter_op
!= NULL
)
712 (p
->save_current_filter_op
)(p
, buf
);
716 cstate
.no_optimize
= 0;
721 cstate
.ic
.root
= NULL
;
722 cstate
.ic
.cur_mark
= 0;
726 if (setjmp(cstate
.top_ctx
)) {
728 if (cstate
.ai
!= NULL
)
729 freeaddrinfo(cstate
.ai
);
731 if (cstate
.e
!= NULL
)
737 cstate
.netmask
= mask
;
739 cstate
.snaplen
= pcap_snapshot(p
);
740 if (cstate
.snaplen
== 0) {
741 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
742 "snaplen of 0 rejects all packets");
747 if (pcap_lex_init(&scanner
) != 0)
748 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
749 errno
, "can't initialize scanner");
750 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
753 * Associate the compiler state with the lexical analyzer
756 pcap_set_extra(&cstate
, scanner
);
758 init_linktype(&cstate
, p
);
759 (void)pcap_parse(scanner
, &cstate
);
761 if (cstate
.ic
.root
== NULL
)
762 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
764 if (optimize
&& !cstate
.no_optimize
) {
765 bpf_optimize(&cstate
, &cstate
.ic
);
766 if (cstate
.ic
.root
== NULL
||
767 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0))
768 bpf_error(&cstate
, "expression rejects all packets");
770 program
->bf_insns
= icode_to_fcode(&cstate
, &cstate
.ic
, cstate
.ic
.root
, &len
);
771 program
->bf_len
= len
;
773 rc
= 0; /* We're all okay */
777 * Clean up everything for the lexical analyzer.
779 if (in_buffer
!= NULL
)
780 pcap__delete_buffer(in_buffer
, scanner
);
782 pcap_lex_destroy(scanner
);
785 * Clean up our own allocated memory.
793 * entry point for using the compiler with no pcap open
794 * pass in all the stuff that is needed explicitly instead.
797 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
798 struct bpf_program
*program
,
799 const char *buf
, int optimize
, bpf_u_int32 mask
)
804 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
807 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
813 * Clean up a "struct bpf_program" by freeing all the memory allocated
817 pcap_freecode(struct bpf_program
*program
)
820 if (program
->bf_insns
!= NULL
) {
821 free((char *)program
->bf_insns
);
822 program
->bf_insns
= NULL
;
827 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
828 * which of the jt and jf fields has been resolved and which is a pointer
829 * back to another unresolved block (or nil). At least one of the fields
830 * in each block is already resolved.
833 backpatch(struct block
*list
, struct block
*target
)
850 * Merge the lists in b0 and b1, using the 'sense' field to indicate
851 * which of jt and jf is the link.
854 merge(struct block
*b0
, struct block
*b1
)
856 register struct block
**p
= &b0
;
858 /* Find end of list. */
860 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
862 /* Concatenate the lists. */
867 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
869 struct block
*ppi_dlt_check
;
872 * Insert before the statements of the first (root) block any
873 * statements needed to load the lengths of any variable-length
874 * headers into registers.
876 * XXX - a fancier strategy would be to insert those before the
877 * statements of all blocks that use those lengths and that
878 * have no predecessors that use them, so that we only compute
879 * the lengths if we need them. There might be even better
880 * approaches than that.
882 * However, those strategies would be more complicated, and
883 * as we don't generate code to compute a length if the
884 * program has no tests that use the length, and as most
885 * tests will probably use those lengths, we would just
886 * postpone computing the lengths so that it's not done
887 * for tests that fail early, and it's not clear that's
890 insert_compute_vloffsets(cstate
, p
->head
);
893 * For DLT_PPI captures, generate a check of the per-packet
894 * DLT value to make sure it's DLT_IEEE802_11.
896 * XXX - TurboCap cards use DLT_PPI for Ethernet.
897 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
898 * with appropriate Ethernet information and use that rather
899 * than using something such as DLT_PPI where you don't know
900 * the link-layer header type until runtime, which, in the
901 * general case, would force us to generate both Ethernet *and*
902 * 802.11 code (*and* anything else for which PPI is used)
903 * and choose between them early in the BPF program?
905 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
906 if (ppi_dlt_check
!= NULL
)
907 gen_and(ppi_dlt_check
, p
);
909 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
910 p
->sense
= !p
->sense
;
911 backpatch(p
, gen_retblk(cstate
, 0));
912 cstate
->ic
.root
= p
->head
;
916 gen_and(struct block
*b0
, struct block
*b1
)
918 backpatch(b0
, b1
->head
);
919 b0
->sense
= !b0
->sense
;
920 b1
->sense
= !b1
->sense
;
922 b1
->sense
= !b1
->sense
;
927 gen_or(struct block
*b0
, struct block
*b1
)
929 b0
->sense
= !b0
->sense
;
930 backpatch(b0
, b1
->head
);
931 b0
->sense
= !b0
->sense
;
937 gen_not(struct block
*b
)
939 b
->sense
= !b
->sense
;
942 static struct block
*
943 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
944 u_int size
, bpf_int32 v
)
946 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
949 static struct block
*
950 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
951 u_int size
, bpf_int32 v
)
953 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
956 static struct block
*
957 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
958 u_int size
, bpf_int32 v
)
960 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
963 static struct block
*
964 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
965 u_int size
, bpf_int32 v
)
967 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
970 static struct block
*
971 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
972 u_int size
, bpf_int32 v
)
974 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
977 static struct block
*
978 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
979 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
981 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
984 static struct block
*
985 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
986 u_int size
, const u_char
*v
)
988 register struct block
*b
, *tmp
;
991 * XXX - the actual *instructions* do unsigned comparisons on
992 * most platforms, and the load instructions don't do sign
993 * extension, so gen_cmp() should really take an unsigned
996 * As the load instructons also don't do sign-extension, we
997 * fetch the values from the byte array as unsigned. We don't
998 * want to use the signed versions of the extract calls.
1002 register const u_char
*p
= &v
[size
- 4];
1004 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1005 (bpf_int32
)EXTRACT_BE_U_4(p
));
1012 register const u_char
*p
= &v
[size
- 2];
1014 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1015 (bpf_int32
)EXTRACT_BE_U_2(p
));
1022 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1031 * AND the field of size "size" at offset "offset" relative to the header
1032 * specified by "offrel" with "mask", and compare it with the value "v"
1033 * with the test specified by "jtype"; if "reverse" is true, the test
1034 * should test the opposite of "jtype".
1036 static struct block
*
1037 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1038 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1041 struct slist
*s
, *s2
;
1044 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1046 if (mask
!= 0xffffffff) {
1047 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1052 b
= new_block(cstate
, JMP(jtype
));
1055 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1061 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1063 cstate
->pcap_fddipad
= p
->fddipad
;
1066 * We start out with only one link-layer header.
1068 cstate
->outermostlinktype
= pcap_datalink(p
);
1069 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1070 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1071 cstate
->off_outermostlinkhdr
.reg
= -1;
1073 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1074 cstate
->off_prevlinkhdr
.constant_part
= 0;
1075 cstate
->off_prevlinkhdr
.is_variable
= 0;
1076 cstate
->off_prevlinkhdr
.reg
= -1;
1078 cstate
->linktype
= cstate
->outermostlinktype
;
1079 cstate
->off_linkhdr
.constant_part
= 0;
1080 cstate
->off_linkhdr
.is_variable
= 0;
1081 cstate
->off_linkhdr
.reg
= -1;
1086 cstate
->off_linkpl
.constant_part
= 0;
1087 cstate
->off_linkpl
.is_variable
= 0;
1088 cstate
->off_linkpl
.reg
= -1;
1090 cstate
->off_linktype
.constant_part
= 0;
1091 cstate
->off_linktype
.is_variable
= 0;
1092 cstate
->off_linktype
.reg
= -1;
1095 * Assume it's not raw ATM with a pseudo-header, for now.
1098 cstate
->off_vpi
= OFFSET_NOT_SET
;
1099 cstate
->off_vci
= OFFSET_NOT_SET
;
1100 cstate
->off_proto
= OFFSET_NOT_SET
;
1101 cstate
->off_payload
= OFFSET_NOT_SET
;
1106 cstate
->is_geneve
= 0;
1109 * No variable length VLAN offset by default
1111 cstate
->is_vlan_vloffset
= 0;
1114 * And assume we're not doing SS7.
1116 cstate
->off_li
= OFFSET_NOT_SET
;
1117 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1118 cstate
->off_sio
= OFFSET_NOT_SET
;
1119 cstate
->off_opc
= OFFSET_NOT_SET
;
1120 cstate
->off_dpc
= OFFSET_NOT_SET
;
1121 cstate
->off_sls
= OFFSET_NOT_SET
;
1123 cstate
->label_stack_depth
= 0;
1124 cstate
->vlan_stack_depth
= 0;
1126 switch (cstate
->linktype
) {
1129 cstate
->off_linktype
.constant_part
= 2;
1130 cstate
->off_linkpl
.constant_part
= 6;
1131 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1132 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1135 case DLT_ARCNET_LINUX
:
1136 cstate
->off_linktype
.constant_part
= 4;
1137 cstate
->off_linkpl
.constant_part
= 8;
1138 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1139 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1143 cstate
->off_linktype
.constant_part
= 12;
1144 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1145 cstate
->off_nl
= 0; /* Ethernet II */
1146 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1151 * SLIP doesn't have a link level type. The 16 byte
1152 * header is hacked into our SLIP driver.
1154 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1155 cstate
->off_linkpl
.constant_part
= 16;
1157 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1160 case DLT_SLIP_BSDOS
:
1161 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1162 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1164 cstate
->off_linkpl
.constant_part
= 24;
1166 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1171 cstate
->off_linktype
.constant_part
= 0;
1172 cstate
->off_linkpl
.constant_part
= 4;
1174 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1178 cstate
->off_linktype
.constant_part
= 0;
1179 cstate
->off_linkpl
.constant_part
= 12;
1181 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1186 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1187 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1188 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1189 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1191 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1196 * This does no include the Ethernet header, and
1197 * only covers session state.
1199 cstate
->off_linktype
.constant_part
= 6;
1200 cstate
->off_linkpl
.constant_part
= 8;
1202 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1206 cstate
->off_linktype
.constant_part
= 5;
1207 cstate
->off_linkpl
.constant_part
= 24;
1209 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1214 * FDDI doesn't really have a link-level type field.
1215 * We set "off_linktype" to the offset of the LLC header.
1217 * To check for Ethernet types, we assume that SSAP = SNAP
1218 * is being used and pick out the encapsulated Ethernet type.
1219 * XXX - should we generate code to check for SNAP?
1221 cstate
->off_linktype
.constant_part
= 13;
1222 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1223 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1224 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1225 cstate
->off_nl
= 8; /* 802.2+SNAP */
1226 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1231 * Token Ring doesn't really have a link-level type field.
1232 * We set "off_linktype" to the offset of the LLC header.
1234 * To check for Ethernet types, we assume that SSAP = SNAP
1235 * is being used and pick out the encapsulated Ethernet type.
1236 * XXX - should we generate code to check for SNAP?
1238 * XXX - the header is actually variable-length.
1239 * Some various Linux patched versions gave 38
1240 * as "off_linktype" and 40 as "off_nl"; however,
1241 * if a token ring packet has *no* routing
1242 * information, i.e. is not source-routed, the correct
1243 * values are 20 and 22, as they are in the vanilla code.
1245 * A packet is source-routed iff the uppermost bit
1246 * of the first byte of the source address, at an
1247 * offset of 8, has the uppermost bit set. If the
1248 * packet is source-routed, the total number of bytes
1249 * of routing information is 2 plus bits 0x1F00 of
1250 * the 16-bit value at an offset of 14 (shifted right
1251 * 8 - figure out which byte that is).
1253 cstate
->off_linktype
.constant_part
= 14;
1254 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1255 cstate
->off_nl
= 8; /* 802.2+SNAP */
1256 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1259 case DLT_PRISM_HEADER
:
1260 case DLT_IEEE802_11_RADIO_AVS
:
1261 case DLT_IEEE802_11_RADIO
:
1262 cstate
->off_linkhdr
.is_variable
= 1;
1263 /* Fall through, 802.11 doesn't have a variable link
1264 * prefix but is otherwise the same. */
1266 case DLT_IEEE802_11
:
1268 * 802.11 doesn't really have a link-level type field.
1269 * We set "off_linktype.constant_part" to the offset of
1272 * To check for Ethernet types, we assume that SSAP = SNAP
1273 * is being used and pick out the encapsulated Ethernet type.
1274 * XXX - should we generate code to check for SNAP?
1276 * We also handle variable-length radio headers here.
1277 * The Prism header is in theory variable-length, but in
1278 * practice it's always 144 bytes long. However, some
1279 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1280 * sometimes or always supply an AVS header, so we
1281 * have to check whether the radio header is a Prism
1282 * header or an AVS header, so, in practice, it's
1285 cstate
->off_linktype
.constant_part
= 24;
1286 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1287 cstate
->off_linkpl
.is_variable
= 1;
1288 cstate
->off_nl
= 8; /* 802.2+SNAP */
1289 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1294 * At the moment we treat PPI the same way that we treat
1295 * normal Radiotap encoded packets. The difference is in
1296 * the function that generates the code at the beginning
1297 * to compute the header length. Since this code generator
1298 * of PPI supports bare 802.11 encapsulation only (i.e.
1299 * the encapsulated DLT should be DLT_IEEE802_11) we
1300 * generate code to check for this too.
1302 cstate
->off_linktype
.constant_part
= 24;
1303 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1304 cstate
->off_linkpl
.is_variable
= 1;
1305 cstate
->off_linkhdr
.is_variable
= 1;
1306 cstate
->off_nl
= 8; /* 802.2+SNAP */
1307 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1310 case DLT_ATM_RFC1483
:
1311 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1313 * assume routed, non-ISO PDUs
1314 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1316 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1317 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1318 * latter would presumably be treated the way PPPoE
1319 * should be, so you can do "pppoe and udp port 2049"
1320 * or "pppoa and tcp port 80" and have it check for
1321 * PPPo{A,E} and a PPP protocol of IP and....
1323 cstate
->off_linktype
.constant_part
= 0;
1324 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1325 cstate
->off_nl
= 8; /* 802.2+SNAP */
1326 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1331 * Full Frontal ATM; you get AALn PDUs with an ATM
1335 cstate
->off_vpi
= SUNATM_VPI_POS
;
1336 cstate
->off_vci
= SUNATM_VCI_POS
;
1337 cstate
->off_proto
= PROTO_POS
;
1338 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1339 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1340 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1341 cstate
->off_nl
= 8; /* 802.2+SNAP */
1342 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1348 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1349 cstate
->off_linkpl
.constant_part
= 0;
1351 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1354 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1355 cstate
->off_linktype
.constant_part
= 14;
1356 cstate
->off_linkpl
.constant_part
= 16;
1358 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1361 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1362 cstate
->off_linktype
.constant_part
= 0;
1363 cstate
->off_linkpl
.constant_part
= 20;
1365 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1370 * LocalTalk does have a 1-byte type field in the LLAP header,
1371 * but really it just indicates whether there is a "short" or
1372 * "long" DDP packet following.
1374 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1375 cstate
->off_linkpl
.constant_part
= 0;
1377 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1380 case DLT_IP_OVER_FC
:
1382 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1383 * link-level type field. We set "off_linktype" to the
1384 * offset of the LLC header.
1386 * To check for Ethernet types, we assume that SSAP = SNAP
1387 * is being used and pick out the encapsulated Ethernet type.
1388 * XXX - should we generate code to check for SNAP? RFC
1389 * 2625 says SNAP should be used.
1391 cstate
->off_linktype
.constant_part
= 16;
1392 cstate
->off_linkpl
.constant_part
= 16;
1393 cstate
->off_nl
= 8; /* 802.2+SNAP */
1394 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1399 * XXX - we should set this to handle SNAP-encapsulated
1400 * frames (NLPID of 0x80).
1402 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1403 cstate
->off_linkpl
.constant_part
= 0;
1405 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1409 * the only BPF-interesting FRF.16 frames are non-control frames;
1410 * Frame Relay has a variable length link-layer
1411 * so lets start with offset 4 for now and increments later on (FIXME);
1414 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1415 cstate
->off_linkpl
.constant_part
= 0;
1417 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1420 case DLT_APPLE_IP_OVER_IEEE1394
:
1421 cstate
->off_linktype
.constant_part
= 16;
1422 cstate
->off_linkpl
.constant_part
= 18;
1424 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1427 case DLT_SYMANTEC_FIREWALL
:
1428 cstate
->off_linktype
.constant_part
= 6;
1429 cstate
->off_linkpl
.constant_part
= 44;
1430 cstate
->off_nl
= 0; /* Ethernet II */
1431 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1434 #ifdef HAVE_NET_PFVAR_H
1436 cstate
->off_linktype
.constant_part
= 0;
1437 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1439 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1443 case DLT_JUNIPER_MFR
:
1444 case DLT_JUNIPER_MLFR
:
1445 case DLT_JUNIPER_MLPPP
:
1446 case DLT_JUNIPER_PPP
:
1447 case DLT_JUNIPER_CHDLC
:
1448 case DLT_JUNIPER_FRELAY
:
1449 cstate
->off_linktype
.constant_part
= 4;
1450 cstate
->off_linkpl
.constant_part
= 4;
1452 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1455 case DLT_JUNIPER_ATM1
:
1456 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1457 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1459 cstate
->off_nl_nosnap
= 10;
1462 case DLT_JUNIPER_ATM2
:
1463 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1464 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1466 cstate
->off_nl_nosnap
= 10;
1469 /* frames captured on a Juniper PPPoE service PIC
1470 * contain raw ethernet frames */
1471 case DLT_JUNIPER_PPPOE
:
1472 case DLT_JUNIPER_ETHER
:
1473 cstate
->off_linkpl
.constant_part
= 14;
1474 cstate
->off_linktype
.constant_part
= 16;
1475 cstate
->off_nl
= 18; /* Ethernet II */
1476 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1479 case DLT_JUNIPER_PPPOE_ATM
:
1480 cstate
->off_linktype
.constant_part
= 4;
1481 cstate
->off_linkpl
.constant_part
= 6;
1483 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1486 case DLT_JUNIPER_GGSN
:
1487 cstate
->off_linktype
.constant_part
= 6;
1488 cstate
->off_linkpl
.constant_part
= 12;
1490 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1493 case DLT_JUNIPER_ES
:
1494 cstate
->off_linktype
.constant_part
= 6;
1495 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1496 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1497 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1500 case DLT_JUNIPER_MONITOR
:
1501 cstate
->off_linktype
.constant_part
= 12;
1502 cstate
->off_linkpl
.constant_part
= 12;
1503 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1504 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1507 case DLT_BACNET_MS_TP
:
1508 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1509 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1510 cstate
->off_nl
= OFFSET_NOT_SET
;
1511 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1514 case DLT_JUNIPER_SERVICES
:
1515 cstate
->off_linktype
.constant_part
= 12;
1516 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1517 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1518 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1521 case DLT_JUNIPER_VP
:
1522 cstate
->off_linktype
.constant_part
= 18;
1523 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1524 cstate
->off_nl
= OFFSET_NOT_SET
;
1525 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1528 case DLT_JUNIPER_ST
:
1529 cstate
->off_linktype
.constant_part
= 18;
1530 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1531 cstate
->off_nl
= OFFSET_NOT_SET
;
1532 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1535 case DLT_JUNIPER_ISM
:
1536 cstate
->off_linktype
.constant_part
= 8;
1537 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1538 cstate
->off_nl
= OFFSET_NOT_SET
;
1539 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1542 case DLT_JUNIPER_VS
:
1543 case DLT_JUNIPER_SRX_E2E
:
1544 case DLT_JUNIPER_FIBRECHANNEL
:
1545 case DLT_JUNIPER_ATM_CEMIC
:
1546 cstate
->off_linktype
.constant_part
= 8;
1547 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1548 cstate
->off_nl
= OFFSET_NOT_SET
;
1549 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1554 cstate
->off_li_hsl
= 4;
1555 cstate
->off_sio
= 3;
1556 cstate
->off_opc
= 4;
1557 cstate
->off_dpc
= 4;
1558 cstate
->off_sls
= 7;
1559 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1560 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1561 cstate
->off_nl
= OFFSET_NOT_SET
;
1562 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1565 case DLT_MTP2_WITH_PHDR
:
1567 cstate
->off_li_hsl
= 8;
1568 cstate
->off_sio
= 7;
1569 cstate
->off_opc
= 8;
1570 cstate
->off_dpc
= 8;
1571 cstate
->off_sls
= 11;
1572 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1573 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1574 cstate
->off_nl
= OFFSET_NOT_SET
;
1575 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1579 cstate
->off_li
= 22;
1580 cstate
->off_li_hsl
= 24;
1581 cstate
->off_sio
= 23;
1582 cstate
->off_opc
= 24;
1583 cstate
->off_dpc
= 24;
1584 cstate
->off_sls
= 27;
1585 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1586 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1587 cstate
->off_nl
= OFFSET_NOT_SET
;
1588 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1592 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1593 cstate
->off_linkpl
.constant_part
= 4;
1595 cstate
->off_nl_nosnap
= 0;
1600 * Currently, only raw "link[N:M]" filtering is supported.
1602 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1603 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1604 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1605 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1609 cstate
->off_linktype
.constant_part
= 1;
1610 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1612 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1615 case DLT_NETANALYZER
:
1616 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1617 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1618 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1619 cstate
->off_nl
= 0; /* Ethernet II */
1620 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1623 case DLT_NETANALYZER_TRANSPARENT
:
1624 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1625 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1626 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1627 cstate
->off_nl
= 0; /* Ethernet II */
1628 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1633 * For values in the range in which we've assigned new
1634 * DLT_ values, only raw "link[N:M]" filtering is supported.
1636 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1637 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1638 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1639 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1640 cstate
->off_nl
= OFFSET_NOT_SET
;
1641 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1643 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1648 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1652 * Load a value relative to the specified absolute offset.
1654 static struct slist
*
1655 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1656 u_int offset
, u_int size
)
1658 struct slist
*s
, *s2
;
1660 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1663 * If "s" is non-null, it has code to arrange that the X register
1664 * contains the variable part of the absolute offset, so we
1665 * generate a load relative to that, with an offset of
1666 * abs_offset->constant_part + offset.
1668 * Otherwise, we can do an absolute load with an offset of
1669 * abs_offset->constant_part + offset.
1673 * "s" points to a list of statements that puts the
1674 * variable part of the absolute offset into the X register.
1675 * Do an indirect load, to use the X register as an offset.
1677 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1678 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1682 * There is no variable part of the absolute offset, so
1683 * just do an absolute load.
1685 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1686 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1692 * Load a value relative to the beginning of the specified header.
1694 static struct slist
*
1695 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1698 struct slist
*s
, *s2
;
1703 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1708 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1711 case OR_PREVLINKHDR
:
1712 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1716 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1719 case OR_PREVMPLSHDR
:
1720 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1724 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1727 case OR_LINKPL_NOSNAP
:
1728 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1732 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1737 * Load the X register with the length of the IPv4 header
1738 * (plus the offset of the link-layer header, if it's
1739 * preceded by a variable-length header such as a radio
1740 * header), in bytes.
1742 s
= gen_loadx_iphdrlen(cstate
);
1745 * Load the item at {offset of the link-layer payload} +
1746 * {offset, relative to the start of the link-layer
1747 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1748 * {specified offset}.
1750 * If the offset of the link-layer payload is variable,
1751 * the variable part of that offset is included in the
1752 * value in the X register, and we include the constant
1753 * part in the offset of the load.
1755 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1756 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1761 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1772 * Generate code to load into the X register the sum of the length of
1773 * the IPv4 header and the variable part of the offset of the link-layer
1776 static struct slist
*
1777 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1779 struct slist
*s
, *s2
;
1781 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1784 * The offset of the link-layer payload has a variable
1785 * part. "s" points to a list of statements that put
1786 * the variable part of that offset into the X register.
1788 * The 4*([k]&0xf) addressing mode can't be used, as we
1789 * don't have a constant offset, so we have to load the
1790 * value in question into the A register and add to it
1791 * the value from the X register.
1793 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1794 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1796 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1799 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1804 * The A register now contains the length of the IP header.
1805 * We need to add to it the variable part of the offset of
1806 * the link-layer payload, which is still in the X
1807 * register, and move the result into the X register.
1809 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1810 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1813 * The offset of the link-layer payload is a constant,
1814 * so no code was generated to load the (non-existent)
1815 * variable part of that offset.
1817 * This means we can use the 4*([k]&0xf) addressing
1818 * mode. Load the length of the IPv4 header, which
1819 * is at an offset of cstate->off_nl from the beginning of
1820 * the link-layer payload, and thus at an offset of
1821 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1822 * of the raw packet data, using that addressing mode.
1824 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1825 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1831 static struct block
*
1832 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1837 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1839 b
= new_block(cstate
, JMP(BPF_JEQ
));
1845 static inline struct block
*
1846 gen_true(compiler_state_t
*cstate
)
1848 return gen_uncond(cstate
, 1);
1851 static inline struct block
*
1852 gen_false(compiler_state_t
*cstate
)
1854 return gen_uncond(cstate
, 0);
1858 * Byte-swap a 32-bit number.
1859 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1860 * big-endian platforms.)
1862 #define SWAPLONG(y) \
1863 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1866 * Generate code to match a particular packet type.
1868 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1869 * value, if <= ETHERMTU. We use that to determine whether to
1870 * match the type/length field or to check the type/length field for
1871 * a value <= ETHERMTU to see whether it's a type field and then do
1872 * the appropriate test.
1874 static struct block
*
1875 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1877 struct block
*b0
, *b1
;
1883 case LLCSAP_NETBEUI
:
1885 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1886 * so we check the DSAP and SSAP.
1888 * LLCSAP_IP checks for IP-over-802.2, rather
1889 * than IP-over-Ethernet or IP-over-SNAP.
1891 * XXX - should we check both the DSAP and the
1892 * SSAP, like this, or should we check just the
1893 * DSAP, as we do for other types <= ETHERMTU
1894 * (i.e., other SAP values)?
1896 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1898 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1899 ((proto
<< 8) | proto
));
1907 * Ethernet_II frames, which are Ethernet
1908 * frames with a frame type of ETHERTYPE_IPX;
1910 * Ethernet_802.3 frames, which are 802.3
1911 * frames (i.e., the type/length field is
1912 * a length field, <= ETHERMTU, rather than
1913 * a type field) with the first two bytes
1914 * after the Ethernet/802.3 header being
1917 * Ethernet_802.2 frames, which are 802.3
1918 * frames with an 802.2 LLC header and
1919 * with the IPX LSAP as the DSAP in the LLC
1922 * Ethernet_SNAP frames, which are 802.3
1923 * frames with an LLC header and a SNAP
1924 * header and with an OUI of 0x000000
1925 * (encapsulated Ethernet) and a protocol
1926 * ID of ETHERTYPE_IPX in the SNAP header.
1928 * XXX - should we generate the same code both
1929 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1933 * This generates code to check both for the
1934 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1936 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1937 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1941 * Now we add code to check for SNAP frames with
1942 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1944 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1948 * Now we generate code to check for 802.3
1949 * frames in general.
1951 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1955 * Now add the check for 802.3 frames before the
1956 * check for Ethernet_802.2 and Ethernet_802.3,
1957 * as those checks should only be done on 802.3
1958 * frames, not on Ethernet frames.
1963 * Now add the check for Ethernet_II frames, and
1964 * do that before checking for the other frame
1967 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1971 case ETHERTYPE_ATALK
:
1972 case ETHERTYPE_AARP
:
1974 * EtherTalk (AppleTalk protocols on Ethernet link
1975 * layer) may use 802.2 encapsulation.
1979 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1980 * we check for an Ethernet type field less than
1981 * 1500, which means it's an 802.3 length field.
1983 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1987 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1988 * SNAP packets with an organization code of
1989 * 0x080007 (Apple, for Appletalk) and a protocol
1990 * type of ETHERTYPE_ATALK (Appletalk).
1992 * 802.2-encapsulated ETHERTYPE_AARP packets are
1993 * SNAP packets with an organization code of
1994 * 0x000000 (encapsulated Ethernet) and a protocol
1995 * type of ETHERTYPE_AARP (Appletalk ARP).
1997 if (proto
== ETHERTYPE_ATALK
)
1998 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
1999 else /* proto == ETHERTYPE_AARP */
2000 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2004 * Check for Ethernet encapsulation (Ethertalk
2005 * phase 1?); we just check for the Ethernet
2008 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2014 if (proto
<= ETHERMTU
) {
2016 * This is an LLC SAP value, so the frames
2017 * that match would be 802.2 frames.
2018 * Check that the frame is an 802.2 frame
2019 * (i.e., that the length/type field is
2020 * a length field, <= ETHERMTU) and
2021 * then check the DSAP.
2023 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2025 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2030 * This is an Ethernet type, so compare
2031 * the length/type field with it (if
2032 * the frame is an 802.2 frame, the length
2033 * field will be <= ETHERMTU, and, as
2034 * "proto" is > ETHERMTU, this test
2035 * will fail and the frame won't match,
2036 * which is what we want).
2038 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2044 static struct block
*
2045 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2048 * For DLT_NULL, the link-layer header is a 32-bit word
2049 * containing an AF_ value in *host* byte order, and for
2050 * DLT_ENC, the link-layer header begins with a 32-bit
2051 * word containing an AF_ value in host byte order.
2053 * In addition, if we're reading a saved capture file,
2054 * the host byte order in the capture may not be the
2055 * same as the host byte order on this machine.
2057 * For DLT_LOOP, the link-layer header is a 32-bit
2058 * word containing an AF_ value in *network* byte order.
2060 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2062 * The AF_ value is in host byte order, but the BPF
2063 * interpreter will convert it to network byte order.
2065 * If this is a save file, and it's from a machine
2066 * with the opposite byte order to ours, we byte-swap
2069 * Then we run it through "htonl()", and generate
2070 * code to compare against the result.
2072 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2073 proto
= SWAPLONG(proto
);
2074 proto
= htonl(proto
);
2076 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2080 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2081 * or IPv6 then we have an error.
2083 static struct block
*
2084 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2089 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2092 case ETHERTYPE_IPV6
:
2093 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2094 (bpf_int32
)IPH_AF_INET6
);
2101 return gen_false(cstate
);
2105 * Generate code to match a particular packet type.
2107 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2108 * value, if <= ETHERMTU. We use that to determine whether to
2109 * match the type field or to check the type field for the special
2110 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2112 static struct block
*
2113 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2115 struct block
*b0
, *b1
;
2121 case LLCSAP_NETBEUI
:
2123 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2124 * so we check the DSAP and SSAP.
2126 * LLCSAP_IP checks for IP-over-802.2, rather
2127 * than IP-over-Ethernet or IP-over-SNAP.
2129 * XXX - should we check both the DSAP and the
2130 * SSAP, like this, or should we check just the
2131 * DSAP, as we do for other types <= ETHERMTU
2132 * (i.e., other SAP values)?
2134 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2135 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2136 ((proto
<< 8) | proto
));
2142 * Ethernet_II frames, which are Ethernet
2143 * frames with a frame type of ETHERTYPE_IPX;
2145 * Ethernet_802.3 frames, which have a frame
2146 * type of LINUX_SLL_P_802_3;
2148 * Ethernet_802.2 frames, which are 802.3
2149 * frames with an 802.2 LLC header (i.e, have
2150 * a frame type of LINUX_SLL_P_802_2) and
2151 * with the IPX LSAP as the DSAP in the LLC
2154 * Ethernet_SNAP frames, which are 802.3
2155 * frames with an LLC header and a SNAP
2156 * header and with an OUI of 0x000000
2157 * (encapsulated Ethernet) and a protocol
2158 * ID of ETHERTYPE_IPX in the SNAP header.
2160 * First, do the checks on LINUX_SLL_P_802_2
2161 * frames; generate the check for either
2162 * Ethernet_802.2 or Ethernet_SNAP frames, and
2163 * then put a check for LINUX_SLL_P_802_2 frames
2166 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2167 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2169 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2173 * Now check for 802.3 frames and OR that with
2174 * the previous test.
2176 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2180 * Now add the check for Ethernet_II frames, and
2181 * do that before checking for the other frame
2184 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2188 case ETHERTYPE_ATALK
:
2189 case ETHERTYPE_AARP
:
2191 * EtherTalk (AppleTalk protocols on Ethernet link
2192 * layer) may use 802.2 encapsulation.
2196 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2197 * we check for the 802.2 protocol type in the
2198 * "Ethernet type" field.
2200 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2203 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2204 * SNAP packets with an organization code of
2205 * 0x080007 (Apple, for Appletalk) and a protocol
2206 * type of ETHERTYPE_ATALK (Appletalk).
2208 * 802.2-encapsulated ETHERTYPE_AARP packets are
2209 * SNAP packets with an organization code of
2210 * 0x000000 (encapsulated Ethernet) and a protocol
2211 * type of ETHERTYPE_AARP (Appletalk ARP).
2213 if (proto
== ETHERTYPE_ATALK
)
2214 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2215 else /* proto == ETHERTYPE_AARP */
2216 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2220 * Check for Ethernet encapsulation (Ethertalk
2221 * phase 1?); we just check for the Ethernet
2224 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2230 if (proto
<= ETHERMTU
) {
2232 * This is an LLC SAP value, so the frames
2233 * that match would be 802.2 frames.
2234 * Check for the 802.2 protocol type
2235 * in the "Ethernet type" field, and
2236 * then check the DSAP.
2238 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2239 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2245 * This is an Ethernet type, so compare
2246 * the length/type field with it (if
2247 * the frame is an 802.2 frame, the length
2248 * field will be <= ETHERMTU, and, as
2249 * "proto" is > ETHERMTU, this test
2250 * will fail and the frame won't match,
2251 * which is what we want).
2253 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2258 static struct slist
*
2259 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2261 struct slist
*s1
, *s2
;
2262 struct slist
*sjeq_avs_cookie
;
2263 struct slist
*sjcommon
;
2266 * This code is not compatible with the optimizer, as
2267 * we are generating jmp instructions within a normal
2268 * slist of instructions
2270 cstate
->no_optimize
= 1;
2273 * Generate code to load the length of the radio header into
2274 * the register assigned to hold that length, if one has been
2275 * assigned. (If one hasn't been assigned, no code we've
2276 * generated uses that prefix, so we don't need to generate any
2279 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2280 * or always use the AVS header rather than the Prism header.
2281 * We load a 4-byte big-endian value at the beginning of the
2282 * raw packet data, and see whether, when masked with 0xFFFFF000,
2283 * it's equal to 0x80211000. If so, that indicates that it's
2284 * an AVS header (the masked-out bits are the version number).
2285 * Otherwise, it's a Prism header.
2287 * XXX - the Prism header is also, in theory, variable-length,
2288 * but no known software generates headers that aren't 144
2291 if (cstate
->off_linkhdr
.reg
!= -1) {
2295 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2299 * AND it with 0xFFFFF000.
2301 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2302 s2
->s
.k
= 0xFFFFF000;
2306 * Compare with 0x80211000.
2308 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2309 sjeq_avs_cookie
->s
.k
= 0x80211000;
2310 sappend(s1
, sjeq_avs_cookie
);
2315 * The 4 bytes at an offset of 4 from the beginning of
2316 * the AVS header are the length of the AVS header.
2317 * That field is big-endian.
2319 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2322 sjeq_avs_cookie
->s
.jt
= s2
;
2325 * Now jump to the code to allocate a register
2326 * into which to save the header length and
2327 * store the length there. (The "jump always"
2328 * instruction needs to have the k field set;
2329 * it's added to the PC, so, as we're jumping
2330 * over a single instruction, it should be 1.)
2332 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2334 sappend(s1
, sjcommon
);
2337 * Now for the code that handles the Prism header.
2338 * Just load the length of the Prism header (144)
2339 * into the A register. Have the test for an AVS
2340 * header branch here if we don't have an AVS header.
2342 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2345 sjeq_avs_cookie
->s
.jf
= s2
;
2348 * Now allocate a register to hold that value and store
2349 * it. The code for the AVS header will jump here after
2350 * loading the length of the AVS header.
2352 s2
= new_stmt(cstate
, BPF_ST
);
2353 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2355 sjcommon
->s
.jf
= s2
;
2358 * Now move it into the X register.
2360 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2368 static struct slist
*
2369 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2371 struct slist
*s1
, *s2
;
2374 * Generate code to load the length of the AVS header into
2375 * the register assigned to hold that length, if one has been
2376 * assigned. (If one hasn't been assigned, no code we've
2377 * generated uses that prefix, so we don't need to generate any
2380 if (cstate
->off_linkhdr
.reg
!= -1) {
2382 * The 4 bytes at an offset of 4 from the beginning of
2383 * the AVS header are the length of the AVS header.
2384 * That field is big-endian.
2386 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2390 * Now allocate a register to hold that value and store
2393 s2
= new_stmt(cstate
, BPF_ST
);
2394 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2398 * Now move it into the X register.
2400 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2408 static struct slist
*
2409 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2411 struct slist
*s1
, *s2
;
2414 * Generate code to load the length of the radiotap header into
2415 * the register assigned to hold that length, if one has been
2416 * assigned. (If one hasn't been assigned, no code we've
2417 * generated uses that prefix, so we don't need to generate any
2420 if (cstate
->off_linkhdr
.reg
!= -1) {
2422 * The 2 bytes at offsets of 2 and 3 from the beginning
2423 * of the radiotap header are the length of the radiotap
2424 * header; unfortunately, it's little-endian, so we have
2425 * to load it a byte at a time and construct the value.
2429 * Load the high-order byte, at an offset of 3, shift it
2430 * left a byte, and put the result in the X register.
2432 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2434 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2437 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2441 * Load the next byte, at an offset of 2, and OR the
2442 * value from the X register into it.
2444 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2447 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2451 * Now allocate a register to hold that value and store
2454 s2
= new_stmt(cstate
, BPF_ST
);
2455 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2459 * Now move it into the X register.
2461 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2470 * At the moment we treat PPI as normal Radiotap encoded
2471 * packets. The difference is in the function that generates
2472 * the code at the beginning to compute the header length.
2473 * Since this code generator of PPI supports bare 802.11
2474 * encapsulation only (i.e. the encapsulated DLT should be
2475 * DLT_IEEE802_11) we generate code to check for this too;
2476 * that's done in finish_parse().
2478 static struct slist
*
2479 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2481 struct slist
*s1
, *s2
;
2484 * Generate code to load the length of the radiotap header
2485 * into the register assigned to hold that length, if one has
2488 if (cstate
->off_linkhdr
.reg
!= -1) {
2490 * The 2 bytes at offsets of 2 and 3 from the beginning
2491 * of the radiotap header are the length of the radiotap
2492 * header; unfortunately, it's little-endian, so we have
2493 * to load it a byte at a time and construct the value.
2497 * Load the high-order byte, at an offset of 3, shift it
2498 * left a byte, and put the result in the X register.
2500 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2502 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2505 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2509 * Load the next byte, at an offset of 2, and OR the
2510 * value from the X register into it.
2512 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2515 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2519 * Now allocate a register to hold that value and store
2522 s2
= new_stmt(cstate
, BPF_ST
);
2523 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2527 * Now move it into the X register.
2529 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2538 * Load a value relative to the beginning of the link-layer header after the 802.11
2539 * header, i.e. LLC_SNAP.
2540 * The link-layer header doesn't necessarily begin at the beginning
2541 * of the packet data; there might be a variable-length prefix containing
2542 * radio information.
2544 static struct slist
*
2545 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2548 struct slist
*sjset_data_frame_1
;
2549 struct slist
*sjset_data_frame_2
;
2550 struct slist
*sjset_qos
;
2551 struct slist
*sjset_radiotap_flags_present
;
2552 struct slist
*sjset_radiotap_ext_present
;
2553 struct slist
*sjset_radiotap_tsft_present
;
2554 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2555 struct slist
*s_roundup
;
2557 if (cstate
->off_linkpl
.reg
== -1) {
2559 * No register has been assigned to the offset of
2560 * the link-layer payload, which means nobody needs
2561 * it; don't bother computing it - just return
2562 * what we already have.
2568 * This code is not compatible with the optimizer, as
2569 * we are generating jmp instructions within a normal
2570 * slist of instructions
2572 cstate
->no_optimize
= 1;
2575 * If "s" is non-null, it has code to arrange that the X register
2576 * contains the length of the prefix preceding the link-layer
2579 * Otherwise, the length of the prefix preceding the link-layer
2580 * header is "off_outermostlinkhdr.constant_part".
2584 * There is no variable-length header preceding the
2585 * link-layer header.
2587 * Load the length of the fixed-length prefix preceding
2588 * the link-layer header (if any) into the X register,
2589 * and store it in the cstate->off_linkpl.reg register.
2590 * That length is off_outermostlinkhdr.constant_part.
2592 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2593 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2597 * The X register contains the offset of the beginning of the
2598 * link-layer header; add 24, which is the minimum length
2599 * of the MAC header for a data frame, to that, and store it
2600 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2601 * which is at the offset in the X register, with an indexed load.
2603 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2605 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2608 s2
= new_stmt(cstate
, BPF_ST
);
2609 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2612 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2617 * Check the Frame Control field to see if this is a data frame;
2618 * a data frame has the 0x08 bit (b3) in that field set and the
2619 * 0x04 bit (b2) clear.
2621 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2622 sjset_data_frame_1
->s
.k
= 0x08;
2623 sappend(s
, sjset_data_frame_1
);
2626 * If b3 is set, test b2, otherwise go to the first statement of
2627 * the rest of the program.
2629 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2630 sjset_data_frame_2
->s
.k
= 0x04;
2631 sappend(s
, sjset_data_frame_2
);
2632 sjset_data_frame_1
->s
.jf
= snext
;
2635 * If b2 is not set, this is a data frame; test the QoS bit.
2636 * Otherwise, go to the first statement of the rest of the
2639 sjset_data_frame_2
->s
.jt
= snext
;
2640 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2641 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2642 sappend(s
, sjset_qos
);
2645 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2647 * Otherwise, go to the first statement of the rest of the
2650 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2651 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2653 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2656 s2
= new_stmt(cstate
, BPF_ST
);
2657 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2661 * If we have a radiotap header, look at it to see whether
2662 * there's Atheros padding between the MAC-layer header
2665 * Note: all of the fields in the radiotap header are
2666 * little-endian, so we byte-swap all of the values
2667 * we test against, as they will be loaded as big-endian
2670 * XXX - in the general case, we would have to scan through
2671 * *all* the presence bits, if there's more than one word of
2672 * presence bits. That would require a loop, meaning that
2673 * we wouldn't be able to run the filter in the kernel.
2675 * We assume here that the Atheros adapters that insert the
2676 * annoying padding don't have multiple antennae and therefore
2677 * do not generate radiotap headers with multiple presence words.
2679 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2681 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2682 * in the first presence flag word?
2684 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2688 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2689 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2690 sappend(s
, sjset_radiotap_flags_present
);
2693 * If not, skip all of this.
2695 sjset_radiotap_flags_present
->s
.jf
= snext
;
2698 * Otherwise, is the "extension" bit set in that word?
2700 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2701 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2702 sappend(s
, sjset_radiotap_ext_present
);
2703 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2706 * If so, skip all of this.
2708 sjset_radiotap_ext_present
->s
.jt
= snext
;
2711 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2713 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2714 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2715 sappend(s
, sjset_radiotap_tsft_present
);
2716 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2719 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2720 * at an offset of 16 from the beginning of the raw packet
2721 * data (8 bytes for the radiotap header and 8 bytes for
2724 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2727 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2730 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2732 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2733 sjset_tsft_datapad
->s
.k
= 0x20;
2734 sappend(s
, sjset_tsft_datapad
);
2737 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2738 * at an offset of 8 from the beginning of the raw packet
2739 * data (8 bytes for the radiotap header).
2741 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2744 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2747 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2749 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2750 sjset_notsft_datapad
->s
.k
= 0x20;
2751 sappend(s
, sjset_notsft_datapad
);
2754 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2755 * set, round the length of the 802.11 header to
2756 * a multiple of 4. Do that by adding 3 and then
2757 * dividing by and multiplying by 4, which we do by
2760 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2761 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2762 sappend(s
, s_roundup
);
2763 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2766 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2769 s2
= new_stmt(cstate
, BPF_ST
);
2770 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2773 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2774 sjset_tsft_datapad
->s
.jf
= snext
;
2775 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2776 sjset_notsft_datapad
->s
.jf
= snext
;
2778 sjset_qos
->s
.jf
= snext
;
2784 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2788 /* There is an implicit dependency between the link
2789 * payload and link header since the payload computation
2790 * includes the variable part of the header. Therefore,
2791 * if nobody else has allocated a register for the link
2792 * header and we need it, do it now. */
2793 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2794 cstate
->off_linkhdr
.reg
== -1)
2795 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2798 * For link-layer types that have a variable-length header
2799 * preceding the link-layer header, generate code to load
2800 * the offset of the link-layer header into the register
2801 * assigned to that offset, if any.
2803 * XXX - this, and the next switch statement, won't handle
2804 * encapsulation of 802.11 or 802.11+radio information in
2805 * some other protocol stack. That's significantly more
2808 switch (cstate
->outermostlinktype
) {
2810 case DLT_PRISM_HEADER
:
2811 s
= gen_load_prism_llprefixlen(cstate
);
2814 case DLT_IEEE802_11_RADIO_AVS
:
2815 s
= gen_load_avs_llprefixlen(cstate
);
2818 case DLT_IEEE802_11_RADIO
:
2819 s
= gen_load_radiotap_llprefixlen(cstate
);
2823 s
= gen_load_ppi_llprefixlen(cstate
);
2832 * For link-layer types that have a variable-length link-layer
2833 * header, generate code to load the offset of the link-layer
2834 * payload into the register assigned to that offset, if any.
2836 switch (cstate
->outermostlinktype
) {
2838 case DLT_IEEE802_11
:
2839 case DLT_PRISM_HEADER
:
2840 case DLT_IEEE802_11_RADIO_AVS
:
2841 case DLT_IEEE802_11_RADIO
:
2843 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2848 * If there there is no initialization yet and we need variable
2849 * length offsets for VLAN, initialize them to zero
2851 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2854 if (cstate
->off_linkpl
.reg
== -1)
2855 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2856 if (cstate
->off_linktype
.reg
== -1)
2857 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2859 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2861 s2
= new_stmt(cstate
, BPF_ST
);
2862 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2864 s2
= new_stmt(cstate
, BPF_ST
);
2865 s2
->s
.k
= cstate
->off_linktype
.reg
;
2870 * If we have any offset-loading code, append all the
2871 * existing statements in the block to those statements,
2872 * and make the resulting list the list of statements
2876 sappend(s
, b
->stmts
);
2881 static struct block
*
2882 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2884 struct slist
*s_load_dlt
;
2887 if (cstate
->linktype
== DLT_PPI
)
2889 /* Create the statements that check for the DLT
2891 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2892 s_load_dlt
->s
.k
= 4;
2894 b
= new_block(cstate
, JMP(BPF_JEQ
));
2896 b
->stmts
= s_load_dlt
;
2897 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2908 * Take an absolute offset, and:
2910 * if it has no variable part, return NULL;
2912 * if it has a variable part, generate code to load the register
2913 * containing that variable part into the X register, returning
2914 * a pointer to that code - if no register for that offset has
2915 * been allocated, allocate it first.
2917 * (The code to set that register will be generated later, but will
2918 * be placed earlier in the code sequence.)
2920 static struct slist
*
2921 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2925 if (off
->is_variable
) {
2926 if (off
->reg
== -1) {
2928 * We haven't yet assigned a register for the
2929 * variable part of the offset of the link-layer
2930 * header; allocate one.
2932 off
->reg
= alloc_reg(cstate
);
2936 * Load the register containing the variable part of the
2937 * offset of the link-layer header into the X register.
2939 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2944 * That offset isn't variable, there's no variable part,
2945 * so we don't need to generate any code.
2952 * Map an Ethernet type to the equivalent PPP type.
2955 ethertype_to_ppptype(int proto
)
2963 case ETHERTYPE_IPV6
:
2971 case ETHERTYPE_ATALK
:
2985 * I'm assuming the "Bridging PDU"s that go
2986 * over PPP are Spanning Tree Protocol
3000 * Generate any tests that, for encapsulation of a link-layer packet
3001 * inside another protocol stack, need to be done to check for those
3002 * link-layer packets (and that haven't already been done by a check
3003 * for that encapsulation).
3005 static struct block
*
3006 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3010 if (cstate
->is_geneve
)
3011 return gen_geneve_ll_check(cstate
);
3013 switch (cstate
->prevlinktype
) {
3017 * This is LANE-encapsulated Ethernet; check that the LANE
3018 * packet doesn't begin with an LE Control marker, i.e.
3019 * that it's data, not a control message.
3021 * (We've already generated a test for LANE.)
3023 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3029 * No such tests are necessary.
3037 * The three different values we should check for when checking for an
3038 * IPv6 packet with DLT_NULL.
3040 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3041 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3042 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3045 * Generate code to match a particular packet type by matching the
3046 * link-layer type field or fields in the 802.2 LLC header.
3048 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3049 * value, if <= ETHERMTU.
3051 static struct block
*
3052 gen_linktype(compiler_state_t
*cstate
, int proto
)
3054 struct block
*b0
, *b1
, *b2
;
3055 const char *description
;
3057 /* are we checking MPLS-encapsulated packets? */
3058 if (cstate
->label_stack_depth
> 0) {
3062 /* FIXME add other L3 proto IDs */
3063 return gen_mpls_linktype(cstate
, Q_IP
);
3065 case ETHERTYPE_IPV6
:
3067 /* FIXME add other L3 proto IDs */
3068 return gen_mpls_linktype(cstate
, Q_IPV6
);
3071 bpf_error(cstate
, "unsupported protocol over mpls");
3076 switch (cstate
->linktype
) {
3079 case DLT_NETANALYZER
:
3080 case DLT_NETANALYZER_TRANSPARENT
:
3081 /* Geneve has an EtherType regardless of whether there is an
3083 if (!cstate
->is_geneve
)
3084 b0
= gen_prevlinkhdr_check(cstate
);
3088 b1
= gen_ether_linktype(cstate
, proto
);
3099 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3103 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3109 case DLT_IEEE802_11
:
3110 case DLT_PRISM_HEADER
:
3111 case DLT_IEEE802_11_RADIO_AVS
:
3112 case DLT_IEEE802_11_RADIO
:
3115 * Check that we have a data frame.
3117 b0
= gen_check_802_11_data_frame(cstate
);
3120 * Now check for the specified link-layer type.
3122 b1
= gen_llc_linktype(cstate
, proto
);
3130 * XXX - check for LLC frames.
3132 return gen_llc_linktype(cstate
, proto
);
3138 * XXX - check for LLC PDUs, as per IEEE 802.5.
3140 return gen_llc_linktype(cstate
, proto
);
3144 case DLT_ATM_RFC1483
:
3146 case DLT_IP_OVER_FC
:
3147 return gen_llc_linktype(cstate
, proto
);
3153 * Check for an LLC-encapsulated version of this protocol;
3154 * if we were checking for LANE, linktype would no longer
3157 * Check for LLC encapsulation and then check the protocol.
3159 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3160 b1
= gen_llc_linktype(cstate
, proto
);
3167 return gen_linux_sll_linktype(cstate
, proto
);
3172 case DLT_SLIP_BSDOS
:
3175 * These types don't provide any type field; packets
3176 * are always IPv4 or IPv6.
3178 * XXX - for IPv4, check for a version number of 4, and,
3179 * for IPv6, check for a version number of 6?
3184 /* Check for a version number of 4. */
3185 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3187 case ETHERTYPE_IPV6
:
3188 /* Check for a version number of 6. */
3189 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3192 return gen_false(cstate
); /* always false */
3199 * Raw IPv4, so no type field.
3201 if (proto
== ETHERTYPE_IP
)
3202 return gen_true(cstate
); /* always true */
3204 /* Checking for something other than IPv4; always false */
3205 return gen_false(cstate
);
3211 * Raw IPv6, so no type field.
3213 if (proto
== ETHERTYPE_IPV6
)
3214 return gen_true(cstate
); /* always true */
3216 /* Checking for something other than IPv6; always false */
3217 return gen_false(cstate
);
3223 case DLT_PPP_SERIAL
:
3226 * We use Ethernet protocol types inside libpcap;
3227 * map them to the corresponding PPP protocol types.
3229 proto
= ethertype_to_ppptype(proto
);
3230 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3236 * We use Ethernet protocol types inside libpcap;
3237 * map them to the corresponding PPP protocol types.
3243 * Also check for Van Jacobson-compressed IP.
3244 * XXX - do this for other forms of PPP?
3246 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3247 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3249 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3254 proto
= ethertype_to_ppptype(proto
);
3255 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3267 return (gen_loopback_linktype(cstate
, AF_INET
));
3269 case ETHERTYPE_IPV6
:
3271 * AF_ values may, unfortunately, be platform-
3272 * dependent; AF_INET isn't, because everybody
3273 * used 4.2BSD's value, but AF_INET6 is, because
3274 * 4.2BSD didn't have a value for it (given that
3275 * IPv6 didn't exist back in the early 1980's),
3276 * and they all picked their own values.
3278 * This means that, if we're reading from a
3279 * savefile, we need to check for all the
3282 * If we're doing a live capture, we only need
3283 * to check for this platform's value; however,
3284 * Npcap uses 24, which isn't Windows's AF_INET6
3285 * value. (Given the multiple different values,
3286 * programs that read pcap files shouldn't be
3287 * checking for their platform's AF_INET6 value
3288 * anyway, they should check for all of the
3289 * possible values. and they might as well do
3290 * that even for live captures.)
3292 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3294 * Savefile - check for all three
3295 * possible IPv6 values.
3297 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3298 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3300 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3305 * Live capture, so we only need to
3306 * check for the value used on this
3311 * Npcap doesn't use Windows's AF_INET6,
3312 * as that collides with AF_IPX on
3313 * some BSDs (both have the value 23).
3314 * Instead, it uses 24.
3316 return (gen_loopback_linktype(cstate
, 24));
3319 return (gen_loopback_linktype(cstate
, AF_INET6
));
3320 #else /* AF_INET6 */
3322 * I guess this platform doesn't support
3323 * IPv6, so we just reject all packets.
3325 return gen_false(cstate
);
3326 #endif /* AF_INET6 */
3332 * Not a type on which we support filtering.
3333 * XXX - support those that have AF_ values
3334 * #defined on this platform, at least?
3336 return gen_false(cstate
);
3339 #ifdef HAVE_NET_PFVAR_H
3342 * af field is host byte order in contrast to the rest of
3345 if (proto
== ETHERTYPE_IP
)
3346 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3347 BPF_B
, (bpf_int32
)AF_INET
));
3348 else if (proto
== ETHERTYPE_IPV6
)
3349 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3350 BPF_B
, (bpf_int32
)AF_INET6
));
3352 return gen_false(cstate
);
3355 #endif /* HAVE_NET_PFVAR_H */
3358 case DLT_ARCNET_LINUX
:
3360 * XXX should we check for first fragment if the protocol
3366 return gen_false(cstate
);
3368 case ETHERTYPE_IPV6
:
3369 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3370 (bpf_int32
)ARCTYPE_INET6
));
3373 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3374 (bpf_int32
)ARCTYPE_IP
);
3375 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3376 (bpf_int32
)ARCTYPE_IP_OLD
);
3381 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3382 (bpf_int32
)ARCTYPE_ARP
);
3383 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3384 (bpf_int32
)ARCTYPE_ARP_OLD
);
3388 case ETHERTYPE_REVARP
:
3389 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3390 (bpf_int32
)ARCTYPE_REVARP
));
3392 case ETHERTYPE_ATALK
:
3393 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3394 (bpf_int32
)ARCTYPE_ATALK
));
3401 case ETHERTYPE_ATALK
:
3402 return gen_true(cstate
);
3404 return gen_false(cstate
);
3411 * XXX - assumes a 2-byte Frame Relay header with
3412 * DLCI and flags. What if the address is longer?
3418 * Check for the special NLPID for IP.
3420 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3422 case ETHERTYPE_IPV6
:
3424 * Check for the special NLPID for IPv6.
3426 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3430 * Check for several OSI protocols.
3432 * Frame Relay packets typically have an OSI
3433 * NLPID at the beginning; we check for each
3436 * What we check for is the NLPID and a frame
3437 * control field of UI, i.e. 0x03 followed
3440 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3441 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3442 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3448 return gen_false(cstate
);
3454 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3456 case DLT_JUNIPER_MFR
:
3457 case DLT_JUNIPER_MLFR
:
3458 case DLT_JUNIPER_MLPPP
:
3459 case DLT_JUNIPER_ATM1
:
3460 case DLT_JUNIPER_ATM2
:
3461 case DLT_JUNIPER_PPPOE
:
3462 case DLT_JUNIPER_PPPOE_ATM
:
3463 case DLT_JUNIPER_GGSN
:
3464 case DLT_JUNIPER_ES
:
3465 case DLT_JUNIPER_MONITOR
:
3466 case DLT_JUNIPER_SERVICES
:
3467 case DLT_JUNIPER_ETHER
:
3468 case DLT_JUNIPER_PPP
:
3469 case DLT_JUNIPER_FRELAY
:
3470 case DLT_JUNIPER_CHDLC
:
3471 case DLT_JUNIPER_VP
:
3472 case DLT_JUNIPER_ST
:
3473 case DLT_JUNIPER_ISM
:
3474 case DLT_JUNIPER_VS
:
3475 case DLT_JUNIPER_SRX_E2E
:
3476 case DLT_JUNIPER_FIBRECHANNEL
:
3477 case DLT_JUNIPER_ATM_CEMIC
:
3479 /* just lets verify the magic number for now -
3480 * on ATM we may have up to 6 different encapsulations on the wire
3481 * and need a lot of heuristics to figure out that the payload
3484 * FIXME encapsulation specific BPF_ filters
3486 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3488 case DLT_BACNET_MS_TP
:
3489 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3492 return gen_ipnet_linktype(cstate
, proto
);
3494 case DLT_LINUX_IRDA
:
3495 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3498 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3501 case DLT_MTP2_WITH_PHDR
:
3502 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3505 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3508 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3510 case DLT_LINUX_LAPD
:
3511 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3513 case DLT_USB_FREEBSD
:
3515 case DLT_USB_LINUX_MMAPPED
:
3517 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3519 case DLT_BLUETOOTH_HCI_H4
:
3520 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3521 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3524 case DLT_CAN_SOCKETCAN
:
3525 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3527 case DLT_IEEE802_15_4
:
3528 case DLT_IEEE802_15_4_LINUX
:
3529 case DLT_IEEE802_15_4_NONASK_PHY
:
3530 case DLT_IEEE802_15_4_NOFCS
:
3531 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3533 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3534 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3537 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3540 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3543 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3546 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3549 /* Using the fixed-size NFLOG header it is possible to tell only
3550 * the address family of the packet, other meaningful data is
3551 * either missing or behind TLVs.
3553 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3557 * Does this link-layer header type have a field
3558 * indicating the type of the next protocol? If
3559 * so, off_linktype.constant_part will be the offset of that
3560 * field in the packet; if not, it will be OFFSET_NOT_SET.
3562 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3564 * Yes; assume it's an Ethernet type. (If
3565 * it's not, it needs to be handled specially
3568 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3571 * No; report an error.
3573 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3574 if (description
!= NULL
) {
3575 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3578 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3587 * Check for an LLC SNAP packet with a given organization code and
3588 * protocol type; we check the entire contents of the 802.2 LLC and
3589 * snap headers, checking for DSAP and SSAP of SNAP and a control
3590 * field of 0x03 in the LLC header, and for the specified organization
3591 * code and protocol type in the SNAP header.
3593 static struct block
*
3594 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3596 u_char snapblock
[8];
3598 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3599 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3600 snapblock
[2] = 0x03; /* control = UI */
3601 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3602 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3603 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3604 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3605 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3606 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3610 * Generate code to match frames with an LLC header.
3613 gen_llc(compiler_state_t
*cstate
)
3615 struct block
*b0
, *b1
;
3617 switch (cstate
->linktype
) {
3621 * We check for an Ethernet type field less than
3622 * 1500, which means it's an 802.3 length field.
3624 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3628 * Now check for the purported DSAP and SSAP not being
3629 * 0xFF, to rule out NetWare-over-802.3.
3631 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3638 * We check for LLC traffic.
3640 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3643 case DLT_IEEE802
: /* Token Ring */
3645 * XXX - check for LLC frames.
3647 return gen_true(cstate
);
3651 * XXX - check for LLC frames.
3653 return gen_true(cstate
);
3655 case DLT_ATM_RFC1483
:
3657 * For LLC encapsulation, these are defined to have an
3660 * For VC encapsulation, they don't, but there's no
3661 * way to check for that; the protocol used on the VC
3662 * is negotiated out of band.
3664 return gen_true(cstate
);
3666 case DLT_IEEE802_11
:
3667 case DLT_PRISM_HEADER
:
3668 case DLT_IEEE802_11_RADIO
:
3669 case DLT_IEEE802_11_RADIO_AVS
:
3672 * Check that we have a data frame.
3674 b0
= gen_check_802_11_data_frame(cstate
);
3678 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3684 gen_llc_i(compiler_state_t
*cstate
)
3686 struct block
*b0
, *b1
;
3690 * Check whether this is an LLC frame.
3692 b0
= gen_llc(cstate
);
3695 * Load the control byte and test the low-order bit; it must
3696 * be clear for I frames.
3698 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3699 b1
= new_block(cstate
, JMP(BPF_JSET
));
3708 gen_llc_s(compiler_state_t
*cstate
)
3710 struct block
*b0
, *b1
;
3713 * Check whether this is an LLC frame.
3715 b0
= gen_llc(cstate
);
3718 * Now compare the low-order 2 bit of the control byte against
3719 * the appropriate value for S frames.
3721 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3727 gen_llc_u(compiler_state_t
*cstate
)
3729 struct block
*b0
, *b1
;
3732 * Check whether this is an LLC frame.
3734 b0
= gen_llc(cstate
);
3737 * Now compare the low-order 2 bit of the control byte against
3738 * the appropriate value for U frames.
3740 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3746 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3748 struct block
*b0
, *b1
;
3751 * Check whether this is an LLC frame.
3753 b0
= gen_llc(cstate
);
3756 * Now check for an S frame with the appropriate type.
3758 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3764 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3766 struct block
*b0
, *b1
;
3769 * Check whether this is an LLC frame.
3771 b0
= gen_llc(cstate
);
3774 * Now check for a U frame with the appropriate type.
3776 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3782 * Generate code to match a particular packet type, for link-layer types
3783 * using 802.2 LLC headers.
3785 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3786 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3788 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3789 * value, if <= ETHERMTU. We use that to determine whether to
3790 * match the DSAP or both DSAP and LSAP or to check the OUI and
3791 * protocol ID in a SNAP header.
3793 static struct block
*
3794 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3797 * XXX - handle token-ring variable-length header.
3803 case LLCSAP_NETBEUI
:
3805 * XXX - should we check both the DSAP and the
3806 * SSAP, like this, or should we check just the
3807 * DSAP, as we do for other SAP values?
3809 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3810 ((proto
<< 8) | proto
));
3814 * XXX - are there ever SNAP frames for IPX on
3815 * non-Ethernet 802.x networks?
3817 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3818 (bpf_int32
)LLCSAP_IPX
);
3820 case ETHERTYPE_ATALK
:
3822 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3823 * SNAP packets with an organization code of
3824 * 0x080007 (Apple, for Appletalk) and a protocol
3825 * type of ETHERTYPE_ATALK (Appletalk).
3827 * XXX - check for an organization code of
3828 * encapsulated Ethernet as well?
3830 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3834 * XXX - we don't have to check for IPX 802.3
3835 * here, but should we check for the IPX Ethertype?
3837 if (proto
<= ETHERMTU
) {
3839 * This is an LLC SAP value, so check
3842 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3845 * This is an Ethernet type; we assume that it's
3846 * unlikely that it'll appear in the right place
3847 * at random, and therefore check only the
3848 * location that would hold the Ethernet type
3849 * in a SNAP frame with an organization code of
3850 * 0x000000 (encapsulated Ethernet).
3852 * XXX - if we were to check for the SNAP DSAP and
3853 * LSAP, as per XXX, and were also to check for an
3854 * organization code of 0x000000 (encapsulated
3855 * Ethernet), we'd do
3857 * return gen_snap(cstate, 0x000000, proto);
3859 * here; for now, we don't, as per the above.
3860 * I don't know whether it's worth the extra CPU
3861 * time to do the right check or not.
3863 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3868 static struct block
*
3869 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3870 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3872 struct block
*b0
, *b1
;
3886 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3887 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3893 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3894 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3899 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3903 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3907 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3911 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3915 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3919 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3925 b0
= gen_linktype(cstate
, proto
);
3926 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3932 static struct block
*
3933 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3934 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3936 struct block
*b0
, *b1
;
3951 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3952 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3958 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3959 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3964 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3968 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3972 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3976 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3980 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3984 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3990 /* this order is important */
3991 a
= (uint32_t *)addr
;
3992 m
= (uint32_t *)mask
;
3993 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3994 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3996 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3998 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4000 b0
= gen_linktype(cstate
, proto
);
4006 static struct block
*
4007 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4009 register struct block
*b0
, *b1
;
4013 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4016 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4019 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4020 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4026 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4027 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4032 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4036 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4040 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4044 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4048 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4052 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4060 * Like gen_ehostop, but for DLT_FDDI
4062 static struct block
*
4063 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4065 struct block
*b0
, *b1
;
4069 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4072 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4075 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4076 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4082 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4083 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4088 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4092 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4096 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4100 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4104 bpf_error(cstate
, "'ra' is only supported on 802.11");
4108 bpf_error(cstate
, "'ta' is only supported on 802.11");
4116 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4118 static struct block
*
4119 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4121 register struct block
*b0
, *b1
;
4125 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4128 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4131 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4132 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4138 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4139 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4144 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4148 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4152 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4156 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4160 bpf_error(cstate
, "'ra' is only supported on 802.11");
4164 bpf_error(cstate
, "'ta' is only supported on 802.11");
4172 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4173 * various 802.11 + radio headers.
4175 static struct block
*
4176 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4178 register struct block
*b0
, *b1
, *b2
;
4179 register struct slist
*s
;
4181 #ifdef ENABLE_WLAN_FILTERING_PATCH
4184 * We need to disable the optimizer because the optimizer is buggy
4185 * and wipes out some LD instructions generated by the below
4186 * code to validate the Frame Control bits
4188 cstate
->no_optimize
= 1;
4189 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4196 * For control frames, there is no SA.
4198 * For management frames, SA is at an
4199 * offset of 10 from the beginning of
4202 * For data frames, SA is at an offset
4203 * of 10 from the beginning of the packet
4204 * if From DS is clear, at an offset of
4205 * 16 from the beginning of the packet
4206 * if From DS is set and To DS is clear,
4207 * and an offset of 24 from the beginning
4208 * of the packet if From DS is set and To DS
4213 * Generate the tests to be done for data frames
4216 * First, check for To DS set, i.e. check "link[1] & 0x01".
4218 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4219 b1
= new_block(cstate
, JMP(BPF_JSET
));
4220 b1
->s
.k
= 0x01; /* To DS */
4224 * If To DS is set, the SA is at 24.
4226 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4230 * Now, check for To DS not set, i.e. check
4231 * "!(link[1] & 0x01)".
4233 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4234 b2
= new_block(cstate
, JMP(BPF_JSET
));
4235 b2
->s
.k
= 0x01; /* To DS */
4240 * If To DS is not set, the SA is at 16.
4242 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4246 * Now OR together the last two checks. That gives
4247 * the complete set of checks for data frames with
4253 * Now check for From DS being set, and AND that with
4254 * the ORed-together checks.
4256 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4257 b1
= new_block(cstate
, JMP(BPF_JSET
));
4258 b1
->s
.k
= 0x02; /* From DS */
4263 * Now check for data frames with From DS not set.
4265 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4266 b2
= new_block(cstate
, JMP(BPF_JSET
));
4267 b2
->s
.k
= 0x02; /* From DS */
4272 * If From DS isn't set, the SA is at 10.
4274 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4278 * Now OR together the checks for data frames with
4279 * From DS not set and for data frames with From DS
4280 * set; that gives the checks done for data frames.
4285 * Now check for a data frame.
4286 * I.e, check "link[0] & 0x08".
4288 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4289 b1
= new_block(cstate
, JMP(BPF_JSET
));
4294 * AND that with the checks done for data frames.
4299 * If the high-order bit of the type value is 0, this
4300 * is a management frame.
4301 * I.e, check "!(link[0] & 0x08)".
4303 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4304 b2
= new_block(cstate
, JMP(BPF_JSET
));
4310 * For management frames, the SA is at 10.
4312 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4316 * OR that with the checks done for data frames.
4317 * That gives the checks done for management and
4323 * If the low-order bit of the type value is 1,
4324 * this is either a control frame or a frame
4325 * with a reserved type, and thus not a
4328 * I.e., check "!(link[0] & 0x04)".
4330 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4331 b1
= new_block(cstate
, JMP(BPF_JSET
));
4337 * AND that with the checks for data and management
4347 * For control frames, there is no DA.
4349 * For management frames, DA is at an
4350 * offset of 4 from the beginning of
4353 * For data frames, DA is at an offset
4354 * of 4 from the beginning of the packet
4355 * if To DS is clear and at an offset of
4356 * 16 from the beginning of the packet
4361 * Generate the tests to be done for data frames.
4363 * First, check for To DS set, i.e. "link[1] & 0x01".
4365 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4366 b1
= new_block(cstate
, JMP(BPF_JSET
));
4367 b1
->s
.k
= 0x01; /* To DS */
4371 * If To DS is set, the DA is at 16.
4373 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4377 * Now, check for To DS not set, i.e. check
4378 * "!(link[1] & 0x01)".
4380 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4381 b2
= new_block(cstate
, JMP(BPF_JSET
));
4382 b2
->s
.k
= 0x01; /* To DS */
4387 * If To DS is not set, the DA is at 4.
4389 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4393 * Now OR together the last two checks. That gives
4394 * the complete set of checks for data frames.
4399 * Now check for a data frame.
4400 * I.e, check "link[0] & 0x08".
4402 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4403 b1
= new_block(cstate
, JMP(BPF_JSET
));
4408 * AND that with the checks done for data frames.
4413 * If the high-order bit of the type value is 0, this
4414 * is a management frame.
4415 * I.e, check "!(link[0] & 0x08)".
4417 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4418 b2
= new_block(cstate
, JMP(BPF_JSET
));
4424 * For management frames, the DA is at 4.
4426 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4430 * OR that with the checks done for data frames.
4431 * That gives the checks done for management and
4437 * If the low-order bit of the type value is 1,
4438 * this is either a control frame or a frame
4439 * with a reserved type, and thus not a
4442 * I.e., check "!(link[0] & 0x04)".
4444 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4445 b1
= new_block(cstate
, JMP(BPF_JSET
));
4451 * AND that with the checks for data and management
4458 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4459 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4465 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4466 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4471 * XXX - add BSSID keyword?
4474 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4478 * Not present in CTS or ACK control frames.
4480 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4481 IEEE80211_FC0_TYPE_MASK
);
4483 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4484 IEEE80211_FC0_SUBTYPE_MASK
);
4486 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4487 IEEE80211_FC0_SUBTYPE_MASK
);
4491 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4497 * Not present in control frames.
4499 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4500 IEEE80211_FC0_TYPE_MASK
);
4502 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4508 * Present only if the direction mask has both "From DS"
4509 * and "To DS" set. Neither control frames nor management
4510 * frames should have both of those set, so we don't
4511 * check the frame type.
4513 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4514 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4515 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4521 * Not present in management frames; addr1 in other
4526 * If the high-order bit of the type value is 0, this
4527 * is a management frame.
4528 * I.e, check "(link[0] & 0x08)".
4530 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4531 b1
= new_block(cstate
, JMP(BPF_JSET
));
4538 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4541 * AND that with the check of addr1.
4548 * Not present in management frames; addr2, if present,
4553 * Not present in CTS or ACK control frames.
4555 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4556 IEEE80211_FC0_TYPE_MASK
);
4558 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4559 IEEE80211_FC0_SUBTYPE_MASK
);
4561 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4562 IEEE80211_FC0_SUBTYPE_MASK
);
4568 * If the high-order bit of the type value is 0, this
4569 * is a management frame.
4570 * I.e, check "(link[0] & 0x08)".
4572 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4573 b1
= new_block(cstate
, JMP(BPF_JSET
));
4578 * AND that with the check for frames other than
4579 * CTS and ACK frames.
4586 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4595 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4596 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4597 * as the RFC states.)
4599 static struct block
*
4600 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4602 register struct block
*b0
, *b1
;
4606 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4609 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4612 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4613 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4619 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4620 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4625 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4629 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4633 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4637 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4641 bpf_error(cstate
, "'ra' is only supported on 802.11");
4645 bpf_error(cstate
, "'ta' is only supported on 802.11");
4653 * This is quite tricky because there may be pad bytes in front of the
4654 * DECNET header, and then there are two possible data packet formats that
4655 * carry both src and dst addresses, plus 5 packet types in a format that
4656 * carries only the src node, plus 2 types that use a different format and
4657 * also carry just the src node.
4661 * Instead of doing those all right, we just look for data packets with
4662 * 0 or 1 bytes of padding. If you want to look at other packets, that
4663 * will require a lot more hacking.
4665 * To add support for filtering on DECNET "areas" (network numbers)
4666 * one would want to add a "mask" argument to this routine. That would
4667 * make the filter even more inefficient, although one could be clever
4668 * and not generate masking instructions if the mask is 0xFFFF.
4670 static struct block
*
4671 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4673 struct block
*b0
, *b1
, *b2
, *tmp
;
4674 u_int offset_lh
; /* offset if long header is received */
4675 u_int offset_sh
; /* offset if short header is received */
4680 offset_sh
= 1; /* follows flags */
4681 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4685 offset_sh
= 3; /* follows flags, dstnode */
4686 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4690 /* Inefficient because we do our Calvinball dance twice */
4691 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4692 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4698 /* Inefficient because we do our Calvinball dance twice */
4699 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4700 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4705 bpf_error(cstate
, "ISO host filtering not implemented");
4710 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4711 /* Check for pad = 1, long header case */
4712 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4713 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4714 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4715 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4717 /* Check for pad = 0, long header case */
4718 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4719 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4722 /* Check for pad = 1, short header case */
4723 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4724 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4725 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4728 /* Check for pad = 0, short header case */
4729 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4730 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4734 /* Combine with test for cstate->linktype */
4740 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4741 * test the bottom-of-stack bit, and then check the version number
4742 * field in the IP header.
4744 static struct block
*
4745 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4747 struct block
*b0
, *b1
;
4752 /* match the bottom-of-stack bit */
4753 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4754 /* match the IPv4 version number */
4755 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4760 /* match the bottom-of-stack bit */
4761 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4762 /* match the IPv4 version number */
4763 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4772 static struct block
*
4773 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4774 int proto
, int dir
, int type
)
4776 struct block
*b0
, *b1
;
4777 const char *typestr
;
4787 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4789 * Only check for non-IPv4 addresses if we're not
4790 * checking MPLS-encapsulated packets.
4792 if (cstate
->label_stack_depth
== 0) {
4793 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4795 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4801 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4804 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4807 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4810 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4813 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4816 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4819 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4822 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4825 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4828 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4831 return gen_dnhostop(cstate
, addr
, dir
);
4834 bpf_error(cstate
, "LAT host filtering not implemented");
4837 bpf_error(cstate
, "SCA host filtering not implemented");
4840 bpf_error(cstate
, "MOPRC host filtering not implemented");
4843 bpf_error(cstate
, "MOPDL host filtering not implemented");
4846 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4849 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4852 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4855 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4858 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4861 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4864 bpf_error(cstate
, "AARP host filtering not implemented");
4867 bpf_error(cstate
, "ISO host filtering not implemented");
4870 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4873 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4876 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4879 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4882 bpf_error(cstate
, "IPX host filtering not implemented");
4885 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4888 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
4891 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
4894 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
4897 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
4900 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
4903 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
4906 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
4909 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4912 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4921 static struct block
*
4922 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4923 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4925 const char *typestr
;
4935 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4938 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4941 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4944 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4947 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4950 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4953 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4956 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4959 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4962 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4965 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4968 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4971 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4974 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4977 bpf_error(cstate
, "AppleTalk modifier applied to %s", typestr
);
4980 bpf_error(cstate
, "AARP host filtering not implemented");
4983 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
4986 bpf_error(cstate
, "SCA host filtering not implemented");
4989 bpf_error(cstate
, "LAT host filtering not implemented");
4992 bpf_error(cstate
, "MOPDL host filtering not implemented");
4995 bpf_error(cstate
, "MOPRC host filtering not implemented");
4998 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5001 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5004 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5007 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5010 bpf_error(cstate
, "ISO host filtering not implemented");
5013 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5016 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5019 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5022 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5025 bpf_error(cstate
, "IPX host filtering not implemented");
5028 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5031 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5041 static struct block
*
5042 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5043 struct addrinfo
*alist
, int proto
, int dir
)
5045 struct block
*b0
, *b1
, *tmp
;
5046 struct addrinfo
*ai
;
5047 struct sockaddr_in
*sin
;
5050 bpf_error(cstate
, "direction applied to 'gateway'");
5057 switch (cstate
->linktype
) {
5059 case DLT_NETANALYZER
:
5060 case DLT_NETANALYZER_TRANSPARENT
:
5061 b1
= gen_prevlinkhdr_check(cstate
);
5062 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5067 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5070 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5072 case DLT_IEEE802_11
:
5073 case DLT_PRISM_HEADER
:
5074 case DLT_IEEE802_11_RADIO_AVS
:
5075 case DLT_IEEE802_11_RADIO
:
5077 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5081 * This is LLC-multiplexed traffic; if it were
5082 * LANE, cstate->linktype would have been set to
5086 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5088 case DLT_IP_OVER_FC
:
5089 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5093 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5096 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5098 * Does it have an address?
5100 if (ai
->ai_addr
!= NULL
) {
5102 * Yes. Is it an IPv4 address?
5104 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5106 * Generate an entry for it.
5108 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5109 tmp
= gen_host(cstate
,
5110 ntohl(sin
->sin_addr
.s_addr
),
5111 0xffffffff, proto
, Q_OR
, Q_HOST
);
5113 * Is it the *first* IPv4 address?
5117 * Yes, so start with it.
5122 * No, so OR it into the
5134 * No IPv4 addresses found.
5142 bpf_error(cstate
, "illegal modifier of 'gateway'");
5148 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5156 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5157 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5162 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5163 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5168 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5169 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5174 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5177 #ifndef IPPROTO_IGMP
5178 #define IPPROTO_IGMP 2
5182 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5185 #ifndef IPPROTO_IGRP
5186 #define IPPROTO_IGRP 9
5189 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5193 #define IPPROTO_PIM 103
5197 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5198 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5202 #ifndef IPPROTO_VRRP
5203 #define IPPROTO_VRRP 112
5207 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5210 #ifndef IPPROTO_CARP
5211 #define IPPROTO_CARP 112
5215 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5219 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5223 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5227 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5231 bpf_error(cstate
, "link layer applied in wrong context");
5234 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5238 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5242 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5246 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5250 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5254 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5258 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5262 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5265 #ifndef IPPROTO_ICMPV6
5266 #define IPPROTO_ICMPV6 58
5269 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5273 #define IPPROTO_AH 51
5276 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5277 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5282 #define IPPROTO_ESP 50
5285 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5286 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5291 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5295 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5299 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5302 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5303 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5304 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5306 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5308 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5310 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5314 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5315 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5316 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5318 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5320 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5322 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5326 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5327 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5328 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5330 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5335 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5336 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5341 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5342 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5344 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5346 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5351 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5352 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5357 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5358 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5363 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5367 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5371 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5375 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5379 bpf_error(cstate
, "'radio' is not a valid protocol type");
5387 static struct block
*
5388 gen_ipfrag(compiler_state_t
*cstate
)
5393 /* not IPv4 frag other than the first frag */
5394 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5395 b
= new_block(cstate
, JMP(BPF_JSET
));
5404 * Generate a comparison to a port value in the transport-layer header
5405 * at the specified offset from the beginning of that header.
5407 * XXX - this handles a variable-length prefix preceding the link-layer
5408 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5409 * variable-length link-layer headers (such as Token Ring or 802.11
5412 static struct block
*
5413 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5415 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5418 static struct block
*
5419 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5421 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5425 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5427 struct block
*b0
, *b1
, *tmp
;
5429 /* ip proto 'proto' and not a fragment other than the first fragment */
5430 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5431 b0
= gen_ipfrag(cstate
);
5436 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5440 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5444 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5445 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5451 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5452 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5457 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5461 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5465 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5469 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5473 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5477 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5488 static struct block
*
5489 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5491 struct block
*b0
, *b1
, *tmp
;
5496 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5497 * not LLC encapsulation with LLCSAP_IP.
5499 * For IEEE 802 networks - which includes 802.5 token ring
5500 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5501 * says that SNAP encapsulation is used, not LLC encapsulation
5504 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5505 * RFC 2225 say that SNAP encapsulation is used, not LLC
5506 * encapsulation with LLCSAP_IP.
5508 * So we always check for ETHERTYPE_IP.
5510 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5516 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5520 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5521 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5523 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5535 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5537 struct block
*b0
, *b1
, *tmp
;
5539 /* ip6 proto 'proto' */
5540 /* XXX - catch the first fragment of a fragmented packet? */
5541 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5545 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5549 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5553 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5554 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5560 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5561 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5573 static struct block
*
5574 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5576 struct block
*b0
, *b1
, *tmp
;
5578 /* link proto ip6 */
5579 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5585 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5589 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5590 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5592 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5603 /* gen_portrange code */
5604 static struct block
*
5605 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5608 struct block
*b1
, *b2
;
5612 * Reverse the order of the ports, so v1 is the lower one.
5621 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5622 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5630 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5633 struct block
*b0
, *b1
, *tmp
;
5635 /* ip proto 'proto' and not a fragment other than the first fragment */
5636 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5637 b0
= gen_ipfrag(cstate
);
5642 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5646 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5650 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5651 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5657 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5658 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5663 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5667 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5671 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5675 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5679 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5683 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5694 static struct block
*
5695 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5698 struct block
*b0
, *b1
, *tmp
;
5701 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5707 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5711 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5712 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5714 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5725 static struct block
*
5726 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5729 struct block
*b1
, *b2
;
5733 * Reverse the order of the ports, so v1 is the lower one.
5742 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5743 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5751 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5754 struct block
*b0
, *b1
, *tmp
;
5756 /* ip6 proto 'proto' */
5757 /* XXX - catch the first fragment of a fragmented packet? */
5758 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5762 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5766 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5770 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5771 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5777 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5778 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5790 static struct block
*
5791 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5794 struct block
*b0
, *b1
, *tmp
;
5796 /* link proto ip6 */
5797 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5803 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5807 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5808 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5810 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5822 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5831 v
= pcap_nametoproto(name
);
5832 if (v
== PROTO_UNDEF
)
5833 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5837 /* XXX should look up h/w protocol type based on cstate->linktype */
5838 v
= pcap_nametoeproto(name
);
5839 if (v
== PROTO_UNDEF
) {
5840 v
= pcap_nametollc(name
);
5841 if (v
== PROTO_UNDEF
)
5842 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5847 if (strcmp(name
, "esis") == 0)
5849 else if (strcmp(name
, "isis") == 0)
5851 else if (strcmp(name
, "clnp") == 0)
5854 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5866 gen_joinsp(struct stmt
**s
, int n
)
5872 static struct block
*
5873 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5875 #ifdef NO_PROTOCHAIN
5876 return gen_proto(cstate
, v
, proto
, dir
);
5878 struct block
*b0
, *b
;
5879 struct slist
*s
[100];
5880 int fix2
, fix3
, fix4
, fix5
;
5881 int ahcheck
, again
, end
;
5883 int reg2
= alloc_reg(cstate
);
5885 memset(s
, 0, sizeof(s
));
5886 fix3
= fix4
= fix5
= 0;
5893 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5894 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5898 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5903 * We don't handle variable-length prefixes before the link-layer
5904 * header, or variable-length link-layer headers, here yet.
5905 * We might want to add BPF instructions to do the protochain
5906 * work, to simplify that and, on platforms that have a BPF
5907 * interpreter with the new instructions, let the filtering
5908 * be done in the kernel. (We already require a modified BPF
5909 * engine to do the protochain stuff, to support backward
5910 * branches, and backward branch support is unlikely to appear
5911 * in kernel BPF engines.)
5913 if (cstate
->off_linkpl
.is_variable
)
5914 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5916 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
5919 * s[0] is a dummy entry to protect other BPF insn from damage
5920 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5921 * hard to find interdependency made by jump table fixup.
5924 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5929 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5932 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5933 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
5935 /* X = ip->ip_hl << 2 */
5936 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5937 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
5942 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5944 /* A = ip6->ip_nxt */
5945 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5946 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
5948 /* X = sizeof(struct ip6_hdr) */
5949 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
5955 bpf_error(cstate
, "unsupported proto to gen_protochain");
5959 /* again: if (A == v) goto end; else fall through; */
5961 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5963 s
[i
]->s
.jt
= NULL
; /*later*/
5964 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5968 #ifndef IPPROTO_NONE
5969 #define IPPROTO_NONE 59
5971 /* if (A == IPPROTO_NONE) goto end */
5972 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5973 s
[i
]->s
.jt
= NULL
; /*later*/
5974 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5975 s
[i
]->s
.k
= IPPROTO_NONE
;
5976 s
[fix5
]->s
.jf
= s
[i
];
5980 if (proto
== Q_IPV6
) {
5981 int v6start
, v6end
, v6advance
, j
;
5984 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5985 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5986 s
[i
]->s
.jt
= NULL
; /*later*/
5987 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5988 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5989 s
[fix2
]->s
.jf
= s
[i
];
5991 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5992 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5993 s
[i
]->s
.jt
= NULL
; /*later*/
5994 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5995 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5997 /* if (A == IPPROTO_ROUTING) goto v6advance */
5998 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
5999 s
[i
]->s
.jt
= NULL
; /*later*/
6000 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6001 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6003 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6004 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6005 s
[i
]->s
.jt
= NULL
; /*later*/
6006 s
[i
]->s
.jf
= NULL
; /*later*/
6007 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6017 * A = P[X + packet head];
6018 * X = X + (P[X + packet head + 1] + 1) * 8;
6020 /* A = P[X + packet head] */
6021 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6022 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6025 s
[i
] = new_stmt(cstate
, BPF_ST
);
6028 /* A = P[X + packet head + 1]; */
6029 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6030 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6033 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6037 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6041 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6045 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6048 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6052 /* goto again; (must use BPF_JA for backward jump) */
6053 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6054 s
[i
]->s
.k
= again
- i
- 1;
6055 s
[i
- 1]->s
.jf
= s
[i
];
6059 for (j
= v6start
; j
<= v6end
; j
++)
6060 s
[j
]->s
.jt
= s
[v6advance
];
6063 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6065 s
[fix2
]->s
.jf
= s
[i
];
6071 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6072 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6073 s
[i
]->s
.jt
= NULL
; /*later*/
6074 s
[i
]->s
.jf
= NULL
; /*later*/
6075 s
[i
]->s
.k
= IPPROTO_AH
;
6077 s
[fix3
]->s
.jf
= s
[ahcheck
];
6084 * X = X + (P[X + 1] + 2) * 4;
6087 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6089 /* A = P[X + packet head]; */
6090 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6091 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6094 s
[i
] = new_stmt(cstate
, BPF_ST
);
6098 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6101 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6105 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6107 /* A = P[X + packet head] */
6108 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6109 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6112 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6116 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6120 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6123 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6127 /* goto again; (must use BPF_JA for backward jump) */
6128 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6129 s
[i
]->s
.k
= again
- i
- 1;
6134 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6136 s
[fix2
]->s
.jt
= s
[end
];
6137 s
[fix4
]->s
.jf
= s
[end
];
6138 s
[fix5
]->s
.jt
= s
[end
];
6145 for (i
= 0; i
< max
- 1; i
++)
6146 s
[i
]->next
= s
[i
+ 1];
6147 s
[max
- 1]->next
= NULL
;
6152 b
= new_block(cstate
, JMP(BPF_JEQ
));
6153 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6156 free_reg(cstate
, reg2
);
6163 static struct block
*
6164 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6167 struct block
*b0
, *b1
;
6170 * A data frame has the 0x08 bit (b3) in the frame control field set
6171 * and the 0x04 bit (b2) clear.
6173 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6174 b0
= new_block(cstate
, JMP(BPF_JSET
));
6178 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6179 b1
= new_block(cstate
, JMP(BPF_JSET
));
6190 * Generate code that checks whether the packet is a packet for protocol
6191 * <proto> and whether the type field in that protocol's header has
6192 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6193 * IP packet and checks the protocol number in the IP header against <v>.
6195 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6196 * against Q_IP and Q_IPV6.
6198 static struct block
*
6199 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6201 struct block
*b0
, *b1
;
6206 if (dir
!= Q_DEFAULT
)
6207 bpf_error(cstate
, "direction applied to 'proto'");
6211 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6212 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6218 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6219 * not LLC encapsulation with LLCSAP_IP.
6221 * For IEEE 802 networks - which includes 802.5 token ring
6222 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6223 * says that SNAP encapsulation is used, not LLC encapsulation
6226 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6227 * RFC 2225 say that SNAP encapsulation is used, not LLC
6228 * encapsulation with LLCSAP_IP.
6230 * So we always check for ETHERTYPE_IP.
6232 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6234 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6236 b1
= gen_protochain(cstate
, v
, Q_IP
);
6242 switch (cstate
->linktype
) {
6246 * Frame Relay packets typically have an OSI
6247 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6248 * generates code to check for all the OSI
6249 * NLPIDs, so calling it and then adding a check
6250 * for the particular NLPID for which we're
6251 * looking is bogus, as we can just check for
6254 * What we check for is the NLPID and a frame
6255 * control field value of UI, i.e. 0x03 followed
6258 * XXX - assumes a 2-byte Frame Relay header with
6259 * DLCI and flags. What if the address is longer?
6261 * XXX - what about SNAP-encapsulated frames?
6263 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6269 * Cisco uses an Ethertype lookalike - for OSI,
6272 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6273 /* OSI in C-HDLC is stuffed with a fudge byte */
6274 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (bpf_int32
)v
);
6279 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6280 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (bpf_int32
)v
);
6286 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6288 * 4 is the offset of the PDU type relative to the IS-IS
6291 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (bpf_int32
)v
);
6296 bpf_error(cstate
, "arp does not encapsulate another protocol");
6300 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6304 bpf_error(cstate
, "atalk encapsulation is not specifiable");
6308 bpf_error(cstate
, "decnet encapsulation is not specifiable");
6312 bpf_error(cstate
, "sca does not encapsulate another protocol");
6316 bpf_error(cstate
, "lat does not encapsulate another protocol");
6320 bpf_error(cstate
, "moprc does not encapsulate another protocol");
6324 bpf_error(cstate
, "mopdl does not encapsulate another protocol");
6328 return gen_linktype(cstate
, v
);
6331 bpf_error(cstate
, "'udp proto' is bogus");
6335 bpf_error(cstate
, "'tcp proto' is bogus");
6339 bpf_error(cstate
, "'sctp proto' is bogus");
6343 bpf_error(cstate
, "'icmp proto' is bogus");
6347 bpf_error(cstate
, "'igmp proto' is bogus");
6351 bpf_error(cstate
, "'igrp proto' is bogus");
6355 bpf_error(cstate
, "'pim proto' is bogus");
6359 bpf_error(cstate
, "'vrrp proto' is bogus");
6363 bpf_error(cstate
, "'carp proto' is bogus");
6367 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6370 * Also check for a fragment header before the final
6373 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6374 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6376 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6379 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6385 bpf_error(cstate
, "'icmp6 proto' is bogus");
6388 bpf_error(cstate
, "'ah proto' is bogus");
6391 bpf_error(cstate
, "'ah proto' is bogus");
6394 bpf_error(cstate
, "'stp proto' is bogus");
6397 bpf_error(cstate
, "'ipx proto' is bogus");
6400 bpf_error(cstate
, "'netbeui proto' is bogus");
6403 bpf_error(cstate
, "'radio proto' is bogus");
6413 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6415 int proto
= q
.proto
;
6419 bpf_u_int32 mask
, addr
;
6420 struct addrinfo
*res
, *res0
;
6421 struct sockaddr_in
*sin4
;
6424 struct sockaddr_in6
*sin6
;
6425 struct in6_addr mask128
;
6427 struct block
*b
, *tmp
;
6428 int port
, real_proto
;
6434 addr
= pcap_nametonetaddr(name
);
6436 bpf_error(cstate
, "unknown network '%s'", name
);
6437 /* Left justify network addr and calculate its network mask */
6439 while (addr
&& (addr
& 0xff000000) == 0) {
6443 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6447 if (proto
== Q_LINK
) {
6448 switch (cstate
->linktype
) {
6451 case DLT_NETANALYZER
:
6452 case DLT_NETANALYZER_TRANSPARENT
:
6453 eaddr
= pcap_ether_hostton(name
);
6456 "unknown ether host '%s'", name
);
6457 tmp
= gen_prevlinkhdr_check(cstate
);
6458 b
= gen_ehostop(cstate
, eaddr
, dir
);
6465 eaddr
= pcap_ether_hostton(name
);
6468 "unknown FDDI host '%s'", name
);
6469 b
= gen_fhostop(cstate
, eaddr
, dir
);
6474 eaddr
= pcap_ether_hostton(name
);
6477 "unknown token ring host '%s'", name
);
6478 b
= gen_thostop(cstate
, eaddr
, dir
);
6482 case DLT_IEEE802_11
:
6483 case DLT_PRISM_HEADER
:
6484 case DLT_IEEE802_11_RADIO_AVS
:
6485 case DLT_IEEE802_11_RADIO
:
6487 eaddr
= pcap_ether_hostton(name
);
6490 "unknown 802.11 host '%s'", name
);
6491 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6495 case DLT_IP_OVER_FC
:
6496 eaddr
= pcap_ether_hostton(name
);
6499 "unknown Fibre Channel host '%s'", name
);
6500 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6505 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6506 } else if (proto
== Q_DECNET
) {
6507 unsigned short dn_addr
;
6509 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6511 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6513 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6518 * I don't think DECNET hosts can be multihomed, so
6519 * there is no need to build up a list of addresses
6521 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6524 memset(&mask128
, 0xff, sizeof(mask128
));
6526 res0
= res
= pcap_nametoaddrinfo(name
);
6528 bpf_error(cstate
, "unknown host '%s'", name
);
6535 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6536 tproto
== Q_DEFAULT
) {
6542 for (res
= res0
; res
; res
= res
->ai_next
) {
6543 switch (res
->ai_family
) {
6546 if (tproto
== Q_IPV6
)
6550 sin4
= (struct sockaddr_in
*)
6552 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6553 0xffffffff, tproto
, dir
, q
.addr
);
6557 if (tproto6
== Q_IP
)
6560 sin6
= (struct sockaddr_in6
*)
6562 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6563 &mask128
, tproto6
, dir
, q
.addr
);
6576 bpf_error(cstate
, "unknown host '%s'%s", name
,
6577 (proto
== Q_DEFAULT
)
6579 : " for specified address family");
6585 if (proto
!= Q_DEFAULT
&&
6586 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6587 bpf_error(cstate
, "illegal qualifier of 'port'");
6588 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6589 bpf_error(cstate
, "unknown port '%s'", name
);
6590 if (proto
== Q_UDP
) {
6591 if (real_proto
== IPPROTO_TCP
)
6592 bpf_error(cstate
, "port '%s' is tcp", name
);
6593 else if (real_proto
== IPPROTO_SCTP
)
6594 bpf_error(cstate
, "port '%s' is sctp", name
);
6596 /* override PROTO_UNDEF */
6597 real_proto
= IPPROTO_UDP
;
6599 if (proto
== Q_TCP
) {
6600 if (real_proto
== IPPROTO_UDP
)
6601 bpf_error(cstate
, "port '%s' is udp", name
);
6603 else if (real_proto
== IPPROTO_SCTP
)
6604 bpf_error(cstate
, "port '%s' is sctp", name
);
6606 /* override PROTO_UNDEF */
6607 real_proto
= IPPROTO_TCP
;
6609 if (proto
== Q_SCTP
) {
6610 if (real_proto
== IPPROTO_UDP
)
6611 bpf_error(cstate
, "port '%s' is udp", name
);
6613 else if (real_proto
== IPPROTO_TCP
)
6614 bpf_error(cstate
, "port '%s' is tcp", name
);
6616 /* override PROTO_UNDEF */
6617 real_proto
= IPPROTO_SCTP
;
6620 bpf_error(cstate
, "illegal port number %d < 0", port
);
6622 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6623 b
= gen_port(cstate
, port
, real_proto
, dir
);
6624 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6628 if (proto
!= Q_DEFAULT
&&
6629 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6630 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6631 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6632 bpf_error(cstate
, "unknown port in range '%s'", name
);
6633 if (proto
== Q_UDP
) {
6634 if (real_proto
== IPPROTO_TCP
)
6635 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6636 else if (real_proto
== IPPROTO_SCTP
)
6637 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6639 /* override PROTO_UNDEF */
6640 real_proto
= IPPROTO_UDP
;
6642 if (proto
== Q_TCP
) {
6643 if (real_proto
== IPPROTO_UDP
)
6644 bpf_error(cstate
, "port in range '%s' is udp", name
);
6645 else if (real_proto
== IPPROTO_SCTP
)
6646 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6648 /* override PROTO_UNDEF */
6649 real_proto
= IPPROTO_TCP
;
6651 if (proto
== Q_SCTP
) {
6652 if (real_proto
== IPPROTO_UDP
)
6653 bpf_error(cstate
, "port in range '%s' is udp", name
);
6654 else if (real_proto
== IPPROTO_TCP
)
6655 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6657 /* override PROTO_UNDEF */
6658 real_proto
= IPPROTO_SCTP
;
6661 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6663 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6665 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6667 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6669 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6670 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6675 eaddr
= pcap_ether_hostton(name
);
6677 bpf_error(cstate
, "unknown ether host: %s", name
);
6679 res
= pcap_nametoaddrinfo(name
);
6682 bpf_error(cstate
, "unknown host '%s'", name
);
6683 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6687 bpf_error(cstate
, "unknown host '%s'", name
);
6690 bpf_error(cstate
, "'gateway' not supported in this configuration");
6694 real_proto
= lookup_proto(cstate
, name
, proto
);
6695 if (real_proto
>= 0)
6696 return gen_proto(cstate
, real_proto
, proto
, dir
);
6698 bpf_error(cstate
, "unknown protocol: %s", name
);
6701 real_proto
= lookup_proto(cstate
, name
, proto
);
6702 if (real_proto
>= 0)
6703 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6705 bpf_error(cstate
, "unknown protocol: %s", name
);
6716 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6717 unsigned int masklen
, struct qual q
)
6719 register int nlen
, mlen
;
6722 nlen
= __pcap_atoin(s1
, &n
);
6723 /* Promote short ipaddr */
6727 mlen
= __pcap_atoin(s2
, &m
);
6728 /* Promote short ipaddr */
6731 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6734 /* Convert mask len to mask */
6736 bpf_error(cstate
, "mask length must be <= 32");
6739 * X << 32 is not guaranteed by C to be 0; it's
6744 m
= 0xffffffff << (32 - masklen
);
6746 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6753 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6756 bpf_error(cstate
, "Mask syntax for networks only");
6763 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6766 int proto
= q
.proto
;
6772 else if (q
.proto
== Q_DECNET
) {
6773 vlen
= __pcap_atodn(s
, &v
);
6775 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6777 vlen
= __pcap_atoin(s
, &v
);
6784 if (proto
== Q_DECNET
)
6785 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6786 else if (proto
== Q_LINK
) {
6787 bpf_error(cstate
, "illegal link layer address");
6790 if (s
== NULL
&& q
.addr
== Q_NET
) {
6791 /* Promote short net number */
6792 while (v
&& (v
& 0xff000000) == 0) {
6797 /* Promote short ipaddr */
6799 mask
<<= 32 - vlen
;
6801 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6806 proto
= IPPROTO_UDP
;
6807 else if (proto
== Q_TCP
)
6808 proto
= IPPROTO_TCP
;
6809 else if (proto
== Q_SCTP
)
6810 proto
= IPPROTO_SCTP
;
6811 else if (proto
== Q_DEFAULT
)
6812 proto
= PROTO_UNDEF
;
6814 bpf_error(cstate
, "illegal qualifier of 'port'");
6817 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6821 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6822 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6828 proto
= IPPROTO_UDP
;
6829 else if (proto
== Q_TCP
)
6830 proto
= IPPROTO_TCP
;
6831 else if (proto
== Q_SCTP
)
6832 proto
= IPPROTO_SCTP
;
6833 else if (proto
== Q_DEFAULT
)
6834 proto
= PROTO_UNDEF
;
6836 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6839 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6843 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6844 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6849 bpf_error(cstate
, "'gateway' requires a name");
6853 return gen_proto(cstate
, (int)v
, proto
, dir
);
6856 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6871 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6872 unsigned int masklen
, struct qual q
)
6874 struct addrinfo
*res
;
6875 struct in6_addr
*addr
;
6876 struct in6_addr mask
;
6881 bpf_error(cstate
, "no mask %s supported", s2
);
6883 res
= pcap_nametoaddrinfo(s1
);
6885 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6888 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6889 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6891 if (sizeof(mask
) * 8 < masklen
)
6892 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6893 memset(&mask
, 0, sizeof(mask
));
6894 memset(&mask
, 0xff, masklen
/ 8);
6896 mask
.s6_addr
[masklen
/ 8] =
6897 (0xff << (8 - masklen
% 8)) & 0xff;
6900 a
= (uint32_t *)addr
;
6901 m
= (uint32_t *)&mask
;
6902 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6903 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6904 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
6912 bpf_error(cstate
, "Mask syntax for networks only");
6916 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6922 bpf_error(cstate
, "invalid qualifier against IPv6 address");
6929 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
6931 struct block
*b
, *tmp
;
6933 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6934 cstate
->e
= pcap_ether_aton(s
);
6935 if (cstate
->e
== NULL
)
6936 bpf_error(cstate
, "malloc");
6937 switch (cstate
->linktype
) {
6939 case DLT_NETANALYZER
:
6940 case DLT_NETANALYZER_TRANSPARENT
:
6941 tmp
= gen_prevlinkhdr_check(cstate
);
6942 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
6947 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
6950 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
6952 case DLT_IEEE802_11
:
6953 case DLT_PRISM_HEADER
:
6954 case DLT_IEEE802_11_RADIO_AVS
:
6955 case DLT_IEEE802_11_RADIO
:
6957 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
6959 case DLT_IP_OVER_FC
:
6960 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
6965 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6973 bpf_error(cstate
, "ethernet address used in non-ether expression");
6978 sappend(struct slist
*s0
, struct slist
*s1
)
6981 * This is definitely not the best way to do this, but the
6982 * lists will rarely get long.
6989 static struct slist
*
6990 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
6994 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
6999 static struct slist
*
7000 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7004 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7010 * Modify "index" to use the value stored into its register as an
7011 * offset relative to the beginning of the header for the protocol
7012 * "proto", and allocate a register and put an item "size" bytes long
7013 * (1, 2, or 4) at that offset into that register, making it the register
7017 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7019 struct slist
*s
, *tmp
;
7021 int regno
= alloc_reg(cstate
);
7023 free_reg(cstate
, inst
->regno
);
7027 bpf_error(cstate
, "data size must be 1, 2, or 4");
7043 bpf_error(cstate
, "unsupported index operation");
7047 * The offset is relative to the beginning of the packet
7048 * data, if we have a radio header. (If we don't, this
7051 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7052 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7053 cstate
->linktype
!= DLT_PRISM_HEADER
)
7054 bpf_error(cstate
, "radio information not present in capture");
7057 * Load into the X register the offset computed into the
7058 * register specified by "index".
7060 s
= xfer_to_x(cstate
, inst
);
7063 * Load the item at that offset.
7065 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7067 sappend(inst
->s
, s
);
7072 * The offset is relative to the beginning of
7073 * the link-layer header.
7075 * XXX - what about ATM LANE? Should the index be
7076 * relative to the beginning of the AAL5 frame, so
7077 * that 0 refers to the beginning of the LE Control
7078 * field, or relative to the beginning of the LAN
7079 * frame, so that 0 refers, for Ethernet LANE, to
7080 * the beginning of the destination address?
7082 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7085 * If "s" is non-null, it has code to arrange that the
7086 * X register contains the length of the prefix preceding
7087 * the link-layer header. Add to it the offset computed
7088 * into the register specified by "index", and move that
7089 * into the X register. Otherwise, just load into the X
7090 * register the offset computed into the register specified
7094 sappend(s
, xfer_to_a(cstate
, inst
));
7095 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7096 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7098 s
= xfer_to_x(cstate
, inst
);
7101 * Load the item at the sum of the offset we've put in the
7102 * X register and the offset of the start of the link
7103 * layer header (which is 0 if the radio header is
7104 * variable-length; that header length is what we put
7105 * into the X register and then added to the index).
7107 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7108 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7110 sappend(inst
->s
, s
);
7124 * The offset is relative to the beginning of
7125 * the network-layer header.
7126 * XXX - are there any cases where we want
7127 * cstate->off_nl_nosnap?
7129 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7132 * If "s" is non-null, it has code to arrange that the
7133 * X register contains the variable part of the offset
7134 * of the link-layer payload. Add to it the offset
7135 * computed into the register specified by "index",
7136 * and move that into the X register. Otherwise, just
7137 * load into the X register the offset computed into
7138 * the register specified by "index".
7141 sappend(s
, xfer_to_a(cstate
, inst
));
7142 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7143 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7145 s
= xfer_to_x(cstate
, inst
);
7148 * Load the item at the sum of the offset we've put in the
7149 * X register, the offset of the start of the network
7150 * layer header from the beginning of the link-layer
7151 * payload, and the constant part of the offset of the
7152 * start of the link-layer payload.
7154 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7155 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7157 sappend(inst
->s
, s
);
7160 * Do the computation only if the packet contains
7161 * the protocol in question.
7163 b
= gen_proto_abbrev(cstate
, proto
);
7165 gen_and(inst
->b
, b
);
7179 * The offset is relative to the beginning of
7180 * the transport-layer header.
7182 * Load the X register with the length of the IPv4 header
7183 * (plus the offset of the link-layer header, if it's
7184 * a variable-length header), in bytes.
7186 * XXX - are there any cases where we want
7187 * cstate->off_nl_nosnap?
7188 * XXX - we should, if we're built with
7189 * IPv6 support, generate code to load either
7190 * IPv4, IPv6, or both, as appropriate.
7192 s
= gen_loadx_iphdrlen(cstate
);
7195 * The X register now contains the sum of the variable
7196 * part of the offset of the link-layer payload and the
7197 * length of the network-layer header.
7199 * Load into the A register the offset relative to
7200 * the beginning of the transport layer header,
7201 * add the X register to that, move that to the
7202 * X register, and load with an offset from the
7203 * X register equal to the sum of the constant part of
7204 * the offset of the link-layer payload and the offset,
7205 * relative to the beginning of the link-layer payload,
7206 * of the network-layer header.
7208 sappend(s
, xfer_to_a(cstate
, inst
));
7209 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7210 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7211 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7212 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7213 sappend(inst
->s
, s
);
7216 * Do the computation only if the packet contains
7217 * the protocol in question - which is true only
7218 * if this is an IP datagram and is the first or
7219 * only fragment of that datagram.
7221 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7223 gen_and(inst
->b
, b
);
7224 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7229 * Do the computation only if the packet contains
7230 * the protocol in question.
7232 b
= gen_proto_abbrev(cstate
, Q_IPV6
);
7234 gen_and(inst
->b
, b
);
7239 * Check if we have an icmp6 next header
7241 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7243 gen_and(inst
->b
, b
);
7248 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7250 * If "s" is non-null, it has code to arrange that the
7251 * X register contains the variable part of the offset
7252 * of the link-layer payload. Add to it the offset
7253 * computed into the register specified by "index",
7254 * and move that into the X register. Otherwise, just
7255 * load into the X register the offset computed into
7256 * the register specified by "index".
7259 sappend(s
, xfer_to_a(cstate
, inst
));
7260 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7261 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7263 s
= xfer_to_x(cstate
, inst
);
7267 * Load the item at the sum of the offset we've put in the
7268 * X register, the offset of the start of the network
7269 * layer header from the beginning of the link-layer
7270 * payload, and the constant part of the offset of the
7271 * start of the link-layer payload.
7273 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7274 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7277 sappend(inst
->s
, s
);
7281 inst
->regno
= regno
;
7282 s
= new_stmt(cstate
, BPF_ST
);
7284 sappend(inst
->s
, s
);
7290 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7291 struct arth
*a1
, int reversed
)
7293 struct slist
*s0
, *s1
, *s2
;
7294 struct block
*b
, *tmp
;
7296 s0
= xfer_to_x(cstate
, a1
);
7297 s1
= xfer_to_a(cstate
, a0
);
7298 if (code
== BPF_JEQ
) {
7299 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7300 b
= new_block(cstate
, JMP(code
));
7304 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7310 sappend(a0
->s
, a1
->s
);
7314 free_reg(cstate
, a0
->regno
);
7315 free_reg(cstate
, a1
->regno
);
7317 /* 'and' together protocol checks */
7320 gen_and(a0
->b
, tmp
= a1
->b
);
7334 gen_loadlen(compiler_state_t
*cstate
)
7336 int regno
= alloc_reg(cstate
);
7337 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7340 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7341 s
->next
= new_stmt(cstate
, BPF_ST
);
7342 s
->next
->s
.k
= regno
;
7350 gen_loadi(compiler_state_t
*cstate
, int val
)
7356 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7358 reg
= alloc_reg(cstate
);
7360 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7362 s
->next
= new_stmt(cstate
, BPF_ST
);
7371 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7375 s
= xfer_to_a(cstate
, a
);
7377 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7380 s
= new_stmt(cstate
, BPF_ST
);
7388 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7391 struct slist
*s0
, *s1
, *s2
;
7394 * Disallow division by, or modulus by, zero; we do this here
7395 * so that it gets done even if the optimizer is disabled.
7397 if (code
== BPF_DIV
) {
7398 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7399 bpf_error(cstate
, "division by zero");
7400 } else if (code
== BPF_MOD
) {
7401 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7402 bpf_error(cstate
, "modulus by zero");
7404 s0
= xfer_to_x(cstate
, a1
);
7405 s1
= xfer_to_a(cstate
, a0
);
7406 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7411 sappend(a0
->s
, a1
->s
);
7413 free_reg(cstate
, a0
->regno
);
7414 free_reg(cstate
, a1
->regno
);
7416 s0
= new_stmt(cstate
, BPF_ST
);
7417 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7424 * Initialize the table of used registers and the current register.
7427 init_regs(compiler_state_t
*cstate
)
7430 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7434 * Return the next free register.
7437 alloc_reg(compiler_state_t
*cstate
)
7439 int n
= BPF_MEMWORDS
;
7442 if (cstate
->regused
[cstate
->curreg
])
7443 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7445 cstate
->regused
[cstate
->curreg
] = 1;
7446 return cstate
->curreg
;
7449 bpf_error(cstate
, "too many registers needed to evaluate expression");
7454 * Return a register to the table so it can
7458 free_reg(compiler_state_t
*cstate
, int n
)
7460 cstate
->regused
[n
] = 0;
7463 static struct block
*
7464 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7469 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7470 b
= new_block(cstate
, JMP(jmp
));
7478 gen_greater(compiler_state_t
*cstate
, int n
)
7480 return gen_len(cstate
, BPF_JGE
, n
);
7484 * Actually, this is less than or equal.
7487 gen_less(compiler_state_t
*cstate
, int n
)
7491 b
= gen_len(cstate
, BPF_JGT
, n
);
7498 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7499 * the beginning of the link-layer header.
7500 * XXX - that means you can't test values in the radiotap header, but
7501 * as that header is difficult if not impossible to parse generally
7502 * without a loop, that might not be a severe problem. A new keyword
7503 * "radio" could be added for that, although what you'd really want
7504 * would be a way of testing particular radio header values, which
7505 * would generate code appropriate to the radio header in question.
7508 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7518 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7521 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7525 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7529 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7533 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7537 b
= new_block(cstate
, JMP(BPF_JEQ
));
7544 static const u_char abroadcast
[] = { 0x0 };
7547 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7549 bpf_u_int32 hostmask
;
7550 struct block
*b0
, *b1
, *b2
;
7551 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7557 switch (cstate
->linktype
) {
7559 case DLT_ARCNET_LINUX
:
7560 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7562 case DLT_NETANALYZER
:
7563 case DLT_NETANALYZER_TRANSPARENT
:
7564 b1
= gen_prevlinkhdr_check(cstate
);
7565 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7570 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7572 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7573 case DLT_IEEE802_11
:
7574 case DLT_PRISM_HEADER
:
7575 case DLT_IEEE802_11_RADIO_AVS
:
7576 case DLT_IEEE802_11_RADIO
:
7578 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7579 case DLT_IP_OVER_FC
:
7580 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7582 bpf_error(cstate
, "not a broadcast link");
7588 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7589 * as an indication that we don't know the netmask, and fail
7592 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7593 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7594 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7595 hostmask
= ~cstate
->netmask
;
7596 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7597 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7598 (bpf_int32
)(~0 & hostmask
), hostmask
);
7603 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7608 * Generate code to test the low-order bit of a MAC address (that's
7609 * the bottom bit of the *first* byte).
7611 static struct block
*
7612 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7614 register struct block
*b0
;
7615 register struct slist
*s
;
7617 /* link[offset] & 1 != 0 */
7618 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7619 b0
= new_block(cstate
, JMP(BPF_JSET
));
7626 gen_multicast(compiler_state_t
*cstate
, int proto
)
7628 register struct block
*b0
, *b1
, *b2
;
7629 register struct slist
*s
;
7635 switch (cstate
->linktype
) {
7637 case DLT_ARCNET_LINUX
:
7638 /* all ARCnet multicasts use the same address */
7639 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7641 case DLT_NETANALYZER
:
7642 case DLT_NETANALYZER_TRANSPARENT
:
7643 b1
= gen_prevlinkhdr_check(cstate
);
7644 /* ether[0] & 1 != 0 */
7645 b0
= gen_mac_multicast(cstate
, 0);
7651 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7653 * XXX - was that referring to bit-order issues?
7655 /* fddi[1] & 1 != 0 */
7656 return gen_mac_multicast(cstate
, 1);
7658 /* tr[2] & 1 != 0 */
7659 return gen_mac_multicast(cstate
, 2);
7660 case DLT_IEEE802_11
:
7661 case DLT_PRISM_HEADER
:
7662 case DLT_IEEE802_11_RADIO_AVS
:
7663 case DLT_IEEE802_11_RADIO
:
7668 * For control frames, there is no DA.
7670 * For management frames, DA is at an
7671 * offset of 4 from the beginning of
7674 * For data frames, DA is at an offset
7675 * of 4 from the beginning of the packet
7676 * if To DS is clear and at an offset of
7677 * 16 from the beginning of the packet
7682 * Generate the tests to be done for data frames.
7684 * First, check for To DS set, i.e. "link[1] & 0x01".
7686 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7687 b1
= new_block(cstate
, JMP(BPF_JSET
));
7688 b1
->s
.k
= 0x01; /* To DS */
7692 * If To DS is set, the DA is at 16.
7694 b0
= gen_mac_multicast(cstate
, 16);
7698 * Now, check for To DS not set, i.e. check
7699 * "!(link[1] & 0x01)".
7701 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7702 b2
= new_block(cstate
, JMP(BPF_JSET
));
7703 b2
->s
.k
= 0x01; /* To DS */
7708 * If To DS is not set, the DA is at 4.
7710 b1
= gen_mac_multicast(cstate
, 4);
7714 * Now OR together the last two checks. That gives
7715 * the complete set of checks for data frames.
7720 * Now check for a data frame.
7721 * I.e, check "link[0] & 0x08".
7723 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7724 b1
= new_block(cstate
, JMP(BPF_JSET
));
7729 * AND that with the checks done for data frames.
7734 * If the high-order bit of the type value is 0, this
7735 * is a management frame.
7736 * I.e, check "!(link[0] & 0x08)".
7738 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7739 b2
= new_block(cstate
, JMP(BPF_JSET
));
7745 * For management frames, the DA is at 4.
7747 b1
= gen_mac_multicast(cstate
, 4);
7751 * OR that with the checks done for data frames.
7752 * That gives the checks done for management and
7758 * If the low-order bit of the type value is 1,
7759 * this is either a control frame or a frame
7760 * with a reserved type, and thus not a
7763 * I.e., check "!(link[0] & 0x04)".
7765 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7766 b1
= new_block(cstate
, JMP(BPF_JSET
));
7772 * AND that with the checks for data and management
7777 case DLT_IP_OVER_FC
:
7778 b0
= gen_mac_multicast(cstate
, 2);
7783 /* Link not known to support multicasts */
7787 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7788 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7793 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7794 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7798 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7803 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7804 * Outbound traffic is sent by this machine, while inbound traffic is
7805 * sent by a remote machine (and may include packets destined for a
7806 * unicast or multicast link-layer address we are not subscribing to).
7807 * These are the same definitions implemented by pcap_setdirection().
7808 * Capturing only unicast traffic destined for this host is probably
7809 * better accomplished using a higher-layer filter.
7812 gen_inbound(compiler_state_t
*cstate
, int dir
)
7814 register struct block
*b0
;
7817 * Only some data link types support inbound/outbound qualifiers.
7819 switch (cstate
->linktype
) {
7821 b0
= gen_relation(cstate
, BPF_JEQ
,
7822 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7823 gen_loadi(cstate
, 0),
7829 /* match outgoing packets */
7830 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7832 /* match incoming packets */
7833 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7838 /* match outgoing packets */
7839 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7841 /* to filter on inbound traffic, invert the match */
7846 case DLT_LINUX_SLL2
:
7847 /* match outgoing packets */
7848 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
7850 /* to filter on inbound traffic, invert the match */
7855 #ifdef HAVE_NET_PFVAR_H
7857 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7858 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7864 /* match outgoing packets */
7865 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7867 /* match incoming packets */
7868 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7872 case DLT_JUNIPER_MFR
:
7873 case DLT_JUNIPER_MLFR
:
7874 case DLT_JUNIPER_MLPPP
:
7875 case DLT_JUNIPER_ATM1
:
7876 case DLT_JUNIPER_ATM2
:
7877 case DLT_JUNIPER_PPPOE
:
7878 case DLT_JUNIPER_PPPOE_ATM
:
7879 case DLT_JUNIPER_GGSN
:
7880 case DLT_JUNIPER_ES
:
7881 case DLT_JUNIPER_MONITOR
:
7882 case DLT_JUNIPER_SERVICES
:
7883 case DLT_JUNIPER_ETHER
:
7884 case DLT_JUNIPER_PPP
:
7885 case DLT_JUNIPER_FRELAY
:
7886 case DLT_JUNIPER_CHDLC
:
7887 case DLT_JUNIPER_VP
:
7888 case DLT_JUNIPER_ST
:
7889 case DLT_JUNIPER_ISM
:
7890 case DLT_JUNIPER_VS
:
7891 case DLT_JUNIPER_SRX_E2E
:
7892 case DLT_JUNIPER_FIBRECHANNEL
:
7893 case DLT_JUNIPER_ATM_CEMIC
:
7895 /* juniper flags (including direction) are stored
7896 * the byte after the 3-byte magic number */
7898 /* match outgoing packets */
7899 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7901 /* match incoming packets */
7902 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7908 * If we have packet meta-data indicating a direction,
7909 * and that metadata can be checked by BPF code, check
7910 * it. Otherwise, give up, as this link-layer type has
7911 * nothing in the packet data.
7913 * Currently, the only platform where a BPF filter can
7914 * check that metadata is Linux with the in-kernel
7915 * BPF interpreter. If other packet capture mechanisms
7916 * and BPF filters also supported this, it would be
7917 * nice. It would be even better if they made that
7918 * metadata available so that we could provide it
7919 * with newer capture APIs, allowing it to be saved
7922 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7924 * This is Linux with PF_PACKET support.
7925 * If this is a *live* capture, we can look at
7926 * special meta-data in the filter expression;
7927 * if it's a savefile, we can't.
7929 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
7930 /* We have a FILE *, so this is a savefile */
7931 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
7936 /* match outgoing packets */
7937 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7940 /* to filter on inbound traffic, invert the match */
7943 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7944 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
7947 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7952 #ifdef HAVE_NET_PFVAR_H
7953 /* PF firewall log matched interface */
7955 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
7960 if (cstate
->linktype
!= DLT_PFLOG
) {
7961 bpf_error(cstate
, "ifname supported only on PF linktype");
7964 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7965 off
= offsetof(struct pfloghdr
, ifname
);
7966 if (strlen(ifname
) >= len
) {
7967 bpf_error(cstate
, "ifname interface names can only be %d characters",
7971 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7975 /* PF firewall log ruleset name */
7977 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
7981 if (cstate
->linktype
!= DLT_PFLOG
) {
7982 bpf_error(cstate
, "ruleset supported only on PF linktype");
7986 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7987 bpf_error(cstate
, "ruleset names can only be %ld characters",
7988 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7992 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7993 strlen(ruleset
), (const u_char
*)ruleset
);
7997 /* PF firewall log rule number */
7999 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8003 if (cstate
->linktype
!= DLT_PFLOG
) {
8004 bpf_error(cstate
, "rnr supported only on PF linktype");
8008 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8013 /* PF firewall log sub-rule number */
8015 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8019 if (cstate
->linktype
!= DLT_PFLOG
) {
8020 bpf_error(cstate
, "srnr supported only on PF linktype");
8024 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8029 /* PF firewall log reason code */
8031 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8035 if (cstate
->linktype
!= DLT_PFLOG
) {
8036 bpf_error(cstate
, "reason supported only on PF linktype");
8040 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8045 /* PF firewall log action */
8047 gen_pf_action(compiler_state_t
*cstate
, int action
)
8051 if (cstate
->linktype
!= DLT_PFLOG
) {
8052 bpf_error(cstate
, "action supported only on PF linktype");
8056 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8060 #else /* !HAVE_NET_PFVAR_H */
8062 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8064 bpf_error(cstate
, "libpcap was compiled without pf support");
8069 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8071 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8076 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8078 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8083 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8085 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8090 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8092 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8097 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8099 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8102 #endif /* HAVE_NET_PFVAR_H */
8104 /* IEEE 802.11 wireless header */
8106 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
8110 switch (cstate
->linktype
) {
8112 case DLT_IEEE802_11
:
8113 case DLT_PRISM_HEADER
:
8114 case DLT_IEEE802_11_RADIO_AVS
:
8115 case DLT_IEEE802_11_RADIO
:
8116 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8121 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8129 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8133 switch (cstate
->linktype
) {
8135 case DLT_IEEE802_11
:
8136 case DLT_PRISM_HEADER
:
8137 case DLT_IEEE802_11_RADIO_AVS
:
8138 case DLT_IEEE802_11_RADIO
:
8142 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8146 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8147 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8153 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8157 switch (cstate
->linktype
) {
8160 case DLT_ARCNET_LINUX
:
8161 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8162 q
.proto
== Q_LINK
) {
8163 cstate
->e
= pcap_ether_aton(s
);
8164 if (cstate
->e
== NULL
)
8165 bpf_error(cstate
, "malloc");
8166 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8171 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8177 bpf_error(cstate
, "aid supported only on ARCnet");
8182 static struct block
*
8183 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8185 register struct block
*b0
, *b1
;
8188 /* src comes first, different from Ethernet */
8190 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8193 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8196 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8197 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8203 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8204 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8209 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8213 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8217 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8221 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8225 bpf_error(cstate
, "'ra' is only supported on 802.11");
8229 bpf_error(cstate
, "'ta' is only supported on 802.11");
8236 static struct block
*
8237 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8239 struct block
*b0
, *b1
;
8241 /* check for VLAN, including QinQ */
8242 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8243 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8246 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8252 static struct block
*
8253 gen_vlan_vid_test(compiler_state_t
*cstate
, int vlan_num
)
8255 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8258 static struct block
*
8259 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8261 struct block
*b0
, *b1
;
8263 b0
= gen_vlan_tpid_test(cstate
);
8265 if (vlan_num
>= 0) {
8266 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8272 * Both payload and link header type follow the VLAN tags so that
8273 * both need to be updated.
8275 cstate
->off_linkpl
.constant_part
+= 4;
8276 cstate
->off_linktype
.constant_part
+= 4;
8281 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8282 /* add v to variable part of off */
8284 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8288 if (!off
->is_variable
)
8289 off
->is_variable
= 1;
8291 off
->reg
= alloc_reg(cstate
);
8293 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8296 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8299 s2
= new_stmt(cstate
, BPF_ST
);
8305 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8306 * and link type offsets first
8309 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8313 /* offset determined at run time, shift variable part */
8315 cstate
->is_vlan_vloffset
= 1;
8316 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8317 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8319 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8320 sappend(s
.next
, b_tpid
->head
->stmts
);
8321 b_tpid
->head
->stmts
= s
.next
;
8325 * patch block b_vid (VLAN id test) to load VID value either from packet
8326 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8329 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8331 struct slist
*s
, *s2
, *sjeq
;
8334 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8335 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8337 /* true -> next instructions, false -> beginning of b_vid */
8338 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8340 sjeq
->s
.jf
= b_vid
->stmts
;
8343 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8344 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8348 /* Jump to the test in b_vid. We need to jump one instruction before
8349 * the end of the b_vid block so that we only skip loading the TCI
8350 * from packet data and not the 'and' instruction extractging VID.
8353 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8355 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8359 /* insert our statements at the beginning of b_vid */
8360 sappend(s
, b_vid
->stmts
);
8365 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8366 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8367 * tag can be either in metadata or in packet data; therefore if the
8368 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8369 * header for VLAN tag. As the decision is done at run time, we need
8370 * update variable part of the offsets
8372 static struct block
*
8373 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, int vlan_num
)
8375 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8378 /* generate new filter code based on extracting packet
8380 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8381 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8383 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8388 * This is tricky. We need to insert the statements updating variable
8389 * parts of offsets before the the traditional TPID and VID tests so
8390 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8391 * we do not want this update to affect those checks. That's why we
8392 * generate both test blocks first and insert the statements updating
8393 * variable parts of both offsets after that. This wouldn't work if
8394 * there already were variable length link header when entering this
8395 * function but gen_vlan_bpf_extensions() isn't called in that case.
8397 b_tpid
= gen_vlan_tpid_test(cstate
);
8399 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8401 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8405 if (vlan_num
>= 0) {
8406 gen_vlan_patch_vid_test(cstate
, b_vid
);
8416 * support IEEE 802.1Q VLAN trunk over ethernet
8419 gen_vlan(compiler_state_t
*cstate
, int vlan_num
)
8423 /* can't check for VLAN-encapsulated packets inside MPLS */
8424 if (cstate
->label_stack_depth
> 0)
8425 bpf_error(cstate
, "no VLAN match after MPLS");
8428 * Check for a VLAN packet, and then change the offsets to point
8429 * to the type and data fields within the VLAN packet. Just
8430 * increment the offsets, so that we can support a hierarchy, e.g.
8431 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8434 * XXX - this is a bit of a kludge. If we were to split the
8435 * compiler into a parser that parses an expression and
8436 * generates an expression tree, and a code generator that
8437 * takes an expression tree (which could come from our
8438 * parser or from some other parser) and generates BPF code,
8439 * we could perhaps make the offsets parameters of routines
8440 * and, in the handler for an "AND" node, pass to subnodes
8441 * other than the VLAN node the adjusted offsets.
8443 * This would mean that "vlan" would, instead of changing the
8444 * behavior of *all* tests after it, change only the behavior
8445 * of tests ANDed with it. That would change the documented
8446 * semantics of "vlan", which might break some expressions.
8447 * However, it would mean that "(vlan and ip) or ip" would check
8448 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8449 * checking only for VLAN-encapsulated IP, so that could still
8450 * be considered worth doing; it wouldn't break expressions
8451 * that are of the form "vlan and ..." or "vlan N and ...",
8452 * which I suspect are the most common expressions involving
8453 * "vlan". "vlan or ..." doesn't necessarily do what the user
8454 * would really want, now, as all the "or ..." tests would
8455 * be done assuming a VLAN, even though the "or" could be viewed
8456 * as meaning "or, if this isn't a VLAN packet...".
8458 switch (cstate
->linktype
) {
8461 case DLT_NETANALYZER
:
8462 case DLT_NETANALYZER_TRANSPARENT
:
8463 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8464 /* Verify that this is the outer part of the packet and
8465 * not encapsulated somehow. */
8466 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8467 cstate
->off_linkhdr
.constant_part
==
8468 cstate
->off_outermostlinkhdr
.constant_part
) {
8470 * Do we need special VLAN handling?
8472 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8473 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
);
8475 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8478 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8481 case DLT_IEEE802_11
:
8482 case DLT_PRISM_HEADER
:
8483 case DLT_IEEE802_11_RADIO_AVS
:
8484 case DLT_IEEE802_11_RADIO
:
8485 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
);
8489 bpf_error(cstate
, "no VLAN support for data link type %d",
8494 cstate
->vlan_stack_depth
++;
8503 gen_mpls(compiler_state_t
*cstate
, int label_num
)
8505 struct block
*b0
, *b1
;
8507 if (cstate
->label_stack_depth
> 0) {
8508 /* just match the bottom-of-stack bit clear */
8509 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8512 * We're not in an MPLS stack yet, so check the link-layer
8513 * type against MPLS.
8515 switch (cstate
->linktype
) {
8517 case DLT_C_HDLC
: /* fall through */
8519 case DLT_NETANALYZER
:
8520 case DLT_NETANALYZER_TRANSPARENT
:
8521 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8525 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8528 /* FIXME add other DLT_s ...
8529 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8530 * leave it for now */
8533 bpf_error(cstate
, "no MPLS support for data link type %d",
8540 /* If a specific MPLS label is requested, check it */
8541 if (label_num
>= 0) {
8542 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8543 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8544 0xfffff000); /* only compare the first 20 bits */
8550 * Change the offsets to point to the type and data fields within
8551 * the MPLS packet. Just increment the offsets, so that we
8552 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8553 * capture packets with an outer label of 100000 and an inner
8556 * Increment the MPLS stack depth as well; this indicates that
8557 * we're checking MPLS-encapsulated headers, to make sure higher
8558 * level code generators don't try to match against IP-related
8559 * protocols such as Q_ARP, Q_RARP etc.
8561 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8563 cstate
->off_nl_nosnap
+= 4;
8564 cstate
->off_nl
+= 4;
8565 cstate
->label_stack_depth
++;
8570 * Support PPPOE discovery and session.
8573 gen_pppoed(compiler_state_t
*cstate
)
8575 /* check for PPPoE discovery */
8576 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8580 gen_pppoes(compiler_state_t
*cstate
, int sess_num
)
8582 struct block
*b0
, *b1
;
8585 * Test against the PPPoE session link-layer type.
8587 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8589 /* If a specific session is requested, check PPPoE session id */
8590 if (sess_num
>= 0) {
8591 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8592 (bpf_int32
)sess_num
, 0x0000ffff);
8598 * Change the offsets to point to the type and data fields within
8599 * the PPP packet, and note that this is PPPoE rather than
8602 * XXX - this is a bit of a kludge. If we were to split the
8603 * compiler into a parser that parses an expression and
8604 * generates an expression tree, and a code generator that
8605 * takes an expression tree (which could come from our
8606 * parser or from some other parser) and generates BPF code,
8607 * we could perhaps make the offsets parameters of routines
8608 * and, in the handler for an "AND" node, pass to subnodes
8609 * other than the PPPoE node the adjusted offsets.
8611 * This would mean that "pppoes" would, instead of changing the
8612 * behavior of *all* tests after it, change only the behavior
8613 * of tests ANDed with it. That would change the documented
8614 * semantics of "pppoes", which might break some expressions.
8615 * However, it would mean that "(pppoes and ip) or ip" would check
8616 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8617 * checking only for VLAN-encapsulated IP, so that could still
8618 * be considered worth doing; it wouldn't break expressions
8619 * that are of the form "pppoes and ..." which I suspect are the
8620 * most common expressions involving "pppoes". "pppoes or ..."
8621 * doesn't necessarily do what the user would really want, now,
8622 * as all the "or ..." tests would be done assuming PPPoE, even
8623 * though the "or" could be viewed as meaning "or, if this isn't
8624 * a PPPoE packet...".
8626 * The "network-layer" protocol is PPPoE, which has a 6-byte
8627 * PPPoE header, followed by a PPP packet.
8629 * There is no HDLC encapsulation for the PPP packet (it's
8630 * encapsulated in PPPoES instead), so the link-layer type
8631 * starts at the first byte of the PPP packet. For PPPoE,
8632 * that offset is relative to the beginning of the total
8633 * link-layer payload, including any 802.2 LLC header, so
8634 * it's 6 bytes past cstate->off_nl.
8636 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
8637 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8638 cstate
->off_linkpl
.reg
);
8640 cstate
->off_linktype
= cstate
->off_linkhdr
;
8641 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
8644 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8649 /* Check that this is Geneve and the VNI is correct if
8650 * specified. Parameterized to handle both IPv4 and IPv6. */
8651 static struct block
*
8652 gen_geneve_check(compiler_state_t
*cstate
,
8653 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8654 enum e_offrel offrel
, int vni
)
8656 struct block
*b0
, *b1
;
8658 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8660 /* Check that we are operating on version 0. Otherwise, we
8661 * can't decode the rest of the fields. The version is 2 bits
8662 * in the first byte of the Geneve header. */
8663 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8668 vni
<<= 8; /* VNI is in the upper 3 bytes */
8669 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8678 /* The IPv4 and IPv6 Geneve checks need to do two things:
8679 * - Verify that this actually is Geneve with the right VNI.
8680 * - Place the IP header length (plus variable link prefix if
8681 * needed) into register A to be used later to compute
8682 * the inner packet offsets. */
8683 static struct block
*
8684 gen_geneve4(compiler_state_t
*cstate
, int vni
)
8686 struct block
*b0
, *b1
;
8687 struct slist
*s
, *s1
;
8689 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
);
8691 /* Load the IP header length into A. */
8692 s
= gen_loadx_iphdrlen(cstate
);
8694 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8697 /* Forcibly append these statements to the true condition
8698 * of the protocol check by creating a new block that is
8699 * always true and ANDing them. */
8700 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8709 static struct block
*
8710 gen_geneve6(compiler_state_t
*cstate
, int vni
)
8712 struct block
*b0
, *b1
;
8713 struct slist
*s
, *s1
;
8715 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
);
8717 /* Load the IP header length. We need to account for a
8718 * variable length link prefix if there is one. */
8719 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
8721 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8725 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8729 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8733 /* Forcibly append these statements to the true condition
8734 * of the protocol check by creating a new block that is
8735 * always true and ANDing them. */
8736 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8739 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8748 /* We need to store three values based on the Geneve header::
8749 * - The offset of the linktype.
8750 * - The offset of the end of the Geneve header.
8751 * - The offset of the end of the encapsulated MAC header. */
8752 static struct slist
*
8753 gen_geneve_offsets(compiler_state_t
*cstate
)
8755 struct slist
*s
, *s1
, *s_proto
;
8757 /* First we need to calculate the offset of the Geneve header
8758 * itself. This is composed of the IP header previously calculated
8759 * (include any variable link prefix) and stored in A plus the
8760 * fixed sized headers (fixed link prefix, MAC length, and UDP
8762 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8763 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
8765 /* Stash this in X since we'll need it later. */
8766 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8769 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8771 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8775 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
8776 cstate
->off_linktype
.is_variable
= 1;
8777 cstate
->off_linktype
.constant_part
= 0;
8779 s1
= new_stmt(cstate
, BPF_ST
);
8780 s1
->s
.k
= cstate
->off_linktype
.reg
;
8783 /* Load the Geneve option length and mask and shift to get the
8784 * number of bytes. It is stored in the first byte of the Geneve
8786 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8790 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8794 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8798 /* Add in the rest of the Geneve base header. */
8799 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8803 /* Add the Geneve header length to its offset and store. */
8804 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8808 /* Set the encapsulated type as Ethernet. Even though we may
8809 * not actually have Ethernet inside there are two reasons this
8811 * - The linktype field is always in EtherType format regardless
8812 * of whether it is in Geneve or an inner Ethernet frame.
8813 * - The only link layer that we have specific support for is
8814 * Ethernet. We will confirm that the packet actually is
8815 * Ethernet at runtime before executing these checks. */
8816 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8818 s1
= new_stmt(cstate
, BPF_ST
);
8819 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8822 /* Calculate whether we have an Ethernet header or just raw IP/
8823 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8824 * and linktype by 14 bytes so that the network header can be found
8825 * seamlessly. Otherwise, keep what we've calculated already. */
8827 /* We have a bare jmp so we can't use the optimizer. */
8828 cstate
->no_optimize
= 1;
8830 /* Load the EtherType in the Geneve header, 2 bytes in. */
8831 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8835 /* Load X with the end of the Geneve header. */
8836 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8837 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
8840 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8841 * end of this check, we should have the total length in X. In
8842 * the non-Ethernet case, it's already there. */
8843 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8844 s_proto
->s
.k
= ETHERTYPE_TEB
;
8845 sappend(s
, s_proto
);
8847 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8851 /* Since this is Ethernet, use the EtherType of the payload
8852 * directly as the linktype. Overwrite what we already have. */
8853 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8857 s1
= new_stmt(cstate
, BPF_ST
);
8858 s1
->s
.k
= cstate
->off_linktype
.reg
;
8861 /* Advance two bytes further to get the end of the Ethernet
8863 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8867 /* Move the result to X. */
8868 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8871 /* Store the final result of our linkpl calculation. */
8872 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
8873 cstate
->off_linkpl
.is_variable
= 1;
8874 cstate
->off_linkpl
.constant_part
= 0;
8876 s1
= new_stmt(cstate
, BPF_STX
);
8877 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8886 /* Check to see if this is a Geneve packet. */
8888 gen_geneve(compiler_state_t
*cstate
, int vni
)
8890 struct block
*b0
, *b1
;
8893 b0
= gen_geneve4(cstate
, vni
);
8894 b1
= gen_geneve6(cstate
, vni
);
8899 /* Later filters should act on the payload of the Geneve frame,
8900 * update all of the header pointers. Attach this code so that
8901 * it gets executed in the event that the Geneve filter matches. */
8902 s
= gen_geneve_offsets(cstate
);
8904 b1
= gen_true(cstate
);
8905 sappend(s
, b1
->stmts
);
8910 cstate
->is_geneve
= 1;
8915 /* Check that the encapsulated frame has a link layer header
8916 * for Ethernet filters. */
8917 static struct block
*
8918 gen_geneve_ll_check(compiler_state_t
*cstate
)
8921 struct slist
*s
, *s1
;
8923 /* The easiest way to see if there is a link layer present
8924 * is to check if the link layer header and payload are not
8927 /* Geneve always generates pure variable offsets so we can
8928 * compare only the registers. */
8929 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8930 s
->s
.k
= cstate
->off_linkhdr
.reg
;
8932 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8933 s1
->s
.k
= cstate
->off_linkpl
.reg
;
8936 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8945 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
8946 bpf_u_int32 jtype
, int reverse
)
8953 if (!cstate
->is_atm
)
8954 bpf_error(cstate
, "'vpi' supported only on raw ATM");
8955 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
8957 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
8962 if (!cstate
->is_atm
)
8963 bpf_error(cstate
, "'vci' supported only on raw ATM");
8964 if (cstate
->off_vci
== OFFSET_NOT_SET
)
8966 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
8971 if (cstate
->off_proto
== OFFSET_NOT_SET
)
8972 abort(); /* XXX - this isn't on FreeBSD */
8973 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
8978 if (cstate
->off_payload
== OFFSET_NOT_SET
)
8980 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
8981 0xffffffff, jtype
, reverse
, jvalue
);
8985 if (!cstate
->is_atm
)
8986 bpf_error(cstate
, "'callref' supported only on raw ATM");
8987 if (cstate
->off_proto
== OFFSET_NOT_SET
)
8989 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
8990 jtype
, reverse
, jvalue
);
9000 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9002 struct block
*b0
, *b1
;
9007 /* Get all packets in Meta signalling Circuit */
9008 if (!cstate
->is_atm
)
9009 bpf_error(cstate
, "'metac' supported only on raw ATM");
9010 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9011 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9016 /* Get all packets in Broadcast Circuit*/
9017 if (!cstate
->is_atm
)
9018 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9019 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9020 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9025 /* Get all cells in Segment OAM F4 circuit*/
9026 if (!cstate
->is_atm
)
9027 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9028 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9029 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9034 /* Get all cells in End-to-End OAM F4 Circuit*/
9035 if (!cstate
->is_atm
)
9036 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9037 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9038 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9043 /* Get all packets in connection Signalling Circuit */
9044 if (!cstate
->is_atm
)
9045 bpf_error(cstate
, "'sc' supported only on raw ATM");
9046 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9047 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9052 /* Get all packets in ILMI Circuit */
9053 if (!cstate
->is_atm
)
9054 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9055 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9056 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9061 /* Get all LANE packets */
9062 if (!cstate
->is_atm
)
9063 bpf_error(cstate
, "'lane' supported only on raw ATM");
9064 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9067 * Arrange that all subsequent tests assume LANE
9068 * rather than LLC-encapsulated packets, and set
9069 * the offsets appropriately for LANE-encapsulated
9072 * We assume LANE means Ethernet, not Token Ring.
9074 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9075 cstate
->off_payload
+ 2, /* Ethernet header */
9077 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9078 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9079 cstate
->off_nl
= 0; /* Ethernet II */
9080 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9084 /* Get all LLC-encapsulated packets */
9085 if (!cstate
->is_atm
)
9086 bpf_error(cstate
, "'llc' supported only on raw ATM");
9087 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9088 cstate
->linktype
= cstate
->prevlinktype
;
9098 * Filtering for MTP2 messages based on li value
9099 * FISU, length is null
9100 * LSSU, length is 1 or 2
9101 * MSU, length is 3 or more
9102 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9105 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9107 struct block
*b0
, *b1
;
9112 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9113 (cstate
->linktype
!= DLT_ERF
) &&
9114 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9115 bpf_error(cstate
, "'fisu' supported only on MTP2");
9116 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9117 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
9121 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9122 (cstate
->linktype
!= DLT_ERF
) &&
9123 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9124 bpf_error(cstate
, "'lssu' supported only on MTP2");
9125 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
9126 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9131 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9132 (cstate
->linktype
!= DLT_ERF
) &&
9133 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9134 bpf_error(cstate
, "'msu' supported only on MTP2");
9135 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9139 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9140 (cstate
->linktype
!= DLT_ERF
) &&
9141 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9142 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9143 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9144 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9148 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9149 (cstate
->linktype
!= DLT_ERF
) &&
9150 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9151 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9152 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9153 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9158 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9159 (cstate
->linktype
!= DLT_ERF
) &&
9160 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9161 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9162 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9172 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
9173 bpf_u_int32 jtype
, int reverse
)
9176 bpf_u_int32 val1
, val2
, val3
;
9177 u_int newoff_sio
= cstate
->off_sio
;
9178 u_int newoff_opc
= cstate
->off_opc
;
9179 u_int newoff_dpc
= cstate
->off_dpc
;
9180 u_int newoff_sls
= cstate
->off_sls
;
9182 switch (mtp3field
) {
9185 newoff_sio
+= 3; /* offset for MTP2_HSL */
9189 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9190 bpf_error(cstate
, "'sio' supported only on SS7");
9191 /* sio coded on 1 byte so max value 255 */
9193 bpf_error(cstate
, "sio value %u too big; max value = 255",
9195 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9196 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9202 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9203 bpf_error(cstate
, "'opc' supported only on SS7");
9204 /* opc coded on 14 bits so max value 16383 */
9206 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9208 /* the following instructions are made to convert jvalue
9209 * to the form used to write opc in an ss7 message*/
9210 val1
= jvalue
& 0x00003c00;
9212 val2
= jvalue
& 0x000003fc;
9214 val3
= jvalue
& 0x00000003;
9216 jvalue
= val1
+ val2
+ val3
;
9217 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9218 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9226 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9227 bpf_error(cstate
, "'dpc' supported only on SS7");
9228 /* dpc coded on 14 bits so max value 16383 */
9230 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9232 /* the following instructions are made to convert jvalue
9233 * to the forme used to write dpc in an ss7 message*/
9234 val1
= jvalue
& 0x000000ff;
9236 val2
= jvalue
& 0x00003f00;
9238 jvalue
= val1
+ val2
;
9239 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9240 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9246 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9247 bpf_error(cstate
, "'sls' supported only on SS7");
9248 /* sls coded on 4 bits so max value 15 */
9250 bpf_error(cstate
, "sls value %u too big; max value = 15",
9252 /* the following instruction is made to convert jvalue
9253 * to the forme used to write sls in an ss7 message*/
9254 jvalue
= jvalue
<< 4;
9255 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9256 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9265 static struct block
*
9266 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9271 * Q.2931 signalling protocol messages for handling virtual circuits
9272 * establishment and teardown
9277 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9281 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9285 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9289 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9293 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9296 case A_RELEASE_DONE
:
9297 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9307 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9309 struct block
*b0
, *b1
;
9314 if (!cstate
->is_atm
)
9315 bpf_error(cstate
, "'oam' supported only on raw ATM");
9316 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
9320 if (!cstate
->is_atm
)
9321 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9323 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9324 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9326 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9332 * Get Q.2931 signalling messages for switched
9333 * virtual connection
9335 if (!cstate
->is_atm
)
9336 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9337 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9338 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9340 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9342 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9344 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9346 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9348 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9353 if (!cstate
->is_atm
)
9354 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9355 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9356 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9358 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9360 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9362 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9364 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);