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"
65 #include "pcap/ipnet.h"
67 #include "diag-control.h"
72 #include <linux/types.h>
73 #include <linux/if_packet.h>
74 #include <linux/filter.h>
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
83 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
90 uint16_t u6_addr16
[8];
91 uint32_t u6_addr32
[4];
93 #define s6_addr in6_u.u6_addr8
94 #define s6_addr16 in6_u.u6_addr16
95 #define s6_addr32 in6_u.u6_addr32
96 #define s6_addr64 in6_u.u6_addr64
99 typedef unsigned short sa_family_t
;
101 #define __SOCKADDR_COMMON(sa_prefix) \
102 sa_family_t sa_prefix##family
104 /* Ditto, for IPv6. */
107 __SOCKADDR_COMMON (sin6_
);
108 uint16_t sin6_port
; /* Transport layer port # */
109 uint32_t sin6_flowinfo
; /* IPv6 flow information */
110 struct in6_addr sin6_addr
; /* IPv6 address */
113 #ifndef EAI_ADDRFAMILY
115 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
116 int ai_family
; /* PF_xxx */
117 int ai_socktype
; /* SOCK_xxx */
118 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
119 size_t ai_addrlen
; /* length of ai_addr */
120 char *ai_canonname
; /* canonical name for hostname */
121 struct sockaddr
*ai_addr
; /* binary address */
122 struct addrinfo
*ai_next
; /* next structure in linked list */
124 #endif /* EAI_ADDRFAMILY */
125 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
128 #include <netdb.h> /* for "struct addrinfo" */
130 #include <pcap/namedb.h>
132 #include "nametoaddr.h"
134 #define ETHERMTU 1500
136 #ifndef IPPROTO_HOPOPTS
137 #define IPPROTO_HOPOPTS 0
139 #ifndef IPPROTO_ROUTING
140 #define IPPROTO_ROUTING 43
142 #ifndef IPPROTO_FRAGMENT
143 #define IPPROTO_FRAGMENT 44
145 #ifndef IPPROTO_DSTOPTS
146 #define IPPROTO_DSTOPTS 60
149 #define IPPROTO_SCTP 132
152 #define GENEVE_PORT 6081
154 #ifdef HAVE_OS_PROTO_H
155 #include "os-proto.h"
158 #define JMP(c) ((c)|BPF_JMP|BPF_K)
161 * "Push" the current value of the link-layer header type and link-layer
162 * header offset onto a "stack", and set a new value. (It's not a
163 * full-blown stack; we keep only the top two items.)
165 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
167 (cs)->prevlinktype = (cs)->linktype; \
168 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
169 (cs)->linktype = (new_linktype); \
170 (cs)->off_linkhdr.is_variable = (new_is_variable); \
171 (cs)->off_linkhdr.constant_part = (new_constant_part); \
172 (cs)->off_linkhdr.reg = (new_reg); \
173 (cs)->is_geneve = 0; \
177 * Offset "not set" value.
179 #define OFFSET_NOT_SET 0xffffffffU
182 * Absolute offsets, which are offsets from the beginning of the raw
183 * packet data, are, in the general case, the sum of a variable value
184 * and a constant value; the variable value may be absent, in which
185 * case the offset is only the constant value, and the constant value
186 * may be zero, in which case the offset is only the variable value.
188 * bpf_abs_offset is a structure containing all that information:
190 * is_variable is 1 if there's a variable part.
192 * constant_part is the constant part of the value, possibly zero;
194 * if is_variable is 1, reg is the register number for a register
195 * containing the variable value if the register has been assigned,
205 * Value passed to gen_load_a() to indicate what the offset argument
206 * is relative to the beginning of.
209 OR_PACKET
, /* full packet data */
210 OR_LINKHDR
, /* link-layer header */
211 OR_PREVLINKHDR
, /* previous link-layer header */
212 OR_LLC
, /* 802.2 LLC header */
213 OR_PREVMPLSHDR
, /* previous MPLS header */
214 OR_LINKTYPE
, /* link-layer type */
215 OR_LINKPL
, /* link-layer payload */
216 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
217 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
218 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
222 * We divy out chunks of memory rather than call malloc each time so
223 * we don't have to worry about leaking memory. It's probably
224 * not a big deal if all this memory was wasted but if this ever
225 * goes into a library that would probably not be a good idea.
227 * XXX - this *is* in a library....
230 #define CHUNK0SIZE 1024
236 /* Code generator state */
238 struct _compiler_state
{
249 int outermostlinktype
;
254 /* Hack for handling VLAN and MPLS stacks. */
255 u_int label_stack_depth
;
256 u_int vlan_stack_depth
;
262 * As errors are handled by a longjmp, anything allocated must
263 * be freed in the longjmp handler, so it must be reachable
266 * One thing that's allocated is the result of pcap_nametoaddrinfo();
267 * it must be freed with freeaddrinfo(). This variable points to
268 * any addrinfo structure that would need to be freed.
273 * Another thing that's allocated is the result of pcap_ether_aton();
274 * it must be freed with free(). This variable points to any
275 * address that would need to be freed.
280 * Various code constructs need to know the layout of the packet.
281 * These values give the necessary offsets from the beginning
282 * of the packet data.
286 * Absolute offset of the beginning of the link-layer header.
288 bpf_abs_offset off_linkhdr
;
291 * If we're checking a link-layer header for a packet encapsulated
292 * in another protocol layer, this is the equivalent information
293 * for the previous layers' link-layer header from the beginning
294 * of the raw packet data.
296 bpf_abs_offset off_prevlinkhdr
;
299 * This is the equivalent information for the outermost layers'
302 bpf_abs_offset off_outermostlinkhdr
;
305 * Absolute offset of the beginning of the link-layer payload.
307 bpf_abs_offset off_linkpl
;
310 * "off_linktype" is the offset to information in the link-layer
311 * header giving the packet type. This is an absolute offset
312 * from the beginning of the packet.
314 * For Ethernet, it's the offset of the Ethernet type field; this
315 * means that it must have a value that skips VLAN tags.
317 * For link-layer types that always use 802.2 headers, it's the
318 * offset of the LLC header; this means that it must have a value
319 * that skips VLAN tags.
321 * For PPP, it's the offset of the PPP type field.
323 * For Cisco HDLC, it's the offset of the CHDLC type field.
325 * For BSD loopback, it's the offset of the AF_ value.
327 * For Linux cooked sockets, it's the offset of the type field.
329 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
330 * encapsulation, in which case, IP is assumed.
332 bpf_abs_offset off_linktype
;
335 * TRUE if the link layer includes an ATM pseudo-header.
340 * TRUE if "geneve" appeared in the filter; it causes us to
341 * generate code that checks for a Geneve header and assume
342 * that later filters apply to the encapsulated payload.
347 * TRUE if we need variable length part of VLAN offset
349 int is_vlan_vloffset
;
352 * These are offsets for the ATM pseudo-header.
359 * These are offsets for the MTP2 fields.
365 * These are offsets for the MTP3 fields.
373 * This is the offset of the first byte after the ATM pseudo_header,
374 * or -1 if there is no ATM pseudo-header.
379 * These are offsets to the beginning of the network-layer header.
380 * They are relative to the beginning of the link-layer payload
381 * (i.e., they don't include off_linkhdr.constant_part or
382 * off_linkpl.constant_part).
384 * If the link layer never uses 802.2 LLC:
386 * "off_nl" and "off_nl_nosnap" are the same.
388 * If the link layer always uses 802.2 LLC:
390 * "off_nl" is the offset if there's a SNAP header following
393 * "off_nl_nosnap" is the offset if there's no SNAP header.
395 * If the link layer is Ethernet:
397 * "off_nl" is the offset if the packet is an Ethernet II packet
398 * (we assume no 802.3+802.2+SNAP);
400 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
401 * with an 802.2 header following it.
407 * Here we handle simple allocation of the scratch registers.
408 * If too many registers are alloc'd, the allocator punts.
410 int regused
[BPF_MEMWORDS
];
416 struct chunk chunks
[NCHUNKS
];
421 * For use by routines outside this file.
425 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
430 * If we've already set an error, don't override it.
431 * The lexical analyzer reports some errors by setting
432 * the error and then returning a LEX_ERROR token, which
433 * is not recognized by any grammar rule, and thus forces
434 * the parse to stop. We don't want the error reported
435 * by the lexical analyzer to be overwritten by the syntax
438 if (!cstate
->error_set
) {
440 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
443 cstate
->error_set
= 1;
448 * For use *ONLY* in routines in this file.
450 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
451 PCAP_PRINTFLIKE(2, 3);
454 static void PCAP_NORETURN
455 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
460 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
463 longjmp(cstate
->top_ctx
, 1);
470 static int init_linktype(compiler_state_t
*, pcap_t
*);
472 static void init_regs(compiler_state_t
*);
473 static int alloc_reg(compiler_state_t
*);
474 static void free_reg(compiler_state_t
*, int);
476 static void initchunks(compiler_state_t
*cstate
);
477 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
478 static void *newchunk(compiler_state_t
*cstate
, size_t);
479 static void freechunks(compiler_state_t
*cstate
);
480 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
481 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
482 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
483 static inline void syntax(compiler_state_t
*cstate
);
485 static void backpatch(struct block
*, struct block
*);
486 static void merge(struct block
*, struct block
*);
487 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
489 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
491 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
493 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
495 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
497 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
498 u_int
, bpf_u_int32
, bpf_u_int32
);
499 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
500 u_int
, const u_char
*);
501 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
502 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
503 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
505 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
507 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
508 static struct block
*gen_uncond(compiler_state_t
*, int);
509 static inline struct block
*gen_true(compiler_state_t
*);
510 static inline struct block
*gen_false(compiler_state_t
*);
511 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
512 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
513 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
514 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
515 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
516 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
517 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
518 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
519 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
520 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
522 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
523 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
524 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
525 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
526 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
527 int, bpf_u_int32
, u_int
, u_int
);
529 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
530 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
532 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
533 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
534 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
535 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
536 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
537 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
538 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
539 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
540 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
543 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
544 struct in6_addr
*, int, int, int);
547 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
548 struct addrinfo
*, int, int);
550 static struct block
*gen_ipfrag(compiler_state_t
*);
551 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
552 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
554 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
555 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
557 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
558 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
559 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
561 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
562 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
563 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
564 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
566 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
567 static int lookup_proto(compiler_state_t
*, const char *, int);
568 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
569 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
570 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
571 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
572 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
573 static struct block
*gen_len(compiler_state_t
*, int, int);
574 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
575 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
577 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
578 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
579 bpf_u_int32
, int, int);
580 static struct block
*gen_atmtype_llc(compiler_state_t
*);
581 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
584 initchunks(compiler_state_t
*cstate
)
588 for (i
= 0; i
< NCHUNKS
; i
++) {
589 cstate
->chunks
[i
].n_left
= 0;
590 cstate
->chunks
[i
].m
= NULL
;
592 cstate
->cur_chunk
= 0;
596 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
603 /* XXX Round up to nearest long. */
604 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
606 /* XXX Round up to structure boundary. */
610 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
611 if (n
> cp
->n_left
) {
613 k
= ++cstate
->cur_chunk
;
615 bpf_set_error(cstate
, "out of memory");
618 size
= CHUNK0SIZE
<< k
;
619 cp
->m
= (void *)malloc(size
);
621 bpf_set_error(cstate
, "out of memory");
624 memset((char *)cp
->m
, 0, size
);
627 bpf_set_error(cstate
, "out of memory");
632 return (void *)((char *)cp
->m
+ cp
->n_left
);
636 newchunk(compiler_state_t
*cstate
, size_t n
)
640 p
= newchunk_nolongjmp(cstate
, n
);
642 longjmp(cstate
->top_ctx
, 1);
649 freechunks(compiler_state_t
*cstate
)
653 for (i
= 0; i
< NCHUNKS
; ++i
)
654 if (cstate
->chunks
[i
].m
!= NULL
)
655 free(cstate
->chunks
[i
].m
);
659 * A strdup whose allocations are freed after code generation is over.
660 * This is used by the lexical analyzer, so it can't longjmp; it just
661 * returns NULL on an allocation error, and the callers must check
665 sdup(compiler_state_t
*cstate
, const char *s
)
667 size_t n
= strlen(s
) + 1;
668 char *cp
= newchunk_nolongjmp(cstate
, n
);
672 pcap_strlcpy(cp
, s
, n
);
676 static inline struct block
*
677 new_block(compiler_state_t
*cstate
, int code
)
681 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
688 static inline struct slist
*
689 new_stmt(compiler_state_t
*cstate
, int code
)
693 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
699 static struct block
*
700 gen_retblk(compiler_state_t
*cstate
, int v
)
702 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
708 static inline PCAP_NORETURN_DEF
void
709 syntax(compiler_state_t
*cstate
)
711 bpf_error(cstate
, "syntax error in filter expression");
715 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
716 const char *buf
, int optimize
, bpf_u_int32 mask
)
721 compiler_state_t cstate
;
722 const char * volatile xbuf
= buf
;
723 yyscan_t scanner
= NULL
;
724 volatile YY_BUFFER_STATE in_buffer
= NULL
;
729 * If this pcap_t hasn't been activated, it doesn't have a
730 * link-layer type, so we can't use it.
733 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
734 "not-yet-activated pcap_t passed to pcap_compile");
746 * If the device on which we're capturing need to be notified
747 * that a new filter is being compiled, do so.
749 * This allows them to save a copy of it, in case, for example,
750 * they're implementing a form of remote packet capture, and
751 * want the remote machine to filter out the packets in which
752 * it's sending the packets it's captured.
754 * XXX - the fact that we happen to be compiling a filter
755 * doesn't necessarily mean we'll be installing it as the
756 * filter for this pcap_t; we might be running it from userland
757 * on captured packets to do packet classification. We really
758 * need a better way of handling this, but this is all that
759 * the WinPcap remote capture code did.
761 if (p
->save_current_filter_op
!= NULL
)
762 (p
->save_current_filter_op
)(p
, buf
);
766 cstate
.no_optimize
= 0;
771 cstate
.ic
.root
= NULL
;
772 cstate
.ic
.cur_mark
= 0;
774 cstate
.error_set
= 0;
777 cstate
.netmask
= mask
;
779 cstate
.snaplen
= pcap_snapshot(p
);
780 if (cstate
.snaplen
== 0) {
781 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
782 "snaplen of 0 rejects all packets");
787 if (pcap_lex_init(&scanner
) != 0)
788 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
789 errno
, "can't initialize scanner");
790 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
793 * Associate the compiler state with the lexical analyzer
796 pcap_set_extra(&cstate
, scanner
);
798 if (init_linktype(&cstate
, p
) == -1) {
802 if (pcap_parse(scanner
, &cstate
) != 0) {
804 if (cstate
.ai
!= NULL
)
805 freeaddrinfo(cstate
.ai
);
807 if (cstate
.e
!= NULL
)
813 if (cstate
.ic
.root
== NULL
) {
815 * Catch errors reported by gen_retblk().
817 if (setjmp(cstate
.top_ctx
)) {
821 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
824 if (optimize
&& !cstate
.no_optimize
) {
825 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
830 if (cstate
.ic
.root
== NULL
||
831 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
832 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
833 "expression rejects all packets");
838 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
839 cstate
.ic
.root
, &len
, p
->errbuf
);
840 if (program
->bf_insns
== NULL
) {
845 program
->bf_len
= len
;
847 rc
= 0; /* We're all okay */
851 * Clean up everything for the lexical analyzer.
853 if (in_buffer
!= NULL
)
854 pcap__delete_buffer(in_buffer
, scanner
);
856 pcap_lex_destroy(scanner
);
859 * Clean up our own allocated memory.
867 * entry point for using the compiler with no pcap open
868 * pass in all the stuff that is needed explicitly instead.
871 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
872 struct bpf_program
*program
,
873 const char *buf
, int optimize
, bpf_u_int32 mask
)
878 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
881 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
887 * Clean up a "struct bpf_program" by freeing all the memory allocated
891 pcap_freecode(struct bpf_program
*program
)
894 if (program
->bf_insns
!= NULL
) {
895 free((char *)program
->bf_insns
);
896 program
->bf_insns
= NULL
;
901 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
902 * which of the jt and jf fields has been resolved and which is a pointer
903 * back to another unresolved block (or nil). At least one of the fields
904 * in each block is already resolved.
907 backpatch(struct block
*list
, struct block
*target
)
924 * Merge the lists in b0 and b1, using the 'sense' field to indicate
925 * which of jt and jf is the link.
928 merge(struct block
*b0
, struct block
*b1
)
930 register struct block
**p
= &b0
;
932 /* Find end of list. */
934 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
936 /* Concatenate the lists. */
941 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
943 struct block
*ppi_dlt_check
;
946 * Catch errors reported by us and routines below us, and return -1
949 if (setjmp(cstate
->top_ctx
))
953 * Insert before the statements of the first (root) block any
954 * statements needed to load the lengths of any variable-length
955 * headers into registers.
957 * XXX - a fancier strategy would be to insert those before the
958 * statements of all blocks that use those lengths and that
959 * have no predecessors that use them, so that we only compute
960 * the lengths if we need them. There might be even better
961 * approaches than that.
963 * However, those strategies would be more complicated, and
964 * as we don't generate code to compute a length if the
965 * program has no tests that use the length, and as most
966 * tests will probably use those lengths, we would just
967 * postpone computing the lengths so that it's not done
968 * for tests that fail early, and it's not clear that's
971 insert_compute_vloffsets(cstate
, p
->head
);
974 * For DLT_PPI captures, generate a check of the per-packet
975 * DLT value to make sure it's DLT_IEEE802_11.
977 * XXX - TurboCap cards use DLT_PPI for Ethernet.
978 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
979 * with appropriate Ethernet information and use that rather
980 * than using something such as DLT_PPI where you don't know
981 * the link-layer header type until runtime, which, in the
982 * general case, would force us to generate both Ethernet *and*
983 * 802.11 code (*and* anything else for which PPI is used)
984 * and choose between them early in the BPF program?
986 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
987 if (ppi_dlt_check
!= NULL
)
988 gen_and(ppi_dlt_check
, p
);
990 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
991 p
->sense
= !p
->sense
;
992 backpatch(p
, gen_retblk(cstate
, 0));
993 cstate
->ic
.root
= p
->head
;
998 gen_and(struct block
*b0
, struct block
*b1
)
1000 backpatch(b0
, b1
->head
);
1001 b0
->sense
= !b0
->sense
;
1002 b1
->sense
= !b1
->sense
;
1004 b1
->sense
= !b1
->sense
;
1005 b1
->head
= b0
->head
;
1009 gen_or(struct block
*b0
, struct block
*b1
)
1011 b0
->sense
= !b0
->sense
;
1012 backpatch(b0
, b1
->head
);
1013 b0
->sense
= !b0
->sense
;
1015 b1
->head
= b0
->head
;
1019 gen_not(struct block
*b
)
1021 b
->sense
= !b
->sense
;
1024 static struct block
*
1025 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1026 u_int size
, bpf_u_int32 v
)
1028 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1031 static struct block
*
1032 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1033 u_int size
, bpf_u_int32 v
)
1035 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1038 static struct block
*
1039 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1040 u_int size
, bpf_u_int32 v
)
1042 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1045 static struct block
*
1046 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1047 u_int size
, bpf_u_int32 v
)
1049 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1052 static struct block
*
1053 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1054 u_int size
, bpf_u_int32 v
)
1056 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1059 static struct block
*
1060 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1061 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1063 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1066 static struct block
*
1067 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1068 u_int size
, const u_char
*v
)
1070 register struct block
*b
, *tmp
;
1074 register const u_char
*p
= &v
[size
- 4];
1076 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1084 register const u_char
*p
= &v
[size
- 2];
1086 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1094 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1103 * AND the field of size "size" at offset "offset" relative to the header
1104 * specified by "offrel" with "mask", and compare it with the value "v"
1105 * with the test specified by "jtype"; if "reverse" is true, the test
1106 * should test the opposite of "jtype".
1108 static struct block
*
1109 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1110 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1113 struct slist
*s
, *s2
;
1116 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1118 if (mask
!= 0xffffffff) {
1119 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1124 b
= new_block(cstate
, JMP(jtype
));
1127 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1133 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1135 cstate
->pcap_fddipad
= p
->fddipad
;
1138 * We start out with only one link-layer header.
1140 cstate
->outermostlinktype
= pcap_datalink(p
);
1141 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1142 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1143 cstate
->off_outermostlinkhdr
.reg
= -1;
1145 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1146 cstate
->off_prevlinkhdr
.constant_part
= 0;
1147 cstate
->off_prevlinkhdr
.is_variable
= 0;
1148 cstate
->off_prevlinkhdr
.reg
= -1;
1150 cstate
->linktype
= cstate
->outermostlinktype
;
1151 cstate
->off_linkhdr
.constant_part
= 0;
1152 cstate
->off_linkhdr
.is_variable
= 0;
1153 cstate
->off_linkhdr
.reg
= -1;
1158 cstate
->off_linkpl
.constant_part
= 0;
1159 cstate
->off_linkpl
.is_variable
= 0;
1160 cstate
->off_linkpl
.reg
= -1;
1162 cstate
->off_linktype
.constant_part
= 0;
1163 cstate
->off_linktype
.is_variable
= 0;
1164 cstate
->off_linktype
.reg
= -1;
1167 * Assume it's not raw ATM with a pseudo-header, for now.
1170 cstate
->off_vpi
= OFFSET_NOT_SET
;
1171 cstate
->off_vci
= OFFSET_NOT_SET
;
1172 cstate
->off_proto
= OFFSET_NOT_SET
;
1173 cstate
->off_payload
= OFFSET_NOT_SET
;
1178 cstate
->is_geneve
= 0;
1181 * No variable length VLAN offset by default
1183 cstate
->is_vlan_vloffset
= 0;
1186 * And assume we're not doing SS7.
1188 cstate
->off_li
= OFFSET_NOT_SET
;
1189 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1190 cstate
->off_sio
= OFFSET_NOT_SET
;
1191 cstate
->off_opc
= OFFSET_NOT_SET
;
1192 cstate
->off_dpc
= OFFSET_NOT_SET
;
1193 cstate
->off_sls
= OFFSET_NOT_SET
;
1195 cstate
->label_stack_depth
= 0;
1196 cstate
->vlan_stack_depth
= 0;
1198 switch (cstate
->linktype
) {
1201 cstate
->off_linktype
.constant_part
= 2;
1202 cstate
->off_linkpl
.constant_part
= 6;
1203 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1204 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1207 case DLT_ARCNET_LINUX
:
1208 cstate
->off_linktype
.constant_part
= 4;
1209 cstate
->off_linkpl
.constant_part
= 8;
1210 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1211 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1215 cstate
->off_linktype
.constant_part
= 12;
1216 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1217 cstate
->off_nl
= 0; /* Ethernet II */
1218 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1223 * SLIP doesn't have a link level type. The 16 byte
1224 * header is hacked into our SLIP driver.
1226 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1227 cstate
->off_linkpl
.constant_part
= 16;
1229 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1232 case DLT_SLIP_BSDOS
:
1233 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1234 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1236 cstate
->off_linkpl
.constant_part
= 24;
1238 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1243 cstate
->off_linktype
.constant_part
= 0;
1244 cstate
->off_linkpl
.constant_part
= 4;
1246 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1250 cstate
->off_linktype
.constant_part
= 0;
1251 cstate
->off_linkpl
.constant_part
= 12;
1253 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1258 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1259 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1260 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1261 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1262 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1264 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1269 * This does no include the Ethernet header, and
1270 * only covers session state.
1272 cstate
->off_linktype
.constant_part
= 6;
1273 cstate
->off_linkpl
.constant_part
= 8;
1275 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1279 cstate
->off_linktype
.constant_part
= 5;
1280 cstate
->off_linkpl
.constant_part
= 24;
1282 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1287 * FDDI doesn't really have a link-level type field.
1288 * We set "off_linktype" to the offset of the LLC header.
1290 * To check for Ethernet types, we assume that SSAP = SNAP
1291 * is being used and pick out the encapsulated Ethernet type.
1292 * XXX - should we generate code to check for SNAP?
1294 cstate
->off_linktype
.constant_part
= 13;
1295 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1296 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1297 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1298 cstate
->off_nl
= 8; /* 802.2+SNAP */
1299 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1304 * Token Ring doesn't really have a link-level type field.
1305 * We set "off_linktype" to the offset of the LLC header.
1307 * To check for Ethernet types, we assume that SSAP = SNAP
1308 * is being used and pick out the encapsulated Ethernet type.
1309 * XXX - should we generate code to check for SNAP?
1311 * XXX - the header is actually variable-length.
1312 * Some various Linux patched versions gave 38
1313 * as "off_linktype" and 40 as "off_nl"; however,
1314 * if a token ring packet has *no* routing
1315 * information, i.e. is not source-routed, the correct
1316 * values are 20 and 22, as they are in the vanilla code.
1318 * A packet is source-routed iff the uppermost bit
1319 * of the first byte of the source address, at an
1320 * offset of 8, has the uppermost bit set. If the
1321 * packet is source-routed, the total number of bytes
1322 * of routing information is 2 plus bits 0x1F00 of
1323 * the 16-bit value at an offset of 14 (shifted right
1324 * 8 - figure out which byte that is).
1326 cstate
->off_linktype
.constant_part
= 14;
1327 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1328 cstate
->off_nl
= 8; /* 802.2+SNAP */
1329 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1332 case DLT_PRISM_HEADER
:
1333 case DLT_IEEE802_11_RADIO_AVS
:
1334 case DLT_IEEE802_11_RADIO
:
1335 cstate
->off_linkhdr
.is_variable
= 1;
1336 /* Fall through, 802.11 doesn't have a variable link
1337 * prefix but is otherwise the same. */
1340 case DLT_IEEE802_11
:
1342 * 802.11 doesn't really have a link-level type field.
1343 * We set "off_linktype.constant_part" to the offset of
1346 * To check for Ethernet types, we assume that SSAP = SNAP
1347 * is being used and pick out the encapsulated Ethernet type.
1348 * XXX - should we generate code to check for SNAP?
1350 * We also handle variable-length radio headers here.
1351 * The Prism header is in theory variable-length, but in
1352 * practice it's always 144 bytes long. However, some
1353 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1354 * sometimes or always supply an AVS header, so we
1355 * have to check whether the radio header is a Prism
1356 * header or an AVS header, so, in practice, it's
1359 cstate
->off_linktype
.constant_part
= 24;
1360 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1361 cstate
->off_linkpl
.is_variable
= 1;
1362 cstate
->off_nl
= 8; /* 802.2+SNAP */
1363 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1368 * At the moment we treat PPI the same way that we treat
1369 * normal Radiotap encoded packets. The difference is in
1370 * the function that generates the code at the beginning
1371 * to compute the header length. Since this code generator
1372 * of PPI supports bare 802.11 encapsulation only (i.e.
1373 * the encapsulated DLT should be DLT_IEEE802_11) we
1374 * generate code to check for this too.
1376 cstate
->off_linktype
.constant_part
= 24;
1377 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1378 cstate
->off_linkpl
.is_variable
= 1;
1379 cstate
->off_linkhdr
.is_variable
= 1;
1380 cstate
->off_nl
= 8; /* 802.2+SNAP */
1381 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1384 case DLT_ATM_RFC1483
:
1385 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1387 * assume routed, non-ISO PDUs
1388 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1390 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1391 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1392 * latter would presumably be treated the way PPPoE
1393 * should be, so you can do "pppoe and udp port 2049"
1394 * or "pppoa and tcp port 80" and have it check for
1395 * PPPo{A,E} and a PPP protocol of IP and....
1397 cstate
->off_linktype
.constant_part
= 0;
1398 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1399 cstate
->off_nl
= 8; /* 802.2+SNAP */
1400 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1405 * Full Frontal ATM; you get AALn PDUs with an ATM
1409 cstate
->off_vpi
= SUNATM_VPI_POS
;
1410 cstate
->off_vci
= SUNATM_VCI_POS
;
1411 cstate
->off_proto
= PROTO_POS
;
1412 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1413 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1414 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1415 cstate
->off_nl
= 8; /* 802.2+SNAP */
1416 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1422 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1423 cstate
->off_linkpl
.constant_part
= 0;
1425 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1428 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1429 cstate
->off_linktype
.constant_part
= 14;
1430 cstate
->off_linkpl
.constant_part
= 16;
1432 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1435 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1436 cstate
->off_linktype
.constant_part
= 0;
1437 cstate
->off_linkpl
.constant_part
= 20;
1439 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1444 * LocalTalk does have a 1-byte type field in the LLAP header,
1445 * but really it just indicates whether there is a "short" or
1446 * "long" DDP packet following.
1448 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1449 cstate
->off_linkpl
.constant_part
= 0;
1451 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1454 case DLT_IP_OVER_FC
:
1456 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1457 * link-level type field. We set "off_linktype" to the
1458 * offset of the LLC header.
1460 * To check for Ethernet types, we assume that SSAP = SNAP
1461 * is being used and pick out the encapsulated Ethernet type.
1462 * XXX - should we generate code to check for SNAP? RFC
1463 * 2625 says SNAP should be used.
1465 cstate
->off_linktype
.constant_part
= 16;
1466 cstate
->off_linkpl
.constant_part
= 16;
1467 cstate
->off_nl
= 8; /* 802.2+SNAP */
1468 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1473 * XXX - we should set this to handle SNAP-encapsulated
1474 * frames (NLPID of 0x80).
1476 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1477 cstate
->off_linkpl
.constant_part
= 0;
1479 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1483 * the only BPF-interesting FRF.16 frames are non-control frames;
1484 * Frame Relay has a variable length link-layer
1485 * so lets start with offset 4 for now and increments later on (FIXME);
1488 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1489 cstate
->off_linkpl
.constant_part
= 0;
1491 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1494 case DLT_APPLE_IP_OVER_IEEE1394
:
1495 cstate
->off_linktype
.constant_part
= 16;
1496 cstate
->off_linkpl
.constant_part
= 18;
1498 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1501 case DLT_SYMANTEC_FIREWALL
:
1502 cstate
->off_linktype
.constant_part
= 6;
1503 cstate
->off_linkpl
.constant_part
= 44;
1504 cstate
->off_nl
= 0; /* Ethernet II */
1505 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1509 cstate
->off_linktype
.constant_part
= 0;
1510 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1511 cstate
->off_linkpl
.is_variable
= 1;
1513 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1516 case DLT_JUNIPER_MFR
:
1517 case DLT_JUNIPER_MLFR
:
1518 case DLT_JUNIPER_MLPPP
:
1519 case DLT_JUNIPER_PPP
:
1520 case DLT_JUNIPER_CHDLC
:
1521 case DLT_JUNIPER_FRELAY
:
1522 cstate
->off_linktype
.constant_part
= 4;
1523 cstate
->off_linkpl
.constant_part
= 4;
1525 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1528 case DLT_JUNIPER_ATM1
:
1529 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1530 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1532 cstate
->off_nl_nosnap
= 10;
1535 case DLT_JUNIPER_ATM2
:
1536 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1537 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1539 cstate
->off_nl_nosnap
= 10;
1542 /* frames captured on a Juniper PPPoE service PIC
1543 * contain raw ethernet frames */
1544 case DLT_JUNIPER_PPPOE
:
1545 case DLT_JUNIPER_ETHER
:
1546 cstate
->off_linkpl
.constant_part
= 14;
1547 cstate
->off_linktype
.constant_part
= 16;
1548 cstate
->off_nl
= 18; /* Ethernet II */
1549 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1552 case DLT_JUNIPER_PPPOE_ATM
:
1553 cstate
->off_linktype
.constant_part
= 4;
1554 cstate
->off_linkpl
.constant_part
= 6;
1556 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1559 case DLT_JUNIPER_GGSN
:
1560 cstate
->off_linktype
.constant_part
= 6;
1561 cstate
->off_linkpl
.constant_part
= 12;
1563 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1566 case DLT_JUNIPER_ES
:
1567 cstate
->off_linktype
.constant_part
= 6;
1568 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1569 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1570 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1573 case DLT_JUNIPER_MONITOR
:
1574 cstate
->off_linktype
.constant_part
= 12;
1575 cstate
->off_linkpl
.constant_part
= 12;
1576 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1577 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1580 case DLT_BACNET_MS_TP
:
1581 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1582 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1583 cstate
->off_nl
= OFFSET_NOT_SET
;
1584 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1587 case DLT_JUNIPER_SERVICES
:
1588 cstate
->off_linktype
.constant_part
= 12;
1589 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1590 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1591 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1594 case DLT_JUNIPER_VP
:
1595 cstate
->off_linktype
.constant_part
= 18;
1596 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1597 cstate
->off_nl
= OFFSET_NOT_SET
;
1598 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1601 case DLT_JUNIPER_ST
:
1602 cstate
->off_linktype
.constant_part
= 18;
1603 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1604 cstate
->off_nl
= OFFSET_NOT_SET
;
1605 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1608 case DLT_JUNIPER_ISM
:
1609 cstate
->off_linktype
.constant_part
= 8;
1610 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1611 cstate
->off_nl
= OFFSET_NOT_SET
;
1612 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1615 case DLT_JUNIPER_VS
:
1616 case DLT_JUNIPER_SRX_E2E
:
1617 case DLT_JUNIPER_FIBRECHANNEL
:
1618 case DLT_JUNIPER_ATM_CEMIC
:
1619 cstate
->off_linktype
.constant_part
= 8;
1620 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1621 cstate
->off_nl
= OFFSET_NOT_SET
;
1622 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1627 cstate
->off_li_hsl
= 4;
1628 cstate
->off_sio
= 3;
1629 cstate
->off_opc
= 4;
1630 cstate
->off_dpc
= 4;
1631 cstate
->off_sls
= 7;
1632 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1633 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1634 cstate
->off_nl
= OFFSET_NOT_SET
;
1635 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1638 case DLT_MTP2_WITH_PHDR
:
1640 cstate
->off_li_hsl
= 8;
1641 cstate
->off_sio
= 7;
1642 cstate
->off_opc
= 8;
1643 cstate
->off_dpc
= 8;
1644 cstate
->off_sls
= 11;
1645 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1646 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1647 cstate
->off_nl
= OFFSET_NOT_SET
;
1648 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1652 cstate
->off_li
= 22;
1653 cstate
->off_li_hsl
= 24;
1654 cstate
->off_sio
= 23;
1655 cstate
->off_opc
= 24;
1656 cstate
->off_dpc
= 24;
1657 cstate
->off_sls
= 27;
1658 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1659 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1660 cstate
->off_nl
= OFFSET_NOT_SET
;
1661 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1665 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1666 cstate
->off_linkpl
.constant_part
= 4;
1668 cstate
->off_nl_nosnap
= 0;
1673 * Currently, only raw "link[N:M]" filtering is supported.
1675 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1676 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1677 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1678 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1682 cstate
->off_linktype
.constant_part
= 1;
1683 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1685 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1688 case DLT_NETANALYZER
:
1689 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1690 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1691 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1692 cstate
->off_nl
= 0; /* Ethernet II */
1693 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1696 case DLT_NETANALYZER_TRANSPARENT
:
1697 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1698 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1699 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1700 cstate
->off_nl
= 0; /* Ethernet II */
1701 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1706 * For values in the range in which we've assigned new
1707 * DLT_ values, only raw "link[N:M]" filtering is supported.
1709 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1710 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1711 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1712 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1713 cstate
->off_nl
= OFFSET_NOT_SET
;
1714 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1716 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1717 cstate
->linktype
, DLT_MATCHING_MIN
, DLT_MATCHING_MAX
);
1723 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1728 * Load a value relative to the specified absolute offset.
1730 static struct slist
*
1731 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1732 u_int offset
, u_int size
)
1734 struct slist
*s
, *s2
;
1736 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1739 * If "s" is non-null, it has code to arrange that the X register
1740 * contains the variable part of the absolute offset, so we
1741 * generate a load relative to that, with an offset of
1742 * abs_offset->constant_part + offset.
1744 * Otherwise, we can do an absolute load with an offset of
1745 * abs_offset->constant_part + offset.
1749 * "s" points to a list of statements that puts the
1750 * variable part of the absolute offset into the X register.
1751 * Do an indirect load, to use the X register as an offset.
1753 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1754 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1758 * There is no variable part of the absolute offset, so
1759 * just do an absolute load.
1761 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1762 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1768 * Load a value relative to the beginning of the specified header.
1770 static struct slist
*
1771 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1774 struct slist
*s
, *s2
;
1777 * Squelch warnings from compilers that *don't* assume that
1778 * offrel always has a valid enum value and therefore don't
1779 * assume that we'll always go through one of the case arms.
1781 * If we have a default case, compilers that *do* assume that
1782 * will then complain about the default case code being
1785 * Damned if you do, damned if you don't.
1792 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1797 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1800 case OR_PREVLINKHDR
:
1801 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1805 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1808 case OR_PREVMPLSHDR
:
1809 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1813 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1816 case OR_LINKPL_NOSNAP
:
1817 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1821 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1826 * Load the X register with the length of the IPv4 header
1827 * (plus the offset of the link-layer header, if it's
1828 * preceded by a variable-length header such as a radio
1829 * header), in bytes.
1831 s
= gen_loadx_iphdrlen(cstate
);
1834 * Load the item at {offset of the link-layer payload} +
1835 * {offset, relative to the start of the link-layer
1836 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1837 * {specified offset}.
1839 * If the offset of the link-layer payload is variable,
1840 * the variable part of that offset is included in the
1841 * value in the X register, and we include the constant
1842 * part in the offset of the load.
1844 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1845 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1850 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1857 * Generate code to load into the X register the sum of the length of
1858 * the IPv4 header and the variable part of the offset of the link-layer
1861 static struct slist
*
1862 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1864 struct slist
*s
, *s2
;
1866 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1869 * The offset of the link-layer payload has a variable
1870 * part. "s" points to a list of statements that put
1871 * the variable part of that offset into the X register.
1873 * The 4*([k]&0xf) addressing mode can't be used, as we
1874 * don't have a constant offset, so we have to load the
1875 * value in question into the A register and add to it
1876 * the value from the X register.
1878 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1879 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1881 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1884 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1889 * The A register now contains the length of the IP header.
1890 * We need to add to it the variable part of the offset of
1891 * the link-layer payload, which is still in the X
1892 * register, and move the result into the X register.
1894 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1895 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1898 * The offset of the link-layer payload is a constant,
1899 * so no code was generated to load the (non-existent)
1900 * variable part of that offset.
1902 * This means we can use the 4*([k]&0xf) addressing
1903 * mode. Load the length of the IPv4 header, which
1904 * is at an offset of cstate->off_nl from the beginning of
1905 * the link-layer payload, and thus at an offset of
1906 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1907 * of the raw packet data, using that addressing mode.
1909 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1910 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1916 static struct block
*
1917 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1922 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1924 b
= new_block(cstate
, JMP(BPF_JEQ
));
1930 static inline struct block
*
1931 gen_true(compiler_state_t
*cstate
)
1933 return gen_uncond(cstate
, 1);
1936 static inline struct block
*
1937 gen_false(compiler_state_t
*cstate
)
1939 return gen_uncond(cstate
, 0);
1943 * Byte-swap a 32-bit number.
1944 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1945 * big-endian platforms.)
1947 #define SWAPLONG(y) \
1948 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1951 * Generate code to match a particular packet type.
1953 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1954 * value, if <= ETHERMTU. We use that to determine whether to
1955 * match the type/length field or to check the type/length field for
1956 * a value <= ETHERMTU to see whether it's a type field and then do
1957 * the appropriate test.
1959 static struct block
*
1960 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1962 struct block
*b0
, *b1
;
1968 case LLCSAP_NETBEUI
:
1970 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1971 * so we check the DSAP and SSAP.
1973 * LLCSAP_IP checks for IP-over-802.2, rather
1974 * than IP-over-Ethernet or IP-over-SNAP.
1976 * XXX - should we check both the DSAP and the
1977 * SSAP, like this, or should we check just the
1978 * DSAP, as we do for other types <= ETHERMTU
1979 * (i.e., other SAP values)?
1981 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1983 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
1991 * Ethernet_II frames, which are Ethernet
1992 * frames with a frame type of ETHERTYPE_IPX;
1994 * Ethernet_802.3 frames, which are 802.3
1995 * frames (i.e., the type/length field is
1996 * a length field, <= ETHERMTU, rather than
1997 * a type field) with the first two bytes
1998 * after the Ethernet/802.3 header being
2001 * Ethernet_802.2 frames, which are 802.3
2002 * frames with an 802.2 LLC header and
2003 * with the IPX LSAP as the DSAP in the LLC
2006 * Ethernet_SNAP frames, which are 802.3
2007 * frames with an LLC header and a SNAP
2008 * header and with an OUI of 0x000000
2009 * (encapsulated Ethernet) and a protocol
2010 * ID of ETHERTYPE_IPX in the SNAP header.
2012 * XXX - should we generate the same code both
2013 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2017 * This generates code to check both for the
2018 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2020 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2021 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2025 * Now we add code to check for SNAP frames with
2026 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2028 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2032 * Now we generate code to check for 802.3
2033 * frames in general.
2035 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2039 * Now add the check for 802.3 frames before the
2040 * check for Ethernet_802.2 and Ethernet_802.3,
2041 * as those checks should only be done on 802.3
2042 * frames, not on Ethernet frames.
2047 * Now add the check for Ethernet_II frames, and
2048 * do that before checking for the other frame
2051 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2055 case ETHERTYPE_ATALK
:
2056 case ETHERTYPE_AARP
:
2058 * EtherTalk (AppleTalk protocols on Ethernet link
2059 * layer) may use 802.2 encapsulation.
2063 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2064 * we check for an Ethernet type field less than
2065 * 1500, which means it's an 802.3 length field.
2067 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2071 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2072 * SNAP packets with an organization code of
2073 * 0x080007 (Apple, for Appletalk) and a protocol
2074 * type of ETHERTYPE_ATALK (Appletalk).
2076 * 802.2-encapsulated ETHERTYPE_AARP packets are
2077 * SNAP packets with an organization code of
2078 * 0x000000 (encapsulated Ethernet) and a protocol
2079 * type of ETHERTYPE_AARP (Appletalk ARP).
2081 if (ll_proto
== ETHERTYPE_ATALK
)
2082 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2083 else /* ll_proto == ETHERTYPE_AARP */
2084 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2088 * Check for Ethernet encapsulation (Ethertalk
2089 * phase 1?); we just check for the Ethernet
2092 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2098 if (ll_proto
<= ETHERMTU
) {
2100 * This is an LLC SAP value, so the frames
2101 * that match would be 802.2 frames.
2102 * Check that the frame is an 802.2 frame
2103 * (i.e., that the length/type field is
2104 * a length field, <= ETHERMTU) and
2105 * then check the DSAP.
2107 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2109 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2114 * This is an Ethernet type, so compare
2115 * the length/type field with it (if
2116 * the frame is an 802.2 frame, the length
2117 * field will be <= ETHERMTU, and, as
2118 * "ll_proto" is > ETHERMTU, this test
2119 * will fail and the frame won't match,
2120 * which is what we want).
2122 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2127 static struct block
*
2128 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2131 * For DLT_NULL, the link-layer header is a 32-bit word
2132 * containing an AF_ value in *host* byte order, and for
2133 * DLT_ENC, the link-layer header begins with a 32-bit
2134 * word containing an AF_ value in host byte order.
2136 * In addition, if we're reading a saved capture file,
2137 * the host byte order in the capture may not be the
2138 * same as the host byte order on this machine.
2140 * For DLT_LOOP, the link-layer header is a 32-bit
2141 * word containing an AF_ value in *network* byte order.
2143 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2145 * The AF_ value is in host byte order, but the BPF
2146 * interpreter will convert it to network byte order.
2148 * If this is a save file, and it's from a machine
2149 * with the opposite byte order to ours, we byte-swap
2152 * Then we run it through "htonl()", and generate
2153 * code to compare against the result.
2155 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2156 ll_proto
= SWAPLONG(ll_proto
);
2157 ll_proto
= htonl(ll_proto
);
2159 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2163 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2164 * or IPv6 then we have an error.
2166 static struct block
*
2167 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2172 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2175 case ETHERTYPE_IPV6
:
2176 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2183 return gen_false(cstate
);
2187 * Generate code to match a particular packet type.
2189 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2190 * value, if <= ETHERMTU. We use that to determine whether to
2191 * match the type field or to check the type field for the special
2192 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2194 static struct block
*
2195 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2197 struct block
*b0
, *b1
;
2203 case LLCSAP_NETBEUI
:
2205 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2206 * so we check the DSAP and SSAP.
2208 * LLCSAP_IP checks for IP-over-802.2, rather
2209 * than IP-over-Ethernet or IP-over-SNAP.
2211 * XXX - should we check both the DSAP and the
2212 * SSAP, like this, or should we check just the
2213 * DSAP, as we do for other types <= ETHERMTU
2214 * (i.e., other SAP values)?
2216 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2217 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2223 * Ethernet_II frames, which are Ethernet
2224 * frames with a frame type of ETHERTYPE_IPX;
2226 * Ethernet_802.3 frames, which have a frame
2227 * type of LINUX_SLL_P_802_3;
2229 * Ethernet_802.2 frames, which are 802.3
2230 * frames with an 802.2 LLC header (i.e, have
2231 * a frame type of LINUX_SLL_P_802_2) and
2232 * with the IPX LSAP as the DSAP in the LLC
2235 * Ethernet_SNAP frames, which are 802.3
2236 * frames with an LLC header and a SNAP
2237 * header and with an OUI of 0x000000
2238 * (encapsulated Ethernet) and a protocol
2239 * ID of ETHERTYPE_IPX in the SNAP header.
2241 * First, do the checks on LINUX_SLL_P_802_2
2242 * frames; generate the check for either
2243 * Ethernet_802.2 or Ethernet_SNAP frames, and
2244 * then put a check for LINUX_SLL_P_802_2 frames
2247 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2248 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2250 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2254 * Now check for 802.3 frames and OR that with
2255 * the previous test.
2257 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2261 * Now add the check for Ethernet_II frames, and
2262 * do that before checking for the other frame
2265 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2269 case ETHERTYPE_ATALK
:
2270 case ETHERTYPE_AARP
:
2272 * EtherTalk (AppleTalk protocols on Ethernet link
2273 * layer) may use 802.2 encapsulation.
2277 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2278 * we check for the 802.2 protocol type in the
2279 * "Ethernet type" field.
2281 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2284 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2285 * SNAP packets with an organization code of
2286 * 0x080007 (Apple, for Appletalk) and a protocol
2287 * type of ETHERTYPE_ATALK (Appletalk).
2289 * 802.2-encapsulated ETHERTYPE_AARP packets are
2290 * SNAP packets with an organization code of
2291 * 0x000000 (encapsulated Ethernet) and a protocol
2292 * type of ETHERTYPE_AARP (Appletalk ARP).
2294 if (ll_proto
== ETHERTYPE_ATALK
)
2295 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2296 else /* ll_proto == ETHERTYPE_AARP */
2297 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2301 * Check for Ethernet encapsulation (Ethertalk
2302 * phase 1?); we just check for the Ethernet
2305 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2311 if (ll_proto
<= ETHERMTU
) {
2313 * This is an LLC SAP value, so the frames
2314 * that match would be 802.2 frames.
2315 * Check for the 802.2 protocol type
2316 * in the "Ethernet type" field, and
2317 * then check the DSAP.
2319 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2320 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2326 * This is an Ethernet type, so compare
2327 * the length/type field with it (if
2328 * the frame is an 802.2 frame, the length
2329 * field will be <= ETHERMTU, and, as
2330 * "ll_proto" is > ETHERMTU, this test
2331 * will fail and the frame won't match,
2332 * which is what we want).
2334 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2340 * Load a value relative to the beginning of the link-layer header after the
2343 static struct slist
*
2344 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2346 struct slist
*s1
, *s2
;
2349 * Generate code to load the length of the pflog header into
2350 * the register assigned to hold that length, if one has been
2351 * assigned. (If one hasn't been assigned, no code we've
2352 * generated uses that prefix, so we don't need to generate any
2355 if (cstate
->off_linkpl
.reg
!= -1) {
2357 * The length is in the first byte of the header.
2359 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2363 * Round it up to a multiple of 4.
2364 * Add 3, and clear the lower 2 bits.
2366 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2369 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2370 s2
->s
.k
= 0xfffffffc;
2374 * Now allocate a register to hold that value and store
2377 s2
= new_stmt(cstate
, BPF_ST
);
2378 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2382 * Now move it into the X register.
2384 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2392 static struct slist
*
2393 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2395 struct slist
*s1
, *s2
;
2396 struct slist
*sjeq_avs_cookie
;
2397 struct slist
*sjcommon
;
2400 * This code is not compatible with the optimizer, as
2401 * we are generating jmp instructions within a normal
2402 * slist of instructions
2404 cstate
->no_optimize
= 1;
2407 * Generate code to load the length of the radio header into
2408 * the register assigned to hold that length, if one has been
2409 * assigned. (If one hasn't been assigned, no code we've
2410 * generated uses that prefix, so we don't need to generate any
2413 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2414 * or always use the AVS header rather than the Prism header.
2415 * We load a 4-byte big-endian value at the beginning of the
2416 * raw packet data, and see whether, when masked with 0xFFFFF000,
2417 * it's equal to 0x80211000. If so, that indicates that it's
2418 * an AVS header (the masked-out bits are the version number).
2419 * Otherwise, it's a Prism header.
2421 * XXX - the Prism header is also, in theory, variable-length,
2422 * but no known software generates headers that aren't 144
2425 if (cstate
->off_linkhdr
.reg
!= -1) {
2429 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2433 * AND it with 0xFFFFF000.
2435 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2436 s2
->s
.k
= 0xFFFFF000;
2440 * Compare with 0x80211000.
2442 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2443 sjeq_avs_cookie
->s
.k
= 0x80211000;
2444 sappend(s1
, sjeq_avs_cookie
);
2449 * The 4 bytes at an offset of 4 from the beginning of
2450 * the AVS header are the length of the AVS header.
2451 * That field is big-endian.
2453 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2456 sjeq_avs_cookie
->s
.jt
= s2
;
2459 * Now jump to the code to allocate a register
2460 * into which to save the header length and
2461 * store the length there. (The "jump always"
2462 * instruction needs to have the k field set;
2463 * it's added to the PC, so, as we're jumping
2464 * over a single instruction, it should be 1.)
2466 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2468 sappend(s1
, sjcommon
);
2471 * Now for the code that handles the Prism header.
2472 * Just load the length of the Prism header (144)
2473 * into the A register. Have the test for an AVS
2474 * header branch here if we don't have an AVS header.
2476 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2479 sjeq_avs_cookie
->s
.jf
= s2
;
2482 * Now allocate a register to hold that value and store
2483 * it. The code for the AVS header will jump here after
2484 * loading the length of the AVS header.
2486 s2
= new_stmt(cstate
, BPF_ST
);
2487 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2489 sjcommon
->s
.jf
= s2
;
2492 * Now move it into the X register.
2494 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2502 static struct slist
*
2503 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2505 struct slist
*s1
, *s2
;
2508 * Generate code to load the length of the AVS header into
2509 * the register assigned to hold that length, if one has been
2510 * assigned. (If one hasn't been assigned, no code we've
2511 * generated uses that prefix, so we don't need to generate any
2514 if (cstate
->off_linkhdr
.reg
!= -1) {
2516 * The 4 bytes at an offset of 4 from the beginning of
2517 * the AVS header are the length of the AVS header.
2518 * That field is big-endian.
2520 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2524 * Now allocate a register to hold that value and store
2527 s2
= new_stmt(cstate
, BPF_ST
);
2528 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2532 * Now move it into the X register.
2534 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2542 static struct slist
*
2543 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2545 struct slist
*s1
, *s2
;
2548 * Generate code to load the length of the radiotap header into
2549 * the register assigned to hold that length, if one has been
2550 * assigned. (If one hasn't been assigned, no code we've
2551 * generated uses that prefix, so we don't need to generate any
2554 if (cstate
->off_linkhdr
.reg
!= -1) {
2556 * The 2 bytes at offsets of 2 and 3 from the beginning
2557 * of the radiotap header are the length of the radiotap
2558 * header; unfortunately, it's little-endian, so we have
2559 * to load it a byte at a time and construct the value.
2563 * Load the high-order byte, at an offset of 3, shift it
2564 * left a byte, and put the result in the X register.
2566 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2568 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2571 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2575 * Load the next byte, at an offset of 2, and OR the
2576 * value from the X register into it.
2578 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2581 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2585 * Now allocate a register to hold that value and store
2588 s2
= new_stmt(cstate
, BPF_ST
);
2589 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2593 * Now move it into the X register.
2595 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2604 * At the moment we treat PPI as normal Radiotap encoded
2605 * packets. The difference is in the function that generates
2606 * the code at the beginning to compute the header length.
2607 * Since this code generator of PPI supports bare 802.11
2608 * encapsulation only (i.e. the encapsulated DLT should be
2609 * DLT_IEEE802_11) we generate code to check for this too;
2610 * that's done in finish_parse().
2612 static struct slist
*
2613 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2615 struct slist
*s1
, *s2
;
2618 * Generate code to load the length of the radiotap header
2619 * into the register assigned to hold that length, if one has
2622 if (cstate
->off_linkhdr
.reg
!= -1) {
2624 * The 2 bytes at offsets of 2 and 3 from the beginning
2625 * of the radiotap header are the length of the radiotap
2626 * header; unfortunately, it's little-endian, so we have
2627 * to load it a byte at a time and construct the value.
2631 * Load the high-order byte, at an offset of 3, shift it
2632 * left a byte, and put the result in the X register.
2634 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2636 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2639 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2643 * Load the next byte, at an offset of 2, and OR the
2644 * value from the X register into it.
2646 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2649 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2653 * Now allocate a register to hold that value and store
2656 s2
= new_stmt(cstate
, BPF_ST
);
2657 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2661 * Now move it into the X register.
2663 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2672 * Load a value relative to the beginning of the link-layer header after the 802.11
2673 * header, i.e. LLC_SNAP.
2674 * The link-layer header doesn't necessarily begin at the beginning
2675 * of the packet data; there might be a variable-length prefix containing
2676 * radio information.
2678 static struct slist
*
2679 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2682 struct slist
*sjset_data_frame_1
;
2683 struct slist
*sjset_data_frame_2
;
2684 struct slist
*sjset_qos
;
2685 struct slist
*sjset_radiotap_flags_present
;
2686 struct slist
*sjset_radiotap_ext_present
;
2687 struct slist
*sjset_radiotap_tsft_present
;
2688 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2689 struct slist
*s_roundup
;
2691 if (cstate
->off_linkpl
.reg
== -1) {
2693 * No register has been assigned to the offset of
2694 * the link-layer payload, which means nobody needs
2695 * it; don't bother computing it - just return
2696 * what we already have.
2702 * This code is not compatible with the optimizer, as
2703 * we are generating jmp instructions within a normal
2704 * slist of instructions
2706 cstate
->no_optimize
= 1;
2709 * If "s" is non-null, it has code to arrange that the X register
2710 * contains the length of the prefix preceding the link-layer
2713 * Otherwise, the length of the prefix preceding the link-layer
2714 * header is "off_outermostlinkhdr.constant_part".
2718 * There is no variable-length header preceding the
2719 * link-layer header.
2721 * Load the length of the fixed-length prefix preceding
2722 * the link-layer header (if any) into the X register,
2723 * and store it in the cstate->off_linkpl.reg register.
2724 * That length is off_outermostlinkhdr.constant_part.
2726 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2727 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2731 * The X register contains the offset of the beginning of the
2732 * link-layer header; add 24, which is the minimum length
2733 * of the MAC header for a data frame, to that, and store it
2734 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2735 * which is at the offset in the X register, with an indexed load.
2737 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2739 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2742 s2
= new_stmt(cstate
, BPF_ST
);
2743 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2746 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2751 * Check the Frame Control field to see if this is a data frame;
2752 * a data frame has the 0x08 bit (b3) in that field set and the
2753 * 0x04 bit (b2) clear.
2755 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2756 sjset_data_frame_1
->s
.k
= 0x08;
2757 sappend(s
, sjset_data_frame_1
);
2760 * If b3 is set, test b2, otherwise go to the first statement of
2761 * the rest of the program.
2763 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2764 sjset_data_frame_2
->s
.k
= 0x04;
2765 sappend(s
, sjset_data_frame_2
);
2766 sjset_data_frame_1
->s
.jf
= snext
;
2769 * If b2 is not set, this is a data frame; test the QoS bit.
2770 * Otherwise, go to the first statement of the rest of the
2773 sjset_data_frame_2
->s
.jt
= snext
;
2774 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2775 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2776 sappend(s
, sjset_qos
);
2779 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2781 * Otherwise, go to the first statement of the rest of the
2784 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2785 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2787 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2790 s2
= new_stmt(cstate
, BPF_ST
);
2791 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2795 * If we have a radiotap header, look at it to see whether
2796 * there's Atheros padding between the MAC-layer header
2799 * Note: all of the fields in the radiotap header are
2800 * little-endian, so we byte-swap all of the values
2801 * we test against, as they will be loaded as big-endian
2804 * XXX - in the general case, we would have to scan through
2805 * *all* the presence bits, if there's more than one word of
2806 * presence bits. That would require a loop, meaning that
2807 * we wouldn't be able to run the filter in the kernel.
2809 * We assume here that the Atheros adapters that insert the
2810 * annoying padding don't have multiple antennae and therefore
2811 * do not generate radiotap headers with multiple presence words.
2813 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2815 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2816 * in the first presence flag word?
2818 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2822 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2823 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2824 sappend(s
, sjset_radiotap_flags_present
);
2827 * If not, skip all of this.
2829 sjset_radiotap_flags_present
->s
.jf
= snext
;
2832 * Otherwise, is the "extension" bit set in that word?
2834 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2835 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2836 sappend(s
, sjset_radiotap_ext_present
);
2837 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2840 * If so, skip all of this.
2842 sjset_radiotap_ext_present
->s
.jt
= snext
;
2845 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2847 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2848 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2849 sappend(s
, sjset_radiotap_tsft_present
);
2850 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2853 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2854 * at an offset of 16 from the beginning of the raw packet
2855 * data (8 bytes for the radiotap header and 8 bytes for
2858 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2861 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2864 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2866 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2867 sjset_tsft_datapad
->s
.k
= 0x20;
2868 sappend(s
, sjset_tsft_datapad
);
2871 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2872 * at an offset of 8 from the beginning of the raw packet
2873 * data (8 bytes for the radiotap header).
2875 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2878 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2881 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2883 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2884 sjset_notsft_datapad
->s
.k
= 0x20;
2885 sappend(s
, sjset_notsft_datapad
);
2888 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2889 * set, round the length of the 802.11 header to
2890 * a multiple of 4. Do that by adding 3 and then
2891 * dividing by and multiplying by 4, which we do by
2894 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2895 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2896 sappend(s
, s_roundup
);
2897 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2900 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2901 s2
->s
.k
= (bpf_u_int32
)~3;
2903 s2
= new_stmt(cstate
, BPF_ST
);
2904 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2907 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2908 sjset_tsft_datapad
->s
.jf
= snext
;
2909 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2910 sjset_notsft_datapad
->s
.jf
= snext
;
2912 sjset_qos
->s
.jf
= snext
;
2918 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2922 /* There is an implicit dependency between the link
2923 * payload and link header since the payload computation
2924 * includes the variable part of the header. Therefore,
2925 * if nobody else has allocated a register for the link
2926 * header and we need it, do it now. */
2927 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2928 cstate
->off_linkhdr
.reg
== -1)
2929 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2932 * For link-layer types that have a variable-length header
2933 * preceding the link-layer header, generate code to load
2934 * the offset of the link-layer header into the register
2935 * assigned to that offset, if any.
2937 * XXX - this, and the next switch statement, won't handle
2938 * encapsulation of 802.11 or 802.11+radio information in
2939 * some other protocol stack. That's significantly more
2942 switch (cstate
->outermostlinktype
) {
2944 case DLT_PRISM_HEADER
:
2945 s
= gen_load_prism_llprefixlen(cstate
);
2948 case DLT_IEEE802_11_RADIO_AVS
:
2949 s
= gen_load_avs_llprefixlen(cstate
);
2952 case DLT_IEEE802_11_RADIO
:
2953 s
= gen_load_radiotap_llprefixlen(cstate
);
2957 s
= gen_load_ppi_llprefixlen(cstate
);
2966 * For link-layer types that have a variable-length link-layer
2967 * header, generate code to load the offset of the link-layer
2968 * payload into the register assigned to that offset, if any.
2970 switch (cstate
->outermostlinktype
) {
2972 case DLT_IEEE802_11
:
2973 case DLT_PRISM_HEADER
:
2974 case DLT_IEEE802_11_RADIO_AVS
:
2975 case DLT_IEEE802_11_RADIO
:
2977 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2981 s
= gen_load_pflog_llprefixlen(cstate
);
2986 * If there is no initialization yet and we need variable
2987 * length offsets for VLAN, initialize them to zero
2989 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2992 if (cstate
->off_linkpl
.reg
== -1)
2993 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2994 if (cstate
->off_linktype
.reg
== -1)
2995 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2997 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2999 s2
= new_stmt(cstate
, BPF_ST
);
3000 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3002 s2
= new_stmt(cstate
, BPF_ST
);
3003 s2
->s
.k
= cstate
->off_linktype
.reg
;
3008 * If we have any offset-loading code, append all the
3009 * existing statements in the block to those statements,
3010 * and make the resulting list the list of statements
3014 sappend(s
, b
->stmts
);
3019 static struct block
*
3020 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3022 struct slist
*s_load_dlt
;
3025 if (cstate
->linktype
== DLT_PPI
)
3027 /* Create the statements that check for the DLT
3029 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3030 s_load_dlt
->s
.k
= 4;
3032 b
= new_block(cstate
, JMP(BPF_JEQ
));
3034 b
->stmts
= s_load_dlt
;
3035 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3046 * Take an absolute offset, and:
3048 * if it has no variable part, return NULL;
3050 * if it has a variable part, generate code to load the register
3051 * containing that variable part into the X register, returning
3052 * a pointer to that code - if no register for that offset has
3053 * been allocated, allocate it first.
3055 * (The code to set that register will be generated later, but will
3056 * be placed earlier in the code sequence.)
3058 static struct slist
*
3059 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3063 if (off
->is_variable
) {
3064 if (off
->reg
== -1) {
3066 * We haven't yet assigned a register for the
3067 * variable part of the offset of the link-layer
3068 * header; allocate one.
3070 off
->reg
= alloc_reg(cstate
);
3074 * Load the register containing the variable part of the
3075 * offset of the link-layer header into the X register.
3077 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3082 * That offset isn't variable, there's no variable part,
3083 * so we don't need to generate any code.
3090 * Map an Ethernet type to the equivalent PPP type.
3093 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3101 case ETHERTYPE_IPV6
:
3102 ll_proto
= PPP_IPV6
;
3106 ll_proto
= PPP_DECNET
;
3109 case ETHERTYPE_ATALK
:
3110 ll_proto
= PPP_APPLE
;
3123 * I'm assuming the "Bridging PDU"s that go
3124 * over PPP are Spanning Tree Protocol
3127 ll_proto
= PPP_BRPDU
;
3138 * Generate any tests that, for encapsulation of a link-layer packet
3139 * inside another protocol stack, need to be done to check for those
3140 * link-layer packets (and that haven't already been done by a check
3141 * for that encapsulation).
3143 static struct block
*
3144 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3148 if (cstate
->is_geneve
)
3149 return gen_geneve_ll_check(cstate
);
3151 switch (cstate
->prevlinktype
) {
3155 * This is LANE-encapsulated Ethernet; check that the LANE
3156 * packet doesn't begin with an LE Control marker, i.e.
3157 * that it's data, not a control message.
3159 * (We've already generated a test for LANE.)
3161 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3167 * No such tests are necessary.
3175 * The three different values we should check for when checking for an
3176 * IPv6 packet with DLT_NULL.
3178 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3179 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3180 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3183 * Generate code to match a particular packet type by matching the
3184 * link-layer type field or fields in the 802.2 LLC header.
3186 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3187 * value, if <= ETHERMTU.
3189 static struct block
*
3190 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3192 struct block
*b0
, *b1
, *b2
;
3193 const char *description
;
3195 /* are we checking MPLS-encapsulated packets? */
3196 if (cstate
->label_stack_depth
> 0)
3197 return gen_mpls_linktype(cstate
, ll_proto
);
3199 switch (cstate
->linktype
) {
3202 case DLT_NETANALYZER
:
3203 case DLT_NETANALYZER_TRANSPARENT
:
3204 /* Geneve has an EtherType regardless of whether there is an
3206 if (!cstate
->is_geneve
)
3207 b0
= gen_prevlinkhdr_check(cstate
);
3211 b1
= gen_ether_linktype(cstate
, ll_proto
);
3222 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3226 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3230 case DLT_IEEE802_11
:
3231 case DLT_PRISM_HEADER
:
3232 case DLT_IEEE802_11_RADIO_AVS
:
3233 case DLT_IEEE802_11_RADIO
:
3236 * Check that we have a data frame.
3238 b0
= gen_check_802_11_data_frame(cstate
);
3241 * Now check for the specified link-layer type.
3243 b1
= gen_llc_linktype(cstate
, ll_proto
);
3250 * XXX - check for LLC frames.
3252 return gen_llc_linktype(cstate
, ll_proto
);
3257 * XXX - check for LLC PDUs, as per IEEE 802.5.
3259 return gen_llc_linktype(cstate
, ll_proto
);
3262 case DLT_ATM_RFC1483
:
3264 case DLT_IP_OVER_FC
:
3265 return gen_llc_linktype(cstate
, ll_proto
);
3270 * Check for an LLC-encapsulated version of this protocol;
3271 * if we were checking for LANE, linktype would no longer
3274 * Check for LLC encapsulation and then check the protocol.
3276 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3277 b1
= gen_llc_linktype(cstate
, ll_proto
);
3283 return gen_linux_sll_linktype(cstate
, ll_proto
);
3287 case DLT_SLIP_BSDOS
:
3290 * These types don't provide any type field; packets
3291 * are always IPv4 or IPv6.
3293 * XXX - for IPv4, check for a version number of 4, and,
3294 * for IPv6, check for a version number of 6?
3299 /* Check for a version number of 4. */
3300 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3302 case ETHERTYPE_IPV6
:
3303 /* Check for a version number of 6. */
3304 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3307 return gen_false(cstate
); /* always false */
3313 * Raw IPv4, so no type field.
3315 if (ll_proto
== ETHERTYPE_IP
)
3316 return gen_true(cstate
); /* always true */
3318 /* Checking for something other than IPv4; always false */
3319 return gen_false(cstate
);
3324 * Raw IPv6, so no type field.
3326 if (ll_proto
== ETHERTYPE_IPV6
)
3327 return gen_true(cstate
); /* always true */
3329 /* Checking for something other than IPv6; always false */
3330 return gen_false(cstate
);
3335 case DLT_PPP_SERIAL
:
3338 * We use Ethernet protocol types inside libpcap;
3339 * map them to the corresponding PPP protocol types.
3341 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3342 ethertype_to_ppptype(ll_proto
));
3347 * We use Ethernet protocol types inside libpcap;
3348 * map them to the corresponding PPP protocol types.
3354 * Also check for Van Jacobson-compressed IP.
3355 * XXX - do this for other forms of PPP?
3357 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3358 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3360 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3365 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3366 ethertype_to_ppptype(ll_proto
));
3376 return (gen_loopback_linktype(cstate
, AF_INET
));
3378 case ETHERTYPE_IPV6
:
3380 * AF_ values may, unfortunately, be platform-
3381 * dependent; AF_INET isn't, because everybody
3382 * used 4.2BSD's value, but AF_INET6 is, because
3383 * 4.2BSD didn't have a value for it (given that
3384 * IPv6 didn't exist back in the early 1980's),
3385 * and they all picked their own values.
3387 * This means that, if we're reading from a
3388 * savefile, we need to check for all the
3391 * If we're doing a live capture, we only need
3392 * to check for this platform's value; however,
3393 * Npcap uses 24, which isn't Windows's AF_INET6
3394 * value. (Given the multiple different values,
3395 * programs that read pcap files shouldn't be
3396 * checking for their platform's AF_INET6 value
3397 * anyway, they should check for all of the
3398 * possible values. and they might as well do
3399 * that even for live captures.)
3401 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3403 * Savefile - check for all three
3404 * possible IPv6 values.
3406 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3407 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3409 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3414 * Live capture, so we only need to
3415 * check for the value used on this
3420 * Npcap doesn't use Windows's AF_INET6,
3421 * as that collides with AF_IPX on
3422 * some BSDs (both have the value 23).
3423 * Instead, it uses 24.
3425 return (gen_loopback_linktype(cstate
, 24));
3428 return (gen_loopback_linktype(cstate
, AF_INET6
));
3429 #else /* AF_INET6 */
3431 * I guess this platform doesn't support
3432 * IPv6, so we just reject all packets.
3434 return gen_false(cstate
);
3435 #endif /* AF_INET6 */
3441 * Not a type on which we support filtering.
3442 * XXX - support those that have AF_ values
3443 * #defined on this platform, at least?
3445 return gen_false(cstate
);
3450 * af field is host byte order in contrast to the rest of
3453 if (ll_proto
== ETHERTYPE_IP
)
3454 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3456 else if (ll_proto
== ETHERTYPE_IPV6
)
3457 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3460 return gen_false(cstate
);
3464 case DLT_ARCNET_LINUX
:
3466 * XXX should we check for first fragment if the protocol
3472 return gen_false(cstate
);
3474 case ETHERTYPE_IPV6
:
3475 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3479 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3481 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3487 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3489 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3494 case ETHERTYPE_REVARP
:
3495 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3498 case ETHERTYPE_ATALK
:
3499 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3506 case ETHERTYPE_ATALK
:
3507 return gen_true(cstate
);
3509 return gen_false(cstate
);
3515 * XXX - assumes a 2-byte Frame Relay header with
3516 * DLCI and flags. What if the address is longer?
3522 * Check for the special NLPID for IP.
3524 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3526 case ETHERTYPE_IPV6
:
3528 * Check for the special NLPID for IPv6.
3530 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3534 * Check for several OSI protocols.
3536 * Frame Relay packets typically have an OSI
3537 * NLPID at the beginning; we check for each
3540 * What we check for is the NLPID and a frame
3541 * control field of UI, i.e. 0x03 followed
3544 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3545 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3546 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3552 return gen_false(cstate
);
3557 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3559 case DLT_JUNIPER_MFR
:
3560 case DLT_JUNIPER_MLFR
:
3561 case DLT_JUNIPER_MLPPP
:
3562 case DLT_JUNIPER_ATM1
:
3563 case DLT_JUNIPER_ATM2
:
3564 case DLT_JUNIPER_PPPOE
:
3565 case DLT_JUNIPER_PPPOE_ATM
:
3566 case DLT_JUNIPER_GGSN
:
3567 case DLT_JUNIPER_ES
:
3568 case DLT_JUNIPER_MONITOR
:
3569 case DLT_JUNIPER_SERVICES
:
3570 case DLT_JUNIPER_ETHER
:
3571 case DLT_JUNIPER_PPP
:
3572 case DLT_JUNIPER_FRELAY
:
3573 case DLT_JUNIPER_CHDLC
:
3574 case DLT_JUNIPER_VP
:
3575 case DLT_JUNIPER_ST
:
3576 case DLT_JUNIPER_ISM
:
3577 case DLT_JUNIPER_VS
:
3578 case DLT_JUNIPER_SRX_E2E
:
3579 case DLT_JUNIPER_FIBRECHANNEL
:
3580 case DLT_JUNIPER_ATM_CEMIC
:
3582 /* just lets verify the magic number for now -
3583 * on ATM we may have up to 6 different encapsulations on the wire
3584 * and need a lot of heuristics to figure out that the payload
3587 * FIXME encapsulation specific BPF_ filters
3589 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3591 case DLT_BACNET_MS_TP
:
3592 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3595 return gen_ipnet_linktype(cstate
, ll_proto
);
3597 case DLT_LINUX_IRDA
:
3598 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3601 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3604 case DLT_MTP2_WITH_PHDR
:
3605 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3608 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3611 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3613 case DLT_LINUX_LAPD
:
3614 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3616 case DLT_USB_FREEBSD
:
3618 case DLT_USB_LINUX_MMAPPED
:
3620 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3622 case DLT_BLUETOOTH_HCI_H4
:
3623 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3624 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3627 case DLT_CAN_SOCKETCAN
:
3628 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3630 case DLT_IEEE802_15_4
:
3631 case DLT_IEEE802_15_4_LINUX
:
3632 case DLT_IEEE802_15_4_NONASK_PHY
:
3633 case DLT_IEEE802_15_4_NOFCS
:
3634 case DLT_IEEE802_15_4_TAP
:
3635 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3637 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3638 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3641 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3644 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3646 case DLT_IPMB_KONTRON
:
3647 case DLT_IPMB_LINUX
:
3648 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3651 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3654 /* Using the fixed-size NFLOG header it is possible to tell only
3655 * the address family of the packet, other meaningful data is
3656 * either missing or behind TLVs.
3658 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3662 * Does this link-layer header type have a field
3663 * indicating the type of the next protocol? If
3664 * so, off_linktype.constant_part will be the offset of that
3665 * field in the packet; if not, it will be OFFSET_NOT_SET.
3667 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3669 * Yes; assume it's an Ethernet type. (If
3670 * it's not, it needs to be handled specially
3673 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3677 * No; report an error.
3679 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3680 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3688 * Check for an LLC SNAP packet with a given organization code and
3689 * protocol type; we check the entire contents of the 802.2 LLC and
3690 * snap headers, checking for DSAP and SSAP of SNAP and a control
3691 * field of 0x03 in the LLC header, and for the specified organization
3692 * code and protocol type in the SNAP header.
3694 static struct block
*
3695 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3697 u_char snapblock
[8];
3699 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3700 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3701 snapblock
[2] = 0x03; /* control = UI */
3702 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3703 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3704 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3705 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3706 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3707 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3711 * Generate code to match frames with an LLC header.
3713 static struct block
*
3714 gen_llc_internal(compiler_state_t
*cstate
)
3716 struct block
*b0
, *b1
;
3718 switch (cstate
->linktype
) {
3722 * We check for an Ethernet type field less than
3723 * 1500, which means it's an 802.3 length field.
3725 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3729 * Now check for the purported DSAP and SSAP not being
3730 * 0xFF, to rule out NetWare-over-802.3.
3732 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3739 * We check for LLC traffic.
3741 b0
= gen_atmtype_llc(cstate
);
3744 case DLT_IEEE802
: /* Token Ring */
3746 * XXX - check for LLC frames.
3748 return gen_true(cstate
);
3752 * XXX - check for LLC frames.
3754 return gen_true(cstate
);
3756 case DLT_ATM_RFC1483
:
3758 * For LLC encapsulation, these are defined to have an
3761 * For VC encapsulation, they don't, but there's no
3762 * way to check for that; the protocol used on the VC
3763 * is negotiated out of band.
3765 return gen_true(cstate
);
3767 case DLT_IEEE802_11
:
3768 case DLT_PRISM_HEADER
:
3769 case DLT_IEEE802_11_RADIO
:
3770 case DLT_IEEE802_11_RADIO_AVS
:
3773 * Check that we have a data frame.
3775 b0
= gen_check_802_11_data_frame(cstate
);
3779 bpf_error(cstate
, "'llc' not supported for %s",
3780 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3786 gen_llc(compiler_state_t
*cstate
)
3789 * Catch errors reported by us and routines below us, and return NULL
3792 if (setjmp(cstate
->top_ctx
))
3795 return gen_llc_internal(cstate
);
3799 gen_llc_i(compiler_state_t
*cstate
)
3801 struct block
*b0
, *b1
;
3805 * Catch errors reported by us and routines below us, and return NULL
3808 if (setjmp(cstate
->top_ctx
))
3812 * Check whether this is an LLC frame.
3814 b0
= gen_llc_internal(cstate
);
3817 * Load the control byte and test the low-order bit; it must
3818 * be clear for I frames.
3820 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3821 b1
= new_block(cstate
, JMP(BPF_JSET
));
3830 gen_llc_s(compiler_state_t
*cstate
)
3832 struct block
*b0
, *b1
;
3835 * Catch errors reported by us and routines below us, and return NULL
3838 if (setjmp(cstate
->top_ctx
))
3842 * Check whether this is an LLC frame.
3844 b0
= gen_llc_internal(cstate
);
3847 * Now compare the low-order 2 bit of the control byte against
3848 * the appropriate value for S frames.
3850 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3856 gen_llc_u(compiler_state_t
*cstate
)
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 compare the low-order 2 bit of the control byte against
3874 * the appropriate value for U frames.
3876 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3882 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3884 struct block
*b0
, *b1
;
3887 * Catch errors reported by us and routines below us, and return NULL
3890 if (setjmp(cstate
->top_ctx
))
3894 * Check whether this is an LLC frame.
3896 b0
= gen_llc_internal(cstate
);
3899 * Now check for an S frame with the appropriate type.
3901 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3907 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3909 struct block
*b0
, *b1
;
3912 * Catch errors reported by us and routines below us, and return NULL
3915 if (setjmp(cstate
->top_ctx
))
3919 * Check whether this is an LLC frame.
3921 b0
= gen_llc_internal(cstate
);
3924 * Now check for a U frame with the appropriate type.
3926 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3932 * Generate code to match a particular packet type, for link-layer types
3933 * using 802.2 LLC headers.
3935 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3936 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3938 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3939 * value, if <= ETHERMTU. We use that to determine whether to
3940 * match the DSAP or both DSAP and LSAP or to check the OUI and
3941 * protocol ID in a SNAP header.
3943 static struct block
*
3944 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3947 * XXX - handle token-ring variable-length header.
3953 case LLCSAP_NETBEUI
:
3955 * XXX - should we check both the DSAP and the
3956 * SSAP, like this, or should we check just the
3957 * DSAP, as we do for other SAP values?
3959 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3960 ((ll_proto
<< 8) | ll_proto
));
3964 * XXX - are there ever SNAP frames for IPX on
3965 * non-Ethernet 802.x networks?
3967 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3969 case ETHERTYPE_ATALK
:
3971 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3972 * SNAP packets with an organization code of
3973 * 0x080007 (Apple, for Appletalk) and a protocol
3974 * type of ETHERTYPE_ATALK (Appletalk).
3976 * XXX - check for an organization code of
3977 * encapsulated Ethernet as well?
3979 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3983 * XXX - we don't have to check for IPX 802.3
3984 * here, but should we check for the IPX Ethertype?
3986 if (ll_proto
<= ETHERMTU
) {
3988 * This is an LLC SAP value, so check
3991 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
3994 * This is an Ethernet type; we assume that it's
3995 * unlikely that it'll appear in the right place
3996 * at random, and therefore check only the
3997 * location that would hold the Ethernet type
3998 * in a SNAP frame with an organization code of
3999 * 0x000000 (encapsulated Ethernet).
4001 * XXX - if we were to check for the SNAP DSAP and
4002 * LSAP, as per XXX, and were also to check for an
4003 * organization code of 0x000000 (encapsulated
4004 * Ethernet), we'd do
4006 * return gen_snap(cstate, 0x000000, ll_proto);
4008 * here; for now, we don't, as per the above.
4009 * I don't know whether it's worth the extra CPU
4010 * time to do the right check or not.
4012 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4017 static struct block
*
4018 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4019 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4021 struct block
*b0
, *b1
;
4035 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4036 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4042 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4043 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4048 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4052 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4056 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4060 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4064 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4068 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4075 b0
= gen_linktype(cstate
, ll_proto
);
4076 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4082 static struct block
*
4083 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4084 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4087 struct block
*b0
, *b1
;
4102 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4103 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4109 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4110 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4115 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4119 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4123 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4127 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4131 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4135 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4142 /* this order is important */
4143 a
= (uint32_t *)addr
;
4144 m
= (uint32_t *)mask
;
4145 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4146 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4148 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4150 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4152 b0
= gen_linktype(cstate
, ll_proto
);
4158 static struct block
*
4159 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4161 register struct block
*b0
, *b1
;
4165 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4168 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4171 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4172 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4178 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4179 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4184 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4188 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4192 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4196 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4200 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4204 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4212 * Like gen_ehostop, but for DLT_FDDI
4214 static struct block
*
4215 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4217 struct block
*b0
, *b1
;
4221 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4224 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4227 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4228 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4234 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4235 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4240 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4244 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4248 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4252 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4256 bpf_error(cstate
, "'ra' is only supported on 802.11");
4260 bpf_error(cstate
, "'ta' is only supported on 802.11");
4268 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4270 static struct block
*
4271 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4273 register struct block
*b0
, *b1
;
4277 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4280 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4283 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4284 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4290 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4291 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4296 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4300 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4304 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4308 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4312 bpf_error(cstate
, "'ra' is only supported on 802.11");
4316 bpf_error(cstate
, "'ta' is only supported on 802.11");
4324 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4325 * various 802.11 + radio headers.
4327 static struct block
*
4328 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4330 register struct block
*b0
, *b1
, *b2
;
4331 register struct slist
*s
;
4333 #ifdef ENABLE_WLAN_FILTERING_PATCH
4336 * We need to disable the optimizer because the optimizer is buggy
4337 * and wipes out some LD instructions generated by the below
4338 * code to validate the Frame Control bits
4340 cstate
->no_optimize
= 1;
4341 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4348 * For control frames, there is no SA.
4350 * For management frames, SA is at an
4351 * offset of 10 from the beginning of
4354 * For data frames, SA is at an offset
4355 * of 10 from the beginning of the packet
4356 * if From DS is clear, at an offset of
4357 * 16 from the beginning of the packet
4358 * if From DS is set and To DS is clear,
4359 * and an offset of 24 from the beginning
4360 * of the packet if From DS is set and To DS
4365 * Generate the tests to be done for data frames
4368 * First, check for To DS set, i.e. check "link[1] & 0x01".
4370 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4371 b1
= new_block(cstate
, JMP(BPF_JSET
));
4372 b1
->s
.k
= 0x01; /* To DS */
4376 * If To DS is set, the SA is at 24.
4378 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4382 * Now, check for To DS not set, i.e. check
4383 * "!(link[1] & 0x01)".
4385 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4386 b2
= new_block(cstate
, JMP(BPF_JSET
));
4387 b2
->s
.k
= 0x01; /* To DS */
4392 * If To DS is not set, the SA is at 16.
4394 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4398 * Now OR together the last two checks. That gives
4399 * the complete set of checks for data frames with
4405 * Now check for From DS being set, and AND that with
4406 * the ORed-together checks.
4408 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4409 b1
= new_block(cstate
, JMP(BPF_JSET
));
4410 b1
->s
.k
= 0x02; /* From DS */
4415 * Now check for data frames with From DS not set.
4417 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4418 b2
= new_block(cstate
, JMP(BPF_JSET
));
4419 b2
->s
.k
= 0x02; /* From DS */
4424 * If From DS isn't set, the SA is at 10.
4426 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4430 * Now OR together the checks for data frames with
4431 * From DS not set and for data frames with From DS
4432 * set; that gives the checks done for data frames.
4437 * Now check for a data frame.
4438 * I.e, check "link[0] & 0x08".
4440 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4441 b1
= new_block(cstate
, JMP(BPF_JSET
));
4446 * AND that with the checks done for data frames.
4451 * If the high-order bit of the type value is 0, this
4452 * is a management frame.
4453 * I.e, check "!(link[0] & 0x08)".
4455 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4456 b2
= new_block(cstate
, JMP(BPF_JSET
));
4462 * For management frames, the SA is at 10.
4464 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4468 * OR that with the checks done for data frames.
4469 * That gives the checks done for management and
4475 * If the low-order bit of the type value is 1,
4476 * this is either a control frame or a frame
4477 * with a reserved type, and thus not a
4480 * I.e., check "!(link[0] & 0x04)".
4482 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4483 b1
= new_block(cstate
, JMP(BPF_JSET
));
4489 * AND that with the checks for data and management
4499 * For control frames, there is no DA.
4501 * For management frames, DA is at an
4502 * offset of 4 from the beginning of
4505 * For data frames, DA is at an offset
4506 * of 4 from the beginning of the packet
4507 * if To DS is clear and at an offset of
4508 * 16 from the beginning of the packet
4513 * Generate the tests to be done for data frames.
4515 * First, check for To DS set, i.e. "link[1] & 0x01".
4517 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4518 b1
= new_block(cstate
, JMP(BPF_JSET
));
4519 b1
->s
.k
= 0x01; /* To DS */
4523 * If To DS is set, the DA is at 16.
4525 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4529 * Now, check for To DS not set, i.e. check
4530 * "!(link[1] & 0x01)".
4532 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4533 b2
= new_block(cstate
, JMP(BPF_JSET
));
4534 b2
->s
.k
= 0x01; /* To DS */
4539 * If To DS is not set, the DA is at 4.
4541 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4545 * Now OR together the last two checks. That gives
4546 * the complete set of checks for data frames.
4551 * Now check for a data frame.
4552 * I.e, check "link[0] & 0x08".
4554 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4555 b1
= new_block(cstate
, JMP(BPF_JSET
));
4560 * AND that with the checks done for data frames.
4565 * If the high-order bit of the type value is 0, this
4566 * is a management frame.
4567 * I.e, check "!(link[0] & 0x08)".
4569 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4570 b2
= new_block(cstate
, JMP(BPF_JSET
));
4576 * For management frames, the DA is at 4.
4578 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4582 * OR that with the checks done for data frames.
4583 * That gives the checks done for management and
4589 * If the low-order bit of the type value is 1,
4590 * this is either a control frame or a frame
4591 * with a reserved type, and thus not a
4594 * I.e., check "!(link[0] & 0x04)".
4596 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4597 b1
= new_block(cstate
, JMP(BPF_JSET
));
4603 * AND that with the checks for data and management
4610 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4611 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4617 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4618 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4623 * XXX - add BSSID keyword?
4626 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4630 * Not present in CTS or ACK control frames.
4632 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4633 IEEE80211_FC0_TYPE_MASK
);
4635 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4636 IEEE80211_FC0_SUBTYPE_MASK
);
4638 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4639 IEEE80211_FC0_SUBTYPE_MASK
);
4643 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4649 * Not present in control frames.
4651 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4652 IEEE80211_FC0_TYPE_MASK
);
4654 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4660 * Present only if the direction mask has both "From DS"
4661 * and "To DS" set. Neither control frames nor management
4662 * frames should have both of those set, so we don't
4663 * check the frame type.
4665 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4666 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4667 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4673 * Not present in management frames; addr1 in other
4678 * If the high-order bit of the type value is 0, this
4679 * is a management frame.
4680 * I.e, check "(link[0] & 0x08)".
4682 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4683 b1
= new_block(cstate
, JMP(BPF_JSET
));
4690 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4693 * AND that with the check of addr1.
4700 * Not present in management frames; addr2, if present,
4705 * Not present in CTS or ACK control frames.
4707 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4708 IEEE80211_FC0_TYPE_MASK
);
4710 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4711 IEEE80211_FC0_SUBTYPE_MASK
);
4713 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4714 IEEE80211_FC0_SUBTYPE_MASK
);
4720 * If the high-order bit of the type value is 0, this
4721 * is a management frame.
4722 * I.e, check "(link[0] & 0x08)".
4724 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4725 b1
= new_block(cstate
, JMP(BPF_JSET
));
4730 * AND that with the check for frames other than
4731 * CTS and ACK frames.
4738 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4747 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4748 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4749 * as the RFC states.)
4751 static struct block
*
4752 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4754 register struct block
*b0
, *b1
;
4758 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4761 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4764 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4765 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4771 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4772 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4777 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4781 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4785 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4789 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4793 bpf_error(cstate
, "'ra' is only supported on 802.11");
4797 bpf_error(cstate
, "'ta' is only supported on 802.11");
4805 * This is quite tricky because there may be pad bytes in front of the
4806 * DECNET header, and then there are two possible data packet formats that
4807 * carry both src and dst addresses, plus 5 packet types in a format that
4808 * carries only the src node, plus 2 types that use a different format and
4809 * also carry just the src node.
4813 * Instead of doing those all right, we just look for data packets with
4814 * 0 or 1 bytes of padding. If you want to look at other packets, that
4815 * will require a lot more hacking.
4817 * To add support for filtering on DECNET "areas" (network numbers)
4818 * one would want to add a "mask" argument to this routine. That would
4819 * make the filter even more inefficient, although one could be clever
4820 * and not generate masking instructions if the mask is 0xFFFF.
4822 static struct block
*
4823 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4825 struct block
*b0
, *b1
, *b2
, *tmp
;
4826 u_int offset_lh
; /* offset if long header is received */
4827 u_int offset_sh
; /* offset if short header is received */
4832 offset_sh
= 1; /* follows flags */
4833 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4837 offset_sh
= 3; /* follows flags, dstnode */
4838 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4842 /* Inefficient because we do our Calvinball dance twice */
4843 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4844 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4850 /* Inefficient because we do our Calvinball dance twice */
4851 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4852 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4857 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4861 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4865 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4869 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4873 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4877 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4884 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4885 /* Check for pad = 1, long header case */
4886 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4887 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4888 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4889 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4891 /* Check for pad = 0, long header case */
4892 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4894 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4895 (bpf_u_int32
)ntohs((u_short
)addr
));
4898 /* Check for pad = 1, short header case */
4899 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4900 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4901 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4902 (bpf_u_int32
)ntohs((u_short
)addr
));
4905 /* Check for pad = 0, short header case */
4906 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4908 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4909 (bpf_u_int32
)ntohs((u_short
)addr
));
4913 /* Combine with test for cstate->linktype */
4919 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4920 * test the bottom-of-stack bit, and then check the version number
4921 * field in the IP header.
4923 static struct block
*
4924 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4926 struct block
*b0
, *b1
;
4931 /* match the bottom-of-stack bit */
4932 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4933 /* match the IPv4 version number */
4934 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4938 case ETHERTYPE_IPV6
:
4939 /* match the bottom-of-stack bit */
4940 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4941 /* match the IPv4 version number */
4942 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4947 /* FIXME add other L3 proto IDs */
4948 bpf_error(cstate
, "unsupported protocol over mpls");
4953 static struct block
*
4954 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4955 int proto
, int dir
, int type
)
4957 struct block
*b0
, *b1
;
4958 const char *typestr
;
4968 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4970 * Only check for non-IPv4 addresses if we're not
4971 * checking MPLS-encapsulated packets.
4973 if (cstate
->label_stack_depth
== 0) {
4974 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4976 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4982 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4985 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4988 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4991 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4994 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4997 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
5000 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5003 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5006 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5009 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5012 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5015 return gen_dnhostop(cstate
, addr
, dir
);
5018 bpf_error(cstate
, "LAT host filtering not implemented");
5021 bpf_error(cstate
, "SCA host filtering not implemented");
5024 bpf_error(cstate
, "MOPRC host filtering not implemented");
5027 bpf_error(cstate
, "MOPDL host filtering not implemented");
5030 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5033 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5036 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5039 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5042 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5045 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5048 bpf_error(cstate
, "AARP host filtering not implemented");
5051 bpf_error(cstate
, "ISO host filtering not implemented");
5054 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5057 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5060 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5063 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5066 bpf_error(cstate
, "IPX host filtering not implemented");
5069 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5072 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5075 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5078 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5081 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5084 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5087 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5090 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5093 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5096 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5105 static struct block
*
5106 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5107 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5109 const char *typestr
;
5119 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5122 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5125 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5128 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5131 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5134 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5137 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5140 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5143 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5146 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5149 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5152 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5155 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5158 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5161 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5164 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5167 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5170 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5173 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5176 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5179 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5182 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5185 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5188 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5191 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5194 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5197 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5200 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5203 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5206 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5209 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5212 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5215 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5218 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5221 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5224 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5227 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5230 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5233 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5236 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5246 static struct block
*
5247 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5248 struct addrinfo
*alist
, int proto
, int dir
)
5250 struct block
*b0
, *b1
, *tmp
;
5251 struct addrinfo
*ai
;
5252 struct sockaddr_in
*sin
;
5255 bpf_error(cstate
, "direction applied to 'gateway'");
5262 switch (cstate
->linktype
) {
5264 case DLT_NETANALYZER
:
5265 case DLT_NETANALYZER_TRANSPARENT
:
5266 b1
= gen_prevlinkhdr_check(cstate
);
5267 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5272 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5275 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5277 case DLT_IEEE802_11
:
5278 case DLT_PRISM_HEADER
:
5279 case DLT_IEEE802_11_RADIO_AVS
:
5280 case DLT_IEEE802_11_RADIO
:
5282 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5286 * This is LLC-multiplexed traffic; if it were
5287 * LANE, cstate->linktype would have been set to
5291 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5293 case DLT_IP_OVER_FC
:
5294 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5298 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5301 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5303 * Does it have an address?
5305 if (ai
->ai_addr
!= NULL
) {
5307 * Yes. Is it an IPv4 address?
5309 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5311 * Generate an entry for it.
5313 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5314 tmp
= gen_host(cstate
,
5315 ntohl(sin
->sin_addr
.s_addr
),
5316 0xffffffff, proto
, Q_OR
, Q_HOST
);
5318 * Is it the *first* IPv4 address?
5322 * Yes, so start with it.
5327 * No, so OR it into the
5339 * No IPv4 addresses found.
5347 bpf_error(cstate
, "illegal modifier of 'gateway'");
5352 static struct block
*
5353 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5361 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5365 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5369 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5373 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5376 #ifndef IPPROTO_IGMP
5377 #define IPPROTO_IGMP 2
5381 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5384 #ifndef IPPROTO_IGRP
5385 #define IPPROTO_IGRP 9
5388 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5392 #define IPPROTO_PIM 103
5396 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5399 #ifndef IPPROTO_VRRP
5400 #define IPPROTO_VRRP 112
5404 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5407 #ifndef IPPROTO_CARP
5408 #define IPPROTO_CARP 112
5412 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5416 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5420 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5424 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5428 bpf_error(cstate
, "link layer applied in wrong context");
5431 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5435 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5439 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5443 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5447 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5451 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5455 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5459 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5462 #ifndef IPPROTO_ICMPV6
5463 #define IPPROTO_ICMPV6 58
5466 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5470 #define IPPROTO_AH 51
5473 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5477 #define IPPROTO_ESP 50
5480 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5484 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5488 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5492 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5495 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5496 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5497 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5499 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5501 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5503 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5507 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5508 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5509 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5511 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5513 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5515 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5519 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5520 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5521 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5523 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5528 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5529 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5534 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5535 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5537 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5539 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5544 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5545 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5550 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5551 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5556 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5560 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5564 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5568 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5572 bpf_error(cstate
, "'radio' is not a valid protocol type");
5581 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5584 * Catch errors reported by us and routines below us, and return NULL
5587 if (setjmp(cstate
->top_ctx
))
5590 return gen_proto_abbrev_internal(cstate
, proto
);
5593 static struct block
*
5594 gen_ipfrag(compiler_state_t
*cstate
)
5599 /* not IPv4 frag other than the first frag */
5600 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5601 b
= new_block(cstate
, JMP(BPF_JSET
));
5610 * Generate a comparison to a port value in the transport-layer header
5611 * at the specified offset from the beginning of that header.
5613 * XXX - this handles a variable-length prefix preceding the link-layer
5614 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5615 * variable-length link-layer headers (such as Token Ring or 802.11
5618 static struct block
*
5619 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5621 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5624 static struct block
*
5625 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5627 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5630 static struct block
*
5631 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5633 struct block
*b0
, *b1
, *tmp
;
5635 /* ip proto 'proto' and not a fragment other than the first fragment */
5636 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5637 b0
= gen_ipfrag(cstate
);
5642 b1
= gen_portatom(cstate
, 0, port
);
5646 b1
= gen_portatom(cstate
, 2, port
);
5650 tmp
= gen_portatom(cstate
, 0, port
);
5651 b1
= gen_portatom(cstate
, 2, port
);
5657 tmp
= gen_portatom(cstate
, 0, port
);
5658 b1
= gen_portatom(cstate
, 2, port
);
5663 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5667 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5671 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5675 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5679 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5683 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5695 static struct block
*
5696 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5698 struct block
*b0
, *b1
, *tmp
;
5703 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5704 * not LLC encapsulation with LLCSAP_IP.
5706 * For IEEE 802 networks - which includes 802.5 token ring
5707 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5708 * says that SNAP encapsulation is used, not LLC encapsulation
5711 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5712 * RFC 2225 say that SNAP encapsulation is used, not LLC
5713 * encapsulation with LLCSAP_IP.
5715 * So we always check for ETHERTYPE_IP.
5717 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5723 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5727 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5728 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5730 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5742 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5744 struct block
*b0
, *b1
, *tmp
;
5746 /* ip6 proto 'proto' */
5747 /* XXX - catch the first fragment of a fragmented packet? */
5748 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5752 b1
= gen_portatom6(cstate
, 0, port
);
5756 b1
= gen_portatom6(cstate
, 2, port
);
5760 tmp
= gen_portatom6(cstate
, 0, port
);
5761 b1
= gen_portatom6(cstate
, 2, port
);
5767 tmp
= gen_portatom6(cstate
, 0, port
);
5768 b1
= gen_portatom6(cstate
, 2, port
);
5780 static struct block
*
5781 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5783 struct block
*b0
, *b1
, *tmp
;
5785 /* link proto ip6 */
5786 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5792 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5796 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5797 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5799 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5810 /* gen_portrange code */
5811 static struct block
*
5812 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5815 struct block
*b1
, *b2
;
5819 * Reverse the order of the ports, so v1 is the lower one.
5828 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5829 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5836 static struct block
*
5837 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5838 bpf_u_int32 proto
, int dir
)
5840 struct block
*b0
, *b1
, *tmp
;
5842 /* ip proto 'proto' and not a fragment other than the first fragment */
5843 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5844 b0
= gen_ipfrag(cstate
);
5849 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5853 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5857 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5858 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5864 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5865 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5870 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5874 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5878 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5882 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5886 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5890 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5902 static struct block
*
5903 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5906 struct block
*b0
, *b1
, *tmp
;
5909 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5915 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5920 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5921 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5923 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5934 static struct block
*
5935 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5938 struct block
*b1
, *b2
;
5942 * Reverse the order of the ports, so v1 is the lower one.
5951 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5952 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5959 static struct block
*
5960 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5961 bpf_u_int32 proto
, int dir
)
5963 struct block
*b0
, *b1
, *tmp
;
5965 /* ip6 proto 'proto' */
5966 /* XXX - catch the first fragment of a fragmented packet? */
5967 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5971 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5975 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5979 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5980 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5986 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5987 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5999 static struct block
*
6000 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6003 struct block
*b0
, *b1
, *tmp
;
6005 /* link proto ip6 */
6006 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6012 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6017 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6018 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6020 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6032 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6041 v
= pcap_nametoproto(name
);
6042 if (v
== PROTO_UNDEF
)
6043 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6047 /* XXX should look up h/w protocol type based on cstate->linktype */
6048 v
= pcap_nametoeproto(name
);
6049 if (v
== PROTO_UNDEF
) {
6050 v
= pcap_nametollc(name
);
6051 if (v
== PROTO_UNDEF
)
6052 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6057 if (strcmp(name
, "esis") == 0)
6059 else if (strcmp(name
, "isis") == 0)
6061 else if (strcmp(name
, "clnp") == 0)
6064 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6076 gen_joinsp(struct stmt
**s
, int n
)
6082 static struct block
*
6083 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6085 #ifdef NO_PROTOCHAIN
6086 return gen_proto(cstate
, v
, proto
);
6088 struct block
*b0
, *b
;
6089 struct slist
*s
[100];
6090 int fix2
, fix3
, fix4
, fix5
;
6091 int ahcheck
, again
, end
;
6093 int reg2
= alloc_reg(cstate
);
6095 memset(s
, 0, sizeof(s
));
6096 fix3
= fix4
= fix5
= 0;
6103 b0
= gen_protochain(cstate
, v
, Q_IP
);
6104 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6108 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6113 * We don't handle variable-length prefixes before the link-layer
6114 * header, or variable-length link-layer headers, here yet.
6115 * We might want to add BPF instructions to do the protochain
6116 * work, to simplify that and, on platforms that have a BPF
6117 * interpreter with the new instructions, let the filtering
6118 * be done in the kernel. (We already require a modified BPF
6119 * engine to do the protochain stuff, to support backward
6120 * branches, and backward branch support is unlikely to appear
6121 * in kernel BPF engines.)
6123 if (cstate
->off_linkpl
.is_variable
)
6124 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6127 * To quote a comment in optimize.c:
6129 * "These data structures are used in a Cocke and Shwarz style
6130 * value numbering scheme. Since the flowgraph is acyclic,
6131 * exit values can be propagated from a node's predecessors
6132 * provided it is uniquely defined."
6134 * "Acyclic" means "no backward branches", which means "no
6135 * loops", so we have to turn the optimizer off.
6137 cstate
->no_optimize
= 1;
6140 * s[0] is a dummy entry to protect other BPF insn from damage
6141 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6142 * hard to find interdependency made by jump table fixup.
6145 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6150 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6153 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6154 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6156 /* X = ip->ip_hl << 2 */
6157 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6158 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6163 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6165 /* A = ip6->ip_nxt */
6166 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6167 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6169 /* X = sizeof(struct ip6_hdr) */
6170 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6176 bpf_error(cstate
, "unsupported proto to gen_protochain");
6180 /* again: if (A == v) goto end; else fall through; */
6182 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6184 s
[i
]->s
.jt
= NULL
; /*later*/
6185 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6189 #ifndef IPPROTO_NONE
6190 #define IPPROTO_NONE 59
6192 /* if (A == IPPROTO_NONE) goto end */
6193 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6194 s
[i
]->s
.jt
= NULL
; /*later*/
6195 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6196 s
[i
]->s
.k
= IPPROTO_NONE
;
6197 s
[fix5
]->s
.jf
= s
[i
];
6201 if (proto
== Q_IPV6
) {
6202 int v6start
, v6end
, v6advance
, j
;
6205 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6206 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6207 s
[i
]->s
.jt
= NULL
; /*later*/
6208 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6209 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6210 s
[fix2
]->s
.jf
= s
[i
];
6212 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6213 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6214 s
[i
]->s
.jt
= NULL
; /*later*/
6215 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6216 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6218 /* if (A == IPPROTO_ROUTING) goto v6advance */
6219 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6220 s
[i
]->s
.jt
= NULL
; /*later*/
6221 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6222 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6224 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6225 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6226 s
[i
]->s
.jt
= NULL
; /*later*/
6227 s
[i
]->s
.jf
= NULL
; /*later*/
6228 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6238 * A = P[X + packet head];
6239 * X = X + (P[X + packet head + 1] + 1) * 8;
6241 /* A = P[X + packet head] */
6242 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6243 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6246 s
[i
] = new_stmt(cstate
, BPF_ST
);
6249 /* A = P[X + packet head + 1]; */
6250 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6251 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6254 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6258 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6262 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6266 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6269 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6273 /* goto again; (must use BPF_JA for backward jump) */
6274 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6275 s
[i
]->s
.k
= again
- i
- 1;
6276 s
[i
- 1]->s
.jf
= s
[i
];
6280 for (j
= v6start
; j
<= v6end
; j
++)
6281 s
[j
]->s
.jt
= s
[v6advance
];
6284 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6286 s
[fix2
]->s
.jf
= s
[i
];
6292 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6293 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6294 s
[i
]->s
.jt
= NULL
; /*later*/
6295 s
[i
]->s
.jf
= NULL
; /*later*/
6296 s
[i
]->s
.k
= IPPROTO_AH
;
6298 s
[fix3
]->s
.jf
= s
[ahcheck
];
6305 * X = X + (P[X + 1] + 2) * 4;
6308 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6310 /* A = P[X + packet head]; */
6311 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6312 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6315 s
[i
] = new_stmt(cstate
, BPF_ST
);
6319 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6322 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6326 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6328 /* A = P[X + packet head] */
6329 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6330 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6333 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6337 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6341 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6344 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6348 /* goto again; (must use BPF_JA for backward jump) */
6349 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6350 s
[i
]->s
.k
= again
- i
- 1;
6355 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6357 s
[fix2
]->s
.jt
= s
[end
];
6358 s
[fix4
]->s
.jf
= s
[end
];
6359 s
[fix5
]->s
.jt
= s
[end
];
6366 for (i
= 0; i
< max
- 1; i
++)
6367 s
[i
]->next
= s
[i
+ 1];
6368 s
[max
- 1]->next
= NULL
;
6373 b
= new_block(cstate
, JMP(BPF_JEQ
));
6374 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6377 free_reg(cstate
, reg2
);
6384 static struct block
*
6385 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6388 struct block
*b0
, *b1
;
6391 * A data frame has the 0x08 bit (b3) in the frame control field set
6392 * and the 0x04 bit (b2) clear.
6394 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6395 b0
= new_block(cstate
, JMP(BPF_JSET
));
6399 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6400 b1
= new_block(cstate
, JMP(BPF_JSET
));
6411 * Generate code that checks whether the packet is a packet for protocol
6412 * <proto> and whether the type field in that protocol's header has
6413 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6414 * IP packet and checks the protocol number in the IP header against <v>.
6416 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6417 * against Q_IP and Q_IPV6.
6419 static struct block
*
6420 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6422 struct block
*b0
, *b1
;
6427 if (dir
!= Q_DEFAULT
)
6428 bpf_error(cstate
, "direction applied to 'proto'");
6432 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6433 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6438 return gen_linktype(cstate
, v
);
6442 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6443 * not LLC encapsulation with LLCSAP_IP.
6445 * For IEEE 802 networks - which includes 802.5 token ring
6446 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6447 * says that SNAP encapsulation is used, not LLC encapsulation
6450 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6451 * RFC 2225 say that SNAP encapsulation is used, not LLC
6452 * encapsulation with LLCSAP_IP.
6454 * So we always check for ETHERTYPE_IP.
6456 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6458 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6460 b1
= gen_protochain(cstate
, v
, Q_IP
);
6466 bpf_error(cstate
, "arp does not encapsulate another protocol");
6470 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6474 bpf_error(cstate
, "'sctp proto' is bogus");
6478 bpf_error(cstate
, "'tcp proto' is bogus");
6482 bpf_error(cstate
, "'udp proto' is bogus");
6486 bpf_error(cstate
, "'icmp proto' is bogus");
6490 bpf_error(cstate
, "'igmp proto' is bogus");
6494 bpf_error(cstate
, "'igrp proto' is bogus");
6498 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6502 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6506 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6510 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6514 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6518 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6522 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6525 * Also check for a fragment header before the final
6528 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6529 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6531 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6534 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6540 bpf_error(cstate
, "'icmp6 proto' is bogus");
6544 bpf_error(cstate
, "'ah proto' is bogus");
6548 bpf_error(cstate
, "'esp proto' is bogus");
6552 bpf_error(cstate
, "'pim proto' is bogus");
6556 bpf_error(cstate
, "'vrrp proto' is bogus");
6560 bpf_error(cstate
, "'aarp proto' is bogus");
6564 switch (cstate
->linktype
) {
6568 * Frame Relay packets typically have an OSI
6569 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6570 * generates code to check for all the OSI
6571 * NLPIDs, so calling it and then adding a check
6572 * for the particular NLPID for which we're
6573 * looking is bogus, as we can just check for
6576 * What we check for is the NLPID and a frame
6577 * control field value of UI, i.e. 0x03 followed
6580 * XXX - assumes a 2-byte Frame Relay header with
6581 * DLCI and flags. What if the address is longer?
6583 * XXX - what about SNAP-encapsulated frames?
6585 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6591 * Cisco uses an Ethertype lookalike - for OSI,
6594 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6595 /* OSI in C-HDLC is stuffed with a fudge byte */
6596 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6601 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6602 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6608 bpf_error(cstate
, "'esis proto' is bogus");
6612 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6614 * 4 is the offset of the PDU type relative to the IS-IS
6617 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6622 bpf_error(cstate
, "'clnp proto' is not supported");
6626 bpf_error(cstate
, "'stp proto' is bogus");
6630 bpf_error(cstate
, "'ipx proto' is bogus");
6634 bpf_error(cstate
, "'netbeui proto' is bogus");
6638 bpf_error(cstate
, "'l1 proto' is bogus");
6642 bpf_error(cstate
, "'l2 proto' is bogus");
6646 bpf_error(cstate
, "'iih proto' is bogus");
6650 bpf_error(cstate
, "'snp proto' is bogus");
6654 bpf_error(cstate
, "'csnp proto' is bogus");
6658 bpf_error(cstate
, "'psnp proto' is bogus");
6662 bpf_error(cstate
, "'lsp proto' is bogus");
6666 bpf_error(cstate
, "'radio proto' is bogus");
6670 bpf_error(cstate
, "'carp proto' is bogus");
6681 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6683 int proto
= q
.proto
;
6687 bpf_u_int32 mask
, addr
;
6688 struct addrinfo
*res
, *res0
;
6689 struct sockaddr_in
*sin4
;
6692 struct sockaddr_in6
*sin6
;
6693 struct in6_addr mask128
;
6695 struct block
*b
, *tmp
;
6696 int port
, real_proto
;
6700 * Catch errors reported by us and routines below us, and return NULL
6703 if (setjmp(cstate
->top_ctx
))
6709 addr
= pcap_nametonetaddr(name
);
6711 bpf_error(cstate
, "unknown network '%s'", name
);
6712 /* Left justify network addr and calculate its network mask */
6714 while (addr
&& (addr
& 0xff000000) == 0) {
6718 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6722 if (proto
== Q_LINK
) {
6723 switch (cstate
->linktype
) {
6726 case DLT_NETANALYZER
:
6727 case DLT_NETANALYZER_TRANSPARENT
:
6728 eaddr
= pcap_ether_hostton(name
);
6731 "unknown ether host '%s'", name
);
6732 tmp
= gen_prevlinkhdr_check(cstate
);
6733 b
= gen_ehostop(cstate
, eaddr
, dir
);
6740 eaddr
= pcap_ether_hostton(name
);
6743 "unknown FDDI host '%s'", name
);
6744 b
= gen_fhostop(cstate
, eaddr
, dir
);
6749 eaddr
= pcap_ether_hostton(name
);
6752 "unknown token ring host '%s'", name
);
6753 b
= gen_thostop(cstate
, eaddr
, dir
);
6757 case DLT_IEEE802_11
:
6758 case DLT_PRISM_HEADER
:
6759 case DLT_IEEE802_11_RADIO_AVS
:
6760 case DLT_IEEE802_11_RADIO
:
6762 eaddr
= pcap_ether_hostton(name
);
6765 "unknown 802.11 host '%s'", name
);
6766 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6770 case DLT_IP_OVER_FC
:
6771 eaddr
= pcap_ether_hostton(name
);
6774 "unknown Fibre Channel host '%s'", name
);
6775 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6780 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6781 } else if (proto
== Q_DECNET
) {
6782 unsigned short dn_addr
;
6784 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6786 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6788 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6793 * I don't think DECNET hosts can be multihomed, so
6794 * there is no need to build up a list of addresses
6796 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6799 memset(&mask128
, 0xff, sizeof(mask128
));
6801 res0
= res
= pcap_nametoaddrinfo(name
);
6803 bpf_error(cstate
, "unknown host '%s'", name
);
6810 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6811 tproto
== Q_DEFAULT
) {
6817 for (res
= res0
; res
; res
= res
->ai_next
) {
6818 switch (res
->ai_family
) {
6821 if (tproto
== Q_IPV6
)
6825 sin4
= (struct sockaddr_in
*)
6827 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6828 0xffffffff, tproto
, dir
, q
.addr
);
6832 if (tproto6
== Q_IP
)
6835 sin6
= (struct sockaddr_in6
*)
6837 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6838 &mask128
, tproto6
, dir
, q
.addr
);
6851 bpf_error(cstate
, "unknown host '%s'%s", name
,
6852 (proto
== Q_DEFAULT
)
6854 : " for specified address family");
6860 if (proto
!= Q_DEFAULT
&&
6861 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6862 bpf_error(cstate
, "illegal qualifier of 'port'");
6863 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6864 bpf_error(cstate
, "unknown port '%s'", name
);
6865 if (proto
== Q_UDP
) {
6866 if (real_proto
== IPPROTO_TCP
)
6867 bpf_error(cstate
, "port '%s' is tcp", name
);
6868 else if (real_proto
== IPPROTO_SCTP
)
6869 bpf_error(cstate
, "port '%s' is sctp", name
);
6871 /* override PROTO_UNDEF */
6872 real_proto
= IPPROTO_UDP
;
6874 if (proto
== Q_TCP
) {
6875 if (real_proto
== IPPROTO_UDP
)
6876 bpf_error(cstate
, "port '%s' is udp", name
);
6878 else if (real_proto
== IPPROTO_SCTP
)
6879 bpf_error(cstate
, "port '%s' is sctp", name
);
6881 /* override PROTO_UNDEF */
6882 real_proto
= IPPROTO_TCP
;
6884 if (proto
== Q_SCTP
) {
6885 if (real_proto
== IPPROTO_UDP
)
6886 bpf_error(cstate
, "port '%s' is udp", name
);
6888 else if (real_proto
== IPPROTO_TCP
)
6889 bpf_error(cstate
, "port '%s' is tcp", name
);
6891 /* override PROTO_UNDEF */
6892 real_proto
= IPPROTO_SCTP
;
6895 bpf_error(cstate
, "illegal port number %d < 0", port
);
6897 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6898 b
= gen_port(cstate
, port
, real_proto
, dir
);
6899 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6903 if (proto
!= Q_DEFAULT
&&
6904 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6905 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6906 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6907 bpf_error(cstate
, "unknown port in range '%s'", name
);
6908 if (proto
== Q_UDP
) {
6909 if (real_proto
== IPPROTO_TCP
)
6910 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6911 else if (real_proto
== IPPROTO_SCTP
)
6912 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6914 /* override PROTO_UNDEF */
6915 real_proto
= IPPROTO_UDP
;
6917 if (proto
== Q_TCP
) {
6918 if (real_proto
== IPPROTO_UDP
)
6919 bpf_error(cstate
, "port in range '%s' is udp", name
);
6920 else if (real_proto
== IPPROTO_SCTP
)
6921 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6923 /* override PROTO_UNDEF */
6924 real_proto
= IPPROTO_TCP
;
6926 if (proto
== Q_SCTP
) {
6927 if (real_proto
== IPPROTO_UDP
)
6928 bpf_error(cstate
, "port in range '%s' is udp", name
);
6929 else if (real_proto
== IPPROTO_TCP
)
6930 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6932 /* override PROTO_UNDEF */
6933 real_proto
= IPPROTO_SCTP
;
6936 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6938 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6940 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6942 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6944 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6945 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6950 eaddr
= pcap_ether_hostton(name
);
6952 bpf_error(cstate
, "unknown ether host: %s", name
);
6954 res
= pcap_nametoaddrinfo(name
);
6957 bpf_error(cstate
, "unknown host '%s'", name
);
6958 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6962 bpf_error(cstate
, "unknown host '%s'", name
);
6965 bpf_error(cstate
, "'gateway' not supported in this configuration");
6969 real_proto
= lookup_proto(cstate
, name
, proto
);
6970 if (real_proto
>= 0)
6971 return gen_proto(cstate
, real_proto
, proto
, dir
);
6973 bpf_error(cstate
, "unknown protocol: %s", name
);
6976 real_proto
= lookup_proto(cstate
, name
, proto
);
6977 if (real_proto
>= 0)
6978 return gen_protochain(cstate
, real_proto
, proto
);
6980 bpf_error(cstate
, "unknown protocol: %s", name
);
6991 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6992 bpf_u_int32 masklen
, struct qual q
)
6994 register int nlen
, mlen
;
6998 * Catch errors reported by us and routines below us, and return NULL
7001 if (setjmp(cstate
->top_ctx
))
7004 nlen
= __pcap_atoin(s1
, &n
);
7006 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
7007 /* Promote short ipaddr */
7011 mlen
= __pcap_atoin(s2
, &m
);
7013 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
7014 /* Promote short ipaddr */
7017 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7020 /* Convert mask len to mask */
7022 bpf_error(cstate
, "mask length must be <= 32");
7025 * X << 32 is not guaranteed by C to be 0; it's
7030 m
= 0xffffffff << (32 - masklen
);
7032 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7039 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7042 bpf_error(cstate
, "Mask syntax for networks only");
7049 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7057 * Catch errors reported by us and routines below us, and return NULL
7060 if (setjmp(cstate
->top_ctx
))
7067 else if (q
.proto
== Q_DECNET
) {
7068 vlen
= __pcap_atodn(s
, &v
);
7070 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7072 vlen
= __pcap_atoin(s
, &v
);
7074 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7082 if (proto
== Q_DECNET
)
7083 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7084 else if (proto
== Q_LINK
) {
7085 bpf_error(cstate
, "illegal link layer address");
7088 if (s
== NULL
&& q
.addr
== Q_NET
) {
7089 /* Promote short net number */
7090 while (v
&& (v
& 0xff000000) == 0) {
7095 /* Promote short ipaddr */
7097 mask
<<= 32 - vlen
;
7099 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7104 proto
= IPPROTO_UDP
;
7105 else if (proto
== Q_TCP
)
7106 proto
= IPPROTO_TCP
;
7107 else if (proto
== Q_SCTP
)
7108 proto
= IPPROTO_SCTP
;
7109 else if (proto
== Q_DEFAULT
)
7110 proto
= PROTO_UNDEF
;
7112 bpf_error(cstate
, "illegal qualifier of 'port'");
7115 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7119 b
= gen_port(cstate
, v
, proto
, dir
);
7120 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7126 proto
= IPPROTO_UDP
;
7127 else if (proto
== Q_TCP
)
7128 proto
= IPPROTO_TCP
;
7129 else if (proto
== Q_SCTP
)
7130 proto
= IPPROTO_SCTP
;
7131 else if (proto
== Q_DEFAULT
)
7132 proto
= PROTO_UNDEF
;
7134 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7137 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7141 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7142 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7147 bpf_error(cstate
, "'gateway' requires a name");
7151 return gen_proto(cstate
, v
, proto
, dir
);
7154 return gen_protochain(cstate
, v
, proto
);
7169 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7170 bpf_u_int32 masklen
, struct qual q
)
7172 struct addrinfo
*res
;
7173 struct in6_addr
*addr
;
7174 struct in6_addr mask
;
7179 * Catch errors reported by us and routines below us, and return NULL
7182 if (setjmp(cstate
->top_ctx
))
7186 bpf_error(cstate
, "no mask %s supported", s2
);
7188 res
= pcap_nametoaddrinfo(s1
);
7190 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7193 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7194 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7196 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7197 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
.s6_addr
) * 8));
7198 memset(&mask
, 0, sizeof(mask
));
7199 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7201 mask
.s6_addr
[masklen
/ 8] =
7202 (0xff << (8 - masklen
% 8)) & 0xff;
7205 a
= (uint32_t *)addr
;
7206 m
= (uint32_t *)&mask
;
7207 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7208 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7209 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7217 bpf_error(cstate
, "Mask syntax for networks only");
7221 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7227 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7234 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7236 struct block
*b
, *tmp
;
7239 * Catch errors reported by us and routines below us, and return NULL
7242 if (setjmp(cstate
->top_ctx
))
7245 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7246 cstate
->e
= pcap_ether_aton(s
);
7247 if (cstate
->e
== NULL
)
7248 bpf_error(cstate
, "malloc");
7249 switch (cstate
->linktype
) {
7251 case DLT_NETANALYZER
:
7252 case DLT_NETANALYZER_TRANSPARENT
:
7253 tmp
= gen_prevlinkhdr_check(cstate
);
7254 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7259 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7262 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7264 case DLT_IEEE802_11
:
7265 case DLT_PRISM_HEADER
:
7266 case DLT_IEEE802_11_RADIO_AVS
:
7267 case DLT_IEEE802_11_RADIO
:
7269 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7271 case DLT_IP_OVER_FC
:
7272 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7277 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7284 bpf_error(cstate
, "ethernet address used in non-ether expression");
7289 sappend(struct slist
*s0
, struct slist
*s1
)
7292 * This is definitely not the best way to do this, but the
7293 * lists will rarely get long.
7300 static struct slist
*
7301 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7305 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7310 static struct slist
*
7311 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7315 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7321 * Modify "index" to use the value stored into its register as an
7322 * offset relative to the beginning of the header for the protocol
7323 * "proto", and allocate a register and put an item "size" bytes long
7324 * (1, 2, or 4) at that offset into that register, making it the register
7327 static struct arth
*
7328 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7332 struct slist
*s
, *tmp
;
7334 int regno
= alloc_reg(cstate
);
7336 free_reg(cstate
, inst
->regno
);
7340 bpf_error(cstate
, "data size must be 1, 2, or 4");
7357 bpf_error(cstate
, "unsupported index operation");
7361 * The offset is relative to the beginning of the packet
7362 * data, if we have a radio header. (If we don't, this
7365 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7366 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7367 cstate
->linktype
!= DLT_PRISM_HEADER
)
7368 bpf_error(cstate
, "radio information not present in capture");
7371 * Load into the X register the offset computed into the
7372 * register specified by "index".
7374 s
= xfer_to_x(cstate
, inst
);
7377 * Load the item at that offset.
7379 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7381 sappend(inst
->s
, s
);
7386 * The offset is relative to the beginning of
7387 * the link-layer header.
7389 * XXX - what about ATM LANE? Should the index be
7390 * relative to the beginning of the AAL5 frame, so
7391 * that 0 refers to the beginning of the LE Control
7392 * field, or relative to the beginning of the LAN
7393 * frame, so that 0 refers, for Ethernet LANE, to
7394 * the beginning of the destination address?
7396 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7399 * If "s" is non-null, it has code to arrange that the
7400 * X register contains the length of the prefix preceding
7401 * the link-layer header. Add to it the offset computed
7402 * into the register specified by "index", and move that
7403 * into the X register. Otherwise, just load into the X
7404 * register the offset computed into the register specified
7408 sappend(s
, xfer_to_a(cstate
, inst
));
7409 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7410 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7412 s
= xfer_to_x(cstate
, inst
);
7415 * Load the item at the sum of the offset we've put in the
7416 * X register and the offset of the start of the link
7417 * layer header (which is 0 if the radio header is
7418 * variable-length; that header length is what we put
7419 * into the X register and then added to the index).
7421 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7422 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7424 sappend(inst
->s
, s
);
7438 * The offset is relative to the beginning of
7439 * the network-layer header.
7440 * XXX - are there any cases where we want
7441 * cstate->off_nl_nosnap?
7443 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7446 * If "s" is non-null, it has code to arrange that the
7447 * X register contains the variable part of the offset
7448 * of the link-layer payload. Add to it the offset
7449 * computed into the register specified by "index",
7450 * and move that into the X register. Otherwise, just
7451 * load into the X register the offset computed into
7452 * the register specified by "index".
7455 sappend(s
, xfer_to_a(cstate
, inst
));
7456 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7457 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7459 s
= xfer_to_x(cstate
, inst
);
7462 * Load the item at the sum of the offset we've put in the
7463 * X register, the offset of the start of the network
7464 * layer header from the beginning of the link-layer
7465 * payload, and the constant part of the offset of the
7466 * start of the link-layer payload.
7468 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7469 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7471 sappend(inst
->s
, s
);
7474 * Do the computation only if the packet contains
7475 * the protocol in question.
7477 b
= gen_proto_abbrev_internal(cstate
, proto
);
7479 gen_and(inst
->b
, b
);
7493 * The offset is relative to the beginning of
7494 * the transport-layer header.
7496 * Load the X register with the length of the IPv4 header
7497 * (plus the offset of the link-layer header, if it's
7498 * a variable-length header), in bytes.
7500 * XXX - are there any cases where we want
7501 * cstate->off_nl_nosnap?
7502 * XXX - we should, if we're built with
7503 * IPv6 support, generate code to load either
7504 * IPv4, IPv6, or both, as appropriate.
7506 s
= gen_loadx_iphdrlen(cstate
);
7509 * The X register now contains the sum of the variable
7510 * part of the offset of the link-layer payload and the
7511 * length of the network-layer header.
7513 * Load into the A register the offset relative to
7514 * the beginning of the transport layer header,
7515 * add the X register to that, move that to the
7516 * X register, and load with an offset from the
7517 * X register equal to the sum of the constant part of
7518 * the offset of the link-layer payload and the offset,
7519 * relative to the beginning of the link-layer payload,
7520 * of the network-layer header.
7522 sappend(s
, xfer_to_a(cstate
, inst
));
7523 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7524 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7525 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7526 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7527 sappend(inst
->s
, s
);
7530 * Do the computation only if the packet contains
7531 * the protocol in question - which is true only
7532 * if this is an IP datagram and is the first or
7533 * only fragment of that datagram.
7535 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7537 gen_and(inst
->b
, b
);
7538 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7543 * Do the computation only if the packet contains
7544 * the protocol in question.
7546 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7548 gen_and(inst
->b
, b
);
7553 * Check if we have an icmp6 next header
7555 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7557 gen_and(inst
->b
, b
);
7562 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7564 * If "s" is non-null, it has code to arrange that the
7565 * X register contains the variable part of the offset
7566 * of the link-layer payload. Add to it the offset
7567 * computed into the register specified by "index",
7568 * and move that into the X register. Otherwise, just
7569 * load into the X register the offset computed into
7570 * the register specified by "index".
7573 sappend(s
, xfer_to_a(cstate
, inst
));
7574 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7575 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7577 s
= xfer_to_x(cstate
, inst
);
7581 * Load the item at the sum of the offset we've put in the
7582 * X register, the offset of the start of the network
7583 * layer header from the beginning of the link-layer
7584 * payload, and the constant part of the offset of the
7585 * start of the link-layer payload.
7587 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7588 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7591 sappend(inst
->s
, s
);
7595 inst
->regno
= regno
;
7596 s
= new_stmt(cstate
, BPF_ST
);
7598 sappend(inst
->s
, s
);
7604 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7608 * Catch errors reported by us and routines below us, and return NULL
7611 if (setjmp(cstate
->top_ctx
))
7614 return gen_load_internal(cstate
, proto
, inst
, size
);
7617 static struct block
*
7618 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7619 struct arth
*a1
, int reversed
)
7621 struct slist
*s0
, *s1
, *s2
;
7622 struct block
*b
, *tmp
;
7624 s0
= xfer_to_x(cstate
, a1
);
7625 s1
= xfer_to_a(cstate
, a0
);
7626 if (code
== BPF_JEQ
) {
7627 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7628 b
= new_block(cstate
, JMP(code
));
7632 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7638 sappend(a0
->s
, a1
->s
);
7642 free_reg(cstate
, a0
->regno
);
7643 free_reg(cstate
, a1
->regno
);
7645 /* 'and' together protocol checks */
7648 gen_and(a0
->b
, tmp
= a1
->b
);
7662 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7663 struct arth
*a1
, int reversed
)
7666 * Catch errors reported by us and routines below us, and return NULL
7669 if (setjmp(cstate
->top_ctx
))
7672 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7676 gen_loadlen(compiler_state_t
*cstate
)
7683 * Catch errors reported by us and routines below us, and return NULL
7686 if (setjmp(cstate
->top_ctx
))
7689 regno
= alloc_reg(cstate
);
7690 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7691 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7692 s
->next
= new_stmt(cstate
, BPF_ST
);
7693 s
->next
->s
.k
= regno
;
7700 static struct arth
*
7701 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7707 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7709 reg
= alloc_reg(cstate
);
7711 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7713 s
->next
= new_stmt(cstate
, BPF_ST
);
7722 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7725 * Catch errors reported by us and routines below us, and return NULL
7728 if (setjmp(cstate
->top_ctx
))
7731 return gen_loadi_internal(cstate
, val
);
7735 * The a_arg dance is to avoid annoying whining by compilers that
7736 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7737 * It's not *used* after setjmp returns.
7740 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7742 struct arth
*a
= a_arg
;
7746 * Catch errors reported by us and routines below us, and return NULL
7749 if (setjmp(cstate
->top_ctx
))
7752 s
= xfer_to_a(cstate
, a
);
7754 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7757 s
= new_stmt(cstate
, BPF_ST
);
7765 * The a0_arg dance is to avoid annoying whining by compilers that
7766 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7767 * It's not *used* after setjmp returns.
7770 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7773 struct arth
*a0
= a0_arg
;
7774 struct slist
*s0
, *s1
, *s2
;
7777 * Catch errors reported by us and routines below us, and return NULL
7780 if (setjmp(cstate
->top_ctx
))
7784 * Disallow division by, or modulus by, zero; we do this here
7785 * so that it gets done even if the optimizer is disabled.
7787 * Also disallow shifts by a value greater than 31; we do this
7788 * here, for the same reason.
7790 if (code
== BPF_DIV
) {
7791 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7792 bpf_error(cstate
, "division by zero");
7793 } else if (code
== BPF_MOD
) {
7794 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7795 bpf_error(cstate
, "modulus by zero");
7796 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7797 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7798 bpf_error(cstate
, "shift by more than 31 bits");
7800 s0
= xfer_to_x(cstate
, a1
);
7801 s1
= xfer_to_a(cstate
, a0
);
7802 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7807 sappend(a0
->s
, a1
->s
);
7809 free_reg(cstate
, a0
->regno
);
7810 free_reg(cstate
, a1
->regno
);
7812 s0
= new_stmt(cstate
, BPF_ST
);
7813 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7820 * Initialize the table of used registers and the current register.
7823 init_regs(compiler_state_t
*cstate
)
7826 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7830 * Return the next free register.
7833 alloc_reg(compiler_state_t
*cstate
)
7835 int n
= BPF_MEMWORDS
;
7838 if (cstate
->regused
[cstate
->curreg
])
7839 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7841 cstate
->regused
[cstate
->curreg
] = 1;
7842 return cstate
->curreg
;
7845 bpf_error(cstate
, "too many registers needed to evaluate expression");
7850 * Return a register to the table so it can
7854 free_reg(compiler_state_t
*cstate
, int n
)
7856 cstate
->regused
[n
] = 0;
7859 static struct block
*
7860 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7865 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7866 b
= new_block(cstate
, JMP(jmp
));
7874 gen_greater(compiler_state_t
*cstate
, int n
)
7877 * Catch errors reported by us and routines below us, and return NULL
7880 if (setjmp(cstate
->top_ctx
))
7883 return gen_len(cstate
, BPF_JGE
, n
);
7887 * Actually, this is less than or equal.
7890 gen_less(compiler_state_t
*cstate
, int n
)
7895 * Catch errors reported by us and routines below us, and return NULL
7898 if (setjmp(cstate
->top_ctx
))
7901 b
= gen_len(cstate
, BPF_JGT
, n
);
7908 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7909 * the beginning of the link-layer header.
7910 * XXX - that means you can't test values in the radiotap header, but
7911 * as that header is difficult if not impossible to parse generally
7912 * without a loop, that might not be a severe problem. A new keyword
7913 * "radio" could be added for that, although what you'd really want
7914 * would be a way of testing particular radio header values, which
7915 * would generate code appropriate to the radio header in question.
7918 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7924 * Catch errors reported by us and routines below us, and return NULL
7927 if (setjmp(cstate
->top_ctx
))
7935 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7938 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7942 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7946 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7950 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7954 b
= new_block(cstate
, JMP(BPF_JEQ
));
7961 static const u_char abroadcast
[] = { 0x0 };
7964 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7966 bpf_u_int32 hostmask
;
7967 struct block
*b0
, *b1
, *b2
;
7968 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7971 * Catch errors reported by us and routines below us, and return NULL
7974 if (setjmp(cstate
->top_ctx
))
7981 switch (cstate
->linktype
) {
7983 case DLT_ARCNET_LINUX
:
7984 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7986 case DLT_NETANALYZER
:
7987 case DLT_NETANALYZER_TRANSPARENT
:
7988 b1
= gen_prevlinkhdr_check(cstate
);
7989 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7994 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7996 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7997 case DLT_IEEE802_11
:
7998 case DLT_PRISM_HEADER
:
7999 case DLT_IEEE802_11_RADIO_AVS
:
8000 case DLT_IEEE802_11_RADIO
:
8002 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
8003 case DLT_IP_OVER_FC
:
8004 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
8006 bpf_error(cstate
, "not a broadcast link");
8012 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
8013 * as an indication that we don't know the netmask, and fail
8016 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
8017 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
8018 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8019 hostmask
= ~cstate
->netmask
;
8020 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8021 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8022 ~0 & hostmask
, hostmask
);
8027 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8032 * Generate code to test the low-order bit of a MAC address (that's
8033 * the bottom bit of the *first* byte).
8035 static struct block
*
8036 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8038 register struct block
*b0
;
8039 register struct slist
*s
;
8041 /* link[offset] & 1 != 0 */
8042 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8043 b0
= new_block(cstate
, JMP(BPF_JSET
));
8050 gen_multicast(compiler_state_t
*cstate
, int proto
)
8052 register struct block
*b0
, *b1
, *b2
;
8053 register struct slist
*s
;
8056 * Catch errors reported by us and routines below us, and return NULL
8059 if (setjmp(cstate
->top_ctx
))
8066 switch (cstate
->linktype
) {
8068 case DLT_ARCNET_LINUX
:
8069 /* all ARCnet multicasts use the same address */
8070 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8072 case DLT_NETANALYZER
:
8073 case DLT_NETANALYZER_TRANSPARENT
:
8074 b1
= gen_prevlinkhdr_check(cstate
);
8075 /* ether[0] & 1 != 0 */
8076 b0
= gen_mac_multicast(cstate
, 0);
8082 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8084 * XXX - was that referring to bit-order issues?
8086 /* fddi[1] & 1 != 0 */
8087 return gen_mac_multicast(cstate
, 1);
8089 /* tr[2] & 1 != 0 */
8090 return gen_mac_multicast(cstate
, 2);
8091 case DLT_IEEE802_11
:
8092 case DLT_PRISM_HEADER
:
8093 case DLT_IEEE802_11_RADIO_AVS
:
8094 case DLT_IEEE802_11_RADIO
:
8099 * For control frames, there is no DA.
8101 * For management frames, DA is at an
8102 * offset of 4 from the beginning of
8105 * For data frames, DA is at an offset
8106 * of 4 from the beginning of the packet
8107 * if To DS is clear and at an offset of
8108 * 16 from the beginning of the packet
8113 * Generate the tests to be done for data frames.
8115 * First, check for To DS set, i.e. "link[1] & 0x01".
8117 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8118 b1
= new_block(cstate
, JMP(BPF_JSET
));
8119 b1
->s
.k
= 0x01; /* To DS */
8123 * If To DS is set, the DA is at 16.
8125 b0
= gen_mac_multicast(cstate
, 16);
8129 * Now, check for To DS not set, i.e. check
8130 * "!(link[1] & 0x01)".
8132 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8133 b2
= new_block(cstate
, JMP(BPF_JSET
));
8134 b2
->s
.k
= 0x01; /* To DS */
8139 * If To DS is not set, the DA is at 4.
8141 b1
= gen_mac_multicast(cstate
, 4);
8145 * Now OR together the last two checks. That gives
8146 * the complete set of checks for data frames.
8151 * Now check for a data frame.
8152 * I.e, check "link[0] & 0x08".
8154 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8155 b1
= new_block(cstate
, JMP(BPF_JSET
));
8160 * AND that with the checks done for data frames.
8165 * If the high-order bit of the type value is 0, this
8166 * is a management frame.
8167 * I.e, check "!(link[0] & 0x08)".
8169 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8170 b2
= new_block(cstate
, JMP(BPF_JSET
));
8176 * For management frames, the DA is at 4.
8178 b1
= gen_mac_multicast(cstate
, 4);
8182 * OR that with the checks done for data frames.
8183 * That gives the checks done for management and
8189 * If the low-order bit of the type value is 1,
8190 * this is either a control frame or a frame
8191 * with a reserved type, and thus not a
8194 * I.e., check "!(link[0] & 0x04)".
8196 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8197 b1
= new_block(cstate
, JMP(BPF_JSET
));
8203 * AND that with the checks for data and management
8208 case DLT_IP_OVER_FC
:
8209 b0
= gen_mac_multicast(cstate
, 2);
8214 /* Link not known to support multicasts */
8218 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8219 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8224 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8225 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8229 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8234 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8236 register struct block
*b0
;
8239 * Catch errors reported by us and routines below us, and return NULL
8242 if (setjmp(cstate
->top_ctx
))
8246 * Only some data link types support ifindex qualifiers.
8248 switch (cstate
->linktype
) {
8249 case DLT_LINUX_SLL2
:
8250 /* match packets on this interface */
8251 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8256 * This is Linux; we require PF_PACKET support.
8257 * If this is a *live* capture, we can look at
8258 * special meta-data in the filter expression;
8259 * if it's a savefile, we can't.
8261 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8262 /* We have a FILE *, so this is a savefile */
8263 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8264 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8269 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8271 #else /* defined(linux) */
8272 bpf_error(cstate
, "ifindex not supported on %s",
8273 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8275 #endif /* defined(linux) */
8281 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8282 * Outbound traffic is sent by this machine, while inbound traffic is
8283 * sent by a remote machine (and may include packets destined for a
8284 * unicast or multicast link-layer address we are not subscribing to).
8285 * These are the same definitions implemented by pcap_setdirection().
8286 * Capturing only unicast traffic destined for this host is probably
8287 * better accomplished using a higher-layer filter.
8290 gen_inbound(compiler_state_t
*cstate
, int dir
)
8292 register struct block
*b0
;
8295 * Catch errors reported by us and routines below us, and return NULL
8298 if (setjmp(cstate
->top_ctx
))
8302 * Only some data link types support inbound/outbound qualifiers.
8304 switch (cstate
->linktype
) {
8306 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8307 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8308 gen_loadi_internal(cstate
, 0),
8314 /* match outgoing packets */
8315 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8317 /* match incoming packets */
8318 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8323 /* match outgoing packets */
8324 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8326 /* to filter on inbound traffic, invert the match */
8331 case DLT_LINUX_SLL2
:
8332 /* match outgoing packets */
8333 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8335 /* to filter on inbound traffic, invert the match */
8341 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8342 ((dir
== 0) ? PF_IN
: PF_OUT
));
8347 /* match outgoing packets */
8348 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8350 /* match incoming packets */
8351 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8355 case DLT_JUNIPER_MFR
:
8356 case DLT_JUNIPER_MLFR
:
8357 case DLT_JUNIPER_MLPPP
:
8358 case DLT_JUNIPER_ATM1
:
8359 case DLT_JUNIPER_ATM2
:
8360 case DLT_JUNIPER_PPPOE
:
8361 case DLT_JUNIPER_PPPOE_ATM
:
8362 case DLT_JUNIPER_GGSN
:
8363 case DLT_JUNIPER_ES
:
8364 case DLT_JUNIPER_MONITOR
:
8365 case DLT_JUNIPER_SERVICES
:
8366 case DLT_JUNIPER_ETHER
:
8367 case DLT_JUNIPER_PPP
:
8368 case DLT_JUNIPER_FRELAY
:
8369 case DLT_JUNIPER_CHDLC
:
8370 case DLT_JUNIPER_VP
:
8371 case DLT_JUNIPER_ST
:
8372 case DLT_JUNIPER_ISM
:
8373 case DLT_JUNIPER_VS
:
8374 case DLT_JUNIPER_SRX_E2E
:
8375 case DLT_JUNIPER_FIBRECHANNEL
:
8376 case DLT_JUNIPER_ATM_CEMIC
:
8378 /* juniper flags (including direction) are stored
8379 * the byte after the 3-byte magic number */
8381 /* match outgoing packets */
8382 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8384 /* match incoming packets */
8385 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8391 * If we have packet meta-data indicating a direction,
8392 * and that metadata can be checked by BPF code, check
8393 * it. Otherwise, give up, as this link-layer type has
8394 * nothing in the packet data.
8396 * Currently, the only platform where a BPF filter can
8397 * check that metadata is Linux with the in-kernel
8398 * BPF interpreter. If other packet capture mechanisms
8399 * and BPF filters also supported this, it would be
8400 * nice. It would be even better if they made that
8401 * metadata available so that we could provide it
8402 * with newer capture APIs, allowing it to be saved
8407 * This is Linux; we require PF_PACKET support.
8408 * If this is a *live* capture, we can look at
8409 * special meta-data in the filter expression;
8410 * if it's a savefile, we can't.
8412 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8413 /* We have a FILE *, so this is a savefile */
8414 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8415 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8418 /* match outgoing packets */
8419 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8422 /* to filter on inbound traffic, invert the match */
8425 #else /* defined(linux) */
8426 bpf_error(cstate
, "inbound/outbound not supported on %s",
8427 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8429 #endif /* defined(linux) */
8434 /* PF firewall log matched interface */
8436 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8442 * Catch errors reported by us and routines below us, and return NULL
8445 if (setjmp(cstate
->top_ctx
))
8448 if (cstate
->linktype
!= DLT_PFLOG
) {
8449 bpf_error(cstate
, "ifname supported only on PF linktype");
8452 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8453 off
= offsetof(struct pfloghdr
, ifname
);
8454 if (strlen(ifname
) >= len
) {
8455 bpf_error(cstate
, "ifname interface names can only be %d characters",
8459 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8460 (const u_char
*)ifname
);
8464 /* PF firewall log ruleset name */
8466 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8471 * Catch errors reported by us and routines below us, and return NULL
8474 if (setjmp(cstate
->top_ctx
))
8477 if (cstate
->linktype
!= DLT_PFLOG
) {
8478 bpf_error(cstate
, "ruleset supported only on PF linktype");
8482 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8483 bpf_error(cstate
, "ruleset names can only be %ld characters",
8484 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8488 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8489 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8493 /* PF firewall log rule number */
8495 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8500 * Catch errors reported by us and routines below us, and return NULL
8503 if (setjmp(cstate
->top_ctx
))
8506 if (cstate
->linktype
!= DLT_PFLOG
) {
8507 bpf_error(cstate
, "rnr supported only on PF linktype");
8511 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8516 /* PF firewall log sub-rule number */
8518 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8523 * Catch errors reported by us and routines below us, and return NULL
8526 if (setjmp(cstate
->top_ctx
))
8529 if (cstate
->linktype
!= DLT_PFLOG
) {
8530 bpf_error(cstate
, "srnr supported only on PF linktype");
8534 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8539 /* PF firewall log reason code */
8541 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8546 * Catch errors reported by us and routines below us, and return NULL
8549 if (setjmp(cstate
->top_ctx
))
8552 if (cstate
->linktype
!= DLT_PFLOG
) {
8553 bpf_error(cstate
, "reason supported only on PF linktype");
8557 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8558 (bpf_u_int32
)reason
);
8562 /* PF firewall log action */
8564 gen_pf_action(compiler_state_t
*cstate
, int action
)
8569 * Catch errors reported by us and routines below us, and return NULL
8572 if (setjmp(cstate
->top_ctx
))
8575 if (cstate
->linktype
!= DLT_PFLOG
) {
8576 bpf_error(cstate
, "action supported only on PF linktype");
8580 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8581 (bpf_u_int32
)action
);
8585 /* IEEE 802.11 wireless header */
8587 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8592 * Catch errors reported by us and routines below us, and return NULL
8595 if (setjmp(cstate
->top_ctx
))
8598 switch (cstate
->linktype
) {
8600 case DLT_IEEE802_11
:
8601 case DLT_PRISM_HEADER
:
8602 case DLT_IEEE802_11_RADIO_AVS
:
8603 case DLT_IEEE802_11_RADIO
:
8604 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8608 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8616 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8621 * Catch errors reported by us and routines below us, and return NULL
8624 if (setjmp(cstate
->top_ctx
))
8627 switch (cstate
->linktype
) {
8629 case DLT_IEEE802_11
:
8630 case DLT_PRISM_HEADER
:
8631 case DLT_IEEE802_11_RADIO_AVS
:
8632 case DLT_IEEE802_11_RADIO
:
8636 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8640 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8641 IEEE80211_FC1_DIR_MASK
);
8647 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8652 * Catch errors reported by us and routines below us, and return NULL
8655 if (setjmp(cstate
->top_ctx
))
8658 switch (cstate
->linktype
) {
8661 case DLT_ARCNET_LINUX
:
8662 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8663 q
.proto
== Q_LINK
) {
8664 cstate
->e
= pcap_ether_aton(s
);
8665 if (cstate
->e
== NULL
)
8666 bpf_error(cstate
, "malloc");
8667 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8672 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8676 bpf_error(cstate
, "aid supported only on ARCnet");
8681 static struct block
*
8682 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8684 register struct block
*b0
, *b1
;
8687 /* src comes first, different from Ethernet */
8689 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8692 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8695 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8696 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8702 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8703 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8708 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8712 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8716 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8720 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8724 bpf_error(cstate
, "'ra' is only supported on 802.11");
8728 bpf_error(cstate
, "'ta' is only supported on 802.11");
8735 static struct block
*
8736 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8738 struct block
*b0
, *b1
;
8740 /* check for VLAN, including QinQ */
8741 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8742 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8745 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8751 static struct block
*
8752 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8754 if (vlan_num
> 0x0fff) {
8755 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8758 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8761 static struct block
*
8762 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8765 struct block
*b0
, *b1
;
8767 b0
= gen_vlan_tpid_test(cstate
);
8770 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8776 * Both payload and link header type follow the VLAN tags so that
8777 * both need to be updated.
8779 cstate
->off_linkpl
.constant_part
+= 4;
8780 cstate
->off_linktype
.constant_part
+= 4;
8785 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8786 /* add v to variable part of off */
8788 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8789 bpf_u_int32 v
, struct slist
*s
)
8793 if (!off
->is_variable
)
8794 off
->is_variable
= 1;
8796 off
->reg
= alloc_reg(cstate
);
8798 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8801 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8804 s2
= new_stmt(cstate
, BPF_ST
);
8810 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8811 * and link type offsets first
8814 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8818 /* offset determined at run time, shift variable part */
8820 cstate
->is_vlan_vloffset
= 1;
8821 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8822 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8824 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8825 sappend(s
.next
, b_tpid
->head
->stmts
);
8826 b_tpid
->head
->stmts
= s
.next
;
8830 * patch block b_vid (VLAN id test) to load VID value either from packet
8831 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8834 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8836 struct slist
*s
, *s2
, *sjeq
;
8839 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8840 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8842 /* true -> next instructions, false -> beginning of b_vid */
8843 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8845 sjeq
->s
.jf
= b_vid
->stmts
;
8848 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8849 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8853 /* Jump to the test in b_vid. We need to jump one instruction before
8854 * the end of the b_vid block so that we only skip loading the TCI
8855 * from packet data and not the 'and' instruction extractging VID.
8858 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8860 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8864 /* insert our statements at the beginning of b_vid */
8865 sappend(s
, b_vid
->stmts
);
8870 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8871 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8872 * tag can be either in metadata or in packet data; therefore if the
8873 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8874 * header for VLAN tag. As the decision is done at run time, we need
8875 * update variable part of the offsets
8877 static struct block
*
8878 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8881 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8884 /* generate new filter code based on extracting packet
8886 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8887 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8889 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8894 * This is tricky. We need to insert the statements updating variable
8895 * parts of offsets before the traditional TPID and VID tests so
8896 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8897 * we do not want this update to affect those checks. That's why we
8898 * generate both test blocks first and insert the statements updating
8899 * variable parts of both offsets after that. This wouldn't work if
8900 * there already were variable length link header when entering this
8901 * function but gen_vlan_bpf_extensions() isn't called in that case.
8903 b_tpid
= gen_vlan_tpid_test(cstate
);
8905 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8907 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8912 gen_vlan_patch_vid_test(cstate
, b_vid
);
8922 * support IEEE 802.1Q VLAN trunk over ethernet
8925 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8930 * Catch errors reported by us and routines below us, and return NULL
8933 if (setjmp(cstate
->top_ctx
))
8936 /* can't check for VLAN-encapsulated packets inside MPLS */
8937 if (cstate
->label_stack_depth
> 0)
8938 bpf_error(cstate
, "no VLAN match after MPLS");
8941 * Check for a VLAN packet, and then change the offsets to point
8942 * to the type and data fields within the VLAN packet. Just
8943 * increment the offsets, so that we can support a hierarchy, e.g.
8944 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8947 * XXX - this is a bit of a kludge. If we were to split the
8948 * compiler into a parser that parses an expression and
8949 * generates an expression tree, and a code generator that
8950 * takes an expression tree (which could come from our
8951 * parser or from some other parser) and generates BPF code,
8952 * we could perhaps make the offsets parameters of routines
8953 * and, in the handler for an "AND" node, pass to subnodes
8954 * other than the VLAN node the adjusted offsets.
8956 * This would mean that "vlan" would, instead of changing the
8957 * behavior of *all* tests after it, change only the behavior
8958 * of tests ANDed with it. That would change the documented
8959 * semantics of "vlan", which might break some expressions.
8960 * However, it would mean that "(vlan and ip) or ip" would check
8961 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8962 * checking only for VLAN-encapsulated IP, so that could still
8963 * be considered worth doing; it wouldn't break expressions
8964 * that are of the form "vlan and ..." or "vlan N and ...",
8965 * which I suspect are the most common expressions involving
8966 * "vlan". "vlan or ..." doesn't necessarily do what the user
8967 * would really want, now, as all the "or ..." tests would
8968 * be done assuming a VLAN, even though the "or" could be viewed
8969 * as meaning "or, if this isn't a VLAN packet...".
8971 switch (cstate
->linktype
) {
8974 case DLT_NETANALYZER
:
8975 case DLT_NETANALYZER_TRANSPARENT
:
8976 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8977 /* Verify that this is the outer part of the packet and
8978 * not encapsulated somehow. */
8979 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8980 cstate
->off_linkhdr
.constant_part
==
8981 cstate
->off_outermostlinkhdr
.constant_part
) {
8983 * Do we need special VLAN handling?
8985 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8986 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8989 b0
= gen_vlan_no_bpf_extensions(cstate
,
8990 vlan_num
, has_vlan_tag
);
8993 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8997 case DLT_IEEE802_11
:
8998 case DLT_PRISM_HEADER
:
8999 case DLT_IEEE802_11_RADIO_AVS
:
9000 case DLT_IEEE802_11_RADIO
:
9001 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9005 bpf_error(cstate
, "no VLAN support for %s",
9006 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9010 cstate
->vlan_stack_depth
++;
9018 * The label_num_arg dance is to avoid annoying whining by compilers that
9019 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9020 * It's not *used* after setjmp returns.
9023 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9026 volatile bpf_u_int32 label_num
= label_num_arg
;
9027 struct block
*b0
, *b1
;
9030 * Catch errors reported by us and routines below us, and return NULL
9033 if (setjmp(cstate
->top_ctx
))
9036 if (cstate
->label_stack_depth
> 0) {
9037 /* just match the bottom-of-stack bit clear */
9038 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9041 * We're not in an MPLS stack yet, so check the link-layer
9042 * type against MPLS.
9044 switch (cstate
->linktype
) {
9046 case DLT_C_HDLC
: /* fall through */
9049 case DLT_NETANALYZER
:
9050 case DLT_NETANALYZER_TRANSPARENT
:
9051 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9055 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9058 /* FIXME add other DLT_s ...
9059 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9060 * leave it for now */
9063 bpf_error(cstate
, "no MPLS support for %s",
9064 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9069 /* If a specific MPLS label is requested, check it */
9070 if (has_label_num
) {
9071 if (label_num
> 0xFFFFF) {
9072 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9073 label_num
, 0xFFFFF);
9075 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9076 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9077 0xfffff000); /* only compare the first 20 bits */
9083 * Change the offsets to point to the type and data fields within
9084 * the MPLS packet. Just increment the offsets, so that we
9085 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9086 * capture packets with an outer label of 100000 and an inner
9089 * Increment the MPLS stack depth as well; this indicates that
9090 * we're checking MPLS-encapsulated headers, to make sure higher
9091 * level code generators don't try to match against IP-related
9092 * protocols such as Q_ARP, Q_RARP etc.
9094 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9096 cstate
->off_nl_nosnap
+= 4;
9097 cstate
->off_nl
+= 4;
9098 cstate
->label_stack_depth
++;
9103 * Support PPPOE discovery and session.
9106 gen_pppoed(compiler_state_t
*cstate
)
9109 * Catch errors reported by us and routines below us, and return NULL
9112 if (setjmp(cstate
->top_ctx
))
9115 /* check for PPPoE discovery */
9116 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9120 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9122 struct block
*b0
, *b1
;
9125 * Catch errors reported by us and routines below us, and return NULL
9128 if (setjmp(cstate
->top_ctx
))
9132 * Test against the PPPoE session link-layer type.
9134 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9136 /* If a specific session is requested, check PPPoE session id */
9138 if (sess_num
> 0x0000ffff) {
9139 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9140 sess_num
, 0x0000ffff);
9142 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9148 * Change the offsets to point to the type and data fields within
9149 * the PPP packet, and note that this is PPPoE rather than
9152 * XXX - this is a bit of a kludge. See the comments in
9155 * The "network-layer" protocol is PPPoE, which has a 6-byte
9156 * PPPoE header, followed by a PPP packet.
9158 * There is no HDLC encapsulation for the PPP packet (it's
9159 * encapsulated in PPPoES instead), so the link-layer type
9160 * starts at the first byte of the PPP packet. For PPPoE,
9161 * that offset is relative to the beginning of the total
9162 * link-layer payload, including any 802.2 LLC header, so
9163 * it's 6 bytes past cstate->off_nl.
9165 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9166 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9167 cstate
->off_linkpl
.reg
);
9169 cstate
->off_linktype
= cstate
->off_linkhdr
;
9170 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9173 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9178 /* Check that this is Geneve and the VNI is correct if
9179 * specified. Parameterized to handle both IPv4 and IPv6. */
9180 static struct block
*
9181 gen_geneve_check(compiler_state_t
*cstate
,
9182 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9183 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9185 struct block
*b0
, *b1
;
9187 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9189 /* Check that we are operating on version 0. Otherwise, we
9190 * can't decode the rest of the fields. The version is 2 bits
9191 * in the first byte of the Geneve header. */
9192 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9197 if (vni
> 0xffffff) {
9198 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9201 vni
<<= 8; /* VNI is in the upper 3 bytes */
9202 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9210 /* The IPv4 and IPv6 Geneve checks need to do two things:
9211 * - Verify that this actually is Geneve with the right VNI.
9212 * - Place the IP header length (plus variable link prefix if
9213 * needed) into register A to be used later to compute
9214 * the inner packet offsets. */
9215 static struct block
*
9216 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9218 struct block
*b0
, *b1
;
9219 struct slist
*s
, *s1
;
9221 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9223 /* Load the IP header length into A. */
9224 s
= gen_loadx_iphdrlen(cstate
);
9226 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9229 /* Forcibly append these statements to the true condition
9230 * of the protocol check by creating a new block that is
9231 * always true and ANDing them. */
9232 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9241 static struct block
*
9242 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9244 struct block
*b0
, *b1
;
9245 struct slist
*s
, *s1
;
9247 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9249 /* Load the IP header length. We need to account for a
9250 * variable length link prefix if there is one. */
9251 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9253 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9257 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9261 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9265 /* Forcibly append these statements to the true condition
9266 * of the protocol check by creating a new block that is
9267 * always true and ANDing them. */
9268 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9271 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9280 /* We need to store three values based on the Geneve header::
9281 * - The offset of the linktype.
9282 * - The offset of the end of the Geneve header.
9283 * - The offset of the end of the encapsulated MAC header. */
9284 static struct slist
*
9285 gen_geneve_offsets(compiler_state_t
*cstate
)
9287 struct slist
*s
, *s1
, *s_proto
;
9289 /* First we need to calculate the offset of the Geneve header
9290 * itself. This is composed of the IP header previously calculated
9291 * (include any variable link prefix) and stored in A plus the
9292 * fixed sized headers (fixed link prefix, MAC length, and UDP
9294 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9295 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9297 /* Stash this in X since we'll need it later. */
9298 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9301 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9303 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9307 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9308 cstate
->off_linktype
.is_variable
= 1;
9309 cstate
->off_linktype
.constant_part
= 0;
9311 s1
= new_stmt(cstate
, BPF_ST
);
9312 s1
->s
.k
= cstate
->off_linktype
.reg
;
9315 /* Load the Geneve option length and mask and shift to get the
9316 * number of bytes. It is stored in the first byte of the Geneve
9318 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9322 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9326 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9330 /* Add in the rest of the Geneve base header. */
9331 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9335 /* Add the Geneve header length to its offset and store. */
9336 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9340 /* Set the encapsulated type as Ethernet. Even though we may
9341 * not actually have Ethernet inside there are two reasons this
9343 * - The linktype field is always in EtherType format regardless
9344 * of whether it is in Geneve or an inner Ethernet frame.
9345 * - The only link layer that we have specific support for is
9346 * Ethernet. We will confirm that the packet actually is
9347 * Ethernet at runtime before executing these checks. */
9348 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9350 s1
= new_stmt(cstate
, BPF_ST
);
9351 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9354 /* Calculate whether we have an Ethernet header or just raw IP/
9355 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9356 * and linktype by 14 bytes so that the network header can be found
9357 * seamlessly. Otherwise, keep what we've calculated already. */
9359 /* We have a bare jmp so we can't use the optimizer. */
9360 cstate
->no_optimize
= 1;
9362 /* Load the EtherType in the Geneve header, 2 bytes in. */
9363 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9367 /* Load X with the end of the Geneve header. */
9368 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9369 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9372 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9373 * end of this check, we should have the total length in X. In
9374 * the non-Ethernet case, it's already there. */
9375 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9376 s_proto
->s
.k
= ETHERTYPE_TEB
;
9377 sappend(s
, s_proto
);
9379 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9383 /* Since this is Ethernet, use the EtherType of the payload
9384 * directly as the linktype. Overwrite what we already have. */
9385 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9389 s1
= new_stmt(cstate
, BPF_ST
);
9390 s1
->s
.k
= cstate
->off_linktype
.reg
;
9393 /* Advance two bytes further to get the end of the Ethernet
9395 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9399 /* Move the result to X. */
9400 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9403 /* Store the final result of our linkpl calculation. */
9404 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9405 cstate
->off_linkpl
.is_variable
= 1;
9406 cstate
->off_linkpl
.constant_part
= 0;
9408 s1
= new_stmt(cstate
, BPF_STX
);
9409 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9418 /* Check to see if this is a Geneve packet. */
9420 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9422 struct block
*b0
, *b1
;
9426 * Catch errors reported by us and routines below us, and return NULL
9429 if (setjmp(cstate
->top_ctx
))
9432 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9433 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9438 /* Later filters should act on the payload of the Geneve frame,
9439 * update all of the header pointers. Attach this code so that
9440 * it gets executed in the event that the Geneve filter matches. */
9441 s
= gen_geneve_offsets(cstate
);
9443 b1
= gen_true(cstate
);
9444 sappend(s
, b1
->stmts
);
9449 cstate
->is_geneve
= 1;
9454 /* Check that the encapsulated frame has a link layer header
9455 * for Ethernet filters. */
9456 static struct block
*
9457 gen_geneve_ll_check(compiler_state_t
*cstate
)
9460 struct slist
*s
, *s1
;
9462 /* The easiest way to see if there is a link layer present
9463 * is to check if the link layer header and payload are not
9466 /* Geneve always generates pure variable offsets so we can
9467 * compare only the registers. */
9468 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9469 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9471 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9472 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9475 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9483 static struct block
*
9484 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9485 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9492 if (!cstate
->is_atm
)
9493 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9494 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9496 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9497 0xffffffffU
, jtype
, reverse
, jvalue
);
9501 if (!cstate
->is_atm
)
9502 bpf_error(cstate
, "'vci' supported only on raw ATM");
9503 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9505 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9506 0xffffffffU
, jtype
, reverse
, jvalue
);
9510 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9511 abort(); /* XXX - this isn't on FreeBSD */
9512 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9513 0x0fU
, jtype
, reverse
, jvalue
);
9517 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9519 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9520 0xffffffffU
, jtype
, reverse
, jvalue
);
9524 if (!cstate
->is_atm
)
9525 bpf_error(cstate
, "'callref' supported only on raw ATM");
9526 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9528 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9529 0xffffffffU
, jtype
, reverse
, jvalue
);
9538 static struct block
*
9539 gen_atmtype_metac(compiler_state_t
*cstate
)
9541 struct block
*b0
, *b1
;
9543 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9544 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9549 static struct block
*
9550 gen_atmtype_sc(compiler_state_t
*cstate
)
9552 struct block
*b0
, *b1
;
9554 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9555 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9560 static struct block
*
9561 gen_atmtype_llc(compiler_state_t
*cstate
)
9565 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9566 cstate
->linktype
= cstate
->prevlinktype
;
9571 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9572 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9575 * Catch errors reported by us and routines below us, and return NULL
9578 if (setjmp(cstate
->top_ctx
))
9581 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9586 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9588 struct block
*b0
, *b1
;
9591 * Catch errors reported by us and routines below us, and return NULL
9594 if (setjmp(cstate
->top_ctx
))
9600 /* Get all packets in Meta signalling Circuit */
9601 if (!cstate
->is_atm
)
9602 bpf_error(cstate
, "'metac' supported only on raw ATM");
9603 b1
= gen_atmtype_metac(cstate
);
9607 /* Get all packets in Broadcast Circuit*/
9608 if (!cstate
->is_atm
)
9609 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9610 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9611 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9616 /* Get all cells in Segment OAM F4 circuit*/
9617 if (!cstate
->is_atm
)
9618 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9619 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9620 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9625 /* Get all cells in End-to-End OAM F4 Circuit*/
9626 if (!cstate
->is_atm
)
9627 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9628 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9629 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9634 /* Get all packets in connection Signalling Circuit */
9635 if (!cstate
->is_atm
)
9636 bpf_error(cstate
, "'sc' supported only on raw ATM");
9637 b1
= gen_atmtype_sc(cstate
);
9641 /* Get all packets in ILMI Circuit */
9642 if (!cstate
->is_atm
)
9643 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9644 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9645 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9650 /* Get all LANE packets */
9651 if (!cstate
->is_atm
)
9652 bpf_error(cstate
, "'lane' supported only on raw ATM");
9653 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9656 * Arrange that all subsequent tests assume LANE
9657 * rather than LLC-encapsulated packets, and set
9658 * the offsets appropriately for LANE-encapsulated
9661 * We assume LANE means Ethernet, not Token Ring.
9663 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9664 cstate
->off_payload
+ 2, /* Ethernet header */
9666 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9667 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9668 cstate
->off_nl
= 0; /* Ethernet II */
9669 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9673 /* Get all LLC-encapsulated packets */
9674 if (!cstate
->is_atm
)
9675 bpf_error(cstate
, "'llc' supported only on raw ATM");
9676 b1
= gen_atmtype_llc(cstate
);
9686 * Filtering for MTP2 messages based on li value
9687 * FISU, length is null
9688 * LSSU, length is 1 or 2
9689 * MSU, length is 3 or more
9690 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9693 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9695 struct block
*b0
, *b1
;
9698 * Catch errors reported by us and routines below us, and return NULL
9701 if (setjmp(cstate
->top_ctx
))
9707 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9708 (cstate
->linktype
!= DLT_ERF
) &&
9709 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9710 bpf_error(cstate
, "'fisu' supported only on MTP2");
9711 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9712 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9713 0x3fU
, BPF_JEQ
, 0, 0U);
9717 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9718 (cstate
->linktype
!= DLT_ERF
) &&
9719 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9720 bpf_error(cstate
, "'lssu' supported only on MTP2");
9721 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9722 0x3fU
, BPF_JGT
, 1, 2U);
9723 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9724 0x3fU
, BPF_JGT
, 0, 0U);
9729 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9730 (cstate
->linktype
!= DLT_ERF
) &&
9731 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9732 bpf_error(cstate
, "'msu' supported only on MTP2");
9733 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9734 0x3fU
, BPF_JGT
, 0, 2U);
9738 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9739 (cstate
->linktype
!= DLT_ERF
) &&
9740 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9741 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9742 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9743 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9744 0xff80U
, BPF_JEQ
, 0, 0U);
9748 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9749 (cstate
->linktype
!= DLT_ERF
) &&
9750 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9751 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9752 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9753 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9754 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9755 0xff80U
, BPF_JGT
, 0, 0U);
9760 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9761 (cstate
->linktype
!= DLT_ERF
) &&
9762 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9763 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9764 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9765 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9775 * The jvalue_arg dance is to avoid annoying whining by compilers that
9776 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9777 * It's not *used* after setjmp returns.
9780 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9781 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
9783 volatile bpf_u_int32 jvalue
= jvalue_arg
;
9785 bpf_u_int32 val1
, val2
, val3
;
9792 * Catch errors reported by us and routines below us, and return NULL
9795 if (setjmp(cstate
->top_ctx
))
9798 newoff_sio
= cstate
->off_sio
;
9799 newoff_opc
= cstate
->off_opc
;
9800 newoff_dpc
= cstate
->off_dpc
;
9801 newoff_sls
= cstate
->off_sls
;
9802 switch (mtp3field
) {
9805 newoff_sio
+= 3; /* offset for MTP2_HSL */
9809 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9810 bpf_error(cstate
, "'sio' supported only on SS7");
9811 /* sio coded on 1 byte so max value 255 */
9813 bpf_error(cstate
, "sio value %u too big; max value = 255",
9815 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
9816 jtype
, reverse
, jvalue
);
9824 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9825 bpf_error(cstate
, "'opc' supported only on SS7");
9826 /* opc coded on 14 bits so max value 16383 */
9828 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9830 /* the following instructions are made to convert jvalue
9831 * to the form used to write opc in an ss7 message*/
9832 val1
= jvalue
& 0x00003c00;
9834 val2
= jvalue
& 0x000003fc;
9836 val3
= jvalue
& 0x00000003;
9838 jvalue
= val1
+ val2
+ val3
;
9839 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
9840 jtype
, reverse
, jvalue
);
9848 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9849 bpf_error(cstate
, "'dpc' supported only on SS7");
9850 /* dpc coded on 14 bits so max value 16383 */
9852 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9854 /* the following instructions are made to convert jvalue
9855 * to the forme used to write dpc in an ss7 message*/
9856 val1
= jvalue
& 0x000000ff;
9858 val2
= jvalue
& 0x00003f00;
9860 jvalue
= val1
+ val2
;
9861 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
9862 jtype
, reverse
, jvalue
);
9870 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9871 bpf_error(cstate
, "'sls' supported only on SS7");
9872 /* sls coded on 4 bits so max value 15 */
9874 bpf_error(cstate
, "sls value %u too big; max value = 15",
9876 /* the following instruction is made to convert jvalue
9877 * to the forme used to write sls in an ss7 message*/
9878 jvalue
= jvalue
<< 4;
9879 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
9880 jtype
, reverse
, jvalue
);
9889 static struct block
*
9890 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9895 * Q.2931 signalling protocol messages for handling virtual circuits
9896 * establishment and teardown
9901 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9905 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9909 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9913 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9917 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9920 case A_RELEASE_DONE
:
9921 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9931 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9933 struct block
*b0
, *b1
;
9936 * Catch errors reported by us and routines below us, and return NULL
9939 if (setjmp(cstate
->top_ctx
))
9945 if (!cstate
->is_atm
)
9946 bpf_error(cstate
, "'oam' supported only on raw ATM");
9948 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9949 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9951 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9956 if (!cstate
->is_atm
)
9957 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9959 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9960 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9962 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9968 * Get Q.2931 signalling messages for switched
9969 * virtual connection
9971 if (!cstate
->is_atm
)
9972 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9973 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9974 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9976 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9978 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9980 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9982 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9984 b0
= gen_atmtype_sc(cstate
);
9989 if (!cstate
->is_atm
)
9990 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9991 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9992 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9994 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9996 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9998 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10000 b0
= gen_atmtype_metac(cstate
);