1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <pcap-types.h>
31 #include <sys/socket.h>
34 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
55 #include "ethertype.h"
59 #include "ieee80211.h"
61 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
96 uint16_t u6_addr16
[8];
97 uint32_t u6_addr32
[4];
99 #define s6_addr in6_u.u6_addr8
100 #define s6_addr16 in6_u.u6_addr16
101 #define s6_addr32 in6_u.u6_addr32
102 #define s6_addr64 in6_u.u6_addr64
105 typedef unsigned short sa_family_t
;
107 #define __SOCKADDR_COMMON(sa_prefix) \
108 sa_family_t sa_prefix##family
110 /* Ditto, for IPv6. */
113 __SOCKADDR_COMMON (sin6_
);
114 uint16_t sin6_port
; /* Transport layer port # */
115 uint32_t sin6_flowinfo
; /* IPv6 flow information */
116 struct in6_addr sin6_addr
; /* IPv6 address */
119 #ifndef EAI_ADDRFAMILY
121 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
122 int ai_family
; /* PF_xxx */
123 int ai_socktype
; /* SOCK_xxx */
124 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
125 size_t ai_addrlen
; /* length of ai_addr */
126 char *ai_canonname
; /* canonical name for hostname */
127 struct sockaddr
*ai_addr
; /* binary address */
128 struct addrinfo
*ai_next
; /* next structure in linked list */
130 #endif /* EAI_ADDRFAMILY */
131 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
134 #include <netdb.h> /* for "struct addrinfo" */
136 #include <pcap/namedb.h>
138 #include "nametoaddr.h"
140 #define ETHERMTU 1500
142 #ifndef IPPROTO_HOPOPTS
143 #define IPPROTO_HOPOPTS 0
145 #ifndef IPPROTO_ROUTING
146 #define IPPROTO_ROUTING 43
148 #ifndef IPPROTO_FRAGMENT
149 #define IPPROTO_FRAGMENT 44
151 #ifndef IPPROTO_DSTOPTS
152 #define IPPROTO_DSTOPTS 60
155 #define IPPROTO_SCTP 132
158 #define GENEVE_PORT 6081
160 #ifdef HAVE_OS_PROTO_H
161 #include "os-proto.h"
164 #define JMP(c) ((c)|BPF_JMP|BPF_K)
167 * "Push" the current value of the link-layer header type and link-layer
168 * header offset onto a "stack", and set a new value. (It's not a
169 * full-blown stack; we keep only the top two items.)
171 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
173 (cs)->prevlinktype = (cs)->linktype; \
174 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
175 (cs)->linktype = (new_linktype); \
176 (cs)->off_linkhdr.is_variable = (new_is_variable); \
177 (cs)->off_linkhdr.constant_part = (new_constant_part); \
178 (cs)->off_linkhdr.reg = (new_reg); \
179 (cs)->is_geneve = 0; \
183 * Offset "not set" value.
185 #define OFFSET_NOT_SET 0xffffffffU
188 * Absolute offsets, which are offsets from the beginning of the raw
189 * packet data, are, in the general case, the sum of a variable value
190 * and a constant value; the variable value may be absent, in which
191 * case the offset is only the constant value, and the constant value
192 * may be zero, in which case the offset is only the variable value.
194 * bpf_abs_offset is a structure containing all that information:
196 * is_variable is 1 if there's a variable part.
198 * constant_part is the constant part of the value, possibly zero;
200 * if is_variable is 1, reg is the register number for a register
201 * containing the variable value if the register has been assigned,
211 * Value passed to gen_load_a() to indicate what the offset argument
212 * is relative to the beginning of.
215 OR_PACKET
, /* full packet data */
216 OR_LINKHDR
, /* link-layer header */
217 OR_PREVLINKHDR
, /* previous link-layer header */
218 OR_LLC
, /* 802.2 LLC header */
219 OR_PREVMPLSHDR
, /* previous MPLS header */
220 OR_LINKTYPE
, /* link-layer type */
221 OR_LINKPL
, /* link-layer payload */
222 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
223 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
224 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
228 * We divy out chunks of memory rather than call malloc each time so
229 * we don't have to worry about leaking memory. It's probably
230 * not a big deal if all this memory was wasted but if this ever
231 * goes into a library that would probably not be a good idea.
233 * XXX - this *is* in a library....
236 #define CHUNK0SIZE 1024
242 /* Code generator state */
244 struct _compiler_state
{
255 int outermostlinktype
;
260 /* Hack for handling VLAN and MPLS stacks. */
261 u_int label_stack_depth
;
262 u_int vlan_stack_depth
;
268 * As errors are handled by a longjmp, anything allocated must
269 * be freed in the longjmp handler, so it must be reachable
272 * One thing that's allocated is the result of pcap_nametoaddrinfo();
273 * it must be freed with freeaddrinfo(). This variable points to
274 * any addrinfo structure that would need to be freed.
279 * Another thing that's allocated is the result of pcap_ether_aton();
280 * it must be freed with free(). This variable points to any
281 * address that would need to be freed.
286 * Various code constructs need to know the layout of the packet.
287 * These values give the necessary offsets from the beginning
288 * of the packet data.
292 * Absolute offset of the beginning of the link-layer header.
294 bpf_abs_offset off_linkhdr
;
297 * If we're checking a link-layer header for a packet encapsulated
298 * in another protocol layer, this is the equivalent information
299 * for the previous layers' link-layer header from the beginning
300 * of the raw packet data.
302 bpf_abs_offset off_prevlinkhdr
;
305 * This is the equivalent information for the outermost layers'
308 bpf_abs_offset off_outermostlinkhdr
;
311 * Absolute offset of the beginning of the link-layer payload.
313 bpf_abs_offset off_linkpl
;
316 * "off_linktype" is the offset to information in the link-layer
317 * header giving the packet type. This is an absolute offset
318 * from the beginning of the packet.
320 * For Ethernet, it's the offset of the Ethernet type field; this
321 * means that it must have a value that skips VLAN tags.
323 * For link-layer types that always use 802.2 headers, it's the
324 * offset of the LLC header; this means that it must have a value
325 * that skips VLAN tags.
327 * For PPP, it's the offset of the PPP type field.
329 * For Cisco HDLC, it's the offset of the CHDLC type field.
331 * For BSD loopback, it's the offset of the AF_ value.
333 * For Linux cooked sockets, it's the offset of the type field.
335 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
336 * encapsulation, in which case, IP is assumed.
338 bpf_abs_offset off_linktype
;
341 * TRUE if the link layer includes an ATM pseudo-header.
346 * TRUE if "geneve" appeared in the filter; it causes us to
347 * generate code that checks for a Geneve header and assume
348 * that later filters apply to the encapsulated payload.
353 * TRUE if we need variable length part of VLAN offset
355 int is_vlan_vloffset
;
358 * These are offsets for the ATM pseudo-header.
365 * These are offsets for the MTP2 fields.
371 * These are offsets for the MTP3 fields.
379 * This is the offset of the first byte after the ATM pseudo_header,
380 * or -1 if there is no ATM pseudo-header.
385 * These are offsets to the beginning of the network-layer header.
386 * They are relative to the beginning of the link-layer payload
387 * (i.e., they don't include off_linkhdr.constant_part or
388 * off_linkpl.constant_part).
390 * If the link layer never uses 802.2 LLC:
392 * "off_nl" and "off_nl_nosnap" are the same.
394 * If the link layer always uses 802.2 LLC:
396 * "off_nl" is the offset if there's a SNAP header following
399 * "off_nl_nosnap" is the offset if there's no SNAP header.
401 * If the link layer is Ethernet:
403 * "off_nl" is the offset if the packet is an Ethernet II packet
404 * (we assume no 802.3+802.2+SNAP);
406 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
407 * with an 802.2 header following it.
413 * Here we handle simple allocation of the scratch registers.
414 * If too many registers are alloc'd, the allocator punts.
416 int regused
[BPF_MEMWORDS
];
422 struct chunk chunks
[NCHUNKS
];
427 * For use by routines outside this file.
431 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
436 * If we've already set an error, don't override it.
437 * The lexical analyzer reports some errors by setting
438 * the error and then returning a LEX_ERROR token, which
439 * is not recognized by any grammar rule, and thus forces
440 * the parse to stop. We don't want the error reported
441 * by the lexical analyzer to be overwritten by the syntax
444 if (!cstate
->error_set
) {
446 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
449 cstate
->error_set
= 1;
454 * For use *ONLY* in routines in this file.
456 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
457 PCAP_PRINTFLIKE(2, 3);
460 static void PCAP_NORETURN
461 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
466 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
469 longjmp(cstate
->top_ctx
, 1);
473 static int init_linktype(compiler_state_t
*, pcap_t
*);
475 static void init_regs(compiler_state_t
*);
476 static int alloc_reg(compiler_state_t
*);
477 static void free_reg(compiler_state_t
*, int);
479 static void initchunks(compiler_state_t
*cstate
);
480 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
481 static void *newchunk(compiler_state_t
*cstate
, size_t);
482 static void freechunks(compiler_state_t
*cstate
);
483 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
484 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
485 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
486 static inline void syntax(compiler_state_t
*cstate
);
488 static void backpatch(struct block
*, struct block
*);
489 static void merge(struct block
*, struct block
*);
490 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
492 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
494 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
496 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
498 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
500 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
501 u_int
, bpf_u_int32
, bpf_u_int32
);
502 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
503 u_int
, const u_char
*);
504 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
505 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
506 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
508 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
510 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
511 static struct block
*gen_uncond(compiler_state_t
*, int);
512 static inline struct block
*gen_true(compiler_state_t
*);
513 static inline struct block
*gen_false(compiler_state_t
*);
514 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
515 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
516 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
517 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
518 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
519 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
520 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
521 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
522 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
524 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
525 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
526 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
527 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
528 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
529 int, bpf_u_int32
, u_int
, u_int
);
531 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
532 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
534 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
535 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
536 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
537 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
538 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
539 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
540 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
541 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
542 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
545 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
546 struct in6_addr
*, int, int, int);
549 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
550 struct addrinfo
*, int, int);
552 static struct block
*gen_ipfrag(compiler_state_t
*);
553 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
554 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
556 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
557 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
559 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
560 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
561 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
563 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
564 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
565 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
566 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
568 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
569 static int lookup_proto(compiler_state_t
*, const char *, int);
570 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
571 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
572 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
573 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
574 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
575 static struct block
*gen_len(compiler_state_t
*, int, int);
576 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
577 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
579 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
580 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
581 bpf_u_int32
, int, int);
582 static struct block
*gen_atmtype_llc(compiler_state_t
*);
583 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
586 initchunks(compiler_state_t
*cstate
)
590 for (i
= 0; i
< NCHUNKS
; i
++) {
591 cstate
->chunks
[i
].n_left
= 0;
592 cstate
->chunks
[i
].m
= NULL
;
594 cstate
->cur_chunk
= 0;
598 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
605 /* XXX Round up to nearest long. */
606 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
608 /* XXX Round up to structure boundary. */
612 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
613 if (n
> cp
->n_left
) {
615 k
= ++cstate
->cur_chunk
;
617 bpf_set_error(cstate
, "out of memory");
620 size
= CHUNK0SIZE
<< k
;
621 cp
->m
= (void *)malloc(size
);
623 bpf_set_error(cstate
, "out of memory");
626 memset((char *)cp
->m
, 0, size
);
629 bpf_set_error(cstate
, "out of memory");
634 return (void *)((char *)cp
->m
+ cp
->n_left
);
638 newchunk(compiler_state_t
*cstate
, size_t n
)
642 p
= newchunk_nolongjmp(cstate
, n
);
644 longjmp(cstate
->top_ctx
, 1);
651 freechunks(compiler_state_t
*cstate
)
655 for (i
= 0; i
< NCHUNKS
; ++i
)
656 if (cstate
->chunks
[i
].m
!= NULL
)
657 free(cstate
->chunks
[i
].m
);
661 * A strdup whose allocations are freed after code generation is over.
662 * This is used by the lexical analyzer, so it can't longjmp; it just
663 * returns NULL on an allocation error, and the callers must check
667 sdup(compiler_state_t
*cstate
, const char *s
)
669 size_t n
= strlen(s
) + 1;
670 char *cp
= newchunk_nolongjmp(cstate
, n
);
674 pcap_strlcpy(cp
, s
, n
);
678 static inline struct block
*
679 new_block(compiler_state_t
*cstate
, int code
)
683 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
690 static inline struct slist
*
691 new_stmt(compiler_state_t
*cstate
, int code
)
695 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
701 static struct block
*
702 gen_retblk(compiler_state_t
*cstate
, int v
)
704 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
710 static inline PCAP_NORETURN_DEF
void
711 syntax(compiler_state_t
*cstate
)
713 bpf_error(cstate
, "syntax error in filter expression");
717 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
718 const char *buf
, int optimize
, bpf_u_int32 mask
)
723 compiler_state_t cstate
;
724 const char * volatile xbuf
= buf
;
725 yyscan_t scanner
= NULL
;
726 volatile YY_BUFFER_STATE in_buffer
= NULL
;
731 * If this pcap_t hasn't been activated, it doesn't have a
732 * link-layer type, so we can't use it.
735 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
736 "not-yet-activated pcap_t passed to pcap_compile");
748 * If the device on which we're capturing need to be notified
749 * that a new filter is being compiled, do so.
751 * This allows them to save a copy of it, in case, for example,
752 * they're implementing a form of remote packet capture, and
753 * want the remote machine to filter out the packets in which
754 * it's sending the packets it's captured.
756 * XXX - the fact that we happen to be compiling a filter
757 * doesn't necessarily mean we'll be installing it as the
758 * filter for this pcap_t; we might be running it from userland
759 * on captured packets to do packet classification. We really
760 * need a better way of handling this, but this is all that
761 * the WinPcap remote capture code did.
763 if (p
->save_current_filter_op
!= NULL
)
764 (p
->save_current_filter_op
)(p
, buf
);
768 cstate
.no_optimize
= 0;
773 cstate
.ic
.root
= NULL
;
774 cstate
.ic
.cur_mark
= 0;
776 cstate
.error_set
= 0;
779 cstate
.netmask
= mask
;
781 cstate
.snaplen
= pcap_snapshot(p
);
782 if (cstate
.snaplen
== 0) {
783 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
784 "snaplen of 0 rejects all packets");
789 if (pcap_lex_init(&scanner
) != 0)
790 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
791 errno
, "can't initialize scanner");
792 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
795 * Associate the compiler state with the lexical analyzer
798 pcap_set_extra(&cstate
, scanner
);
800 if (init_linktype(&cstate
, p
) == -1) {
804 if (pcap_parse(scanner
, &cstate
) != 0) {
806 if (cstate
.ai
!= NULL
)
807 freeaddrinfo(cstate
.ai
);
809 if (cstate
.e
!= NULL
)
815 if (cstate
.ic
.root
== NULL
) {
817 * Catch errors reported by gen_retblk().
819 if (setjmp(cstate
.top_ctx
)) {
823 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
826 if (optimize
&& !cstate
.no_optimize
) {
827 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
832 if (cstate
.ic
.root
== NULL
||
833 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
834 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
835 "expression rejects all packets");
840 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
841 cstate
.ic
.root
, &len
, p
->errbuf
);
842 if (program
->bf_insns
== NULL
) {
847 program
->bf_len
= len
;
849 rc
= 0; /* We're all okay */
853 * Clean up everything for the lexical analyzer.
855 if (in_buffer
!= NULL
)
856 pcap__delete_buffer(in_buffer
, scanner
);
858 pcap_lex_destroy(scanner
);
861 * Clean up our own allocated memory.
869 * entry point for using the compiler with no pcap open
870 * pass in all the stuff that is needed explicitly instead.
873 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
874 struct bpf_program
*program
,
875 const char *buf
, int optimize
, bpf_u_int32 mask
)
880 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
883 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
889 * Clean up a "struct bpf_program" by freeing all the memory allocated
893 pcap_freecode(struct bpf_program
*program
)
896 if (program
->bf_insns
!= NULL
) {
897 free((char *)program
->bf_insns
);
898 program
->bf_insns
= NULL
;
903 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
904 * which of the jt and jf fields has been resolved and which is a pointer
905 * back to another unresolved block (or nil). At least one of the fields
906 * in each block is already resolved.
909 backpatch(struct block
*list
, struct block
*target
)
926 * Merge the lists in b0 and b1, using the 'sense' field to indicate
927 * which of jt and jf is the link.
930 merge(struct block
*b0
, struct block
*b1
)
932 register struct block
**p
= &b0
;
934 /* Find end of list. */
936 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
938 /* Concatenate the lists. */
943 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
945 struct block
*ppi_dlt_check
;
948 * Catch errors reported by us and routines below us, and return -1
951 if (setjmp(cstate
->top_ctx
))
955 * Insert before the statements of the first (root) block any
956 * statements needed to load the lengths of any variable-length
957 * headers into registers.
959 * XXX - a fancier strategy would be to insert those before the
960 * statements of all blocks that use those lengths and that
961 * have no predecessors that use them, so that we only compute
962 * the lengths if we need them. There might be even better
963 * approaches than that.
965 * However, those strategies would be more complicated, and
966 * as we don't generate code to compute a length if the
967 * program has no tests that use the length, and as most
968 * tests will probably use those lengths, we would just
969 * postpone computing the lengths so that it's not done
970 * for tests that fail early, and it's not clear that's
973 insert_compute_vloffsets(cstate
, p
->head
);
976 * For DLT_PPI captures, generate a check of the per-packet
977 * DLT value to make sure it's DLT_IEEE802_11.
979 * XXX - TurboCap cards use DLT_PPI for Ethernet.
980 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
981 * with appropriate Ethernet information and use that rather
982 * than using something such as DLT_PPI where you don't know
983 * the link-layer header type until runtime, which, in the
984 * general case, would force us to generate both Ethernet *and*
985 * 802.11 code (*and* anything else for which PPI is used)
986 * and choose between them early in the BPF program?
988 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
989 if (ppi_dlt_check
!= NULL
)
990 gen_and(ppi_dlt_check
, p
);
992 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
993 p
->sense
= !p
->sense
;
994 backpatch(p
, gen_retblk(cstate
, 0));
995 cstate
->ic
.root
= p
->head
;
1000 gen_and(struct block
*b0
, struct block
*b1
)
1002 backpatch(b0
, b1
->head
);
1003 b0
->sense
= !b0
->sense
;
1004 b1
->sense
= !b1
->sense
;
1006 b1
->sense
= !b1
->sense
;
1007 b1
->head
= b0
->head
;
1011 gen_or(struct block
*b0
, struct block
*b1
)
1013 b0
->sense
= !b0
->sense
;
1014 backpatch(b0
, b1
->head
);
1015 b0
->sense
= !b0
->sense
;
1017 b1
->head
= b0
->head
;
1021 gen_not(struct block
*b
)
1023 b
->sense
= !b
->sense
;
1026 static struct block
*
1027 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1028 u_int size
, bpf_u_int32 v
)
1030 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1033 static struct block
*
1034 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1035 u_int size
, bpf_u_int32 v
)
1037 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1040 static struct block
*
1041 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1042 u_int size
, bpf_u_int32 v
)
1044 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1047 static struct block
*
1048 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1049 u_int size
, bpf_u_int32 v
)
1051 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1054 static struct block
*
1055 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1056 u_int size
, bpf_u_int32 v
)
1058 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1061 static struct block
*
1062 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1063 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1065 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1068 static struct block
*
1069 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1070 u_int size
, const u_char
*v
)
1072 register struct block
*b
, *tmp
;
1076 register const u_char
*p
= &v
[size
- 4];
1078 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1086 register const u_char
*p
= &v
[size
- 2];
1088 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1096 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1105 * AND the field of size "size" at offset "offset" relative to the header
1106 * specified by "offrel" with "mask", and compare it with the value "v"
1107 * with the test specified by "jtype"; if "reverse" is true, the test
1108 * should test the opposite of "jtype".
1110 static struct block
*
1111 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1112 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1115 struct slist
*s
, *s2
;
1118 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1120 if (mask
!= 0xffffffff) {
1121 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1126 b
= new_block(cstate
, JMP(jtype
));
1129 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1135 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1137 cstate
->pcap_fddipad
= p
->fddipad
;
1140 * We start out with only one link-layer header.
1142 cstate
->outermostlinktype
= pcap_datalink(p
);
1143 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1144 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1145 cstate
->off_outermostlinkhdr
.reg
= -1;
1147 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1148 cstate
->off_prevlinkhdr
.constant_part
= 0;
1149 cstate
->off_prevlinkhdr
.is_variable
= 0;
1150 cstate
->off_prevlinkhdr
.reg
= -1;
1152 cstate
->linktype
= cstate
->outermostlinktype
;
1153 cstate
->off_linkhdr
.constant_part
= 0;
1154 cstate
->off_linkhdr
.is_variable
= 0;
1155 cstate
->off_linkhdr
.reg
= -1;
1160 cstate
->off_linkpl
.constant_part
= 0;
1161 cstate
->off_linkpl
.is_variable
= 0;
1162 cstate
->off_linkpl
.reg
= -1;
1164 cstate
->off_linktype
.constant_part
= 0;
1165 cstate
->off_linktype
.is_variable
= 0;
1166 cstate
->off_linktype
.reg
= -1;
1169 * Assume it's not raw ATM with a pseudo-header, for now.
1172 cstate
->off_vpi
= OFFSET_NOT_SET
;
1173 cstate
->off_vci
= OFFSET_NOT_SET
;
1174 cstate
->off_proto
= OFFSET_NOT_SET
;
1175 cstate
->off_payload
= OFFSET_NOT_SET
;
1180 cstate
->is_geneve
= 0;
1183 * No variable length VLAN offset by default
1185 cstate
->is_vlan_vloffset
= 0;
1188 * And assume we're not doing SS7.
1190 cstate
->off_li
= OFFSET_NOT_SET
;
1191 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1192 cstate
->off_sio
= OFFSET_NOT_SET
;
1193 cstate
->off_opc
= OFFSET_NOT_SET
;
1194 cstate
->off_dpc
= OFFSET_NOT_SET
;
1195 cstate
->off_sls
= OFFSET_NOT_SET
;
1197 cstate
->label_stack_depth
= 0;
1198 cstate
->vlan_stack_depth
= 0;
1200 switch (cstate
->linktype
) {
1203 cstate
->off_linktype
.constant_part
= 2;
1204 cstate
->off_linkpl
.constant_part
= 6;
1205 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1206 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1209 case DLT_ARCNET_LINUX
:
1210 cstate
->off_linktype
.constant_part
= 4;
1211 cstate
->off_linkpl
.constant_part
= 8;
1212 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1213 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1217 cstate
->off_linktype
.constant_part
= 12;
1218 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1219 cstate
->off_nl
= 0; /* Ethernet II */
1220 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1225 * SLIP doesn't have a link level type. The 16 byte
1226 * header is hacked into our SLIP driver.
1228 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1229 cstate
->off_linkpl
.constant_part
= 16;
1231 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1234 case DLT_SLIP_BSDOS
:
1235 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1236 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1238 cstate
->off_linkpl
.constant_part
= 24;
1240 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1245 cstate
->off_linktype
.constant_part
= 0;
1246 cstate
->off_linkpl
.constant_part
= 4;
1248 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1252 cstate
->off_linktype
.constant_part
= 0;
1253 cstate
->off_linkpl
.constant_part
= 12;
1255 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1260 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1261 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1262 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1263 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1265 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1270 * This does no include the Ethernet header, and
1271 * only covers session state.
1273 cstate
->off_linktype
.constant_part
= 6;
1274 cstate
->off_linkpl
.constant_part
= 8;
1276 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1280 cstate
->off_linktype
.constant_part
= 5;
1281 cstate
->off_linkpl
.constant_part
= 24;
1283 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1288 * FDDI doesn't really have a link-level type field.
1289 * We set "off_linktype" to the offset of the LLC header.
1291 * To check for Ethernet types, we assume that SSAP = SNAP
1292 * is being used and pick out the encapsulated Ethernet type.
1293 * XXX - should we generate code to check for SNAP?
1295 cstate
->off_linktype
.constant_part
= 13;
1296 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1297 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1298 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1299 cstate
->off_nl
= 8; /* 802.2+SNAP */
1300 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1305 * Token Ring doesn't really have a link-level type field.
1306 * We set "off_linktype" to the offset of the LLC header.
1308 * To check for Ethernet types, we assume that SSAP = SNAP
1309 * is being used and pick out the encapsulated Ethernet type.
1310 * XXX - should we generate code to check for SNAP?
1312 * XXX - the header is actually variable-length.
1313 * Some various Linux patched versions gave 38
1314 * as "off_linktype" and 40 as "off_nl"; however,
1315 * if a token ring packet has *no* routing
1316 * information, i.e. is not source-routed, the correct
1317 * values are 20 and 22, as they are in the vanilla code.
1319 * A packet is source-routed iff the uppermost bit
1320 * of the first byte of the source address, at an
1321 * offset of 8, has the uppermost bit set. If the
1322 * packet is source-routed, the total number of bytes
1323 * of routing information is 2 plus bits 0x1F00 of
1324 * the 16-bit value at an offset of 14 (shifted right
1325 * 8 - figure out which byte that is).
1327 cstate
->off_linktype
.constant_part
= 14;
1328 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1329 cstate
->off_nl
= 8; /* 802.2+SNAP */
1330 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1333 case DLT_PRISM_HEADER
:
1334 case DLT_IEEE802_11_RADIO_AVS
:
1335 case DLT_IEEE802_11_RADIO
:
1336 cstate
->off_linkhdr
.is_variable
= 1;
1337 /* Fall through, 802.11 doesn't have a variable link
1338 * prefix but is otherwise the same. */
1341 case DLT_IEEE802_11
:
1343 * 802.11 doesn't really have a link-level type field.
1344 * We set "off_linktype.constant_part" to the offset of
1347 * To check for Ethernet types, we assume that SSAP = SNAP
1348 * is being used and pick out the encapsulated Ethernet type.
1349 * XXX - should we generate code to check for SNAP?
1351 * We also handle variable-length radio headers here.
1352 * The Prism header is in theory variable-length, but in
1353 * practice it's always 144 bytes long. However, some
1354 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1355 * sometimes or always supply an AVS header, so we
1356 * have to check whether the radio header is a Prism
1357 * header or an AVS header, so, in practice, it's
1360 cstate
->off_linktype
.constant_part
= 24;
1361 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1362 cstate
->off_linkpl
.is_variable
= 1;
1363 cstate
->off_nl
= 8; /* 802.2+SNAP */
1364 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1369 * At the moment we treat PPI the same way that we treat
1370 * normal Radiotap encoded packets. The difference is in
1371 * the function that generates the code at the beginning
1372 * to compute the header length. Since this code generator
1373 * of PPI supports bare 802.11 encapsulation only (i.e.
1374 * the encapsulated DLT should be DLT_IEEE802_11) we
1375 * generate code to check for this too.
1377 cstate
->off_linktype
.constant_part
= 24;
1378 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1379 cstate
->off_linkpl
.is_variable
= 1;
1380 cstate
->off_linkhdr
.is_variable
= 1;
1381 cstate
->off_nl
= 8; /* 802.2+SNAP */
1382 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1385 case DLT_ATM_RFC1483
:
1386 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1388 * assume routed, non-ISO PDUs
1389 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1391 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1392 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1393 * latter would presumably be treated the way PPPoE
1394 * should be, so you can do "pppoe and udp port 2049"
1395 * or "pppoa and tcp port 80" and have it check for
1396 * PPPo{A,E} and a PPP protocol of IP and....
1398 cstate
->off_linktype
.constant_part
= 0;
1399 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1400 cstate
->off_nl
= 8; /* 802.2+SNAP */
1401 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1406 * Full Frontal ATM; you get AALn PDUs with an ATM
1410 cstate
->off_vpi
= SUNATM_VPI_POS
;
1411 cstate
->off_vci
= SUNATM_VCI_POS
;
1412 cstate
->off_proto
= PROTO_POS
;
1413 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1414 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1415 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1416 cstate
->off_nl
= 8; /* 802.2+SNAP */
1417 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1423 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1424 cstate
->off_linkpl
.constant_part
= 0;
1426 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1429 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1430 cstate
->off_linktype
.constant_part
= 14;
1431 cstate
->off_linkpl
.constant_part
= 16;
1433 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1436 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1437 cstate
->off_linktype
.constant_part
= 0;
1438 cstate
->off_linkpl
.constant_part
= 20;
1440 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1445 * LocalTalk does have a 1-byte type field in the LLAP header,
1446 * but really it just indicates whether there is a "short" or
1447 * "long" DDP packet following.
1449 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1450 cstate
->off_linkpl
.constant_part
= 0;
1452 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1455 case DLT_IP_OVER_FC
:
1457 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1458 * link-level type field. We set "off_linktype" to the
1459 * offset of the LLC header.
1461 * To check for Ethernet types, we assume that SSAP = SNAP
1462 * is being used and pick out the encapsulated Ethernet type.
1463 * XXX - should we generate code to check for SNAP? RFC
1464 * 2625 says SNAP should be used.
1466 cstate
->off_linktype
.constant_part
= 16;
1467 cstate
->off_linkpl
.constant_part
= 16;
1468 cstate
->off_nl
= 8; /* 802.2+SNAP */
1469 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1474 * XXX - we should set this to handle SNAP-encapsulated
1475 * frames (NLPID of 0x80).
1477 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1478 cstate
->off_linkpl
.constant_part
= 0;
1480 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1484 * the only BPF-interesting FRF.16 frames are non-control frames;
1485 * Frame Relay has a variable length link-layer
1486 * so lets start with offset 4 for now and increments later on (FIXME);
1489 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1490 cstate
->off_linkpl
.constant_part
= 0;
1492 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1495 case DLT_APPLE_IP_OVER_IEEE1394
:
1496 cstate
->off_linktype
.constant_part
= 16;
1497 cstate
->off_linkpl
.constant_part
= 18;
1499 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1502 case DLT_SYMANTEC_FIREWALL
:
1503 cstate
->off_linktype
.constant_part
= 6;
1504 cstate
->off_linkpl
.constant_part
= 44;
1505 cstate
->off_nl
= 0; /* Ethernet II */
1506 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1509 #ifdef HAVE_NET_PFVAR_H
1511 cstate
->off_linktype
.constant_part
= 0;
1512 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1514 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1518 case DLT_JUNIPER_MFR
:
1519 case DLT_JUNIPER_MLFR
:
1520 case DLT_JUNIPER_MLPPP
:
1521 case DLT_JUNIPER_PPP
:
1522 case DLT_JUNIPER_CHDLC
:
1523 case DLT_JUNIPER_FRELAY
:
1524 cstate
->off_linktype
.constant_part
= 4;
1525 cstate
->off_linkpl
.constant_part
= 4;
1527 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1530 case DLT_JUNIPER_ATM1
:
1531 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1532 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1534 cstate
->off_nl_nosnap
= 10;
1537 case DLT_JUNIPER_ATM2
:
1538 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1539 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1541 cstate
->off_nl_nosnap
= 10;
1544 /* frames captured on a Juniper PPPoE service PIC
1545 * contain raw ethernet frames */
1546 case DLT_JUNIPER_PPPOE
:
1547 case DLT_JUNIPER_ETHER
:
1548 cstate
->off_linkpl
.constant_part
= 14;
1549 cstate
->off_linktype
.constant_part
= 16;
1550 cstate
->off_nl
= 18; /* Ethernet II */
1551 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1554 case DLT_JUNIPER_PPPOE_ATM
:
1555 cstate
->off_linktype
.constant_part
= 4;
1556 cstate
->off_linkpl
.constant_part
= 6;
1558 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1561 case DLT_JUNIPER_GGSN
:
1562 cstate
->off_linktype
.constant_part
= 6;
1563 cstate
->off_linkpl
.constant_part
= 12;
1565 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1568 case DLT_JUNIPER_ES
:
1569 cstate
->off_linktype
.constant_part
= 6;
1570 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1571 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1572 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1575 case DLT_JUNIPER_MONITOR
:
1576 cstate
->off_linktype
.constant_part
= 12;
1577 cstate
->off_linkpl
.constant_part
= 12;
1578 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1579 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1582 case DLT_BACNET_MS_TP
:
1583 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1584 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1585 cstate
->off_nl
= OFFSET_NOT_SET
;
1586 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1589 case DLT_JUNIPER_SERVICES
:
1590 cstate
->off_linktype
.constant_part
= 12;
1591 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1592 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1593 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1596 case DLT_JUNIPER_VP
:
1597 cstate
->off_linktype
.constant_part
= 18;
1598 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1599 cstate
->off_nl
= OFFSET_NOT_SET
;
1600 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1603 case DLT_JUNIPER_ST
:
1604 cstate
->off_linktype
.constant_part
= 18;
1605 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1606 cstate
->off_nl
= OFFSET_NOT_SET
;
1607 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1610 case DLT_JUNIPER_ISM
:
1611 cstate
->off_linktype
.constant_part
= 8;
1612 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1613 cstate
->off_nl
= OFFSET_NOT_SET
;
1614 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1617 case DLT_JUNIPER_VS
:
1618 case DLT_JUNIPER_SRX_E2E
:
1619 case DLT_JUNIPER_FIBRECHANNEL
:
1620 case DLT_JUNIPER_ATM_CEMIC
:
1621 cstate
->off_linktype
.constant_part
= 8;
1622 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1623 cstate
->off_nl
= OFFSET_NOT_SET
;
1624 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1629 cstate
->off_li_hsl
= 4;
1630 cstate
->off_sio
= 3;
1631 cstate
->off_opc
= 4;
1632 cstate
->off_dpc
= 4;
1633 cstate
->off_sls
= 7;
1634 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1635 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1636 cstate
->off_nl
= OFFSET_NOT_SET
;
1637 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1640 case DLT_MTP2_WITH_PHDR
:
1642 cstate
->off_li_hsl
= 8;
1643 cstate
->off_sio
= 7;
1644 cstate
->off_opc
= 8;
1645 cstate
->off_dpc
= 8;
1646 cstate
->off_sls
= 11;
1647 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1648 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1649 cstate
->off_nl
= OFFSET_NOT_SET
;
1650 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1654 cstate
->off_li
= 22;
1655 cstate
->off_li_hsl
= 24;
1656 cstate
->off_sio
= 23;
1657 cstate
->off_opc
= 24;
1658 cstate
->off_dpc
= 24;
1659 cstate
->off_sls
= 27;
1660 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1661 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1662 cstate
->off_nl
= OFFSET_NOT_SET
;
1663 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1667 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1668 cstate
->off_linkpl
.constant_part
= 4;
1670 cstate
->off_nl_nosnap
= 0;
1675 * Currently, only raw "link[N:M]" filtering is supported.
1677 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1678 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1679 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1680 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1684 cstate
->off_linktype
.constant_part
= 1;
1685 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1687 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1690 case DLT_NETANALYZER
:
1691 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1692 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1693 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1694 cstate
->off_nl
= 0; /* Ethernet II */
1695 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1698 case DLT_NETANALYZER_TRANSPARENT
:
1699 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1700 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1701 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1702 cstate
->off_nl
= 0; /* Ethernet II */
1703 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1708 * For values in the range in which we've assigned new
1709 * DLT_ values, only raw "link[N:M]" filtering is supported.
1711 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1712 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1713 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1714 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1715 cstate
->off_nl
= OFFSET_NOT_SET
;
1716 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1718 bpf_set_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1724 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1729 * Load a value relative to the specified absolute offset.
1731 static struct slist
*
1732 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1733 u_int offset
, u_int size
)
1735 struct slist
*s
, *s2
;
1737 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1740 * If "s" is non-null, it has code to arrange that the X register
1741 * contains the variable part of the absolute offset, so we
1742 * generate a load relative to that, with an offset of
1743 * abs_offset->constant_part + offset.
1745 * Otherwise, we can do an absolute load with an offset of
1746 * abs_offset->constant_part + offset.
1750 * "s" points to a list of statements that puts the
1751 * variable part of the absolute offset into the X register.
1752 * Do an indirect load, to use the X register as an offset.
1754 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1755 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1759 * There is no variable part of the absolute offset, so
1760 * just do an absolute load.
1762 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1763 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1769 * Load a value relative to the beginning of the specified header.
1771 static struct slist
*
1772 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1775 struct slist
*s
, *s2
;
1778 * Squelch warnings from compilers that *don't* assume that
1779 * offrel always has a valid enum value and therefore don't
1780 * assume that we'll always go through one of the case arms.
1782 * If we have a default case, compilers that *do* assume that
1783 * will then complain about the default case code being
1786 * Damned if you do, damned if you don't.
1793 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1798 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1801 case OR_PREVLINKHDR
:
1802 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1806 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1809 case OR_PREVMPLSHDR
:
1810 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1814 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1817 case OR_LINKPL_NOSNAP
:
1818 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1822 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1827 * Load the X register with the length of the IPv4 header
1828 * (plus the offset of the link-layer header, if it's
1829 * preceded by a variable-length header such as a radio
1830 * header), in bytes.
1832 s
= gen_loadx_iphdrlen(cstate
);
1835 * Load the item at {offset of the link-layer payload} +
1836 * {offset, relative to the start of the link-layer
1837 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1838 * {specified offset}.
1840 * If the offset of the link-layer payload is variable,
1841 * the variable part of that offset is included in the
1842 * value in the X register, and we include the constant
1843 * part in the offset of the load.
1845 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1846 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1851 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1858 * Generate code to load into the X register the sum of the length of
1859 * the IPv4 header and the variable part of the offset of the link-layer
1862 static struct slist
*
1863 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1865 struct slist
*s
, *s2
;
1867 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1870 * The offset of the link-layer payload has a variable
1871 * part. "s" points to a list of statements that put
1872 * the variable part of that offset into the X register.
1874 * The 4*([k]&0xf) addressing mode can't be used, as we
1875 * don't have a constant offset, so we have to load the
1876 * value in question into the A register and add to it
1877 * the value from the X register.
1879 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1880 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1882 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1885 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1890 * The A register now contains the length of the IP header.
1891 * We need to add to it the variable part of the offset of
1892 * the link-layer payload, which is still in the X
1893 * register, and move the result into the X register.
1895 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1896 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1899 * The offset of the link-layer payload is a constant,
1900 * so no code was generated to load the (non-existent)
1901 * variable part of that offset.
1903 * This means we can use the 4*([k]&0xf) addressing
1904 * mode. Load the length of the IPv4 header, which
1905 * is at an offset of cstate->off_nl from the beginning of
1906 * the link-layer payload, and thus at an offset of
1907 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1908 * of the raw packet data, using that addressing mode.
1910 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1911 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1917 static struct block
*
1918 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1923 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1925 b
= new_block(cstate
, JMP(BPF_JEQ
));
1931 static inline struct block
*
1932 gen_true(compiler_state_t
*cstate
)
1934 return gen_uncond(cstate
, 1);
1937 static inline struct block
*
1938 gen_false(compiler_state_t
*cstate
)
1940 return gen_uncond(cstate
, 0);
1944 * Byte-swap a 32-bit number.
1945 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1946 * big-endian platforms.)
1948 #define SWAPLONG(y) \
1949 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1952 * Generate code to match a particular packet type.
1954 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1955 * value, if <= ETHERMTU. We use that to determine whether to
1956 * match the type/length field or to check the type/length field for
1957 * a value <= ETHERMTU to see whether it's a type field and then do
1958 * the appropriate test.
1960 static struct block
*
1961 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1963 struct block
*b0
, *b1
;
1969 case LLCSAP_NETBEUI
:
1971 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1972 * so we check the DSAP and SSAP.
1974 * LLCSAP_IP checks for IP-over-802.2, rather
1975 * than IP-over-Ethernet or IP-over-SNAP.
1977 * XXX - should we check both the DSAP and the
1978 * SSAP, like this, or should we check just the
1979 * DSAP, as we do for other types <= ETHERMTU
1980 * (i.e., other SAP values)?
1982 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1984 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
1992 * Ethernet_II frames, which are Ethernet
1993 * frames with a frame type of ETHERTYPE_IPX;
1995 * Ethernet_802.3 frames, which are 802.3
1996 * frames (i.e., the type/length field is
1997 * a length field, <= ETHERMTU, rather than
1998 * a type field) with the first two bytes
1999 * after the Ethernet/802.3 header being
2002 * Ethernet_802.2 frames, which are 802.3
2003 * frames with an 802.2 LLC header and
2004 * with the IPX LSAP as the DSAP in the LLC
2007 * Ethernet_SNAP frames, which are 802.3
2008 * frames with an LLC header and a SNAP
2009 * header and with an OUI of 0x000000
2010 * (encapsulated Ethernet) and a protocol
2011 * ID of ETHERTYPE_IPX in the SNAP header.
2013 * XXX - should we generate the same code both
2014 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2018 * This generates code to check both for the
2019 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2021 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2022 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2026 * Now we add code to check for SNAP frames with
2027 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2029 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2033 * Now we generate code to check for 802.3
2034 * frames in general.
2036 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2040 * Now add the check for 802.3 frames before the
2041 * check for Ethernet_802.2 and Ethernet_802.3,
2042 * as those checks should only be done on 802.3
2043 * frames, not on Ethernet frames.
2048 * Now add the check for Ethernet_II frames, and
2049 * do that before checking for the other frame
2052 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2056 case ETHERTYPE_ATALK
:
2057 case ETHERTYPE_AARP
:
2059 * EtherTalk (AppleTalk protocols on Ethernet link
2060 * layer) may use 802.2 encapsulation.
2064 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2065 * we check for an Ethernet type field less than
2066 * 1500, which means it's an 802.3 length field.
2068 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2072 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2073 * SNAP packets with an organization code of
2074 * 0x080007 (Apple, for Appletalk) and a protocol
2075 * type of ETHERTYPE_ATALK (Appletalk).
2077 * 802.2-encapsulated ETHERTYPE_AARP packets are
2078 * SNAP packets with an organization code of
2079 * 0x000000 (encapsulated Ethernet) and a protocol
2080 * type of ETHERTYPE_AARP (Appletalk ARP).
2082 if (ll_proto
== ETHERTYPE_ATALK
)
2083 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2084 else /* ll_proto == ETHERTYPE_AARP */
2085 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2089 * Check for Ethernet encapsulation (Ethertalk
2090 * phase 1?); we just check for the Ethernet
2093 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2099 if (ll_proto
<= ETHERMTU
) {
2101 * This is an LLC SAP value, so the frames
2102 * that match would be 802.2 frames.
2103 * Check that the frame is an 802.2 frame
2104 * (i.e., that the length/type field is
2105 * a length field, <= ETHERMTU) and
2106 * then check the DSAP.
2108 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2110 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2115 * This is an Ethernet type, so compare
2116 * the length/type field with it (if
2117 * the frame is an 802.2 frame, the length
2118 * field will be <= ETHERMTU, and, as
2119 * "ll_proto" is > ETHERMTU, this test
2120 * will fail and the frame won't match,
2121 * which is what we want).
2123 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2128 static struct block
*
2129 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2132 * For DLT_NULL, the link-layer header is a 32-bit word
2133 * containing an AF_ value in *host* byte order, and for
2134 * DLT_ENC, the link-layer header begins with a 32-bit
2135 * word containing an AF_ value in host byte order.
2137 * In addition, if we're reading a saved capture file,
2138 * the host byte order in the capture may not be the
2139 * same as the host byte order on this machine.
2141 * For DLT_LOOP, the link-layer header is a 32-bit
2142 * word containing an AF_ value in *network* byte order.
2144 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2146 * The AF_ value is in host byte order, but the BPF
2147 * interpreter will convert it to network byte order.
2149 * If this is a save file, and it's from a machine
2150 * with the opposite byte order to ours, we byte-swap
2153 * Then we run it through "htonl()", and generate
2154 * code to compare against the result.
2156 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2157 ll_proto
= SWAPLONG(ll_proto
);
2158 ll_proto
= htonl(ll_proto
);
2160 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2164 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2165 * or IPv6 then we have an error.
2167 static struct block
*
2168 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2173 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2176 case ETHERTYPE_IPV6
:
2177 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2184 return gen_false(cstate
);
2188 * Generate code to match a particular packet type.
2190 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2191 * value, if <= ETHERMTU. We use that to determine whether to
2192 * match the type field or to check the type field for the special
2193 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2195 static struct block
*
2196 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2198 struct block
*b0
, *b1
;
2204 case LLCSAP_NETBEUI
:
2206 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2207 * so we check the DSAP and SSAP.
2209 * LLCSAP_IP checks for IP-over-802.2, rather
2210 * than IP-over-Ethernet or IP-over-SNAP.
2212 * XXX - should we check both the DSAP and the
2213 * SSAP, like this, or should we check just the
2214 * DSAP, as we do for other types <= ETHERMTU
2215 * (i.e., other SAP values)?
2217 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2218 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2224 * Ethernet_II frames, which are Ethernet
2225 * frames with a frame type of ETHERTYPE_IPX;
2227 * Ethernet_802.3 frames, which have a frame
2228 * type of LINUX_SLL_P_802_3;
2230 * Ethernet_802.2 frames, which are 802.3
2231 * frames with an 802.2 LLC header (i.e, have
2232 * a frame type of LINUX_SLL_P_802_2) and
2233 * with the IPX LSAP as the DSAP in the LLC
2236 * Ethernet_SNAP frames, which are 802.3
2237 * frames with an LLC header and a SNAP
2238 * header and with an OUI of 0x000000
2239 * (encapsulated Ethernet) and a protocol
2240 * ID of ETHERTYPE_IPX in the SNAP header.
2242 * First, do the checks on LINUX_SLL_P_802_2
2243 * frames; generate the check for either
2244 * Ethernet_802.2 or Ethernet_SNAP frames, and
2245 * then put a check for LINUX_SLL_P_802_2 frames
2248 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2249 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2251 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2255 * Now check for 802.3 frames and OR that with
2256 * the previous test.
2258 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2262 * Now add the check for Ethernet_II frames, and
2263 * do that before checking for the other frame
2266 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2270 case ETHERTYPE_ATALK
:
2271 case ETHERTYPE_AARP
:
2273 * EtherTalk (AppleTalk protocols on Ethernet link
2274 * layer) may use 802.2 encapsulation.
2278 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2279 * we check for the 802.2 protocol type in the
2280 * "Ethernet type" field.
2282 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2285 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2286 * SNAP packets with an organization code of
2287 * 0x080007 (Apple, for Appletalk) and a protocol
2288 * type of ETHERTYPE_ATALK (Appletalk).
2290 * 802.2-encapsulated ETHERTYPE_AARP packets are
2291 * SNAP packets with an organization code of
2292 * 0x000000 (encapsulated Ethernet) and a protocol
2293 * type of ETHERTYPE_AARP (Appletalk ARP).
2295 if (ll_proto
== ETHERTYPE_ATALK
)
2296 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2297 else /* ll_proto == ETHERTYPE_AARP */
2298 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2302 * Check for Ethernet encapsulation (Ethertalk
2303 * phase 1?); we just check for the Ethernet
2306 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2312 if (ll_proto
<= ETHERMTU
) {
2314 * This is an LLC SAP value, so the frames
2315 * that match would be 802.2 frames.
2316 * Check for the 802.2 protocol type
2317 * in the "Ethernet type" field, and
2318 * then check the DSAP.
2320 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2321 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2327 * This is an Ethernet type, so compare
2328 * the length/type field with it (if
2329 * the frame is an 802.2 frame, the length
2330 * field will be <= ETHERMTU, and, as
2331 * "ll_proto" is > ETHERMTU, this test
2332 * will fail and the frame won't match,
2333 * which is what we want).
2335 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2340 static struct slist
*
2341 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2343 struct slist
*s1
, *s2
;
2344 struct slist
*sjeq_avs_cookie
;
2345 struct slist
*sjcommon
;
2348 * This code is not compatible with the optimizer, as
2349 * we are generating jmp instructions within a normal
2350 * slist of instructions
2352 cstate
->no_optimize
= 1;
2355 * Generate code to load the length of the radio header into
2356 * the register assigned to hold that length, if one has been
2357 * assigned. (If one hasn't been assigned, no code we've
2358 * generated uses that prefix, so we don't need to generate any
2361 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2362 * or always use the AVS header rather than the Prism header.
2363 * We load a 4-byte big-endian value at the beginning of the
2364 * raw packet data, and see whether, when masked with 0xFFFFF000,
2365 * it's equal to 0x80211000. If so, that indicates that it's
2366 * an AVS header (the masked-out bits are the version number).
2367 * Otherwise, it's a Prism header.
2369 * XXX - the Prism header is also, in theory, variable-length,
2370 * but no known software generates headers that aren't 144
2373 if (cstate
->off_linkhdr
.reg
!= -1) {
2377 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2381 * AND it with 0xFFFFF000.
2383 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2384 s2
->s
.k
= 0xFFFFF000;
2388 * Compare with 0x80211000.
2390 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2391 sjeq_avs_cookie
->s
.k
= 0x80211000;
2392 sappend(s1
, sjeq_avs_cookie
);
2397 * The 4 bytes at an offset of 4 from the beginning of
2398 * the AVS header are the length of the AVS header.
2399 * That field is big-endian.
2401 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2404 sjeq_avs_cookie
->s
.jt
= s2
;
2407 * Now jump to the code to allocate a register
2408 * into which to save the header length and
2409 * store the length there. (The "jump always"
2410 * instruction needs to have the k field set;
2411 * it's added to the PC, so, as we're jumping
2412 * over a single instruction, it should be 1.)
2414 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2416 sappend(s1
, sjcommon
);
2419 * Now for the code that handles the Prism header.
2420 * Just load the length of the Prism header (144)
2421 * into the A register. Have the test for an AVS
2422 * header branch here if we don't have an AVS header.
2424 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2427 sjeq_avs_cookie
->s
.jf
= s2
;
2430 * Now allocate a register to hold that value and store
2431 * it. The code for the AVS header will jump here after
2432 * loading the length of the AVS header.
2434 s2
= new_stmt(cstate
, BPF_ST
);
2435 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2437 sjcommon
->s
.jf
= s2
;
2440 * Now move it into the X register.
2442 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2450 static struct slist
*
2451 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2453 struct slist
*s1
, *s2
;
2456 * Generate code to load the length of the AVS header into
2457 * the register assigned to hold that length, if one has been
2458 * assigned. (If one hasn't been assigned, no code we've
2459 * generated uses that prefix, so we don't need to generate any
2462 if (cstate
->off_linkhdr
.reg
!= -1) {
2464 * The 4 bytes at an offset of 4 from the beginning of
2465 * the AVS header are the length of the AVS header.
2466 * That field is big-endian.
2468 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2472 * Now allocate a register to hold that value and store
2475 s2
= new_stmt(cstate
, BPF_ST
);
2476 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2480 * Now move it into the X register.
2482 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2490 static struct slist
*
2491 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2493 struct slist
*s1
, *s2
;
2496 * Generate code to load the length of the radiotap header into
2497 * the register assigned to hold that length, if one has been
2498 * assigned. (If one hasn't been assigned, no code we've
2499 * generated uses that prefix, so we don't need to generate any
2502 if (cstate
->off_linkhdr
.reg
!= -1) {
2504 * The 2 bytes at offsets of 2 and 3 from the beginning
2505 * of the radiotap header are the length of the radiotap
2506 * header; unfortunately, it's little-endian, so we have
2507 * to load it a byte at a time and construct the value.
2511 * Load the high-order byte, at an offset of 3, shift it
2512 * left a byte, and put the result in the X register.
2514 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2516 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2519 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2523 * Load the next byte, at an offset of 2, and OR the
2524 * value from the X register into it.
2526 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2529 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2533 * Now allocate a register to hold that value and store
2536 s2
= new_stmt(cstate
, BPF_ST
);
2537 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2541 * Now move it into the X register.
2543 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2552 * At the moment we treat PPI as normal Radiotap encoded
2553 * packets. The difference is in the function that generates
2554 * the code at the beginning to compute the header length.
2555 * Since this code generator of PPI supports bare 802.11
2556 * encapsulation only (i.e. the encapsulated DLT should be
2557 * DLT_IEEE802_11) we generate code to check for this too;
2558 * that's done in finish_parse().
2560 static struct slist
*
2561 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2563 struct slist
*s1
, *s2
;
2566 * Generate code to load the length of the radiotap header
2567 * into the register assigned to hold that length, if one has
2570 if (cstate
->off_linkhdr
.reg
!= -1) {
2572 * The 2 bytes at offsets of 2 and 3 from the beginning
2573 * of the radiotap header are the length of the radiotap
2574 * header; unfortunately, it's little-endian, so we have
2575 * to load it a byte at a time and construct the value.
2579 * Load the high-order byte, at an offset of 3, shift it
2580 * left a byte, and put the result in the X register.
2582 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2584 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2587 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2591 * Load the next byte, at an offset of 2, and OR the
2592 * value from the X register into it.
2594 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2597 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2601 * Now allocate a register to hold that value and store
2604 s2
= new_stmt(cstate
, BPF_ST
);
2605 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2609 * Now move it into the X register.
2611 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2620 * Load a value relative to the beginning of the link-layer header after the 802.11
2621 * header, i.e. LLC_SNAP.
2622 * The link-layer header doesn't necessarily begin at the beginning
2623 * of the packet data; there might be a variable-length prefix containing
2624 * radio information.
2626 static struct slist
*
2627 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2630 struct slist
*sjset_data_frame_1
;
2631 struct slist
*sjset_data_frame_2
;
2632 struct slist
*sjset_qos
;
2633 struct slist
*sjset_radiotap_flags_present
;
2634 struct slist
*sjset_radiotap_ext_present
;
2635 struct slist
*sjset_radiotap_tsft_present
;
2636 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2637 struct slist
*s_roundup
;
2639 if (cstate
->off_linkpl
.reg
== -1) {
2641 * No register has been assigned to the offset of
2642 * the link-layer payload, which means nobody needs
2643 * it; don't bother computing it - just return
2644 * what we already have.
2650 * This code is not compatible with the optimizer, as
2651 * we are generating jmp instructions within a normal
2652 * slist of instructions
2654 cstate
->no_optimize
= 1;
2657 * If "s" is non-null, it has code to arrange that the X register
2658 * contains the length of the prefix preceding the link-layer
2661 * Otherwise, the length of the prefix preceding the link-layer
2662 * header is "off_outermostlinkhdr.constant_part".
2666 * There is no variable-length header preceding the
2667 * link-layer header.
2669 * Load the length of the fixed-length prefix preceding
2670 * the link-layer header (if any) into the X register,
2671 * and store it in the cstate->off_linkpl.reg register.
2672 * That length is off_outermostlinkhdr.constant_part.
2674 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2675 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2679 * The X register contains the offset of the beginning of the
2680 * link-layer header; add 24, which is the minimum length
2681 * of the MAC header for a data frame, to that, and store it
2682 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2683 * which is at the offset in the X register, with an indexed load.
2685 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2687 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2690 s2
= new_stmt(cstate
, BPF_ST
);
2691 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2694 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2699 * Check the Frame Control field to see if this is a data frame;
2700 * a data frame has the 0x08 bit (b3) in that field set and the
2701 * 0x04 bit (b2) clear.
2703 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2704 sjset_data_frame_1
->s
.k
= 0x08;
2705 sappend(s
, sjset_data_frame_1
);
2708 * If b3 is set, test b2, otherwise go to the first statement of
2709 * the rest of the program.
2711 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2712 sjset_data_frame_2
->s
.k
= 0x04;
2713 sappend(s
, sjset_data_frame_2
);
2714 sjset_data_frame_1
->s
.jf
= snext
;
2717 * If b2 is not set, this is a data frame; test the QoS bit.
2718 * Otherwise, go to the first statement of the rest of the
2721 sjset_data_frame_2
->s
.jt
= snext
;
2722 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2723 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2724 sappend(s
, sjset_qos
);
2727 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2729 * Otherwise, go to the first statement of the rest of the
2732 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2733 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2735 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2738 s2
= new_stmt(cstate
, BPF_ST
);
2739 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2743 * If we have a radiotap header, look at it to see whether
2744 * there's Atheros padding between the MAC-layer header
2747 * Note: all of the fields in the radiotap header are
2748 * little-endian, so we byte-swap all of the values
2749 * we test against, as they will be loaded as big-endian
2752 * XXX - in the general case, we would have to scan through
2753 * *all* the presence bits, if there's more than one word of
2754 * presence bits. That would require a loop, meaning that
2755 * we wouldn't be able to run the filter in the kernel.
2757 * We assume here that the Atheros adapters that insert the
2758 * annoying padding don't have multiple antennae and therefore
2759 * do not generate radiotap headers with multiple presence words.
2761 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2763 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2764 * in the first presence flag word?
2766 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2770 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2771 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2772 sappend(s
, sjset_radiotap_flags_present
);
2775 * If not, skip all of this.
2777 sjset_radiotap_flags_present
->s
.jf
= snext
;
2780 * Otherwise, is the "extension" bit set in that word?
2782 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2783 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2784 sappend(s
, sjset_radiotap_ext_present
);
2785 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2788 * If so, skip all of this.
2790 sjset_radiotap_ext_present
->s
.jt
= snext
;
2793 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2795 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2796 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2797 sappend(s
, sjset_radiotap_tsft_present
);
2798 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2801 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2802 * at an offset of 16 from the beginning of the raw packet
2803 * data (8 bytes for the radiotap header and 8 bytes for
2806 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2809 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2812 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2814 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2815 sjset_tsft_datapad
->s
.k
= 0x20;
2816 sappend(s
, sjset_tsft_datapad
);
2819 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2820 * at an offset of 8 from the beginning of the raw packet
2821 * data (8 bytes for the radiotap header).
2823 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2826 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2829 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2831 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2832 sjset_notsft_datapad
->s
.k
= 0x20;
2833 sappend(s
, sjset_notsft_datapad
);
2836 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2837 * set, round the length of the 802.11 header to
2838 * a multiple of 4. Do that by adding 3 and then
2839 * dividing by and multiplying by 4, which we do by
2842 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2843 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2844 sappend(s
, s_roundup
);
2845 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2848 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2849 s2
->s
.k
= (bpf_u_int32
)~3;
2851 s2
= new_stmt(cstate
, BPF_ST
);
2852 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2855 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2856 sjset_tsft_datapad
->s
.jf
= snext
;
2857 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2858 sjset_notsft_datapad
->s
.jf
= snext
;
2860 sjset_qos
->s
.jf
= snext
;
2866 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2870 /* There is an implicit dependency between the link
2871 * payload and link header since the payload computation
2872 * includes the variable part of the header. Therefore,
2873 * if nobody else has allocated a register for the link
2874 * header and we need it, do it now. */
2875 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2876 cstate
->off_linkhdr
.reg
== -1)
2877 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2880 * For link-layer types that have a variable-length header
2881 * preceding the link-layer header, generate code to load
2882 * the offset of the link-layer header into the register
2883 * assigned to that offset, if any.
2885 * XXX - this, and the next switch statement, won't handle
2886 * encapsulation of 802.11 or 802.11+radio information in
2887 * some other protocol stack. That's significantly more
2890 switch (cstate
->outermostlinktype
) {
2892 case DLT_PRISM_HEADER
:
2893 s
= gen_load_prism_llprefixlen(cstate
);
2896 case DLT_IEEE802_11_RADIO_AVS
:
2897 s
= gen_load_avs_llprefixlen(cstate
);
2900 case DLT_IEEE802_11_RADIO
:
2901 s
= gen_load_radiotap_llprefixlen(cstate
);
2905 s
= gen_load_ppi_llprefixlen(cstate
);
2914 * For link-layer types that have a variable-length link-layer
2915 * header, generate code to load the offset of the link-layer
2916 * payload into the register assigned to that offset, if any.
2918 switch (cstate
->outermostlinktype
) {
2920 case DLT_IEEE802_11
:
2921 case DLT_PRISM_HEADER
:
2922 case DLT_IEEE802_11_RADIO_AVS
:
2923 case DLT_IEEE802_11_RADIO
:
2925 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2930 * If there is no initialization yet and we need variable
2931 * length offsets for VLAN, initialize them to zero
2933 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2936 if (cstate
->off_linkpl
.reg
== -1)
2937 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2938 if (cstate
->off_linktype
.reg
== -1)
2939 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2941 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2943 s2
= new_stmt(cstate
, BPF_ST
);
2944 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2946 s2
= new_stmt(cstate
, BPF_ST
);
2947 s2
->s
.k
= cstate
->off_linktype
.reg
;
2952 * If we have any offset-loading code, append all the
2953 * existing statements in the block to those statements,
2954 * and make the resulting list the list of statements
2958 sappend(s
, b
->stmts
);
2963 static struct block
*
2964 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2966 struct slist
*s_load_dlt
;
2969 if (cstate
->linktype
== DLT_PPI
)
2971 /* Create the statements that check for the DLT
2973 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2974 s_load_dlt
->s
.k
= 4;
2976 b
= new_block(cstate
, JMP(BPF_JEQ
));
2978 b
->stmts
= s_load_dlt
;
2979 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2990 * Take an absolute offset, and:
2992 * if it has no variable part, return NULL;
2994 * if it has a variable part, generate code to load the register
2995 * containing that variable part into the X register, returning
2996 * a pointer to that code - if no register for that offset has
2997 * been allocated, allocate it first.
2999 * (The code to set that register will be generated later, but will
3000 * be placed earlier in the code sequence.)
3002 static struct slist
*
3003 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3007 if (off
->is_variable
) {
3008 if (off
->reg
== -1) {
3010 * We haven't yet assigned a register for the
3011 * variable part of the offset of the link-layer
3012 * header; allocate one.
3014 off
->reg
= alloc_reg(cstate
);
3018 * Load the register containing the variable part of the
3019 * offset of the link-layer header into the X register.
3021 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3026 * That offset isn't variable, there's no variable part,
3027 * so we don't need to generate any code.
3034 * Map an Ethernet type to the equivalent PPP type.
3037 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3045 case ETHERTYPE_IPV6
:
3046 ll_proto
= PPP_IPV6
;
3050 ll_proto
= PPP_DECNET
;
3053 case ETHERTYPE_ATALK
:
3054 ll_proto
= PPP_APPLE
;
3067 * I'm assuming the "Bridging PDU"s that go
3068 * over PPP are Spanning Tree Protocol
3071 ll_proto
= PPP_BRPDU
;
3082 * Generate any tests that, for encapsulation of a link-layer packet
3083 * inside another protocol stack, need to be done to check for those
3084 * link-layer packets (and that haven't already been done by a check
3085 * for that encapsulation).
3087 static struct block
*
3088 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3092 if (cstate
->is_geneve
)
3093 return gen_geneve_ll_check(cstate
);
3095 switch (cstate
->prevlinktype
) {
3099 * This is LANE-encapsulated Ethernet; check that the LANE
3100 * packet doesn't begin with an LE Control marker, i.e.
3101 * that it's data, not a control message.
3103 * (We've already generated a test for LANE.)
3105 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3111 * No such tests are necessary.
3119 * The three different values we should check for when checking for an
3120 * IPv6 packet with DLT_NULL.
3122 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3123 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3124 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3127 * Generate code to match a particular packet type by matching the
3128 * link-layer type field or fields in the 802.2 LLC header.
3130 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3131 * value, if <= ETHERMTU.
3133 static struct block
*
3134 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3136 struct block
*b0
, *b1
, *b2
;
3137 const char *description
;
3139 /* are we checking MPLS-encapsulated packets? */
3140 if (cstate
->label_stack_depth
> 0)
3141 return gen_mpls_linktype(cstate
, ll_proto
);
3143 switch (cstate
->linktype
) {
3146 case DLT_NETANALYZER
:
3147 case DLT_NETANALYZER_TRANSPARENT
:
3148 /* Geneve has an EtherType regardless of whether there is an
3150 if (!cstate
->is_geneve
)
3151 b0
= gen_prevlinkhdr_check(cstate
);
3155 b1
= gen_ether_linktype(cstate
, ll_proto
);
3165 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3169 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3173 case DLT_IEEE802_11
:
3174 case DLT_PRISM_HEADER
:
3175 case DLT_IEEE802_11_RADIO_AVS
:
3176 case DLT_IEEE802_11_RADIO
:
3179 * Check that we have a data frame.
3181 b0
= gen_check_802_11_data_frame(cstate
);
3184 * Now check for the specified link-layer type.
3186 b1
= gen_llc_linktype(cstate
, ll_proto
);
3193 * XXX - check for LLC frames.
3195 return gen_llc_linktype(cstate
, ll_proto
);
3200 * XXX - check for LLC PDUs, as per IEEE 802.5.
3202 return gen_llc_linktype(cstate
, ll_proto
);
3205 case DLT_ATM_RFC1483
:
3207 case DLT_IP_OVER_FC
:
3208 return gen_llc_linktype(cstate
, ll_proto
);
3213 * Check for an LLC-encapsulated version of this protocol;
3214 * if we were checking for LANE, linktype would no longer
3217 * Check for LLC encapsulation and then check the protocol.
3219 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3220 b1
= gen_llc_linktype(cstate
, ll_proto
);
3226 return gen_linux_sll_linktype(cstate
, ll_proto
);
3230 case DLT_SLIP_BSDOS
:
3233 * These types don't provide any type field; packets
3234 * are always IPv4 or IPv6.
3236 * XXX - for IPv4, check for a version number of 4, and,
3237 * for IPv6, check for a version number of 6?
3242 /* Check for a version number of 4. */
3243 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3245 case ETHERTYPE_IPV6
:
3246 /* Check for a version number of 6. */
3247 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3250 return gen_false(cstate
); /* always false */
3256 * Raw IPv4, so no type field.
3258 if (ll_proto
== ETHERTYPE_IP
)
3259 return gen_true(cstate
); /* always true */
3261 /* Checking for something other than IPv4; always false */
3262 return gen_false(cstate
);
3267 * Raw IPv6, so no type field.
3269 if (ll_proto
== ETHERTYPE_IPV6
)
3270 return gen_true(cstate
); /* always true */
3272 /* Checking for something other than IPv6; always false */
3273 return gen_false(cstate
);
3278 case DLT_PPP_SERIAL
:
3281 * We use Ethernet protocol types inside libpcap;
3282 * map them to the corresponding PPP protocol types.
3284 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3285 ethertype_to_ppptype(ll_proto
));
3290 * We use Ethernet protocol types inside libpcap;
3291 * map them to the corresponding PPP protocol types.
3297 * Also check for Van Jacobson-compressed IP.
3298 * XXX - do this for other forms of PPP?
3300 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3301 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3303 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3308 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3309 ethertype_to_ppptype(ll_proto
));
3319 return (gen_loopback_linktype(cstate
, AF_INET
));
3321 case ETHERTYPE_IPV6
:
3323 * AF_ values may, unfortunately, be platform-
3324 * dependent; AF_INET isn't, because everybody
3325 * used 4.2BSD's value, but AF_INET6 is, because
3326 * 4.2BSD didn't have a value for it (given that
3327 * IPv6 didn't exist back in the early 1980's),
3328 * and they all picked their own values.
3330 * This means that, if we're reading from a
3331 * savefile, we need to check for all the
3334 * If we're doing a live capture, we only need
3335 * to check for this platform's value; however,
3336 * Npcap uses 24, which isn't Windows's AF_INET6
3337 * value. (Given the multiple different values,
3338 * programs that read pcap files shouldn't be
3339 * checking for their platform's AF_INET6 value
3340 * anyway, they should check for all of the
3341 * possible values. and they might as well do
3342 * that even for live captures.)
3344 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3346 * Savefile - check for all three
3347 * possible IPv6 values.
3349 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3350 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3352 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3357 * Live capture, so we only need to
3358 * check for the value used on this
3363 * Npcap doesn't use Windows's AF_INET6,
3364 * as that collides with AF_IPX on
3365 * some BSDs (both have the value 23).
3366 * Instead, it uses 24.
3368 return (gen_loopback_linktype(cstate
, 24));
3371 return (gen_loopback_linktype(cstate
, AF_INET6
));
3372 #else /* AF_INET6 */
3374 * I guess this platform doesn't support
3375 * IPv6, so we just reject all packets.
3377 return gen_false(cstate
);
3378 #endif /* AF_INET6 */
3384 * Not a type on which we support filtering.
3385 * XXX - support those that have AF_ values
3386 * #defined on this platform, at least?
3388 return gen_false(cstate
);
3391 #ifdef HAVE_NET_PFVAR_H
3394 * af field is host byte order in contrast to the rest of
3397 if (ll_proto
== ETHERTYPE_IP
)
3398 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3400 else if (ll_proto
== ETHERTYPE_IPV6
)
3401 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3404 return gen_false(cstate
);
3406 #endif /* HAVE_NET_PFVAR_H */
3409 case DLT_ARCNET_LINUX
:
3411 * XXX should we check for first fragment if the protocol
3417 return gen_false(cstate
);
3419 case ETHERTYPE_IPV6
:
3420 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3424 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3426 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3432 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3434 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3439 case ETHERTYPE_REVARP
:
3440 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3443 case ETHERTYPE_ATALK
:
3444 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3451 case ETHERTYPE_ATALK
:
3452 return gen_true(cstate
);
3454 return gen_false(cstate
);
3460 * XXX - assumes a 2-byte Frame Relay header with
3461 * DLCI and flags. What if the address is longer?
3467 * Check for the special NLPID for IP.
3469 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3471 case ETHERTYPE_IPV6
:
3473 * Check for the special NLPID for IPv6.
3475 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3479 * Check for several OSI protocols.
3481 * Frame Relay packets typically have an OSI
3482 * NLPID at the beginning; we check for each
3485 * What we check for is the NLPID and a frame
3486 * control field of UI, i.e. 0x03 followed
3489 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3490 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3491 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3497 return gen_false(cstate
);
3502 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3504 case DLT_JUNIPER_MFR
:
3505 case DLT_JUNIPER_MLFR
:
3506 case DLT_JUNIPER_MLPPP
:
3507 case DLT_JUNIPER_ATM1
:
3508 case DLT_JUNIPER_ATM2
:
3509 case DLT_JUNIPER_PPPOE
:
3510 case DLT_JUNIPER_PPPOE_ATM
:
3511 case DLT_JUNIPER_GGSN
:
3512 case DLT_JUNIPER_ES
:
3513 case DLT_JUNIPER_MONITOR
:
3514 case DLT_JUNIPER_SERVICES
:
3515 case DLT_JUNIPER_ETHER
:
3516 case DLT_JUNIPER_PPP
:
3517 case DLT_JUNIPER_FRELAY
:
3518 case DLT_JUNIPER_CHDLC
:
3519 case DLT_JUNIPER_VP
:
3520 case DLT_JUNIPER_ST
:
3521 case DLT_JUNIPER_ISM
:
3522 case DLT_JUNIPER_VS
:
3523 case DLT_JUNIPER_SRX_E2E
:
3524 case DLT_JUNIPER_FIBRECHANNEL
:
3525 case DLT_JUNIPER_ATM_CEMIC
:
3527 /* just lets verify the magic number for now -
3528 * on ATM we may have up to 6 different encapsulations on the wire
3529 * and need a lot of heuristics to figure out that the payload
3532 * FIXME encapsulation specific BPF_ filters
3534 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3536 case DLT_BACNET_MS_TP
:
3537 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3540 return gen_ipnet_linktype(cstate
, ll_proto
);
3542 case DLT_LINUX_IRDA
:
3543 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3546 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3549 case DLT_MTP2_WITH_PHDR
:
3550 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3553 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3556 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3558 case DLT_LINUX_LAPD
:
3559 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3561 case DLT_USB_FREEBSD
:
3563 case DLT_USB_LINUX_MMAPPED
:
3565 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3567 case DLT_BLUETOOTH_HCI_H4
:
3568 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3569 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3572 case DLT_CAN_SOCKETCAN
:
3573 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3575 case DLT_IEEE802_15_4
:
3576 case DLT_IEEE802_15_4_LINUX
:
3577 case DLT_IEEE802_15_4_NONASK_PHY
:
3578 case DLT_IEEE802_15_4_NOFCS
:
3579 case DLT_IEEE802_15_4_TAP
:
3580 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3582 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3583 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3586 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3589 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3591 case DLT_IPMB_KONTRON
:
3592 case DLT_IPMB_LINUX
:
3593 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3596 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3599 /* Using the fixed-size NFLOG header it is possible to tell only
3600 * the address family of the packet, other meaningful data is
3601 * either missing or behind TLVs.
3603 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3607 * Does this link-layer header type have a field
3608 * indicating the type of the next protocol? If
3609 * so, off_linktype.constant_part will be the offset of that
3610 * field in the packet; if not, it will be OFFSET_NOT_SET.
3612 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3614 * Yes; assume it's an Ethernet type. (If
3615 * it's not, it needs to be handled specially
3618 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3622 * No; report an error.
3624 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3625 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3633 * Check for an LLC SNAP packet with a given organization code and
3634 * protocol type; we check the entire contents of the 802.2 LLC and
3635 * snap headers, checking for DSAP and SSAP of SNAP and a control
3636 * field of 0x03 in the LLC header, and for the specified organization
3637 * code and protocol type in the SNAP header.
3639 static struct block
*
3640 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3642 u_char snapblock
[8];
3644 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3645 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3646 snapblock
[2] = 0x03; /* control = UI */
3647 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3648 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3649 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3650 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3651 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3652 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3656 * Generate code to match frames with an LLC header.
3658 static struct block
*
3659 gen_llc_internal(compiler_state_t
*cstate
)
3661 struct block
*b0
, *b1
;
3663 switch (cstate
->linktype
) {
3667 * We check for an Ethernet type field less than
3668 * 1500, which means it's an 802.3 length field.
3670 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3674 * Now check for the purported DSAP and SSAP not being
3675 * 0xFF, to rule out NetWare-over-802.3.
3677 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3684 * We check for LLC traffic.
3686 b0
= gen_atmtype_llc(cstate
);
3689 case DLT_IEEE802
: /* Token Ring */
3691 * XXX - check for LLC frames.
3693 return gen_true(cstate
);
3697 * XXX - check for LLC frames.
3699 return gen_true(cstate
);
3701 case DLT_ATM_RFC1483
:
3703 * For LLC encapsulation, these are defined to have an
3706 * For VC encapsulation, they don't, but there's no
3707 * way to check for that; the protocol used on the VC
3708 * is negotiated out of band.
3710 return gen_true(cstate
);
3712 case DLT_IEEE802_11
:
3713 case DLT_PRISM_HEADER
:
3714 case DLT_IEEE802_11_RADIO
:
3715 case DLT_IEEE802_11_RADIO_AVS
:
3718 * Check that we have a data frame.
3720 b0
= gen_check_802_11_data_frame(cstate
);
3724 bpf_error(cstate
, "'llc' not supported for %s",
3725 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3731 gen_llc(compiler_state_t
*cstate
)
3734 * Catch errors reported by us and routines below us, and return NULL
3737 if (setjmp(cstate
->top_ctx
))
3740 return gen_llc_internal(cstate
);
3744 gen_llc_i(compiler_state_t
*cstate
)
3746 struct block
*b0
, *b1
;
3750 * Catch errors reported by us and routines below us, and return NULL
3753 if (setjmp(cstate
->top_ctx
))
3757 * Check whether this is an LLC frame.
3759 b0
= gen_llc_internal(cstate
);
3762 * Load the control byte and test the low-order bit; it must
3763 * be clear for I frames.
3765 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3766 b1
= new_block(cstate
, JMP(BPF_JSET
));
3775 gen_llc_s(compiler_state_t
*cstate
)
3777 struct block
*b0
, *b1
;
3780 * Catch errors reported by us and routines below us, and return NULL
3783 if (setjmp(cstate
->top_ctx
))
3787 * Check whether this is an LLC frame.
3789 b0
= gen_llc_internal(cstate
);
3792 * Now compare the low-order 2 bit of the control byte against
3793 * the appropriate value for S frames.
3795 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3801 gen_llc_u(compiler_state_t
*cstate
)
3803 struct block
*b0
, *b1
;
3806 * Catch errors reported by us and routines below us, and return NULL
3809 if (setjmp(cstate
->top_ctx
))
3813 * Check whether this is an LLC frame.
3815 b0
= gen_llc_internal(cstate
);
3818 * Now compare the low-order 2 bit of the control byte against
3819 * the appropriate value for U frames.
3821 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3827 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3829 struct block
*b0
, *b1
;
3832 * Catch errors reported by us and routines below us, and return NULL
3835 if (setjmp(cstate
->top_ctx
))
3839 * Check whether this is an LLC frame.
3841 b0
= gen_llc_internal(cstate
);
3844 * Now check for an S frame with the appropriate type.
3846 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3852 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3854 struct block
*b0
, *b1
;
3857 * Catch errors reported by us and routines below us, and return NULL
3860 if (setjmp(cstate
->top_ctx
))
3864 * Check whether this is an LLC frame.
3866 b0
= gen_llc_internal(cstate
);
3869 * Now check for a U frame with the appropriate type.
3871 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3877 * Generate code to match a particular packet type, for link-layer types
3878 * using 802.2 LLC headers.
3880 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3881 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3883 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3884 * value, if <= ETHERMTU. We use that to determine whether to
3885 * match the DSAP or both DSAP and LSAP or to check the OUI and
3886 * protocol ID in a SNAP header.
3888 static struct block
*
3889 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3892 * XXX - handle token-ring variable-length header.
3898 case LLCSAP_NETBEUI
:
3900 * XXX - should we check both the DSAP and the
3901 * SSAP, like this, or should we check just the
3902 * DSAP, as we do for other SAP values?
3904 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3905 ((ll_proto
<< 8) | ll_proto
));
3909 * XXX - are there ever SNAP frames for IPX on
3910 * non-Ethernet 802.x networks?
3912 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3914 case ETHERTYPE_ATALK
:
3916 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3917 * SNAP packets with an organization code of
3918 * 0x080007 (Apple, for Appletalk) and a protocol
3919 * type of ETHERTYPE_ATALK (Appletalk).
3921 * XXX - check for an organization code of
3922 * encapsulated Ethernet as well?
3924 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3928 * XXX - we don't have to check for IPX 802.3
3929 * here, but should we check for the IPX Ethertype?
3931 if (ll_proto
<= ETHERMTU
) {
3933 * This is an LLC SAP value, so check
3936 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
3939 * This is an Ethernet type; we assume that it's
3940 * unlikely that it'll appear in the right place
3941 * at random, and therefore check only the
3942 * location that would hold the Ethernet type
3943 * in a SNAP frame with an organization code of
3944 * 0x000000 (encapsulated Ethernet).
3946 * XXX - if we were to check for the SNAP DSAP and
3947 * LSAP, as per XXX, and were also to check for an
3948 * organization code of 0x000000 (encapsulated
3949 * Ethernet), we'd do
3951 * return gen_snap(cstate, 0x000000, ll_proto);
3953 * here; for now, we don't, as per the above.
3954 * I don't know whether it's worth the extra CPU
3955 * time to do the right check or not.
3957 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
3962 static struct block
*
3963 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3964 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
3966 struct block
*b0
, *b1
;
3980 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
3981 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
3987 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
3988 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
3993 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3997 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4001 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4005 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4009 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4013 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4020 b0
= gen_linktype(cstate
, ll_proto
);
4021 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4027 static struct block
*
4028 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4029 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4032 struct block
*b0
, *b1
;
4047 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4048 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4054 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4055 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4060 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4064 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4068 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4072 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4076 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4080 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4087 /* this order is important */
4088 a
= (uint32_t *)addr
;
4089 m
= (uint32_t *)mask
;
4090 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4091 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4093 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4095 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4097 b0
= gen_linktype(cstate
, ll_proto
);
4103 static struct block
*
4104 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4106 register struct block
*b0
, *b1
;
4110 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4113 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4116 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4117 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4123 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4124 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4129 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4133 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4137 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4141 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4145 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4149 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4157 * Like gen_ehostop, but for DLT_FDDI
4159 static struct block
*
4160 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4162 struct block
*b0
, *b1
;
4166 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4169 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4172 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4173 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4179 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4180 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4185 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4189 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4193 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4197 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4201 bpf_error(cstate
, "'ra' is only supported on 802.11");
4205 bpf_error(cstate
, "'ta' is only supported on 802.11");
4213 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4215 static struct block
*
4216 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4218 register struct block
*b0
, *b1
;
4222 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4225 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4228 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4229 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4235 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4236 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4241 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4245 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4249 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4253 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4257 bpf_error(cstate
, "'ra' is only supported on 802.11");
4261 bpf_error(cstate
, "'ta' is only supported on 802.11");
4269 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4270 * various 802.11 + radio headers.
4272 static struct block
*
4273 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4275 register struct block
*b0
, *b1
, *b2
;
4276 register struct slist
*s
;
4278 #ifdef ENABLE_WLAN_FILTERING_PATCH
4281 * We need to disable the optimizer because the optimizer is buggy
4282 * and wipes out some LD instructions generated by the below
4283 * code to validate the Frame Control bits
4285 cstate
->no_optimize
= 1;
4286 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4293 * For control frames, there is no SA.
4295 * For management frames, SA is at an
4296 * offset of 10 from the beginning of
4299 * For data frames, SA is at an offset
4300 * of 10 from the beginning of the packet
4301 * if From DS is clear, at an offset of
4302 * 16 from the beginning of the packet
4303 * if From DS is set and To DS is clear,
4304 * and an offset of 24 from the beginning
4305 * of the packet if From DS is set and To DS
4310 * Generate the tests to be done for data frames
4313 * First, check for To DS set, i.e. check "link[1] & 0x01".
4315 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4316 b1
= new_block(cstate
, JMP(BPF_JSET
));
4317 b1
->s
.k
= 0x01; /* To DS */
4321 * If To DS is set, the SA is at 24.
4323 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4327 * Now, check for To DS not set, i.e. check
4328 * "!(link[1] & 0x01)".
4330 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4331 b2
= new_block(cstate
, JMP(BPF_JSET
));
4332 b2
->s
.k
= 0x01; /* To DS */
4337 * If To DS is not set, the SA is at 16.
4339 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4343 * Now OR together the last two checks. That gives
4344 * the complete set of checks for data frames with
4350 * Now check for From DS being set, and AND that with
4351 * the ORed-together checks.
4353 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4354 b1
= new_block(cstate
, JMP(BPF_JSET
));
4355 b1
->s
.k
= 0x02; /* From DS */
4360 * Now check for data frames with From DS not set.
4362 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4363 b2
= new_block(cstate
, JMP(BPF_JSET
));
4364 b2
->s
.k
= 0x02; /* From DS */
4369 * If From DS isn't set, the SA is at 10.
4371 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4375 * Now OR together the checks for data frames with
4376 * From DS not set and for data frames with From DS
4377 * set; that gives the checks done for data frames.
4382 * Now check for a data frame.
4383 * I.e, check "link[0] & 0x08".
4385 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4386 b1
= new_block(cstate
, JMP(BPF_JSET
));
4391 * AND that with the checks done for data frames.
4396 * If the high-order bit of the type value is 0, this
4397 * is a management frame.
4398 * I.e, check "!(link[0] & 0x08)".
4400 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4401 b2
= new_block(cstate
, JMP(BPF_JSET
));
4407 * For management frames, the SA is at 10.
4409 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4413 * OR that with the checks done for data frames.
4414 * That gives the checks done for management and
4420 * If the low-order bit of the type value is 1,
4421 * this is either a control frame or a frame
4422 * with a reserved type, and thus not a
4425 * I.e., check "!(link[0] & 0x04)".
4427 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4428 b1
= new_block(cstate
, JMP(BPF_JSET
));
4434 * AND that with the checks for data and management
4444 * For control frames, there is no DA.
4446 * For management frames, DA is at an
4447 * offset of 4 from the beginning of
4450 * For data frames, DA is at an offset
4451 * of 4 from the beginning of the packet
4452 * if To DS is clear and at an offset of
4453 * 16 from the beginning of the packet
4458 * Generate the tests to be done for data frames.
4460 * First, check for To DS set, i.e. "link[1] & 0x01".
4462 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4463 b1
= new_block(cstate
, JMP(BPF_JSET
));
4464 b1
->s
.k
= 0x01; /* To DS */
4468 * If To DS is set, the DA is at 16.
4470 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4474 * Now, check for To DS not set, i.e. check
4475 * "!(link[1] & 0x01)".
4477 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4478 b2
= new_block(cstate
, JMP(BPF_JSET
));
4479 b2
->s
.k
= 0x01; /* To DS */
4484 * If To DS is not set, the DA is at 4.
4486 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4490 * Now OR together the last two checks. That gives
4491 * the complete set of checks for data frames.
4496 * Now check for a data frame.
4497 * I.e, check "link[0] & 0x08".
4499 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4500 b1
= new_block(cstate
, JMP(BPF_JSET
));
4505 * AND that with the checks done for data frames.
4510 * If the high-order bit of the type value is 0, this
4511 * is a management frame.
4512 * I.e, check "!(link[0] & 0x08)".
4514 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4515 b2
= new_block(cstate
, JMP(BPF_JSET
));
4521 * For management frames, the DA is at 4.
4523 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4527 * OR that with the checks done for data frames.
4528 * That gives the checks done for management and
4534 * If the low-order bit of the type value is 1,
4535 * this is either a control frame or a frame
4536 * with a reserved type, and thus not a
4539 * I.e., check "!(link[0] & 0x04)".
4541 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4542 b1
= new_block(cstate
, JMP(BPF_JSET
));
4548 * AND that with the checks for data and management
4555 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4556 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4562 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4563 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4568 * XXX - add BSSID keyword?
4571 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4575 * Not present in CTS or ACK control frames.
4577 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4578 IEEE80211_FC0_TYPE_MASK
);
4580 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4581 IEEE80211_FC0_SUBTYPE_MASK
);
4583 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4584 IEEE80211_FC0_SUBTYPE_MASK
);
4588 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4594 * Not present in control frames.
4596 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4597 IEEE80211_FC0_TYPE_MASK
);
4599 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4605 * Present only if the direction mask has both "From DS"
4606 * and "To DS" set. Neither control frames nor management
4607 * frames should have both of those set, so we don't
4608 * check the frame type.
4610 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4611 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4612 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4618 * Not present in management frames; addr1 in other
4623 * If the high-order bit of the type value is 0, this
4624 * is a management frame.
4625 * I.e, check "(link[0] & 0x08)".
4627 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4628 b1
= new_block(cstate
, JMP(BPF_JSET
));
4635 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4638 * AND that with the check of addr1.
4645 * Not present in management frames; addr2, if present,
4650 * Not present in CTS or ACK control frames.
4652 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4653 IEEE80211_FC0_TYPE_MASK
);
4655 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4656 IEEE80211_FC0_SUBTYPE_MASK
);
4658 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4659 IEEE80211_FC0_SUBTYPE_MASK
);
4665 * If the high-order bit of the type value is 0, this
4666 * is a management frame.
4667 * I.e, check "(link[0] & 0x08)".
4669 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4670 b1
= new_block(cstate
, JMP(BPF_JSET
));
4675 * AND that with the check for frames other than
4676 * CTS and ACK frames.
4683 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4692 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4693 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4694 * as the RFC states.)
4696 static struct block
*
4697 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4699 register struct block
*b0
, *b1
;
4703 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4706 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4709 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4710 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4716 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4717 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4722 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4726 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4730 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4734 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4738 bpf_error(cstate
, "'ra' is only supported on 802.11");
4742 bpf_error(cstate
, "'ta' is only supported on 802.11");
4750 * This is quite tricky because there may be pad bytes in front of the
4751 * DECNET header, and then there are two possible data packet formats that
4752 * carry both src and dst addresses, plus 5 packet types in a format that
4753 * carries only the src node, plus 2 types that use a different format and
4754 * also carry just the src node.
4758 * Instead of doing those all right, we just look for data packets with
4759 * 0 or 1 bytes of padding. If you want to look at other packets, that
4760 * will require a lot more hacking.
4762 * To add support for filtering on DECNET "areas" (network numbers)
4763 * one would want to add a "mask" argument to this routine. That would
4764 * make the filter even more inefficient, although one could be clever
4765 * and not generate masking instructions if the mask is 0xFFFF.
4767 static struct block
*
4768 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4770 struct block
*b0
, *b1
, *b2
, *tmp
;
4771 u_int offset_lh
; /* offset if long header is received */
4772 u_int offset_sh
; /* offset if short header is received */
4777 offset_sh
= 1; /* follows flags */
4778 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4782 offset_sh
= 3; /* follows flags, dstnode */
4783 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4787 /* Inefficient because we do our Calvinball dance twice */
4788 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4789 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4795 /* Inefficient because we do our Calvinball dance twice */
4796 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4797 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4802 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4806 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4810 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4814 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4818 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4822 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4829 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4830 /* Check for pad = 1, long header case */
4831 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4832 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4833 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4834 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4836 /* Check for pad = 0, long header case */
4837 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4839 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4840 (bpf_u_int32
)ntohs((u_short
)addr
));
4843 /* Check for pad = 1, short header case */
4844 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4845 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4846 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4847 (bpf_u_int32
)ntohs((u_short
)addr
));
4850 /* Check for pad = 0, short header case */
4851 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4853 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4854 (bpf_u_int32
)ntohs((u_short
)addr
));
4858 /* Combine with test for cstate->linktype */
4864 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4865 * test the bottom-of-stack bit, and then check the version number
4866 * field in the IP header.
4868 static struct block
*
4869 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4871 struct block
*b0
, *b1
;
4876 /* match the bottom-of-stack bit */
4877 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4878 /* match the IPv4 version number */
4879 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4883 case ETHERTYPE_IPV6
:
4884 /* match the bottom-of-stack bit */
4885 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4886 /* match the IPv4 version number */
4887 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4892 /* FIXME add other L3 proto IDs */
4893 bpf_error(cstate
, "unsupported protocol over mpls");
4898 static struct block
*
4899 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4900 int proto
, int dir
, int type
)
4902 struct block
*b0
, *b1
;
4903 const char *typestr
;
4913 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4915 * Only check for non-IPv4 addresses if we're not
4916 * checking MPLS-encapsulated packets.
4918 if (cstate
->label_stack_depth
== 0) {
4919 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4921 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4927 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4930 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4933 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4936 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4939 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4942 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4945 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4948 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4951 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4954 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4957 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4960 return gen_dnhostop(cstate
, addr
, dir
);
4963 bpf_error(cstate
, "LAT host filtering not implemented");
4966 bpf_error(cstate
, "SCA host filtering not implemented");
4969 bpf_error(cstate
, "MOPRC host filtering not implemented");
4972 bpf_error(cstate
, "MOPDL host filtering not implemented");
4975 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4978 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4981 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4984 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4987 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4990 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4993 bpf_error(cstate
, "AARP host filtering not implemented");
4996 bpf_error(cstate
, "ISO host filtering not implemented");
4999 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5002 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5005 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5008 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5011 bpf_error(cstate
, "IPX host filtering not implemented");
5014 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5017 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5020 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5023 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5026 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5029 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5032 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5035 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5038 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5041 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5050 static struct block
*
5051 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5052 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5054 const char *typestr
;
5064 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5067 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5070 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5073 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5076 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5079 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5082 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5085 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5088 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5091 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5094 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5097 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5100 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5103 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5106 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5109 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5112 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5115 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5118 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5121 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5124 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5127 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5130 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5133 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5136 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5139 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5142 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5145 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5148 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5151 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5154 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5157 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5160 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5163 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5166 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5169 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5172 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5175 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5178 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5181 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5191 static struct block
*
5192 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5193 struct addrinfo
*alist
, int proto
, int dir
)
5195 struct block
*b0
, *b1
, *tmp
;
5196 struct addrinfo
*ai
;
5197 struct sockaddr_in
*sin
;
5200 bpf_error(cstate
, "direction applied to 'gateway'");
5207 switch (cstate
->linktype
) {
5209 case DLT_NETANALYZER
:
5210 case DLT_NETANALYZER_TRANSPARENT
:
5211 b1
= gen_prevlinkhdr_check(cstate
);
5212 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5217 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5220 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5222 case DLT_IEEE802_11
:
5223 case DLT_PRISM_HEADER
:
5224 case DLT_IEEE802_11_RADIO_AVS
:
5225 case DLT_IEEE802_11_RADIO
:
5227 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5231 * This is LLC-multiplexed traffic; if it were
5232 * LANE, cstate->linktype would have been set to
5236 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5238 case DLT_IP_OVER_FC
:
5239 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5243 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5246 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5248 * Does it have an address?
5250 if (ai
->ai_addr
!= NULL
) {
5252 * Yes. Is it an IPv4 address?
5254 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5256 * Generate an entry for it.
5258 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5259 tmp
= gen_host(cstate
,
5260 ntohl(sin
->sin_addr
.s_addr
),
5261 0xffffffff, proto
, Q_OR
, Q_HOST
);
5263 * Is it the *first* IPv4 address?
5267 * Yes, so start with it.
5272 * No, so OR it into the
5284 * No IPv4 addresses found.
5292 bpf_error(cstate
, "illegal modifier of 'gateway'");
5297 static struct block
*
5298 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5306 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5307 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5312 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5313 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5318 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5319 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5324 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5327 #ifndef IPPROTO_IGMP
5328 #define IPPROTO_IGMP 2
5332 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5335 #ifndef IPPROTO_IGRP
5336 #define IPPROTO_IGRP 9
5339 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5343 #define IPPROTO_PIM 103
5347 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5348 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5352 #ifndef IPPROTO_VRRP
5353 #define IPPROTO_VRRP 112
5357 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5360 #ifndef IPPROTO_CARP
5361 #define IPPROTO_CARP 112
5365 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5369 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5373 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5377 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5381 bpf_error(cstate
, "link layer applied in wrong context");
5384 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5388 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5392 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5396 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5400 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5404 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5408 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5412 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5415 #ifndef IPPROTO_ICMPV6
5416 #define IPPROTO_ICMPV6 58
5419 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5423 #define IPPROTO_AH 51
5426 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5427 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5432 #define IPPROTO_ESP 50
5435 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5436 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5441 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5445 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5449 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5452 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5453 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5454 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5456 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5458 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5460 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5464 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5465 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5466 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5468 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5470 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5472 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5476 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5477 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5478 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5480 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5485 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5486 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5491 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5492 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5494 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5496 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5501 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5502 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5507 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5508 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5513 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5517 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5521 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5525 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5529 bpf_error(cstate
, "'radio' is not a valid protocol type");
5538 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5541 * Catch errors reported by us and routines below us, and return NULL
5544 if (setjmp(cstate
->top_ctx
))
5547 return gen_proto_abbrev_internal(cstate
, proto
);
5550 static struct block
*
5551 gen_ipfrag(compiler_state_t
*cstate
)
5556 /* not IPv4 frag other than the first frag */
5557 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5558 b
= new_block(cstate
, JMP(BPF_JSET
));
5567 * Generate a comparison to a port value in the transport-layer header
5568 * at the specified offset from the beginning of that header.
5570 * XXX - this handles a variable-length prefix preceding the link-layer
5571 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5572 * variable-length link-layer headers (such as Token Ring or 802.11
5575 static struct block
*
5576 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5578 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5581 static struct block
*
5582 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5584 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5587 static struct block
*
5588 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5590 struct block
*b0
, *b1
, *tmp
;
5592 /* ip proto 'proto' and not a fragment other than the first fragment */
5593 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5594 b0
= gen_ipfrag(cstate
);
5599 b1
= gen_portatom(cstate
, 0, port
);
5603 b1
= gen_portatom(cstate
, 2, port
);
5607 tmp
= gen_portatom(cstate
, 0, port
);
5608 b1
= gen_portatom(cstate
, 2, port
);
5614 tmp
= gen_portatom(cstate
, 0, port
);
5615 b1
= gen_portatom(cstate
, 2, port
);
5620 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5624 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5628 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5632 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5636 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5640 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5652 static struct block
*
5653 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5655 struct block
*b0
, *b1
, *tmp
;
5660 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5661 * not LLC encapsulation with LLCSAP_IP.
5663 * For IEEE 802 networks - which includes 802.5 token ring
5664 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5665 * says that SNAP encapsulation is used, not LLC encapsulation
5668 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5669 * RFC 2225 say that SNAP encapsulation is used, not LLC
5670 * encapsulation with LLCSAP_IP.
5672 * So we always check for ETHERTYPE_IP.
5674 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5680 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5684 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5685 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5687 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5699 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5701 struct block
*b0
, *b1
, *tmp
;
5703 /* ip6 proto 'proto' */
5704 /* XXX - catch the first fragment of a fragmented packet? */
5705 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5709 b1
= gen_portatom6(cstate
, 0, port
);
5713 b1
= gen_portatom6(cstate
, 2, port
);
5717 tmp
= gen_portatom6(cstate
, 0, port
);
5718 b1
= gen_portatom6(cstate
, 2, port
);
5724 tmp
= gen_portatom6(cstate
, 0, port
);
5725 b1
= gen_portatom6(cstate
, 2, port
);
5737 static struct block
*
5738 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5740 struct block
*b0
, *b1
, *tmp
;
5742 /* link proto ip6 */
5743 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5749 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5753 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5754 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5756 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5767 /* gen_portrange code */
5768 static struct block
*
5769 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5772 struct block
*b1
, *b2
;
5776 * Reverse the order of the ports, so v1 is the lower one.
5785 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5786 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5793 static struct block
*
5794 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5795 bpf_u_int32 proto
, int dir
)
5797 struct block
*b0
, *b1
, *tmp
;
5799 /* ip proto 'proto' and not a fragment other than the first fragment */
5800 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5801 b0
= gen_ipfrag(cstate
);
5806 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5810 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5814 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5815 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5821 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5822 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5827 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5831 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5835 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5839 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5843 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5847 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5859 static struct block
*
5860 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5863 struct block
*b0
, *b1
, *tmp
;
5866 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5872 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5877 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5878 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5880 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5891 static struct block
*
5892 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5895 struct block
*b1
, *b2
;
5899 * Reverse the order of the ports, so v1 is the lower one.
5908 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5909 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5916 static struct block
*
5917 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5918 bpf_u_int32 proto
, int dir
)
5920 struct block
*b0
, *b1
, *tmp
;
5922 /* ip6 proto 'proto' */
5923 /* XXX - catch the first fragment of a fragmented packet? */
5924 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5928 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5932 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5936 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5937 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5943 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5944 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5956 static struct block
*
5957 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5960 struct block
*b0
, *b1
, *tmp
;
5962 /* link proto ip6 */
5963 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5969 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5974 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5975 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5977 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5989 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5998 v
= pcap_nametoproto(name
);
5999 if (v
== PROTO_UNDEF
)
6000 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6004 /* XXX should look up h/w protocol type based on cstate->linktype */
6005 v
= pcap_nametoeproto(name
);
6006 if (v
== PROTO_UNDEF
) {
6007 v
= pcap_nametollc(name
);
6008 if (v
== PROTO_UNDEF
)
6009 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6014 if (strcmp(name
, "esis") == 0)
6016 else if (strcmp(name
, "isis") == 0)
6018 else if (strcmp(name
, "clnp") == 0)
6021 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6033 gen_joinsp(struct stmt
**s
, int n
)
6039 static struct block
*
6040 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6042 #ifdef NO_PROTOCHAIN
6043 return gen_proto(cstate
, v
, proto
);
6045 struct block
*b0
, *b
;
6046 struct slist
*s
[100];
6047 int fix2
, fix3
, fix4
, fix5
;
6048 int ahcheck
, again
, end
;
6050 int reg2
= alloc_reg(cstate
);
6052 memset(s
, 0, sizeof(s
));
6053 fix3
= fix4
= fix5
= 0;
6060 b0
= gen_protochain(cstate
, v
, Q_IP
);
6061 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6065 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6070 * We don't handle variable-length prefixes before the link-layer
6071 * header, or variable-length link-layer headers, here yet.
6072 * We might want to add BPF instructions to do the protochain
6073 * work, to simplify that and, on platforms that have a BPF
6074 * interpreter with the new instructions, let the filtering
6075 * be done in the kernel. (We already require a modified BPF
6076 * engine to do the protochain stuff, to support backward
6077 * branches, and backward branch support is unlikely to appear
6078 * in kernel BPF engines.)
6080 if (cstate
->off_linkpl
.is_variable
)
6081 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6083 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
6086 * s[0] is a dummy entry to protect other BPF insn from damage
6087 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6088 * hard to find interdependency made by jump table fixup.
6091 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6096 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6099 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6100 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6102 /* X = ip->ip_hl << 2 */
6103 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6104 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6109 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6111 /* A = ip6->ip_nxt */
6112 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6113 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6115 /* X = sizeof(struct ip6_hdr) */
6116 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6122 bpf_error(cstate
, "unsupported proto to gen_protochain");
6126 /* again: if (A == v) goto end; else fall through; */
6128 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6130 s
[i
]->s
.jt
= NULL
; /*later*/
6131 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6135 #ifndef IPPROTO_NONE
6136 #define IPPROTO_NONE 59
6138 /* if (A == IPPROTO_NONE) goto end */
6139 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6140 s
[i
]->s
.jt
= NULL
; /*later*/
6141 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6142 s
[i
]->s
.k
= IPPROTO_NONE
;
6143 s
[fix5
]->s
.jf
= s
[i
];
6147 if (proto
== Q_IPV6
) {
6148 int v6start
, v6end
, v6advance
, j
;
6151 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6152 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6153 s
[i
]->s
.jt
= NULL
; /*later*/
6154 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6155 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6156 s
[fix2
]->s
.jf
= s
[i
];
6158 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6159 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6160 s
[i
]->s
.jt
= NULL
; /*later*/
6161 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6162 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6164 /* if (A == IPPROTO_ROUTING) goto v6advance */
6165 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6166 s
[i
]->s
.jt
= NULL
; /*later*/
6167 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6168 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6170 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6171 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6172 s
[i
]->s
.jt
= NULL
; /*later*/
6173 s
[i
]->s
.jf
= NULL
; /*later*/
6174 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6184 * A = P[X + packet head];
6185 * X = X + (P[X + packet head + 1] + 1) * 8;
6187 /* A = P[X + packet head] */
6188 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6189 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6192 s
[i
] = new_stmt(cstate
, BPF_ST
);
6195 /* A = P[X + packet head + 1]; */
6196 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6197 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6200 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6204 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6208 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6212 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6215 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6219 /* goto again; (must use BPF_JA for backward jump) */
6220 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6221 s
[i
]->s
.k
= again
- i
- 1;
6222 s
[i
- 1]->s
.jf
= s
[i
];
6226 for (j
= v6start
; j
<= v6end
; j
++)
6227 s
[j
]->s
.jt
= s
[v6advance
];
6230 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6232 s
[fix2
]->s
.jf
= s
[i
];
6238 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6239 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6240 s
[i
]->s
.jt
= NULL
; /*later*/
6241 s
[i
]->s
.jf
= NULL
; /*later*/
6242 s
[i
]->s
.k
= IPPROTO_AH
;
6244 s
[fix3
]->s
.jf
= s
[ahcheck
];
6251 * X = X + (P[X + 1] + 2) * 4;
6254 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6256 /* A = P[X + packet head]; */
6257 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6258 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6261 s
[i
] = new_stmt(cstate
, BPF_ST
);
6265 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6268 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6272 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6274 /* A = P[X + packet head] */
6275 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6276 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6279 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6283 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6287 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6290 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6294 /* goto again; (must use BPF_JA for backward jump) */
6295 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6296 s
[i
]->s
.k
= again
- i
- 1;
6301 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6303 s
[fix2
]->s
.jt
= s
[end
];
6304 s
[fix4
]->s
.jf
= s
[end
];
6305 s
[fix5
]->s
.jt
= s
[end
];
6312 for (i
= 0; i
< max
- 1; i
++)
6313 s
[i
]->next
= s
[i
+ 1];
6314 s
[max
- 1]->next
= NULL
;
6319 b
= new_block(cstate
, JMP(BPF_JEQ
));
6320 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6323 free_reg(cstate
, reg2
);
6330 static struct block
*
6331 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6334 struct block
*b0
, *b1
;
6337 * A data frame has the 0x08 bit (b3) in the frame control field set
6338 * and the 0x04 bit (b2) clear.
6340 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6341 b0
= new_block(cstate
, JMP(BPF_JSET
));
6345 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6346 b1
= new_block(cstate
, JMP(BPF_JSET
));
6357 * Generate code that checks whether the packet is a packet for protocol
6358 * <proto> and whether the type field in that protocol's header has
6359 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6360 * IP packet and checks the protocol number in the IP header against <v>.
6362 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6363 * against Q_IP and Q_IPV6.
6365 static struct block
*
6366 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6368 struct block
*b0
, *b1
;
6373 if (dir
!= Q_DEFAULT
)
6374 bpf_error(cstate
, "direction applied to 'proto'");
6378 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6379 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6384 return gen_linktype(cstate
, v
);
6388 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6389 * not LLC encapsulation with LLCSAP_IP.
6391 * For IEEE 802 networks - which includes 802.5 token ring
6392 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6393 * says that SNAP encapsulation is used, not LLC encapsulation
6396 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6397 * RFC 2225 say that SNAP encapsulation is used, not LLC
6398 * encapsulation with LLCSAP_IP.
6400 * So we always check for ETHERTYPE_IP.
6402 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6404 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6406 b1
= gen_protochain(cstate
, v
, Q_IP
);
6412 bpf_error(cstate
, "arp does not encapsulate another protocol");
6416 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6420 bpf_error(cstate
, "'sctp proto' is bogus");
6424 bpf_error(cstate
, "'tcp proto' is bogus");
6428 bpf_error(cstate
, "'udp proto' is bogus");
6432 bpf_error(cstate
, "'icmp proto' is bogus");
6436 bpf_error(cstate
, "'igmp proto' is bogus");
6440 bpf_error(cstate
, "'igrp proto' is bogus");
6444 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6448 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6452 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6456 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6460 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6464 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6468 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6471 * Also check for a fragment header before the final
6474 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6475 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6477 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6480 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6486 bpf_error(cstate
, "'icmp6 proto' is bogus");
6490 bpf_error(cstate
, "'ah proto' is bogus");
6494 bpf_error(cstate
, "'ah proto' is bogus");
6498 bpf_error(cstate
, "'pim proto' is bogus");
6502 bpf_error(cstate
, "'vrrp proto' is bogus");
6506 bpf_error(cstate
, "'aarp proto' is bogus");
6510 switch (cstate
->linktype
) {
6514 * Frame Relay packets typically have an OSI
6515 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6516 * generates code to check for all the OSI
6517 * NLPIDs, so calling it and then adding a check
6518 * for the particular NLPID for which we're
6519 * looking is bogus, as we can just check for
6522 * What we check for is the NLPID and a frame
6523 * control field value of UI, i.e. 0x03 followed
6526 * XXX - assumes a 2-byte Frame Relay header with
6527 * DLCI and flags. What if the address is longer?
6529 * XXX - what about SNAP-encapsulated frames?
6531 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6536 * Cisco uses an Ethertype lookalike - for OSI,
6539 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6540 /* OSI in C-HDLC is stuffed with a fudge byte */
6541 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6546 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6547 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6553 bpf_error(cstate
, "'esis proto' is bogus");
6557 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6559 * 4 is the offset of the PDU type relative to the IS-IS
6562 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6567 bpf_error(cstate
, "'clnp proto' is not supported");
6571 bpf_error(cstate
, "'stp proto' is bogus");
6575 bpf_error(cstate
, "'ipx proto' is bogus");
6579 bpf_error(cstate
, "'netbeui proto' is bogus");
6583 bpf_error(cstate
, "'l1 proto' is bogus");
6587 bpf_error(cstate
, "'l2 proto' is bogus");
6591 bpf_error(cstate
, "'iih proto' is bogus");
6595 bpf_error(cstate
, "'snp proto' is bogus");
6599 bpf_error(cstate
, "'csnp proto' is bogus");
6603 bpf_error(cstate
, "'psnp proto' is bogus");
6607 bpf_error(cstate
, "'lsp proto' is bogus");
6611 bpf_error(cstate
, "'radio proto' is bogus");
6615 bpf_error(cstate
, "'carp proto' is bogus");
6626 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6628 int proto
= q
.proto
;
6632 bpf_u_int32 mask
, addr
;
6633 struct addrinfo
*res
, *res0
;
6634 struct sockaddr_in
*sin4
;
6637 struct sockaddr_in6
*sin6
;
6638 struct in6_addr mask128
;
6640 struct block
*b
, *tmp
;
6641 int port
, real_proto
;
6645 * Catch errors reported by us and routines below us, and return NULL
6648 if (setjmp(cstate
->top_ctx
))
6654 addr
= pcap_nametonetaddr(name
);
6656 bpf_error(cstate
, "unknown network '%s'", name
);
6657 /* Left justify network addr and calculate its network mask */
6659 while (addr
&& (addr
& 0xff000000) == 0) {
6663 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6667 if (proto
== Q_LINK
) {
6668 switch (cstate
->linktype
) {
6671 case DLT_NETANALYZER
:
6672 case DLT_NETANALYZER_TRANSPARENT
:
6673 eaddr
= pcap_ether_hostton(name
);
6676 "unknown ether host '%s'", name
);
6677 tmp
= gen_prevlinkhdr_check(cstate
);
6678 b
= gen_ehostop(cstate
, eaddr
, dir
);
6685 eaddr
= pcap_ether_hostton(name
);
6688 "unknown FDDI host '%s'", name
);
6689 b
= gen_fhostop(cstate
, eaddr
, dir
);
6694 eaddr
= pcap_ether_hostton(name
);
6697 "unknown token ring host '%s'", name
);
6698 b
= gen_thostop(cstate
, eaddr
, dir
);
6702 case DLT_IEEE802_11
:
6703 case DLT_PRISM_HEADER
:
6704 case DLT_IEEE802_11_RADIO_AVS
:
6705 case DLT_IEEE802_11_RADIO
:
6707 eaddr
= pcap_ether_hostton(name
);
6710 "unknown 802.11 host '%s'", name
);
6711 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6715 case DLT_IP_OVER_FC
:
6716 eaddr
= pcap_ether_hostton(name
);
6719 "unknown Fibre Channel host '%s'", name
);
6720 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6725 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6726 } else if (proto
== Q_DECNET
) {
6727 unsigned short dn_addr
;
6729 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6731 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6733 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6738 * I don't think DECNET hosts can be multihomed, so
6739 * there is no need to build up a list of addresses
6741 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6744 memset(&mask128
, 0xff, sizeof(mask128
));
6746 res0
= res
= pcap_nametoaddrinfo(name
);
6748 bpf_error(cstate
, "unknown host '%s'", name
);
6755 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6756 tproto
== Q_DEFAULT
) {
6762 for (res
= res0
; res
; res
= res
->ai_next
) {
6763 switch (res
->ai_family
) {
6766 if (tproto
== Q_IPV6
)
6770 sin4
= (struct sockaddr_in
*)
6772 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6773 0xffffffff, tproto
, dir
, q
.addr
);
6777 if (tproto6
== Q_IP
)
6780 sin6
= (struct sockaddr_in6
*)
6782 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6783 &mask128
, tproto6
, dir
, q
.addr
);
6796 bpf_error(cstate
, "unknown host '%s'%s", name
,
6797 (proto
== Q_DEFAULT
)
6799 : " for specified address family");
6805 if (proto
!= Q_DEFAULT
&&
6806 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6807 bpf_error(cstate
, "illegal qualifier of 'port'");
6808 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6809 bpf_error(cstate
, "unknown port '%s'", name
);
6810 if (proto
== Q_UDP
) {
6811 if (real_proto
== IPPROTO_TCP
)
6812 bpf_error(cstate
, "port '%s' is tcp", name
);
6813 else if (real_proto
== IPPROTO_SCTP
)
6814 bpf_error(cstate
, "port '%s' is sctp", name
);
6816 /* override PROTO_UNDEF */
6817 real_proto
= IPPROTO_UDP
;
6819 if (proto
== Q_TCP
) {
6820 if (real_proto
== IPPROTO_UDP
)
6821 bpf_error(cstate
, "port '%s' is udp", name
);
6823 else if (real_proto
== IPPROTO_SCTP
)
6824 bpf_error(cstate
, "port '%s' is sctp", name
);
6826 /* override PROTO_UNDEF */
6827 real_proto
= IPPROTO_TCP
;
6829 if (proto
== Q_SCTP
) {
6830 if (real_proto
== IPPROTO_UDP
)
6831 bpf_error(cstate
, "port '%s' is udp", name
);
6833 else if (real_proto
== IPPROTO_TCP
)
6834 bpf_error(cstate
, "port '%s' is tcp", name
);
6836 /* override PROTO_UNDEF */
6837 real_proto
= IPPROTO_SCTP
;
6840 bpf_error(cstate
, "illegal port number %d < 0", port
);
6842 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6843 b
= gen_port(cstate
, port
, real_proto
, dir
);
6844 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6848 if (proto
!= Q_DEFAULT
&&
6849 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6850 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6851 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6852 bpf_error(cstate
, "unknown port in range '%s'", name
);
6853 if (proto
== Q_UDP
) {
6854 if (real_proto
== IPPROTO_TCP
)
6855 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6856 else if (real_proto
== IPPROTO_SCTP
)
6857 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6859 /* override PROTO_UNDEF */
6860 real_proto
= IPPROTO_UDP
;
6862 if (proto
== Q_TCP
) {
6863 if (real_proto
== IPPROTO_UDP
)
6864 bpf_error(cstate
, "port in range '%s' is udp", name
);
6865 else if (real_proto
== IPPROTO_SCTP
)
6866 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6868 /* override PROTO_UNDEF */
6869 real_proto
= IPPROTO_TCP
;
6871 if (proto
== Q_SCTP
) {
6872 if (real_proto
== IPPROTO_UDP
)
6873 bpf_error(cstate
, "port in range '%s' is udp", name
);
6874 else if (real_proto
== IPPROTO_TCP
)
6875 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6877 /* override PROTO_UNDEF */
6878 real_proto
= IPPROTO_SCTP
;
6881 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6883 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6885 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6887 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6889 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6890 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6895 eaddr
= pcap_ether_hostton(name
);
6897 bpf_error(cstate
, "unknown ether host: %s", name
);
6899 res
= pcap_nametoaddrinfo(name
);
6902 bpf_error(cstate
, "unknown host '%s'", name
);
6903 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6907 bpf_error(cstate
, "unknown host '%s'", name
);
6910 bpf_error(cstate
, "'gateway' not supported in this configuration");
6914 real_proto
= lookup_proto(cstate
, name
, proto
);
6915 if (real_proto
>= 0)
6916 return gen_proto(cstate
, real_proto
, proto
, dir
);
6918 bpf_error(cstate
, "unknown protocol: %s", name
);
6921 real_proto
= lookup_proto(cstate
, name
, proto
);
6922 if (real_proto
>= 0)
6923 return gen_protochain(cstate
, real_proto
, proto
);
6925 bpf_error(cstate
, "unknown protocol: %s", name
);
6936 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6937 bpf_u_int32 masklen
, struct qual q
)
6939 register int nlen
, mlen
;
6943 * Catch errors reported by us and routines below us, and return NULL
6946 if (setjmp(cstate
->top_ctx
))
6949 nlen
= __pcap_atoin(s1
, &n
);
6951 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
6952 /* Promote short ipaddr */
6956 mlen
= __pcap_atoin(s2
, &m
);
6958 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
6959 /* Promote short ipaddr */
6962 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6965 /* Convert mask len to mask */
6967 bpf_error(cstate
, "mask length must be <= 32");
6970 * X << 32 is not guaranteed by C to be 0; it's
6975 m
= 0xffffffff << (32 - masklen
);
6977 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6984 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6987 bpf_error(cstate
, "Mask syntax for networks only");
6994 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7002 * Catch errors reported by us and routines below us, and return NULL
7005 if (setjmp(cstate
->top_ctx
))
7012 else if (q
.proto
== Q_DECNET
) {
7013 vlen
= __pcap_atodn(s
, &v
);
7015 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7017 vlen
= __pcap_atoin(s
, &v
);
7019 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7027 if (proto
== Q_DECNET
)
7028 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7029 else if (proto
== Q_LINK
) {
7030 bpf_error(cstate
, "illegal link layer address");
7033 if (s
== NULL
&& q
.addr
== Q_NET
) {
7034 /* Promote short net number */
7035 while (v
&& (v
& 0xff000000) == 0) {
7040 /* Promote short ipaddr */
7042 mask
<<= 32 - vlen
;
7044 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7049 proto
= IPPROTO_UDP
;
7050 else if (proto
== Q_TCP
)
7051 proto
= IPPROTO_TCP
;
7052 else if (proto
== Q_SCTP
)
7053 proto
= IPPROTO_SCTP
;
7054 else if (proto
== Q_DEFAULT
)
7055 proto
= PROTO_UNDEF
;
7057 bpf_error(cstate
, "illegal qualifier of 'port'");
7060 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7064 b
= gen_port(cstate
, v
, proto
, dir
);
7065 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7071 proto
= IPPROTO_UDP
;
7072 else if (proto
== Q_TCP
)
7073 proto
= IPPROTO_TCP
;
7074 else if (proto
== Q_SCTP
)
7075 proto
= IPPROTO_SCTP
;
7076 else if (proto
== Q_DEFAULT
)
7077 proto
= PROTO_UNDEF
;
7079 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7082 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7086 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7087 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7092 bpf_error(cstate
, "'gateway' requires a name");
7096 return gen_proto(cstate
, v
, proto
, dir
);
7099 return gen_protochain(cstate
, v
, proto
);
7114 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7115 bpf_u_int32 masklen
, struct qual q
)
7117 struct addrinfo
*res
;
7118 struct in6_addr
*addr
;
7119 struct in6_addr mask
;
7124 * Catch errors reported by us and routines below us, and return NULL
7127 if (setjmp(cstate
->top_ctx
))
7131 bpf_error(cstate
, "no mask %s supported", s2
);
7133 res
= pcap_nametoaddrinfo(s1
);
7135 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7138 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7139 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7141 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7142 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
.s6_addr
) * 8));
7143 memset(&mask
, 0, sizeof(mask
));
7144 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7146 mask
.s6_addr
[masklen
/ 8] =
7147 (0xff << (8 - masklen
% 8)) & 0xff;
7150 a
= (uint32_t *)addr
;
7151 m
= (uint32_t *)&mask
;
7152 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7153 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7154 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7162 bpf_error(cstate
, "Mask syntax for networks only");
7166 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7172 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7179 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7181 struct block
*b
, *tmp
;
7184 * Catch errors reported by us and routines below us, and return NULL
7187 if (setjmp(cstate
->top_ctx
))
7190 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7191 cstate
->e
= pcap_ether_aton(s
);
7192 if (cstate
->e
== NULL
)
7193 bpf_error(cstate
, "malloc");
7194 switch (cstate
->linktype
) {
7196 case DLT_NETANALYZER
:
7197 case DLT_NETANALYZER_TRANSPARENT
:
7198 tmp
= gen_prevlinkhdr_check(cstate
);
7199 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7204 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7207 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7209 case DLT_IEEE802_11
:
7210 case DLT_PRISM_HEADER
:
7211 case DLT_IEEE802_11_RADIO_AVS
:
7212 case DLT_IEEE802_11_RADIO
:
7214 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7216 case DLT_IP_OVER_FC
:
7217 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7222 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7229 bpf_error(cstate
, "ethernet address used in non-ether expression");
7234 sappend(struct slist
*s0
, struct slist
*s1
)
7237 * This is definitely not the best way to do this, but the
7238 * lists will rarely get long.
7245 static struct slist
*
7246 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7250 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7255 static struct slist
*
7256 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7260 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7266 * Modify "index" to use the value stored into its register as an
7267 * offset relative to the beginning of the header for the protocol
7268 * "proto", and allocate a register and put an item "size" bytes long
7269 * (1, 2, or 4) at that offset into that register, making it the register
7272 static struct arth
*
7273 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7277 struct slist
*s
, *tmp
;
7279 int regno
= alloc_reg(cstate
);
7281 free_reg(cstate
, inst
->regno
);
7285 bpf_error(cstate
, "data size must be 1, 2, or 4");
7302 bpf_error(cstate
, "unsupported index operation");
7306 * The offset is relative to the beginning of the packet
7307 * data, if we have a radio header. (If we don't, this
7310 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7311 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7312 cstate
->linktype
!= DLT_PRISM_HEADER
)
7313 bpf_error(cstate
, "radio information not present in capture");
7316 * Load into the X register the offset computed into the
7317 * register specified by "index".
7319 s
= xfer_to_x(cstate
, inst
);
7322 * Load the item at that offset.
7324 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7326 sappend(inst
->s
, s
);
7331 * The offset is relative to the beginning of
7332 * the link-layer header.
7334 * XXX - what about ATM LANE? Should the index be
7335 * relative to the beginning of the AAL5 frame, so
7336 * that 0 refers to the beginning of the LE Control
7337 * field, or relative to the beginning of the LAN
7338 * frame, so that 0 refers, for Ethernet LANE, to
7339 * the beginning of the destination address?
7341 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7344 * If "s" is non-null, it has code to arrange that the
7345 * X register contains the length of the prefix preceding
7346 * the link-layer header. Add to it the offset computed
7347 * into the register specified by "index", and move that
7348 * into the X register. Otherwise, just load into the X
7349 * register the offset computed into the register specified
7353 sappend(s
, xfer_to_a(cstate
, inst
));
7354 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7355 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7357 s
= xfer_to_x(cstate
, inst
);
7360 * Load the item at the sum of the offset we've put in the
7361 * X register and the offset of the start of the link
7362 * layer header (which is 0 if the radio header is
7363 * variable-length; that header length is what we put
7364 * into the X register and then added to the index).
7366 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7367 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7369 sappend(inst
->s
, s
);
7383 * The offset is relative to the beginning of
7384 * the network-layer header.
7385 * XXX - are there any cases where we want
7386 * cstate->off_nl_nosnap?
7388 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7391 * If "s" is non-null, it has code to arrange that the
7392 * X register contains the variable part of the offset
7393 * of the link-layer payload. Add to it the offset
7394 * computed into the register specified by "index",
7395 * and move that into the X register. Otherwise, just
7396 * load into the X register the offset computed into
7397 * the register specified by "index".
7400 sappend(s
, xfer_to_a(cstate
, inst
));
7401 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7402 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7404 s
= xfer_to_x(cstate
, inst
);
7407 * Load the item at the sum of the offset we've put in the
7408 * X register, the offset of the start of the network
7409 * layer header from the beginning of the link-layer
7410 * payload, and the constant part of the offset of the
7411 * start of the link-layer payload.
7413 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7414 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7416 sappend(inst
->s
, s
);
7419 * Do the computation only if the packet contains
7420 * the protocol in question.
7422 b
= gen_proto_abbrev_internal(cstate
, proto
);
7424 gen_and(inst
->b
, b
);
7438 * The offset is relative to the beginning of
7439 * the transport-layer header.
7441 * Load the X register with the length of the IPv4 header
7442 * (plus the offset of the link-layer header, if it's
7443 * a variable-length header), in bytes.
7445 * XXX - are there any cases where we want
7446 * cstate->off_nl_nosnap?
7447 * XXX - we should, if we're built with
7448 * IPv6 support, generate code to load either
7449 * IPv4, IPv6, or both, as appropriate.
7451 s
= gen_loadx_iphdrlen(cstate
);
7454 * The X register now contains the sum of the variable
7455 * part of the offset of the link-layer payload and the
7456 * length of the network-layer header.
7458 * Load into the A register the offset relative to
7459 * the beginning of the transport layer header,
7460 * add the X register to that, move that to the
7461 * X register, and load with an offset from the
7462 * X register equal to the sum of the constant part of
7463 * the offset of the link-layer payload and the offset,
7464 * relative to the beginning of the link-layer payload,
7465 * of the network-layer header.
7467 sappend(s
, xfer_to_a(cstate
, inst
));
7468 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7469 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7470 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7471 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7472 sappend(inst
->s
, s
);
7475 * Do the computation only if the packet contains
7476 * the protocol in question - which is true only
7477 * if this is an IP datagram and is the first or
7478 * only fragment of that datagram.
7480 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7482 gen_and(inst
->b
, b
);
7483 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7488 * Do the computation only if the packet contains
7489 * the protocol in question.
7491 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7493 gen_and(inst
->b
, b
);
7498 * Check if we have an icmp6 next header
7500 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7502 gen_and(inst
->b
, b
);
7507 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7509 * If "s" is non-null, it has code to arrange that the
7510 * X register contains the variable part of the offset
7511 * of the link-layer payload. Add to it the offset
7512 * computed into the register specified by "index",
7513 * and move that into the X register. Otherwise, just
7514 * load into the X register the offset computed into
7515 * the register specified by "index".
7518 sappend(s
, xfer_to_a(cstate
, inst
));
7519 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7520 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7522 s
= xfer_to_x(cstate
, inst
);
7526 * Load the item at the sum of the offset we've put in the
7527 * X register, the offset of the start of the network
7528 * layer header from the beginning of the link-layer
7529 * payload, and the constant part of the offset of the
7530 * start of the link-layer payload.
7532 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7533 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7536 sappend(inst
->s
, s
);
7540 inst
->regno
= regno
;
7541 s
= new_stmt(cstate
, BPF_ST
);
7543 sappend(inst
->s
, s
);
7549 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7553 * Catch errors reported by us and routines below us, and return NULL
7556 if (setjmp(cstate
->top_ctx
))
7559 return gen_load_internal(cstate
, proto
, inst
, size
);
7562 static struct block
*
7563 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7564 struct arth
*a1
, int reversed
)
7566 struct slist
*s0
, *s1
, *s2
;
7567 struct block
*b
, *tmp
;
7569 s0
= xfer_to_x(cstate
, a1
);
7570 s1
= xfer_to_a(cstate
, a0
);
7571 if (code
== BPF_JEQ
) {
7572 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7573 b
= new_block(cstate
, JMP(code
));
7577 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7583 sappend(a0
->s
, a1
->s
);
7587 free_reg(cstate
, a0
->regno
);
7588 free_reg(cstate
, a1
->regno
);
7590 /* 'and' together protocol checks */
7593 gen_and(a0
->b
, tmp
= a1
->b
);
7607 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7608 struct arth
*a1
, int reversed
)
7611 * Catch errors reported by us and routines below us, and return NULL
7614 if (setjmp(cstate
->top_ctx
))
7617 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7621 gen_loadlen(compiler_state_t
*cstate
)
7628 * Catch errors reported by us and routines below us, and return NULL
7631 if (setjmp(cstate
->top_ctx
))
7634 regno
= alloc_reg(cstate
);
7635 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7636 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7637 s
->next
= new_stmt(cstate
, BPF_ST
);
7638 s
->next
->s
.k
= regno
;
7645 static struct arth
*
7646 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7652 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7654 reg
= alloc_reg(cstate
);
7656 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7658 s
->next
= new_stmt(cstate
, BPF_ST
);
7667 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7670 * Catch errors reported by us and routines below us, and return NULL
7673 if (setjmp(cstate
->top_ctx
))
7676 return gen_loadi_internal(cstate
, val
);
7680 * The a_arg dance is to avoid annoying whining by compilers that
7681 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7682 * It's not *used* after setjmp returns.
7685 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7687 struct arth
*a
= a_arg
;
7691 * Catch errors reported by us and routines below us, and return NULL
7694 if (setjmp(cstate
->top_ctx
))
7697 s
= xfer_to_a(cstate
, a
);
7699 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7702 s
= new_stmt(cstate
, BPF_ST
);
7710 * The a0_arg dance is to avoid annoying whining by compilers that
7711 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7712 * It's not *used* after setjmp returns.
7715 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7718 struct arth
*a0
= a0_arg
;
7719 struct slist
*s0
, *s1
, *s2
;
7722 * Catch errors reported by us and routines below us, and return NULL
7725 if (setjmp(cstate
->top_ctx
))
7729 * Disallow division by, or modulus by, zero; we do this here
7730 * so that it gets done even if the optimizer is disabled.
7732 * Also disallow shifts by a value greater than 31; we do this
7733 * here, for the same reason.
7735 if (code
== BPF_DIV
) {
7736 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7737 bpf_error(cstate
, "division by zero");
7738 } else if (code
== BPF_MOD
) {
7739 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7740 bpf_error(cstate
, "modulus by zero");
7741 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7742 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7743 bpf_error(cstate
, "shift by more than 31 bits");
7745 s0
= xfer_to_x(cstate
, a1
);
7746 s1
= xfer_to_a(cstate
, a0
);
7747 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7752 sappend(a0
->s
, a1
->s
);
7754 free_reg(cstate
, a0
->regno
);
7755 free_reg(cstate
, a1
->regno
);
7757 s0
= new_stmt(cstate
, BPF_ST
);
7758 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7765 * Initialize the table of used registers and the current register.
7768 init_regs(compiler_state_t
*cstate
)
7771 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7775 * Return the next free register.
7778 alloc_reg(compiler_state_t
*cstate
)
7780 int n
= BPF_MEMWORDS
;
7783 if (cstate
->regused
[cstate
->curreg
])
7784 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7786 cstate
->regused
[cstate
->curreg
] = 1;
7787 return cstate
->curreg
;
7790 bpf_error(cstate
, "too many registers needed to evaluate expression");
7795 * Return a register to the table so it can
7799 free_reg(compiler_state_t
*cstate
, int n
)
7801 cstate
->regused
[n
] = 0;
7804 static struct block
*
7805 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7810 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7811 b
= new_block(cstate
, JMP(jmp
));
7819 gen_greater(compiler_state_t
*cstate
, int n
)
7822 * Catch errors reported by us and routines below us, and return NULL
7825 if (setjmp(cstate
->top_ctx
))
7828 return gen_len(cstate
, BPF_JGE
, n
);
7832 * Actually, this is less than or equal.
7835 gen_less(compiler_state_t
*cstate
, int n
)
7840 * Catch errors reported by us and routines below us, and return NULL
7843 if (setjmp(cstate
->top_ctx
))
7846 b
= gen_len(cstate
, BPF_JGT
, n
);
7853 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7854 * the beginning of the link-layer header.
7855 * XXX - that means you can't test values in the radiotap header, but
7856 * as that header is difficult if not impossible to parse generally
7857 * without a loop, that might not be a severe problem. A new keyword
7858 * "radio" could be added for that, although what you'd really want
7859 * would be a way of testing particular radio header values, which
7860 * would generate code appropriate to the radio header in question.
7863 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7869 * Catch errors reported by us and routines below us, and return NULL
7872 if (setjmp(cstate
->top_ctx
))
7880 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7883 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7887 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7891 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7895 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7899 b
= new_block(cstate
, JMP(BPF_JEQ
));
7906 static const u_char abroadcast
[] = { 0x0 };
7909 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7911 bpf_u_int32 hostmask
;
7912 struct block
*b0
, *b1
, *b2
;
7913 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7916 * Catch errors reported by us and routines below us, and return NULL
7919 if (setjmp(cstate
->top_ctx
))
7926 switch (cstate
->linktype
) {
7928 case DLT_ARCNET_LINUX
:
7929 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7931 case DLT_NETANALYZER
:
7932 case DLT_NETANALYZER_TRANSPARENT
:
7933 b1
= gen_prevlinkhdr_check(cstate
);
7934 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7939 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7941 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7942 case DLT_IEEE802_11
:
7943 case DLT_PRISM_HEADER
:
7944 case DLT_IEEE802_11_RADIO_AVS
:
7945 case DLT_IEEE802_11_RADIO
:
7947 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7948 case DLT_IP_OVER_FC
:
7949 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7951 bpf_error(cstate
, "not a broadcast link");
7957 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7958 * as an indication that we don't know the netmask, and fail
7961 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7962 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7963 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7964 hostmask
= ~cstate
->netmask
;
7965 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
7966 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7967 ~0 & hostmask
, hostmask
);
7972 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7977 * Generate code to test the low-order bit of a MAC address (that's
7978 * the bottom bit of the *first* byte).
7980 static struct block
*
7981 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7983 register struct block
*b0
;
7984 register struct slist
*s
;
7986 /* link[offset] & 1 != 0 */
7987 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7988 b0
= new_block(cstate
, JMP(BPF_JSET
));
7995 gen_multicast(compiler_state_t
*cstate
, int proto
)
7997 register struct block
*b0
, *b1
, *b2
;
7998 register struct slist
*s
;
8001 * Catch errors reported by us and routines below us, and return NULL
8004 if (setjmp(cstate
->top_ctx
))
8011 switch (cstate
->linktype
) {
8013 case DLT_ARCNET_LINUX
:
8014 /* all ARCnet multicasts use the same address */
8015 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8017 case DLT_NETANALYZER
:
8018 case DLT_NETANALYZER_TRANSPARENT
:
8019 b1
= gen_prevlinkhdr_check(cstate
);
8020 /* ether[0] & 1 != 0 */
8021 b0
= gen_mac_multicast(cstate
, 0);
8027 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8029 * XXX - was that referring to bit-order issues?
8031 /* fddi[1] & 1 != 0 */
8032 return gen_mac_multicast(cstate
, 1);
8034 /* tr[2] & 1 != 0 */
8035 return gen_mac_multicast(cstate
, 2);
8036 case DLT_IEEE802_11
:
8037 case DLT_PRISM_HEADER
:
8038 case DLT_IEEE802_11_RADIO_AVS
:
8039 case DLT_IEEE802_11_RADIO
:
8044 * For control frames, there is no DA.
8046 * For management frames, DA is at an
8047 * offset of 4 from the beginning of
8050 * For data frames, DA is at an offset
8051 * of 4 from the beginning of the packet
8052 * if To DS is clear and at an offset of
8053 * 16 from the beginning of the packet
8058 * Generate the tests to be done for data frames.
8060 * First, check for To DS set, i.e. "link[1] & 0x01".
8062 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8063 b1
= new_block(cstate
, JMP(BPF_JSET
));
8064 b1
->s
.k
= 0x01; /* To DS */
8068 * If To DS is set, the DA is at 16.
8070 b0
= gen_mac_multicast(cstate
, 16);
8074 * Now, check for To DS not set, i.e. check
8075 * "!(link[1] & 0x01)".
8077 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8078 b2
= new_block(cstate
, JMP(BPF_JSET
));
8079 b2
->s
.k
= 0x01; /* To DS */
8084 * If To DS is not set, the DA is at 4.
8086 b1
= gen_mac_multicast(cstate
, 4);
8090 * Now OR together the last two checks. That gives
8091 * the complete set of checks for data frames.
8096 * Now check for a data frame.
8097 * I.e, check "link[0] & 0x08".
8099 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8100 b1
= new_block(cstate
, JMP(BPF_JSET
));
8105 * AND that with the checks done for data frames.
8110 * If the high-order bit of the type value is 0, this
8111 * is a management frame.
8112 * I.e, check "!(link[0] & 0x08)".
8114 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8115 b2
= new_block(cstate
, JMP(BPF_JSET
));
8121 * For management frames, the DA is at 4.
8123 b1
= gen_mac_multicast(cstate
, 4);
8127 * OR that with the checks done for data frames.
8128 * That gives the checks done for management and
8134 * If the low-order bit of the type value is 1,
8135 * this is either a control frame or a frame
8136 * with a reserved type, and thus not a
8139 * I.e., check "!(link[0] & 0x04)".
8141 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8142 b1
= new_block(cstate
, JMP(BPF_JSET
));
8148 * AND that with the checks for data and management
8153 case DLT_IP_OVER_FC
:
8154 b0
= gen_mac_multicast(cstate
, 2);
8159 /* Link not known to support multicasts */
8163 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8164 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8169 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8170 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8174 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8179 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8180 * Outbound traffic is sent by this machine, while inbound traffic is
8181 * sent by a remote machine (and may include packets destined for a
8182 * unicast or multicast link-layer address we are not subscribing to).
8183 * These are the same definitions implemented by pcap_setdirection().
8184 * Capturing only unicast traffic destined for this host is probably
8185 * better accomplished using a higher-layer filter.
8188 gen_inbound(compiler_state_t
*cstate
, int dir
)
8190 register struct block
*b0
;
8193 * Catch errors reported by us and routines below us, and return NULL
8196 if (setjmp(cstate
->top_ctx
))
8200 * Only some data link types support inbound/outbound qualifiers.
8202 switch (cstate
->linktype
) {
8204 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8205 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8206 gen_loadi_internal(cstate
, 0),
8212 /* match outgoing packets */
8213 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8215 /* match incoming packets */
8216 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8221 /* match outgoing packets */
8222 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8224 /* to filter on inbound traffic, invert the match */
8229 case DLT_LINUX_SLL2
:
8230 /* match outgoing packets */
8231 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8233 /* to filter on inbound traffic, invert the match */
8238 #ifdef HAVE_NET_PFVAR_H
8240 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8241 ((dir
== 0) ? PF_IN
: PF_OUT
));
8247 /* match outgoing packets */
8248 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8250 /* match incoming packets */
8251 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8255 case DLT_JUNIPER_MFR
:
8256 case DLT_JUNIPER_MLFR
:
8257 case DLT_JUNIPER_MLPPP
:
8258 case DLT_JUNIPER_ATM1
:
8259 case DLT_JUNIPER_ATM2
:
8260 case DLT_JUNIPER_PPPOE
:
8261 case DLT_JUNIPER_PPPOE_ATM
:
8262 case DLT_JUNIPER_GGSN
:
8263 case DLT_JUNIPER_ES
:
8264 case DLT_JUNIPER_MONITOR
:
8265 case DLT_JUNIPER_SERVICES
:
8266 case DLT_JUNIPER_ETHER
:
8267 case DLT_JUNIPER_PPP
:
8268 case DLT_JUNIPER_FRELAY
:
8269 case DLT_JUNIPER_CHDLC
:
8270 case DLT_JUNIPER_VP
:
8271 case DLT_JUNIPER_ST
:
8272 case DLT_JUNIPER_ISM
:
8273 case DLT_JUNIPER_VS
:
8274 case DLT_JUNIPER_SRX_E2E
:
8275 case DLT_JUNIPER_FIBRECHANNEL
:
8276 case DLT_JUNIPER_ATM_CEMIC
:
8278 /* juniper flags (including direction) are stored
8279 * the byte after the 3-byte magic number */
8281 /* match outgoing packets */
8282 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8284 /* match incoming packets */
8285 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8291 * If we have packet meta-data indicating a direction,
8292 * and that metadata can be checked by BPF code, check
8293 * it. Otherwise, give up, as this link-layer type has
8294 * nothing in the packet data.
8296 * Currently, the only platform where a BPF filter can
8297 * check that metadata is Linux with the in-kernel
8298 * BPF interpreter. If other packet capture mechanisms
8299 * and BPF filters also supported this, it would be
8300 * nice. It would be even better if they made that
8301 * metadata available so that we could provide it
8302 * with newer capture APIs, allowing it to be saved
8307 * This is Linux; we assume it has PF_PACKET support.
8308 * If this is a *live* capture, we can look at
8309 * special meta-data in the filter expression;
8310 * if it's a savefile, we can't.
8312 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8313 /* We have a FILE *, so this is a savefile */
8314 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8315 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8318 /* match outgoing packets */
8319 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8322 /* to filter on inbound traffic, invert the match */
8325 #else /* defined(linux) */
8326 bpf_error(cstate
, "inbound/outbound not supported on %s",
8327 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8329 #endif /* defined(linux) */
8334 #ifdef HAVE_NET_PFVAR_H
8335 /* PF firewall log matched interface */
8337 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8343 * Catch errors reported by us and routines below us, and return NULL
8346 if (setjmp(cstate
->top_ctx
))
8349 if (cstate
->linktype
!= DLT_PFLOG
) {
8350 bpf_error(cstate
, "ifname supported only on PF linktype");
8353 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8354 off
= offsetof(struct pfloghdr
, ifname
);
8355 if (strlen(ifname
) >= len
) {
8356 bpf_error(cstate
, "ifname interface names can only be %d characters",
8360 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8361 (const u_char
*)ifname
);
8365 /* PF firewall log ruleset name */
8367 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8372 * Catch errors reported by us and routines below us, and return NULL
8375 if (setjmp(cstate
->top_ctx
))
8378 if (cstate
->linktype
!= DLT_PFLOG
) {
8379 bpf_error(cstate
, "ruleset supported only on PF linktype");
8383 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8384 bpf_error(cstate
, "ruleset names can only be %ld characters",
8385 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8389 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8390 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8394 /* PF firewall log rule number */
8396 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8401 * Catch errors reported by us and routines below us, and return NULL
8404 if (setjmp(cstate
->top_ctx
))
8407 if (cstate
->linktype
!= DLT_PFLOG
) {
8408 bpf_error(cstate
, "rnr supported only on PF linktype");
8412 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8417 /* PF firewall log sub-rule number */
8419 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8424 * Catch errors reported by us and routines below us, and return NULL
8427 if (setjmp(cstate
->top_ctx
))
8430 if (cstate
->linktype
!= DLT_PFLOG
) {
8431 bpf_error(cstate
, "srnr supported only on PF linktype");
8435 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8440 /* PF firewall log reason code */
8442 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8447 * Catch errors reported by us and routines below us, and return NULL
8450 if (setjmp(cstate
->top_ctx
))
8453 if (cstate
->linktype
!= DLT_PFLOG
) {
8454 bpf_error(cstate
, "reason supported only on PF linktype");
8458 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8459 (bpf_u_int32
)reason
);
8463 /* PF firewall log action */
8465 gen_pf_action(compiler_state_t
*cstate
, int action
)
8470 * Catch errors reported by us and routines below us, and return NULL
8473 if (setjmp(cstate
->top_ctx
))
8476 if (cstate
->linktype
!= DLT_PFLOG
) {
8477 bpf_error(cstate
, "action supported only on PF linktype");
8481 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8482 (bpf_u_int32
)action
);
8485 #else /* !HAVE_NET_PFVAR_H */
8487 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8490 * Catch errors reported by us and routines below us, and return NULL
8493 if (setjmp(cstate
->top_ctx
))
8496 bpf_error(cstate
, "libpcap was compiled without pf support");
8501 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8504 * Catch errors reported by us and routines below us, and return NULL
8507 if (setjmp(cstate
->top_ctx
))
8510 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8515 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8518 * Catch errors reported by us and routines below us, and return NULL
8521 if (setjmp(cstate
->top_ctx
))
8524 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8529 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8532 * Catch errors reported by us and routines below us, and return NULL
8535 if (setjmp(cstate
->top_ctx
))
8538 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8543 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8546 * Catch errors reported by us and routines below us, and return NULL
8549 if (setjmp(cstate
->top_ctx
))
8552 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8557 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8560 * Catch errors reported by us and routines below us, and return NULL
8563 if (setjmp(cstate
->top_ctx
))
8566 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8569 #endif /* HAVE_NET_PFVAR_H */
8571 /* IEEE 802.11 wireless header */
8573 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8578 * Catch errors reported by us and routines below us, and return NULL
8581 if (setjmp(cstate
->top_ctx
))
8584 switch (cstate
->linktype
) {
8586 case DLT_IEEE802_11
:
8587 case DLT_PRISM_HEADER
:
8588 case DLT_IEEE802_11_RADIO_AVS
:
8589 case DLT_IEEE802_11_RADIO
:
8590 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8594 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8602 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8607 * Catch errors reported by us and routines below us, and return NULL
8610 if (setjmp(cstate
->top_ctx
))
8613 switch (cstate
->linktype
) {
8615 case DLT_IEEE802_11
:
8616 case DLT_PRISM_HEADER
:
8617 case DLT_IEEE802_11_RADIO_AVS
:
8618 case DLT_IEEE802_11_RADIO
:
8622 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8626 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8627 IEEE80211_FC1_DIR_MASK
);
8633 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8638 * Catch errors reported by us and routines below us, and return NULL
8641 if (setjmp(cstate
->top_ctx
))
8644 switch (cstate
->linktype
) {
8647 case DLT_ARCNET_LINUX
:
8648 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8649 q
.proto
== Q_LINK
) {
8650 cstate
->e
= pcap_ether_aton(s
);
8651 if (cstate
->e
== NULL
)
8652 bpf_error(cstate
, "malloc");
8653 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8658 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8662 bpf_error(cstate
, "aid supported only on ARCnet");
8667 static struct block
*
8668 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8670 register struct block
*b0
, *b1
;
8673 /* src comes first, different from Ethernet */
8675 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8678 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8681 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8682 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8688 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8689 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8694 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8698 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8702 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8706 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8710 bpf_error(cstate
, "'ra' is only supported on 802.11");
8714 bpf_error(cstate
, "'ta' is only supported on 802.11");
8721 static struct block
*
8722 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8724 struct block
*b0
, *b1
;
8726 /* check for VLAN, including QinQ */
8727 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8728 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8731 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8737 static struct block
*
8738 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8740 if (vlan_num
> 0x0fff) {
8741 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8744 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8747 static struct block
*
8748 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8751 struct block
*b0
, *b1
;
8753 b0
= gen_vlan_tpid_test(cstate
);
8756 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8762 * Both payload and link header type follow the VLAN tags so that
8763 * both need to be updated.
8765 cstate
->off_linkpl
.constant_part
+= 4;
8766 cstate
->off_linktype
.constant_part
+= 4;
8771 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8772 /* add v to variable part of off */
8774 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8775 bpf_u_int32 v
, struct slist
*s
)
8779 if (!off
->is_variable
)
8780 off
->is_variable
= 1;
8782 off
->reg
= alloc_reg(cstate
);
8784 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8787 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8790 s2
= new_stmt(cstate
, BPF_ST
);
8796 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8797 * and link type offsets first
8800 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8804 /* offset determined at run time, shift variable part */
8806 cstate
->is_vlan_vloffset
= 1;
8807 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8808 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8810 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8811 sappend(s
.next
, b_tpid
->head
->stmts
);
8812 b_tpid
->head
->stmts
= s
.next
;
8816 * patch block b_vid (VLAN id test) to load VID value either from packet
8817 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8820 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8822 struct slist
*s
, *s2
, *sjeq
;
8825 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8826 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8828 /* true -> next instructions, false -> beginning of b_vid */
8829 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8831 sjeq
->s
.jf
= b_vid
->stmts
;
8834 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8835 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8839 /* Jump to the test in b_vid. We need to jump one instruction before
8840 * the end of the b_vid block so that we only skip loading the TCI
8841 * from packet data and not the 'and' instruction extractging VID.
8844 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8846 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8850 /* insert our statements at the beginning of b_vid */
8851 sappend(s
, b_vid
->stmts
);
8856 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8857 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8858 * tag can be either in metadata or in packet data; therefore if the
8859 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8860 * header for VLAN tag. As the decision is done at run time, we need
8861 * update variable part of the offsets
8863 static struct block
*
8864 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8867 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8870 /* generate new filter code based on extracting packet
8872 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8873 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8875 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8880 * This is tricky. We need to insert the statements updating variable
8881 * parts of offsets before the traditional TPID and VID tests so
8882 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8883 * we do not want this update to affect those checks. That's why we
8884 * generate both test blocks first and insert the statements updating
8885 * variable parts of both offsets after that. This wouldn't work if
8886 * there already were variable length link header when entering this
8887 * function but gen_vlan_bpf_extensions() isn't called in that case.
8889 b_tpid
= gen_vlan_tpid_test(cstate
);
8891 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8893 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8898 gen_vlan_patch_vid_test(cstate
, b_vid
);
8908 * support IEEE 802.1Q VLAN trunk over ethernet
8911 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8916 * Catch errors reported by us and routines below us, and return NULL
8919 if (setjmp(cstate
->top_ctx
))
8922 /* can't check for VLAN-encapsulated packets inside MPLS */
8923 if (cstate
->label_stack_depth
> 0)
8924 bpf_error(cstate
, "no VLAN match after MPLS");
8927 * Check for a VLAN packet, and then change the offsets to point
8928 * to the type and data fields within the VLAN packet. Just
8929 * increment the offsets, so that we can support a hierarchy, e.g.
8930 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8933 * XXX - this is a bit of a kludge. If we were to split the
8934 * compiler into a parser that parses an expression and
8935 * generates an expression tree, and a code generator that
8936 * takes an expression tree (which could come from our
8937 * parser or from some other parser) and generates BPF code,
8938 * we could perhaps make the offsets parameters of routines
8939 * and, in the handler for an "AND" node, pass to subnodes
8940 * other than the VLAN node the adjusted offsets.
8942 * This would mean that "vlan" would, instead of changing the
8943 * behavior of *all* tests after it, change only the behavior
8944 * of tests ANDed with it. That would change the documented
8945 * semantics of "vlan", which might break some expressions.
8946 * However, it would mean that "(vlan and ip) or ip" would check
8947 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8948 * checking only for VLAN-encapsulated IP, so that could still
8949 * be considered worth doing; it wouldn't break expressions
8950 * that are of the form "vlan and ..." or "vlan N and ...",
8951 * which I suspect are the most common expressions involving
8952 * "vlan". "vlan or ..." doesn't necessarily do what the user
8953 * would really want, now, as all the "or ..." tests would
8954 * be done assuming a VLAN, even though the "or" could be viewed
8955 * as meaning "or, if this isn't a VLAN packet...".
8957 switch (cstate
->linktype
) {
8960 case DLT_NETANALYZER
:
8961 case DLT_NETANALYZER_TRANSPARENT
:
8962 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8963 /* Verify that this is the outer part of the packet and
8964 * not encapsulated somehow. */
8965 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8966 cstate
->off_linkhdr
.constant_part
==
8967 cstate
->off_outermostlinkhdr
.constant_part
) {
8969 * Do we need special VLAN handling?
8971 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8972 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8975 b0
= gen_vlan_no_bpf_extensions(cstate
,
8976 vlan_num
, has_vlan_tag
);
8979 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8983 case DLT_IEEE802_11
:
8984 case DLT_PRISM_HEADER
:
8985 case DLT_IEEE802_11_RADIO_AVS
:
8986 case DLT_IEEE802_11_RADIO
:
8987 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8991 bpf_error(cstate
, "no VLAN support for %s",
8992 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8996 cstate
->vlan_stack_depth
++;
9004 * The label_num_arg dance is to avoid annoying whining by compilers that
9005 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9006 * It's not *used* after setjmp returns.
9009 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9012 volatile bpf_u_int32 label_num
= label_num_arg
;
9013 struct block
*b0
, *b1
;
9016 * Catch errors reported by us and routines below us, and return NULL
9019 if (setjmp(cstate
->top_ctx
))
9022 if (cstate
->label_stack_depth
> 0) {
9023 /* just match the bottom-of-stack bit clear */
9024 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9027 * We're not in an MPLS stack yet, so check the link-layer
9028 * type against MPLS.
9030 switch (cstate
->linktype
) {
9032 case DLT_C_HDLC
: /* fall through */
9034 case DLT_NETANALYZER
:
9035 case DLT_NETANALYZER_TRANSPARENT
:
9036 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9040 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9043 /* FIXME add other DLT_s ...
9044 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9045 * leave it for now */
9048 bpf_error(cstate
, "no MPLS support for %s",
9049 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9054 /* If a specific MPLS label is requested, check it */
9055 if (has_label_num
) {
9056 if (label_num
> 0xFFFFF) {
9057 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9058 label_num
, 0xFFFFF);
9060 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9061 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9062 0xfffff000); /* only compare the first 20 bits */
9068 * Change the offsets to point to the type and data fields within
9069 * the MPLS packet. Just increment the offsets, so that we
9070 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9071 * capture packets with an outer label of 100000 and an inner
9074 * Increment the MPLS stack depth as well; this indicates that
9075 * we're checking MPLS-encapsulated headers, to make sure higher
9076 * level code generators don't try to match against IP-related
9077 * protocols such as Q_ARP, Q_RARP etc.
9079 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9081 cstate
->off_nl_nosnap
+= 4;
9082 cstate
->off_nl
+= 4;
9083 cstate
->label_stack_depth
++;
9088 * Support PPPOE discovery and session.
9091 gen_pppoed(compiler_state_t
*cstate
)
9094 * Catch errors reported by us and routines below us, and return NULL
9097 if (setjmp(cstate
->top_ctx
))
9100 /* check for PPPoE discovery */
9101 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9105 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9107 struct block
*b0
, *b1
;
9110 * Catch errors reported by us and routines below us, and return NULL
9113 if (setjmp(cstate
->top_ctx
))
9117 * Test against the PPPoE session link-layer type.
9119 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9121 /* If a specific session is requested, check PPPoE session id */
9123 if (sess_num
> 0x0000ffff) {
9124 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9125 sess_num
, 0x0000ffff);
9127 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9133 * Change the offsets to point to the type and data fields within
9134 * the PPP packet, and note that this is PPPoE rather than
9137 * XXX - this is a bit of a kludge. See the comments in
9140 * The "network-layer" protocol is PPPoE, which has a 6-byte
9141 * PPPoE header, followed by a PPP packet.
9143 * There is no HDLC encapsulation for the PPP packet (it's
9144 * encapsulated in PPPoES instead), so the link-layer type
9145 * starts at the first byte of the PPP packet. For PPPoE,
9146 * that offset is relative to the beginning of the total
9147 * link-layer payload, including any 802.2 LLC header, so
9148 * it's 6 bytes past cstate->off_nl.
9150 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9151 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9152 cstate
->off_linkpl
.reg
);
9154 cstate
->off_linktype
= cstate
->off_linkhdr
;
9155 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9158 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9163 /* Check that this is Geneve and the VNI is correct if
9164 * specified. Parameterized to handle both IPv4 and IPv6. */
9165 static struct block
*
9166 gen_geneve_check(compiler_state_t
*cstate
,
9167 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9168 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9170 struct block
*b0
, *b1
;
9172 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9174 /* Check that we are operating on version 0. Otherwise, we
9175 * can't decode the rest of the fields. The version is 2 bits
9176 * in the first byte of the Geneve header. */
9177 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9182 if (vni
> 0xffffff) {
9183 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9186 vni
<<= 8; /* VNI is in the upper 3 bytes */
9187 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9195 /* The IPv4 and IPv6 Geneve checks need to do two things:
9196 * - Verify that this actually is Geneve with the right VNI.
9197 * - Place the IP header length (plus variable link prefix if
9198 * needed) into register A to be used later to compute
9199 * the inner packet offsets. */
9200 static struct block
*
9201 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9203 struct block
*b0
, *b1
;
9204 struct slist
*s
, *s1
;
9206 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9208 /* Load the IP header length into A. */
9209 s
= gen_loadx_iphdrlen(cstate
);
9211 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9214 /* Forcibly append these statements to the true condition
9215 * of the protocol check by creating a new block that is
9216 * always true and ANDing them. */
9217 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9226 static struct block
*
9227 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9229 struct block
*b0
, *b1
;
9230 struct slist
*s
, *s1
;
9232 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9234 /* Load the IP header length. We need to account for a
9235 * variable length link prefix if there is one. */
9236 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9238 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9242 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9246 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9250 /* Forcibly append these statements to the true condition
9251 * of the protocol check by creating a new block that is
9252 * always true and ANDing them. */
9253 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9256 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9265 /* We need to store three values based on the Geneve header::
9266 * - The offset of the linktype.
9267 * - The offset of the end of the Geneve header.
9268 * - The offset of the end of the encapsulated MAC header. */
9269 static struct slist
*
9270 gen_geneve_offsets(compiler_state_t
*cstate
)
9272 struct slist
*s
, *s1
, *s_proto
;
9274 /* First we need to calculate the offset of the Geneve header
9275 * itself. This is composed of the IP header previously calculated
9276 * (include any variable link prefix) and stored in A plus the
9277 * fixed sized headers (fixed link prefix, MAC length, and UDP
9279 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9280 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9282 /* Stash this in X since we'll need it later. */
9283 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9286 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9288 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9292 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9293 cstate
->off_linktype
.is_variable
= 1;
9294 cstate
->off_linktype
.constant_part
= 0;
9296 s1
= new_stmt(cstate
, BPF_ST
);
9297 s1
->s
.k
= cstate
->off_linktype
.reg
;
9300 /* Load the Geneve option length and mask and shift to get the
9301 * number of bytes. It is stored in the first byte of the Geneve
9303 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9307 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9311 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9315 /* Add in the rest of the Geneve base header. */
9316 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9320 /* Add the Geneve header length to its offset and store. */
9321 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9325 /* Set the encapsulated type as Ethernet. Even though we may
9326 * not actually have Ethernet inside there are two reasons this
9328 * - The linktype field is always in EtherType format regardless
9329 * of whether it is in Geneve or an inner Ethernet frame.
9330 * - The only link layer that we have specific support for is
9331 * Ethernet. We will confirm that the packet actually is
9332 * Ethernet at runtime before executing these checks. */
9333 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9335 s1
= new_stmt(cstate
, BPF_ST
);
9336 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9339 /* Calculate whether we have an Ethernet header or just raw IP/
9340 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9341 * and linktype by 14 bytes so that the network header can be found
9342 * seamlessly. Otherwise, keep what we've calculated already. */
9344 /* We have a bare jmp so we can't use the optimizer. */
9345 cstate
->no_optimize
= 1;
9347 /* Load the EtherType in the Geneve header, 2 bytes in. */
9348 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9352 /* Load X with the end of the Geneve header. */
9353 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9354 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9357 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9358 * end of this check, we should have the total length in X. In
9359 * the non-Ethernet case, it's already there. */
9360 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9361 s_proto
->s
.k
= ETHERTYPE_TEB
;
9362 sappend(s
, s_proto
);
9364 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9368 /* Since this is Ethernet, use the EtherType of the payload
9369 * directly as the linktype. Overwrite what we already have. */
9370 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9374 s1
= new_stmt(cstate
, BPF_ST
);
9375 s1
->s
.k
= cstate
->off_linktype
.reg
;
9378 /* Advance two bytes further to get the end of the Ethernet
9380 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9384 /* Move the result to X. */
9385 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9388 /* Store the final result of our linkpl calculation. */
9389 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9390 cstate
->off_linkpl
.is_variable
= 1;
9391 cstate
->off_linkpl
.constant_part
= 0;
9393 s1
= new_stmt(cstate
, BPF_STX
);
9394 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9403 /* Check to see if this is a Geneve packet. */
9405 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9407 struct block
*b0
, *b1
;
9411 * Catch errors reported by us and routines below us, and return NULL
9414 if (setjmp(cstate
->top_ctx
))
9417 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9418 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9423 /* Later filters should act on the payload of the Geneve frame,
9424 * update all of the header pointers. Attach this code so that
9425 * it gets executed in the event that the Geneve filter matches. */
9426 s
= gen_geneve_offsets(cstate
);
9428 b1
= gen_true(cstate
);
9429 sappend(s
, b1
->stmts
);
9434 cstate
->is_geneve
= 1;
9439 /* Check that the encapsulated frame has a link layer header
9440 * for Ethernet filters. */
9441 static struct block
*
9442 gen_geneve_ll_check(compiler_state_t
*cstate
)
9445 struct slist
*s
, *s1
;
9447 /* The easiest way to see if there is a link layer present
9448 * is to check if the link layer header and payload are not
9451 /* Geneve always generates pure variable offsets so we can
9452 * compare only the registers. */
9453 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9454 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9456 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9457 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9460 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9468 static struct block
*
9469 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9470 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9477 if (!cstate
->is_atm
)
9478 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9479 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9481 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9482 0xffffffffU
, jtype
, reverse
, jvalue
);
9486 if (!cstate
->is_atm
)
9487 bpf_error(cstate
, "'vci' supported only on raw ATM");
9488 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9490 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9491 0xffffffffU
, jtype
, reverse
, jvalue
);
9495 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9496 abort(); /* XXX - this isn't on FreeBSD */
9497 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9498 0x0fU
, jtype
, reverse
, jvalue
);
9502 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9504 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9505 0xffffffffU
, jtype
, reverse
, jvalue
);
9509 if (!cstate
->is_atm
)
9510 bpf_error(cstate
, "'callref' supported only on raw ATM");
9511 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9513 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9514 0xffffffffU
, jtype
, reverse
, jvalue
);
9523 static struct block
*
9524 gen_atmtype_metac(compiler_state_t
*cstate
)
9526 struct block
*b0
, *b1
;
9528 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9529 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9534 static struct block
*
9535 gen_atmtype_sc(compiler_state_t
*cstate
)
9537 struct block
*b0
, *b1
;
9539 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9540 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9545 static struct block
*
9546 gen_atmtype_llc(compiler_state_t
*cstate
)
9550 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9551 cstate
->linktype
= cstate
->prevlinktype
;
9556 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9557 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9560 * Catch errors reported by us and routines below us, and return NULL
9563 if (setjmp(cstate
->top_ctx
))
9566 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9571 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9573 struct block
*b0
, *b1
;
9576 * Catch errors reported by us and routines below us, and return NULL
9579 if (setjmp(cstate
->top_ctx
))
9585 /* Get all packets in Meta signalling Circuit */
9586 if (!cstate
->is_atm
)
9587 bpf_error(cstate
, "'metac' supported only on raw ATM");
9588 b1
= gen_atmtype_metac(cstate
);
9592 /* Get all packets in Broadcast Circuit*/
9593 if (!cstate
->is_atm
)
9594 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9595 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9596 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9601 /* Get all cells in Segment OAM F4 circuit*/
9602 if (!cstate
->is_atm
)
9603 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9604 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9605 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9610 /* Get all cells in End-to-End OAM F4 Circuit*/
9611 if (!cstate
->is_atm
)
9612 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9613 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9614 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9619 /* Get all packets in connection Signalling Circuit */
9620 if (!cstate
->is_atm
)
9621 bpf_error(cstate
, "'sc' supported only on raw ATM");
9622 b1
= gen_atmtype_sc(cstate
);
9626 /* Get all packets in ILMI Circuit */
9627 if (!cstate
->is_atm
)
9628 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9629 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9630 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9635 /* Get all LANE packets */
9636 if (!cstate
->is_atm
)
9637 bpf_error(cstate
, "'lane' supported only on raw ATM");
9638 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9641 * Arrange that all subsequent tests assume LANE
9642 * rather than LLC-encapsulated packets, and set
9643 * the offsets appropriately for LANE-encapsulated
9646 * We assume LANE means Ethernet, not Token Ring.
9648 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9649 cstate
->off_payload
+ 2, /* Ethernet header */
9651 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9652 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9653 cstate
->off_nl
= 0; /* Ethernet II */
9654 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9658 /* Get all LLC-encapsulated packets */
9659 if (!cstate
->is_atm
)
9660 bpf_error(cstate
, "'llc' supported only on raw ATM");
9661 b1
= gen_atmtype_llc(cstate
);
9671 * Filtering for MTP2 messages based on li value
9672 * FISU, length is null
9673 * LSSU, length is 1 or 2
9674 * MSU, length is 3 or more
9675 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9678 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9680 struct block
*b0
, *b1
;
9683 * Catch errors reported by us and routines below us, and return NULL
9686 if (setjmp(cstate
->top_ctx
))
9692 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9693 (cstate
->linktype
!= DLT_ERF
) &&
9694 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9695 bpf_error(cstate
, "'fisu' supported only on MTP2");
9696 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9697 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9698 0x3fU
, BPF_JEQ
, 0, 0U);
9702 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9703 (cstate
->linktype
!= DLT_ERF
) &&
9704 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9705 bpf_error(cstate
, "'lssu' supported only on MTP2");
9706 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9707 0x3fU
, BPF_JGT
, 1, 2U);
9708 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9709 0x3fU
, BPF_JGT
, 0, 0U);
9714 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9715 (cstate
->linktype
!= DLT_ERF
) &&
9716 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9717 bpf_error(cstate
, "'msu' supported only on MTP2");
9718 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9719 0x3fU
, BPF_JGT
, 0, 2U);
9723 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9724 (cstate
->linktype
!= DLT_ERF
) &&
9725 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9726 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9727 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9728 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9729 0xff80U
, BPF_JEQ
, 0, 0U);
9733 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9734 (cstate
->linktype
!= DLT_ERF
) &&
9735 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9736 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9737 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9738 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9739 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9740 0xff80U
, BPF_JGT
, 0, 0U);
9745 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9746 (cstate
->linktype
!= DLT_ERF
) &&
9747 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9748 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9749 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9750 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9760 * The jvalue_arg dance is to avoid annoying whining by compilers that
9761 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9762 * It's not *used* after setjmp returns.
9765 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9766 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
9768 volatile bpf_u_int32 jvalue
= jvalue_arg
;
9770 bpf_u_int32 val1
, val2
, val3
;
9777 * Catch errors reported by us and routines below us, and return NULL
9780 if (setjmp(cstate
->top_ctx
))
9783 newoff_sio
= cstate
->off_sio
;
9784 newoff_opc
= cstate
->off_opc
;
9785 newoff_dpc
= cstate
->off_dpc
;
9786 newoff_sls
= cstate
->off_sls
;
9787 switch (mtp3field
) {
9790 newoff_sio
+= 3; /* offset for MTP2_HSL */
9794 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9795 bpf_error(cstate
, "'sio' supported only on SS7");
9796 /* sio coded on 1 byte so max value 255 */
9798 bpf_error(cstate
, "sio value %u too big; max value = 255",
9800 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
9801 jtype
, reverse
, jvalue
);
9809 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9810 bpf_error(cstate
, "'opc' supported only on SS7");
9811 /* opc coded on 14 bits so max value 16383 */
9813 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9815 /* the following instructions are made to convert jvalue
9816 * to the form used to write opc in an ss7 message*/
9817 val1
= jvalue
& 0x00003c00;
9819 val2
= jvalue
& 0x000003fc;
9821 val3
= jvalue
& 0x00000003;
9823 jvalue
= val1
+ val2
+ val3
;
9824 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
9825 jtype
, reverse
, jvalue
);
9833 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9834 bpf_error(cstate
, "'dpc' supported only on SS7");
9835 /* dpc coded on 14 bits so max value 16383 */
9837 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9839 /* the following instructions are made to convert jvalue
9840 * to the forme used to write dpc in an ss7 message*/
9841 val1
= jvalue
& 0x000000ff;
9843 val2
= jvalue
& 0x00003f00;
9845 jvalue
= val1
+ val2
;
9846 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
9847 jtype
, reverse
, jvalue
);
9855 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9856 bpf_error(cstate
, "'sls' supported only on SS7");
9857 /* sls coded on 4 bits so max value 15 */
9859 bpf_error(cstate
, "sls value %u too big; max value = 15",
9861 /* the following instruction is made to convert jvalue
9862 * to the forme used to write sls in an ss7 message*/
9863 jvalue
= jvalue
<< 4;
9864 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
9865 jtype
, reverse
, jvalue
);
9874 static struct block
*
9875 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9880 * Q.2931 signalling protocol messages for handling virtual circuits
9881 * establishment and teardown
9886 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9890 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9894 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9898 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9902 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9905 case A_RELEASE_DONE
:
9906 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9916 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9918 struct block
*b0
, *b1
;
9921 * Catch errors reported by us and routines below us, and return NULL
9924 if (setjmp(cstate
->top_ctx
))
9930 if (!cstate
->is_atm
)
9931 bpf_error(cstate
, "'oam' supported only on raw ATM");
9933 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9934 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9936 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9941 if (!cstate
->is_atm
)
9942 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9944 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9945 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9947 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9953 * Get Q.2931 signalling messages for switched
9954 * virtual connection
9956 if (!cstate
->is_atm
)
9957 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9958 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9959 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9961 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9963 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9965 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9967 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9969 b0
= gen_atmtype_sc(cstate
);
9974 if (!cstate
->is_atm
)
9975 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9976 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9977 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9979 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9981 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9983 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9985 b0
= gen_atmtype_metac(cstate
);