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>
51 #ifdef HAVE_NET_PFVAR_H
53 * In NetBSD <net/if.h> includes <net/dlt.h>, which is an older version of
54 * "pcap/dlt.h" with a lower value of DLT_MATCHING_MAX. Include the headers
55 * below before "pcap-int.h", which eventually includes "pcap/dlt.h", which
56 * redefines DLT_MATCHING_MAX from what this version of NetBSD has to what
57 * this version of libpcap has.
59 #include <sys/socket.h>
61 #include <net/pfvar.h>
62 #include <net/if_pflog.h>
63 #endif /* HAVE_NET_PFVAR_H */
69 #include "ethertype.h"
73 #include "ieee80211.h"
75 #include "sunatmpos.h"
78 #include "pcap/ipnet.h"
85 #include <linux/types.h>
86 #include <linux/if_packet.h>
87 #include <linux/filter.h>
91 #define offsetof(s, e) ((size_t)&((s *)0)->e)
96 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
102 uint8_t u6_addr8
[16];
103 uint16_t u6_addr16
[8];
104 uint32_t u6_addr32
[4];
106 #define s6_addr in6_u.u6_addr8
107 #define s6_addr16 in6_u.u6_addr16
108 #define s6_addr32 in6_u.u6_addr32
109 #define s6_addr64 in6_u.u6_addr64
112 typedef unsigned short sa_family_t
;
114 #define __SOCKADDR_COMMON(sa_prefix) \
115 sa_family_t sa_prefix##family
117 /* Ditto, for IPv6. */
120 __SOCKADDR_COMMON (sin6_
);
121 uint16_t sin6_port
; /* Transport layer port # */
122 uint32_t sin6_flowinfo
; /* IPv6 flow information */
123 struct in6_addr sin6_addr
; /* IPv6 address */
126 #ifndef EAI_ADDRFAMILY
128 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
129 int ai_family
; /* PF_xxx */
130 int ai_socktype
; /* SOCK_xxx */
131 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
132 size_t ai_addrlen
; /* length of ai_addr */
133 char *ai_canonname
; /* canonical name for hostname */
134 struct sockaddr
*ai_addr
; /* binary address */
135 struct addrinfo
*ai_next
; /* next structure in linked list */
137 #endif /* EAI_ADDRFAMILY */
138 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
141 #include <netdb.h> /* for "struct addrinfo" */
143 #include <pcap/namedb.h>
145 #include "nametoaddr.h"
147 #define ETHERMTU 1500
149 #ifndef IPPROTO_HOPOPTS
150 #define IPPROTO_HOPOPTS 0
152 #ifndef IPPROTO_ROUTING
153 #define IPPROTO_ROUTING 43
155 #ifndef IPPROTO_FRAGMENT
156 #define IPPROTO_FRAGMENT 44
158 #ifndef IPPROTO_DSTOPTS
159 #define IPPROTO_DSTOPTS 60
162 #define IPPROTO_SCTP 132
165 #define GENEVE_PORT 6081
167 #ifdef HAVE_OS_PROTO_H
168 #include "os-proto.h"
171 #define JMP(c) ((c)|BPF_JMP|BPF_K)
174 * "Push" the current value of the link-layer header type and link-layer
175 * header offset onto a "stack", and set a new value. (It's not a
176 * full-blown stack; we keep only the top two items.)
178 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
180 (cs)->prevlinktype = (cs)->linktype; \
181 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
182 (cs)->linktype = (new_linktype); \
183 (cs)->off_linkhdr.is_variable = (new_is_variable); \
184 (cs)->off_linkhdr.constant_part = (new_constant_part); \
185 (cs)->off_linkhdr.reg = (new_reg); \
186 (cs)->is_geneve = 0; \
190 * Offset "not set" value.
192 #define OFFSET_NOT_SET 0xffffffffU
195 * Absolute offsets, which are offsets from the beginning of the raw
196 * packet data, are, in the general case, the sum of a variable value
197 * and a constant value; the variable value may be absent, in which
198 * case the offset is only the constant value, and the constant value
199 * may be zero, in which case the offset is only the variable value.
201 * bpf_abs_offset is a structure containing all that information:
203 * is_variable is 1 if there's a variable part.
205 * constant_part is the constant part of the value, possibly zero;
207 * if is_variable is 1, reg is the register number for a register
208 * containing the variable value if the register has been assigned,
218 * Value passed to gen_load_a() to indicate what the offset argument
219 * is relative to the beginning of.
222 OR_PACKET
, /* full packet data */
223 OR_LINKHDR
, /* link-layer header */
224 OR_PREVLINKHDR
, /* previous link-layer header */
225 OR_LLC
, /* 802.2 LLC header */
226 OR_PREVMPLSHDR
, /* previous MPLS header */
227 OR_LINKTYPE
, /* link-layer type */
228 OR_LINKPL
, /* link-layer payload */
229 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
230 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
231 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
235 * We divy out chunks of memory rather than call malloc each time so
236 * we don't have to worry about leaking memory. It's probably
237 * not a big deal if all this memory was wasted but if this ever
238 * goes into a library that would probably not be a good idea.
240 * XXX - this *is* in a library....
243 #define CHUNK0SIZE 1024
249 /* Code generator state */
251 struct _compiler_state
{
262 int outermostlinktype
;
267 /* Hack for handling VLAN and MPLS stacks. */
268 u_int label_stack_depth
;
269 u_int vlan_stack_depth
;
275 * As errors are handled by a longjmp, anything allocated must
276 * be freed in the longjmp handler, so it must be reachable
279 * One thing that's allocated is the result of pcap_nametoaddrinfo();
280 * it must be freed with freeaddrinfo(). This variable points to
281 * any addrinfo structure that would need to be freed.
286 * Another thing that's allocated is the result of pcap_ether_aton();
287 * it must be freed with free(). This variable points to any
288 * address that would need to be freed.
293 * Various code constructs need to know the layout of the packet.
294 * These values give the necessary offsets from the beginning
295 * of the packet data.
299 * Absolute offset of the beginning of the link-layer header.
301 bpf_abs_offset off_linkhdr
;
304 * If we're checking a link-layer header for a packet encapsulated
305 * in another protocol layer, this is the equivalent information
306 * for the previous layers' link-layer header from the beginning
307 * of the raw packet data.
309 bpf_abs_offset off_prevlinkhdr
;
312 * This is the equivalent information for the outermost layers'
315 bpf_abs_offset off_outermostlinkhdr
;
318 * Absolute offset of the beginning of the link-layer payload.
320 bpf_abs_offset off_linkpl
;
323 * "off_linktype" is the offset to information in the link-layer
324 * header giving the packet type. This is an absolute offset
325 * from the beginning of the packet.
327 * For Ethernet, it's the offset of the Ethernet type field; this
328 * means that it must have a value that skips VLAN tags.
330 * For link-layer types that always use 802.2 headers, it's the
331 * offset of the LLC header; this means that it must have a value
332 * that skips VLAN tags.
334 * For PPP, it's the offset of the PPP type field.
336 * For Cisco HDLC, it's the offset of the CHDLC type field.
338 * For BSD loopback, it's the offset of the AF_ value.
340 * For Linux cooked sockets, it's the offset of the type field.
342 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
343 * encapsulation, in which case, IP is assumed.
345 bpf_abs_offset off_linktype
;
348 * TRUE if the link layer includes an ATM pseudo-header.
353 * TRUE if "geneve" appeared in the filter; it causes us to
354 * generate code that checks for a Geneve header and assume
355 * that later filters apply to the encapsulated payload.
360 * TRUE if we need variable length part of VLAN offset
362 int is_vlan_vloffset
;
365 * These are offsets for the ATM pseudo-header.
372 * These are offsets for the MTP2 fields.
378 * These are offsets for the MTP3 fields.
386 * This is the offset of the first byte after the ATM pseudo_header,
387 * or -1 if there is no ATM pseudo-header.
392 * These are offsets to the beginning of the network-layer header.
393 * They are relative to the beginning of the link-layer payload
394 * (i.e., they don't include off_linkhdr.constant_part or
395 * off_linkpl.constant_part).
397 * If the link layer never uses 802.2 LLC:
399 * "off_nl" and "off_nl_nosnap" are the same.
401 * If the link layer always uses 802.2 LLC:
403 * "off_nl" is the offset if there's a SNAP header following
406 * "off_nl_nosnap" is the offset if there's no SNAP header.
408 * If the link layer is Ethernet:
410 * "off_nl" is the offset if the packet is an Ethernet II packet
411 * (we assume no 802.3+802.2+SNAP);
413 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
414 * with an 802.2 header following it.
420 * Here we handle simple allocation of the scratch registers.
421 * If too many registers are alloc'd, the allocator punts.
423 int regused
[BPF_MEMWORDS
];
429 struct chunk chunks
[NCHUNKS
];
434 * For use by routines outside this file.
438 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
443 * If we've already set an error, don't override it.
444 * The lexical analyzer reports some errors by setting
445 * the error and then returning a LEX_ERROR token, which
446 * is not recognized by any grammar rule, and thus forces
447 * the parse to stop. We don't want the error reported
448 * by the lexical analyzer to be overwritten by the syntax
451 if (!cstate
->error_set
) {
453 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
456 cstate
->error_set
= 1;
461 * For use *ONLY* in routines in this file.
463 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
464 PCAP_PRINTFLIKE(2, 3);
467 static void PCAP_NORETURN
468 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
473 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
476 longjmp(cstate
->top_ctx
, 1);
480 static int init_linktype(compiler_state_t
*, pcap_t
*);
482 static void init_regs(compiler_state_t
*);
483 static int alloc_reg(compiler_state_t
*);
484 static void free_reg(compiler_state_t
*, int);
486 static void initchunks(compiler_state_t
*cstate
);
487 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
488 static void *newchunk(compiler_state_t
*cstate
, size_t);
489 static void freechunks(compiler_state_t
*cstate
);
490 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
491 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
492 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
493 static inline void syntax(compiler_state_t
*cstate
);
495 static void backpatch(struct block
*, struct block
*);
496 static void merge(struct block
*, struct block
*);
497 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
499 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
501 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
503 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
505 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
507 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
508 u_int
, bpf_u_int32
, bpf_u_int32
);
509 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
510 u_int
, const u_char
*);
511 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
512 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
513 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
515 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
517 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
518 static struct block
*gen_uncond(compiler_state_t
*, int);
519 static inline struct block
*gen_true(compiler_state_t
*);
520 static inline struct block
*gen_false(compiler_state_t
*);
521 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
522 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
523 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
524 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
525 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
526 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
527 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
528 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
529 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
531 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
532 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
533 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
534 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
535 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
536 int, bpf_u_int32
, u_int
, u_int
);
538 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
539 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
541 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
542 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
543 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
544 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
545 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
546 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
547 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
548 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
549 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
552 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
553 struct in6_addr
*, int, int, int);
556 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
557 struct addrinfo
*, int, int);
559 static struct block
*gen_ipfrag(compiler_state_t
*);
560 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
561 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
563 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
564 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
566 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
567 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
568 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
570 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
571 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
572 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
573 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
575 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
576 static int lookup_proto(compiler_state_t
*, const char *, int);
577 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
578 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
579 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
580 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
581 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
582 static struct block
*gen_len(compiler_state_t
*, int, int);
583 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
584 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
586 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
587 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
588 bpf_u_int32
, int, int);
589 static struct block
*gen_atmtype_llc(compiler_state_t
*);
590 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
593 initchunks(compiler_state_t
*cstate
)
597 for (i
= 0; i
< NCHUNKS
; i
++) {
598 cstate
->chunks
[i
].n_left
= 0;
599 cstate
->chunks
[i
].m
= NULL
;
601 cstate
->cur_chunk
= 0;
605 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
612 /* XXX Round up to nearest long. */
613 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
615 /* XXX Round up to structure boundary. */
619 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
620 if (n
> cp
->n_left
) {
622 k
= ++cstate
->cur_chunk
;
624 bpf_set_error(cstate
, "out of memory");
627 size
= CHUNK0SIZE
<< k
;
628 cp
->m
= (void *)malloc(size
);
630 bpf_set_error(cstate
, "out of memory");
633 memset((char *)cp
->m
, 0, size
);
636 bpf_set_error(cstate
, "out of memory");
641 return (void *)((char *)cp
->m
+ cp
->n_left
);
645 newchunk(compiler_state_t
*cstate
, size_t n
)
649 p
= newchunk_nolongjmp(cstate
, n
);
651 longjmp(cstate
->top_ctx
, 1);
658 freechunks(compiler_state_t
*cstate
)
662 for (i
= 0; i
< NCHUNKS
; ++i
)
663 if (cstate
->chunks
[i
].m
!= NULL
)
664 free(cstate
->chunks
[i
].m
);
668 * A strdup whose allocations are freed after code generation is over.
669 * This is used by the lexical analyzer, so it can't longjmp; it just
670 * returns NULL on an allocation error, and the callers must check
674 sdup(compiler_state_t
*cstate
, const char *s
)
676 size_t n
= strlen(s
) + 1;
677 char *cp
= newchunk_nolongjmp(cstate
, n
);
681 pcap_strlcpy(cp
, s
, n
);
685 static inline struct block
*
686 new_block(compiler_state_t
*cstate
, int code
)
690 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
697 static inline struct slist
*
698 new_stmt(compiler_state_t
*cstate
, int code
)
702 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
708 static struct block
*
709 gen_retblk(compiler_state_t
*cstate
, int v
)
711 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
717 static inline PCAP_NORETURN_DEF
void
718 syntax(compiler_state_t
*cstate
)
720 bpf_error(cstate
, "syntax error in filter expression");
724 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
725 const char *buf
, int optimize
, bpf_u_int32 mask
)
730 compiler_state_t cstate
;
731 const char * volatile xbuf
= buf
;
732 yyscan_t scanner
= NULL
;
733 volatile YY_BUFFER_STATE in_buffer
= NULL
;
738 * If this pcap_t hasn't been activated, it doesn't have a
739 * link-layer type, so we can't use it.
742 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
743 "not-yet-activated pcap_t passed to pcap_compile");
755 * If the device on which we're capturing need to be notified
756 * that a new filter is being compiled, do so.
758 * This allows them to save a copy of it, in case, for example,
759 * they're implementing a form of remote packet capture, and
760 * want the remote machine to filter out the packets in which
761 * it's sending the packets it's captured.
763 * XXX - the fact that we happen to be compiling a filter
764 * doesn't necessarily mean we'll be installing it as the
765 * filter for this pcap_t; we might be running it from userland
766 * on captured packets to do packet classification. We really
767 * need a better way of handling this, but this is all that
768 * the WinPcap remote capture code did.
770 if (p
->save_current_filter_op
!= NULL
)
771 (p
->save_current_filter_op
)(p
, buf
);
775 cstate
.no_optimize
= 0;
780 cstate
.ic
.root
= NULL
;
781 cstate
.ic
.cur_mark
= 0;
783 cstate
.error_set
= 0;
786 cstate
.netmask
= mask
;
788 cstate
.snaplen
= pcap_snapshot(p
);
789 if (cstate
.snaplen
== 0) {
790 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
791 "snaplen of 0 rejects all packets");
796 if (pcap_lex_init(&scanner
) != 0)
797 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
798 errno
, "can't initialize scanner");
799 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
802 * Associate the compiler state with the lexical analyzer
805 pcap_set_extra(&cstate
, scanner
);
807 if (init_linktype(&cstate
, p
) == -1) {
811 if (pcap_parse(scanner
, &cstate
) != 0) {
813 if (cstate
.ai
!= NULL
)
814 freeaddrinfo(cstate
.ai
);
816 if (cstate
.e
!= NULL
)
822 if (cstate
.ic
.root
== NULL
) {
824 * Catch errors reported by gen_retblk().
826 if (setjmp(cstate
.top_ctx
)) {
830 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
833 if (optimize
&& !cstate
.no_optimize
) {
834 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
839 if (cstate
.ic
.root
== NULL
||
840 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
841 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
842 "expression rejects all packets");
847 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
848 cstate
.ic
.root
, &len
, p
->errbuf
);
849 if (program
->bf_insns
== NULL
) {
854 program
->bf_len
= len
;
856 rc
= 0; /* We're all okay */
860 * Clean up everything for the lexical analyzer.
862 if (in_buffer
!= NULL
)
863 pcap__delete_buffer(in_buffer
, scanner
);
865 pcap_lex_destroy(scanner
);
868 * Clean up our own allocated memory.
876 * entry point for using the compiler with no pcap open
877 * pass in all the stuff that is needed explicitly instead.
880 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
881 struct bpf_program
*program
,
882 const char *buf
, int optimize
, bpf_u_int32 mask
)
887 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
890 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
896 * Clean up a "struct bpf_program" by freeing all the memory allocated
900 pcap_freecode(struct bpf_program
*program
)
903 if (program
->bf_insns
!= NULL
) {
904 free((char *)program
->bf_insns
);
905 program
->bf_insns
= NULL
;
910 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
911 * which of the jt and jf fields has been resolved and which is a pointer
912 * back to another unresolved block (or nil). At least one of the fields
913 * in each block is already resolved.
916 backpatch(struct block
*list
, struct block
*target
)
933 * Merge the lists in b0 and b1, using the 'sense' field to indicate
934 * which of jt and jf is the link.
937 merge(struct block
*b0
, struct block
*b1
)
939 register struct block
**p
= &b0
;
941 /* Find end of list. */
943 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
945 /* Concatenate the lists. */
950 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
952 struct block
*ppi_dlt_check
;
955 * Catch errors reported by us and routines below us, and return -1
958 if (setjmp(cstate
->top_ctx
))
962 * Insert before the statements of the first (root) block any
963 * statements needed to load the lengths of any variable-length
964 * headers into registers.
966 * XXX - a fancier strategy would be to insert those before the
967 * statements of all blocks that use those lengths and that
968 * have no predecessors that use them, so that we only compute
969 * the lengths if we need them. There might be even better
970 * approaches than that.
972 * However, those strategies would be more complicated, and
973 * as we don't generate code to compute a length if the
974 * program has no tests that use the length, and as most
975 * tests will probably use those lengths, we would just
976 * postpone computing the lengths so that it's not done
977 * for tests that fail early, and it's not clear that's
980 insert_compute_vloffsets(cstate
, p
->head
);
983 * For DLT_PPI captures, generate a check of the per-packet
984 * DLT value to make sure it's DLT_IEEE802_11.
986 * XXX - TurboCap cards use DLT_PPI for Ethernet.
987 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
988 * with appropriate Ethernet information and use that rather
989 * than using something such as DLT_PPI where you don't know
990 * the link-layer header type until runtime, which, in the
991 * general case, would force us to generate both Ethernet *and*
992 * 802.11 code (*and* anything else for which PPI is used)
993 * and choose between them early in the BPF program?
995 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
996 if (ppi_dlt_check
!= NULL
)
997 gen_and(ppi_dlt_check
, p
);
999 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
1000 p
->sense
= !p
->sense
;
1001 backpatch(p
, gen_retblk(cstate
, 0));
1002 cstate
->ic
.root
= p
->head
;
1007 gen_and(struct block
*b0
, struct block
*b1
)
1009 backpatch(b0
, b1
->head
);
1010 b0
->sense
= !b0
->sense
;
1011 b1
->sense
= !b1
->sense
;
1013 b1
->sense
= !b1
->sense
;
1014 b1
->head
= b0
->head
;
1018 gen_or(struct block
*b0
, struct block
*b1
)
1020 b0
->sense
= !b0
->sense
;
1021 backpatch(b0
, b1
->head
);
1022 b0
->sense
= !b0
->sense
;
1024 b1
->head
= b0
->head
;
1028 gen_not(struct block
*b
)
1030 b
->sense
= !b
->sense
;
1033 static struct block
*
1034 gen_cmp(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_JEQ
, 0, v
);
1040 static struct block
*
1041 gen_cmp_gt(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_JGT
, 0, v
);
1047 static struct block
*
1048 gen_cmp_ge(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
, 0, v
);
1054 static struct block
*
1055 gen_cmp_lt(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_JGE
, 1, v
);
1061 static struct block
*
1062 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1063 u_int size
, bpf_u_int32 v
)
1065 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1068 static struct block
*
1069 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1070 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1072 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1075 static struct block
*
1076 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1077 u_int size
, const u_char
*v
)
1079 register struct block
*b
, *tmp
;
1083 register const u_char
*p
= &v
[size
- 4];
1085 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1093 register const u_char
*p
= &v
[size
- 2];
1095 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1103 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1112 * AND the field of size "size" at offset "offset" relative to the header
1113 * specified by "offrel" with "mask", and compare it with the value "v"
1114 * with the test specified by "jtype"; if "reverse" is true, the test
1115 * should test the opposite of "jtype".
1117 static struct block
*
1118 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1119 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1122 struct slist
*s
, *s2
;
1125 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1127 if (mask
!= 0xffffffff) {
1128 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1133 b
= new_block(cstate
, JMP(jtype
));
1136 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1142 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1144 cstate
->pcap_fddipad
= p
->fddipad
;
1147 * We start out with only one link-layer header.
1149 cstate
->outermostlinktype
= pcap_datalink(p
);
1150 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1151 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1152 cstate
->off_outermostlinkhdr
.reg
= -1;
1154 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1155 cstate
->off_prevlinkhdr
.constant_part
= 0;
1156 cstate
->off_prevlinkhdr
.is_variable
= 0;
1157 cstate
->off_prevlinkhdr
.reg
= -1;
1159 cstate
->linktype
= cstate
->outermostlinktype
;
1160 cstate
->off_linkhdr
.constant_part
= 0;
1161 cstate
->off_linkhdr
.is_variable
= 0;
1162 cstate
->off_linkhdr
.reg
= -1;
1167 cstate
->off_linkpl
.constant_part
= 0;
1168 cstate
->off_linkpl
.is_variable
= 0;
1169 cstate
->off_linkpl
.reg
= -1;
1171 cstate
->off_linktype
.constant_part
= 0;
1172 cstate
->off_linktype
.is_variable
= 0;
1173 cstate
->off_linktype
.reg
= -1;
1176 * Assume it's not raw ATM with a pseudo-header, for now.
1179 cstate
->off_vpi
= OFFSET_NOT_SET
;
1180 cstate
->off_vci
= OFFSET_NOT_SET
;
1181 cstate
->off_proto
= OFFSET_NOT_SET
;
1182 cstate
->off_payload
= OFFSET_NOT_SET
;
1187 cstate
->is_geneve
= 0;
1190 * No variable length VLAN offset by default
1192 cstate
->is_vlan_vloffset
= 0;
1195 * And assume we're not doing SS7.
1197 cstate
->off_li
= OFFSET_NOT_SET
;
1198 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1199 cstate
->off_sio
= OFFSET_NOT_SET
;
1200 cstate
->off_opc
= OFFSET_NOT_SET
;
1201 cstate
->off_dpc
= OFFSET_NOT_SET
;
1202 cstate
->off_sls
= OFFSET_NOT_SET
;
1204 cstate
->label_stack_depth
= 0;
1205 cstate
->vlan_stack_depth
= 0;
1207 switch (cstate
->linktype
) {
1210 cstate
->off_linktype
.constant_part
= 2;
1211 cstate
->off_linkpl
.constant_part
= 6;
1212 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1213 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1216 case DLT_ARCNET_LINUX
:
1217 cstate
->off_linktype
.constant_part
= 4;
1218 cstate
->off_linkpl
.constant_part
= 8;
1219 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1220 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1224 cstate
->off_linktype
.constant_part
= 12;
1225 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1226 cstate
->off_nl
= 0; /* Ethernet II */
1227 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1232 * SLIP doesn't have a link level type. The 16 byte
1233 * header is hacked into our SLIP driver.
1235 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1236 cstate
->off_linkpl
.constant_part
= 16;
1238 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1241 case DLT_SLIP_BSDOS
:
1242 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1243 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1245 cstate
->off_linkpl
.constant_part
= 24;
1247 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1252 cstate
->off_linktype
.constant_part
= 0;
1253 cstate
->off_linkpl
.constant_part
= 4;
1255 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1259 cstate
->off_linktype
.constant_part
= 0;
1260 cstate
->off_linkpl
.constant_part
= 12;
1262 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1267 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1268 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1269 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1270 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1272 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1277 * This does no include the Ethernet header, and
1278 * only covers session state.
1280 cstate
->off_linktype
.constant_part
= 6;
1281 cstate
->off_linkpl
.constant_part
= 8;
1283 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1287 cstate
->off_linktype
.constant_part
= 5;
1288 cstate
->off_linkpl
.constant_part
= 24;
1290 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1295 * FDDI doesn't really have a link-level type field.
1296 * We set "off_linktype" to the offset of the LLC header.
1298 * To check for Ethernet types, we assume that SSAP = SNAP
1299 * is being used and pick out the encapsulated Ethernet type.
1300 * XXX - should we generate code to check for SNAP?
1302 cstate
->off_linktype
.constant_part
= 13;
1303 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1304 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1305 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1306 cstate
->off_nl
= 8; /* 802.2+SNAP */
1307 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1312 * Token Ring doesn't really have a link-level type field.
1313 * We set "off_linktype" to the offset of the LLC header.
1315 * To check for Ethernet types, we assume that SSAP = SNAP
1316 * is being used and pick out the encapsulated Ethernet type.
1317 * XXX - should we generate code to check for SNAP?
1319 * XXX - the header is actually variable-length.
1320 * Some various Linux patched versions gave 38
1321 * as "off_linktype" and 40 as "off_nl"; however,
1322 * if a token ring packet has *no* routing
1323 * information, i.e. is not source-routed, the correct
1324 * values are 20 and 22, as they are in the vanilla code.
1326 * A packet is source-routed iff the uppermost bit
1327 * of the first byte of the source address, at an
1328 * offset of 8, has the uppermost bit set. If the
1329 * packet is source-routed, the total number of bytes
1330 * of routing information is 2 plus bits 0x1F00 of
1331 * the 16-bit value at an offset of 14 (shifted right
1332 * 8 - figure out which byte that is).
1334 cstate
->off_linktype
.constant_part
= 14;
1335 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1336 cstate
->off_nl
= 8; /* 802.2+SNAP */
1337 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1340 case DLT_PRISM_HEADER
:
1341 case DLT_IEEE802_11_RADIO_AVS
:
1342 case DLT_IEEE802_11_RADIO
:
1343 cstate
->off_linkhdr
.is_variable
= 1;
1344 /* Fall through, 802.11 doesn't have a variable link
1345 * prefix but is otherwise the same. */
1348 case DLT_IEEE802_11
:
1350 * 802.11 doesn't really have a link-level type field.
1351 * We set "off_linktype.constant_part" to the offset of
1354 * To check for Ethernet types, we assume that SSAP = SNAP
1355 * is being used and pick out the encapsulated Ethernet type.
1356 * XXX - should we generate code to check for SNAP?
1358 * We also handle variable-length radio headers here.
1359 * The Prism header is in theory variable-length, but in
1360 * practice it's always 144 bytes long. However, some
1361 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1362 * sometimes or always supply an AVS header, so we
1363 * have to check whether the radio header is a Prism
1364 * header or an AVS header, so, in practice, it's
1367 cstate
->off_linktype
.constant_part
= 24;
1368 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1369 cstate
->off_linkpl
.is_variable
= 1;
1370 cstate
->off_nl
= 8; /* 802.2+SNAP */
1371 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1376 * At the moment we treat PPI the same way that we treat
1377 * normal Radiotap encoded packets. The difference is in
1378 * the function that generates the code at the beginning
1379 * to compute the header length. Since this code generator
1380 * of PPI supports bare 802.11 encapsulation only (i.e.
1381 * the encapsulated DLT should be DLT_IEEE802_11) we
1382 * generate code to check for this too.
1384 cstate
->off_linktype
.constant_part
= 24;
1385 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1386 cstate
->off_linkpl
.is_variable
= 1;
1387 cstate
->off_linkhdr
.is_variable
= 1;
1388 cstate
->off_nl
= 8; /* 802.2+SNAP */
1389 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1392 case DLT_ATM_RFC1483
:
1393 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1395 * assume routed, non-ISO PDUs
1396 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1398 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1399 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1400 * latter would presumably be treated the way PPPoE
1401 * should be, so you can do "pppoe and udp port 2049"
1402 * or "pppoa and tcp port 80" and have it check for
1403 * PPPo{A,E} and a PPP protocol of IP and....
1405 cstate
->off_linktype
.constant_part
= 0;
1406 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1407 cstate
->off_nl
= 8; /* 802.2+SNAP */
1408 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1413 * Full Frontal ATM; you get AALn PDUs with an ATM
1417 cstate
->off_vpi
= SUNATM_VPI_POS
;
1418 cstate
->off_vci
= SUNATM_VCI_POS
;
1419 cstate
->off_proto
= PROTO_POS
;
1420 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1421 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1422 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1423 cstate
->off_nl
= 8; /* 802.2+SNAP */
1424 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1430 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1431 cstate
->off_linkpl
.constant_part
= 0;
1433 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1436 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1437 cstate
->off_linktype
.constant_part
= 14;
1438 cstate
->off_linkpl
.constant_part
= 16;
1440 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1443 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1444 cstate
->off_linktype
.constant_part
= 0;
1445 cstate
->off_linkpl
.constant_part
= 20;
1447 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1452 * LocalTalk does have a 1-byte type field in the LLAP header,
1453 * but really it just indicates whether there is a "short" or
1454 * "long" DDP packet following.
1456 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1457 cstate
->off_linkpl
.constant_part
= 0;
1459 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1462 case DLT_IP_OVER_FC
:
1464 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1465 * link-level type field. We set "off_linktype" to the
1466 * offset of the LLC header.
1468 * To check for Ethernet types, we assume that SSAP = SNAP
1469 * is being used and pick out the encapsulated Ethernet type.
1470 * XXX - should we generate code to check for SNAP? RFC
1471 * 2625 says SNAP should be used.
1473 cstate
->off_linktype
.constant_part
= 16;
1474 cstate
->off_linkpl
.constant_part
= 16;
1475 cstate
->off_nl
= 8; /* 802.2+SNAP */
1476 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1481 * XXX - we should set this to handle SNAP-encapsulated
1482 * frames (NLPID of 0x80).
1484 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1485 cstate
->off_linkpl
.constant_part
= 0;
1487 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1491 * the only BPF-interesting FRF.16 frames are non-control frames;
1492 * Frame Relay has a variable length link-layer
1493 * so lets start with offset 4 for now and increments later on (FIXME);
1496 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1497 cstate
->off_linkpl
.constant_part
= 0;
1499 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1502 case DLT_APPLE_IP_OVER_IEEE1394
:
1503 cstate
->off_linktype
.constant_part
= 16;
1504 cstate
->off_linkpl
.constant_part
= 18;
1506 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1509 case DLT_SYMANTEC_FIREWALL
:
1510 cstate
->off_linktype
.constant_part
= 6;
1511 cstate
->off_linkpl
.constant_part
= 44;
1512 cstate
->off_nl
= 0; /* Ethernet II */
1513 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1516 #ifdef HAVE_NET_PFVAR_H
1518 cstate
->off_linktype
.constant_part
= 0;
1519 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1521 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1525 case DLT_JUNIPER_MFR
:
1526 case DLT_JUNIPER_MLFR
:
1527 case DLT_JUNIPER_MLPPP
:
1528 case DLT_JUNIPER_PPP
:
1529 case DLT_JUNIPER_CHDLC
:
1530 case DLT_JUNIPER_FRELAY
:
1531 cstate
->off_linktype
.constant_part
= 4;
1532 cstate
->off_linkpl
.constant_part
= 4;
1534 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1537 case DLT_JUNIPER_ATM1
:
1538 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1539 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1541 cstate
->off_nl_nosnap
= 10;
1544 case DLT_JUNIPER_ATM2
:
1545 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1546 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1548 cstate
->off_nl_nosnap
= 10;
1551 /* frames captured on a Juniper PPPoE service PIC
1552 * contain raw ethernet frames */
1553 case DLT_JUNIPER_PPPOE
:
1554 case DLT_JUNIPER_ETHER
:
1555 cstate
->off_linkpl
.constant_part
= 14;
1556 cstate
->off_linktype
.constant_part
= 16;
1557 cstate
->off_nl
= 18; /* Ethernet II */
1558 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1561 case DLT_JUNIPER_PPPOE_ATM
:
1562 cstate
->off_linktype
.constant_part
= 4;
1563 cstate
->off_linkpl
.constant_part
= 6;
1565 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1568 case DLT_JUNIPER_GGSN
:
1569 cstate
->off_linktype
.constant_part
= 6;
1570 cstate
->off_linkpl
.constant_part
= 12;
1572 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1575 case DLT_JUNIPER_ES
:
1576 cstate
->off_linktype
.constant_part
= 6;
1577 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1578 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1579 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1582 case DLT_JUNIPER_MONITOR
:
1583 cstate
->off_linktype
.constant_part
= 12;
1584 cstate
->off_linkpl
.constant_part
= 12;
1585 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1586 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1589 case DLT_BACNET_MS_TP
:
1590 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1591 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1592 cstate
->off_nl
= OFFSET_NOT_SET
;
1593 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1596 case DLT_JUNIPER_SERVICES
:
1597 cstate
->off_linktype
.constant_part
= 12;
1598 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1599 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1600 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1603 case DLT_JUNIPER_VP
:
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_ST
:
1611 cstate
->off_linktype
.constant_part
= 18;
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_ISM
:
1618 cstate
->off_linktype
.constant_part
= 8;
1619 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1620 cstate
->off_nl
= OFFSET_NOT_SET
;
1621 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1624 case DLT_JUNIPER_VS
:
1625 case DLT_JUNIPER_SRX_E2E
:
1626 case DLT_JUNIPER_FIBRECHANNEL
:
1627 case DLT_JUNIPER_ATM_CEMIC
:
1628 cstate
->off_linktype
.constant_part
= 8;
1629 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1630 cstate
->off_nl
= OFFSET_NOT_SET
;
1631 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1636 cstate
->off_li_hsl
= 4;
1637 cstate
->off_sio
= 3;
1638 cstate
->off_opc
= 4;
1639 cstate
->off_dpc
= 4;
1640 cstate
->off_sls
= 7;
1641 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1642 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1643 cstate
->off_nl
= OFFSET_NOT_SET
;
1644 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1647 case DLT_MTP2_WITH_PHDR
:
1649 cstate
->off_li_hsl
= 8;
1650 cstate
->off_sio
= 7;
1651 cstate
->off_opc
= 8;
1652 cstate
->off_dpc
= 8;
1653 cstate
->off_sls
= 11;
1654 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1655 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1656 cstate
->off_nl
= OFFSET_NOT_SET
;
1657 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1661 cstate
->off_li
= 22;
1662 cstate
->off_li_hsl
= 24;
1663 cstate
->off_sio
= 23;
1664 cstate
->off_opc
= 24;
1665 cstate
->off_dpc
= 24;
1666 cstate
->off_sls
= 27;
1667 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1668 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1669 cstate
->off_nl
= OFFSET_NOT_SET
;
1670 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1674 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1675 cstate
->off_linkpl
.constant_part
= 4;
1677 cstate
->off_nl_nosnap
= 0;
1682 * Currently, only raw "link[N:M]" filtering is supported.
1684 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1685 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1686 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1687 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1691 cstate
->off_linktype
.constant_part
= 1;
1692 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1694 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1697 case DLT_NETANALYZER
:
1698 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1699 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1700 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1701 cstate
->off_nl
= 0; /* Ethernet II */
1702 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1705 case DLT_NETANALYZER_TRANSPARENT
:
1706 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1707 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1708 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1709 cstate
->off_nl
= 0; /* Ethernet II */
1710 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1715 * For values in the range in which we've assigned new
1716 * DLT_ values, only raw "link[N:M]" filtering is supported.
1718 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1719 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1720 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1721 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1722 cstate
->off_nl
= OFFSET_NOT_SET
;
1723 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1725 bpf_set_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1731 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1736 * Load a value relative to the specified absolute offset.
1738 static struct slist
*
1739 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1740 u_int offset
, u_int size
)
1742 struct slist
*s
, *s2
;
1744 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1747 * If "s" is non-null, it has code to arrange that the X register
1748 * contains the variable part of the absolute offset, so we
1749 * generate a load relative to that, with an offset of
1750 * abs_offset->constant_part + offset.
1752 * Otherwise, we can do an absolute load with an offset of
1753 * abs_offset->constant_part + offset.
1757 * "s" points to a list of statements that puts the
1758 * variable part of the absolute offset into the X register.
1759 * Do an indirect load, to use the X register as an offset.
1761 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1762 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1766 * There is no variable part of the absolute offset, so
1767 * just do an absolute load.
1769 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1770 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1776 * Load a value relative to the beginning of the specified header.
1778 static struct slist
*
1779 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1782 struct slist
*s
, *s2
;
1785 * Squelch warnings from compilers that *don't* assume that
1786 * offrel always has a valid enum value and therefore don't
1787 * assume that we'll always go through one of the case arms.
1789 * If we have a default case, compilers that *do* assume that
1790 * will then complain about the default case code being
1793 * Damned if you do, damned if you don't.
1800 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1805 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1808 case OR_PREVLINKHDR
:
1809 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1813 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1816 case OR_PREVMPLSHDR
:
1817 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1821 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1824 case OR_LINKPL_NOSNAP
:
1825 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1829 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1834 * Load the X register with the length of the IPv4 header
1835 * (plus the offset of the link-layer header, if it's
1836 * preceded by a variable-length header such as a radio
1837 * header), in bytes.
1839 s
= gen_loadx_iphdrlen(cstate
);
1842 * Load the item at {offset of the link-layer payload} +
1843 * {offset, relative to the start of the link-layer
1844 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1845 * {specified offset}.
1847 * If the offset of the link-layer payload is variable,
1848 * the variable part of that offset is included in the
1849 * value in the X register, and we include the constant
1850 * part in the offset of the load.
1852 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1853 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1858 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1865 * Generate code to load into the X register the sum of the length of
1866 * the IPv4 header and the variable part of the offset of the link-layer
1869 static struct slist
*
1870 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1872 struct slist
*s
, *s2
;
1874 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1877 * The offset of the link-layer payload has a variable
1878 * part. "s" points to a list of statements that put
1879 * the variable part of that offset into the X register.
1881 * The 4*([k]&0xf) addressing mode can't be used, as we
1882 * don't have a constant offset, so we have to load the
1883 * value in question into the A register and add to it
1884 * the value from the X register.
1886 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1887 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1889 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1892 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1897 * The A register now contains the length of the IP header.
1898 * We need to add to it the variable part of the offset of
1899 * the link-layer payload, which is still in the X
1900 * register, and move the result into the X register.
1902 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1903 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1906 * The offset of the link-layer payload is a constant,
1907 * so no code was generated to load the (non-existent)
1908 * variable part of that offset.
1910 * This means we can use the 4*([k]&0xf) addressing
1911 * mode. Load the length of the IPv4 header, which
1912 * is at an offset of cstate->off_nl from the beginning of
1913 * the link-layer payload, and thus at an offset of
1914 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1915 * of the raw packet data, using that addressing mode.
1917 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1918 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1924 static struct block
*
1925 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1930 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1932 b
= new_block(cstate
, JMP(BPF_JEQ
));
1938 static inline struct block
*
1939 gen_true(compiler_state_t
*cstate
)
1941 return gen_uncond(cstate
, 1);
1944 static inline struct block
*
1945 gen_false(compiler_state_t
*cstate
)
1947 return gen_uncond(cstate
, 0);
1951 * Byte-swap a 32-bit number.
1952 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1953 * big-endian platforms.)
1955 #define SWAPLONG(y) \
1956 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1959 * Generate code to match a particular packet type.
1961 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1962 * value, if <= ETHERMTU. We use that to determine whether to
1963 * match the type/length field or to check the type/length field for
1964 * a value <= ETHERMTU to see whether it's a type field and then do
1965 * the appropriate test.
1967 static struct block
*
1968 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1970 struct block
*b0
, *b1
;
1976 case LLCSAP_NETBEUI
:
1978 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1979 * so we check the DSAP and SSAP.
1981 * LLCSAP_IP checks for IP-over-802.2, rather
1982 * than IP-over-Ethernet or IP-over-SNAP.
1984 * XXX - should we check both the DSAP and the
1985 * SSAP, like this, or should we check just the
1986 * DSAP, as we do for other types <= ETHERMTU
1987 * (i.e., other SAP values)?
1989 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1991 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
1999 * Ethernet_II frames, which are Ethernet
2000 * frames with a frame type of ETHERTYPE_IPX;
2002 * Ethernet_802.3 frames, which are 802.3
2003 * frames (i.e., the type/length field is
2004 * a length field, <= ETHERMTU, rather than
2005 * a type field) with the first two bytes
2006 * after the Ethernet/802.3 header being
2009 * Ethernet_802.2 frames, which are 802.3
2010 * frames with an 802.2 LLC header and
2011 * with the IPX LSAP as the DSAP in the LLC
2014 * Ethernet_SNAP frames, which are 802.3
2015 * frames with an LLC header and a SNAP
2016 * header and with an OUI of 0x000000
2017 * (encapsulated Ethernet) and a protocol
2018 * ID of ETHERTYPE_IPX in the SNAP header.
2020 * XXX - should we generate the same code both
2021 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2025 * This generates code to check both for the
2026 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2028 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2029 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2033 * Now we add code to check for SNAP frames with
2034 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2036 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2040 * Now we generate code to check for 802.3
2041 * frames in general.
2043 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2047 * Now add the check for 802.3 frames before the
2048 * check for Ethernet_802.2 and Ethernet_802.3,
2049 * as those checks should only be done on 802.3
2050 * frames, not on Ethernet frames.
2055 * Now add the check for Ethernet_II frames, and
2056 * do that before checking for the other frame
2059 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2063 case ETHERTYPE_ATALK
:
2064 case ETHERTYPE_AARP
:
2066 * EtherTalk (AppleTalk protocols on Ethernet link
2067 * layer) may use 802.2 encapsulation.
2071 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2072 * we check for an Ethernet type field less than
2073 * 1500, which means it's an 802.3 length field.
2075 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2079 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2080 * SNAP packets with an organization code of
2081 * 0x080007 (Apple, for Appletalk) and a protocol
2082 * type of ETHERTYPE_ATALK (Appletalk).
2084 * 802.2-encapsulated ETHERTYPE_AARP packets are
2085 * SNAP packets with an organization code of
2086 * 0x000000 (encapsulated Ethernet) and a protocol
2087 * type of ETHERTYPE_AARP (Appletalk ARP).
2089 if (ll_proto
== ETHERTYPE_ATALK
)
2090 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2091 else /* ll_proto == ETHERTYPE_AARP */
2092 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2096 * Check for Ethernet encapsulation (Ethertalk
2097 * phase 1?); we just check for the Ethernet
2100 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2106 if (ll_proto
<= ETHERMTU
) {
2108 * This is an LLC SAP value, so the frames
2109 * that match would be 802.2 frames.
2110 * Check that the frame is an 802.2 frame
2111 * (i.e., that the length/type field is
2112 * a length field, <= ETHERMTU) and
2113 * then check the DSAP.
2115 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2117 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2122 * This is an Ethernet type, so compare
2123 * the length/type field with it (if
2124 * the frame is an 802.2 frame, the length
2125 * field will be <= ETHERMTU, and, as
2126 * "ll_proto" is > ETHERMTU, this test
2127 * will fail and the frame won't match,
2128 * which is what we want).
2130 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2135 static struct block
*
2136 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2139 * For DLT_NULL, the link-layer header is a 32-bit word
2140 * containing an AF_ value in *host* byte order, and for
2141 * DLT_ENC, the link-layer header begins with a 32-bit
2142 * word containing an AF_ value in host byte order.
2144 * In addition, if we're reading a saved capture file,
2145 * the host byte order in the capture may not be the
2146 * same as the host byte order on this machine.
2148 * For DLT_LOOP, the link-layer header is a 32-bit
2149 * word containing an AF_ value in *network* byte order.
2151 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2153 * The AF_ value is in host byte order, but the BPF
2154 * interpreter will convert it to network byte order.
2156 * If this is a save file, and it's from a machine
2157 * with the opposite byte order to ours, we byte-swap
2160 * Then we run it through "htonl()", and generate
2161 * code to compare against the result.
2163 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2164 ll_proto
= SWAPLONG(ll_proto
);
2165 ll_proto
= htonl(ll_proto
);
2167 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2171 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2172 * or IPv6 then we have an error.
2174 static struct block
*
2175 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2180 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2183 case ETHERTYPE_IPV6
:
2184 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2191 return gen_false(cstate
);
2195 * Generate code to match a particular packet type.
2197 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2198 * value, if <= ETHERMTU. We use that to determine whether to
2199 * match the type field or to check the type field for the special
2200 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2202 static struct block
*
2203 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2205 struct block
*b0
, *b1
;
2211 case LLCSAP_NETBEUI
:
2213 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2214 * so we check the DSAP and SSAP.
2216 * LLCSAP_IP checks for IP-over-802.2, rather
2217 * than IP-over-Ethernet or IP-over-SNAP.
2219 * XXX - should we check both the DSAP and the
2220 * SSAP, like this, or should we check just the
2221 * DSAP, as we do for other types <= ETHERMTU
2222 * (i.e., other SAP values)?
2224 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2225 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2231 * Ethernet_II frames, which are Ethernet
2232 * frames with a frame type of ETHERTYPE_IPX;
2234 * Ethernet_802.3 frames, which have a frame
2235 * type of LINUX_SLL_P_802_3;
2237 * Ethernet_802.2 frames, which are 802.3
2238 * frames with an 802.2 LLC header (i.e, have
2239 * a frame type of LINUX_SLL_P_802_2) and
2240 * with the IPX LSAP as the DSAP in the LLC
2243 * Ethernet_SNAP frames, which are 802.3
2244 * frames with an LLC header and a SNAP
2245 * header and with an OUI of 0x000000
2246 * (encapsulated Ethernet) and a protocol
2247 * ID of ETHERTYPE_IPX in the SNAP header.
2249 * First, do the checks on LINUX_SLL_P_802_2
2250 * frames; generate the check for either
2251 * Ethernet_802.2 or Ethernet_SNAP frames, and
2252 * then put a check for LINUX_SLL_P_802_2 frames
2255 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2256 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2258 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2262 * Now check for 802.3 frames and OR that with
2263 * the previous test.
2265 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2269 * Now add the check for Ethernet_II frames, and
2270 * do that before checking for the other frame
2273 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2277 case ETHERTYPE_ATALK
:
2278 case ETHERTYPE_AARP
:
2280 * EtherTalk (AppleTalk protocols on Ethernet link
2281 * layer) may use 802.2 encapsulation.
2285 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2286 * we check for the 802.2 protocol type in the
2287 * "Ethernet type" field.
2289 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2292 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2293 * SNAP packets with an organization code of
2294 * 0x080007 (Apple, for Appletalk) and a protocol
2295 * type of ETHERTYPE_ATALK (Appletalk).
2297 * 802.2-encapsulated ETHERTYPE_AARP packets are
2298 * SNAP packets with an organization code of
2299 * 0x000000 (encapsulated Ethernet) and a protocol
2300 * type of ETHERTYPE_AARP (Appletalk ARP).
2302 if (ll_proto
== ETHERTYPE_ATALK
)
2303 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2304 else /* ll_proto == ETHERTYPE_AARP */
2305 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2309 * Check for Ethernet encapsulation (Ethertalk
2310 * phase 1?); we just check for the Ethernet
2313 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2319 if (ll_proto
<= ETHERMTU
) {
2321 * This is an LLC SAP value, so the frames
2322 * that match would be 802.2 frames.
2323 * Check for the 802.2 protocol type
2324 * in the "Ethernet type" field, and
2325 * then check the DSAP.
2327 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2328 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2334 * This is an Ethernet type, so compare
2335 * the length/type field with it (if
2336 * the frame is an 802.2 frame, the length
2337 * field will be <= ETHERMTU, and, as
2338 * "ll_proto" is > ETHERMTU, this test
2339 * will fail and the frame won't match,
2340 * which is what we want).
2342 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2347 static struct slist
*
2348 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2350 struct slist
*s1
, *s2
;
2351 struct slist
*sjeq_avs_cookie
;
2352 struct slist
*sjcommon
;
2355 * This code is not compatible with the optimizer, as
2356 * we are generating jmp instructions within a normal
2357 * slist of instructions
2359 cstate
->no_optimize
= 1;
2362 * Generate code to load the length of the radio header into
2363 * the register assigned to hold that length, if one has been
2364 * assigned. (If one hasn't been assigned, no code we've
2365 * generated uses that prefix, so we don't need to generate any
2368 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2369 * or always use the AVS header rather than the Prism header.
2370 * We load a 4-byte big-endian value at the beginning of the
2371 * raw packet data, and see whether, when masked with 0xFFFFF000,
2372 * it's equal to 0x80211000. If so, that indicates that it's
2373 * an AVS header (the masked-out bits are the version number).
2374 * Otherwise, it's a Prism header.
2376 * XXX - the Prism header is also, in theory, variable-length,
2377 * but no known software generates headers that aren't 144
2380 if (cstate
->off_linkhdr
.reg
!= -1) {
2384 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2388 * AND it with 0xFFFFF000.
2390 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2391 s2
->s
.k
= 0xFFFFF000;
2395 * Compare with 0x80211000.
2397 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2398 sjeq_avs_cookie
->s
.k
= 0x80211000;
2399 sappend(s1
, sjeq_avs_cookie
);
2404 * The 4 bytes at an offset of 4 from the beginning of
2405 * the AVS header are the length of the AVS header.
2406 * That field is big-endian.
2408 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2411 sjeq_avs_cookie
->s
.jt
= s2
;
2414 * Now jump to the code to allocate a register
2415 * into which to save the header length and
2416 * store the length there. (The "jump always"
2417 * instruction needs to have the k field set;
2418 * it's added to the PC, so, as we're jumping
2419 * over a single instruction, it should be 1.)
2421 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2423 sappend(s1
, sjcommon
);
2426 * Now for the code that handles the Prism header.
2427 * Just load the length of the Prism header (144)
2428 * into the A register. Have the test for an AVS
2429 * header branch here if we don't have an AVS header.
2431 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2434 sjeq_avs_cookie
->s
.jf
= s2
;
2437 * Now allocate a register to hold that value and store
2438 * it. The code for the AVS header will jump here after
2439 * loading the length of the AVS header.
2441 s2
= new_stmt(cstate
, BPF_ST
);
2442 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2444 sjcommon
->s
.jf
= s2
;
2447 * Now move it into the X register.
2449 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2457 static struct slist
*
2458 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2460 struct slist
*s1
, *s2
;
2463 * Generate code to load the length of the AVS header into
2464 * the register assigned to hold that length, if one has been
2465 * assigned. (If one hasn't been assigned, no code we've
2466 * generated uses that prefix, so we don't need to generate any
2469 if (cstate
->off_linkhdr
.reg
!= -1) {
2471 * The 4 bytes at an offset of 4 from the beginning of
2472 * the AVS header are the length of the AVS header.
2473 * That field is big-endian.
2475 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2479 * Now allocate a register to hold that value and store
2482 s2
= new_stmt(cstate
, BPF_ST
);
2483 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2487 * Now move it into the X register.
2489 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2497 static struct slist
*
2498 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2500 struct slist
*s1
, *s2
;
2503 * Generate code to load the length of the radiotap header into
2504 * the register assigned to hold that length, if one has been
2505 * assigned. (If one hasn't been assigned, no code we've
2506 * generated uses that prefix, so we don't need to generate any
2509 if (cstate
->off_linkhdr
.reg
!= -1) {
2511 * The 2 bytes at offsets of 2 and 3 from the beginning
2512 * of the radiotap header are the length of the radiotap
2513 * header; unfortunately, it's little-endian, so we have
2514 * to load it a byte at a time and construct the value.
2518 * Load the high-order byte, at an offset of 3, shift it
2519 * left a byte, and put the result in the X register.
2521 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2523 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2526 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2530 * Load the next byte, at an offset of 2, and OR the
2531 * value from the X register into it.
2533 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2536 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2540 * Now allocate a register to hold that value and store
2543 s2
= new_stmt(cstate
, BPF_ST
);
2544 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2548 * Now move it into the X register.
2550 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2559 * At the moment we treat PPI as normal Radiotap encoded
2560 * packets. The difference is in the function that generates
2561 * the code at the beginning to compute the header length.
2562 * Since this code generator of PPI supports bare 802.11
2563 * encapsulation only (i.e. the encapsulated DLT should be
2564 * DLT_IEEE802_11) we generate code to check for this too;
2565 * that's done in finish_parse().
2567 static struct slist
*
2568 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2570 struct slist
*s1
, *s2
;
2573 * Generate code to load the length of the radiotap header
2574 * into the register assigned to hold that length, if one has
2577 if (cstate
->off_linkhdr
.reg
!= -1) {
2579 * The 2 bytes at offsets of 2 and 3 from the beginning
2580 * of the radiotap header are the length of the radiotap
2581 * header; unfortunately, it's little-endian, so we have
2582 * to load it a byte at a time and construct the value.
2586 * Load the high-order byte, at an offset of 3, shift it
2587 * left a byte, and put the result in the X register.
2589 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2591 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2594 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2598 * Load the next byte, at an offset of 2, and OR the
2599 * value from the X register into it.
2601 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2604 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2608 * Now allocate a register to hold that value and store
2611 s2
= new_stmt(cstate
, BPF_ST
);
2612 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2616 * Now move it into the X register.
2618 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2627 * Load a value relative to the beginning of the link-layer header after the 802.11
2628 * header, i.e. LLC_SNAP.
2629 * The link-layer header doesn't necessarily begin at the beginning
2630 * of the packet data; there might be a variable-length prefix containing
2631 * radio information.
2633 static struct slist
*
2634 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2637 struct slist
*sjset_data_frame_1
;
2638 struct slist
*sjset_data_frame_2
;
2639 struct slist
*sjset_qos
;
2640 struct slist
*sjset_radiotap_flags_present
;
2641 struct slist
*sjset_radiotap_ext_present
;
2642 struct slist
*sjset_radiotap_tsft_present
;
2643 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2644 struct slist
*s_roundup
;
2646 if (cstate
->off_linkpl
.reg
== -1) {
2648 * No register has been assigned to the offset of
2649 * the link-layer payload, which means nobody needs
2650 * it; don't bother computing it - just return
2651 * what we already have.
2657 * This code is not compatible with the optimizer, as
2658 * we are generating jmp instructions within a normal
2659 * slist of instructions
2661 cstate
->no_optimize
= 1;
2664 * If "s" is non-null, it has code to arrange that the X register
2665 * contains the length of the prefix preceding the link-layer
2668 * Otherwise, the length of the prefix preceding the link-layer
2669 * header is "off_outermostlinkhdr.constant_part".
2673 * There is no variable-length header preceding the
2674 * link-layer header.
2676 * Load the length of the fixed-length prefix preceding
2677 * the link-layer header (if any) into the X register,
2678 * and store it in the cstate->off_linkpl.reg register.
2679 * That length is off_outermostlinkhdr.constant_part.
2681 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2682 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2686 * The X register contains the offset of the beginning of the
2687 * link-layer header; add 24, which is the minimum length
2688 * of the MAC header for a data frame, to that, and store it
2689 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2690 * which is at the offset in the X register, with an indexed load.
2692 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2694 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2697 s2
= new_stmt(cstate
, BPF_ST
);
2698 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2701 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2706 * Check the Frame Control field to see if this is a data frame;
2707 * a data frame has the 0x08 bit (b3) in that field set and the
2708 * 0x04 bit (b2) clear.
2710 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2711 sjset_data_frame_1
->s
.k
= 0x08;
2712 sappend(s
, sjset_data_frame_1
);
2715 * If b3 is set, test b2, otherwise go to the first statement of
2716 * the rest of the program.
2718 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2719 sjset_data_frame_2
->s
.k
= 0x04;
2720 sappend(s
, sjset_data_frame_2
);
2721 sjset_data_frame_1
->s
.jf
= snext
;
2724 * If b2 is not set, this is a data frame; test the QoS bit.
2725 * Otherwise, go to the first statement of the rest of the
2728 sjset_data_frame_2
->s
.jt
= snext
;
2729 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2730 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2731 sappend(s
, sjset_qos
);
2734 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2736 * Otherwise, go to the first statement of the rest of the
2739 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2740 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2742 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2745 s2
= new_stmt(cstate
, BPF_ST
);
2746 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2750 * If we have a radiotap header, look at it to see whether
2751 * there's Atheros padding between the MAC-layer header
2754 * Note: all of the fields in the radiotap header are
2755 * little-endian, so we byte-swap all of the values
2756 * we test against, as they will be loaded as big-endian
2759 * XXX - in the general case, we would have to scan through
2760 * *all* the presence bits, if there's more than one word of
2761 * presence bits. That would require a loop, meaning that
2762 * we wouldn't be able to run the filter in the kernel.
2764 * We assume here that the Atheros adapters that insert the
2765 * annoying padding don't have multiple antennae and therefore
2766 * do not generate radiotap headers with multiple presence words.
2768 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2770 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2771 * in the first presence flag word?
2773 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2777 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2778 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2779 sappend(s
, sjset_radiotap_flags_present
);
2782 * If not, skip all of this.
2784 sjset_radiotap_flags_present
->s
.jf
= snext
;
2787 * Otherwise, is the "extension" bit set in that word?
2789 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2790 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2791 sappend(s
, sjset_radiotap_ext_present
);
2792 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2795 * If so, skip all of this.
2797 sjset_radiotap_ext_present
->s
.jt
= snext
;
2800 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2802 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2803 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2804 sappend(s
, sjset_radiotap_tsft_present
);
2805 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2808 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2809 * at an offset of 16 from the beginning of the raw packet
2810 * data (8 bytes for the radiotap header and 8 bytes for
2813 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2816 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2819 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2821 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2822 sjset_tsft_datapad
->s
.k
= 0x20;
2823 sappend(s
, sjset_tsft_datapad
);
2826 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2827 * at an offset of 8 from the beginning of the raw packet
2828 * data (8 bytes for the radiotap header).
2830 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2833 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2836 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2838 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2839 sjset_notsft_datapad
->s
.k
= 0x20;
2840 sappend(s
, sjset_notsft_datapad
);
2843 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2844 * set, round the length of the 802.11 header to
2845 * a multiple of 4. Do that by adding 3 and then
2846 * dividing by and multiplying by 4, which we do by
2849 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2850 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2851 sappend(s
, s_roundup
);
2852 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2855 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2856 s2
->s
.k
= (bpf_u_int32
)~3;
2858 s2
= new_stmt(cstate
, BPF_ST
);
2859 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2862 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2863 sjset_tsft_datapad
->s
.jf
= snext
;
2864 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2865 sjset_notsft_datapad
->s
.jf
= snext
;
2867 sjset_qos
->s
.jf
= snext
;
2873 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2877 /* There is an implicit dependency between the link
2878 * payload and link header since the payload computation
2879 * includes the variable part of the header. Therefore,
2880 * if nobody else has allocated a register for the link
2881 * header and we need it, do it now. */
2882 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2883 cstate
->off_linkhdr
.reg
== -1)
2884 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2887 * For link-layer types that have a variable-length header
2888 * preceding the link-layer header, generate code to load
2889 * the offset of the link-layer header into the register
2890 * assigned to that offset, if any.
2892 * XXX - this, and the next switch statement, won't handle
2893 * encapsulation of 802.11 or 802.11+radio information in
2894 * some other protocol stack. That's significantly more
2897 switch (cstate
->outermostlinktype
) {
2899 case DLT_PRISM_HEADER
:
2900 s
= gen_load_prism_llprefixlen(cstate
);
2903 case DLT_IEEE802_11_RADIO_AVS
:
2904 s
= gen_load_avs_llprefixlen(cstate
);
2907 case DLT_IEEE802_11_RADIO
:
2908 s
= gen_load_radiotap_llprefixlen(cstate
);
2912 s
= gen_load_ppi_llprefixlen(cstate
);
2921 * For link-layer types that have a variable-length link-layer
2922 * header, generate code to load the offset of the link-layer
2923 * payload into the register assigned to that offset, if any.
2925 switch (cstate
->outermostlinktype
) {
2927 case DLT_IEEE802_11
:
2928 case DLT_PRISM_HEADER
:
2929 case DLT_IEEE802_11_RADIO_AVS
:
2930 case DLT_IEEE802_11_RADIO
:
2932 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2937 * If there is no initialization yet and we need variable
2938 * length offsets for VLAN, initialize them to zero
2940 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2943 if (cstate
->off_linkpl
.reg
== -1)
2944 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2945 if (cstate
->off_linktype
.reg
== -1)
2946 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2948 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2950 s2
= new_stmt(cstate
, BPF_ST
);
2951 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2953 s2
= new_stmt(cstate
, BPF_ST
);
2954 s2
->s
.k
= cstate
->off_linktype
.reg
;
2959 * If we have any offset-loading code, append all the
2960 * existing statements in the block to those statements,
2961 * and make the resulting list the list of statements
2965 sappend(s
, b
->stmts
);
2970 static struct block
*
2971 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2973 struct slist
*s_load_dlt
;
2976 if (cstate
->linktype
== DLT_PPI
)
2978 /* Create the statements that check for the DLT
2980 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2981 s_load_dlt
->s
.k
= 4;
2983 b
= new_block(cstate
, JMP(BPF_JEQ
));
2985 b
->stmts
= s_load_dlt
;
2986 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2997 * Take an absolute offset, and:
2999 * if it has no variable part, return NULL;
3001 * if it has a variable part, generate code to load the register
3002 * containing that variable part into the X register, returning
3003 * a pointer to that code - if no register for that offset has
3004 * been allocated, allocate it first.
3006 * (The code to set that register will be generated later, but will
3007 * be placed earlier in the code sequence.)
3009 static struct slist
*
3010 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3014 if (off
->is_variable
) {
3015 if (off
->reg
== -1) {
3017 * We haven't yet assigned a register for the
3018 * variable part of the offset of the link-layer
3019 * header; allocate one.
3021 off
->reg
= alloc_reg(cstate
);
3025 * Load the register containing the variable part of the
3026 * offset of the link-layer header into the X register.
3028 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3033 * That offset isn't variable, there's no variable part,
3034 * so we don't need to generate any code.
3041 * Map an Ethernet type to the equivalent PPP type.
3044 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3052 case ETHERTYPE_IPV6
:
3053 ll_proto
= PPP_IPV6
;
3057 ll_proto
= PPP_DECNET
;
3060 case ETHERTYPE_ATALK
:
3061 ll_proto
= PPP_APPLE
;
3074 * I'm assuming the "Bridging PDU"s that go
3075 * over PPP are Spanning Tree Protocol
3078 ll_proto
= PPP_BRPDU
;
3089 * Generate any tests that, for encapsulation of a link-layer packet
3090 * inside another protocol stack, need to be done to check for those
3091 * link-layer packets (and that haven't already been done by a check
3092 * for that encapsulation).
3094 static struct block
*
3095 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3099 if (cstate
->is_geneve
)
3100 return gen_geneve_ll_check(cstate
);
3102 switch (cstate
->prevlinktype
) {
3106 * This is LANE-encapsulated Ethernet; check that the LANE
3107 * packet doesn't begin with an LE Control marker, i.e.
3108 * that it's data, not a control message.
3110 * (We've already generated a test for LANE.)
3112 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3118 * No such tests are necessary.
3126 * The three different values we should check for when checking for an
3127 * IPv6 packet with DLT_NULL.
3129 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3130 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3131 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3134 * Generate code to match a particular packet type by matching the
3135 * link-layer type field or fields in the 802.2 LLC header.
3137 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3138 * value, if <= ETHERMTU.
3140 static struct block
*
3141 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3143 struct block
*b0
, *b1
, *b2
;
3144 const char *description
;
3146 /* are we checking MPLS-encapsulated packets? */
3147 if (cstate
->label_stack_depth
> 0)
3148 return gen_mpls_linktype(cstate
, ll_proto
);
3150 switch (cstate
->linktype
) {
3153 case DLT_NETANALYZER
:
3154 case DLT_NETANALYZER_TRANSPARENT
:
3155 /* Geneve has an EtherType regardless of whether there is an
3157 if (!cstate
->is_geneve
)
3158 b0
= gen_prevlinkhdr_check(cstate
);
3162 b1
= gen_ether_linktype(cstate
, ll_proto
);
3172 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3176 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3180 case DLT_IEEE802_11
:
3181 case DLT_PRISM_HEADER
:
3182 case DLT_IEEE802_11_RADIO_AVS
:
3183 case DLT_IEEE802_11_RADIO
:
3186 * Check that we have a data frame.
3188 b0
= gen_check_802_11_data_frame(cstate
);
3191 * Now check for the specified link-layer type.
3193 b1
= gen_llc_linktype(cstate
, ll_proto
);
3200 * XXX - check for LLC frames.
3202 return gen_llc_linktype(cstate
, ll_proto
);
3207 * XXX - check for LLC PDUs, as per IEEE 802.5.
3209 return gen_llc_linktype(cstate
, ll_proto
);
3212 case DLT_ATM_RFC1483
:
3214 case DLT_IP_OVER_FC
:
3215 return gen_llc_linktype(cstate
, ll_proto
);
3220 * Check for an LLC-encapsulated version of this protocol;
3221 * if we were checking for LANE, linktype would no longer
3224 * Check for LLC encapsulation and then check the protocol.
3226 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3227 b1
= gen_llc_linktype(cstate
, ll_proto
);
3233 return gen_linux_sll_linktype(cstate
, ll_proto
);
3237 case DLT_SLIP_BSDOS
:
3240 * These types don't provide any type field; packets
3241 * are always IPv4 or IPv6.
3243 * XXX - for IPv4, check for a version number of 4, and,
3244 * for IPv6, check for a version number of 6?
3249 /* Check for a version number of 4. */
3250 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3252 case ETHERTYPE_IPV6
:
3253 /* Check for a version number of 6. */
3254 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3257 return gen_false(cstate
); /* always false */
3263 * Raw IPv4, so no type field.
3265 if (ll_proto
== ETHERTYPE_IP
)
3266 return gen_true(cstate
); /* always true */
3268 /* Checking for something other than IPv4; always false */
3269 return gen_false(cstate
);
3274 * Raw IPv6, so no type field.
3276 if (ll_proto
== ETHERTYPE_IPV6
)
3277 return gen_true(cstate
); /* always true */
3279 /* Checking for something other than IPv6; always false */
3280 return gen_false(cstate
);
3285 case DLT_PPP_SERIAL
:
3288 * We use Ethernet protocol types inside libpcap;
3289 * map them to the corresponding PPP protocol types.
3291 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3292 ethertype_to_ppptype(ll_proto
));
3297 * We use Ethernet protocol types inside libpcap;
3298 * map them to the corresponding PPP protocol types.
3304 * Also check for Van Jacobson-compressed IP.
3305 * XXX - do this for other forms of PPP?
3307 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3308 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3310 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3315 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3316 ethertype_to_ppptype(ll_proto
));
3326 return (gen_loopback_linktype(cstate
, AF_INET
));
3328 case ETHERTYPE_IPV6
:
3330 * AF_ values may, unfortunately, be platform-
3331 * dependent; AF_INET isn't, because everybody
3332 * used 4.2BSD's value, but AF_INET6 is, because
3333 * 4.2BSD didn't have a value for it (given that
3334 * IPv6 didn't exist back in the early 1980's),
3335 * and they all picked their own values.
3337 * This means that, if we're reading from a
3338 * savefile, we need to check for all the
3341 * If we're doing a live capture, we only need
3342 * to check for this platform's value; however,
3343 * Npcap uses 24, which isn't Windows's AF_INET6
3344 * value. (Given the multiple different values,
3345 * programs that read pcap files shouldn't be
3346 * checking for their platform's AF_INET6 value
3347 * anyway, they should check for all of the
3348 * possible values. and they might as well do
3349 * that even for live captures.)
3351 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3353 * Savefile - check for all three
3354 * possible IPv6 values.
3356 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3357 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3359 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3364 * Live capture, so we only need to
3365 * check for the value used on this
3370 * Npcap doesn't use Windows's AF_INET6,
3371 * as that collides with AF_IPX on
3372 * some BSDs (both have the value 23).
3373 * Instead, it uses 24.
3375 return (gen_loopback_linktype(cstate
, 24));
3378 return (gen_loopback_linktype(cstate
, AF_INET6
));
3379 #else /* AF_INET6 */
3381 * I guess this platform doesn't support
3382 * IPv6, so we just reject all packets.
3384 return gen_false(cstate
);
3385 #endif /* AF_INET6 */
3391 * Not a type on which we support filtering.
3392 * XXX - support those that have AF_ values
3393 * #defined on this platform, at least?
3395 return gen_false(cstate
);
3398 #ifdef HAVE_NET_PFVAR_H
3401 * af field is host byte order in contrast to the rest of
3404 if (ll_proto
== ETHERTYPE_IP
)
3405 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3407 else if (ll_proto
== ETHERTYPE_IPV6
)
3408 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3411 return gen_false(cstate
);
3413 #endif /* HAVE_NET_PFVAR_H */
3416 case DLT_ARCNET_LINUX
:
3418 * XXX should we check for first fragment if the protocol
3424 return gen_false(cstate
);
3426 case ETHERTYPE_IPV6
:
3427 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3431 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3433 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3439 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3441 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3446 case ETHERTYPE_REVARP
:
3447 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3450 case ETHERTYPE_ATALK
:
3451 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3458 case ETHERTYPE_ATALK
:
3459 return gen_true(cstate
);
3461 return gen_false(cstate
);
3467 * XXX - assumes a 2-byte Frame Relay header with
3468 * DLCI and flags. What if the address is longer?
3474 * Check for the special NLPID for IP.
3476 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3478 case ETHERTYPE_IPV6
:
3480 * Check for the special NLPID for IPv6.
3482 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3486 * Check for several OSI protocols.
3488 * Frame Relay packets typically have an OSI
3489 * NLPID at the beginning; we check for each
3492 * What we check for is the NLPID and a frame
3493 * control field of UI, i.e. 0x03 followed
3496 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3497 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3498 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3504 return gen_false(cstate
);
3509 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3511 case DLT_JUNIPER_MFR
:
3512 case DLT_JUNIPER_MLFR
:
3513 case DLT_JUNIPER_MLPPP
:
3514 case DLT_JUNIPER_ATM1
:
3515 case DLT_JUNIPER_ATM2
:
3516 case DLT_JUNIPER_PPPOE
:
3517 case DLT_JUNIPER_PPPOE_ATM
:
3518 case DLT_JUNIPER_GGSN
:
3519 case DLT_JUNIPER_ES
:
3520 case DLT_JUNIPER_MONITOR
:
3521 case DLT_JUNIPER_SERVICES
:
3522 case DLT_JUNIPER_ETHER
:
3523 case DLT_JUNIPER_PPP
:
3524 case DLT_JUNIPER_FRELAY
:
3525 case DLT_JUNIPER_CHDLC
:
3526 case DLT_JUNIPER_VP
:
3527 case DLT_JUNIPER_ST
:
3528 case DLT_JUNIPER_ISM
:
3529 case DLT_JUNIPER_VS
:
3530 case DLT_JUNIPER_SRX_E2E
:
3531 case DLT_JUNIPER_FIBRECHANNEL
:
3532 case DLT_JUNIPER_ATM_CEMIC
:
3534 /* just lets verify the magic number for now -
3535 * on ATM we may have up to 6 different encapsulations on the wire
3536 * and need a lot of heuristics to figure out that the payload
3539 * FIXME encapsulation specific BPF_ filters
3541 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3543 case DLT_BACNET_MS_TP
:
3544 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3547 return gen_ipnet_linktype(cstate
, ll_proto
);
3549 case DLT_LINUX_IRDA
:
3550 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3553 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3556 case DLT_MTP2_WITH_PHDR
:
3557 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3560 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3563 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3565 case DLT_LINUX_LAPD
:
3566 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3568 case DLT_USB_FREEBSD
:
3570 case DLT_USB_LINUX_MMAPPED
:
3572 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3574 case DLT_BLUETOOTH_HCI_H4
:
3575 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3576 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3579 case DLT_CAN_SOCKETCAN
:
3580 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3582 case DLT_IEEE802_15_4
:
3583 case DLT_IEEE802_15_4_LINUX
:
3584 case DLT_IEEE802_15_4_NONASK_PHY
:
3585 case DLT_IEEE802_15_4_NOFCS
:
3586 case DLT_IEEE802_15_4_TAP
:
3587 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3589 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3590 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3593 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3596 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3598 case DLT_IPMB_KONTRON
:
3599 case DLT_IPMB_LINUX
:
3600 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3603 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3606 /* Using the fixed-size NFLOG header it is possible to tell only
3607 * the address family of the packet, other meaningful data is
3608 * either missing or behind TLVs.
3610 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3614 * Does this link-layer header type have a field
3615 * indicating the type of the next protocol? If
3616 * so, off_linktype.constant_part will be the offset of that
3617 * field in the packet; if not, it will be OFFSET_NOT_SET.
3619 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3621 * Yes; assume it's an Ethernet type. (If
3622 * it's not, it needs to be handled specially
3625 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3629 * No; report an error.
3631 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3632 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3640 * Check for an LLC SNAP packet with a given organization code and
3641 * protocol type; we check the entire contents of the 802.2 LLC and
3642 * snap headers, checking for DSAP and SSAP of SNAP and a control
3643 * field of 0x03 in the LLC header, and for the specified organization
3644 * code and protocol type in the SNAP header.
3646 static struct block
*
3647 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3649 u_char snapblock
[8];
3651 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3652 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3653 snapblock
[2] = 0x03; /* control = UI */
3654 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3655 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3656 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3657 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3658 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3659 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3663 * Generate code to match frames with an LLC header.
3665 static struct block
*
3666 gen_llc_internal(compiler_state_t
*cstate
)
3668 struct block
*b0
, *b1
;
3670 switch (cstate
->linktype
) {
3674 * We check for an Ethernet type field less than
3675 * 1500, which means it's an 802.3 length field.
3677 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3681 * Now check for the purported DSAP and SSAP not being
3682 * 0xFF, to rule out NetWare-over-802.3.
3684 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3691 * We check for LLC traffic.
3693 b0
= gen_atmtype_llc(cstate
);
3696 case DLT_IEEE802
: /* Token Ring */
3698 * XXX - check for LLC frames.
3700 return gen_true(cstate
);
3704 * XXX - check for LLC frames.
3706 return gen_true(cstate
);
3708 case DLT_ATM_RFC1483
:
3710 * For LLC encapsulation, these are defined to have an
3713 * For VC encapsulation, they don't, but there's no
3714 * way to check for that; the protocol used on the VC
3715 * is negotiated out of band.
3717 return gen_true(cstate
);
3719 case DLT_IEEE802_11
:
3720 case DLT_PRISM_HEADER
:
3721 case DLT_IEEE802_11_RADIO
:
3722 case DLT_IEEE802_11_RADIO_AVS
:
3725 * Check that we have a data frame.
3727 b0
= gen_check_802_11_data_frame(cstate
);
3731 bpf_error(cstate
, "'llc' not supported for %s",
3732 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3738 gen_llc(compiler_state_t
*cstate
)
3741 * Catch errors reported by us and routines below us, and return NULL
3744 if (setjmp(cstate
->top_ctx
))
3747 return gen_llc_internal(cstate
);
3751 gen_llc_i(compiler_state_t
*cstate
)
3753 struct block
*b0
, *b1
;
3757 * Catch errors reported by us and routines below us, and return NULL
3760 if (setjmp(cstate
->top_ctx
))
3764 * Check whether this is an LLC frame.
3766 b0
= gen_llc_internal(cstate
);
3769 * Load the control byte and test the low-order bit; it must
3770 * be clear for I frames.
3772 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3773 b1
= new_block(cstate
, JMP(BPF_JSET
));
3782 gen_llc_s(compiler_state_t
*cstate
)
3784 struct block
*b0
, *b1
;
3787 * Catch errors reported by us and routines below us, and return NULL
3790 if (setjmp(cstate
->top_ctx
))
3794 * Check whether this is an LLC frame.
3796 b0
= gen_llc_internal(cstate
);
3799 * Now compare the low-order 2 bit of the control byte against
3800 * the appropriate value for S frames.
3802 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3808 gen_llc_u(compiler_state_t
*cstate
)
3810 struct block
*b0
, *b1
;
3813 * Catch errors reported by us and routines below us, and return NULL
3816 if (setjmp(cstate
->top_ctx
))
3820 * Check whether this is an LLC frame.
3822 b0
= gen_llc_internal(cstate
);
3825 * Now compare the low-order 2 bit of the control byte against
3826 * the appropriate value for U frames.
3828 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3834 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3836 struct block
*b0
, *b1
;
3839 * Catch errors reported by us and routines below us, and return NULL
3842 if (setjmp(cstate
->top_ctx
))
3846 * Check whether this is an LLC frame.
3848 b0
= gen_llc_internal(cstate
);
3851 * Now check for an S frame with the appropriate type.
3853 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3859 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3861 struct block
*b0
, *b1
;
3864 * Catch errors reported by us and routines below us, and return NULL
3867 if (setjmp(cstate
->top_ctx
))
3871 * Check whether this is an LLC frame.
3873 b0
= gen_llc_internal(cstate
);
3876 * Now check for a U frame with the appropriate type.
3878 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3884 * Generate code to match a particular packet type, for link-layer types
3885 * using 802.2 LLC headers.
3887 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3888 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3890 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3891 * value, if <= ETHERMTU. We use that to determine whether to
3892 * match the DSAP or both DSAP and LSAP or to check the OUI and
3893 * protocol ID in a SNAP header.
3895 static struct block
*
3896 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3899 * XXX - handle token-ring variable-length header.
3905 case LLCSAP_NETBEUI
:
3907 * XXX - should we check both the DSAP and the
3908 * SSAP, like this, or should we check just the
3909 * DSAP, as we do for other SAP values?
3911 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3912 ((ll_proto
<< 8) | ll_proto
));
3916 * XXX - are there ever SNAP frames for IPX on
3917 * non-Ethernet 802.x networks?
3919 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3921 case ETHERTYPE_ATALK
:
3923 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3924 * SNAP packets with an organization code of
3925 * 0x080007 (Apple, for Appletalk) and a protocol
3926 * type of ETHERTYPE_ATALK (Appletalk).
3928 * XXX - check for an organization code of
3929 * encapsulated Ethernet as well?
3931 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3935 * XXX - we don't have to check for IPX 802.3
3936 * here, but should we check for the IPX Ethertype?
3938 if (ll_proto
<= ETHERMTU
) {
3940 * This is an LLC SAP value, so check
3943 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
3946 * This is an Ethernet type; we assume that it's
3947 * unlikely that it'll appear in the right place
3948 * at random, and therefore check only the
3949 * location that would hold the Ethernet type
3950 * in a SNAP frame with an organization code of
3951 * 0x000000 (encapsulated Ethernet).
3953 * XXX - if we were to check for the SNAP DSAP and
3954 * LSAP, as per XXX, and were also to check for an
3955 * organization code of 0x000000 (encapsulated
3956 * Ethernet), we'd do
3958 * return gen_snap(cstate, 0x000000, ll_proto);
3960 * here; for now, we don't, as per the above.
3961 * I don't know whether it's worth the extra CPU
3962 * time to do the right check or not.
3964 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
3969 static struct block
*
3970 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3971 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
3973 struct block
*b0
, *b1
;
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
);
3994 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
3995 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4000 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4004 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4008 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4012 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4016 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4020 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4027 b0
= gen_linktype(cstate
, ll_proto
);
4028 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4034 static struct block
*
4035 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4036 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4039 struct block
*b0
, *b1
;
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
);
4061 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4062 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4067 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4071 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4075 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4079 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4083 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4087 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4094 /* this order is important */
4095 a
= (uint32_t *)addr
;
4096 m
= (uint32_t *)mask
;
4097 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4098 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4100 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4102 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4104 b0
= gen_linktype(cstate
, ll_proto
);
4110 static struct block
*
4111 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4113 register struct block
*b0
, *b1
;
4117 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4120 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4123 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4124 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4130 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4131 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4136 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4140 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4144 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4148 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4152 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4156 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4164 * Like gen_ehostop, but for DLT_FDDI
4166 static struct block
*
4167 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4169 struct block
*b0
, *b1
;
4173 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4176 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4179 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4180 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4186 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4187 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4192 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4196 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4200 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4204 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4208 bpf_error(cstate
, "'ra' is only supported on 802.11");
4212 bpf_error(cstate
, "'ta' is only supported on 802.11");
4220 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4222 static struct block
*
4223 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4225 register struct block
*b0
, *b1
;
4229 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4232 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4235 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4236 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4242 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4243 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4248 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4252 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4256 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4260 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4264 bpf_error(cstate
, "'ra' is only supported on 802.11");
4268 bpf_error(cstate
, "'ta' is only supported on 802.11");
4276 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4277 * various 802.11 + radio headers.
4279 static struct block
*
4280 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4282 register struct block
*b0
, *b1
, *b2
;
4283 register struct slist
*s
;
4285 #ifdef ENABLE_WLAN_FILTERING_PATCH
4288 * We need to disable the optimizer because the optimizer is buggy
4289 * and wipes out some LD instructions generated by the below
4290 * code to validate the Frame Control bits
4292 cstate
->no_optimize
= 1;
4293 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4300 * For control frames, there is no SA.
4302 * For management frames, SA is at an
4303 * offset of 10 from the beginning of
4306 * For data frames, SA is at an offset
4307 * of 10 from the beginning of the packet
4308 * if From DS is clear, at an offset of
4309 * 16 from the beginning of the packet
4310 * if From DS is set and To DS is clear,
4311 * and an offset of 24 from the beginning
4312 * of the packet if From DS is set and To DS
4317 * Generate the tests to be done for data frames
4320 * First, check for To DS set, i.e. check "link[1] & 0x01".
4322 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4323 b1
= new_block(cstate
, JMP(BPF_JSET
));
4324 b1
->s
.k
= 0x01; /* To DS */
4328 * If To DS is set, the SA is at 24.
4330 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4334 * Now, check for To DS not set, i.e. check
4335 * "!(link[1] & 0x01)".
4337 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4338 b2
= new_block(cstate
, JMP(BPF_JSET
));
4339 b2
->s
.k
= 0x01; /* To DS */
4344 * If To DS is not set, the SA is at 16.
4346 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4350 * Now OR together the last two checks. That gives
4351 * the complete set of checks for data frames with
4357 * Now check for From DS being set, and AND that with
4358 * the ORed-together checks.
4360 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4361 b1
= new_block(cstate
, JMP(BPF_JSET
));
4362 b1
->s
.k
= 0x02; /* From DS */
4367 * Now check for data frames with From DS not set.
4369 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4370 b2
= new_block(cstate
, JMP(BPF_JSET
));
4371 b2
->s
.k
= 0x02; /* From DS */
4376 * If From DS isn't set, the SA is at 10.
4378 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4382 * Now OR together the checks for data frames with
4383 * From DS not set and for data frames with From DS
4384 * set; that gives the checks done for data frames.
4389 * Now check for a data frame.
4390 * I.e, check "link[0] & 0x08".
4392 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4393 b1
= new_block(cstate
, JMP(BPF_JSET
));
4398 * AND that with the checks done for data frames.
4403 * If the high-order bit of the type value is 0, this
4404 * is a management frame.
4405 * I.e, check "!(link[0] & 0x08)".
4407 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4408 b2
= new_block(cstate
, JMP(BPF_JSET
));
4414 * For management frames, the SA is at 10.
4416 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4420 * OR that with the checks done for data frames.
4421 * That gives the checks done for management and
4427 * If the low-order bit of the type value is 1,
4428 * this is either a control frame or a frame
4429 * with a reserved type, and thus not a
4432 * I.e., check "!(link[0] & 0x04)".
4434 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4435 b1
= new_block(cstate
, JMP(BPF_JSET
));
4441 * AND that with the checks for data and management
4451 * For control frames, there is no DA.
4453 * For management frames, DA is at an
4454 * offset of 4 from the beginning of
4457 * For data frames, DA is at an offset
4458 * of 4 from the beginning of the packet
4459 * if To DS is clear and at an offset of
4460 * 16 from the beginning of the packet
4465 * Generate the tests to be done for data frames.
4467 * First, check for To DS set, i.e. "link[1] & 0x01".
4469 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4470 b1
= new_block(cstate
, JMP(BPF_JSET
));
4471 b1
->s
.k
= 0x01; /* To DS */
4475 * If To DS is set, the DA is at 16.
4477 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4481 * Now, check for To DS not set, i.e. check
4482 * "!(link[1] & 0x01)".
4484 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4485 b2
= new_block(cstate
, JMP(BPF_JSET
));
4486 b2
->s
.k
= 0x01; /* To DS */
4491 * If To DS is not set, the DA is at 4.
4493 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4497 * Now OR together the last two checks. That gives
4498 * the complete set of checks for data frames.
4503 * Now check for a data frame.
4504 * I.e, check "link[0] & 0x08".
4506 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4507 b1
= new_block(cstate
, JMP(BPF_JSET
));
4512 * AND that with the checks done for data frames.
4517 * If the high-order bit of the type value is 0, this
4518 * is a management frame.
4519 * I.e, check "!(link[0] & 0x08)".
4521 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4522 b2
= new_block(cstate
, JMP(BPF_JSET
));
4528 * For management frames, the DA is at 4.
4530 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4534 * OR that with the checks done for data frames.
4535 * That gives the checks done for management and
4541 * If the low-order bit of the type value is 1,
4542 * this is either a control frame or a frame
4543 * with a reserved type, and thus not a
4546 * I.e., check "!(link[0] & 0x04)".
4548 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4549 b1
= new_block(cstate
, JMP(BPF_JSET
));
4555 * AND that with the checks for data and management
4562 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4563 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4569 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4570 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4575 * XXX - add BSSID keyword?
4578 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4582 * Not present in CTS or ACK control frames.
4584 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4585 IEEE80211_FC0_TYPE_MASK
);
4587 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4588 IEEE80211_FC0_SUBTYPE_MASK
);
4590 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4591 IEEE80211_FC0_SUBTYPE_MASK
);
4595 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4601 * Not present in control frames.
4603 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4604 IEEE80211_FC0_TYPE_MASK
);
4606 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4612 * Present only if the direction mask has both "From DS"
4613 * and "To DS" set. Neither control frames nor management
4614 * frames should have both of those set, so we don't
4615 * check the frame type.
4617 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4618 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4619 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4625 * Not present in management frames; addr1 in other
4630 * If the high-order bit of the type value is 0, this
4631 * is a management frame.
4632 * I.e, check "(link[0] & 0x08)".
4634 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4635 b1
= new_block(cstate
, JMP(BPF_JSET
));
4642 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4645 * AND that with the check of addr1.
4652 * Not present in management frames; addr2, if present,
4657 * Not present in CTS or ACK control frames.
4659 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4660 IEEE80211_FC0_TYPE_MASK
);
4662 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4663 IEEE80211_FC0_SUBTYPE_MASK
);
4665 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4666 IEEE80211_FC0_SUBTYPE_MASK
);
4672 * If the high-order bit of the type value is 0, this
4673 * is a management frame.
4674 * I.e, check "(link[0] & 0x08)".
4676 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4677 b1
= new_block(cstate
, JMP(BPF_JSET
));
4682 * AND that with the check for frames other than
4683 * CTS and ACK frames.
4690 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4699 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4700 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4701 * as the RFC states.)
4703 static struct block
*
4704 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4706 register struct block
*b0
, *b1
;
4710 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4713 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4716 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4717 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4723 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4724 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4729 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4733 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4737 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4741 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4745 bpf_error(cstate
, "'ra' is only supported on 802.11");
4749 bpf_error(cstate
, "'ta' is only supported on 802.11");
4757 * This is quite tricky because there may be pad bytes in front of the
4758 * DECNET header, and then there are two possible data packet formats that
4759 * carry both src and dst addresses, plus 5 packet types in a format that
4760 * carries only the src node, plus 2 types that use a different format and
4761 * also carry just the src node.
4765 * Instead of doing those all right, we just look for data packets with
4766 * 0 or 1 bytes of padding. If you want to look at other packets, that
4767 * will require a lot more hacking.
4769 * To add support for filtering on DECNET "areas" (network numbers)
4770 * one would want to add a "mask" argument to this routine. That would
4771 * make the filter even more inefficient, although one could be clever
4772 * and not generate masking instructions if the mask is 0xFFFF.
4774 static struct block
*
4775 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4777 struct block
*b0
, *b1
, *b2
, *tmp
;
4778 u_int offset_lh
; /* offset if long header is received */
4779 u_int offset_sh
; /* offset if short header is received */
4784 offset_sh
= 1; /* follows flags */
4785 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4789 offset_sh
= 3; /* follows flags, dstnode */
4790 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4794 /* Inefficient because we do our Calvinball dance twice */
4795 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4796 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4802 /* Inefficient because we do our Calvinball dance twice */
4803 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4804 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4809 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4813 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4817 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4821 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4825 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4829 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4836 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4837 /* Check for pad = 1, long header case */
4838 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4839 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4840 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4841 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4843 /* Check for pad = 0, long header case */
4844 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4846 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4847 (bpf_u_int32
)ntohs((u_short
)addr
));
4850 /* Check for pad = 1, short header case */
4851 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4852 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4853 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4854 (bpf_u_int32
)ntohs((u_short
)addr
));
4857 /* Check for pad = 0, short header case */
4858 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4860 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4861 (bpf_u_int32
)ntohs((u_short
)addr
));
4865 /* Combine with test for cstate->linktype */
4871 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4872 * test the bottom-of-stack bit, and then check the version number
4873 * field in the IP header.
4875 static struct block
*
4876 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4878 struct block
*b0
, *b1
;
4883 /* match the bottom-of-stack bit */
4884 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4885 /* match the IPv4 version number */
4886 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4890 case ETHERTYPE_IPV6
:
4891 /* match the bottom-of-stack bit */
4892 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4893 /* match the IPv4 version number */
4894 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4899 /* FIXME add other L3 proto IDs */
4900 bpf_error(cstate
, "unsupported protocol over mpls");
4905 static struct block
*
4906 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4907 int proto
, int dir
, int type
)
4909 struct block
*b0
, *b1
;
4910 const char *typestr
;
4920 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4922 * Only check for non-IPv4 addresses if we're not
4923 * checking MPLS-encapsulated packets.
4925 if (cstate
->label_stack_depth
== 0) {
4926 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4928 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4934 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4937 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4940 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4943 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4946 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4949 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4952 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4955 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4958 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4961 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4964 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4967 return gen_dnhostop(cstate
, addr
, dir
);
4970 bpf_error(cstate
, "LAT host filtering not implemented");
4973 bpf_error(cstate
, "SCA host filtering not implemented");
4976 bpf_error(cstate
, "MOPRC host filtering not implemented");
4979 bpf_error(cstate
, "MOPDL host filtering not implemented");
4982 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4985 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4988 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4991 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4994 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4997 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5000 bpf_error(cstate
, "AARP host filtering not implemented");
5003 bpf_error(cstate
, "ISO host filtering not implemented");
5006 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5009 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5012 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5015 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5018 bpf_error(cstate
, "IPX host filtering not implemented");
5021 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5024 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5027 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5030 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5033 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5036 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5039 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5042 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5045 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5048 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5057 static struct block
*
5058 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5059 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5061 const char *typestr
;
5071 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5074 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5077 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5080 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5083 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5086 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5089 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5092 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5095 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5098 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5101 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5104 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5107 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5110 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5113 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5116 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5119 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5122 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5125 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5128 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5131 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5134 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5137 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5140 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5143 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5146 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5149 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5152 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5155 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5158 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5161 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5164 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5167 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5170 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5173 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5176 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5179 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5182 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5185 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5188 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5198 static struct block
*
5199 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5200 struct addrinfo
*alist
, int proto
, int dir
)
5202 struct block
*b0
, *b1
, *tmp
;
5203 struct addrinfo
*ai
;
5204 struct sockaddr_in
*sin
;
5207 bpf_error(cstate
, "direction applied to 'gateway'");
5214 switch (cstate
->linktype
) {
5216 case DLT_NETANALYZER
:
5217 case DLT_NETANALYZER_TRANSPARENT
:
5218 b1
= gen_prevlinkhdr_check(cstate
);
5219 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5224 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5227 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5229 case DLT_IEEE802_11
:
5230 case DLT_PRISM_HEADER
:
5231 case DLT_IEEE802_11_RADIO_AVS
:
5232 case DLT_IEEE802_11_RADIO
:
5234 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5238 * This is LLC-multiplexed traffic; if it were
5239 * LANE, cstate->linktype would have been set to
5243 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5245 case DLT_IP_OVER_FC
:
5246 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5250 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5253 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5255 * Does it have an address?
5257 if (ai
->ai_addr
!= NULL
) {
5259 * Yes. Is it an IPv4 address?
5261 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5263 * Generate an entry for it.
5265 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5266 tmp
= gen_host(cstate
,
5267 ntohl(sin
->sin_addr
.s_addr
),
5268 0xffffffff, proto
, Q_OR
, Q_HOST
);
5270 * Is it the *first* IPv4 address?
5274 * Yes, so start with it.
5279 * No, so OR it into the
5291 * No IPv4 addresses found.
5299 bpf_error(cstate
, "illegal modifier of 'gateway'");
5304 static struct block
*
5305 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5313 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5314 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5319 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5320 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5325 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5326 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5331 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5334 #ifndef IPPROTO_IGMP
5335 #define IPPROTO_IGMP 2
5339 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5342 #ifndef IPPROTO_IGRP
5343 #define IPPROTO_IGRP 9
5346 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5350 #define IPPROTO_PIM 103
5354 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5355 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5359 #ifndef IPPROTO_VRRP
5360 #define IPPROTO_VRRP 112
5364 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5367 #ifndef IPPROTO_CARP
5368 #define IPPROTO_CARP 112
5372 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5376 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5380 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5384 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5388 bpf_error(cstate
, "link layer applied in wrong context");
5391 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5395 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5399 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5403 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5407 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5411 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5415 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5419 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5422 #ifndef IPPROTO_ICMPV6
5423 #define IPPROTO_ICMPV6 58
5426 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5430 #define IPPROTO_AH 51
5433 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5434 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5439 #define IPPROTO_ESP 50
5442 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5443 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5448 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5452 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5456 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5459 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5460 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5461 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5463 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5465 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5467 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5471 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5472 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5473 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5475 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5477 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5479 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5483 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5484 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5485 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5487 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5492 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5493 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5498 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5499 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5501 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5503 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5508 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5509 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5514 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5515 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5520 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5524 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5528 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5532 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5536 bpf_error(cstate
, "'radio' is not a valid protocol type");
5545 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5548 * Catch errors reported by us and routines below us, and return NULL
5551 if (setjmp(cstate
->top_ctx
))
5554 return gen_proto_abbrev_internal(cstate
, proto
);
5557 static struct block
*
5558 gen_ipfrag(compiler_state_t
*cstate
)
5563 /* not IPv4 frag other than the first frag */
5564 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5565 b
= new_block(cstate
, JMP(BPF_JSET
));
5574 * Generate a comparison to a port value in the transport-layer header
5575 * at the specified offset from the beginning of that header.
5577 * XXX - this handles a variable-length prefix preceding the link-layer
5578 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5579 * variable-length link-layer headers (such as Token Ring or 802.11
5582 static struct block
*
5583 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5585 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5588 static struct block
*
5589 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5591 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5594 static struct block
*
5595 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5597 struct block
*b0
, *b1
, *tmp
;
5599 /* ip proto 'proto' and not a fragment other than the first fragment */
5600 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5601 b0
= gen_ipfrag(cstate
);
5606 b1
= gen_portatom(cstate
, 0, port
);
5610 b1
= gen_portatom(cstate
, 2, port
);
5614 tmp
= gen_portatom(cstate
, 0, port
);
5615 b1
= gen_portatom(cstate
, 2, port
);
5621 tmp
= gen_portatom(cstate
, 0, port
);
5622 b1
= gen_portatom(cstate
, 2, port
);
5627 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5631 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5635 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5639 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5643 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5647 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5659 static struct block
*
5660 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5662 struct block
*b0
, *b1
, *tmp
;
5667 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5668 * not LLC encapsulation with LLCSAP_IP.
5670 * For IEEE 802 networks - which includes 802.5 token ring
5671 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5672 * says that SNAP encapsulation is used, not LLC encapsulation
5675 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5676 * RFC 2225 say that SNAP encapsulation is used, not LLC
5677 * encapsulation with LLCSAP_IP.
5679 * So we always check for ETHERTYPE_IP.
5681 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5687 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5691 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5692 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5694 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5706 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5708 struct block
*b0
, *b1
, *tmp
;
5710 /* ip6 proto 'proto' */
5711 /* XXX - catch the first fragment of a fragmented packet? */
5712 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5716 b1
= gen_portatom6(cstate
, 0, port
);
5720 b1
= gen_portatom6(cstate
, 2, port
);
5724 tmp
= gen_portatom6(cstate
, 0, port
);
5725 b1
= gen_portatom6(cstate
, 2, port
);
5731 tmp
= gen_portatom6(cstate
, 0, port
);
5732 b1
= gen_portatom6(cstate
, 2, port
);
5744 static struct block
*
5745 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5747 struct block
*b0
, *b1
, *tmp
;
5749 /* link proto ip6 */
5750 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5756 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5760 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5761 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5763 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5774 /* gen_portrange code */
5775 static struct block
*
5776 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5779 struct block
*b1
, *b2
;
5783 * Reverse the order of the ports, so v1 is the lower one.
5792 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5793 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5800 static struct block
*
5801 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5802 bpf_u_int32 proto
, int dir
)
5804 struct block
*b0
, *b1
, *tmp
;
5806 /* ip proto 'proto' and not a fragment other than the first fragment */
5807 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5808 b0
= gen_ipfrag(cstate
);
5813 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5817 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5821 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5822 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5828 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5829 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5834 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5838 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5842 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5846 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5850 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5854 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5866 static struct block
*
5867 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5870 struct block
*b0
, *b1
, *tmp
;
5873 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5879 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5884 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5885 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5887 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5898 static struct block
*
5899 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5902 struct block
*b1
, *b2
;
5906 * Reverse the order of the ports, so v1 is the lower one.
5915 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5916 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5923 static struct block
*
5924 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5925 bpf_u_int32 proto
, int dir
)
5927 struct block
*b0
, *b1
, *tmp
;
5929 /* ip6 proto 'proto' */
5930 /* XXX - catch the first fragment of a fragmented packet? */
5931 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5935 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5939 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5943 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5944 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5950 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5951 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5963 static struct block
*
5964 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5967 struct block
*b0
, *b1
, *tmp
;
5969 /* link proto ip6 */
5970 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5976 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5981 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5982 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5984 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5996 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6005 v
= pcap_nametoproto(name
);
6006 if (v
== PROTO_UNDEF
)
6007 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6011 /* XXX should look up h/w protocol type based on cstate->linktype */
6012 v
= pcap_nametoeproto(name
);
6013 if (v
== PROTO_UNDEF
) {
6014 v
= pcap_nametollc(name
);
6015 if (v
== PROTO_UNDEF
)
6016 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6021 if (strcmp(name
, "esis") == 0)
6023 else if (strcmp(name
, "isis") == 0)
6025 else if (strcmp(name
, "clnp") == 0)
6028 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6040 gen_joinsp(struct stmt
**s
, int n
)
6046 static struct block
*
6047 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6049 #ifdef NO_PROTOCHAIN
6050 return gen_proto(cstate
, v
, proto
);
6052 struct block
*b0
, *b
;
6053 struct slist
*s
[100];
6054 int fix2
, fix3
, fix4
, fix5
;
6055 int ahcheck
, again
, end
;
6057 int reg2
= alloc_reg(cstate
);
6059 memset(s
, 0, sizeof(s
));
6060 fix3
= fix4
= fix5
= 0;
6067 b0
= gen_protochain(cstate
, v
, Q_IP
);
6068 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6072 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6077 * We don't handle variable-length prefixes before the link-layer
6078 * header, or variable-length link-layer headers, here yet.
6079 * We might want to add BPF instructions to do the protochain
6080 * work, to simplify that and, on platforms that have a BPF
6081 * interpreter with the new instructions, let the filtering
6082 * be done in the kernel. (We already require a modified BPF
6083 * engine to do the protochain stuff, to support backward
6084 * branches, and backward branch support is unlikely to appear
6085 * in kernel BPF engines.)
6087 if (cstate
->off_linkpl
.is_variable
)
6088 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6091 * To quote a comment in optimize.c:
6093 * "These data structures are used in a Cocke and Shwarz style
6094 * value numbering scheme. Since the flowgraph is acyclic,
6095 * exit values can be propagated from a node's predecessors
6096 * provided it is uniquely defined."
6098 * "Acyclic" means "no backward branches", which means "no
6099 * loops", so we have to turn the optimizer off.
6101 cstate
->no_optimize
= 1;
6104 * s[0] is a dummy entry to protect other BPF insn from damage
6105 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6106 * hard to find interdependency made by jump table fixup.
6109 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6114 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6117 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6118 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6120 /* X = ip->ip_hl << 2 */
6121 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6122 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6127 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6129 /* A = ip6->ip_nxt */
6130 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6131 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6133 /* X = sizeof(struct ip6_hdr) */
6134 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6140 bpf_error(cstate
, "unsupported proto to gen_protochain");
6144 /* again: if (A == v) goto end; else fall through; */
6146 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6148 s
[i
]->s
.jt
= NULL
; /*later*/
6149 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6153 #ifndef IPPROTO_NONE
6154 #define IPPROTO_NONE 59
6156 /* if (A == IPPROTO_NONE) goto end */
6157 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6158 s
[i
]->s
.jt
= NULL
; /*later*/
6159 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6160 s
[i
]->s
.k
= IPPROTO_NONE
;
6161 s
[fix5
]->s
.jf
= s
[i
];
6165 if (proto
== Q_IPV6
) {
6166 int v6start
, v6end
, v6advance
, j
;
6169 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6170 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6171 s
[i
]->s
.jt
= NULL
; /*later*/
6172 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6173 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6174 s
[fix2
]->s
.jf
= s
[i
];
6176 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6177 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6178 s
[i
]->s
.jt
= NULL
; /*later*/
6179 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6180 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6182 /* if (A == IPPROTO_ROUTING) goto v6advance */
6183 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6184 s
[i
]->s
.jt
= NULL
; /*later*/
6185 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6186 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6188 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6189 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6190 s
[i
]->s
.jt
= NULL
; /*later*/
6191 s
[i
]->s
.jf
= NULL
; /*later*/
6192 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6202 * A = P[X + packet head];
6203 * X = X + (P[X + packet head + 1] + 1) * 8;
6205 /* A = P[X + packet head] */
6206 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6207 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6210 s
[i
] = new_stmt(cstate
, BPF_ST
);
6213 /* A = P[X + packet head + 1]; */
6214 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6215 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6218 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6222 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6226 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6230 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6233 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6237 /* goto again; (must use BPF_JA for backward jump) */
6238 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6239 s
[i
]->s
.k
= again
- i
- 1;
6240 s
[i
- 1]->s
.jf
= s
[i
];
6244 for (j
= v6start
; j
<= v6end
; j
++)
6245 s
[j
]->s
.jt
= s
[v6advance
];
6248 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6250 s
[fix2
]->s
.jf
= s
[i
];
6256 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6257 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6258 s
[i
]->s
.jt
= NULL
; /*later*/
6259 s
[i
]->s
.jf
= NULL
; /*later*/
6260 s
[i
]->s
.k
= IPPROTO_AH
;
6262 s
[fix3
]->s
.jf
= s
[ahcheck
];
6269 * X = X + (P[X + 1] + 2) * 4;
6272 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
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_ST
);
6283 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6286 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6290 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6292 /* A = P[X + packet head] */
6293 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6294 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6297 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6301 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6305 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6308 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6312 /* goto again; (must use BPF_JA for backward jump) */
6313 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6314 s
[i
]->s
.k
= again
- i
- 1;
6319 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6321 s
[fix2
]->s
.jt
= s
[end
];
6322 s
[fix4
]->s
.jf
= s
[end
];
6323 s
[fix5
]->s
.jt
= s
[end
];
6330 for (i
= 0; i
< max
- 1; i
++)
6331 s
[i
]->next
= s
[i
+ 1];
6332 s
[max
- 1]->next
= NULL
;
6337 b
= new_block(cstate
, JMP(BPF_JEQ
));
6338 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6341 free_reg(cstate
, reg2
);
6348 static struct block
*
6349 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6352 struct block
*b0
, *b1
;
6355 * A data frame has the 0x08 bit (b3) in the frame control field set
6356 * and the 0x04 bit (b2) clear.
6358 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6359 b0
= new_block(cstate
, JMP(BPF_JSET
));
6363 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6364 b1
= new_block(cstate
, JMP(BPF_JSET
));
6375 * Generate code that checks whether the packet is a packet for protocol
6376 * <proto> and whether the type field in that protocol's header has
6377 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6378 * IP packet and checks the protocol number in the IP header against <v>.
6380 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6381 * against Q_IP and Q_IPV6.
6383 static struct block
*
6384 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6386 struct block
*b0
, *b1
;
6391 if (dir
!= Q_DEFAULT
)
6392 bpf_error(cstate
, "direction applied to 'proto'");
6396 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6397 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6402 return gen_linktype(cstate
, v
);
6406 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6407 * not LLC encapsulation with LLCSAP_IP.
6409 * For IEEE 802 networks - which includes 802.5 token ring
6410 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6411 * says that SNAP encapsulation is used, not LLC encapsulation
6414 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6415 * RFC 2225 say that SNAP encapsulation is used, not LLC
6416 * encapsulation with LLCSAP_IP.
6418 * So we always check for ETHERTYPE_IP.
6420 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6422 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6424 b1
= gen_protochain(cstate
, v
, Q_IP
);
6430 bpf_error(cstate
, "arp does not encapsulate another protocol");
6434 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6438 bpf_error(cstate
, "'sctp proto' is bogus");
6442 bpf_error(cstate
, "'tcp proto' is bogus");
6446 bpf_error(cstate
, "'udp proto' is bogus");
6450 bpf_error(cstate
, "'icmp proto' is bogus");
6454 bpf_error(cstate
, "'igmp proto' is bogus");
6458 bpf_error(cstate
, "'igrp proto' is bogus");
6462 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6466 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6470 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6474 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6478 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6482 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6486 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6489 * Also check for a fragment header before the final
6492 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6493 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6495 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6498 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6504 bpf_error(cstate
, "'icmp6 proto' is bogus");
6508 bpf_error(cstate
, "'ah proto' is bogus");
6512 bpf_error(cstate
, "'esp proto' is bogus");
6516 bpf_error(cstate
, "'pim proto' is bogus");
6520 bpf_error(cstate
, "'vrrp proto' is bogus");
6524 bpf_error(cstate
, "'aarp proto' is bogus");
6528 switch (cstate
->linktype
) {
6532 * Frame Relay packets typically have an OSI
6533 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6534 * generates code to check for all the OSI
6535 * NLPIDs, so calling it and then adding a check
6536 * for the particular NLPID for which we're
6537 * looking is bogus, as we can just check for
6540 * What we check for is the NLPID and a frame
6541 * control field value of UI, i.e. 0x03 followed
6544 * XXX - assumes a 2-byte Frame Relay header with
6545 * DLCI and flags. What if the address is longer?
6547 * XXX - what about SNAP-encapsulated frames?
6549 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6554 * Cisco uses an Ethertype lookalike - for OSI,
6557 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6558 /* OSI in C-HDLC is stuffed with a fudge byte */
6559 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6564 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6565 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6571 bpf_error(cstate
, "'esis proto' is bogus");
6575 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6577 * 4 is the offset of the PDU type relative to the IS-IS
6580 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6585 bpf_error(cstate
, "'clnp proto' is not supported");
6589 bpf_error(cstate
, "'stp proto' is bogus");
6593 bpf_error(cstate
, "'ipx proto' is bogus");
6597 bpf_error(cstate
, "'netbeui proto' is bogus");
6601 bpf_error(cstate
, "'l1 proto' is bogus");
6605 bpf_error(cstate
, "'l2 proto' is bogus");
6609 bpf_error(cstate
, "'iih proto' is bogus");
6613 bpf_error(cstate
, "'snp proto' is bogus");
6617 bpf_error(cstate
, "'csnp proto' is bogus");
6621 bpf_error(cstate
, "'psnp proto' is bogus");
6625 bpf_error(cstate
, "'lsp proto' is bogus");
6629 bpf_error(cstate
, "'radio proto' is bogus");
6633 bpf_error(cstate
, "'carp proto' is bogus");
6644 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6646 int proto
= q
.proto
;
6650 bpf_u_int32 mask
, addr
;
6651 struct addrinfo
*res
, *res0
;
6652 struct sockaddr_in
*sin4
;
6655 struct sockaddr_in6
*sin6
;
6656 struct in6_addr mask128
;
6658 struct block
*b
, *tmp
;
6659 int port
, real_proto
;
6663 * Catch errors reported by us and routines below us, and return NULL
6666 if (setjmp(cstate
->top_ctx
))
6672 addr
= pcap_nametonetaddr(name
);
6674 bpf_error(cstate
, "unknown network '%s'", name
);
6675 /* Left justify network addr and calculate its network mask */
6677 while (addr
&& (addr
& 0xff000000) == 0) {
6681 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6685 if (proto
== Q_LINK
) {
6686 switch (cstate
->linktype
) {
6689 case DLT_NETANALYZER
:
6690 case DLT_NETANALYZER_TRANSPARENT
:
6691 eaddr
= pcap_ether_hostton(name
);
6694 "unknown ether host '%s'", name
);
6695 tmp
= gen_prevlinkhdr_check(cstate
);
6696 b
= gen_ehostop(cstate
, eaddr
, dir
);
6703 eaddr
= pcap_ether_hostton(name
);
6706 "unknown FDDI host '%s'", name
);
6707 b
= gen_fhostop(cstate
, eaddr
, dir
);
6712 eaddr
= pcap_ether_hostton(name
);
6715 "unknown token ring host '%s'", name
);
6716 b
= gen_thostop(cstate
, eaddr
, dir
);
6720 case DLT_IEEE802_11
:
6721 case DLT_PRISM_HEADER
:
6722 case DLT_IEEE802_11_RADIO_AVS
:
6723 case DLT_IEEE802_11_RADIO
:
6725 eaddr
= pcap_ether_hostton(name
);
6728 "unknown 802.11 host '%s'", name
);
6729 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6733 case DLT_IP_OVER_FC
:
6734 eaddr
= pcap_ether_hostton(name
);
6737 "unknown Fibre Channel host '%s'", name
);
6738 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6743 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6744 } else if (proto
== Q_DECNET
) {
6745 unsigned short dn_addr
;
6747 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6749 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6751 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6756 * I don't think DECNET hosts can be multihomed, so
6757 * there is no need to build up a list of addresses
6759 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6762 memset(&mask128
, 0xff, sizeof(mask128
));
6764 res0
= res
= pcap_nametoaddrinfo(name
);
6766 bpf_error(cstate
, "unknown host '%s'", name
);
6773 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6774 tproto
== Q_DEFAULT
) {
6780 for (res
= res0
; res
; res
= res
->ai_next
) {
6781 switch (res
->ai_family
) {
6784 if (tproto
== Q_IPV6
)
6788 sin4
= (struct sockaddr_in
*)
6790 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6791 0xffffffff, tproto
, dir
, q
.addr
);
6795 if (tproto6
== Q_IP
)
6798 sin6
= (struct sockaddr_in6
*)
6800 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6801 &mask128
, tproto6
, dir
, q
.addr
);
6814 bpf_error(cstate
, "unknown host '%s'%s", name
,
6815 (proto
== Q_DEFAULT
)
6817 : " for specified address family");
6823 if (proto
!= Q_DEFAULT
&&
6824 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6825 bpf_error(cstate
, "illegal qualifier of 'port'");
6826 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6827 bpf_error(cstate
, "unknown port '%s'", name
);
6828 if (proto
== Q_UDP
) {
6829 if (real_proto
== IPPROTO_TCP
)
6830 bpf_error(cstate
, "port '%s' is tcp", name
);
6831 else if (real_proto
== IPPROTO_SCTP
)
6832 bpf_error(cstate
, "port '%s' is sctp", name
);
6834 /* override PROTO_UNDEF */
6835 real_proto
= IPPROTO_UDP
;
6837 if (proto
== Q_TCP
) {
6838 if (real_proto
== IPPROTO_UDP
)
6839 bpf_error(cstate
, "port '%s' is udp", name
);
6841 else if (real_proto
== IPPROTO_SCTP
)
6842 bpf_error(cstate
, "port '%s' is sctp", name
);
6844 /* override PROTO_UNDEF */
6845 real_proto
= IPPROTO_TCP
;
6847 if (proto
== Q_SCTP
) {
6848 if (real_proto
== IPPROTO_UDP
)
6849 bpf_error(cstate
, "port '%s' is udp", name
);
6851 else if (real_proto
== IPPROTO_TCP
)
6852 bpf_error(cstate
, "port '%s' is tcp", name
);
6854 /* override PROTO_UNDEF */
6855 real_proto
= IPPROTO_SCTP
;
6858 bpf_error(cstate
, "illegal port number %d < 0", port
);
6860 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6861 b
= gen_port(cstate
, port
, real_proto
, dir
);
6862 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6866 if (proto
!= Q_DEFAULT
&&
6867 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6868 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6869 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6870 bpf_error(cstate
, "unknown port in range '%s'", name
);
6871 if (proto
== Q_UDP
) {
6872 if (real_proto
== IPPROTO_TCP
)
6873 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6874 else if (real_proto
== IPPROTO_SCTP
)
6875 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6877 /* override PROTO_UNDEF */
6878 real_proto
= IPPROTO_UDP
;
6880 if (proto
== Q_TCP
) {
6881 if (real_proto
== IPPROTO_UDP
)
6882 bpf_error(cstate
, "port in range '%s' is udp", name
);
6883 else if (real_proto
== IPPROTO_SCTP
)
6884 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6886 /* override PROTO_UNDEF */
6887 real_proto
= IPPROTO_TCP
;
6889 if (proto
== Q_SCTP
) {
6890 if (real_proto
== IPPROTO_UDP
)
6891 bpf_error(cstate
, "port in range '%s' is udp", name
);
6892 else if (real_proto
== IPPROTO_TCP
)
6893 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6895 /* override PROTO_UNDEF */
6896 real_proto
= IPPROTO_SCTP
;
6899 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6901 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6903 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6905 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6907 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6908 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6913 eaddr
= pcap_ether_hostton(name
);
6915 bpf_error(cstate
, "unknown ether host: %s", name
);
6917 res
= pcap_nametoaddrinfo(name
);
6920 bpf_error(cstate
, "unknown host '%s'", name
);
6921 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6925 bpf_error(cstate
, "unknown host '%s'", name
);
6928 bpf_error(cstate
, "'gateway' not supported in this configuration");
6932 real_proto
= lookup_proto(cstate
, name
, proto
);
6933 if (real_proto
>= 0)
6934 return gen_proto(cstate
, real_proto
, proto
, dir
);
6936 bpf_error(cstate
, "unknown protocol: %s", name
);
6939 real_proto
= lookup_proto(cstate
, name
, proto
);
6940 if (real_proto
>= 0)
6941 return gen_protochain(cstate
, real_proto
, proto
);
6943 bpf_error(cstate
, "unknown protocol: %s", name
);
6954 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6955 bpf_u_int32 masklen
, struct qual q
)
6957 register int nlen
, mlen
;
6961 * Catch errors reported by us and routines below us, and return NULL
6964 if (setjmp(cstate
->top_ctx
))
6967 nlen
= __pcap_atoin(s1
, &n
);
6969 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
6970 /* Promote short ipaddr */
6974 mlen
= __pcap_atoin(s2
, &m
);
6976 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
6977 /* Promote short ipaddr */
6980 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6983 /* Convert mask len to mask */
6985 bpf_error(cstate
, "mask length must be <= 32");
6988 * X << 32 is not guaranteed by C to be 0; it's
6993 m
= 0xffffffff << (32 - masklen
);
6995 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7002 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7005 bpf_error(cstate
, "Mask syntax for networks only");
7012 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7020 * Catch errors reported by us and routines below us, and return NULL
7023 if (setjmp(cstate
->top_ctx
))
7030 else if (q
.proto
== Q_DECNET
) {
7031 vlen
= __pcap_atodn(s
, &v
);
7033 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7035 vlen
= __pcap_atoin(s
, &v
);
7037 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7045 if (proto
== Q_DECNET
)
7046 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7047 else if (proto
== Q_LINK
) {
7048 bpf_error(cstate
, "illegal link layer address");
7051 if (s
== NULL
&& q
.addr
== Q_NET
) {
7052 /* Promote short net number */
7053 while (v
&& (v
& 0xff000000) == 0) {
7058 /* Promote short ipaddr */
7060 mask
<<= 32 - vlen
;
7062 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7067 proto
= IPPROTO_UDP
;
7068 else if (proto
== Q_TCP
)
7069 proto
= IPPROTO_TCP
;
7070 else if (proto
== Q_SCTP
)
7071 proto
= IPPROTO_SCTP
;
7072 else if (proto
== Q_DEFAULT
)
7073 proto
= PROTO_UNDEF
;
7075 bpf_error(cstate
, "illegal qualifier of 'port'");
7078 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7082 b
= gen_port(cstate
, v
, proto
, dir
);
7083 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7089 proto
= IPPROTO_UDP
;
7090 else if (proto
== Q_TCP
)
7091 proto
= IPPROTO_TCP
;
7092 else if (proto
== Q_SCTP
)
7093 proto
= IPPROTO_SCTP
;
7094 else if (proto
== Q_DEFAULT
)
7095 proto
= PROTO_UNDEF
;
7097 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7100 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7104 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7105 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7110 bpf_error(cstate
, "'gateway' requires a name");
7114 return gen_proto(cstate
, v
, proto
, dir
);
7117 return gen_protochain(cstate
, v
, proto
);
7132 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7133 bpf_u_int32 masklen
, struct qual q
)
7135 struct addrinfo
*res
;
7136 struct in6_addr
*addr
;
7137 struct in6_addr mask
;
7142 * Catch errors reported by us and routines below us, and return NULL
7145 if (setjmp(cstate
->top_ctx
))
7149 bpf_error(cstate
, "no mask %s supported", s2
);
7151 res
= pcap_nametoaddrinfo(s1
);
7153 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7156 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7157 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7159 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7160 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
.s6_addr
) * 8));
7161 memset(&mask
, 0, sizeof(mask
));
7162 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7164 mask
.s6_addr
[masklen
/ 8] =
7165 (0xff << (8 - masklen
% 8)) & 0xff;
7168 a
= (uint32_t *)addr
;
7169 m
= (uint32_t *)&mask
;
7170 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7171 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7172 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7180 bpf_error(cstate
, "Mask syntax for networks only");
7184 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7190 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7197 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7199 struct block
*b
, *tmp
;
7202 * Catch errors reported by us and routines below us, and return NULL
7205 if (setjmp(cstate
->top_ctx
))
7208 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7209 cstate
->e
= pcap_ether_aton(s
);
7210 if (cstate
->e
== NULL
)
7211 bpf_error(cstate
, "malloc");
7212 switch (cstate
->linktype
) {
7214 case DLT_NETANALYZER
:
7215 case DLT_NETANALYZER_TRANSPARENT
:
7216 tmp
= gen_prevlinkhdr_check(cstate
);
7217 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7222 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7225 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7227 case DLT_IEEE802_11
:
7228 case DLT_PRISM_HEADER
:
7229 case DLT_IEEE802_11_RADIO_AVS
:
7230 case DLT_IEEE802_11_RADIO
:
7232 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7234 case DLT_IP_OVER_FC
:
7235 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7240 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7247 bpf_error(cstate
, "ethernet address used in non-ether expression");
7252 sappend(struct slist
*s0
, struct slist
*s1
)
7255 * This is definitely not the best way to do this, but the
7256 * lists will rarely get long.
7263 static struct slist
*
7264 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7268 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7273 static struct slist
*
7274 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7278 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7284 * Modify "index" to use the value stored into its register as an
7285 * offset relative to the beginning of the header for the protocol
7286 * "proto", and allocate a register and put an item "size" bytes long
7287 * (1, 2, or 4) at that offset into that register, making it the register
7290 static struct arth
*
7291 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7295 struct slist
*s
, *tmp
;
7297 int regno
= alloc_reg(cstate
);
7299 free_reg(cstate
, inst
->regno
);
7303 bpf_error(cstate
, "data size must be 1, 2, or 4");
7320 bpf_error(cstate
, "unsupported index operation");
7324 * The offset is relative to the beginning of the packet
7325 * data, if we have a radio header. (If we don't, this
7328 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7329 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7330 cstate
->linktype
!= DLT_PRISM_HEADER
)
7331 bpf_error(cstate
, "radio information not present in capture");
7334 * Load into the X register the offset computed into the
7335 * register specified by "index".
7337 s
= xfer_to_x(cstate
, inst
);
7340 * Load the item at that offset.
7342 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7344 sappend(inst
->s
, s
);
7349 * The offset is relative to the beginning of
7350 * the link-layer header.
7352 * XXX - what about ATM LANE? Should the index be
7353 * relative to the beginning of the AAL5 frame, so
7354 * that 0 refers to the beginning of the LE Control
7355 * field, or relative to the beginning of the LAN
7356 * frame, so that 0 refers, for Ethernet LANE, to
7357 * the beginning of the destination address?
7359 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7362 * If "s" is non-null, it has code to arrange that the
7363 * X register contains the length of the prefix preceding
7364 * the link-layer header. Add to it the offset computed
7365 * into the register specified by "index", and move that
7366 * into the X register. Otherwise, just load into the X
7367 * register the offset computed into the register specified
7371 sappend(s
, xfer_to_a(cstate
, inst
));
7372 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7373 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7375 s
= xfer_to_x(cstate
, inst
);
7378 * Load the item at the sum of the offset we've put in the
7379 * X register and the offset of the start of the link
7380 * layer header (which is 0 if the radio header is
7381 * variable-length; that header length is what we put
7382 * into the X register and then added to the index).
7384 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7385 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7387 sappend(inst
->s
, s
);
7401 * The offset is relative to the beginning of
7402 * the network-layer header.
7403 * XXX - are there any cases where we want
7404 * cstate->off_nl_nosnap?
7406 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7409 * If "s" is non-null, it has code to arrange that the
7410 * X register contains the variable part of the offset
7411 * of the link-layer payload. Add to it the offset
7412 * computed into the register specified by "index",
7413 * and move that into the X register. Otherwise, just
7414 * load into the X register the offset computed into
7415 * the register specified by "index".
7418 sappend(s
, xfer_to_a(cstate
, inst
));
7419 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7420 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7422 s
= xfer_to_x(cstate
, inst
);
7425 * Load the item at the sum of the offset we've put in the
7426 * X register, the offset of the start of the network
7427 * layer header from the beginning of the link-layer
7428 * payload, and the constant part of the offset of the
7429 * start of the link-layer payload.
7431 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7432 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7434 sappend(inst
->s
, s
);
7437 * Do the computation only if the packet contains
7438 * the protocol in question.
7440 b
= gen_proto_abbrev_internal(cstate
, proto
);
7442 gen_and(inst
->b
, b
);
7456 * The offset is relative to the beginning of
7457 * the transport-layer header.
7459 * Load the X register with the length of the IPv4 header
7460 * (plus the offset of the link-layer header, if it's
7461 * a variable-length header), in bytes.
7463 * XXX - are there any cases where we want
7464 * cstate->off_nl_nosnap?
7465 * XXX - we should, if we're built with
7466 * IPv6 support, generate code to load either
7467 * IPv4, IPv6, or both, as appropriate.
7469 s
= gen_loadx_iphdrlen(cstate
);
7472 * The X register now contains the sum of the variable
7473 * part of the offset of the link-layer payload and the
7474 * length of the network-layer header.
7476 * Load into the A register the offset relative to
7477 * the beginning of the transport layer header,
7478 * add the X register to that, move that to the
7479 * X register, and load with an offset from the
7480 * X register equal to the sum of the constant part of
7481 * the offset of the link-layer payload and the offset,
7482 * relative to the beginning of the link-layer payload,
7483 * of the network-layer header.
7485 sappend(s
, xfer_to_a(cstate
, inst
));
7486 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7487 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7488 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7489 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7490 sappend(inst
->s
, s
);
7493 * Do the computation only if the packet contains
7494 * the protocol in question - which is true only
7495 * if this is an IP datagram and is the first or
7496 * only fragment of that datagram.
7498 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7500 gen_and(inst
->b
, b
);
7501 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7506 * Do the computation only if the packet contains
7507 * the protocol in question.
7509 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7511 gen_and(inst
->b
, b
);
7516 * Check if we have an icmp6 next header
7518 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7520 gen_and(inst
->b
, b
);
7525 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7527 * If "s" is non-null, it has code to arrange that the
7528 * X register contains the variable part of the offset
7529 * of the link-layer payload. Add to it the offset
7530 * computed into the register specified by "index",
7531 * and move that into the X register. Otherwise, just
7532 * load into the X register the offset computed into
7533 * the register specified by "index".
7536 sappend(s
, xfer_to_a(cstate
, inst
));
7537 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7538 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7540 s
= xfer_to_x(cstate
, inst
);
7544 * Load the item at the sum of the offset we've put in the
7545 * X register, the offset of the start of the network
7546 * layer header from the beginning of the link-layer
7547 * payload, and the constant part of the offset of the
7548 * start of the link-layer payload.
7550 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7551 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7554 sappend(inst
->s
, s
);
7558 inst
->regno
= regno
;
7559 s
= new_stmt(cstate
, BPF_ST
);
7561 sappend(inst
->s
, s
);
7567 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7571 * Catch errors reported by us and routines below us, and return NULL
7574 if (setjmp(cstate
->top_ctx
))
7577 return gen_load_internal(cstate
, proto
, inst
, size
);
7580 static struct block
*
7581 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7582 struct arth
*a1
, int reversed
)
7584 struct slist
*s0
, *s1
, *s2
;
7585 struct block
*b
, *tmp
;
7587 s0
= xfer_to_x(cstate
, a1
);
7588 s1
= xfer_to_a(cstate
, a0
);
7589 if (code
== BPF_JEQ
) {
7590 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7591 b
= new_block(cstate
, JMP(code
));
7595 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7601 sappend(a0
->s
, a1
->s
);
7605 free_reg(cstate
, a0
->regno
);
7606 free_reg(cstate
, a1
->regno
);
7608 /* 'and' together protocol checks */
7611 gen_and(a0
->b
, tmp
= a1
->b
);
7625 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7626 struct arth
*a1
, int reversed
)
7629 * Catch errors reported by us and routines below us, and return NULL
7632 if (setjmp(cstate
->top_ctx
))
7635 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7639 gen_loadlen(compiler_state_t
*cstate
)
7646 * Catch errors reported by us and routines below us, and return NULL
7649 if (setjmp(cstate
->top_ctx
))
7652 regno
= alloc_reg(cstate
);
7653 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7654 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7655 s
->next
= new_stmt(cstate
, BPF_ST
);
7656 s
->next
->s
.k
= regno
;
7663 static struct arth
*
7664 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7670 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7672 reg
= alloc_reg(cstate
);
7674 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7676 s
->next
= new_stmt(cstate
, BPF_ST
);
7685 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7688 * Catch errors reported by us and routines below us, and return NULL
7691 if (setjmp(cstate
->top_ctx
))
7694 return gen_loadi_internal(cstate
, val
);
7698 * The a_arg dance is to avoid annoying whining by compilers that
7699 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7700 * It's not *used* after setjmp returns.
7703 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7705 struct arth
*a
= a_arg
;
7709 * Catch errors reported by us and routines below us, and return NULL
7712 if (setjmp(cstate
->top_ctx
))
7715 s
= xfer_to_a(cstate
, a
);
7717 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7720 s
= new_stmt(cstate
, BPF_ST
);
7728 * The a0_arg dance is to avoid annoying whining by compilers that
7729 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7730 * It's not *used* after setjmp returns.
7733 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7736 struct arth
*a0
= a0_arg
;
7737 struct slist
*s0
, *s1
, *s2
;
7740 * Catch errors reported by us and routines below us, and return NULL
7743 if (setjmp(cstate
->top_ctx
))
7747 * Disallow division by, or modulus by, zero; we do this here
7748 * so that it gets done even if the optimizer is disabled.
7750 * Also disallow shifts by a value greater than 31; we do this
7751 * here, for the same reason.
7753 if (code
== BPF_DIV
) {
7754 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7755 bpf_error(cstate
, "division by zero");
7756 } else if (code
== BPF_MOD
) {
7757 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7758 bpf_error(cstate
, "modulus by zero");
7759 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7760 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7761 bpf_error(cstate
, "shift by more than 31 bits");
7763 s0
= xfer_to_x(cstate
, a1
);
7764 s1
= xfer_to_a(cstate
, a0
);
7765 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7770 sappend(a0
->s
, a1
->s
);
7772 free_reg(cstate
, a0
->regno
);
7773 free_reg(cstate
, a1
->regno
);
7775 s0
= new_stmt(cstate
, BPF_ST
);
7776 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7783 * Initialize the table of used registers and the current register.
7786 init_regs(compiler_state_t
*cstate
)
7789 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7793 * Return the next free register.
7796 alloc_reg(compiler_state_t
*cstate
)
7798 int n
= BPF_MEMWORDS
;
7801 if (cstate
->regused
[cstate
->curreg
])
7802 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7804 cstate
->regused
[cstate
->curreg
] = 1;
7805 return cstate
->curreg
;
7808 bpf_error(cstate
, "too many registers needed to evaluate expression");
7813 * Return a register to the table so it can
7817 free_reg(compiler_state_t
*cstate
, int n
)
7819 cstate
->regused
[n
] = 0;
7822 static struct block
*
7823 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7828 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7829 b
= new_block(cstate
, JMP(jmp
));
7837 gen_greater(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 return gen_len(cstate
, BPF_JGE
, n
);
7850 * Actually, this is less than or equal.
7853 gen_less(compiler_state_t
*cstate
, int n
)
7858 * Catch errors reported by us and routines below us, and return NULL
7861 if (setjmp(cstate
->top_ctx
))
7864 b
= gen_len(cstate
, BPF_JGT
, n
);
7871 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7872 * the beginning of the link-layer header.
7873 * XXX - that means you can't test values in the radiotap header, but
7874 * as that header is difficult if not impossible to parse generally
7875 * without a loop, that might not be a severe problem. A new keyword
7876 * "radio" could be added for that, although what you'd really want
7877 * would be a way of testing particular radio header values, which
7878 * would generate code appropriate to the radio header in question.
7881 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7887 * Catch errors reported by us and routines below us, and return NULL
7890 if (setjmp(cstate
->top_ctx
))
7898 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7901 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7905 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7909 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7913 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7917 b
= new_block(cstate
, JMP(BPF_JEQ
));
7924 static const u_char abroadcast
[] = { 0x0 };
7927 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7929 bpf_u_int32 hostmask
;
7930 struct block
*b0
, *b1
, *b2
;
7931 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7934 * Catch errors reported by us and routines below us, and return NULL
7937 if (setjmp(cstate
->top_ctx
))
7944 switch (cstate
->linktype
) {
7946 case DLT_ARCNET_LINUX
:
7947 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7949 case DLT_NETANALYZER
:
7950 case DLT_NETANALYZER_TRANSPARENT
:
7951 b1
= gen_prevlinkhdr_check(cstate
);
7952 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7957 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7959 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7960 case DLT_IEEE802_11
:
7961 case DLT_PRISM_HEADER
:
7962 case DLT_IEEE802_11_RADIO_AVS
:
7963 case DLT_IEEE802_11_RADIO
:
7965 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7966 case DLT_IP_OVER_FC
:
7967 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7969 bpf_error(cstate
, "not a broadcast link");
7975 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7976 * as an indication that we don't know the netmask, and fail
7979 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7980 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7981 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7982 hostmask
= ~cstate
->netmask
;
7983 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
7984 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7985 ~0 & hostmask
, hostmask
);
7990 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7995 * Generate code to test the low-order bit of a MAC address (that's
7996 * the bottom bit of the *first* byte).
7998 static struct block
*
7999 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8001 register struct block
*b0
;
8002 register struct slist
*s
;
8004 /* link[offset] & 1 != 0 */
8005 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8006 b0
= new_block(cstate
, JMP(BPF_JSET
));
8013 gen_multicast(compiler_state_t
*cstate
, int proto
)
8015 register struct block
*b0
, *b1
, *b2
;
8016 register struct slist
*s
;
8019 * Catch errors reported by us and routines below us, and return NULL
8022 if (setjmp(cstate
->top_ctx
))
8029 switch (cstate
->linktype
) {
8031 case DLT_ARCNET_LINUX
:
8032 /* all ARCnet multicasts use the same address */
8033 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8035 case DLT_NETANALYZER
:
8036 case DLT_NETANALYZER_TRANSPARENT
:
8037 b1
= gen_prevlinkhdr_check(cstate
);
8038 /* ether[0] & 1 != 0 */
8039 b0
= gen_mac_multicast(cstate
, 0);
8045 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8047 * XXX - was that referring to bit-order issues?
8049 /* fddi[1] & 1 != 0 */
8050 return gen_mac_multicast(cstate
, 1);
8052 /* tr[2] & 1 != 0 */
8053 return gen_mac_multicast(cstate
, 2);
8054 case DLT_IEEE802_11
:
8055 case DLT_PRISM_HEADER
:
8056 case DLT_IEEE802_11_RADIO_AVS
:
8057 case DLT_IEEE802_11_RADIO
:
8062 * For control frames, there is no DA.
8064 * For management frames, DA is at an
8065 * offset of 4 from the beginning of
8068 * For data frames, DA is at an offset
8069 * of 4 from the beginning of the packet
8070 * if To DS is clear and at an offset of
8071 * 16 from the beginning of the packet
8076 * Generate the tests to be done for data frames.
8078 * First, check for To DS set, i.e. "link[1] & 0x01".
8080 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8081 b1
= new_block(cstate
, JMP(BPF_JSET
));
8082 b1
->s
.k
= 0x01; /* To DS */
8086 * If To DS is set, the DA is at 16.
8088 b0
= gen_mac_multicast(cstate
, 16);
8092 * Now, check for To DS not set, i.e. check
8093 * "!(link[1] & 0x01)".
8095 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8096 b2
= new_block(cstate
, JMP(BPF_JSET
));
8097 b2
->s
.k
= 0x01; /* To DS */
8102 * If To DS is not set, the DA is at 4.
8104 b1
= gen_mac_multicast(cstate
, 4);
8108 * Now OR together the last two checks. That gives
8109 * the complete set of checks for data frames.
8114 * Now check for a data frame.
8115 * I.e, check "link[0] & 0x08".
8117 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8118 b1
= new_block(cstate
, JMP(BPF_JSET
));
8123 * AND that with the checks done for data frames.
8128 * If the high-order bit of the type value is 0, this
8129 * is a management frame.
8130 * I.e, check "!(link[0] & 0x08)".
8132 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8133 b2
= new_block(cstate
, JMP(BPF_JSET
));
8139 * For management frames, the DA is at 4.
8141 b1
= gen_mac_multicast(cstate
, 4);
8145 * OR that with the checks done for data frames.
8146 * That gives the checks done for management and
8152 * If the low-order bit of the type value is 1,
8153 * this is either a control frame or a frame
8154 * with a reserved type, and thus not a
8157 * I.e., check "!(link[0] & 0x04)".
8159 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8160 b1
= new_block(cstate
, JMP(BPF_JSET
));
8166 * AND that with the checks for data and management
8171 case DLT_IP_OVER_FC
:
8172 b0
= gen_mac_multicast(cstate
, 2);
8177 /* Link not known to support multicasts */
8181 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8182 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8187 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8188 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8192 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8197 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8199 register struct block
*b0
;
8202 * Catch errors reported by us and routines below us, and return NULL
8205 if (setjmp(cstate
->top_ctx
))
8209 * Only some data link types support ifindex qualifiers.
8211 switch (cstate
->linktype
) {
8212 case DLT_LINUX_SLL2
:
8213 /* match packets on this interface */
8214 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8219 * This is Linux; we require PF_PACKET support.
8220 * If this is a *live* capture, we can look at
8221 * special meta-data in the filter expression;
8222 * if it's a savefile, we can't.
8224 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8225 /* We have a FILE *, so this is a savefile */
8226 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8227 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8232 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8234 #else /* defined(linux) */
8235 bpf_error(cstate
, "ifindex not supported on %s",
8236 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8238 #endif /* defined(linux) */
8244 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8245 * Outbound traffic is sent by this machine, while inbound traffic is
8246 * sent by a remote machine (and may include packets destined for a
8247 * unicast or multicast link-layer address we are not subscribing to).
8248 * These are the same definitions implemented by pcap_setdirection().
8249 * Capturing only unicast traffic destined for this host is probably
8250 * better accomplished using a higher-layer filter.
8253 gen_inbound(compiler_state_t
*cstate
, int dir
)
8255 register struct block
*b0
;
8258 * Catch errors reported by us and routines below us, and return NULL
8261 if (setjmp(cstate
->top_ctx
))
8265 * Only some data link types support inbound/outbound qualifiers.
8267 switch (cstate
->linktype
) {
8269 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8270 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8271 gen_loadi_internal(cstate
, 0),
8277 /* match outgoing packets */
8278 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8280 /* match incoming packets */
8281 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8286 /* match outgoing packets */
8287 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8289 /* to filter on inbound traffic, invert the match */
8294 case DLT_LINUX_SLL2
:
8295 /* match outgoing packets */
8296 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8298 /* to filter on inbound traffic, invert the match */
8303 #ifdef HAVE_NET_PFVAR_H
8305 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8306 ((dir
== 0) ? PF_IN
: PF_OUT
));
8312 /* match outgoing packets */
8313 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8315 /* match incoming packets */
8316 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8320 case DLT_JUNIPER_MFR
:
8321 case DLT_JUNIPER_MLFR
:
8322 case DLT_JUNIPER_MLPPP
:
8323 case DLT_JUNIPER_ATM1
:
8324 case DLT_JUNIPER_ATM2
:
8325 case DLT_JUNIPER_PPPOE
:
8326 case DLT_JUNIPER_PPPOE_ATM
:
8327 case DLT_JUNIPER_GGSN
:
8328 case DLT_JUNIPER_ES
:
8329 case DLT_JUNIPER_MONITOR
:
8330 case DLT_JUNIPER_SERVICES
:
8331 case DLT_JUNIPER_ETHER
:
8332 case DLT_JUNIPER_PPP
:
8333 case DLT_JUNIPER_FRELAY
:
8334 case DLT_JUNIPER_CHDLC
:
8335 case DLT_JUNIPER_VP
:
8336 case DLT_JUNIPER_ST
:
8337 case DLT_JUNIPER_ISM
:
8338 case DLT_JUNIPER_VS
:
8339 case DLT_JUNIPER_SRX_E2E
:
8340 case DLT_JUNIPER_FIBRECHANNEL
:
8341 case DLT_JUNIPER_ATM_CEMIC
:
8343 /* juniper flags (including direction) are stored
8344 * the byte after the 3-byte magic number */
8346 /* match outgoing packets */
8347 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8349 /* match incoming packets */
8350 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8356 * If we have packet meta-data indicating a direction,
8357 * and that metadata can be checked by BPF code, check
8358 * it. Otherwise, give up, as this link-layer type has
8359 * nothing in the packet data.
8361 * Currently, the only platform where a BPF filter can
8362 * check that metadata is Linux with the in-kernel
8363 * BPF interpreter. If other packet capture mechanisms
8364 * and BPF filters also supported this, it would be
8365 * nice. It would be even better if they made that
8366 * metadata available so that we could provide it
8367 * with newer capture APIs, allowing it to be saved
8372 * This is Linux; we require PF_PACKET support.
8373 * If this is a *live* capture, we can look at
8374 * special meta-data in the filter expression;
8375 * if it's a savefile, we can't.
8377 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8378 /* We have a FILE *, so this is a savefile */
8379 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8380 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8383 /* match outgoing packets */
8384 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8387 /* to filter on inbound traffic, invert the match */
8390 #else /* defined(linux) */
8391 bpf_error(cstate
, "inbound/outbound not supported on %s",
8392 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8394 #endif /* defined(linux) */
8399 #ifdef HAVE_NET_PFVAR_H
8400 /* PF firewall log matched interface */
8402 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8408 * Catch errors reported by us and routines below us, and return NULL
8411 if (setjmp(cstate
->top_ctx
))
8414 if (cstate
->linktype
!= DLT_PFLOG
) {
8415 bpf_error(cstate
, "ifname supported only on PF linktype");
8418 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8419 off
= offsetof(struct pfloghdr
, ifname
);
8420 if (strlen(ifname
) >= len
) {
8421 bpf_error(cstate
, "ifname interface names can only be %d characters",
8425 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8426 (const u_char
*)ifname
);
8430 /* PF firewall log ruleset name */
8432 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8437 * Catch errors reported by us and routines below us, and return NULL
8440 if (setjmp(cstate
->top_ctx
))
8443 if (cstate
->linktype
!= DLT_PFLOG
) {
8444 bpf_error(cstate
, "ruleset supported only on PF linktype");
8448 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8449 bpf_error(cstate
, "ruleset names can only be %ld characters",
8450 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8454 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8455 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8459 /* PF firewall log rule number */
8461 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8466 * Catch errors reported by us and routines below us, and return NULL
8469 if (setjmp(cstate
->top_ctx
))
8472 if (cstate
->linktype
!= DLT_PFLOG
) {
8473 bpf_error(cstate
, "rnr supported only on PF linktype");
8477 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8482 /* PF firewall log sub-rule number */
8484 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8489 * Catch errors reported by us and routines below us, and return NULL
8492 if (setjmp(cstate
->top_ctx
))
8495 if (cstate
->linktype
!= DLT_PFLOG
) {
8496 bpf_error(cstate
, "srnr supported only on PF linktype");
8500 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8505 /* PF firewall log reason code */
8507 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8512 * Catch errors reported by us and routines below us, and return NULL
8515 if (setjmp(cstate
->top_ctx
))
8518 if (cstate
->linktype
!= DLT_PFLOG
) {
8519 bpf_error(cstate
, "reason supported only on PF linktype");
8523 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8524 (bpf_u_int32
)reason
);
8528 /* PF firewall log action */
8530 gen_pf_action(compiler_state_t
*cstate
, int action
)
8535 * Catch errors reported by us and routines below us, and return NULL
8538 if (setjmp(cstate
->top_ctx
))
8541 if (cstate
->linktype
!= DLT_PFLOG
) {
8542 bpf_error(cstate
, "action supported only on PF linktype");
8546 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8547 (bpf_u_int32
)action
);
8550 #else /* !HAVE_NET_PFVAR_H */
8552 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8555 * Catch errors reported by us and routines below us, and return NULL
8558 if (setjmp(cstate
->top_ctx
))
8561 bpf_error(cstate
, "libpcap was compiled without pf support");
8566 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8569 * Catch errors reported by us and routines below us, and return NULL
8572 if (setjmp(cstate
->top_ctx
))
8575 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8580 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8583 * Catch errors reported by us and routines below us, and return NULL
8586 if (setjmp(cstate
->top_ctx
))
8589 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8594 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8597 * Catch errors reported by us and routines below us, and return NULL
8600 if (setjmp(cstate
->top_ctx
))
8603 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8608 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8611 * Catch errors reported by us and routines below us, and return NULL
8614 if (setjmp(cstate
->top_ctx
))
8617 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8622 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8625 * Catch errors reported by us and routines below us, and return NULL
8628 if (setjmp(cstate
->top_ctx
))
8631 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8634 #endif /* HAVE_NET_PFVAR_H */
8636 /* IEEE 802.11 wireless header */
8638 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8643 * Catch errors reported by us and routines below us, and return NULL
8646 if (setjmp(cstate
->top_ctx
))
8649 switch (cstate
->linktype
) {
8651 case DLT_IEEE802_11
:
8652 case DLT_PRISM_HEADER
:
8653 case DLT_IEEE802_11_RADIO_AVS
:
8654 case DLT_IEEE802_11_RADIO
:
8655 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8659 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8667 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8672 * Catch errors reported by us and routines below us, and return NULL
8675 if (setjmp(cstate
->top_ctx
))
8678 switch (cstate
->linktype
) {
8680 case DLT_IEEE802_11
:
8681 case DLT_PRISM_HEADER
:
8682 case DLT_IEEE802_11_RADIO_AVS
:
8683 case DLT_IEEE802_11_RADIO
:
8687 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8691 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8692 IEEE80211_FC1_DIR_MASK
);
8698 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8703 * Catch errors reported by us and routines below us, and return NULL
8706 if (setjmp(cstate
->top_ctx
))
8709 switch (cstate
->linktype
) {
8712 case DLT_ARCNET_LINUX
:
8713 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8714 q
.proto
== Q_LINK
) {
8715 cstate
->e
= pcap_ether_aton(s
);
8716 if (cstate
->e
== NULL
)
8717 bpf_error(cstate
, "malloc");
8718 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8723 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8727 bpf_error(cstate
, "aid supported only on ARCnet");
8732 static struct block
*
8733 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8735 register struct block
*b0
, *b1
;
8738 /* src comes first, different from Ethernet */
8740 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8743 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8746 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8747 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8753 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8754 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8759 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8763 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8767 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8771 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8775 bpf_error(cstate
, "'ra' is only supported on 802.11");
8779 bpf_error(cstate
, "'ta' is only supported on 802.11");
8786 static struct block
*
8787 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8789 struct block
*b0
, *b1
;
8791 /* check for VLAN, including QinQ */
8792 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8793 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8796 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8802 static struct block
*
8803 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8805 if (vlan_num
> 0x0fff) {
8806 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8809 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8812 static struct block
*
8813 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8816 struct block
*b0
, *b1
;
8818 b0
= gen_vlan_tpid_test(cstate
);
8821 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8827 * Both payload and link header type follow the VLAN tags so that
8828 * both need to be updated.
8830 cstate
->off_linkpl
.constant_part
+= 4;
8831 cstate
->off_linktype
.constant_part
+= 4;
8836 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8837 /* add v to variable part of off */
8839 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8840 bpf_u_int32 v
, struct slist
*s
)
8844 if (!off
->is_variable
)
8845 off
->is_variable
= 1;
8847 off
->reg
= alloc_reg(cstate
);
8849 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8852 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8855 s2
= new_stmt(cstate
, BPF_ST
);
8861 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8862 * and link type offsets first
8865 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8869 /* offset determined at run time, shift variable part */
8871 cstate
->is_vlan_vloffset
= 1;
8872 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8873 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8875 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8876 sappend(s
.next
, b_tpid
->head
->stmts
);
8877 b_tpid
->head
->stmts
= s
.next
;
8881 * patch block b_vid (VLAN id test) to load VID value either from packet
8882 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8885 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8887 struct slist
*s
, *s2
, *sjeq
;
8890 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8891 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8893 /* true -> next instructions, false -> beginning of b_vid */
8894 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8896 sjeq
->s
.jf
= b_vid
->stmts
;
8899 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8900 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8904 /* Jump to the test in b_vid. We need to jump one instruction before
8905 * the end of the b_vid block so that we only skip loading the TCI
8906 * from packet data and not the 'and' instruction extractging VID.
8909 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8911 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8915 /* insert our statements at the beginning of b_vid */
8916 sappend(s
, b_vid
->stmts
);
8921 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8922 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8923 * tag can be either in metadata or in packet data; therefore if the
8924 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8925 * header for VLAN tag. As the decision is done at run time, we need
8926 * update variable part of the offsets
8928 static struct block
*
8929 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8932 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8935 /* generate new filter code based on extracting packet
8937 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8938 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8940 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8945 * This is tricky. We need to insert the statements updating variable
8946 * parts of offsets before the traditional TPID and VID tests so
8947 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8948 * we do not want this update to affect those checks. That's why we
8949 * generate both test blocks first and insert the statements updating
8950 * variable parts of both offsets after that. This wouldn't work if
8951 * there already were variable length link header when entering this
8952 * function but gen_vlan_bpf_extensions() isn't called in that case.
8954 b_tpid
= gen_vlan_tpid_test(cstate
);
8956 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8958 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8963 gen_vlan_patch_vid_test(cstate
, b_vid
);
8973 * support IEEE 802.1Q VLAN trunk over ethernet
8976 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8981 * Catch errors reported by us and routines below us, and return NULL
8984 if (setjmp(cstate
->top_ctx
))
8987 /* can't check for VLAN-encapsulated packets inside MPLS */
8988 if (cstate
->label_stack_depth
> 0)
8989 bpf_error(cstate
, "no VLAN match after MPLS");
8992 * Check for a VLAN packet, and then change the offsets to point
8993 * to the type and data fields within the VLAN packet. Just
8994 * increment the offsets, so that we can support a hierarchy, e.g.
8995 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8998 * XXX - this is a bit of a kludge. If we were to split the
8999 * compiler into a parser that parses an expression and
9000 * generates an expression tree, and a code generator that
9001 * takes an expression tree (which could come from our
9002 * parser or from some other parser) and generates BPF code,
9003 * we could perhaps make the offsets parameters of routines
9004 * and, in the handler for an "AND" node, pass to subnodes
9005 * other than the VLAN node the adjusted offsets.
9007 * This would mean that "vlan" would, instead of changing the
9008 * behavior of *all* tests after it, change only the behavior
9009 * of tests ANDed with it. That would change the documented
9010 * semantics of "vlan", which might break some expressions.
9011 * However, it would mean that "(vlan and ip) or ip" would check
9012 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9013 * checking only for VLAN-encapsulated IP, so that could still
9014 * be considered worth doing; it wouldn't break expressions
9015 * that are of the form "vlan and ..." or "vlan N and ...",
9016 * which I suspect are the most common expressions involving
9017 * "vlan". "vlan or ..." doesn't necessarily do what the user
9018 * would really want, now, as all the "or ..." tests would
9019 * be done assuming a VLAN, even though the "or" could be viewed
9020 * as meaning "or, if this isn't a VLAN packet...".
9022 switch (cstate
->linktype
) {
9025 case DLT_NETANALYZER
:
9026 case DLT_NETANALYZER_TRANSPARENT
:
9027 #if defined(SKF_AD_VLAN_TAG_PRESENT)
9028 /* Verify that this is the outer part of the packet and
9029 * not encapsulated somehow. */
9030 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
9031 cstate
->off_linkhdr
.constant_part
==
9032 cstate
->off_outermostlinkhdr
.constant_part
) {
9034 * Do we need special VLAN handling?
9036 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
9037 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
9040 b0
= gen_vlan_no_bpf_extensions(cstate
,
9041 vlan_num
, has_vlan_tag
);
9044 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
9048 case DLT_IEEE802_11
:
9049 case DLT_PRISM_HEADER
:
9050 case DLT_IEEE802_11_RADIO_AVS
:
9051 case DLT_IEEE802_11_RADIO
:
9052 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
9056 bpf_error(cstate
, "no VLAN support for %s",
9057 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9061 cstate
->vlan_stack_depth
++;
9069 * The label_num_arg dance is to avoid annoying whining by compilers that
9070 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9071 * It's not *used* after setjmp returns.
9074 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9077 volatile bpf_u_int32 label_num
= label_num_arg
;
9078 struct block
*b0
, *b1
;
9081 * Catch errors reported by us and routines below us, and return NULL
9084 if (setjmp(cstate
->top_ctx
))
9087 if (cstate
->label_stack_depth
> 0) {
9088 /* just match the bottom-of-stack bit clear */
9089 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9092 * We're not in an MPLS stack yet, so check the link-layer
9093 * type against MPLS.
9095 switch (cstate
->linktype
) {
9097 case DLT_C_HDLC
: /* fall through */
9099 case DLT_NETANALYZER
:
9100 case DLT_NETANALYZER_TRANSPARENT
:
9101 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9105 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9108 /* FIXME add other DLT_s ...
9109 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9110 * leave it for now */
9113 bpf_error(cstate
, "no MPLS support for %s",
9114 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9119 /* If a specific MPLS label is requested, check it */
9120 if (has_label_num
) {
9121 if (label_num
> 0xFFFFF) {
9122 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9123 label_num
, 0xFFFFF);
9125 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9126 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9127 0xfffff000); /* only compare the first 20 bits */
9133 * Change the offsets to point to the type and data fields within
9134 * the MPLS packet. Just increment the offsets, so that we
9135 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9136 * capture packets with an outer label of 100000 and an inner
9139 * Increment the MPLS stack depth as well; this indicates that
9140 * we're checking MPLS-encapsulated headers, to make sure higher
9141 * level code generators don't try to match against IP-related
9142 * protocols such as Q_ARP, Q_RARP etc.
9144 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9146 cstate
->off_nl_nosnap
+= 4;
9147 cstate
->off_nl
+= 4;
9148 cstate
->label_stack_depth
++;
9153 * Support PPPOE discovery and session.
9156 gen_pppoed(compiler_state_t
*cstate
)
9159 * Catch errors reported by us and routines below us, and return NULL
9162 if (setjmp(cstate
->top_ctx
))
9165 /* check for PPPoE discovery */
9166 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9170 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9172 struct block
*b0
, *b1
;
9175 * Catch errors reported by us and routines below us, and return NULL
9178 if (setjmp(cstate
->top_ctx
))
9182 * Test against the PPPoE session link-layer type.
9184 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9186 /* If a specific session is requested, check PPPoE session id */
9188 if (sess_num
> 0x0000ffff) {
9189 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9190 sess_num
, 0x0000ffff);
9192 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9198 * Change the offsets to point to the type and data fields within
9199 * the PPP packet, and note that this is PPPoE rather than
9202 * XXX - this is a bit of a kludge. See the comments in
9205 * The "network-layer" protocol is PPPoE, which has a 6-byte
9206 * PPPoE header, followed by a PPP packet.
9208 * There is no HDLC encapsulation for the PPP packet (it's
9209 * encapsulated in PPPoES instead), so the link-layer type
9210 * starts at the first byte of the PPP packet. For PPPoE,
9211 * that offset is relative to the beginning of the total
9212 * link-layer payload, including any 802.2 LLC header, so
9213 * it's 6 bytes past cstate->off_nl.
9215 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9216 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9217 cstate
->off_linkpl
.reg
);
9219 cstate
->off_linktype
= cstate
->off_linkhdr
;
9220 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9223 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9228 /* Check that this is Geneve and the VNI is correct if
9229 * specified. Parameterized to handle both IPv4 and IPv6. */
9230 static struct block
*
9231 gen_geneve_check(compiler_state_t
*cstate
,
9232 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9233 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9235 struct block
*b0
, *b1
;
9237 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9239 /* Check that we are operating on version 0. Otherwise, we
9240 * can't decode the rest of the fields. The version is 2 bits
9241 * in the first byte of the Geneve header. */
9242 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9247 if (vni
> 0xffffff) {
9248 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9251 vni
<<= 8; /* VNI is in the upper 3 bytes */
9252 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9260 /* The IPv4 and IPv6 Geneve checks need to do two things:
9261 * - Verify that this actually is Geneve with the right VNI.
9262 * - Place the IP header length (plus variable link prefix if
9263 * needed) into register A to be used later to compute
9264 * the inner packet offsets. */
9265 static struct block
*
9266 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9268 struct block
*b0
, *b1
;
9269 struct slist
*s
, *s1
;
9271 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9273 /* Load the IP header length into A. */
9274 s
= gen_loadx_iphdrlen(cstate
);
9276 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9279 /* Forcibly append these statements to the true condition
9280 * of the protocol check by creating a new block that is
9281 * always true and ANDing them. */
9282 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9291 static struct block
*
9292 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9294 struct block
*b0
, *b1
;
9295 struct slist
*s
, *s1
;
9297 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9299 /* Load the IP header length. We need to account for a
9300 * variable length link prefix if there is one. */
9301 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9303 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9307 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9311 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9315 /* Forcibly append these statements to the true condition
9316 * of the protocol check by creating a new block that is
9317 * always true and ANDing them. */
9318 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9321 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9330 /* We need to store three values based on the Geneve header::
9331 * - The offset of the linktype.
9332 * - The offset of the end of the Geneve header.
9333 * - The offset of the end of the encapsulated MAC header. */
9334 static struct slist
*
9335 gen_geneve_offsets(compiler_state_t
*cstate
)
9337 struct slist
*s
, *s1
, *s_proto
;
9339 /* First we need to calculate the offset of the Geneve header
9340 * itself. This is composed of the IP header previously calculated
9341 * (include any variable link prefix) and stored in A plus the
9342 * fixed sized headers (fixed link prefix, MAC length, and UDP
9344 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9345 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9347 /* Stash this in X since we'll need it later. */
9348 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9351 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9353 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9357 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9358 cstate
->off_linktype
.is_variable
= 1;
9359 cstate
->off_linktype
.constant_part
= 0;
9361 s1
= new_stmt(cstate
, BPF_ST
);
9362 s1
->s
.k
= cstate
->off_linktype
.reg
;
9365 /* Load the Geneve option length and mask and shift to get the
9366 * number of bytes. It is stored in the first byte of the Geneve
9368 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9372 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9376 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9380 /* Add in the rest of the Geneve base header. */
9381 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9385 /* Add the Geneve header length to its offset and store. */
9386 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9390 /* Set the encapsulated type as Ethernet. Even though we may
9391 * not actually have Ethernet inside there are two reasons this
9393 * - The linktype field is always in EtherType format regardless
9394 * of whether it is in Geneve or an inner Ethernet frame.
9395 * - The only link layer that we have specific support for is
9396 * Ethernet. We will confirm that the packet actually is
9397 * Ethernet at runtime before executing these checks. */
9398 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9400 s1
= new_stmt(cstate
, BPF_ST
);
9401 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9404 /* Calculate whether we have an Ethernet header or just raw IP/
9405 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9406 * and linktype by 14 bytes so that the network header can be found
9407 * seamlessly. Otherwise, keep what we've calculated already. */
9409 /* We have a bare jmp so we can't use the optimizer. */
9410 cstate
->no_optimize
= 1;
9412 /* Load the EtherType in the Geneve header, 2 bytes in. */
9413 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9417 /* Load X with the end of the Geneve header. */
9418 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9419 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9422 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9423 * end of this check, we should have the total length in X. In
9424 * the non-Ethernet case, it's already there. */
9425 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9426 s_proto
->s
.k
= ETHERTYPE_TEB
;
9427 sappend(s
, s_proto
);
9429 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9433 /* Since this is Ethernet, use the EtherType of the payload
9434 * directly as the linktype. Overwrite what we already have. */
9435 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9439 s1
= new_stmt(cstate
, BPF_ST
);
9440 s1
->s
.k
= cstate
->off_linktype
.reg
;
9443 /* Advance two bytes further to get the end of the Ethernet
9445 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9449 /* Move the result to X. */
9450 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9453 /* Store the final result of our linkpl calculation. */
9454 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9455 cstate
->off_linkpl
.is_variable
= 1;
9456 cstate
->off_linkpl
.constant_part
= 0;
9458 s1
= new_stmt(cstate
, BPF_STX
);
9459 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9468 /* Check to see if this is a Geneve packet. */
9470 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9472 struct block
*b0
, *b1
;
9476 * Catch errors reported by us and routines below us, and return NULL
9479 if (setjmp(cstate
->top_ctx
))
9482 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9483 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9488 /* Later filters should act on the payload of the Geneve frame,
9489 * update all of the header pointers. Attach this code so that
9490 * it gets executed in the event that the Geneve filter matches. */
9491 s
= gen_geneve_offsets(cstate
);
9493 b1
= gen_true(cstate
);
9494 sappend(s
, b1
->stmts
);
9499 cstate
->is_geneve
= 1;
9504 /* Check that the encapsulated frame has a link layer header
9505 * for Ethernet filters. */
9506 static struct block
*
9507 gen_geneve_ll_check(compiler_state_t
*cstate
)
9510 struct slist
*s
, *s1
;
9512 /* The easiest way to see if there is a link layer present
9513 * is to check if the link layer header and payload are not
9516 /* Geneve always generates pure variable offsets so we can
9517 * compare only the registers. */
9518 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9519 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9521 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9522 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9525 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9533 static struct block
*
9534 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9535 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9542 if (!cstate
->is_atm
)
9543 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9544 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9546 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9547 0xffffffffU
, jtype
, reverse
, jvalue
);
9551 if (!cstate
->is_atm
)
9552 bpf_error(cstate
, "'vci' supported only on raw ATM");
9553 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9555 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9556 0xffffffffU
, jtype
, reverse
, jvalue
);
9560 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9561 abort(); /* XXX - this isn't on FreeBSD */
9562 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9563 0x0fU
, jtype
, reverse
, jvalue
);
9567 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9569 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9570 0xffffffffU
, jtype
, reverse
, jvalue
);
9574 if (!cstate
->is_atm
)
9575 bpf_error(cstate
, "'callref' supported only on raw ATM");
9576 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9578 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9579 0xffffffffU
, jtype
, reverse
, jvalue
);
9588 static struct block
*
9589 gen_atmtype_metac(compiler_state_t
*cstate
)
9591 struct block
*b0
, *b1
;
9593 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9594 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9599 static struct block
*
9600 gen_atmtype_sc(compiler_state_t
*cstate
)
9602 struct block
*b0
, *b1
;
9604 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9605 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9610 static struct block
*
9611 gen_atmtype_llc(compiler_state_t
*cstate
)
9615 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9616 cstate
->linktype
= cstate
->prevlinktype
;
9621 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9622 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9625 * Catch errors reported by us and routines below us, and return NULL
9628 if (setjmp(cstate
->top_ctx
))
9631 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9636 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9638 struct block
*b0
, *b1
;
9641 * Catch errors reported by us and routines below us, and return NULL
9644 if (setjmp(cstate
->top_ctx
))
9650 /* Get all packets in Meta signalling Circuit */
9651 if (!cstate
->is_atm
)
9652 bpf_error(cstate
, "'metac' supported only on raw ATM");
9653 b1
= gen_atmtype_metac(cstate
);
9657 /* Get all packets in Broadcast Circuit*/
9658 if (!cstate
->is_atm
)
9659 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9660 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9661 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9666 /* Get all cells in Segment OAM F4 circuit*/
9667 if (!cstate
->is_atm
)
9668 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9669 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9670 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9675 /* Get all cells in End-to-End OAM F4 Circuit*/
9676 if (!cstate
->is_atm
)
9677 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9678 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9679 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9684 /* Get all packets in connection Signalling Circuit */
9685 if (!cstate
->is_atm
)
9686 bpf_error(cstate
, "'sc' supported only on raw ATM");
9687 b1
= gen_atmtype_sc(cstate
);
9691 /* Get all packets in ILMI Circuit */
9692 if (!cstate
->is_atm
)
9693 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9694 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9695 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9700 /* Get all LANE packets */
9701 if (!cstate
->is_atm
)
9702 bpf_error(cstate
, "'lane' supported only on raw ATM");
9703 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9706 * Arrange that all subsequent tests assume LANE
9707 * rather than LLC-encapsulated packets, and set
9708 * the offsets appropriately for LANE-encapsulated
9711 * We assume LANE means Ethernet, not Token Ring.
9713 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9714 cstate
->off_payload
+ 2, /* Ethernet header */
9716 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9717 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9718 cstate
->off_nl
= 0; /* Ethernet II */
9719 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9723 /* Get all LLC-encapsulated packets */
9724 if (!cstate
->is_atm
)
9725 bpf_error(cstate
, "'llc' supported only on raw ATM");
9726 b1
= gen_atmtype_llc(cstate
);
9736 * Filtering for MTP2 messages based on li value
9737 * FISU, length is null
9738 * LSSU, length is 1 or 2
9739 * MSU, length is 3 or more
9740 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9743 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9745 struct block
*b0
, *b1
;
9748 * Catch errors reported by us and routines below us, and return NULL
9751 if (setjmp(cstate
->top_ctx
))
9757 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9758 (cstate
->linktype
!= DLT_ERF
) &&
9759 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9760 bpf_error(cstate
, "'fisu' supported only on MTP2");
9761 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9762 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9763 0x3fU
, BPF_JEQ
, 0, 0U);
9767 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9768 (cstate
->linktype
!= DLT_ERF
) &&
9769 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9770 bpf_error(cstate
, "'lssu' supported only on MTP2");
9771 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9772 0x3fU
, BPF_JGT
, 1, 2U);
9773 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9774 0x3fU
, BPF_JGT
, 0, 0U);
9779 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9780 (cstate
->linktype
!= DLT_ERF
) &&
9781 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9782 bpf_error(cstate
, "'msu' supported only on MTP2");
9783 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9784 0x3fU
, BPF_JGT
, 0, 2U);
9788 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9789 (cstate
->linktype
!= DLT_ERF
) &&
9790 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9791 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9792 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9793 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9794 0xff80U
, BPF_JEQ
, 0, 0U);
9798 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9799 (cstate
->linktype
!= DLT_ERF
) &&
9800 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9801 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9802 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9803 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9804 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9805 0xff80U
, BPF_JGT
, 0, 0U);
9810 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9811 (cstate
->linktype
!= DLT_ERF
) &&
9812 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9813 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9814 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9815 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9825 * The jvalue_arg dance is to avoid annoying whining by compilers that
9826 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9827 * It's not *used* after setjmp returns.
9830 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9831 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
9833 volatile bpf_u_int32 jvalue
= jvalue_arg
;
9835 bpf_u_int32 val1
, val2
, val3
;
9842 * Catch errors reported by us and routines below us, and return NULL
9845 if (setjmp(cstate
->top_ctx
))
9848 newoff_sio
= cstate
->off_sio
;
9849 newoff_opc
= cstate
->off_opc
;
9850 newoff_dpc
= cstate
->off_dpc
;
9851 newoff_sls
= cstate
->off_sls
;
9852 switch (mtp3field
) {
9855 newoff_sio
+= 3; /* offset for MTP2_HSL */
9859 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9860 bpf_error(cstate
, "'sio' supported only on SS7");
9861 /* sio coded on 1 byte so max value 255 */
9863 bpf_error(cstate
, "sio value %u too big; max value = 255",
9865 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
9866 jtype
, reverse
, jvalue
);
9874 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9875 bpf_error(cstate
, "'opc' supported only on SS7");
9876 /* opc coded on 14 bits so max value 16383 */
9878 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9880 /* the following instructions are made to convert jvalue
9881 * to the form used to write opc in an ss7 message*/
9882 val1
= jvalue
& 0x00003c00;
9884 val2
= jvalue
& 0x000003fc;
9886 val3
= jvalue
& 0x00000003;
9888 jvalue
= val1
+ val2
+ val3
;
9889 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
9890 jtype
, reverse
, jvalue
);
9898 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9899 bpf_error(cstate
, "'dpc' supported only on SS7");
9900 /* dpc coded on 14 bits so max value 16383 */
9902 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9904 /* the following instructions are made to convert jvalue
9905 * to the forme used to write dpc in an ss7 message*/
9906 val1
= jvalue
& 0x000000ff;
9908 val2
= jvalue
& 0x00003f00;
9910 jvalue
= val1
+ val2
;
9911 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
9912 jtype
, reverse
, jvalue
);
9920 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9921 bpf_error(cstate
, "'sls' supported only on SS7");
9922 /* sls coded on 4 bits so max value 15 */
9924 bpf_error(cstate
, "sls value %u too big; max value = 15",
9926 /* the following instruction is made to convert jvalue
9927 * to the forme used to write sls in an ss7 message*/
9928 jvalue
= jvalue
<< 4;
9929 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
9930 jtype
, reverse
, jvalue
);
9939 static struct block
*
9940 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9945 * Q.2931 signalling protocol messages for handling virtual circuits
9946 * establishment and teardown
9951 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9955 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9959 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9963 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9967 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9970 case A_RELEASE_DONE
:
9971 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9981 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9983 struct block
*b0
, *b1
;
9986 * Catch errors reported by us and routines below us, and return NULL
9989 if (setjmp(cstate
->top_ctx
))
9995 if (!cstate
->is_atm
)
9996 bpf_error(cstate
, "'oam' supported only on raw ATM");
9998 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9999 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10001 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10006 if (!cstate
->is_atm
)
10007 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
10009 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
10010 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
10012 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
10018 * Get Q.2931 signalling messages for switched
10019 * virtual connection
10021 if (!cstate
->is_atm
)
10022 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
10023 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10024 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10026 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10028 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
10030 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10032 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10034 b0
= gen_atmtype_sc(cstate
);
10038 case A_METACONNECT
:
10039 if (!cstate
->is_atm
)
10040 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
10041 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
10042 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
10044 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
10046 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
10048 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
10050 b0
= gen_atmtype_metac(cstate
);