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
{
259 int outermostlinktype
;
264 /* Hack for handling VLAN and MPLS stacks. */
265 u_int label_stack_depth
;
266 u_int vlan_stack_depth
;
272 * As errors are handled by a longjmp, anything allocated must
273 * be freed in the longjmp handler, so it must be reachable
276 * One thing that's allocated is the result of pcap_nametoaddrinfo();
277 * it must be freed with freeaddrinfo(). This variable points to
278 * any addrinfo structure that would need to be freed.
283 * Another thing that's allocated is the result of pcap_ether_aton();
284 * it must be freed with free(). This variable points to any
285 * address that would need to be freed.
290 * Various code constructs need to know the layout of the packet.
291 * These values give the necessary offsets from the beginning
292 * of the packet data.
296 * Absolute offset of the beginning of the link-layer header.
298 bpf_abs_offset off_linkhdr
;
301 * If we're checking a link-layer header for a packet encapsulated
302 * in another protocol layer, this is the equivalent information
303 * for the previous layers' link-layer header from the beginning
304 * of the raw packet data.
306 bpf_abs_offset off_prevlinkhdr
;
309 * This is the equivalent information for the outermost layers'
312 bpf_abs_offset off_outermostlinkhdr
;
315 * Absolute offset of the beginning of the link-layer payload.
317 bpf_abs_offset off_linkpl
;
320 * "off_linktype" is the offset to information in the link-layer
321 * header giving the packet type. This is an absolute offset
322 * from the beginning of the packet.
324 * For Ethernet, it's the offset of the Ethernet type field; this
325 * means that it must have a value that skips VLAN tags.
327 * For link-layer types that always use 802.2 headers, it's the
328 * offset of the LLC header; this means that it must have a value
329 * that skips VLAN tags.
331 * For PPP, it's the offset of the PPP type field.
333 * For Cisco HDLC, it's the offset of the CHDLC type field.
335 * For BSD loopback, it's the offset of the AF_ value.
337 * For Linux cooked sockets, it's the offset of the type field.
339 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
340 * encapsulation, in which case, IP is assumed.
342 bpf_abs_offset off_linktype
;
345 * TRUE if the link layer includes an ATM pseudo-header.
350 * TRUE if "geneve" appeared in the filter; it causes us to
351 * generate code that checks for a Geneve header and assume
352 * that later filters apply to the encapsulated payload.
357 * TRUE if we need variable length part of VLAN offset
359 int is_vlan_vloffset
;
362 * These are offsets for the ATM pseudo-header.
369 * These are offsets for the MTP2 fields.
375 * These are offsets for the MTP3 fields.
383 * This is the offset of the first byte after the ATM pseudo_header,
384 * or -1 if there is no ATM pseudo-header.
389 * These are offsets to the beginning of the network-layer header.
390 * They are relative to the beginning of the link-layer payload
391 * (i.e., they don't include off_linkhdr.constant_part or
392 * off_linkpl.constant_part).
394 * If the link layer never uses 802.2 LLC:
396 * "off_nl" and "off_nl_nosnap" are the same.
398 * If the link layer always uses 802.2 LLC:
400 * "off_nl" is the offset if there's a SNAP header following
403 * "off_nl_nosnap" is the offset if there's no SNAP header.
405 * If the link layer is Ethernet:
407 * "off_nl" is the offset if the packet is an Ethernet II packet
408 * (we assume no 802.3+802.2+SNAP);
410 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
411 * with an 802.2 header following it.
417 * Here we handle simple allocation of the scratch registers.
418 * If too many registers are alloc'd, the allocator punts.
420 int regused
[BPF_MEMWORDS
];
426 struct chunk chunks
[NCHUNKS
];
431 * For use by routines outside this file.
435 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
440 * If we've already set an error, don't override it.
441 * The lexical analyzer reports some errors by setting
442 * the error and then returning a LEX_ERROR token, which
443 * is not recognized by any grammar rule, and thus forces
444 * the parse to stop. We don't want the error reported
445 * by the lexical analyzer to be overwritten by the syntax
448 if (!cstate
->error_set
) {
450 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
453 cstate
->error_set
= 1;
458 * For use *ONLY* in routines in this file.
460 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
461 PCAP_PRINTFLIKE(2, 3);
464 static void PCAP_NORETURN
465 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
470 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
473 longjmp(cstate
->top_ctx
, 1);
477 static int init_linktype(compiler_state_t
*, pcap_t
*);
479 static void init_regs(compiler_state_t
*);
480 static int alloc_reg(compiler_state_t
*);
481 static void free_reg(compiler_state_t
*, int);
483 static void initchunks(compiler_state_t
*cstate
);
484 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
485 static void *newchunk(compiler_state_t
*cstate
, size_t);
486 static void freechunks(compiler_state_t
*cstate
);
487 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
488 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
489 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
490 static inline void syntax(compiler_state_t
*cstate
);
492 static void backpatch(struct block
*, struct block
*);
493 static void merge(struct block
*, struct block
*);
494 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
496 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
498 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
500 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
502 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
504 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
505 u_int
, bpf_u_int32
, bpf_u_int32
);
506 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
507 u_int
, const u_char
*);
508 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
509 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
510 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
512 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
514 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
515 static struct block
*gen_uncond(compiler_state_t
*, int);
516 static inline struct block
*gen_true(compiler_state_t
*);
517 static inline struct block
*gen_false(compiler_state_t
*);
518 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
519 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
520 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
521 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
522 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
523 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
524 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
525 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
526 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
528 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
529 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
530 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
531 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
532 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
533 int, bpf_u_int32
, u_int
, u_int
);
535 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
536 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
538 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
539 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
540 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
541 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
542 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
543 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
544 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
545 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
546 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
549 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
550 struct in6_addr
*, int, int, int);
553 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
554 struct addrinfo
*, int, int);
556 static struct block
*gen_ipfrag(compiler_state_t
*);
557 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
558 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
560 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
561 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
563 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
564 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
565 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
567 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
568 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
569 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
570 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
572 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
573 static int lookup_proto(compiler_state_t
*, const char *, int);
574 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
575 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
576 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
577 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
578 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
579 static struct block
*gen_len(compiler_state_t
*, int, int);
580 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
581 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
583 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
584 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
585 bpf_u_int32
, int, int);
586 static struct block
*gen_atmtype_llc(compiler_state_t
*);
587 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
590 initchunks(compiler_state_t
*cstate
)
594 for (i
= 0; i
< NCHUNKS
; i
++) {
595 cstate
->chunks
[i
].n_left
= 0;
596 cstate
->chunks
[i
].m
= NULL
;
598 cstate
->cur_chunk
= 0;
602 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
609 /* XXX Round up to nearest long. */
610 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
612 /* XXX Round up to structure boundary. */
616 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
617 if (n
> cp
->n_left
) {
619 k
= ++cstate
->cur_chunk
;
621 bpf_set_error(cstate
, "out of memory");
624 size
= CHUNK0SIZE
<< k
;
625 cp
->m
= (void *)malloc(size
);
627 bpf_set_error(cstate
, "out of memory");
630 memset((char *)cp
->m
, 0, size
);
633 bpf_set_error(cstate
, "out of memory");
638 return (void *)((char *)cp
->m
+ cp
->n_left
);
642 newchunk(compiler_state_t
*cstate
, size_t n
)
646 p
= newchunk_nolongjmp(cstate
, n
);
648 longjmp(cstate
->top_ctx
, 1);
655 freechunks(compiler_state_t
*cstate
)
659 for (i
= 0; i
< NCHUNKS
; ++i
)
660 if (cstate
->chunks
[i
].m
!= NULL
)
661 free(cstate
->chunks
[i
].m
);
665 * A strdup whose allocations are freed after code generation is over.
666 * This is used by the lexical analyzer, so it can't longjmp; it just
667 * returns NULL on an allocation error, and the callers must check
671 sdup(compiler_state_t
*cstate
, const char *s
)
673 size_t n
= strlen(s
) + 1;
674 char *cp
= newchunk_nolongjmp(cstate
, n
);
678 pcap_strlcpy(cp
, s
, n
);
682 static inline struct block
*
683 new_block(compiler_state_t
*cstate
, int code
)
687 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
694 static inline struct slist
*
695 new_stmt(compiler_state_t
*cstate
, int code
)
699 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
705 static struct block
*
706 gen_retblk(compiler_state_t
*cstate
, int v
)
708 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
714 static inline PCAP_NORETURN_DEF
void
715 syntax(compiler_state_t
*cstate
)
717 bpf_error(cstate
, "syntax error in filter expression");
721 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
722 const char *buf
, int optimize
, bpf_u_int32 mask
)
727 compiler_state_t cstate
;
728 const char * volatile xbuf
= buf
;
729 yyscan_t scanner
= NULL
;
730 volatile YY_BUFFER_STATE in_buffer
= NULL
;
735 * If this pcap_t hasn't been activated, it doesn't have a
736 * link-layer type, so we can't use it.
739 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
740 "not-yet-activated pcap_t passed to pcap_compile");
752 * If the device on which we're capturing need to be notified
753 * that a new filter is being compiled, do so.
755 * This allows them to save a copy of it, in case, for example,
756 * they're implementing a form of remote packet capture, and
757 * want the remote machine to filter out the packets in which
758 * it's sending the packets it's captured.
760 * XXX - the fact that we happen to be compiling a filter
761 * doesn't necessarily mean we'll be installing it as the
762 * filter for this pcap_t; we might be running it from userland
763 * on captured packets to do packet classification. We really
764 * need a better way of handling this, but this is all that
765 * the WinPcap remote capture code did.
767 if (p
->save_current_filter_op
!= NULL
)
768 (p
->save_current_filter_op
)(p
, buf
);
772 cstate
.no_optimize
= 0;
777 cstate
.ic
.root
= NULL
;
778 cstate
.ic
.cur_mark
= 0;
780 cstate
.error_set
= 0;
783 cstate
.netmask
= mask
;
785 cstate
.snaplen
= pcap_snapshot(p
);
786 if (cstate
.snaplen
== 0) {
787 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
788 "snaplen of 0 rejects all packets");
793 if (pcap_lex_init(&scanner
) != 0)
794 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
795 errno
, "can't initialize scanner");
796 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
799 * Associate the compiler state with the lexical analyzer
802 pcap_set_extra(&cstate
, scanner
);
804 if (init_linktype(&cstate
, p
) == -1) {
808 if (pcap_parse(scanner
, &cstate
) != 0) {
810 if (cstate
.ai
!= NULL
)
811 freeaddrinfo(cstate
.ai
);
813 if (cstate
.e
!= NULL
)
819 if (cstate
.ic
.root
== NULL
) {
821 * Catch errors reported by gen_retblk().
823 if (setjmp(cstate
.top_ctx
)) {
827 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
830 if (optimize
&& !cstate
.no_optimize
) {
831 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
836 if (cstate
.ic
.root
== NULL
||
837 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
838 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
839 "expression rejects all packets");
844 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
845 cstate
.ic
.root
, &len
, p
->errbuf
);
846 if (program
->bf_insns
== NULL
) {
851 program
->bf_len
= len
;
853 rc
= 0; /* We're all okay */
857 * Clean up everything for the lexical analyzer.
859 if (in_buffer
!= NULL
)
860 pcap__delete_buffer(in_buffer
, scanner
);
862 pcap_lex_destroy(scanner
);
865 * Clean up our own allocated memory.
873 * entry point for using the compiler with no pcap open
874 * pass in all the stuff that is needed explicitly instead.
877 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
878 struct bpf_program
*program
,
879 const char *buf
, int optimize
, bpf_u_int32 mask
)
884 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
887 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
893 * Clean up a "struct bpf_program" by freeing all the memory allocated
897 pcap_freecode(struct bpf_program
*program
)
900 if (program
->bf_insns
!= NULL
) {
901 free((char *)program
->bf_insns
);
902 program
->bf_insns
= NULL
;
907 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
908 * which of the jt and jf fields has been resolved and which is a pointer
909 * back to another unresolved block (or nil). At least one of the fields
910 * in each block is already resolved.
913 backpatch(struct block
*list
, struct block
*target
)
930 * Merge the lists in b0 and b1, using the 'sense' field to indicate
931 * which of jt and jf is the link.
934 merge(struct block
*b0
, struct block
*b1
)
936 register struct block
**p
= &b0
;
938 /* Find end of list. */
940 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
942 /* Concatenate the lists. */
947 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
949 struct block
*ppi_dlt_check
;
952 * Catch errors reported by us and routines below us, and return -1
955 if (setjmp(cstate
->top_ctx
))
959 * Insert before the statements of the first (root) block any
960 * statements needed to load the lengths of any variable-length
961 * headers into registers.
963 * XXX - a fancier strategy would be to insert those before the
964 * statements of all blocks that use those lengths and that
965 * have no predecessors that use them, so that we only compute
966 * the lengths if we need them. There might be even better
967 * approaches than that.
969 * However, those strategies would be more complicated, and
970 * as we don't generate code to compute a length if the
971 * program has no tests that use the length, and as most
972 * tests will probably use those lengths, we would just
973 * postpone computing the lengths so that it's not done
974 * for tests that fail early, and it's not clear that's
977 insert_compute_vloffsets(cstate
, p
->head
);
980 * For DLT_PPI captures, generate a check of the per-packet
981 * DLT value to make sure it's DLT_IEEE802_11.
983 * XXX - TurboCap cards use DLT_PPI for Ethernet.
984 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
985 * with appropriate Ethernet information and use that rather
986 * than using something such as DLT_PPI where you don't know
987 * the link-layer header type until runtime, which, in the
988 * general case, would force us to generate both Ethernet *and*
989 * 802.11 code (*and* anything else for which PPI is used)
990 * and choose between them early in the BPF program?
992 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
993 if (ppi_dlt_check
!= NULL
)
994 gen_and(ppi_dlt_check
, p
);
996 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
997 p
->sense
= !p
->sense
;
998 backpatch(p
, gen_retblk(cstate
, 0));
999 cstate
->ic
.root
= p
->head
;
1004 gen_and(struct block
*b0
, struct block
*b1
)
1006 backpatch(b0
, b1
->head
);
1007 b0
->sense
= !b0
->sense
;
1008 b1
->sense
= !b1
->sense
;
1010 b1
->sense
= !b1
->sense
;
1011 b1
->head
= b0
->head
;
1015 gen_or(struct block
*b0
, struct block
*b1
)
1017 b0
->sense
= !b0
->sense
;
1018 backpatch(b0
, b1
->head
);
1019 b0
->sense
= !b0
->sense
;
1021 b1
->head
= b0
->head
;
1025 gen_not(struct block
*b
)
1027 b
->sense
= !b
->sense
;
1030 static struct block
*
1031 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1032 u_int size
, bpf_u_int32 v
)
1034 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1037 static struct block
*
1038 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1039 u_int size
, bpf_u_int32 v
)
1041 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1044 static struct block
*
1045 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1046 u_int size
, bpf_u_int32 v
)
1048 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1051 static struct block
*
1052 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1053 u_int size
, bpf_u_int32 v
)
1055 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1058 static struct block
*
1059 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1060 u_int size
, bpf_u_int32 v
)
1062 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1065 static struct block
*
1066 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1067 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1069 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1072 static struct block
*
1073 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1074 u_int size
, const u_char
*v
)
1076 register struct block
*b
, *tmp
;
1080 register const u_char
*p
= &v
[size
- 4];
1082 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1090 register const u_char
*p
= &v
[size
- 2];
1092 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1100 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1109 * AND the field of size "size" at offset "offset" relative to the header
1110 * specified by "offrel" with "mask", and compare it with the value "v"
1111 * with the test specified by "jtype"; if "reverse" is true, the test
1112 * should test the opposite of "jtype".
1114 static struct block
*
1115 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1116 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1119 struct slist
*s
, *s2
;
1122 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1124 if (mask
!= 0xffffffff) {
1125 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1130 b
= new_block(cstate
, JMP(jtype
));
1133 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1139 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1141 cstate
->pcap_fddipad
= p
->fddipad
;
1144 * We start out with only one link-layer header.
1146 cstate
->outermostlinktype
= pcap_datalink(p
);
1147 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1148 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1149 cstate
->off_outermostlinkhdr
.reg
= -1;
1151 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1152 cstate
->off_prevlinkhdr
.constant_part
= 0;
1153 cstate
->off_prevlinkhdr
.is_variable
= 0;
1154 cstate
->off_prevlinkhdr
.reg
= -1;
1156 cstate
->linktype
= cstate
->outermostlinktype
;
1157 cstate
->off_linkhdr
.constant_part
= 0;
1158 cstate
->off_linkhdr
.is_variable
= 0;
1159 cstate
->off_linkhdr
.reg
= -1;
1164 cstate
->off_linkpl
.constant_part
= 0;
1165 cstate
->off_linkpl
.is_variable
= 0;
1166 cstate
->off_linkpl
.reg
= -1;
1168 cstate
->off_linktype
.constant_part
= 0;
1169 cstate
->off_linktype
.is_variable
= 0;
1170 cstate
->off_linktype
.reg
= -1;
1173 * Assume it's not raw ATM with a pseudo-header, for now.
1176 cstate
->off_vpi
= OFFSET_NOT_SET
;
1177 cstate
->off_vci
= OFFSET_NOT_SET
;
1178 cstate
->off_proto
= OFFSET_NOT_SET
;
1179 cstate
->off_payload
= OFFSET_NOT_SET
;
1184 cstate
->is_geneve
= 0;
1187 * No variable length VLAN offset by default
1189 cstate
->is_vlan_vloffset
= 0;
1192 * And assume we're not doing SS7.
1194 cstate
->off_li
= OFFSET_NOT_SET
;
1195 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1196 cstate
->off_sio
= OFFSET_NOT_SET
;
1197 cstate
->off_opc
= OFFSET_NOT_SET
;
1198 cstate
->off_dpc
= OFFSET_NOT_SET
;
1199 cstate
->off_sls
= OFFSET_NOT_SET
;
1201 cstate
->label_stack_depth
= 0;
1202 cstate
->vlan_stack_depth
= 0;
1204 switch (cstate
->linktype
) {
1207 cstate
->off_linktype
.constant_part
= 2;
1208 cstate
->off_linkpl
.constant_part
= 6;
1209 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1210 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1213 case DLT_ARCNET_LINUX
:
1214 cstate
->off_linktype
.constant_part
= 4;
1215 cstate
->off_linkpl
.constant_part
= 8;
1216 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1217 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1221 cstate
->off_linktype
.constant_part
= 12;
1222 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1223 cstate
->off_nl
= 0; /* Ethernet II */
1224 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1229 * SLIP doesn't have a link level type. The 16 byte
1230 * header is hacked into our SLIP driver.
1232 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1233 cstate
->off_linkpl
.constant_part
= 16;
1235 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1238 case DLT_SLIP_BSDOS
:
1239 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1240 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1242 cstate
->off_linkpl
.constant_part
= 24;
1244 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1249 cstate
->off_linktype
.constant_part
= 0;
1250 cstate
->off_linkpl
.constant_part
= 4;
1252 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1256 cstate
->off_linktype
.constant_part
= 0;
1257 cstate
->off_linkpl
.constant_part
= 12;
1259 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1264 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1265 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1266 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1267 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1269 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1274 * This does no include the Ethernet header, and
1275 * only covers session state.
1277 cstate
->off_linktype
.constant_part
= 6;
1278 cstate
->off_linkpl
.constant_part
= 8;
1280 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1284 cstate
->off_linktype
.constant_part
= 5;
1285 cstate
->off_linkpl
.constant_part
= 24;
1287 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1292 * FDDI doesn't really have a link-level type field.
1293 * We set "off_linktype" to the offset of the LLC header.
1295 * To check for Ethernet types, we assume that SSAP = SNAP
1296 * is being used and pick out the encapsulated Ethernet type.
1297 * XXX - should we generate code to check for SNAP?
1299 cstate
->off_linktype
.constant_part
= 13;
1300 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1301 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1302 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1303 cstate
->off_nl
= 8; /* 802.2+SNAP */
1304 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1309 * Token Ring doesn't really have a link-level type field.
1310 * We set "off_linktype" to the offset of the LLC header.
1312 * To check for Ethernet types, we assume that SSAP = SNAP
1313 * is being used and pick out the encapsulated Ethernet type.
1314 * XXX - should we generate code to check for SNAP?
1316 * XXX - the header is actually variable-length.
1317 * Some various Linux patched versions gave 38
1318 * as "off_linktype" and 40 as "off_nl"; however,
1319 * if a token ring packet has *no* routing
1320 * information, i.e. is not source-routed, the correct
1321 * values are 20 and 22, as they are in the vanilla code.
1323 * A packet is source-routed iff the uppermost bit
1324 * of the first byte of the source address, at an
1325 * offset of 8, has the uppermost bit set. If the
1326 * packet is source-routed, the total number of bytes
1327 * of routing information is 2 plus bits 0x1F00 of
1328 * the 16-bit value at an offset of 14 (shifted right
1329 * 8 - figure out which byte that is).
1331 cstate
->off_linktype
.constant_part
= 14;
1332 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1333 cstate
->off_nl
= 8; /* 802.2+SNAP */
1334 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1337 case DLT_PRISM_HEADER
:
1338 case DLT_IEEE802_11_RADIO_AVS
:
1339 case DLT_IEEE802_11_RADIO
:
1340 cstate
->off_linkhdr
.is_variable
= 1;
1341 /* Fall through, 802.11 doesn't have a variable link
1342 * prefix but is otherwise the same. */
1345 case DLT_IEEE802_11
:
1347 * 802.11 doesn't really have a link-level type field.
1348 * We set "off_linktype.constant_part" to the offset of
1351 * To check for Ethernet types, we assume that SSAP = SNAP
1352 * is being used and pick out the encapsulated Ethernet type.
1353 * XXX - should we generate code to check for SNAP?
1355 * We also handle variable-length radio headers here.
1356 * The Prism header is in theory variable-length, but in
1357 * practice it's always 144 bytes long. However, some
1358 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1359 * sometimes or always supply an AVS header, so we
1360 * have to check whether the radio header is a Prism
1361 * header or an AVS header, so, in practice, it's
1364 cstate
->off_linktype
.constant_part
= 24;
1365 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1366 cstate
->off_linkpl
.is_variable
= 1;
1367 cstate
->off_nl
= 8; /* 802.2+SNAP */
1368 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1373 * At the moment we treat PPI the same way that we treat
1374 * normal Radiotap encoded packets. The difference is in
1375 * the function that generates the code at the beginning
1376 * to compute the header length. Since this code generator
1377 * of PPI supports bare 802.11 encapsulation only (i.e.
1378 * the encapsulated DLT should be DLT_IEEE802_11) we
1379 * generate code to check for this too.
1381 cstate
->off_linktype
.constant_part
= 24;
1382 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1383 cstate
->off_linkpl
.is_variable
= 1;
1384 cstate
->off_linkhdr
.is_variable
= 1;
1385 cstate
->off_nl
= 8; /* 802.2+SNAP */
1386 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1389 case DLT_ATM_RFC1483
:
1390 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1392 * assume routed, non-ISO PDUs
1393 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1395 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1396 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1397 * latter would presumably be treated the way PPPoE
1398 * should be, so you can do "pppoe and udp port 2049"
1399 * or "pppoa and tcp port 80" and have it check for
1400 * PPPo{A,E} and a PPP protocol of IP and....
1402 cstate
->off_linktype
.constant_part
= 0;
1403 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1404 cstate
->off_nl
= 8; /* 802.2+SNAP */
1405 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1410 * Full Frontal ATM; you get AALn PDUs with an ATM
1414 cstate
->off_vpi
= SUNATM_VPI_POS
;
1415 cstate
->off_vci
= SUNATM_VCI_POS
;
1416 cstate
->off_proto
= PROTO_POS
;
1417 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1418 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1419 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1420 cstate
->off_nl
= 8; /* 802.2+SNAP */
1421 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1427 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1428 cstate
->off_linkpl
.constant_part
= 0;
1430 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1433 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1434 cstate
->off_linktype
.constant_part
= 14;
1435 cstate
->off_linkpl
.constant_part
= 16;
1437 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1440 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1441 cstate
->off_linktype
.constant_part
= 0;
1442 cstate
->off_linkpl
.constant_part
= 20;
1444 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1449 * LocalTalk does have a 1-byte type field in the LLAP header,
1450 * but really it just indicates whether there is a "short" or
1451 * "long" DDP packet following.
1453 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1454 cstate
->off_linkpl
.constant_part
= 0;
1456 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1459 case DLT_IP_OVER_FC
:
1461 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1462 * link-level type field. We set "off_linktype" to the
1463 * offset of the LLC header.
1465 * To check for Ethernet types, we assume that SSAP = SNAP
1466 * is being used and pick out the encapsulated Ethernet type.
1467 * XXX - should we generate code to check for SNAP? RFC
1468 * 2625 says SNAP should be used.
1470 cstate
->off_linktype
.constant_part
= 16;
1471 cstate
->off_linkpl
.constant_part
= 16;
1472 cstate
->off_nl
= 8; /* 802.2+SNAP */
1473 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1478 * XXX - we should set this to handle SNAP-encapsulated
1479 * frames (NLPID of 0x80).
1481 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1482 cstate
->off_linkpl
.constant_part
= 0;
1484 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1488 * the only BPF-interesting FRF.16 frames are non-control frames;
1489 * Frame Relay has a variable length link-layer
1490 * so lets start with offset 4 for now and increments later on (FIXME);
1493 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1494 cstate
->off_linkpl
.constant_part
= 0;
1496 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1499 case DLT_APPLE_IP_OVER_IEEE1394
:
1500 cstate
->off_linktype
.constant_part
= 16;
1501 cstate
->off_linkpl
.constant_part
= 18;
1503 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1506 case DLT_SYMANTEC_FIREWALL
:
1507 cstate
->off_linktype
.constant_part
= 6;
1508 cstate
->off_linkpl
.constant_part
= 44;
1509 cstate
->off_nl
= 0; /* Ethernet II */
1510 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1513 #ifdef HAVE_NET_PFVAR_H
1515 cstate
->off_linktype
.constant_part
= 0;
1516 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1518 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1522 case DLT_JUNIPER_MFR
:
1523 case DLT_JUNIPER_MLFR
:
1524 case DLT_JUNIPER_MLPPP
:
1525 case DLT_JUNIPER_PPP
:
1526 case DLT_JUNIPER_CHDLC
:
1527 case DLT_JUNIPER_FRELAY
:
1528 cstate
->off_linktype
.constant_part
= 4;
1529 cstate
->off_linkpl
.constant_part
= 4;
1531 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1534 case DLT_JUNIPER_ATM1
:
1535 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1536 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1538 cstate
->off_nl_nosnap
= 10;
1541 case DLT_JUNIPER_ATM2
:
1542 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1543 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1545 cstate
->off_nl_nosnap
= 10;
1548 /* frames captured on a Juniper PPPoE service PIC
1549 * contain raw ethernet frames */
1550 case DLT_JUNIPER_PPPOE
:
1551 case DLT_JUNIPER_ETHER
:
1552 cstate
->off_linkpl
.constant_part
= 14;
1553 cstate
->off_linktype
.constant_part
= 16;
1554 cstate
->off_nl
= 18; /* Ethernet II */
1555 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1558 case DLT_JUNIPER_PPPOE_ATM
:
1559 cstate
->off_linktype
.constant_part
= 4;
1560 cstate
->off_linkpl
.constant_part
= 6;
1562 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1565 case DLT_JUNIPER_GGSN
:
1566 cstate
->off_linktype
.constant_part
= 6;
1567 cstate
->off_linkpl
.constant_part
= 12;
1569 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1572 case DLT_JUNIPER_ES
:
1573 cstate
->off_linktype
.constant_part
= 6;
1574 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1575 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1576 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1579 case DLT_JUNIPER_MONITOR
:
1580 cstate
->off_linktype
.constant_part
= 12;
1581 cstate
->off_linkpl
.constant_part
= 12;
1582 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1583 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1586 case DLT_BACNET_MS_TP
:
1587 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1588 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1589 cstate
->off_nl
= OFFSET_NOT_SET
;
1590 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1593 case DLT_JUNIPER_SERVICES
:
1594 cstate
->off_linktype
.constant_part
= 12;
1595 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1596 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1597 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1600 case DLT_JUNIPER_VP
:
1601 cstate
->off_linktype
.constant_part
= 18;
1602 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1603 cstate
->off_nl
= OFFSET_NOT_SET
;
1604 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1607 case DLT_JUNIPER_ST
:
1608 cstate
->off_linktype
.constant_part
= 18;
1609 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1610 cstate
->off_nl
= OFFSET_NOT_SET
;
1611 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1614 case DLT_JUNIPER_ISM
:
1615 cstate
->off_linktype
.constant_part
= 8;
1616 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1617 cstate
->off_nl
= OFFSET_NOT_SET
;
1618 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1621 case DLT_JUNIPER_VS
:
1622 case DLT_JUNIPER_SRX_E2E
:
1623 case DLT_JUNIPER_FIBRECHANNEL
:
1624 case DLT_JUNIPER_ATM_CEMIC
:
1625 cstate
->off_linktype
.constant_part
= 8;
1626 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1627 cstate
->off_nl
= OFFSET_NOT_SET
;
1628 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1633 cstate
->off_li_hsl
= 4;
1634 cstate
->off_sio
= 3;
1635 cstate
->off_opc
= 4;
1636 cstate
->off_dpc
= 4;
1637 cstate
->off_sls
= 7;
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
;
1644 case DLT_MTP2_WITH_PHDR
:
1646 cstate
->off_li_hsl
= 8;
1647 cstate
->off_sio
= 7;
1648 cstate
->off_opc
= 8;
1649 cstate
->off_dpc
= 8;
1650 cstate
->off_sls
= 11;
1651 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1652 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1653 cstate
->off_nl
= OFFSET_NOT_SET
;
1654 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1658 cstate
->off_li
= 22;
1659 cstate
->off_li_hsl
= 24;
1660 cstate
->off_sio
= 23;
1661 cstate
->off_opc
= 24;
1662 cstate
->off_dpc
= 24;
1663 cstate
->off_sls
= 27;
1664 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1665 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1666 cstate
->off_nl
= OFFSET_NOT_SET
;
1667 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1671 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1672 cstate
->off_linkpl
.constant_part
= 4;
1674 cstate
->off_nl_nosnap
= 0;
1679 * Currently, only raw "link[N:M]" filtering is supported.
1681 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1682 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1683 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1684 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1688 cstate
->off_linktype
.constant_part
= 1;
1689 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1691 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1694 case DLT_NETANALYZER
:
1695 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1696 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1697 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1698 cstate
->off_nl
= 0; /* Ethernet II */
1699 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1702 case DLT_NETANALYZER_TRANSPARENT
:
1703 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1704 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1705 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1706 cstate
->off_nl
= 0; /* Ethernet II */
1707 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1712 * For values in the range in which we've assigned new
1713 * DLT_ values, only raw "link[N:M]" filtering is supported.
1715 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1716 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1717 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1718 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1719 cstate
->off_nl
= OFFSET_NOT_SET
;
1720 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1722 bpf_set_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1728 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1733 * Load a value relative to the specified absolute offset.
1735 static struct slist
*
1736 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1737 u_int offset
, u_int size
)
1739 struct slist
*s
, *s2
;
1741 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1744 * If "s" is non-null, it has code to arrange that the X register
1745 * contains the variable part of the absolute offset, so we
1746 * generate a load relative to that, with an offset of
1747 * abs_offset->constant_part + offset.
1749 * Otherwise, we can do an absolute load with an offset of
1750 * abs_offset->constant_part + offset.
1754 * "s" points to a list of statements that puts the
1755 * variable part of the absolute offset into the X register.
1756 * Do an indirect load, to use the X register as an offset.
1758 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1759 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1763 * There is no variable part of the absolute offset, so
1764 * just do an absolute load.
1766 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1767 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1773 * Load a value relative to the beginning of the specified header.
1775 static struct slist
*
1776 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1779 struct slist
*s
, *s2
;
1782 * Squelch warnings from compilers that *don't* assume that
1783 * offrel always has a valid enum value and therefore don't
1784 * assume that we'll always go through one of the case arms.
1786 * If we have a default case, compilers that *do* assume that
1787 * will then complain about the default case code being
1790 * Damned if you do, damned if you don't.
1797 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1802 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1805 case OR_PREVLINKHDR
:
1806 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1810 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1813 case OR_PREVMPLSHDR
:
1814 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1818 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1821 case OR_LINKPL_NOSNAP
:
1822 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1826 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1831 * Load the X register with the length of the IPv4 header
1832 * (plus the offset of the link-layer header, if it's
1833 * preceded by a variable-length header such as a radio
1834 * header), in bytes.
1836 s
= gen_loadx_iphdrlen(cstate
);
1839 * Load the item at {offset of the link-layer payload} +
1840 * {offset, relative to the start of the link-layer
1841 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1842 * {specified offset}.
1844 * If the offset of the link-layer payload is variable,
1845 * the variable part of that offset is included in the
1846 * value in the X register, and we include the constant
1847 * part in the offset of the load.
1849 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1850 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1855 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1862 * Generate code to load into the X register the sum of the length of
1863 * the IPv4 header and the variable part of the offset of the link-layer
1866 static struct slist
*
1867 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1869 struct slist
*s
, *s2
;
1871 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1874 * The offset of the link-layer payload has a variable
1875 * part. "s" points to a list of statements that put
1876 * the variable part of that offset into the X register.
1878 * The 4*([k]&0xf) addressing mode can't be used, as we
1879 * don't have a constant offset, so we have to load the
1880 * value in question into the A register and add to it
1881 * the value from the X register.
1883 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1884 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1886 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1889 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1894 * The A register now contains the length of the IP header.
1895 * We need to add to it the variable part of the offset of
1896 * the link-layer payload, which is still in the X
1897 * register, and move the result into the X register.
1899 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1900 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1903 * The offset of the link-layer payload is a constant,
1904 * so no code was generated to load the (non-existent)
1905 * variable part of that offset.
1907 * This means we can use the 4*([k]&0xf) addressing
1908 * mode. Load the length of the IPv4 header, which
1909 * is at an offset of cstate->off_nl from the beginning of
1910 * the link-layer payload, and thus at an offset of
1911 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1912 * of the raw packet data, using that addressing mode.
1914 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1915 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1921 static struct block
*
1922 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1927 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1929 b
= new_block(cstate
, JMP(BPF_JEQ
));
1935 static inline struct block
*
1936 gen_true(compiler_state_t
*cstate
)
1938 return gen_uncond(cstate
, 1);
1941 static inline struct block
*
1942 gen_false(compiler_state_t
*cstate
)
1944 return gen_uncond(cstate
, 0);
1948 * Byte-swap a 32-bit number.
1949 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1950 * big-endian platforms.)
1952 #define SWAPLONG(y) \
1953 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1956 * Generate code to match a particular packet type.
1958 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1959 * value, if <= ETHERMTU. We use that to determine whether to
1960 * match the type/length field or to check the type/length field for
1961 * a value <= ETHERMTU to see whether it's a type field and then do
1962 * the appropriate test.
1964 static struct block
*
1965 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1967 struct block
*b0
, *b1
;
1973 case LLCSAP_NETBEUI
:
1975 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1976 * so we check the DSAP and SSAP.
1978 * LLCSAP_IP checks for IP-over-802.2, rather
1979 * than IP-over-Ethernet or IP-over-SNAP.
1981 * XXX - should we check both the DSAP and the
1982 * SSAP, like this, or should we check just the
1983 * DSAP, as we do for other types <= ETHERMTU
1984 * (i.e., other SAP values)?
1986 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1988 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
1996 * Ethernet_II frames, which are Ethernet
1997 * frames with a frame type of ETHERTYPE_IPX;
1999 * Ethernet_802.3 frames, which are 802.3
2000 * frames (i.e., the type/length field is
2001 * a length field, <= ETHERMTU, rather than
2002 * a type field) with the first two bytes
2003 * after the Ethernet/802.3 header being
2006 * Ethernet_802.2 frames, which are 802.3
2007 * frames with an 802.2 LLC header and
2008 * with the IPX LSAP as the DSAP in the LLC
2011 * Ethernet_SNAP frames, which are 802.3
2012 * frames with an LLC header and a SNAP
2013 * header and with an OUI of 0x000000
2014 * (encapsulated Ethernet) and a protocol
2015 * ID of ETHERTYPE_IPX in the SNAP header.
2017 * XXX - should we generate the same code both
2018 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2022 * This generates code to check both for the
2023 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2025 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2026 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2030 * Now we add code to check for SNAP frames with
2031 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2033 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2037 * Now we generate code to check for 802.3
2038 * frames in general.
2040 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2044 * Now add the check for 802.3 frames before the
2045 * check for Ethernet_802.2 and Ethernet_802.3,
2046 * as those checks should only be done on 802.3
2047 * frames, not on Ethernet frames.
2052 * Now add the check for Ethernet_II frames, and
2053 * do that before checking for the other frame
2056 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2060 case ETHERTYPE_ATALK
:
2061 case ETHERTYPE_AARP
:
2063 * EtherTalk (AppleTalk protocols on Ethernet link
2064 * layer) may use 802.2 encapsulation.
2068 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2069 * we check for an Ethernet type field less than
2070 * 1500, which means it's an 802.3 length field.
2072 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2076 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2077 * SNAP packets with an organization code of
2078 * 0x080007 (Apple, for Appletalk) and a protocol
2079 * type of ETHERTYPE_ATALK (Appletalk).
2081 * 802.2-encapsulated ETHERTYPE_AARP packets are
2082 * SNAP packets with an organization code of
2083 * 0x000000 (encapsulated Ethernet) and a protocol
2084 * type of ETHERTYPE_AARP (Appletalk ARP).
2086 if (ll_proto
== ETHERTYPE_ATALK
)
2087 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2088 else /* ll_proto == ETHERTYPE_AARP */
2089 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2093 * Check for Ethernet encapsulation (Ethertalk
2094 * phase 1?); we just check for the Ethernet
2097 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2103 if (ll_proto
<= ETHERMTU
) {
2105 * This is an LLC SAP value, so the frames
2106 * that match would be 802.2 frames.
2107 * Check that the frame is an 802.2 frame
2108 * (i.e., that the length/type field is
2109 * a length field, <= ETHERMTU) and
2110 * then check the DSAP.
2112 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2114 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2119 * This is an Ethernet type, so compare
2120 * the length/type field with it (if
2121 * the frame is an 802.2 frame, the length
2122 * field will be <= ETHERMTU, and, as
2123 * "ll_proto" is > ETHERMTU, this test
2124 * will fail and the frame won't match,
2125 * which is what we want).
2127 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2132 static struct block
*
2133 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2136 * For DLT_NULL, the link-layer header is a 32-bit word
2137 * containing an AF_ value in *host* byte order, and for
2138 * DLT_ENC, the link-layer header begins with a 32-bit
2139 * word containing an AF_ value in host byte order.
2141 * In addition, if we're reading a saved capture file,
2142 * the host byte order in the capture may not be the
2143 * same as the host byte order on this machine.
2145 * For DLT_LOOP, the link-layer header is a 32-bit
2146 * word containing an AF_ value in *network* byte order.
2148 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2150 * The AF_ value is in host byte order, but the BPF
2151 * interpreter will convert it to network byte order.
2153 * If this is a save file, and it's from a machine
2154 * with the opposite byte order to ours, we byte-swap
2157 * Then we run it through "htonl()", and generate
2158 * code to compare against the result.
2160 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2161 ll_proto
= SWAPLONG(ll_proto
);
2162 ll_proto
= htonl(ll_proto
);
2164 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2168 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2169 * or IPv6 then we have an error.
2171 static struct block
*
2172 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2177 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2180 case ETHERTYPE_IPV6
:
2181 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2188 return gen_false(cstate
);
2192 * Generate code to match a particular packet type.
2194 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2195 * value, if <= ETHERMTU. We use that to determine whether to
2196 * match the type field or to check the type field for the special
2197 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2199 static struct block
*
2200 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2202 struct block
*b0
, *b1
;
2208 case LLCSAP_NETBEUI
:
2210 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2211 * so we check the DSAP and SSAP.
2213 * LLCSAP_IP checks for IP-over-802.2, rather
2214 * than IP-over-Ethernet or IP-over-SNAP.
2216 * XXX - should we check both the DSAP and the
2217 * SSAP, like this, or should we check just the
2218 * DSAP, as we do for other types <= ETHERMTU
2219 * (i.e., other SAP values)?
2221 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2222 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2228 * Ethernet_II frames, which are Ethernet
2229 * frames with a frame type of ETHERTYPE_IPX;
2231 * Ethernet_802.3 frames, which have a frame
2232 * type of LINUX_SLL_P_802_3;
2234 * Ethernet_802.2 frames, which are 802.3
2235 * frames with an 802.2 LLC header (i.e, have
2236 * a frame type of LINUX_SLL_P_802_2) and
2237 * with the IPX LSAP as the DSAP in the LLC
2240 * Ethernet_SNAP frames, which are 802.3
2241 * frames with an LLC header and a SNAP
2242 * header and with an OUI of 0x000000
2243 * (encapsulated Ethernet) and a protocol
2244 * ID of ETHERTYPE_IPX in the SNAP header.
2246 * First, do the checks on LINUX_SLL_P_802_2
2247 * frames; generate the check for either
2248 * Ethernet_802.2 or Ethernet_SNAP frames, and
2249 * then put a check for LINUX_SLL_P_802_2 frames
2252 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2253 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2255 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2259 * Now check for 802.3 frames and OR that with
2260 * the previous test.
2262 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2266 * Now add the check for Ethernet_II frames, and
2267 * do that before checking for the other frame
2270 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2274 case ETHERTYPE_ATALK
:
2275 case ETHERTYPE_AARP
:
2277 * EtherTalk (AppleTalk protocols on Ethernet link
2278 * layer) may use 802.2 encapsulation.
2282 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2283 * we check for the 802.2 protocol type in the
2284 * "Ethernet type" field.
2286 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2289 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2290 * SNAP packets with an organization code of
2291 * 0x080007 (Apple, for Appletalk) and a protocol
2292 * type of ETHERTYPE_ATALK (Appletalk).
2294 * 802.2-encapsulated ETHERTYPE_AARP packets are
2295 * SNAP packets with an organization code of
2296 * 0x000000 (encapsulated Ethernet) and a protocol
2297 * type of ETHERTYPE_AARP (Appletalk ARP).
2299 if (ll_proto
== ETHERTYPE_ATALK
)
2300 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2301 else /* ll_proto == ETHERTYPE_AARP */
2302 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2306 * Check for Ethernet encapsulation (Ethertalk
2307 * phase 1?); we just check for the Ethernet
2310 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2316 if (ll_proto
<= ETHERMTU
) {
2318 * This is an LLC SAP value, so the frames
2319 * that match would be 802.2 frames.
2320 * Check for the 802.2 protocol type
2321 * in the "Ethernet type" field, and
2322 * then check the DSAP.
2324 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2325 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2331 * This is an Ethernet type, so compare
2332 * the length/type field with it (if
2333 * the frame is an 802.2 frame, the length
2334 * field will be <= ETHERMTU, and, as
2335 * "ll_proto" is > ETHERMTU, this test
2336 * will fail and the frame won't match,
2337 * which is what we want).
2339 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2344 static struct slist
*
2345 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2347 struct slist
*s1
, *s2
;
2348 struct slist
*sjeq_avs_cookie
;
2349 struct slist
*sjcommon
;
2352 * This code is not compatible with the optimizer, as
2353 * we are generating jmp instructions within a normal
2354 * slist of instructions
2356 cstate
->no_optimize
= 1;
2359 * Generate code to load the length of the radio header into
2360 * the register assigned to hold that length, if one has been
2361 * assigned. (If one hasn't been assigned, no code we've
2362 * generated uses that prefix, so we don't need to generate any
2365 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2366 * or always use the AVS header rather than the Prism header.
2367 * We load a 4-byte big-endian value at the beginning of the
2368 * raw packet data, and see whether, when masked with 0xFFFFF000,
2369 * it's equal to 0x80211000. If so, that indicates that it's
2370 * an AVS header (the masked-out bits are the version number).
2371 * Otherwise, it's a Prism header.
2373 * XXX - the Prism header is also, in theory, variable-length,
2374 * but no known software generates headers that aren't 144
2377 if (cstate
->off_linkhdr
.reg
!= -1) {
2381 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2385 * AND it with 0xFFFFF000.
2387 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2388 s2
->s
.k
= 0xFFFFF000;
2392 * Compare with 0x80211000.
2394 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2395 sjeq_avs_cookie
->s
.k
= 0x80211000;
2396 sappend(s1
, sjeq_avs_cookie
);
2401 * The 4 bytes at an offset of 4 from the beginning of
2402 * the AVS header are the length of the AVS header.
2403 * That field is big-endian.
2405 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2408 sjeq_avs_cookie
->s
.jt
= s2
;
2411 * Now jump to the code to allocate a register
2412 * into which to save the header length and
2413 * store the length there. (The "jump always"
2414 * instruction needs to have the k field set;
2415 * it's added to the PC, so, as we're jumping
2416 * over a single instruction, it should be 1.)
2418 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2420 sappend(s1
, sjcommon
);
2423 * Now for the code that handles the Prism header.
2424 * Just load the length of the Prism header (144)
2425 * into the A register. Have the test for an AVS
2426 * header branch here if we don't have an AVS header.
2428 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2431 sjeq_avs_cookie
->s
.jf
= s2
;
2434 * Now allocate a register to hold that value and store
2435 * it. The code for the AVS header will jump here after
2436 * loading the length of the AVS header.
2438 s2
= new_stmt(cstate
, BPF_ST
);
2439 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2441 sjcommon
->s
.jf
= s2
;
2444 * Now move it into the X register.
2446 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2454 static struct slist
*
2455 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2457 struct slist
*s1
, *s2
;
2460 * Generate code to load the length of the AVS header into
2461 * the register assigned to hold that length, if one has been
2462 * assigned. (If one hasn't been assigned, no code we've
2463 * generated uses that prefix, so we don't need to generate any
2466 if (cstate
->off_linkhdr
.reg
!= -1) {
2468 * The 4 bytes at an offset of 4 from the beginning of
2469 * the AVS header are the length of the AVS header.
2470 * That field is big-endian.
2472 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2476 * Now allocate a register to hold that value and store
2479 s2
= new_stmt(cstate
, BPF_ST
);
2480 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2484 * Now move it into the X register.
2486 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2494 static struct slist
*
2495 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2497 struct slist
*s1
, *s2
;
2500 * Generate code to load the length of the radiotap header into
2501 * the register assigned to hold that length, if one has been
2502 * assigned. (If one hasn't been assigned, no code we've
2503 * generated uses that prefix, so we don't need to generate any
2506 if (cstate
->off_linkhdr
.reg
!= -1) {
2508 * The 2 bytes at offsets of 2 and 3 from the beginning
2509 * of the radiotap header are the length of the radiotap
2510 * header; unfortunately, it's little-endian, so we have
2511 * to load it a byte at a time and construct the value.
2515 * Load the high-order byte, at an offset of 3, shift it
2516 * left a byte, and put the result in the X register.
2518 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2520 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2523 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2527 * Load the next byte, at an offset of 2, and OR the
2528 * value from the X register into it.
2530 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2533 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2537 * Now allocate a register to hold that value and store
2540 s2
= new_stmt(cstate
, BPF_ST
);
2541 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2545 * Now move it into the X register.
2547 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2556 * At the moment we treat PPI as normal Radiotap encoded
2557 * packets. The difference is in the function that generates
2558 * the code at the beginning to compute the header length.
2559 * Since this code generator of PPI supports bare 802.11
2560 * encapsulation only (i.e. the encapsulated DLT should be
2561 * DLT_IEEE802_11) we generate code to check for this too;
2562 * that's done in finish_parse().
2564 static struct slist
*
2565 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2567 struct slist
*s1
, *s2
;
2570 * Generate code to load the length of the radiotap header
2571 * into the register assigned to hold that length, if one has
2574 if (cstate
->off_linkhdr
.reg
!= -1) {
2576 * The 2 bytes at offsets of 2 and 3 from the beginning
2577 * of the radiotap header are the length of the radiotap
2578 * header; unfortunately, it's little-endian, so we have
2579 * to load it a byte at a time and construct the value.
2583 * Load the high-order byte, at an offset of 3, shift it
2584 * left a byte, and put the result in the X register.
2586 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2588 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2591 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2595 * Load the next byte, at an offset of 2, and OR the
2596 * value from the X register into it.
2598 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2601 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2605 * Now allocate a register to hold that value and store
2608 s2
= new_stmt(cstate
, BPF_ST
);
2609 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2613 * Now move it into the X register.
2615 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2624 * Load a value relative to the beginning of the link-layer header after the 802.11
2625 * header, i.e. LLC_SNAP.
2626 * The link-layer header doesn't necessarily begin at the beginning
2627 * of the packet data; there might be a variable-length prefix containing
2628 * radio information.
2630 static struct slist
*
2631 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2634 struct slist
*sjset_data_frame_1
;
2635 struct slist
*sjset_data_frame_2
;
2636 struct slist
*sjset_qos
;
2637 struct slist
*sjset_radiotap_flags_present
;
2638 struct slist
*sjset_radiotap_ext_present
;
2639 struct slist
*sjset_radiotap_tsft_present
;
2640 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2641 struct slist
*s_roundup
;
2643 if (cstate
->off_linkpl
.reg
== -1) {
2645 * No register has been assigned to the offset of
2646 * the link-layer payload, which means nobody needs
2647 * it; don't bother computing it - just return
2648 * what we already have.
2654 * This code is not compatible with the optimizer, as
2655 * we are generating jmp instructions within a normal
2656 * slist of instructions
2658 cstate
->no_optimize
= 1;
2661 * If "s" is non-null, it has code to arrange that the X register
2662 * contains the length of the prefix preceding the link-layer
2665 * Otherwise, the length of the prefix preceding the link-layer
2666 * header is "off_outermostlinkhdr.constant_part".
2670 * There is no variable-length header preceding the
2671 * link-layer header.
2673 * Load the length of the fixed-length prefix preceding
2674 * the link-layer header (if any) into the X register,
2675 * and store it in the cstate->off_linkpl.reg register.
2676 * That length is off_outermostlinkhdr.constant_part.
2678 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2679 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2683 * The X register contains the offset of the beginning of the
2684 * link-layer header; add 24, which is the minimum length
2685 * of the MAC header for a data frame, to that, and store it
2686 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2687 * which is at the offset in the X register, with an indexed load.
2689 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2691 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2694 s2
= new_stmt(cstate
, BPF_ST
);
2695 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2698 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2703 * Check the Frame Control field to see if this is a data frame;
2704 * a data frame has the 0x08 bit (b3) in that field set and the
2705 * 0x04 bit (b2) clear.
2707 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2708 sjset_data_frame_1
->s
.k
= 0x08;
2709 sappend(s
, sjset_data_frame_1
);
2712 * If b3 is set, test b2, otherwise go to the first statement of
2713 * the rest of the program.
2715 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2716 sjset_data_frame_2
->s
.k
= 0x04;
2717 sappend(s
, sjset_data_frame_2
);
2718 sjset_data_frame_1
->s
.jf
= snext
;
2721 * If b2 is not set, this is a data frame; test the QoS bit.
2722 * Otherwise, go to the first statement of the rest of the
2725 sjset_data_frame_2
->s
.jt
= snext
;
2726 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2727 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2728 sappend(s
, sjset_qos
);
2731 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2733 * Otherwise, go to the first statement of the rest of the
2736 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2737 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2739 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2742 s2
= new_stmt(cstate
, BPF_ST
);
2743 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2747 * If we have a radiotap header, look at it to see whether
2748 * there's Atheros padding between the MAC-layer header
2751 * Note: all of the fields in the radiotap header are
2752 * little-endian, so we byte-swap all of the values
2753 * we test against, as they will be loaded as big-endian
2756 * XXX - in the general case, we would have to scan through
2757 * *all* the presence bits, if there's more than one word of
2758 * presence bits. That would require a loop, meaning that
2759 * we wouldn't be able to run the filter in the kernel.
2761 * We assume here that the Atheros adapters that insert the
2762 * annoying padding don't have multiple antennae and therefore
2763 * do not generate radiotap headers with multiple presence words.
2765 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2767 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2768 * in the first presence flag word?
2770 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2774 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2775 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2776 sappend(s
, sjset_radiotap_flags_present
);
2779 * If not, skip all of this.
2781 sjset_radiotap_flags_present
->s
.jf
= snext
;
2784 * Otherwise, is the "extension" bit set in that word?
2786 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2787 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2788 sappend(s
, sjset_radiotap_ext_present
);
2789 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2792 * If so, skip all of this.
2794 sjset_radiotap_ext_present
->s
.jt
= snext
;
2797 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2799 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2800 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2801 sappend(s
, sjset_radiotap_tsft_present
);
2802 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2805 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2806 * at an offset of 16 from the beginning of the raw packet
2807 * data (8 bytes for the radiotap header and 8 bytes for
2810 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2813 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2816 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2818 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2819 sjset_tsft_datapad
->s
.k
= 0x20;
2820 sappend(s
, sjset_tsft_datapad
);
2823 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2824 * at an offset of 8 from the beginning of the raw packet
2825 * data (8 bytes for the radiotap header).
2827 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2830 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2833 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2835 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2836 sjset_notsft_datapad
->s
.k
= 0x20;
2837 sappend(s
, sjset_notsft_datapad
);
2840 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2841 * set, round the length of the 802.11 header to
2842 * a multiple of 4. Do that by adding 3 and then
2843 * dividing by and multiplying by 4, which we do by
2846 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2847 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2848 sappend(s
, s_roundup
);
2849 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2852 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2853 s2
->s
.k
= (bpf_u_int32
)~3;
2855 s2
= new_stmt(cstate
, BPF_ST
);
2856 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2859 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2860 sjset_tsft_datapad
->s
.jf
= snext
;
2861 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2862 sjset_notsft_datapad
->s
.jf
= snext
;
2864 sjset_qos
->s
.jf
= snext
;
2870 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2874 /* There is an implicit dependency between the link
2875 * payload and link header since the payload computation
2876 * includes the variable part of the header. Therefore,
2877 * if nobody else has allocated a register for the link
2878 * header and we need it, do it now. */
2879 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2880 cstate
->off_linkhdr
.reg
== -1)
2881 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2884 * For link-layer types that have a variable-length header
2885 * preceding the link-layer header, generate code to load
2886 * the offset of the link-layer header into the register
2887 * assigned to that offset, if any.
2889 * XXX - this, and the next switch statement, won't handle
2890 * encapsulation of 802.11 or 802.11+radio information in
2891 * some other protocol stack. That's significantly more
2894 switch (cstate
->outermostlinktype
) {
2896 case DLT_PRISM_HEADER
:
2897 s
= gen_load_prism_llprefixlen(cstate
);
2900 case DLT_IEEE802_11_RADIO_AVS
:
2901 s
= gen_load_avs_llprefixlen(cstate
);
2904 case DLT_IEEE802_11_RADIO
:
2905 s
= gen_load_radiotap_llprefixlen(cstate
);
2909 s
= gen_load_ppi_llprefixlen(cstate
);
2918 * For link-layer types that have a variable-length link-layer
2919 * header, generate code to load the offset of the link-layer
2920 * payload into the register assigned to that offset, if any.
2922 switch (cstate
->outermostlinktype
) {
2924 case DLT_IEEE802_11
:
2925 case DLT_PRISM_HEADER
:
2926 case DLT_IEEE802_11_RADIO_AVS
:
2927 case DLT_IEEE802_11_RADIO
:
2929 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2934 * If there there is no initialization yet and we need variable
2935 * length offsets for VLAN, initialize them to zero
2937 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2940 if (cstate
->off_linkpl
.reg
== -1)
2941 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2942 if (cstate
->off_linktype
.reg
== -1)
2943 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2945 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2947 s2
= new_stmt(cstate
, BPF_ST
);
2948 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2950 s2
= new_stmt(cstate
, BPF_ST
);
2951 s2
->s
.k
= cstate
->off_linktype
.reg
;
2956 * If we have any offset-loading code, append all the
2957 * existing statements in the block to those statements,
2958 * and make the resulting list the list of statements
2962 sappend(s
, b
->stmts
);
2967 static struct block
*
2968 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2970 struct slist
*s_load_dlt
;
2973 if (cstate
->linktype
== DLT_PPI
)
2975 /* Create the statements that check for the DLT
2977 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2978 s_load_dlt
->s
.k
= 4;
2980 b
= new_block(cstate
, JMP(BPF_JEQ
));
2982 b
->stmts
= s_load_dlt
;
2983 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2994 * Take an absolute offset, and:
2996 * if it has no variable part, return NULL;
2998 * if it has a variable part, generate code to load the register
2999 * containing that variable part into the X register, returning
3000 * a pointer to that code - if no register for that offset has
3001 * been allocated, allocate it first.
3003 * (The code to set that register will be generated later, but will
3004 * be placed earlier in the code sequence.)
3006 static struct slist
*
3007 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3011 if (off
->is_variable
) {
3012 if (off
->reg
== -1) {
3014 * We haven't yet assigned a register for the
3015 * variable part of the offset of the link-layer
3016 * header; allocate one.
3018 off
->reg
= alloc_reg(cstate
);
3022 * Load the register containing the variable part of the
3023 * offset of the link-layer header into the X register.
3025 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3030 * That offset isn't variable, there's no variable part,
3031 * so we don't need to generate any code.
3038 * Map an Ethernet type to the equivalent PPP type.
3041 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3049 case ETHERTYPE_IPV6
:
3050 ll_proto
= PPP_IPV6
;
3054 ll_proto
= PPP_DECNET
;
3057 case ETHERTYPE_ATALK
:
3058 ll_proto
= PPP_APPLE
;
3071 * I'm assuming the "Bridging PDU"s that go
3072 * over PPP are Spanning Tree Protocol
3075 ll_proto
= PPP_BRPDU
;
3086 * Generate any tests that, for encapsulation of a link-layer packet
3087 * inside another protocol stack, need to be done to check for those
3088 * link-layer packets (and that haven't already been done by a check
3089 * for that encapsulation).
3091 static struct block
*
3092 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3096 if (cstate
->is_geneve
)
3097 return gen_geneve_ll_check(cstate
);
3099 switch (cstate
->prevlinktype
) {
3103 * This is LANE-encapsulated Ethernet; check that the LANE
3104 * packet doesn't begin with an LE Control marker, i.e.
3105 * that it's data, not a control message.
3107 * (We've already generated a test for LANE.)
3109 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3115 * No such tests are necessary.
3123 * The three different values we should check for when checking for an
3124 * IPv6 packet with DLT_NULL.
3126 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3127 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3128 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3131 * Generate code to match a particular packet type by matching the
3132 * link-layer type field or fields in the 802.2 LLC header.
3134 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3135 * value, if <= ETHERMTU.
3137 static struct block
*
3138 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3140 struct block
*b0
, *b1
, *b2
;
3141 const char *description
;
3143 /* are we checking MPLS-encapsulated packets? */
3144 if (cstate
->label_stack_depth
> 0)
3145 return gen_mpls_linktype(cstate
, ll_proto
);
3147 switch (cstate
->linktype
) {
3150 case DLT_NETANALYZER
:
3151 case DLT_NETANALYZER_TRANSPARENT
:
3152 /* Geneve has an EtherType regardless of whether there is an
3154 if (!cstate
->is_geneve
)
3155 b0
= gen_prevlinkhdr_check(cstate
);
3159 b1
= gen_ether_linktype(cstate
, ll_proto
);
3169 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3173 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3177 case DLT_IEEE802_11
:
3178 case DLT_PRISM_HEADER
:
3179 case DLT_IEEE802_11_RADIO_AVS
:
3180 case DLT_IEEE802_11_RADIO
:
3183 * Check that we have a data frame.
3185 b0
= gen_check_802_11_data_frame(cstate
);
3188 * Now check for the specified link-layer type.
3190 b1
= gen_llc_linktype(cstate
, ll_proto
);
3197 * XXX - check for LLC frames.
3199 return gen_llc_linktype(cstate
, ll_proto
);
3204 * XXX - check for LLC PDUs, as per IEEE 802.5.
3206 return gen_llc_linktype(cstate
, ll_proto
);
3209 case DLT_ATM_RFC1483
:
3211 case DLT_IP_OVER_FC
:
3212 return gen_llc_linktype(cstate
, ll_proto
);
3217 * Check for an LLC-encapsulated version of this protocol;
3218 * if we were checking for LANE, linktype would no longer
3221 * Check for LLC encapsulation and then check the protocol.
3223 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3224 b1
= gen_llc_linktype(cstate
, ll_proto
);
3230 return gen_linux_sll_linktype(cstate
, ll_proto
);
3234 case DLT_SLIP_BSDOS
:
3237 * These types don't provide any type field; packets
3238 * are always IPv4 or IPv6.
3240 * XXX - for IPv4, check for a version number of 4, and,
3241 * for IPv6, check for a version number of 6?
3246 /* Check for a version number of 4. */
3247 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3249 case ETHERTYPE_IPV6
:
3250 /* Check for a version number of 6. */
3251 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3254 return gen_false(cstate
); /* always false */
3260 * Raw IPv4, so no type field.
3262 if (ll_proto
== ETHERTYPE_IP
)
3263 return gen_true(cstate
); /* always true */
3265 /* Checking for something other than IPv4; always false */
3266 return gen_false(cstate
);
3271 * Raw IPv6, so no type field.
3273 if (ll_proto
== ETHERTYPE_IPV6
)
3274 return gen_true(cstate
); /* always true */
3276 /* Checking for something other than IPv6; always false */
3277 return gen_false(cstate
);
3282 case DLT_PPP_SERIAL
:
3285 * We use Ethernet protocol types inside libpcap;
3286 * map them to the corresponding PPP protocol types.
3288 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3289 ethertype_to_ppptype(ll_proto
));
3294 * We use Ethernet protocol types inside libpcap;
3295 * map them to the corresponding PPP protocol types.
3301 * Also check for Van Jacobson-compressed IP.
3302 * XXX - do this for other forms of PPP?
3304 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3305 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3307 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3312 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3313 ethertype_to_ppptype(ll_proto
));
3323 return (gen_loopback_linktype(cstate
, AF_INET
));
3325 case ETHERTYPE_IPV6
:
3327 * AF_ values may, unfortunately, be platform-
3328 * dependent; AF_INET isn't, because everybody
3329 * used 4.2BSD's value, but AF_INET6 is, because
3330 * 4.2BSD didn't have a value for it (given that
3331 * IPv6 didn't exist back in the early 1980's),
3332 * and they all picked their own values.
3334 * This means that, if we're reading from a
3335 * savefile, we need to check for all the
3338 * If we're doing a live capture, we only need
3339 * to check for this platform's value; however,
3340 * Npcap uses 24, which isn't Windows's AF_INET6
3341 * value. (Given the multiple different values,
3342 * programs that read pcap files shouldn't be
3343 * checking for their platform's AF_INET6 value
3344 * anyway, they should check for all of the
3345 * possible values. and they might as well do
3346 * that even for live captures.)
3348 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3350 * Savefile - check for all three
3351 * possible IPv6 values.
3353 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3354 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3356 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3361 * Live capture, so we only need to
3362 * check for the value used on this
3367 * Npcap doesn't use Windows's AF_INET6,
3368 * as that collides with AF_IPX on
3369 * some BSDs (both have the value 23).
3370 * Instead, it uses 24.
3372 return (gen_loopback_linktype(cstate
, 24));
3375 return (gen_loopback_linktype(cstate
, AF_INET6
));
3376 #else /* AF_INET6 */
3378 * I guess this platform doesn't support
3379 * IPv6, so we just reject all packets.
3381 return gen_false(cstate
);
3382 #endif /* AF_INET6 */
3388 * Not a type on which we support filtering.
3389 * XXX - support those that have AF_ values
3390 * #defined on this platform, at least?
3392 return gen_false(cstate
);
3395 #ifdef HAVE_NET_PFVAR_H
3398 * af field is host byte order in contrast to the rest of
3401 if (ll_proto
== ETHERTYPE_IP
)
3402 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3404 else if (ll_proto
== ETHERTYPE_IPV6
)
3405 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3408 return gen_false(cstate
);
3410 #endif /* HAVE_NET_PFVAR_H */
3413 case DLT_ARCNET_LINUX
:
3415 * XXX should we check for first fragment if the protocol
3421 return gen_false(cstate
);
3423 case ETHERTYPE_IPV6
:
3424 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3428 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3430 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3436 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3438 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3443 case ETHERTYPE_REVARP
:
3444 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3447 case ETHERTYPE_ATALK
:
3448 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3455 case ETHERTYPE_ATALK
:
3456 return gen_true(cstate
);
3458 return gen_false(cstate
);
3464 * XXX - assumes a 2-byte Frame Relay header with
3465 * DLCI and flags. What if the address is longer?
3471 * Check for the special NLPID for IP.
3473 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3475 case ETHERTYPE_IPV6
:
3477 * Check for the special NLPID for IPv6.
3479 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3483 * Check for several OSI protocols.
3485 * Frame Relay packets typically have an OSI
3486 * NLPID at the beginning; we check for each
3489 * What we check for is the NLPID and a frame
3490 * control field of UI, i.e. 0x03 followed
3493 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3494 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3495 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3501 return gen_false(cstate
);
3506 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3508 case DLT_JUNIPER_MFR
:
3509 case DLT_JUNIPER_MLFR
:
3510 case DLT_JUNIPER_MLPPP
:
3511 case DLT_JUNIPER_ATM1
:
3512 case DLT_JUNIPER_ATM2
:
3513 case DLT_JUNIPER_PPPOE
:
3514 case DLT_JUNIPER_PPPOE_ATM
:
3515 case DLT_JUNIPER_GGSN
:
3516 case DLT_JUNIPER_ES
:
3517 case DLT_JUNIPER_MONITOR
:
3518 case DLT_JUNIPER_SERVICES
:
3519 case DLT_JUNIPER_ETHER
:
3520 case DLT_JUNIPER_PPP
:
3521 case DLT_JUNIPER_FRELAY
:
3522 case DLT_JUNIPER_CHDLC
:
3523 case DLT_JUNIPER_VP
:
3524 case DLT_JUNIPER_ST
:
3525 case DLT_JUNIPER_ISM
:
3526 case DLT_JUNIPER_VS
:
3527 case DLT_JUNIPER_SRX_E2E
:
3528 case DLT_JUNIPER_FIBRECHANNEL
:
3529 case DLT_JUNIPER_ATM_CEMIC
:
3531 /* just lets verify the magic number for now -
3532 * on ATM we may have up to 6 different encapsulations on the wire
3533 * and need a lot of heuristics to figure out that the payload
3536 * FIXME encapsulation specific BPF_ filters
3538 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3540 case DLT_BACNET_MS_TP
:
3541 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3544 return gen_ipnet_linktype(cstate
, ll_proto
);
3546 case DLT_LINUX_IRDA
:
3547 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3550 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3553 case DLT_MTP2_WITH_PHDR
:
3554 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3557 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3560 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3562 case DLT_LINUX_LAPD
:
3563 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3565 case DLT_USB_FREEBSD
:
3567 case DLT_USB_LINUX_MMAPPED
:
3569 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3571 case DLT_BLUETOOTH_HCI_H4
:
3572 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3573 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3576 case DLT_CAN_SOCKETCAN
:
3577 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3579 case DLT_IEEE802_15_4
:
3580 case DLT_IEEE802_15_4_LINUX
:
3581 case DLT_IEEE802_15_4_NONASK_PHY
:
3582 case DLT_IEEE802_15_4_NOFCS
:
3583 case DLT_IEEE802_15_4_TAP
:
3584 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3586 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3587 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3590 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3593 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3595 case DLT_IPMB_KONTRON
:
3596 case DLT_IPMB_LINUX
:
3597 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3600 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3603 /* Using the fixed-size NFLOG header it is possible to tell only
3604 * the address family of the packet, other meaningful data is
3605 * either missing or behind TLVs.
3607 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3611 * Does this link-layer header type have a field
3612 * indicating the type of the next protocol? If
3613 * so, off_linktype.constant_part will be the offset of that
3614 * field in the packet; if not, it will be OFFSET_NOT_SET.
3616 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3618 * Yes; assume it's an Ethernet type. (If
3619 * it's not, it needs to be handled specially
3622 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3626 * No; report an error.
3628 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3629 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3637 * Check for an LLC SNAP packet with a given organization code and
3638 * protocol type; we check the entire contents of the 802.2 LLC and
3639 * snap headers, checking for DSAP and SSAP of SNAP and a control
3640 * field of 0x03 in the LLC header, and for the specified organization
3641 * code and protocol type in the SNAP header.
3643 static struct block
*
3644 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3646 u_char snapblock
[8];
3648 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3649 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3650 snapblock
[2] = 0x03; /* control = UI */
3651 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3652 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3653 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3654 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3655 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3656 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3660 * Generate code to match frames with an LLC header.
3662 static struct block
*
3663 gen_llc_internal(compiler_state_t
*cstate
)
3665 struct block
*b0
, *b1
;
3667 switch (cstate
->linktype
) {
3671 * We check for an Ethernet type field less than
3672 * 1500, which means it's an 802.3 length field.
3674 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3678 * Now check for the purported DSAP and SSAP not being
3679 * 0xFF, to rule out NetWare-over-802.3.
3681 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3688 * We check for LLC traffic.
3690 b0
= gen_atmtype_llc(cstate
);
3693 case DLT_IEEE802
: /* Token Ring */
3695 * XXX - check for LLC frames.
3697 return gen_true(cstate
);
3701 * XXX - check for LLC frames.
3703 return gen_true(cstate
);
3705 case DLT_ATM_RFC1483
:
3707 * For LLC encapsulation, these are defined to have an
3710 * For VC encapsulation, they don't, but there's no
3711 * way to check for that; the protocol used on the VC
3712 * is negotiated out of band.
3714 return gen_true(cstate
);
3716 case DLT_IEEE802_11
:
3717 case DLT_PRISM_HEADER
:
3718 case DLT_IEEE802_11_RADIO
:
3719 case DLT_IEEE802_11_RADIO_AVS
:
3722 * Check that we have a data frame.
3724 b0
= gen_check_802_11_data_frame(cstate
);
3728 bpf_error(cstate
, "'llc' not supported for %s",
3729 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3735 gen_llc(compiler_state_t
*cstate
)
3738 * Catch errors reported by us and routines below us, and return NULL
3741 if (setjmp(cstate
->top_ctx
))
3744 return gen_llc_internal(cstate
);
3748 gen_llc_i(compiler_state_t
*cstate
)
3750 struct block
*b0
, *b1
;
3754 * Catch errors reported by us and routines below us, and return NULL
3757 if (setjmp(cstate
->top_ctx
))
3761 * Check whether this is an LLC frame.
3763 b0
= gen_llc_internal(cstate
);
3766 * Load the control byte and test the low-order bit; it must
3767 * be clear for I frames.
3769 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3770 b1
= new_block(cstate
, JMP(BPF_JSET
));
3779 gen_llc_s(compiler_state_t
*cstate
)
3781 struct block
*b0
, *b1
;
3784 * Catch errors reported by us and routines below us, and return NULL
3787 if (setjmp(cstate
->top_ctx
))
3791 * Check whether this is an LLC frame.
3793 b0
= gen_llc_internal(cstate
);
3796 * Now compare the low-order 2 bit of the control byte against
3797 * the appropriate value for S frames.
3799 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3805 gen_llc_u(compiler_state_t
*cstate
)
3807 struct block
*b0
, *b1
;
3810 * Catch errors reported by us and routines below us, and return NULL
3813 if (setjmp(cstate
->top_ctx
))
3817 * Check whether this is an LLC frame.
3819 b0
= gen_llc_internal(cstate
);
3822 * Now compare the low-order 2 bit of the control byte against
3823 * the appropriate value for U frames.
3825 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3831 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3833 struct block
*b0
, *b1
;
3836 * Catch errors reported by us and routines below us, and return NULL
3839 if (setjmp(cstate
->top_ctx
))
3843 * Check whether this is an LLC frame.
3845 b0
= gen_llc_internal(cstate
);
3848 * Now check for an S frame with the appropriate type.
3850 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3856 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3858 struct block
*b0
, *b1
;
3861 * Catch errors reported by us and routines below us, and return NULL
3864 if (setjmp(cstate
->top_ctx
))
3868 * Check whether this is an LLC frame.
3870 b0
= gen_llc_internal(cstate
);
3873 * Now check for a U frame with the appropriate type.
3875 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3881 * Generate code to match a particular packet type, for link-layer types
3882 * using 802.2 LLC headers.
3884 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3885 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3887 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3888 * value, if <= ETHERMTU. We use that to determine whether to
3889 * match the DSAP or both DSAP and LSAP or to check the OUI and
3890 * protocol ID in a SNAP header.
3892 static struct block
*
3893 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3896 * XXX - handle token-ring variable-length header.
3902 case LLCSAP_NETBEUI
:
3904 * XXX - should we check both the DSAP and the
3905 * SSAP, like this, or should we check just the
3906 * DSAP, as we do for other SAP values?
3908 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3909 ((ll_proto
<< 8) | ll_proto
));
3913 * XXX - are there ever SNAP frames for IPX on
3914 * non-Ethernet 802.x networks?
3916 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3918 case ETHERTYPE_ATALK
:
3920 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3921 * SNAP packets with an organization code of
3922 * 0x080007 (Apple, for Appletalk) and a protocol
3923 * type of ETHERTYPE_ATALK (Appletalk).
3925 * XXX - check for an organization code of
3926 * encapsulated Ethernet as well?
3928 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3932 * XXX - we don't have to check for IPX 802.3
3933 * here, but should we check for the IPX Ethertype?
3935 if (ll_proto
<= ETHERMTU
) {
3937 * This is an LLC SAP value, so check
3940 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
3943 * This is an Ethernet type; we assume that it's
3944 * unlikely that it'll appear in the right place
3945 * at random, and therefore check only the
3946 * location that would hold the Ethernet type
3947 * in a SNAP frame with an organization code of
3948 * 0x000000 (encapsulated Ethernet).
3950 * XXX - if we were to check for the SNAP DSAP and
3951 * LSAP, as per XXX, and were also to check for an
3952 * organization code of 0x000000 (encapsulated
3953 * Ethernet), we'd do
3955 * return gen_snap(cstate, 0x000000, ll_proto);
3957 * here; for now, we don't, as per the above.
3958 * I don't know whether it's worth the extra CPU
3959 * time to do the right check or not.
3961 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
3966 static struct block
*
3967 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3968 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
3970 struct block
*b0
, *b1
;
3984 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
3985 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
3991 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
3992 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
3997 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4001 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4005 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4009 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4013 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4017 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4024 b0
= gen_linktype(cstate
, ll_proto
);
4025 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4031 static struct block
*
4032 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4033 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4036 struct block
*b0
, *b1
;
4051 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4052 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4058 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4059 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4064 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4068 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4072 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4076 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4080 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4084 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4091 /* this order is important */
4092 a
= (uint32_t *)addr
;
4093 m
= (uint32_t *)mask
;
4094 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4095 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4097 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4099 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4101 b0
= gen_linktype(cstate
, ll_proto
);
4107 static struct block
*
4108 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4110 register struct block
*b0
, *b1
;
4114 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4117 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4120 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4121 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4127 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4128 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4133 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4137 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4141 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4145 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4149 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4153 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4161 * Like gen_ehostop, but for DLT_FDDI
4163 static struct block
*
4164 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4166 struct block
*b0
, *b1
;
4170 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4173 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4176 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4177 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4183 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4184 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4189 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4193 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4197 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4201 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4205 bpf_error(cstate
, "'ra' is only supported on 802.11");
4209 bpf_error(cstate
, "'ta' is only supported on 802.11");
4217 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4219 static struct block
*
4220 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4222 register struct block
*b0
, *b1
;
4226 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4229 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4232 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4233 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4239 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4240 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4245 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4249 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4253 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4257 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4261 bpf_error(cstate
, "'ra' is only supported on 802.11");
4265 bpf_error(cstate
, "'ta' is only supported on 802.11");
4273 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4274 * various 802.11 + radio headers.
4276 static struct block
*
4277 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4279 register struct block
*b0
, *b1
, *b2
;
4280 register struct slist
*s
;
4282 #ifdef ENABLE_WLAN_FILTERING_PATCH
4285 * We need to disable the optimizer because the optimizer is buggy
4286 * and wipes out some LD instructions generated by the below
4287 * code to validate the Frame Control bits
4289 cstate
->no_optimize
= 1;
4290 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4297 * For control frames, there is no SA.
4299 * For management frames, SA is at an
4300 * offset of 10 from the beginning of
4303 * For data frames, SA is at an offset
4304 * of 10 from the beginning of the packet
4305 * if From DS is clear, at an offset of
4306 * 16 from the beginning of the packet
4307 * if From DS is set and To DS is clear,
4308 * and an offset of 24 from the beginning
4309 * of the packet if From DS is set and To DS
4314 * Generate the tests to be done for data frames
4317 * First, check for To DS set, i.e. check "link[1] & 0x01".
4319 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4320 b1
= new_block(cstate
, JMP(BPF_JSET
));
4321 b1
->s
.k
= 0x01; /* To DS */
4325 * If To DS is set, the SA is at 24.
4327 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4331 * Now, check for To DS not set, i.e. check
4332 * "!(link[1] & 0x01)".
4334 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4335 b2
= new_block(cstate
, JMP(BPF_JSET
));
4336 b2
->s
.k
= 0x01; /* To DS */
4341 * If To DS is not set, the SA is at 16.
4343 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4347 * Now OR together the last two checks. That gives
4348 * the complete set of checks for data frames with
4354 * Now check for From DS being set, and AND that with
4355 * the ORed-together checks.
4357 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4358 b1
= new_block(cstate
, JMP(BPF_JSET
));
4359 b1
->s
.k
= 0x02; /* From DS */
4364 * Now check for data frames with From DS not set.
4366 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4367 b2
= new_block(cstate
, JMP(BPF_JSET
));
4368 b2
->s
.k
= 0x02; /* From DS */
4373 * If From DS isn't set, the SA is at 10.
4375 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4379 * Now OR together the checks for data frames with
4380 * From DS not set and for data frames with From DS
4381 * set; that gives the checks done for data frames.
4386 * Now check for a data frame.
4387 * I.e, check "link[0] & 0x08".
4389 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4390 b1
= new_block(cstate
, JMP(BPF_JSET
));
4395 * AND that with the checks done for data frames.
4400 * If the high-order bit of the type value is 0, this
4401 * is a management frame.
4402 * I.e, check "!(link[0] & 0x08)".
4404 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4405 b2
= new_block(cstate
, JMP(BPF_JSET
));
4411 * For management frames, the SA is at 10.
4413 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4417 * OR that with the checks done for data frames.
4418 * That gives the checks done for management and
4424 * If the low-order bit of the type value is 1,
4425 * this is either a control frame or a frame
4426 * with a reserved type, and thus not a
4429 * I.e., check "!(link[0] & 0x04)".
4431 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4432 b1
= new_block(cstate
, JMP(BPF_JSET
));
4438 * AND that with the checks for data and management
4448 * For control frames, there is no DA.
4450 * For management frames, DA is at an
4451 * offset of 4 from the beginning of
4454 * For data frames, DA is at an offset
4455 * of 4 from the beginning of the packet
4456 * if To DS is clear and at an offset of
4457 * 16 from the beginning of the packet
4462 * Generate the tests to be done for data frames.
4464 * First, check for To DS set, i.e. "link[1] & 0x01".
4466 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4467 b1
= new_block(cstate
, JMP(BPF_JSET
));
4468 b1
->s
.k
= 0x01; /* To DS */
4472 * If To DS is set, the DA is at 16.
4474 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4478 * Now, check for To DS not set, i.e. check
4479 * "!(link[1] & 0x01)".
4481 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4482 b2
= new_block(cstate
, JMP(BPF_JSET
));
4483 b2
->s
.k
= 0x01; /* To DS */
4488 * If To DS is not set, the DA is at 4.
4490 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4494 * Now OR together the last two checks. That gives
4495 * the complete set of checks for data frames.
4500 * Now check for a data frame.
4501 * I.e, check "link[0] & 0x08".
4503 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4504 b1
= new_block(cstate
, JMP(BPF_JSET
));
4509 * AND that with the checks done for data frames.
4514 * If the high-order bit of the type value is 0, this
4515 * is a management frame.
4516 * I.e, check "!(link[0] & 0x08)".
4518 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4519 b2
= new_block(cstate
, JMP(BPF_JSET
));
4525 * For management frames, the DA is at 4.
4527 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4531 * OR that with the checks done for data frames.
4532 * That gives the checks done for management and
4538 * If the low-order bit of the type value is 1,
4539 * this is either a control frame or a frame
4540 * with a reserved type, and thus not a
4543 * I.e., check "!(link[0] & 0x04)".
4545 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4546 b1
= new_block(cstate
, JMP(BPF_JSET
));
4552 * AND that with the checks for data and management
4559 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4560 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4566 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4567 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4572 * XXX - add BSSID keyword?
4575 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4579 * Not present in CTS or ACK control frames.
4581 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4582 IEEE80211_FC0_TYPE_MASK
);
4584 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4585 IEEE80211_FC0_SUBTYPE_MASK
);
4587 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4588 IEEE80211_FC0_SUBTYPE_MASK
);
4592 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4598 * Not present in control frames.
4600 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4601 IEEE80211_FC0_TYPE_MASK
);
4603 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4609 * Present only if the direction mask has both "From DS"
4610 * and "To DS" set. Neither control frames nor management
4611 * frames should have both of those set, so we don't
4612 * check the frame type.
4614 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4615 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4616 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4622 * Not present in management frames; addr1 in other
4627 * If the high-order bit of the type value is 0, this
4628 * is a management frame.
4629 * I.e, check "(link[0] & 0x08)".
4631 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4632 b1
= new_block(cstate
, JMP(BPF_JSET
));
4639 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4642 * AND that with the check of addr1.
4649 * Not present in management frames; addr2, if present,
4654 * Not present in CTS or ACK control frames.
4656 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4657 IEEE80211_FC0_TYPE_MASK
);
4659 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4660 IEEE80211_FC0_SUBTYPE_MASK
);
4662 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4663 IEEE80211_FC0_SUBTYPE_MASK
);
4669 * If the high-order bit of the type value is 0, this
4670 * is a management frame.
4671 * I.e, check "(link[0] & 0x08)".
4673 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4674 b1
= new_block(cstate
, JMP(BPF_JSET
));
4679 * AND that with the check for frames other than
4680 * CTS and ACK frames.
4687 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4696 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4697 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4698 * as the RFC states.)
4700 static struct block
*
4701 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4703 register struct block
*b0
, *b1
;
4707 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4710 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4713 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4714 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4720 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4721 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4726 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4730 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4734 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4738 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4742 bpf_error(cstate
, "'ra' is only supported on 802.11");
4746 bpf_error(cstate
, "'ta' is only supported on 802.11");
4754 * This is quite tricky because there may be pad bytes in front of the
4755 * DECNET header, and then there are two possible data packet formats that
4756 * carry both src and dst addresses, plus 5 packet types in a format that
4757 * carries only the src node, plus 2 types that use a different format and
4758 * also carry just the src node.
4762 * Instead of doing those all right, we just look for data packets with
4763 * 0 or 1 bytes of padding. If you want to look at other packets, that
4764 * will require a lot more hacking.
4766 * To add support for filtering on DECNET "areas" (network numbers)
4767 * one would want to add a "mask" argument to this routine. That would
4768 * make the filter even more inefficient, although one could be clever
4769 * and not generate masking instructions if the mask is 0xFFFF.
4771 static struct block
*
4772 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4774 struct block
*b0
, *b1
, *b2
, *tmp
;
4775 u_int offset_lh
; /* offset if long header is received */
4776 u_int offset_sh
; /* offset if short header is received */
4781 offset_sh
= 1; /* follows flags */
4782 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4786 offset_sh
= 3; /* follows flags, dstnode */
4787 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4791 /* Inefficient because we do our Calvinball dance twice */
4792 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4793 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4799 /* Inefficient because we do our Calvinball dance twice */
4800 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4801 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4806 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4810 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4814 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4818 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4822 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4826 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4833 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4834 /* Check for pad = 1, long header case */
4835 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4836 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4837 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4838 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4840 /* Check for pad = 0, long header case */
4841 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4843 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4844 (bpf_u_int32
)ntohs((u_short
)addr
));
4847 /* Check for pad = 1, short header case */
4848 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4849 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4850 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4851 (bpf_u_int32
)ntohs((u_short
)addr
));
4854 /* Check for pad = 0, short header case */
4855 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4857 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4858 (bpf_u_int32
)ntohs((u_short
)addr
));
4862 /* Combine with test for cstate->linktype */
4868 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4869 * test the bottom-of-stack bit, and then check the version number
4870 * field in the IP header.
4872 static struct block
*
4873 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4875 struct block
*b0
, *b1
;
4880 /* match the bottom-of-stack bit */
4881 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4882 /* match the IPv4 version number */
4883 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4887 case ETHERTYPE_IPV6
:
4888 /* match the bottom-of-stack bit */
4889 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4890 /* match the IPv4 version number */
4891 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4896 /* FIXME add other L3 proto IDs */
4897 bpf_error(cstate
, "unsupported protocol over mpls");
4902 static struct block
*
4903 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4904 int proto
, int dir
, int type
)
4906 struct block
*b0
, *b1
;
4907 const char *typestr
;
4917 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4919 * Only check for non-IPv4 addresses if we're not
4920 * checking MPLS-encapsulated packets.
4922 if (cstate
->label_stack_depth
== 0) {
4923 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4925 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4931 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4934 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4937 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4940 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4943 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4946 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4949 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4952 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4955 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4958 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4961 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4964 return gen_dnhostop(cstate
, addr
, dir
);
4967 bpf_error(cstate
, "LAT host filtering not implemented");
4970 bpf_error(cstate
, "SCA host filtering not implemented");
4973 bpf_error(cstate
, "MOPRC host filtering not implemented");
4976 bpf_error(cstate
, "MOPDL host filtering not implemented");
4979 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4982 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4985 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4988 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4991 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4994 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4997 bpf_error(cstate
, "AARP host filtering not implemented");
5000 bpf_error(cstate
, "ISO host filtering not implemented");
5003 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5006 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5009 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5012 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5015 bpf_error(cstate
, "IPX host filtering not implemented");
5018 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5021 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5024 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5027 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5030 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5033 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5036 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5039 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5042 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5045 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5054 static struct block
*
5055 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5056 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5058 const char *typestr
;
5068 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5071 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5074 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5077 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5080 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5083 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5086 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5089 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5092 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5095 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5098 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5101 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5104 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5107 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5110 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5113 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5116 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5119 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5122 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5125 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5128 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5131 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5134 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5137 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5140 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5143 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5146 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5149 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5152 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5155 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5158 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5161 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5164 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5167 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5170 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5173 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5176 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5179 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5182 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5185 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5195 static struct block
*
5196 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5197 struct addrinfo
*alist
, int proto
, int dir
)
5199 struct block
*b0
, *b1
, *tmp
;
5200 struct addrinfo
*ai
;
5201 struct sockaddr_in
*sin
;
5204 bpf_error(cstate
, "direction applied to 'gateway'");
5211 switch (cstate
->linktype
) {
5213 case DLT_NETANALYZER
:
5214 case DLT_NETANALYZER_TRANSPARENT
:
5215 b1
= gen_prevlinkhdr_check(cstate
);
5216 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5221 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5224 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5226 case DLT_IEEE802_11
:
5227 case DLT_PRISM_HEADER
:
5228 case DLT_IEEE802_11_RADIO_AVS
:
5229 case DLT_IEEE802_11_RADIO
:
5231 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5235 * This is LLC-multiplexed traffic; if it were
5236 * LANE, cstate->linktype would have been set to
5240 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5242 case DLT_IP_OVER_FC
:
5243 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5247 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5250 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5252 * Does it have an address?
5254 if (ai
->ai_addr
!= NULL
) {
5256 * Yes. Is it an IPv4 address?
5258 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5260 * Generate an entry for it.
5262 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5263 tmp
= gen_host(cstate
,
5264 ntohl(sin
->sin_addr
.s_addr
),
5265 0xffffffff, proto
, Q_OR
, Q_HOST
);
5267 * Is it the *first* IPv4 address?
5271 * Yes, so start with it.
5276 * No, so OR it into the
5288 * No IPv4 addresses found.
5296 bpf_error(cstate
, "illegal modifier of 'gateway'");
5301 static struct block
*
5302 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5310 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5311 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5316 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5317 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5322 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5323 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5328 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5331 #ifndef IPPROTO_IGMP
5332 #define IPPROTO_IGMP 2
5336 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5339 #ifndef IPPROTO_IGRP
5340 #define IPPROTO_IGRP 9
5343 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5347 #define IPPROTO_PIM 103
5351 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5352 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5356 #ifndef IPPROTO_VRRP
5357 #define IPPROTO_VRRP 112
5361 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5364 #ifndef IPPROTO_CARP
5365 #define IPPROTO_CARP 112
5369 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5373 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5377 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5381 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5385 bpf_error(cstate
, "link layer applied in wrong context");
5388 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5392 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5396 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5400 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5404 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5408 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5412 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5416 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5419 #ifndef IPPROTO_ICMPV6
5420 #define IPPROTO_ICMPV6 58
5423 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5427 #define IPPROTO_AH 51
5430 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5431 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5436 #define IPPROTO_ESP 50
5439 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5440 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5445 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5449 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5453 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5456 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5457 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5458 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5460 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5462 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5464 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5468 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5469 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5470 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5472 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5474 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5476 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5480 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5481 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5482 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5484 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5489 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5490 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5495 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5496 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5498 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5500 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5505 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5506 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5511 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5512 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5517 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5521 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5525 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5529 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5533 bpf_error(cstate
, "'radio' is not a valid protocol type");
5542 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5545 * Catch errors reported by us and routines below us, and return NULL
5548 if (setjmp(cstate
->top_ctx
))
5551 return gen_proto_abbrev_internal(cstate
, proto
);
5554 static struct block
*
5555 gen_ipfrag(compiler_state_t
*cstate
)
5560 /* not IPv4 frag other than the first frag */
5561 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5562 b
= new_block(cstate
, JMP(BPF_JSET
));
5571 * Generate a comparison to a port value in the transport-layer header
5572 * at the specified offset from the beginning of that header.
5574 * XXX - this handles a variable-length prefix preceding the link-layer
5575 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5576 * variable-length link-layer headers (such as Token Ring or 802.11
5579 static struct block
*
5580 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5582 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5585 static struct block
*
5586 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5588 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5591 static struct block
*
5592 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5594 struct block
*b0
, *b1
, *tmp
;
5596 /* ip proto 'proto' and not a fragment other than the first fragment */
5597 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5598 b0
= gen_ipfrag(cstate
);
5603 b1
= gen_portatom(cstate
, 0, port
);
5607 b1
= gen_portatom(cstate
, 2, port
);
5611 tmp
= gen_portatom(cstate
, 0, port
);
5612 b1
= gen_portatom(cstate
, 2, port
);
5618 tmp
= gen_portatom(cstate
, 0, port
);
5619 b1
= gen_portatom(cstate
, 2, port
);
5624 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5628 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5632 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5636 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5640 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5644 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5656 static struct block
*
5657 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5659 struct block
*b0
, *b1
, *tmp
;
5664 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5665 * not LLC encapsulation with LLCSAP_IP.
5667 * For IEEE 802 networks - which includes 802.5 token ring
5668 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5669 * says that SNAP encapsulation is used, not LLC encapsulation
5672 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5673 * RFC 2225 say that SNAP encapsulation is used, not LLC
5674 * encapsulation with LLCSAP_IP.
5676 * So we always check for ETHERTYPE_IP.
5678 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5684 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5688 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5689 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5691 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5703 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5705 struct block
*b0
, *b1
, *tmp
;
5707 /* ip6 proto 'proto' */
5708 /* XXX - catch the first fragment of a fragmented packet? */
5709 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5713 b1
= gen_portatom6(cstate
, 0, port
);
5717 b1
= gen_portatom6(cstate
, 2, port
);
5721 tmp
= gen_portatom6(cstate
, 0, port
);
5722 b1
= gen_portatom6(cstate
, 2, port
);
5728 tmp
= gen_portatom6(cstate
, 0, port
);
5729 b1
= gen_portatom6(cstate
, 2, port
);
5741 static struct block
*
5742 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5744 struct block
*b0
, *b1
, *tmp
;
5746 /* link proto ip6 */
5747 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5753 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5757 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5758 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5760 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5771 /* gen_portrange code */
5772 static struct block
*
5773 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5776 struct block
*b1
, *b2
;
5780 * Reverse the order of the ports, so v1 is the lower one.
5789 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5790 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5797 static struct block
*
5798 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5799 bpf_u_int32 proto
, int dir
)
5801 struct block
*b0
, *b1
, *tmp
;
5803 /* ip proto 'proto' and not a fragment other than the first fragment */
5804 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5805 b0
= gen_ipfrag(cstate
);
5810 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5814 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5818 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5819 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5825 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5826 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5831 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5835 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5839 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5843 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5847 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5851 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5863 static struct block
*
5864 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5867 struct block
*b0
, *b1
, *tmp
;
5870 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5876 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5881 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5882 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5884 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5895 static struct block
*
5896 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5899 struct block
*b1
, *b2
;
5903 * Reverse the order of the ports, so v1 is the lower one.
5912 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5913 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5920 static struct block
*
5921 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5922 bpf_u_int32 proto
, int dir
)
5924 struct block
*b0
, *b1
, *tmp
;
5926 /* ip6 proto 'proto' */
5927 /* XXX - catch the first fragment of a fragmented packet? */
5928 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5932 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5936 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5940 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5941 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5947 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5948 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5960 static struct block
*
5961 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5964 struct block
*b0
, *b1
, *tmp
;
5966 /* link proto ip6 */
5967 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5973 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5978 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5979 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5981 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5993 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6002 v
= pcap_nametoproto(name
);
6003 if (v
== PROTO_UNDEF
)
6004 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6008 /* XXX should look up h/w protocol type based on cstate->linktype */
6009 v
= pcap_nametoeproto(name
);
6010 if (v
== PROTO_UNDEF
) {
6011 v
= pcap_nametollc(name
);
6012 if (v
== PROTO_UNDEF
)
6013 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6018 if (strcmp(name
, "esis") == 0)
6020 else if (strcmp(name
, "isis") == 0)
6022 else if (strcmp(name
, "clnp") == 0)
6025 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6037 gen_joinsp(struct stmt
**s
, int n
)
6043 static struct block
*
6044 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6046 #ifdef NO_PROTOCHAIN
6047 return gen_proto(cstate
, v
, proto
);
6049 struct block
*b0
, *b
;
6050 struct slist
*s
[100];
6051 int fix2
, fix3
, fix4
, fix5
;
6052 int ahcheck
, again
, end
;
6054 int reg2
= alloc_reg(cstate
);
6056 memset(s
, 0, sizeof(s
));
6057 fix3
= fix4
= fix5
= 0;
6064 b0
= gen_protochain(cstate
, v
, Q_IP
);
6065 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6069 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6074 * We don't handle variable-length prefixes before the link-layer
6075 * header, or variable-length link-layer headers, here yet.
6076 * We might want to add BPF instructions to do the protochain
6077 * work, to simplify that and, on platforms that have a BPF
6078 * interpreter with the new instructions, let the filtering
6079 * be done in the kernel. (We already require a modified BPF
6080 * engine to do the protochain stuff, to support backward
6081 * branches, and backward branch support is unlikely to appear
6082 * in kernel BPF engines.)
6084 if (cstate
->off_linkpl
.is_variable
)
6085 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6087 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
6090 * s[0] is a dummy entry to protect other BPF insn from damage
6091 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6092 * hard to find interdependency made by jump table fixup.
6095 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6100 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6103 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6104 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6106 /* X = ip->ip_hl << 2 */
6107 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6108 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6113 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6115 /* A = ip6->ip_nxt */
6116 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6117 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6119 /* X = sizeof(struct ip6_hdr) */
6120 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6126 bpf_error(cstate
, "unsupported proto to gen_protochain");
6130 /* again: if (A == v) goto end; else fall through; */
6132 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6134 s
[i
]->s
.jt
= NULL
; /*later*/
6135 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6139 #ifndef IPPROTO_NONE
6140 #define IPPROTO_NONE 59
6142 /* if (A == IPPROTO_NONE) goto end */
6143 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6144 s
[i
]->s
.jt
= NULL
; /*later*/
6145 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6146 s
[i
]->s
.k
= IPPROTO_NONE
;
6147 s
[fix5
]->s
.jf
= s
[i
];
6151 if (proto
== Q_IPV6
) {
6152 int v6start
, v6end
, v6advance
, j
;
6155 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6156 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6157 s
[i
]->s
.jt
= NULL
; /*later*/
6158 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6159 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6160 s
[fix2
]->s
.jf
= s
[i
];
6162 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6163 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6164 s
[i
]->s
.jt
= NULL
; /*later*/
6165 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6166 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6168 /* if (A == IPPROTO_ROUTING) goto v6advance */
6169 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6170 s
[i
]->s
.jt
= NULL
; /*later*/
6171 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6172 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6174 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6175 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6176 s
[i
]->s
.jt
= NULL
; /*later*/
6177 s
[i
]->s
.jf
= NULL
; /*later*/
6178 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6188 * A = P[X + packet head];
6189 * X = X + (P[X + packet head + 1] + 1) * 8;
6191 /* A = P[X + packet head] */
6192 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6193 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6196 s
[i
] = new_stmt(cstate
, BPF_ST
);
6199 /* A = P[X + packet head + 1]; */
6200 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6201 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6204 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6208 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6212 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6216 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6219 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6223 /* goto again; (must use BPF_JA for backward jump) */
6224 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6225 s
[i
]->s
.k
= again
- i
- 1;
6226 s
[i
- 1]->s
.jf
= s
[i
];
6230 for (j
= v6start
; j
<= v6end
; j
++)
6231 s
[j
]->s
.jt
= s
[v6advance
];
6234 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6236 s
[fix2
]->s
.jf
= s
[i
];
6242 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6243 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6244 s
[i
]->s
.jt
= NULL
; /*later*/
6245 s
[i
]->s
.jf
= NULL
; /*later*/
6246 s
[i
]->s
.k
= IPPROTO_AH
;
6248 s
[fix3
]->s
.jf
= s
[ahcheck
];
6255 * X = X + (P[X + 1] + 2) * 4;
6258 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6260 /* A = P[X + packet head]; */
6261 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6262 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6265 s
[i
] = new_stmt(cstate
, BPF_ST
);
6269 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6272 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6276 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6278 /* A = P[X + packet head] */
6279 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6280 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6283 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6287 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6291 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6294 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6298 /* goto again; (must use BPF_JA for backward jump) */
6299 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6300 s
[i
]->s
.k
= again
- i
- 1;
6305 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6307 s
[fix2
]->s
.jt
= s
[end
];
6308 s
[fix4
]->s
.jf
= s
[end
];
6309 s
[fix5
]->s
.jt
= s
[end
];
6316 for (i
= 0; i
< max
- 1; i
++)
6317 s
[i
]->next
= s
[i
+ 1];
6318 s
[max
- 1]->next
= NULL
;
6323 b
= new_block(cstate
, JMP(BPF_JEQ
));
6324 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6327 free_reg(cstate
, reg2
);
6334 static struct block
*
6335 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6338 struct block
*b0
, *b1
;
6341 * A data frame has the 0x08 bit (b3) in the frame control field set
6342 * and the 0x04 bit (b2) clear.
6344 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6345 b0
= new_block(cstate
, JMP(BPF_JSET
));
6349 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6350 b1
= new_block(cstate
, JMP(BPF_JSET
));
6361 * Generate code that checks whether the packet is a packet for protocol
6362 * <proto> and whether the type field in that protocol's header has
6363 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6364 * IP packet and checks the protocol number in the IP header against <v>.
6366 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6367 * against Q_IP and Q_IPV6.
6369 static struct block
*
6370 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6372 struct block
*b0
, *b1
;
6377 if (dir
!= Q_DEFAULT
)
6378 bpf_error(cstate
, "direction applied to 'proto'");
6382 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6383 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6388 return gen_linktype(cstate
, v
);
6392 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6393 * not LLC encapsulation with LLCSAP_IP.
6395 * For IEEE 802 networks - which includes 802.5 token ring
6396 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6397 * says that SNAP encapsulation is used, not LLC encapsulation
6400 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6401 * RFC 2225 say that SNAP encapsulation is used, not LLC
6402 * encapsulation with LLCSAP_IP.
6404 * So we always check for ETHERTYPE_IP.
6406 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6408 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6410 b1
= gen_protochain(cstate
, v
, Q_IP
);
6416 bpf_error(cstate
, "arp does not encapsulate another protocol");
6420 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6424 bpf_error(cstate
, "'sctp proto' is bogus");
6428 bpf_error(cstate
, "'tcp proto' is bogus");
6432 bpf_error(cstate
, "'udp proto' is bogus");
6436 bpf_error(cstate
, "'icmp proto' is bogus");
6440 bpf_error(cstate
, "'igmp proto' is bogus");
6444 bpf_error(cstate
, "'igrp proto' is bogus");
6448 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6452 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6456 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6460 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6464 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6468 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6472 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6475 * Also check for a fragment header before the final
6478 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6479 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6481 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6484 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6490 bpf_error(cstate
, "'icmp6 proto' is bogus");
6494 bpf_error(cstate
, "'ah proto' is bogus");
6498 bpf_error(cstate
, "'ah proto' is bogus");
6502 bpf_error(cstate
, "'pim proto' is bogus");
6506 bpf_error(cstate
, "'vrrp proto' is bogus");
6510 bpf_error(cstate
, "'aarp proto' is bogus");
6514 switch (cstate
->linktype
) {
6518 * Frame Relay packets typically have an OSI
6519 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6520 * generates code to check for all the OSI
6521 * NLPIDs, so calling it and then adding a check
6522 * for the particular NLPID for which we're
6523 * looking is bogus, as we can just check for
6526 * What we check for is the NLPID and a frame
6527 * control field value of UI, i.e. 0x03 followed
6530 * XXX - assumes a 2-byte Frame Relay header with
6531 * DLCI and flags. What if the address is longer?
6533 * XXX - what about SNAP-encapsulated frames?
6535 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6540 * Cisco uses an Ethertype lookalike - for OSI,
6543 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6544 /* OSI in C-HDLC is stuffed with a fudge byte */
6545 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6550 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6551 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6557 bpf_error(cstate
, "'esis proto' is bogus");
6561 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6563 * 4 is the offset of the PDU type relative to the IS-IS
6566 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6571 bpf_error(cstate
, "'clnp proto' is not supported");
6575 bpf_error(cstate
, "'stp proto' is bogus");
6579 bpf_error(cstate
, "'ipx proto' is bogus");
6583 bpf_error(cstate
, "'netbeui proto' is bogus");
6587 bpf_error(cstate
, "'l1 proto' is bogus");
6591 bpf_error(cstate
, "'l2 proto' is bogus");
6595 bpf_error(cstate
, "'iih proto' is bogus");
6599 bpf_error(cstate
, "'snp proto' is bogus");
6603 bpf_error(cstate
, "'csnp proto' is bogus");
6607 bpf_error(cstate
, "'psnp proto' is bogus");
6611 bpf_error(cstate
, "'lsp proto' is bogus");
6615 bpf_error(cstate
, "'radio proto' is bogus");
6619 bpf_error(cstate
, "'carp proto' is bogus");
6630 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6632 int proto
= q
.proto
;
6636 bpf_u_int32 mask
, addr
;
6637 struct addrinfo
*res
, *res0
;
6638 struct sockaddr_in
*sin4
;
6641 struct sockaddr_in6
*sin6
;
6642 struct in6_addr mask128
;
6644 struct block
*b
, *tmp
;
6645 int port
, real_proto
;
6649 * Catch errors reported by us and routines below us, and return NULL
6652 if (setjmp(cstate
->top_ctx
))
6658 addr
= pcap_nametonetaddr(name
);
6660 bpf_error(cstate
, "unknown network '%s'", name
);
6661 /* Left justify network addr and calculate its network mask */
6663 while (addr
&& (addr
& 0xff000000) == 0) {
6667 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6671 if (proto
== Q_LINK
) {
6672 switch (cstate
->linktype
) {
6675 case DLT_NETANALYZER
:
6676 case DLT_NETANALYZER_TRANSPARENT
:
6677 eaddr
= pcap_ether_hostton(name
);
6680 "unknown ether host '%s'", name
);
6681 tmp
= gen_prevlinkhdr_check(cstate
);
6682 b
= gen_ehostop(cstate
, eaddr
, dir
);
6689 eaddr
= pcap_ether_hostton(name
);
6692 "unknown FDDI host '%s'", name
);
6693 b
= gen_fhostop(cstate
, eaddr
, dir
);
6698 eaddr
= pcap_ether_hostton(name
);
6701 "unknown token ring host '%s'", name
);
6702 b
= gen_thostop(cstate
, eaddr
, dir
);
6706 case DLT_IEEE802_11
:
6707 case DLT_PRISM_HEADER
:
6708 case DLT_IEEE802_11_RADIO_AVS
:
6709 case DLT_IEEE802_11_RADIO
:
6711 eaddr
= pcap_ether_hostton(name
);
6714 "unknown 802.11 host '%s'", name
);
6715 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6719 case DLT_IP_OVER_FC
:
6720 eaddr
= pcap_ether_hostton(name
);
6723 "unknown Fibre Channel host '%s'", name
);
6724 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6729 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6730 } else if (proto
== Q_DECNET
) {
6731 unsigned short dn_addr
;
6733 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6735 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6737 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6742 * I don't think DECNET hosts can be multihomed, so
6743 * there is no need to build up a list of addresses
6745 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6748 memset(&mask128
, 0xff, sizeof(mask128
));
6750 res0
= res
= pcap_nametoaddrinfo(name
);
6752 bpf_error(cstate
, "unknown host '%s'", name
);
6759 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6760 tproto
== Q_DEFAULT
) {
6766 for (res
= res0
; res
; res
= res
->ai_next
) {
6767 switch (res
->ai_family
) {
6770 if (tproto
== Q_IPV6
)
6774 sin4
= (struct sockaddr_in
*)
6776 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6777 0xffffffff, tproto
, dir
, q
.addr
);
6781 if (tproto6
== Q_IP
)
6784 sin6
= (struct sockaddr_in6
*)
6786 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6787 &mask128
, tproto6
, dir
, q
.addr
);
6800 bpf_error(cstate
, "unknown host '%s'%s", name
,
6801 (proto
== Q_DEFAULT
)
6803 : " for specified address family");
6809 if (proto
!= Q_DEFAULT
&&
6810 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6811 bpf_error(cstate
, "illegal qualifier of 'port'");
6812 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6813 bpf_error(cstate
, "unknown port '%s'", name
);
6814 if (proto
== Q_UDP
) {
6815 if (real_proto
== IPPROTO_TCP
)
6816 bpf_error(cstate
, "port '%s' is tcp", name
);
6817 else if (real_proto
== IPPROTO_SCTP
)
6818 bpf_error(cstate
, "port '%s' is sctp", name
);
6820 /* override PROTO_UNDEF */
6821 real_proto
= IPPROTO_UDP
;
6823 if (proto
== Q_TCP
) {
6824 if (real_proto
== IPPROTO_UDP
)
6825 bpf_error(cstate
, "port '%s' is udp", name
);
6827 else if (real_proto
== IPPROTO_SCTP
)
6828 bpf_error(cstate
, "port '%s' is sctp", name
);
6830 /* override PROTO_UNDEF */
6831 real_proto
= IPPROTO_TCP
;
6833 if (proto
== Q_SCTP
) {
6834 if (real_proto
== IPPROTO_UDP
)
6835 bpf_error(cstate
, "port '%s' is udp", name
);
6837 else if (real_proto
== IPPROTO_TCP
)
6838 bpf_error(cstate
, "port '%s' is tcp", name
);
6840 /* override PROTO_UNDEF */
6841 real_proto
= IPPROTO_SCTP
;
6844 bpf_error(cstate
, "illegal port number %d < 0", port
);
6846 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6847 b
= gen_port(cstate
, port
, real_proto
, dir
);
6848 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6852 if (proto
!= Q_DEFAULT
&&
6853 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6854 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6855 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6856 bpf_error(cstate
, "unknown port in range '%s'", name
);
6857 if (proto
== Q_UDP
) {
6858 if (real_proto
== IPPROTO_TCP
)
6859 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6860 else if (real_proto
== IPPROTO_SCTP
)
6861 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6863 /* override PROTO_UNDEF */
6864 real_proto
= IPPROTO_UDP
;
6866 if (proto
== Q_TCP
) {
6867 if (real_proto
== IPPROTO_UDP
)
6868 bpf_error(cstate
, "port in range '%s' is udp", name
);
6869 else if (real_proto
== IPPROTO_SCTP
)
6870 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6872 /* override PROTO_UNDEF */
6873 real_proto
= IPPROTO_TCP
;
6875 if (proto
== Q_SCTP
) {
6876 if (real_proto
== IPPROTO_UDP
)
6877 bpf_error(cstate
, "port in range '%s' is udp", name
);
6878 else if (real_proto
== IPPROTO_TCP
)
6879 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6881 /* override PROTO_UNDEF */
6882 real_proto
= IPPROTO_SCTP
;
6885 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6887 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6889 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6891 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6893 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6894 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6899 eaddr
= pcap_ether_hostton(name
);
6901 bpf_error(cstate
, "unknown ether host: %s", name
);
6903 res
= pcap_nametoaddrinfo(name
);
6906 bpf_error(cstate
, "unknown host '%s'", name
);
6907 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6911 bpf_error(cstate
, "unknown host '%s'", name
);
6914 bpf_error(cstate
, "'gateway' not supported in this configuration");
6918 real_proto
= lookup_proto(cstate
, name
, proto
);
6919 if (real_proto
>= 0)
6920 return gen_proto(cstate
, real_proto
, proto
, dir
);
6922 bpf_error(cstate
, "unknown protocol: %s", name
);
6925 real_proto
= lookup_proto(cstate
, name
, proto
);
6926 if (real_proto
>= 0)
6927 return gen_protochain(cstate
, real_proto
, proto
);
6929 bpf_error(cstate
, "unknown protocol: %s", name
);
6940 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6941 bpf_u_int32 masklen
, struct qual q
)
6943 register int nlen
, mlen
;
6947 * Catch errors reported by us and routines below us, and return NULL
6950 if (setjmp(cstate
->top_ctx
))
6953 nlen
= __pcap_atoin(s1
, &n
);
6954 /* Promote short ipaddr */
6958 mlen
= __pcap_atoin(s2
, &m
);
6959 /* Promote short ipaddr */
6962 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6965 /* Convert mask len to mask */
6967 bpf_error(cstate
, "mask length must be <= 32");
6970 * X << 32 is not guaranteed by C to be 0; it's
6975 m
= 0xffffffff << (32 - masklen
);
6977 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6984 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6987 bpf_error(cstate
, "Mask syntax for networks only");
6994 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7002 * Catch errors reported by us and routines below us, and return NULL
7005 if (setjmp(cstate
->top_ctx
))
7012 else if (q
.proto
== Q_DECNET
) {
7013 vlen
= __pcap_atodn(s
, &v
);
7015 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7017 vlen
= __pcap_atoin(s
, &v
);
7024 if (proto
== Q_DECNET
)
7025 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7026 else if (proto
== Q_LINK
) {
7027 bpf_error(cstate
, "illegal link layer address");
7030 if (s
== NULL
&& q
.addr
== Q_NET
) {
7031 /* Promote short net number */
7032 while (v
&& (v
& 0xff000000) == 0) {
7037 /* Promote short ipaddr */
7039 mask
<<= 32 - vlen
;
7041 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7046 proto
= IPPROTO_UDP
;
7047 else if (proto
== Q_TCP
)
7048 proto
= IPPROTO_TCP
;
7049 else if (proto
== Q_SCTP
)
7050 proto
= IPPROTO_SCTP
;
7051 else if (proto
== Q_DEFAULT
)
7052 proto
= PROTO_UNDEF
;
7054 bpf_error(cstate
, "illegal qualifier of 'port'");
7057 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7061 b
= gen_port(cstate
, v
, proto
, dir
);
7062 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7068 proto
= IPPROTO_UDP
;
7069 else if (proto
== Q_TCP
)
7070 proto
= IPPROTO_TCP
;
7071 else if (proto
== Q_SCTP
)
7072 proto
= IPPROTO_SCTP
;
7073 else if (proto
== Q_DEFAULT
)
7074 proto
= PROTO_UNDEF
;
7076 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7079 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7083 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7084 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7089 bpf_error(cstate
, "'gateway' requires a name");
7093 return gen_proto(cstate
, v
, proto
, dir
);
7096 return gen_protochain(cstate
, v
, proto
);
7111 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7112 bpf_u_int32 masklen
, struct qual q
)
7114 struct addrinfo
*res
;
7115 struct in6_addr
*addr
;
7116 struct in6_addr mask
;
7121 * Catch errors reported by us and routines below us, and return NULL
7124 if (setjmp(cstate
->top_ctx
))
7128 bpf_error(cstate
, "no mask %s supported", s2
);
7130 res
= pcap_nametoaddrinfo(s1
);
7132 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7135 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7136 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7138 if (sizeof(mask
) * 8 < masklen
)
7139 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
7140 memset(&mask
, 0, sizeof(mask
));
7141 memset(&mask
, 0xff, masklen
/ 8);
7143 mask
.s6_addr
[masklen
/ 8] =
7144 (0xff << (8 - masklen
% 8)) & 0xff;
7147 a
= (uint32_t *)addr
;
7148 m
= (uint32_t *)&mask
;
7149 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7150 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7151 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7159 bpf_error(cstate
, "Mask syntax for networks only");
7163 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7169 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7176 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7178 struct block
*b
, *tmp
;
7181 * Catch errors reported by us and routines below us, and return NULL
7184 if (setjmp(cstate
->top_ctx
))
7187 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7188 cstate
->e
= pcap_ether_aton(s
);
7189 if (cstate
->e
== NULL
)
7190 bpf_error(cstate
, "malloc");
7191 switch (cstate
->linktype
) {
7193 case DLT_NETANALYZER
:
7194 case DLT_NETANALYZER_TRANSPARENT
:
7195 tmp
= gen_prevlinkhdr_check(cstate
);
7196 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7201 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7204 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7206 case DLT_IEEE802_11
:
7207 case DLT_PRISM_HEADER
:
7208 case DLT_IEEE802_11_RADIO_AVS
:
7209 case DLT_IEEE802_11_RADIO
:
7211 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7213 case DLT_IP_OVER_FC
:
7214 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7219 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7226 bpf_error(cstate
, "ethernet address used in non-ether expression");
7231 sappend(struct slist
*s0
, struct slist
*s1
)
7234 * This is definitely not the best way to do this, but the
7235 * lists will rarely get long.
7242 static struct slist
*
7243 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7247 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7252 static struct slist
*
7253 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7257 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7263 * Modify "index" to use the value stored into its register as an
7264 * offset relative to the beginning of the header for the protocol
7265 * "proto", and allocate a register and put an item "size" bytes long
7266 * (1, 2, or 4) at that offset into that register, making it the register
7269 static struct arth
*
7270 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7274 struct slist
*s
, *tmp
;
7276 int regno
= alloc_reg(cstate
);
7278 free_reg(cstate
, inst
->regno
);
7282 bpf_error(cstate
, "data size must be 1, 2, or 4");
7299 bpf_error(cstate
, "unsupported index operation");
7303 * The offset is relative to the beginning of the packet
7304 * data, if we have a radio header. (If we don't, this
7307 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7308 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7309 cstate
->linktype
!= DLT_PRISM_HEADER
)
7310 bpf_error(cstate
, "radio information not present in capture");
7313 * Load into the X register the offset computed into the
7314 * register specified by "index".
7316 s
= xfer_to_x(cstate
, inst
);
7319 * Load the item at that offset.
7321 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7323 sappend(inst
->s
, s
);
7328 * The offset is relative to the beginning of
7329 * the link-layer header.
7331 * XXX - what about ATM LANE? Should the index be
7332 * relative to the beginning of the AAL5 frame, so
7333 * that 0 refers to the beginning of the LE Control
7334 * field, or relative to the beginning of the LAN
7335 * frame, so that 0 refers, for Ethernet LANE, to
7336 * the beginning of the destination address?
7338 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7341 * If "s" is non-null, it has code to arrange that the
7342 * X register contains the length of the prefix preceding
7343 * the link-layer header. Add to it the offset computed
7344 * into the register specified by "index", and move that
7345 * into the X register. Otherwise, just load into the X
7346 * register the offset computed into the register specified
7350 sappend(s
, xfer_to_a(cstate
, inst
));
7351 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7352 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7354 s
= xfer_to_x(cstate
, inst
);
7357 * Load the item at the sum of the offset we've put in the
7358 * X register and the offset of the start of the link
7359 * layer header (which is 0 if the radio header is
7360 * variable-length; that header length is what we put
7361 * into the X register and then added to the index).
7363 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7364 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7366 sappend(inst
->s
, s
);
7380 * The offset is relative to the beginning of
7381 * the network-layer header.
7382 * XXX - are there any cases where we want
7383 * cstate->off_nl_nosnap?
7385 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7388 * If "s" is non-null, it has code to arrange that the
7389 * X register contains the variable part of the offset
7390 * of the link-layer payload. Add to it the offset
7391 * computed into the register specified by "index",
7392 * and move that into the X register. Otherwise, just
7393 * load into the X register the offset computed into
7394 * the register specified by "index".
7397 sappend(s
, xfer_to_a(cstate
, inst
));
7398 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7399 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7401 s
= xfer_to_x(cstate
, inst
);
7404 * Load the item at the sum of the offset we've put in the
7405 * X register, the offset of the start of the network
7406 * layer header from the beginning of the link-layer
7407 * payload, and the constant part of the offset of the
7408 * start of the link-layer payload.
7410 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7411 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7413 sappend(inst
->s
, s
);
7416 * Do the computation only if the packet contains
7417 * the protocol in question.
7419 b
= gen_proto_abbrev_internal(cstate
, proto
);
7421 gen_and(inst
->b
, b
);
7435 * The offset is relative to the beginning of
7436 * the transport-layer header.
7438 * Load the X register with the length of the IPv4 header
7439 * (plus the offset of the link-layer header, if it's
7440 * a variable-length header), in bytes.
7442 * XXX - are there any cases where we want
7443 * cstate->off_nl_nosnap?
7444 * XXX - we should, if we're built with
7445 * IPv6 support, generate code to load either
7446 * IPv4, IPv6, or both, as appropriate.
7448 s
= gen_loadx_iphdrlen(cstate
);
7451 * The X register now contains the sum of the variable
7452 * part of the offset of the link-layer payload and the
7453 * length of the network-layer header.
7455 * Load into the A register the offset relative to
7456 * the beginning of the transport layer header,
7457 * add the X register to that, move that to the
7458 * X register, and load with an offset from the
7459 * X register equal to the sum of the constant part of
7460 * the offset of the link-layer payload and the offset,
7461 * relative to the beginning of the link-layer payload,
7462 * of the network-layer header.
7464 sappend(s
, xfer_to_a(cstate
, inst
));
7465 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7466 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7467 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7468 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7469 sappend(inst
->s
, s
);
7472 * Do the computation only if the packet contains
7473 * the protocol in question - which is true only
7474 * if this is an IP datagram and is the first or
7475 * only fragment of that datagram.
7477 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7479 gen_and(inst
->b
, b
);
7480 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7485 * Do the computation only if the packet contains
7486 * the protocol in question.
7488 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7490 gen_and(inst
->b
, b
);
7495 * Check if we have an icmp6 next header
7497 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7499 gen_and(inst
->b
, b
);
7504 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7506 * If "s" is non-null, it has code to arrange that the
7507 * X register contains the variable part of the offset
7508 * of the link-layer payload. Add to it the offset
7509 * computed into the register specified by "index",
7510 * and move that into the X register. Otherwise, just
7511 * load into the X register the offset computed into
7512 * the register specified by "index".
7515 sappend(s
, xfer_to_a(cstate
, inst
));
7516 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7517 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7519 s
= xfer_to_x(cstate
, inst
);
7523 * Load the item at the sum of the offset we've put in the
7524 * X register, the offset of the start of the network
7525 * layer header from the beginning of the link-layer
7526 * payload, and the constant part of the offset of the
7527 * start of the link-layer payload.
7529 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7530 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7533 sappend(inst
->s
, s
);
7537 inst
->regno
= regno
;
7538 s
= new_stmt(cstate
, BPF_ST
);
7540 sappend(inst
->s
, s
);
7546 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7550 * Catch errors reported by us and routines below us, and return NULL
7553 if (setjmp(cstate
->top_ctx
))
7556 return gen_load_internal(cstate
, proto
, inst
, size
);
7559 static struct block
*
7560 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7561 struct arth
*a1
, int reversed
)
7563 struct slist
*s0
, *s1
, *s2
;
7564 struct block
*b
, *tmp
;
7566 s0
= xfer_to_x(cstate
, a1
);
7567 s1
= xfer_to_a(cstate
, a0
);
7568 if (code
== BPF_JEQ
) {
7569 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7570 b
= new_block(cstate
, JMP(code
));
7574 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7580 sappend(a0
->s
, a1
->s
);
7584 free_reg(cstate
, a0
->regno
);
7585 free_reg(cstate
, a1
->regno
);
7587 /* 'and' together protocol checks */
7590 gen_and(a0
->b
, tmp
= a1
->b
);
7604 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7605 struct arth
*a1
, int reversed
)
7608 * Catch errors reported by us and routines below us, and return NULL
7611 if (setjmp(cstate
->top_ctx
))
7614 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7618 gen_loadlen(compiler_state_t
*cstate
)
7625 * Catch errors reported by us and routines below us, and return NULL
7628 if (setjmp(cstate
->top_ctx
))
7631 regno
= alloc_reg(cstate
);
7632 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7633 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7634 s
->next
= new_stmt(cstate
, BPF_ST
);
7635 s
->next
->s
.k
= regno
;
7642 static struct arth
*
7643 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7649 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7651 reg
= alloc_reg(cstate
);
7653 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7655 s
->next
= new_stmt(cstate
, BPF_ST
);
7664 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7667 * Catch errors reported by us and routines below us, and return NULL
7670 if (setjmp(cstate
->top_ctx
))
7673 return gen_loadi_internal(cstate
, val
);
7677 * The a_arg dance is to avoid annoying whining by compilers that
7678 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7679 * It's not *used* after setjmp returns.
7682 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7684 struct arth
*a
= a_arg
;
7688 * Catch errors reported by us and routines below us, and return NULL
7691 if (setjmp(cstate
->top_ctx
))
7694 s
= xfer_to_a(cstate
, a
);
7696 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7699 s
= new_stmt(cstate
, BPF_ST
);
7707 * The a0_arg dance is to avoid annoying whining by compilers that
7708 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7709 * It's not *used* after setjmp returns.
7712 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7715 struct arth
*a0
= a0_arg
;
7716 struct slist
*s0
, *s1
, *s2
;
7719 * Catch errors reported by us and routines below us, and return NULL
7722 if (setjmp(cstate
->top_ctx
))
7726 * Disallow division by, or modulus by, zero; we do this here
7727 * so that it gets done even if the optimizer is disabled.
7729 * Also disallow shifts by a value greater than 31; we do this
7730 * here, for the same reason.
7732 if (code
== BPF_DIV
) {
7733 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7734 bpf_error(cstate
, "division by zero");
7735 } else if (code
== BPF_MOD
) {
7736 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7737 bpf_error(cstate
, "modulus by zero");
7738 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7739 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7740 bpf_error(cstate
, "shift by more than 31 bits");
7742 s0
= xfer_to_x(cstate
, a1
);
7743 s1
= xfer_to_a(cstate
, a0
);
7744 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7749 sappend(a0
->s
, a1
->s
);
7751 free_reg(cstate
, a0
->regno
);
7752 free_reg(cstate
, a1
->regno
);
7754 s0
= new_stmt(cstate
, BPF_ST
);
7755 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7762 * Initialize the table of used registers and the current register.
7765 init_regs(compiler_state_t
*cstate
)
7768 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7772 * Return the next free register.
7775 alloc_reg(compiler_state_t
*cstate
)
7777 int n
= BPF_MEMWORDS
;
7780 if (cstate
->regused
[cstate
->curreg
])
7781 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7783 cstate
->regused
[cstate
->curreg
] = 1;
7784 return cstate
->curreg
;
7787 bpf_error(cstate
, "too many registers needed to evaluate expression");
7792 * Return a register to the table so it can
7796 free_reg(compiler_state_t
*cstate
, int n
)
7798 cstate
->regused
[n
] = 0;
7801 static struct block
*
7802 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7807 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7808 b
= new_block(cstate
, JMP(jmp
));
7816 gen_greater(compiler_state_t
*cstate
, int n
)
7819 * Catch errors reported by us and routines below us, and return NULL
7822 if (setjmp(cstate
->top_ctx
))
7825 return gen_len(cstate
, BPF_JGE
, n
);
7829 * Actually, this is less than or equal.
7832 gen_less(compiler_state_t
*cstate
, int n
)
7837 * Catch errors reported by us and routines below us, and return NULL
7840 if (setjmp(cstate
->top_ctx
))
7843 b
= gen_len(cstate
, BPF_JGT
, n
);
7850 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7851 * the beginning of the link-layer header.
7852 * XXX - that means you can't test values in the radiotap header, but
7853 * as that header is difficult if not impossible to parse generally
7854 * without a loop, that might not be a severe problem. A new keyword
7855 * "radio" could be added for that, although what you'd really want
7856 * would be a way of testing particular radio header values, which
7857 * would generate code appropriate to the radio header in question.
7860 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7866 * Catch errors reported by us and routines below us, and return NULL
7869 if (setjmp(cstate
->top_ctx
))
7877 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7880 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7884 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7888 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7892 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7896 b
= new_block(cstate
, JMP(BPF_JEQ
));
7903 static const u_char abroadcast
[] = { 0x0 };
7906 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7908 bpf_u_int32 hostmask
;
7909 struct block
*b0
, *b1
, *b2
;
7910 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7913 * Catch errors reported by us and routines below us, and return NULL
7916 if (setjmp(cstate
->top_ctx
))
7923 switch (cstate
->linktype
) {
7925 case DLT_ARCNET_LINUX
:
7926 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7928 case DLT_NETANALYZER
:
7929 case DLT_NETANALYZER_TRANSPARENT
:
7930 b1
= gen_prevlinkhdr_check(cstate
);
7931 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7936 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7938 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7939 case DLT_IEEE802_11
:
7940 case DLT_PRISM_HEADER
:
7941 case DLT_IEEE802_11_RADIO_AVS
:
7942 case DLT_IEEE802_11_RADIO
:
7944 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7945 case DLT_IP_OVER_FC
:
7946 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7948 bpf_error(cstate
, "not a broadcast link");
7954 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7955 * as an indication that we don't know the netmask, and fail
7958 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7959 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7960 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7961 hostmask
= ~cstate
->netmask
;
7962 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
7963 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7964 ~0 & hostmask
, hostmask
);
7969 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7974 * Generate code to test the low-order bit of a MAC address (that's
7975 * the bottom bit of the *first* byte).
7977 static struct block
*
7978 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7980 register struct block
*b0
;
7981 register struct slist
*s
;
7983 /* link[offset] & 1 != 0 */
7984 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7985 b0
= new_block(cstate
, JMP(BPF_JSET
));
7992 gen_multicast(compiler_state_t
*cstate
, int proto
)
7994 register struct block
*b0
, *b1
, *b2
;
7995 register struct slist
*s
;
7998 * Catch errors reported by us and routines below us, and return NULL
8001 if (setjmp(cstate
->top_ctx
))
8008 switch (cstate
->linktype
) {
8010 case DLT_ARCNET_LINUX
:
8011 /* all ARCnet multicasts use the same address */
8012 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8014 case DLT_NETANALYZER
:
8015 case DLT_NETANALYZER_TRANSPARENT
:
8016 b1
= gen_prevlinkhdr_check(cstate
);
8017 /* ether[0] & 1 != 0 */
8018 b0
= gen_mac_multicast(cstate
, 0);
8024 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8026 * XXX - was that referring to bit-order issues?
8028 /* fddi[1] & 1 != 0 */
8029 return gen_mac_multicast(cstate
, 1);
8031 /* tr[2] & 1 != 0 */
8032 return gen_mac_multicast(cstate
, 2);
8033 case DLT_IEEE802_11
:
8034 case DLT_PRISM_HEADER
:
8035 case DLT_IEEE802_11_RADIO_AVS
:
8036 case DLT_IEEE802_11_RADIO
:
8041 * For control frames, there is no DA.
8043 * For management frames, DA is at an
8044 * offset of 4 from the beginning of
8047 * For data frames, DA is at an offset
8048 * of 4 from the beginning of the packet
8049 * if To DS is clear and at an offset of
8050 * 16 from the beginning of the packet
8055 * Generate the tests to be done for data frames.
8057 * First, check for To DS set, i.e. "link[1] & 0x01".
8059 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8060 b1
= new_block(cstate
, JMP(BPF_JSET
));
8061 b1
->s
.k
= 0x01; /* To DS */
8065 * If To DS is set, the DA is at 16.
8067 b0
= gen_mac_multicast(cstate
, 16);
8071 * Now, check for To DS not set, i.e. check
8072 * "!(link[1] & 0x01)".
8074 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8075 b2
= new_block(cstate
, JMP(BPF_JSET
));
8076 b2
->s
.k
= 0x01; /* To DS */
8081 * If To DS is not set, the DA is at 4.
8083 b1
= gen_mac_multicast(cstate
, 4);
8087 * Now OR together the last two checks. That gives
8088 * the complete set of checks for data frames.
8093 * Now check for a data frame.
8094 * I.e, check "link[0] & 0x08".
8096 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8097 b1
= new_block(cstate
, JMP(BPF_JSET
));
8102 * AND that with the checks done for data frames.
8107 * If the high-order bit of the type value is 0, this
8108 * is a management frame.
8109 * I.e, check "!(link[0] & 0x08)".
8111 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8112 b2
= new_block(cstate
, JMP(BPF_JSET
));
8118 * For management frames, the DA is at 4.
8120 b1
= gen_mac_multicast(cstate
, 4);
8124 * OR that with the checks done for data frames.
8125 * That gives the checks done for management and
8131 * If the low-order bit of the type value is 1,
8132 * this is either a control frame or a frame
8133 * with a reserved type, and thus not a
8136 * I.e., check "!(link[0] & 0x04)".
8138 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8139 b1
= new_block(cstate
, JMP(BPF_JSET
));
8145 * AND that with the checks for data and management
8150 case DLT_IP_OVER_FC
:
8151 b0
= gen_mac_multicast(cstate
, 2);
8156 /* Link not known to support multicasts */
8160 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8161 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8166 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8167 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8171 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8176 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8177 * Outbound traffic is sent by this machine, while inbound traffic is
8178 * sent by a remote machine (and may include packets destined for a
8179 * unicast or multicast link-layer address we are not subscribing to).
8180 * These are the same definitions implemented by pcap_setdirection().
8181 * Capturing only unicast traffic destined for this host is probably
8182 * better accomplished using a higher-layer filter.
8185 gen_inbound(compiler_state_t
*cstate
, int dir
)
8187 register struct block
*b0
;
8190 * Catch errors reported by us and routines below us, and return NULL
8193 if (setjmp(cstate
->top_ctx
))
8197 * Only some data link types support inbound/outbound qualifiers.
8199 switch (cstate
->linktype
) {
8201 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8202 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8203 gen_loadi_internal(cstate
, 0),
8209 /* match outgoing packets */
8210 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8212 /* match incoming packets */
8213 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8218 /* match outgoing packets */
8219 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8221 /* to filter on inbound traffic, invert the match */
8226 case DLT_LINUX_SLL2
:
8227 /* match outgoing packets */
8228 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8230 /* to filter on inbound traffic, invert the match */
8235 #ifdef HAVE_NET_PFVAR_H
8237 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8238 ((dir
== 0) ? PF_IN
: PF_OUT
));
8244 /* match outgoing packets */
8245 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8247 /* match incoming packets */
8248 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8252 case DLT_JUNIPER_MFR
:
8253 case DLT_JUNIPER_MLFR
:
8254 case DLT_JUNIPER_MLPPP
:
8255 case DLT_JUNIPER_ATM1
:
8256 case DLT_JUNIPER_ATM2
:
8257 case DLT_JUNIPER_PPPOE
:
8258 case DLT_JUNIPER_PPPOE_ATM
:
8259 case DLT_JUNIPER_GGSN
:
8260 case DLT_JUNIPER_ES
:
8261 case DLT_JUNIPER_MONITOR
:
8262 case DLT_JUNIPER_SERVICES
:
8263 case DLT_JUNIPER_ETHER
:
8264 case DLT_JUNIPER_PPP
:
8265 case DLT_JUNIPER_FRELAY
:
8266 case DLT_JUNIPER_CHDLC
:
8267 case DLT_JUNIPER_VP
:
8268 case DLT_JUNIPER_ST
:
8269 case DLT_JUNIPER_ISM
:
8270 case DLT_JUNIPER_VS
:
8271 case DLT_JUNIPER_SRX_E2E
:
8272 case DLT_JUNIPER_FIBRECHANNEL
:
8273 case DLT_JUNIPER_ATM_CEMIC
:
8275 /* juniper flags (including direction) are stored
8276 * the byte after the 3-byte magic number */
8278 /* match outgoing packets */
8279 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8281 /* match incoming packets */
8282 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8288 * If we have packet meta-data indicating a direction,
8289 * and that metadata can be checked by BPF code, check
8290 * it. Otherwise, give up, as this link-layer type has
8291 * nothing in the packet data.
8293 * Currently, the only platform where a BPF filter can
8294 * check that metadata is Linux with the in-kernel
8295 * BPF interpreter. If other packet capture mechanisms
8296 * and BPF filters also supported this, it would be
8297 * nice. It would be even better if they made that
8298 * metadata available so that we could provide it
8299 * with newer capture APIs, allowing it to be saved
8302 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
8304 * This is Linux with PF_PACKET support.
8305 * If this is a *live* capture, we can look at
8306 * special meta-data in the filter expression;
8307 * if it's a savefile, we can't.
8309 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8310 /* We have a FILE *, so this is a savefile */
8311 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8312 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8316 /* match outgoing packets */
8317 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8320 /* to filter on inbound traffic, invert the match */
8323 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8324 bpf_error(cstate
, "inbound/outbound not supported on %s",
8325 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8327 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8332 #ifdef HAVE_NET_PFVAR_H
8333 /* PF firewall log matched interface */
8335 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8341 * Catch errors reported by us and routines below us, and return NULL
8344 if (setjmp(cstate
->top_ctx
))
8347 if (cstate
->linktype
!= DLT_PFLOG
) {
8348 bpf_error(cstate
, "ifname supported only on PF linktype");
8351 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8352 off
= offsetof(struct pfloghdr
, ifname
);
8353 if (strlen(ifname
) >= len
) {
8354 bpf_error(cstate
, "ifname interface names can only be %d characters",
8358 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8359 (const u_char
*)ifname
);
8363 /* PF firewall log ruleset name */
8365 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8370 * Catch errors reported by us and routines below us, and return NULL
8373 if (setjmp(cstate
->top_ctx
))
8376 if (cstate
->linktype
!= DLT_PFLOG
) {
8377 bpf_error(cstate
, "ruleset supported only on PF linktype");
8381 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8382 bpf_error(cstate
, "ruleset names can only be %ld characters",
8383 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8387 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8388 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8392 /* PF firewall log rule number */
8394 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8399 * Catch errors reported by us and routines below us, and return NULL
8402 if (setjmp(cstate
->top_ctx
))
8405 if (cstate
->linktype
!= DLT_PFLOG
) {
8406 bpf_error(cstate
, "rnr supported only on PF linktype");
8410 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8415 /* PF firewall log sub-rule number */
8417 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8422 * Catch errors reported by us and routines below us, and return NULL
8425 if (setjmp(cstate
->top_ctx
))
8428 if (cstate
->linktype
!= DLT_PFLOG
) {
8429 bpf_error(cstate
, "srnr supported only on PF linktype");
8433 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8438 /* PF firewall log reason code */
8440 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8445 * Catch errors reported by us and routines below us, and return NULL
8448 if (setjmp(cstate
->top_ctx
))
8451 if (cstate
->linktype
!= DLT_PFLOG
) {
8452 bpf_error(cstate
, "reason supported only on PF linktype");
8456 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8457 (bpf_u_int32
)reason
);
8461 /* PF firewall log action */
8463 gen_pf_action(compiler_state_t
*cstate
, int action
)
8468 * Catch errors reported by us and routines below us, and return NULL
8471 if (setjmp(cstate
->top_ctx
))
8474 if (cstate
->linktype
!= DLT_PFLOG
) {
8475 bpf_error(cstate
, "action supported only on PF linktype");
8479 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8480 (bpf_u_int32
)action
);
8483 #else /* !HAVE_NET_PFVAR_H */
8485 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8488 * Catch errors reported by us and routines below us, and return NULL
8491 if (setjmp(cstate
->top_ctx
))
8494 bpf_error(cstate
, "libpcap was compiled without pf support");
8499 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8502 * Catch errors reported by us and routines below us, and return NULL
8505 if (setjmp(cstate
->top_ctx
))
8508 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8513 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8516 * Catch errors reported by us and routines below us, and return NULL
8519 if (setjmp(cstate
->top_ctx
))
8522 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8527 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8530 * Catch errors reported by us and routines below us, and return NULL
8533 if (setjmp(cstate
->top_ctx
))
8536 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8541 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8544 * Catch errors reported by us and routines below us, and return NULL
8547 if (setjmp(cstate
->top_ctx
))
8550 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8555 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8558 * Catch errors reported by us and routines below us, and return NULL
8561 if (setjmp(cstate
->top_ctx
))
8564 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8567 #endif /* HAVE_NET_PFVAR_H */
8569 /* IEEE 802.11 wireless header */
8571 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8576 * Catch errors reported by us and routines below us, and return NULL
8579 if (setjmp(cstate
->top_ctx
))
8582 switch (cstate
->linktype
) {
8584 case DLT_IEEE802_11
:
8585 case DLT_PRISM_HEADER
:
8586 case DLT_IEEE802_11_RADIO_AVS
:
8587 case DLT_IEEE802_11_RADIO
:
8588 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8592 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8600 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8605 * Catch errors reported by us and routines below us, and return NULL
8608 if (setjmp(cstate
->top_ctx
))
8611 switch (cstate
->linktype
) {
8613 case DLT_IEEE802_11
:
8614 case DLT_PRISM_HEADER
:
8615 case DLT_IEEE802_11_RADIO_AVS
:
8616 case DLT_IEEE802_11_RADIO
:
8620 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8624 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8625 IEEE80211_FC1_DIR_MASK
);
8631 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8636 * Catch errors reported by us and routines below us, and return NULL
8639 if (setjmp(cstate
->top_ctx
))
8642 switch (cstate
->linktype
) {
8645 case DLT_ARCNET_LINUX
:
8646 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8647 q
.proto
== Q_LINK
) {
8648 cstate
->e
= pcap_ether_aton(s
);
8649 if (cstate
->e
== NULL
)
8650 bpf_error(cstate
, "malloc");
8651 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8656 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8660 bpf_error(cstate
, "aid supported only on ARCnet");
8665 static struct block
*
8666 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8668 register struct block
*b0
, *b1
;
8671 /* src comes first, different from Ethernet */
8673 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8676 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8679 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8680 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8686 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8687 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8692 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8696 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8700 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8704 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8708 bpf_error(cstate
, "'ra' is only supported on 802.11");
8712 bpf_error(cstate
, "'ta' is only supported on 802.11");
8719 static struct block
*
8720 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8722 struct block
*b0
, *b1
;
8724 /* check for VLAN, including QinQ */
8725 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8726 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8729 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8735 static struct block
*
8736 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8738 if (vlan_num
> 0x0fff) {
8739 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8742 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8745 static struct block
*
8746 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8749 struct block
*b0
, *b1
;
8751 b0
= gen_vlan_tpid_test(cstate
);
8754 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8760 * Both payload and link header type follow the VLAN tags so that
8761 * both need to be updated.
8763 cstate
->off_linkpl
.constant_part
+= 4;
8764 cstate
->off_linktype
.constant_part
+= 4;
8769 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8770 /* add v to variable part of off */
8772 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8773 bpf_u_int32 v
, struct slist
*s
)
8777 if (!off
->is_variable
)
8778 off
->is_variable
= 1;
8780 off
->reg
= alloc_reg(cstate
);
8782 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8785 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8788 s2
= new_stmt(cstate
, BPF_ST
);
8794 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8795 * and link type offsets first
8798 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8802 /* offset determined at run time, shift variable part */
8804 cstate
->is_vlan_vloffset
= 1;
8805 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8806 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8808 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8809 sappend(s
.next
, b_tpid
->head
->stmts
);
8810 b_tpid
->head
->stmts
= s
.next
;
8814 * patch block b_vid (VLAN id test) to load VID value either from packet
8815 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8818 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8820 struct slist
*s
, *s2
, *sjeq
;
8823 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8824 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8826 /* true -> next instructions, false -> beginning of b_vid */
8827 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8829 sjeq
->s
.jf
= b_vid
->stmts
;
8832 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8833 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8837 /* Jump to the test in b_vid. We need to jump one instruction before
8838 * the end of the b_vid block so that we only skip loading the TCI
8839 * from packet data and not the 'and' instruction extractging VID.
8842 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8844 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8848 /* insert our statements at the beginning of b_vid */
8849 sappend(s
, b_vid
->stmts
);
8854 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8855 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8856 * tag can be either in metadata or in packet data; therefore if the
8857 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8858 * header for VLAN tag. As the decision is done at run time, we need
8859 * update variable part of the offsets
8861 static struct block
*
8862 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8865 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8868 /* generate new filter code based on extracting packet
8870 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8871 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8873 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8878 * This is tricky. We need to insert the statements updating variable
8879 * parts of offsets before the the traditional TPID and VID tests so
8880 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8881 * we do not want this update to affect those checks. That's why we
8882 * generate both test blocks first and insert the statements updating
8883 * variable parts of both offsets after that. This wouldn't work if
8884 * there already were variable length link header when entering this
8885 * function but gen_vlan_bpf_extensions() isn't called in that case.
8887 b_tpid
= gen_vlan_tpid_test(cstate
);
8889 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8891 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8896 gen_vlan_patch_vid_test(cstate
, b_vid
);
8906 * support IEEE 802.1Q VLAN trunk over ethernet
8909 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8914 * Catch errors reported by us and routines below us, and return NULL
8917 if (setjmp(cstate
->top_ctx
))
8920 /* can't check for VLAN-encapsulated packets inside MPLS */
8921 if (cstate
->label_stack_depth
> 0)
8922 bpf_error(cstate
, "no VLAN match after MPLS");
8925 * Check for a VLAN packet, and then change the offsets to point
8926 * to the type and data fields within the VLAN packet. Just
8927 * increment the offsets, so that we can support a hierarchy, e.g.
8928 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8931 * XXX - this is a bit of a kludge. If we were to split the
8932 * compiler into a parser that parses an expression and
8933 * generates an expression tree, and a code generator that
8934 * takes an expression tree (which could come from our
8935 * parser or from some other parser) and generates BPF code,
8936 * we could perhaps make the offsets parameters of routines
8937 * and, in the handler for an "AND" node, pass to subnodes
8938 * other than the VLAN node the adjusted offsets.
8940 * This would mean that "vlan" would, instead of changing the
8941 * behavior of *all* tests after it, change only the behavior
8942 * of tests ANDed with it. That would change the documented
8943 * semantics of "vlan", which might break some expressions.
8944 * However, it would mean that "(vlan and ip) or ip" would check
8945 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8946 * checking only for VLAN-encapsulated IP, so that could still
8947 * be considered worth doing; it wouldn't break expressions
8948 * that are of the form "vlan and ..." or "vlan N and ...",
8949 * which I suspect are the most common expressions involving
8950 * "vlan". "vlan or ..." doesn't necessarily do what the user
8951 * would really want, now, as all the "or ..." tests would
8952 * be done assuming a VLAN, even though the "or" could be viewed
8953 * as meaning "or, if this isn't a VLAN packet...".
8955 switch (cstate
->linktype
) {
8958 case DLT_NETANALYZER
:
8959 case DLT_NETANALYZER_TRANSPARENT
:
8960 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8961 /* Verify that this is the outer part of the packet and
8962 * not encapsulated somehow. */
8963 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8964 cstate
->off_linkhdr
.constant_part
==
8965 cstate
->off_outermostlinkhdr
.constant_part
) {
8967 * Do we need special VLAN handling?
8969 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8970 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8973 b0
= gen_vlan_no_bpf_extensions(cstate
,
8974 vlan_num
, has_vlan_tag
);
8977 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8981 case DLT_IEEE802_11
:
8982 case DLT_PRISM_HEADER
:
8983 case DLT_IEEE802_11_RADIO_AVS
:
8984 case DLT_IEEE802_11_RADIO
:
8985 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8989 bpf_error(cstate
, "no VLAN support for %s",
8990 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8994 cstate
->vlan_stack_depth
++;
9002 * The label_num_arg dance is to avoid annoying whining by compilers that
9003 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9004 * It's not *used* after setjmp returns.
9007 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9010 volatile bpf_u_int32 label_num
= label_num_arg
;
9011 struct block
*b0
, *b1
;
9014 * Catch errors reported by us and routines below us, and return NULL
9017 if (setjmp(cstate
->top_ctx
))
9020 if (cstate
->label_stack_depth
> 0) {
9021 /* just match the bottom-of-stack bit clear */
9022 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9025 * We're not in an MPLS stack yet, so check the link-layer
9026 * type against MPLS.
9028 switch (cstate
->linktype
) {
9030 case DLT_C_HDLC
: /* fall through */
9032 case DLT_NETANALYZER
:
9033 case DLT_NETANALYZER_TRANSPARENT
:
9034 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9038 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9041 /* FIXME add other DLT_s ...
9042 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9043 * leave it for now */
9046 bpf_error(cstate
, "no MPLS support for %s",
9047 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9052 /* If a specific MPLS label is requested, check it */
9053 if (has_label_num
) {
9054 if (label_num
> 0xFFFFF) {
9055 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9056 label_num
, 0xFFFFF);
9058 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9059 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9060 0xfffff000); /* only compare the first 20 bits */
9066 * Change the offsets to point to the type and data fields within
9067 * the MPLS packet. Just increment the offsets, so that we
9068 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9069 * capture packets with an outer label of 100000 and an inner
9072 * Increment the MPLS stack depth as well; this indicates that
9073 * we're checking MPLS-encapsulated headers, to make sure higher
9074 * level code generators don't try to match against IP-related
9075 * protocols such as Q_ARP, Q_RARP etc.
9077 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9079 cstate
->off_nl_nosnap
+= 4;
9080 cstate
->off_nl
+= 4;
9081 cstate
->label_stack_depth
++;
9086 * Support PPPOE discovery and session.
9089 gen_pppoed(compiler_state_t
*cstate
)
9092 * Catch errors reported by us and routines below us, and return NULL
9095 if (setjmp(cstate
->top_ctx
))
9098 /* check for PPPoE discovery */
9099 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9103 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9105 struct block
*b0
, *b1
;
9108 * Catch errors reported by us and routines below us, and return NULL
9111 if (setjmp(cstate
->top_ctx
))
9115 * Test against the PPPoE session link-layer type.
9117 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9119 /* If a specific session is requested, check PPPoE session id */
9121 if (sess_num
> 0x0000ffff) {
9122 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9123 sess_num
, 0x0000ffff);
9125 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9131 * Change the offsets to point to the type and data fields within
9132 * the PPP packet, and note that this is PPPoE rather than
9135 * XXX - this is a bit of a kludge. If we were to split the
9136 * compiler into a parser that parses an expression and
9137 * generates an expression tree, and a code generator that
9138 * takes an expression tree (which could come from our
9139 * parser or from some other parser) and generates BPF code,
9140 * we could perhaps make the offsets parameters of routines
9141 * and, in the handler for an "AND" node, pass to subnodes
9142 * other than the PPPoE node the adjusted offsets.
9144 * This would mean that "pppoes" would, instead of changing the
9145 * behavior of *all* tests after it, change only the behavior
9146 * of tests ANDed with it. That would change the documented
9147 * semantics of "pppoes", which might break some expressions.
9148 * However, it would mean that "(pppoes and ip) or ip" would check
9149 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9150 * checking only for VLAN-encapsulated IP, so that could still
9151 * be considered worth doing; it wouldn't break expressions
9152 * that are of the form "pppoes and ..." which I suspect are the
9153 * most common expressions involving "pppoes". "pppoes or ..."
9154 * doesn't necessarily do what the user would really want, now,
9155 * as all the "or ..." tests would be done assuming PPPoE, even
9156 * though the "or" could be viewed as meaning "or, if this isn't
9157 * a PPPoE packet...".
9159 * The "network-layer" protocol is PPPoE, which has a 6-byte
9160 * PPPoE header, followed by a PPP packet.
9162 * There is no HDLC encapsulation for the PPP packet (it's
9163 * encapsulated in PPPoES instead), so the link-layer type
9164 * starts at the first byte of the PPP packet. For PPPoE,
9165 * that offset is relative to the beginning of the total
9166 * link-layer payload, including any 802.2 LLC header, so
9167 * it's 6 bytes past cstate->off_nl.
9169 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9170 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9171 cstate
->off_linkpl
.reg
);
9173 cstate
->off_linktype
= cstate
->off_linkhdr
;
9174 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9177 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9182 /* Check that this is Geneve and the VNI is correct if
9183 * specified. Parameterized to handle both IPv4 and IPv6. */
9184 static struct block
*
9185 gen_geneve_check(compiler_state_t
*cstate
,
9186 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9187 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9189 struct block
*b0
, *b1
;
9191 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9193 /* Check that we are operating on version 0. Otherwise, we
9194 * can't decode the rest of the fields. The version is 2 bits
9195 * in the first byte of the Geneve header. */
9196 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9201 if (vni
> 0xffffff) {
9202 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9205 vni
<<= 8; /* VNI is in the upper 3 bytes */
9206 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9214 /* The IPv4 and IPv6 Geneve checks need to do two things:
9215 * - Verify that this actually is Geneve with the right VNI.
9216 * - Place the IP header length (plus variable link prefix if
9217 * needed) into register A to be used later to compute
9218 * the inner packet offsets. */
9219 static struct block
*
9220 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9222 struct block
*b0
, *b1
;
9223 struct slist
*s
, *s1
;
9225 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9227 /* Load the IP header length into A. */
9228 s
= gen_loadx_iphdrlen(cstate
);
9230 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9233 /* Forcibly append these statements to the true condition
9234 * of the protocol check by creating a new block that is
9235 * always true and ANDing them. */
9236 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9245 static struct block
*
9246 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9248 struct block
*b0
, *b1
;
9249 struct slist
*s
, *s1
;
9251 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9253 /* Load the IP header length. We need to account for a
9254 * variable length link prefix if there is one. */
9255 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9257 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9261 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9265 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9269 /* Forcibly append these statements to the true condition
9270 * of the protocol check by creating a new block that is
9271 * always true and ANDing them. */
9272 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9275 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9284 /* We need to store three values based on the Geneve header::
9285 * - The offset of the linktype.
9286 * - The offset of the end of the Geneve header.
9287 * - The offset of the end of the encapsulated MAC header. */
9288 static struct slist
*
9289 gen_geneve_offsets(compiler_state_t
*cstate
)
9291 struct slist
*s
, *s1
, *s_proto
;
9293 /* First we need to calculate the offset of the Geneve header
9294 * itself. This is composed of the IP header previously calculated
9295 * (include any variable link prefix) and stored in A plus the
9296 * fixed sized headers (fixed link prefix, MAC length, and UDP
9298 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9299 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9301 /* Stash this in X since we'll need it later. */
9302 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9305 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9307 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9311 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9312 cstate
->off_linktype
.is_variable
= 1;
9313 cstate
->off_linktype
.constant_part
= 0;
9315 s1
= new_stmt(cstate
, BPF_ST
);
9316 s1
->s
.k
= cstate
->off_linktype
.reg
;
9319 /* Load the Geneve option length and mask and shift to get the
9320 * number of bytes. It is stored in the first byte of the Geneve
9322 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9326 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9330 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9334 /* Add in the rest of the Geneve base header. */
9335 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9339 /* Add the Geneve header length to its offset and store. */
9340 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9344 /* Set the encapsulated type as Ethernet. Even though we may
9345 * not actually have Ethernet inside there are two reasons this
9347 * - The linktype field is always in EtherType format regardless
9348 * of whether it is in Geneve or an inner Ethernet frame.
9349 * - The only link layer that we have specific support for is
9350 * Ethernet. We will confirm that the packet actually is
9351 * Ethernet at runtime before executing these checks. */
9352 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9354 s1
= new_stmt(cstate
, BPF_ST
);
9355 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9358 /* Calculate whether we have an Ethernet header or just raw IP/
9359 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9360 * and linktype by 14 bytes so that the network header can be found
9361 * seamlessly. Otherwise, keep what we've calculated already. */
9363 /* We have a bare jmp so we can't use the optimizer. */
9364 cstate
->no_optimize
= 1;
9366 /* Load the EtherType in the Geneve header, 2 bytes in. */
9367 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9371 /* Load X with the end of the Geneve header. */
9372 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9373 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9376 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9377 * end of this check, we should have the total length in X. In
9378 * the non-Ethernet case, it's already there. */
9379 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9380 s_proto
->s
.k
= ETHERTYPE_TEB
;
9381 sappend(s
, s_proto
);
9383 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9387 /* Since this is Ethernet, use the EtherType of the payload
9388 * directly as the linktype. Overwrite what we already have. */
9389 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9393 s1
= new_stmt(cstate
, BPF_ST
);
9394 s1
->s
.k
= cstate
->off_linktype
.reg
;
9397 /* Advance two bytes further to get the end of the Ethernet
9399 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9403 /* Move the result to X. */
9404 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9407 /* Store the final result of our linkpl calculation. */
9408 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9409 cstate
->off_linkpl
.is_variable
= 1;
9410 cstate
->off_linkpl
.constant_part
= 0;
9412 s1
= new_stmt(cstate
, BPF_STX
);
9413 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9422 /* Check to see if this is a Geneve packet. */
9424 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9426 struct block
*b0
, *b1
;
9430 * Catch errors reported by us and routines below us, and return NULL
9433 if (setjmp(cstate
->top_ctx
))
9436 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9437 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9442 /* Later filters should act on the payload of the Geneve frame,
9443 * update all of the header pointers. Attach this code so that
9444 * it gets executed in the event that the Geneve filter matches. */
9445 s
= gen_geneve_offsets(cstate
);
9447 b1
= gen_true(cstate
);
9448 sappend(s
, b1
->stmts
);
9453 cstate
->is_geneve
= 1;
9458 /* Check that the encapsulated frame has a link layer header
9459 * for Ethernet filters. */
9460 static struct block
*
9461 gen_geneve_ll_check(compiler_state_t
*cstate
)
9464 struct slist
*s
, *s1
;
9466 /* The easiest way to see if there is a link layer present
9467 * is to check if the link layer header and payload are not
9470 /* Geneve always generates pure variable offsets so we can
9471 * compare only the registers. */
9472 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9473 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9475 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9476 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9479 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9487 static struct block
*
9488 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9489 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9496 if (!cstate
->is_atm
)
9497 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9498 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9500 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9501 0xffffffffU
, jtype
, reverse
, jvalue
);
9505 if (!cstate
->is_atm
)
9506 bpf_error(cstate
, "'vci' supported only on raw ATM");
9507 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9509 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9510 0xffffffffU
, jtype
, reverse
, jvalue
);
9514 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9515 abort(); /* XXX - this isn't on FreeBSD */
9516 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9517 0x0fU
, jtype
, reverse
, jvalue
);
9521 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9523 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9524 0xffffffffU
, jtype
, reverse
, jvalue
);
9528 if (!cstate
->is_atm
)
9529 bpf_error(cstate
, "'callref' supported only on raw ATM");
9530 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9532 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9533 0xffffffffU
, jtype
, reverse
, jvalue
);
9542 static struct block
*
9543 gen_atmtype_metac(compiler_state_t
*cstate
)
9545 struct block
*b0
, *b1
;
9547 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9548 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9553 static struct block
*
9554 gen_atmtype_sc(compiler_state_t
*cstate
)
9556 struct block
*b0
, *b1
;
9558 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9559 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9564 static struct block
*
9565 gen_atmtype_llc(compiler_state_t
*cstate
)
9569 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9570 cstate
->linktype
= cstate
->prevlinktype
;
9575 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9576 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9579 * Catch errors reported by us and routines below us, and return NULL
9582 if (setjmp(cstate
->top_ctx
))
9585 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9590 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9592 struct block
*b0
, *b1
;
9595 * Catch errors reported by us and routines below us, and return NULL
9598 if (setjmp(cstate
->top_ctx
))
9604 /* Get all packets in Meta signalling Circuit */
9605 if (!cstate
->is_atm
)
9606 bpf_error(cstate
, "'metac' supported only on raw ATM");
9607 b1
= gen_atmtype_metac(cstate
);
9611 /* Get all packets in Broadcast Circuit*/
9612 if (!cstate
->is_atm
)
9613 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9614 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9615 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9620 /* Get all cells in Segment OAM F4 circuit*/
9621 if (!cstate
->is_atm
)
9622 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9623 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9624 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9629 /* Get all cells in End-to-End OAM F4 Circuit*/
9630 if (!cstate
->is_atm
)
9631 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9632 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9633 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9638 /* Get all packets in connection Signalling Circuit */
9639 if (!cstate
->is_atm
)
9640 bpf_error(cstate
, "'sc' supported only on raw ATM");
9641 b1
= gen_atmtype_sc(cstate
);
9645 /* Get all packets in ILMI Circuit */
9646 if (!cstate
->is_atm
)
9647 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9648 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9649 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9654 /* Get all LANE packets */
9655 if (!cstate
->is_atm
)
9656 bpf_error(cstate
, "'lane' supported only on raw ATM");
9657 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9660 * Arrange that all subsequent tests assume LANE
9661 * rather than LLC-encapsulated packets, and set
9662 * the offsets appropriately for LANE-encapsulated
9665 * We assume LANE means Ethernet, not Token Ring.
9667 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9668 cstate
->off_payload
+ 2, /* Ethernet header */
9670 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9671 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9672 cstate
->off_nl
= 0; /* Ethernet II */
9673 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9677 /* Get all LLC-encapsulated packets */
9678 if (!cstate
->is_atm
)
9679 bpf_error(cstate
, "'llc' supported only on raw ATM");
9680 b1
= gen_atmtype_llc(cstate
);
9690 * Filtering for MTP2 messages based on li value
9691 * FISU, length is null
9692 * LSSU, length is 1 or 2
9693 * MSU, length is 3 or more
9694 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9697 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9699 struct block
*b0
, *b1
;
9702 * Catch errors reported by us and routines below us, and return NULL
9705 if (setjmp(cstate
->top_ctx
))
9711 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9712 (cstate
->linktype
!= DLT_ERF
) &&
9713 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9714 bpf_error(cstate
, "'fisu' supported only on MTP2");
9715 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9716 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9717 0x3fU
, BPF_JEQ
, 0, 0U);
9721 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9722 (cstate
->linktype
!= DLT_ERF
) &&
9723 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9724 bpf_error(cstate
, "'lssu' supported only on MTP2");
9725 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9726 0x3fU
, BPF_JGT
, 1, 2U);
9727 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9728 0x3fU
, BPF_JGT
, 0, 0U);
9733 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9734 (cstate
->linktype
!= DLT_ERF
) &&
9735 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9736 bpf_error(cstate
, "'msu' supported only on MTP2");
9737 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9738 0x3fU
, BPF_JGT
, 0, 2U);
9742 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9743 (cstate
->linktype
!= DLT_ERF
) &&
9744 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9745 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9746 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9747 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9748 0xff80U
, BPF_JEQ
, 0, 0U);
9752 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9753 (cstate
->linktype
!= DLT_ERF
) &&
9754 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9755 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9756 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9757 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9758 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9759 0xff80U
, BPF_JGT
, 0, 0U);
9764 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9765 (cstate
->linktype
!= DLT_ERF
) &&
9766 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9767 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9768 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9769 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9779 * The jvalue_arg dance is to avoid annoying whining by compilers that
9780 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9781 * It's not *used* after setjmp returns.
9784 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9785 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
9787 volatile bpf_u_int32 jvalue
= jvalue_arg
;
9789 bpf_u_int32 val1
, val2
, val3
;
9796 * Catch errors reported by us and routines below us, and return NULL
9799 if (setjmp(cstate
->top_ctx
))
9802 newoff_sio
= cstate
->off_sio
;
9803 newoff_opc
= cstate
->off_opc
;
9804 newoff_dpc
= cstate
->off_dpc
;
9805 newoff_sls
= cstate
->off_sls
;
9806 switch (mtp3field
) {
9809 newoff_sio
+= 3; /* offset for MTP2_HSL */
9813 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9814 bpf_error(cstate
, "'sio' supported only on SS7");
9815 /* sio coded on 1 byte so max value 255 */
9817 bpf_error(cstate
, "sio value %u too big; max value = 255",
9819 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
9820 jtype
, reverse
, jvalue
);
9828 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9829 bpf_error(cstate
, "'opc' supported only on SS7");
9830 /* opc coded on 14 bits so max value 16383 */
9832 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9834 /* the following instructions are made to convert jvalue
9835 * to the form used to write opc in an ss7 message*/
9836 val1
= jvalue
& 0x00003c00;
9838 val2
= jvalue
& 0x000003fc;
9840 val3
= jvalue
& 0x00000003;
9842 jvalue
= val1
+ val2
+ val3
;
9843 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
9844 jtype
, reverse
, jvalue
);
9852 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9853 bpf_error(cstate
, "'dpc' supported only on SS7");
9854 /* dpc coded on 14 bits so max value 16383 */
9856 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9858 /* the following instructions are made to convert jvalue
9859 * to the forme used to write dpc in an ss7 message*/
9860 val1
= jvalue
& 0x000000ff;
9862 val2
= jvalue
& 0x00003f00;
9864 jvalue
= val1
+ val2
;
9865 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
9866 jtype
, reverse
, jvalue
);
9874 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9875 bpf_error(cstate
, "'sls' supported only on SS7");
9876 /* sls coded on 4 bits so max value 15 */
9878 bpf_error(cstate
, "sls value %u too big; max value = 15",
9880 /* the following instruction is made to convert jvalue
9881 * to the forme used to write sls in an ss7 message*/
9882 jvalue
= jvalue
<< 4;
9883 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
9884 jtype
, reverse
, jvalue
);
9893 static struct block
*
9894 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9899 * Q.2931 signalling protocol messages for handling virtual circuits
9900 * establishment and teardown
9905 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9909 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9913 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9917 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9921 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9924 case A_RELEASE_DONE
:
9925 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9935 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9937 struct block
*b0
, *b1
;
9940 * Catch errors reported by us and routines below us, and return NULL
9943 if (setjmp(cstate
->top_ctx
))
9949 if (!cstate
->is_atm
)
9950 bpf_error(cstate
, "'oam' supported only on raw ATM");
9952 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9953 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9955 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9960 if (!cstate
->is_atm
)
9961 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9963 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9964 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9966 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9972 * Get Q.2931 signalling messages for switched
9973 * virtual connection
9975 if (!cstate
->is_atm
)
9976 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9977 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9978 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9980 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9982 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9984 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9986 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9988 b0
= gen_atmtype_sc(cstate
);
9993 if (!cstate
->is_atm
)
9994 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9995 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9996 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9998 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10000 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10002 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10004 b0
= gen_atmtype_metac(cstate
);