2 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 #include <pcap-types.h>
30 #include <sys/socket.h>
33 #include <sys/param.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
54 #include "ethertype.h"
58 #include "ieee80211.h"
60 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
66 #include "diag-control.h"
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
77 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
89 uint16_t u6_addr16
[8];
90 uint32_t u6_addr32
[4];
92 #define s6_addr in6_u.u6_addr8
93 #define s6_addr16 in6_u.u6_addr16
94 #define s6_addr32 in6_u.u6_addr32
95 #define s6_addr64 in6_u.u6_addr64
98 typedef unsigned short sa_family_t
;
100 #define __SOCKADDR_COMMON(sa_prefix) \
101 sa_family_t sa_prefix##family
103 /* Ditto, for IPv6. */
106 __SOCKADDR_COMMON (sin6_
);
107 uint16_t sin6_port
; /* Transport layer port # */
108 uint32_t sin6_flowinfo
; /* IPv6 flow information */
109 struct in6_addr sin6_addr
; /* IPv6 address */
112 #ifndef EAI_ADDRFAMILY
114 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
115 int ai_family
; /* PF_xxx */
116 int ai_socktype
; /* SOCK_xxx */
117 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
118 size_t ai_addrlen
; /* length of ai_addr */
119 char *ai_canonname
; /* canonical name for hostname */
120 struct sockaddr
*ai_addr
; /* binary address */
121 struct addrinfo
*ai_next
; /* next structure in linked list */
123 #endif /* EAI_ADDRFAMILY */
124 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
127 #include <netdb.h> /* for "struct addrinfo" */
129 #include <pcap/namedb.h>
131 #include "nametoaddr.h"
133 #define ETHERMTU 1500
135 #ifndef IPPROTO_HOPOPTS
136 #define IPPROTO_HOPOPTS 0
138 #ifndef IPPROTO_ROUTING
139 #define IPPROTO_ROUTING 43
141 #ifndef IPPROTO_FRAGMENT
142 #define IPPROTO_FRAGMENT 44
144 #ifndef IPPROTO_DSTOPTS
145 #define IPPROTO_DSTOPTS 60
148 #define IPPROTO_SCTP 132
151 #define GENEVE_PORT 6081
153 #ifdef HAVE_OS_PROTO_H
154 #include "os-proto.h"
157 #define JMP(c) ((c)|BPF_JMP|BPF_K)
160 * "Push" the current value of the link-layer header type and link-layer
161 * header offset onto a "stack", and set a new value. (It's not a
162 * full-blown stack; we keep only the top two items.)
164 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
166 (cs)->prevlinktype = (cs)->linktype; \
167 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
168 (cs)->linktype = (new_linktype); \
169 (cs)->off_linkhdr.is_variable = (new_is_variable); \
170 (cs)->off_linkhdr.constant_part = (new_constant_part); \
171 (cs)->off_linkhdr.reg = (new_reg); \
172 (cs)->is_geneve = 0; \
176 * Offset "not set" value.
178 #define OFFSET_NOT_SET 0xffffffffU
181 * Absolute offsets, which are offsets from the beginning of the raw
182 * packet data, are, in the general case, the sum of a variable value
183 * and a constant value; the variable value may be absent, in which
184 * case the offset is only the constant value, and the constant value
185 * may be zero, in which case the offset is only the variable value.
187 * bpf_abs_offset is a structure containing all that information:
189 * is_variable is 1 if there's a variable part.
191 * constant_part is the constant part of the value, possibly zero;
193 * if is_variable is 1, reg is the register number for a register
194 * containing the variable value if the register has been assigned,
204 * Value passed to gen_load_a() to indicate what the offset argument
205 * is relative to the beginning of.
208 OR_PACKET
, /* full packet data */
209 OR_LINKHDR
, /* link-layer header */
210 OR_PREVLINKHDR
, /* previous link-layer header */
211 OR_LLC
, /* 802.2 LLC header */
212 OR_PREVMPLSHDR
, /* previous MPLS header */
213 OR_LINKTYPE
, /* link-layer type */
214 OR_LINKPL
, /* link-layer payload */
215 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
216 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
217 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
221 * We divy out chunks of memory rather than call malloc each time so
222 * we don't have to worry about leaking memory. It's probably
223 * not a big deal if all this memory was wasted but if this ever
224 * goes into a library that would probably not be a good idea.
226 * XXX - this *is* in a library....
229 #define CHUNK0SIZE 1024
235 /* Code generator state */
237 struct _compiler_state
{
248 int outermostlinktype
;
253 /* Hack for handling VLAN and MPLS stacks. */
254 u_int label_stack_depth
;
255 u_int vlan_stack_depth
;
261 * As errors are handled by a longjmp, anything allocated must
262 * be freed in the longjmp handler, so it must be reachable
265 * One thing that's allocated is the result of pcap_nametoaddrinfo();
266 * it must be freed with freeaddrinfo(). This variable points to
267 * any addrinfo structure that would need to be freed.
272 * Another thing that's allocated is the result of pcap_ether_aton();
273 * it must be freed with free(). This variable points to any
274 * address that would need to be freed.
279 * Various code constructs need to know the layout of the packet.
280 * These values give the necessary offsets from the beginning
281 * of the packet data.
285 * Absolute offset of the beginning of the link-layer header.
287 bpf_abs_offset off_linkhdr
;
290 * If we're checking a link-layer header for a packet encapsulated
291 * in another protocol layer, this is the equivalent information
292 * for the previous layers' link-layer header from the beginning
293 * of the raw packet data.
295 bpf_abs_offset off_prevlinkhdr
;
298 * This is the equivalent information for the outermost layers'
301 bpf_abs_offset off_outermostlinkhdr
;
304 * Absolute offset of the beginning of the link-layer payload.
306 bpf_abs_offset off_linkpl
;
309 * "off_linktype" is the offset to information in the link-layer
310 * header giving the packet type. This is an absolute offset
311 * from the beginning of the packet.
313 * For Ethernet, it's the offset of the Ethernet type field; this
314 * means that it must have a value that skips VLAN tags.
316 * For link-layer types that always use 802.2 headers, it's the
317 * offset of the LLC header; this means that it must have a value
318 * that skips VLAN tags.
320 * For PPP, it's the offset of the PPP type field.
322 * For Cisco HDLC, it's the offset of the CHDLC type field.
324 * For BSD loopback, it's the offset of the AF_ value.
326 * For Linux cooked sockets, it's the offset of the type field.
328 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
329 * encapsulation, in which case, IP is assumed.
331 bpf_abs_offset off_linktype
;
334 * TRUE if the link layer includes an ATM pseudo-header.
339 * TRUE if "geneve" appeared in the filter; it causes us to
340 * generate code that checks for a Geneve header and assume
341 * that later filters apply to the encapsulated payload.
346 * TRUE if we need variable length part of VLAN offset
348 int is_vlan_vloffset
;
351 * These are offsets for the ATM pseudo-header.
358 * These are offsets for the MTP2 fields.
364 * These are offsets for the MTP3 fields.
372 * This is the offset of the first byte after the ATM pseudo_header,
373 * or -1 if there is no ATM pseudo-header.
378 * These are offsets to the beginning of the network-layer header.
379 * They are relative to the beginning of the link-layer payload
380 * (i.e., they don't include off_linkhdr.constant_part or
381 * off_linkpl.constant_part).
383 * If the link layer never uses 802.2 LLC:
385 * "off_nl" and "off_nl_nosnap" are the same.
387 * If the link layer always uses 802.2 LLC:
389 * "off_nl" is the offset if there's a SNAP header following
392 * "off_nl_nosnap" is the offset if there's no SNAP header.
394 * If the link layer is Ethernet:
396 * "off_nl" is the offset if the packet is an Ethernet II packet
397 * (we assume no 802.3+802.2+SNAP);
399 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
400 * with an 802.2 header following it.
406 * Here we handle simple allocation of the scratch registers.
407 * If too many registers are alloc'd, the allocator punts.
409 int regused
[BPF_MEMWORDS
];
415 struct chunk chunks
[NCHUNKS
];
420 * For use by routines outside this file.
424 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
429 * If we've already set an error, don't override it.
430 * The lexical analyzer reports some errors by setting
431 * the error and then returning a LEX_ERROR token, which
432 * is not recognized by any grammar rule, and thus forces
433 * the parse to stop. We don't want the error reported
434 * by the lexical analyzer to be overwritten by the syntax
437 if (!cstate
->error_set
) {
439 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
442 cstate
->error_set
= 1;
447 * For use *ONLY* in routines in this file.
449 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
450 PCAP_PRINTFLIKE(2, 3);
453 static void PCAP_NORETURN
454 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
459 (void)vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
462 longjmp(cstate
->top_ctx
, 1);
469 static int init_linktype(compiler_state_t
*, pcap_t
*);
471 static void init_regs(compiler_state_t
*);
472 static int alloc_reg(compiler_state_t
*);
473 static void free_reg(compiler_state_t
*, int);
475 static void initchunks(compiler_state_t
*cstate
);
476 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
477 static void *newchunk(compiler_state_t
*cstate
, size_t);
478 static void freechunks(compiler_state_t
*cstate
);
479 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
480 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
481 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
482 static inline void syntax(compiler_state_t
*cstate
);
484 static void backpatch(struct block
*, struct block
*);
485 static void merge(struct block
*, struct block
*);
486 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
488 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
490 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
492 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
494 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
496 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
497 u_int
, bpf_u_int32
, bpf_u_int32
);
498 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
499 u_int
, const u_char
*);
500 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, u_int
,
501 u_int
, bpf_u_int32
, int, int, bpf_u_int32
);
502 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
504 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
506 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
507 static struct block
*gen_uncond(compiler_state_t
*, int);
508 static inline struct block
*gen_true(compiler_state_t
*);
509 static inline struct block
*gen_false(compiler_state_t
*);
510 static struct block
*gen_ether_linktype(compiler_state_t
*, bpf_u_int32
);
511 static struct block
*gen_ipnet_linktype(compiler_state_t
*, bpf_u_int32
);
512 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, bpf_u_int32
);
513 static struct slist
*gen_load_pflog_llprefixlen(compiler_state_t
*);
514 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
515 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
516 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
517 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
518 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
519 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
521 static bpf_u_int32
ethertype_to_ppptype(bpf_u_int32
);
522 static struct block
*gen_linktype(compiler_state_t
*, bpf_u_int32
);
523 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
524 static struct block
*gen_llc_linktype(compiler_state_t
*, bpf_u_int32
);
525 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
526 int, bpf_u_int32
, u_int
, u_int
);
528 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
529 struct in6_addr
*, int, bpf_u_int32
, u_int
, u_int
);
531 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
532 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
533 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
534 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
535 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
536 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
537 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
538 static struct block
*gen_mpls_linktype(compiler_state_t
*, bpf_u_int32
);
539 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
542 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
543 struct in6_addr
*, int, int, int);
546 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
547 struct addrinfo
*, int, int);
549 static struct block
*gen_ipfrag(compiler_state_t
*);
550 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_u_int32
);
551 static struct block
*gen_portrangeatom(compiler_state_t
*, u_int
, bpf_u_int32
,
553 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_u_int32
);
554 static struct block
*gen_portrangeatom6(compiler_state_t
*, u_int
, bpf_u_int32
,
556 static struct block
*gen_portop(compiler_state_t
*, u_int
, u_int
, int);
557 static struct block
*gen_port(compiler_state_t
*, u_int
, int, int);
558 static struct block
*gen_portrangeop(compiler_state_t
*, u_int
, u_int
,
560 static struct block
*gen_portrange(compiler_state_t
*, u_int
, u_int
, int, int);
561 struct block
*gen_portop6(compiler_state_t
*, u_int
, u_int
, int);
562 static struct block
*gen_port6(compiler_state_t
*, u_int
, int, int);
563 static struct block
*gen_portrangeop6(compiler_state_t
*, u_int
, u_int
,
565 static struct block
*gen_portrange6(compiler_state_t
*, u_int
, u_int
, int, int);
566 static int lookup_proto(compiler_state_t
*, const char *, int);
567 static struct block
*gen_protochain(compiler_state_t
*, bpf_u_int32
, int);
568 static struct block
*gen_proto(compiler_state_t
*, bpf_u_int32
, int, int);
569 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
570 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
571 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
572 static struct block
*gen_len(compiler_state_t
*, int, int);
573 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
574 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
576 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
577 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
578 bpf_u_int32
, int, int);
579 static struct block
*gen_atmtype_llc(compiler_state_t
*);
580 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
583 initchunks(compiler_state_t
*cstate
)
587 for (i
= 0; i
< NCHUNKS
; i
++) {
588 cstate
->chunks
[i
].n_left
= 0;
589 cstate
->chunks
[i
].m
= NULL
;
591 cstate
->cur_chunk
= 0;
595 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
602 /* XXX Round up to nearest long. */
603 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
605 /* XXX Round up to structure boundary. */
609 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
610 if (n
> cp
->n_left
) {
612 k
= ++cstate
->cur_chunk
;
614 bpf_set_error(cstate
, "out of memory");
617 size
= CHUNK0SIZE
<< k
;
618 cp
->m
= (void *)malloc(size
);
620 bpf_set_error(cstate
, "out of memory");
623 memset((char *)cp
->m
, 0, size
);
626 bpf_set_error(cstate
, "out of memory");
631 return (void *)((char *)cp
->m
+ cp
->n_left
);
635 newchunk(compiler_state_t
*cstate
, size_t n
)
639 p
= newchunk_nolongjmp(cstate
, n
);
641 longjmp(cstate
->top_ctx
, 1);
648 freechunks(compiler_state_t
*cstate
)
652 for (i
= 0; i
< NCHUNKS
; ++i
)
653 if (cstate
->chunks
[i
].m
!= NULL
)
654 free(cstate
->chunks
[i
].m
);
658 * A strdup whose allocations are freed after code generation is over.
659 * This is used by the lexical analyzer, so it can't longjmp; it just
660 * returns NULL on an allocation error, and the callers must check
664 sdup(compiler_state_t
*cstate
, const char *s
)
666 size_t n
= strlen(s
) + 1;
667 char *cp
= newchunk_nolongjmp(cstate
, n
);
671 pcap_strlcpy(cp
, s
, n
);
675 static inline struct block
*
676 new_block(compiler_state_t
*cstate
, int code
)
680 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
687 static inline struct slist
*
688 new_stmt(compiler_state_t
*cstate
, int code
)
692 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
698 static struct block
*
699 gen_retblk(compiler_state_t
*cstate
, int v
)
701 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
707 static inline PCAP_NORETURN_DEF
void
708 syntax(compiler_state_t
*cstate
)
710 bpf_error(cstate
, "syntax error in filter expression");
714 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
715 const char *buf
, int optimize
, bpf_u_int32 mask
)
720 compiler_state_t cstate
;
721 const char * volatile xbuf
= buf
;
722 yyscan_t scanner
= NULL
;
723 volatile YY_BUFFER_STATE in_buffer
= NULL
;
728 * If this pcap_t hasn't been activated, it doesn't have a
729 * link-layer type, so we can't use it.
732 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
733 "not-yet-activated pcap_t passed to pcap_compile");
745 * If the device on which we're capturing need to be notified
746 * that a new filter is being compiled, do so.
748 * This allows them to save a copy of it, in case, for example,
749 * they're implementing a form of remote packet capture, and
750 * want the remote machine to filter out the packets in which
751 * it's sending the packets it's captured.
753 * XXX - the fact that we happen to be compiling a filter
754 * doesn't necessarily mean we'll be installing it as the
755 * filter for this pcap_t; we might be running it from userland
756 * on captured packets to do packet classification. We really
757 * need a better way of handling this, but this is all that
758 * the WinPcap remote capture code did.
760 if (p
->save_current_filter_op
!= NULL
)
761 (p
->save_current_filter_op
)(p
, buf
);
765 cstate
.no_optimize
= 0;
770 cstate
.ic
.root
= NULL
;
771 cstate
.ic
.cur_mark
= 0;
773 cstate
.error_set
= 0;
776 cstate
.netmask
= mask
;
778 cstate
.snaplen
= pcap_snapshot(p
);
779 if (cstate
.snaplen
== 0) {
780 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
781 "snaplen of 0 rejects all packets");
786 if (pcap_lex_init(&scanner
) != 0)
787 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
788 errno
, "can't initialize scanner");
789 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
792 * Associate the compiler state with the lexical analyzer
795 pcap_set_extra(&cstate
, scanner
);
797 if (init_linktype(&cstate
, p
) == -1) {
801 if (pcap_parse(scanner
, &cstate
) != 0) {
803 if (cstate
.ai
!= NULL
)
804 freeaddrinfo(cstate
.ai
);
806 if (cstate
.e
!= NULL
)
812 if (cstate
.ic
.root
== NULL
) {
814 * Catch errors reported by gen_retblk().
816 if (setjmp(cstate
.top_ctx
)) {
820 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
823 if (optimize
&& !cstate
.no_optimize
) {
824 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
829 if (cstate
.ic
.root
== NULL
||
830 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
831 (void)snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
832 "expression rejects all packets");
837 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
838 cstate
.ic
.root
, &len
, p
->errbuf
);
839 if (program
->bf_insns
== NULL
) {
844 program
->bf_len
= len
;
846 rc
= 0; /* We're all okay */
850 * Clean up everything for the lexical analyzer.
852 if (in_buffer
!= NULL
)
853 pcap__delete_buffer(in_buffer
, scanner
);
855 pcap_lex_destroy(scanner
);
858 * Clean up our own allocated memory.
866 * entry point for using the compiler with no pcap open
867 * pass in all the stuff that is needed explicitly instead.
870 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
871 struct bpf_program
*program
,
872 const char *buf
, int optimize
, bpf_u_int32 mask
)
877 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
880 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
886 * Clean up a "struct bpf_program" by freeing all the memory allocated
890 pcap_freecode(struct bpf_program
*program
)
893 if (program
->bf_insns
!= NULL
) {
894 free((char *)program
->bf_insns
);
895 program
->bf_insns
= NULL
;
900 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
901 * which of the jt and jf fields has been resolved and which is a pointer
902 * back to another unresolved block (or nil). At least one of the fields
903 * in each block is already resolved.
906 backpatch(struct block
*list
, struct block
*target
)
923 * Merge the lists in b0 and b1, using the 'sense' field to indicate
924 * which of jt and jf is the link.
927 merge(struct block
*b0
, struct block
*b1
)
929 register struct block
**p
= &b0
;
931 /* Find end of list. */
933 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
935 /* Concatenate the lists. */
940 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
942 struct block
*ppi_dlt_check
;
945 * Catch errors reported by us and routines below us, and return -1
948 if (setjmp(cstate
->top_ctx
))
952 * Insert before the statements of the first (root) block any
953 * statements needed to load the lengths of any variable-length
954 * headers into registers.
956 * XXX - a fancier strategy would be to insert those before the
957 * statements of all blocks that use those lengths and that
958 * have no predecessors that use them, so that we only compute
959 * the lengths if we need them. There might be even better
960 * approaches than that.
962 * However, those strategies would be more complicated, and
963 * as we don't generate code to compute a length if the
964 * program has no tests that use the length, and as most
965 * tests will probably use those lengths, we would just
966 * postpone computing the lengths so that it's not done
967 * for tests that fail early, and it's not clear that's
970 insert_compute_vloffsets(cstate
, p
->head
);
973 * For DLT_PPI captures, generate a check of the per-packet
974 * DLT value to make sure it's DLT_IEEE802_11.
976 * XXX - TurboCap cards use DLT_PPI for Ethernet.
977 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
978 * with appropriate Ethernet information and use that rather
979 * than using something such as DLT_PPI where you don't know
980 * the link-layer header type until runtime, which, in the
981 * general case, would force us to generate both Ethernet *and*
982 * 802.11 code (*and* anything else for which PPI is used)
983 * and choose between them early in the BPF program?
985 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
986 if (ppi_dlt_check
!= NULL
)
987 gen_and(ppi_dlt_check
, p
);
989 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
990 p
->sense
= !p
->sense
;
991 backpatch(p
, gen_retblk(cstate
, 0));
992 cstate
->ic
.root
= p
->head
;
997 gen_and(struct block
*b0
, struct block
*b1
)
999 backpatch(b0
, b1
->head
);
1000 b0
->sense
= !b0
->sense
;
1001 b1
->sense
= !b1
->sense
;
1003 b1
->sense
= !b1
->sense
;
1004 b1
->head
= b0
->head
;
1008 gen_or(struct block
*b0
, struct block
*b1
)
1010 b0
->sense
= !b0
->sense
;
1011 backpatch(b0
, b1
->head
);
1012 b0
->sense
= !b0
->sense
;
1014 b1
->head
= b0
->head
;
1018 gen_not(struct block
*b
)
1020 b
->sense
= !b
->sense
;
1023 static struct block
*
1024 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1025 u_int size
, bpf_u_int32 v
)
1027 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1030 static struct block
*
1031 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1032 u_int size
, bpf_u_int32 v
)
1034 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1037 static struct block
*
1038 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1039 u_int size
, bpf_u_int32 v
)
1041 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1044 static struct block
*
1045 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1046 u_int size
, bpf_u_int32 v
)
1048 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1051 static struct block
*
1052 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1053 u_int size
, bpf_u_int32 v
)
1055 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1058 static struct block
*
1059 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1060 u_int size
, bpf_u_int32 v
, bpf_u_int32 mask
)
1062 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1065 static struct block
*
1066 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1067 u_int size
, const u_char
*v
)
1069 register struct block
*b
, *tmp
;
1073 register const u_char
*p
= &v
[size
- 4];
1075 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1083 register const u_char
*p
= &v
[size
- 2];
1085 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1093 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, v
[0]);
1102 * AND the field of size "size" at offset "offset" relative to the header
1103 * specified by "offrel" with "mask", and compare it with the value "v"
1104 * with the test specified by "jtype"; if "reverse" is true, the test
1105 * should test the opposite of "jtype".
1107 static struct block
*
1108 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1109 u_int size
, bpf_u_int32 mask
, int jtype
, int reverse
,
1112 struct slist
*s
, *s2
;
1115 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1117 if (mask
!= 0xffffffff) {
1118 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1123 b
= new_block(cstate
, JMP(jtype
));
1126 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1132 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1134 cstate
->pcap_fddipad
= p
->fddipad
;
1137 * We start out with only one link-layer header.
1139 cstate
->outermostlinktype
= pcap_datalink(p
);
1140 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1141 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1142 cstate
->off_outermostlinkhdr
.reg
= -1;
1144 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1145 cstate
->off_prevlinkhdr
.constant_part
= 0;
1146 cstate
->off_prevlinkhdr
.is_variable
= 0;
1147 cstate
->off_prevlinkhdr
.reg
= -1;
1149 cstate
->linktype
= cstate
->outermostlinktype
;
1150 cstate
->off_linkhdr
.constant_part
= 0;
1151 cstate
->off_linkhdr
.is_variable
= 0;
1152 cstate
->off_linkhdr
.reg
= -1;
1157 cstate
->off_linkpl
.constant_part
= 0;
1158 cstate
->off_linkpl
.is_variable
= 0;
1159 cstate
->off_linkpl
.reg
= -1;
1161 cstate
->off_linktype
.constant_part
= 0;
1162 cstate
->off_linktype
.is_variable
= 0;
1163 cstate
->off_linktype
.reg
= -1;
1166 * Assume it's not raw ATM with a pseudo-header, for now.
1169 cstate
->off_vpi
= OFFSET_NOT_SET
;
1170 cstate
->off_vci
= OFFSET_NOT_SET
;
1171 cstate
->off_proto
= OFFSET_NOT_SET
;
1172 cstate
->off_payload
= OFFSET_NOT_SET
;
1177 cstate
->is_geneve
= 0;
1180 * No variable length VLAN offset by default
1182 cstate
->is_vlan_vloffset
= 0;
1185 * And assume we're not doing SS7.
1187 cstate
->off_li
= OFFSET_NOT_SET
;
1188 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1189 cstate
->off_sio
= OFFSET_NOT_SET
;
1190 cstate
->off_opc
= OFFSET_NOT_SET
;
1191 cstate
->off_dpc
= OFFSET_NOT_SET
;
1192 cstate
->off_sls
= OFFSET_NOT_SET
;
1194 cstate
->label_stack_depth
= 0;
1195 cstate
->vlan_stack_depth
= 0;
1197 switch (cstate
->linktype
) {
1200 cstate
->off_linktype
.constant_part
= 2;
1201 cstate
->off_linkpl
.constant_part
= 6;
1202 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1203 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1206 case DLT_ARCNET_LINUX
:
1207 cstate
->off_linktype
.constant_part
= 4;
1208 cstate
->off_linkpl
.constant_part
= 8;
1209 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1210 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1214 cstate
->off_linktype
.constant_part
= 12;
1215 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1216 cstate
->off_nl
= 0; /* Ethernet II */
1217 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1222 * SLIP doesn't have a link level type. The 16 byte
1223 * header is hacked into our SLIP driver.
1225 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1226 cstate
->off_linkpl
.constant_part
= 16;
1228 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1231 case DLT_SLIP_BSDOS
:
1232 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1233 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1235 cstate
->off_linkpl
.constant_part
= 24;
1237 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1242 cstate
->off_linktype
.constant_part
= 0;
1243 cstate
->off_linkpl
.constant_part
= 4;
1245 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1249 cstate
->off_linktype
.constant_part
= 0;
1250 cstate
->off_linkpl
.constant_part
= 12;
1252 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1257 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1258 case DLT_HDLC
: /* NetBSD (Cisco) HDLC */
1259 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1260 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1261 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1263 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1268 * This does no include the Ethernet header, and
1269 * only covers session state.
1271 cstate
->off_linktype
.constant_part
= 6;
1272 cstate
->off_linkpl
.constant_part
= 8;
1274 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1278 cstate
->off_linktype
.constant_part
= 5;
1279 cstate
->off_linkpl
.constant_part
= 24;
1281 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1286 * FDDI doesn't really have a link-level type field.
1287 * We set "off_linktype" to the offset of the LLC header.
1289 * To check for Ethernet types, we assume that SSAP = SNAP
1290 * is being used and pick out the encapsulated Ethernet type.
1291 * XXX - should we generate code to check for SNAP?
1293 cstate
->off_linktype
.constant_part
= 13;
1294 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1295 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1296 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1297 cstate
->off_nl
= 8; /* 802.2+SNAP */
1298 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1303 * Token Ring doesn't really have a link-level type field.
1304 * We set "off_linktype" to the offset of the LLC header.
1306 * To check for Ethernet types, we assume that SSAP = SNAP
1307 * is being used and pick out the encapsulated Ethernet type.
1308 * XXX - should we generate code to check for SNAP?
1310 * XXX - the header is actually variable-length.
1311 * Some various Linux patched versions gave 38
1312 * as "off_linktype" and 40 as "off_nl"; however,
1313 * if a token ring packet has *no* routing
1314 * information, i.e. is not source-routed, the correct
1315 * values are 20 and 22, as they are in the vanilla code.
1317 * A packet is source-routed iff the uppermost bit
1318 * of the first byte of the source address, at an
1319 * offset of 8, has the uppermost bit set. If the
1320 * packet is source-routed, the total number of bytes
1321 * of routing information is 2 plus bits 0x1F00 of
1322 * the 16-bit value at an offset of 14 (shifted right
1323 * 8 - figure out which byte that is).
1325 cstate
->off_linktype
.constant_part
= 14;
1326 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1327 cstate
->off_nl
= 8; /* 802.2+SNAP */
1328 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1331 case DLT_PRISM_HEADER
:
1332 case DLT_IEEE802_11_RADIO_AVS
:
1333 case DLT_IEEE802_11_RADIO
:
1334 cstate
->off_linkhdr
.is_variable
= 1;
1335 /* Fall through, 802.11 doesn't have a variable link
1336 * prefix but is otherwise the same. */
1339 case DLT_IEEE802_11
:
1341 * 802.11 doesn't really have a link-level type field.
1342 * We set "off_linktype.constant_part" to the offset of
1345 * To check for Ethernet types, we assume that SSAP = SNAP
1346 * is being used and pick out the encapsulated Ethernet type.
1347 * XXX - should we generate code to check for SNAP?
1349 * We also handle variable-length radio headers here.
1350 * The Prism header is in theory variable-length, but in
1351 * practice it's always 144 bytes long. However, some
1352 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1353 * sometimes or always supply an AVS header, so we
1354 * have to check whether the radio header is a Prism
1355 * header or an AVS header, so, in practice, it's
1358 cstate
->off_linktype
.constant_part
= 24;
1359 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1360 cstate
->off_linkpl
.is_variable
= 1;
1361 cstate
->off_nl
= 8; /* 802.2+SNAP */
1362 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1367 * At the moment we treat PPI the same way that we treat
1368 * normal Radiotap encoded packets. The difference is in
1369 * the function that generates the code at the beginning
1370 * to compute the header length. Since this code generator
1371 * of PPI supports bare 802.11 encapsulation only (i.e.
1372 * the encapsulated DLT should be DLT_IEEE802_11) we
1373 * generate code to check for this too.
1375 cstate
->off_linktype
.constant_part
= 24;
1376 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1377 cstate
->off_linkpl
.is_variable
= 1;
1378 cstate
->off_linkhdr
.is_variable
= 1;
1379 cstate
->off_nl
= 8; /* 802.2+SNAP */
1380 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1383 case DLT_ATM_RFC1483
:
1384 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1386 * assume routed, non-ISO PDUs
1387 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1389 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1390 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1391 * latter would presumably be treated the way PPPoE
1392 * should be, so you can do "pppoe and udp port 2049"
1393 * or "pppoa and tcp port 80" and have it check for
1394 * PPPo{A,E} and a PPP protocol of IP and....
1396 cstate
->off_linktype
.constant_part
= 0;
1397 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1398 cstate
->off_nl
= 8; /* 802.2+SNAP */
1399 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1404 * Full Frontal ATM; you get AALn PDUs with an ATM
1408 cstate
->off_vpi
= SUNATM_VPI_POS
;
1409 cstate
->off_vci
= SUNATM_VCI_POS
;
1410 cstate
->off_proto
= PROTO_POS
;
1411 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1412 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1413 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1414 cstate
->off_nl
= 8; /* 802.2+SNAP */
1415 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1421 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1422 cstate
->off_linkpl
.constant_part
= 0;
1424 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1427 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1428 cstate
->off_linktype
.constant_part
= 14;
1429 cstate
->off_linkpl
.constant_part
= 16;
1431 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1434 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1435 cstate
->off_linktype
.constant_part
= 0;
1436 cstate
->off_linkpl
.constant_part
= 20;
1438 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1443 * LocalTalk does have a 1-byte type field in the LLAP header,
1444 * but really it just indicates whether there is a "short" or
1445 * "long" DDP packet following.
1447 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1448 cstate
->off_linkpl
.constant_part
= 0;
1450 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1453 case DLT_IP_OVER_FC
:
1455 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1456 * link-level type field. We set "off_linktype" to the
1457 * offset of the LLC header.
1459 * To check for Ethernet types, we assume that SSAP = SNAP
1460 * is being used and pick out the encapsulated Ethernet type.
1461 * XXX - should we generate code to check for SNAP? RFC
1462 * 2625 says SNAP should be used.
1464 cstate
->off_linktype
.constant_part
= 16;
1465 cstate
->off_linkpl
.constant_part
= 16;
1466 cstate
->off_nl
= 8; /* 802.2+SNAP */
1467 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1472 * XXX - we should set this to handle SNAP-encapsulated
1473 * frames (NLPID of 0x80).
1475 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1476 cstate
->off_linkpl
.constant_part
= 0;
1478 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1482 * the only BPF-interesting FRF.16 frames are non-control frames;
1483 * Frame Relay has a variable length link-layer
1484 * so lets start with offset 4 for now and increments later on (FIXME);
1487 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1488 cstate
->off_linkpl
.constant_part
= 0;
1490 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1493 case DLT_APPLE_IP_OVER_IEEE1394
:
1494 cstate
->off_linktype
.constant_part
= 16;
1495 cstate
->off_linkpl
.constant_part
= 18;
1497 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1500 case DLT_SYMANTEC_FIREWALL
:
1501 cstate
->off_linktype
.constant_part
= 6;
1502 cstate
->off_linkpl
.constant_part
= 44;
1503 cstate
->off_nl
= 0; /* Ethernet II */
1504 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1508 cstate
->off_linktype
.constant_part
= 0;
1509 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1510 cstate
->off_linkpl
.is_variable
= 1;
1512 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1515 case DLT_JUNIPER_MFR
:
1516 case DLT_JUNIPER_MLFR
:
1517 case DLT_JUNIPER_MLPPP
:
1518 case DLT_JUNIPER_PPP
:
1519 case DLT_JUNIPER_CHDLC
:
1520 case DLT_JUNIPER_FRELAY
:
1521 cstate
->off_linktype
.constant_part
= 4;
1522 cstate
->off_linkpl
.constant_part
= 4;
1524 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1527 case DLT_JUNIPER_ATM1
:
1528 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1529 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1531 cstate
->off_nl_nosnap
= 10;
1534 case DLT_JUNIPER_ATM2
:
1535 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1536 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1538 cstate
->off_nl_nosnap
= 10;
1541 /* frames captured on a Juniper PPPoE service PIC
1542 * contain raw ethernet frames */
1543 case DLT_JUNIPER_PPPOE
:
1544 case DLT_JUNIPER_ETHER
:
1545 cstate
->off_linkpl
.constant_part
= 14;
1546 cstate
->off_linktype
.constant_part
= 16;
1547 cstate
->off_nl
= 18; /* Ethernet II */
1548 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1551 case DLT_JUNIPER_PPPOE_ATM
:
1552 cstate
->off_linktype
.constant_part
= 4;
1553 cstate
->off_linkpl
.constant_part
= 6;
1555 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1558 case DLT_JUNIPER_GGSN
:
1559 cstate
->off_linktype
.constant_part
= 6;
1560 cstate
->off_linkpl
.constant_part
= 12;
1562 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1565 case DLT_JUNIPER_ES
:
1566 cstate
->off_linktype
.constant_part
= 6;
1567 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1568 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1569 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1572 case DLT_JUNIPER_MONITOR
:
1573 cstate
->off_linktype
.constant_part
= 12;
1574 cstate
->off_linkpl
.constant_part
= 12;
1575 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1576 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1579 case DLT_BACNET_MS_TP
:
1580 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1581 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1582 cstate
->off_nl
= OFFSET_NOT_SET
;
1583 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1586 case DLT_JUNIPER_SERVICES
:
1587 cstate
->off_linktype
.constant_part
= 12;
1588 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1589 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1590 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1593 case DLT_JUNIPER_VP
:
1594 cstate
->off_linktype
.constant_part
= 18;
1595 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1596 cstate
->off_nl
= OFFSET_NOT_SET
;
1597 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1600 case DLT_JUNIPER_ST
:
1601 cstate
->off_linktype
.constant_part
= 18;
1602 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1603 cstate
->off_nl
= OFFSET_NOT_SET
;
1604 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1607 case DLT_JUNIPER_ISM
:
1608 cstate
->off_linktype
.constant_part
= 8;
1609 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1610 cstate
->off_nl
= OFFSET_NOT_SET
;
1611 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1614 case DLT_JUNIPER_VS
:
1615 case DLT_JUNIPER_SRX_E2E
:
1616 case DLT_JUNIPER_FIBRECHANNEL
:
1617 case DLT_JUNIPER_ATM_CEMIC
:
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
;
1626 cstate
->off_li_hsl
= 4;
1627 cstate
->off_sio
= 3;
1628 cstate
->off_opc
= 4;
1629 cstate
->off_dpc
= 4;
1630 cstate
->off_sls
= 7;
1631 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1632 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1633 cstate
->off_nl
= OFFSET_NOT_SET
;
1634 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1637 case DLT_MTP2_WITH_PHDR
:
1639 cstate
->off_li_hsl
= 8;
1640 cstate
->off_sio
= 7;
1641 cstate
->off_opc
= 8;
1642 cstate
->off_dpc
= 8;
1643 cstate
->off_sls
= 11;
1644 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1645 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1646 cstate
->off_nl
= OFFSET_NOT_SET
;
1647 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1651 cstate
->off_li
= 22;
1652 cstate
->off_li_hsl
= 24;
1653 cstate
->off_sio
= 23;
1654 cstate
->off_opc
= 24;
1655 cstate
->off_dpc
= 24;
1656 cstate
->off_sls
= 27;
1657 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1658 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1659 cstate
->off_nl
= OFFSET_NOT_SET
;
1660 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1664 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1665 cstate
->off_linkpl
.constant_part
= 4;
1667 cstate
->off_nl_nosnap
= 0;
1672 * Currently, only raw "link[N:M]" filtering is supported.
1674 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1675 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1676 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1677 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1681 cstate
->off_linktype
.constant_part
= 1;
1682 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1684 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1687 case DLT_NETANALYZER
:
1688 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1689 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1690 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1691 cstate
->off_nl
= 0; /* Ethernet II */
1692 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1695 case DLT_NETANALYZER_TRANSPARENT
:
1696 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1697 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1698 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1699 cstate
->off_nl
= 0; /* Ethernet II */
1700 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1705 * For values in the range in which we've assigned new
1706 * DLT_ values, only raw "link[N:M]" filtering is supported.
1708 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1709 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1710 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1711 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1712 cstate
->off_nl
= OFFSET_NOT_SET
;
1713 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1715 bpf_set_error(cstate
, "unknown data link type %d (min %d, max %d)",
1716 cstate
->linktype
, DLT_MATCHING_MIN
, DLT_MATCHING_MAX
);
1722 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1727 * Load a value relative to the specified absolute offset.
1729 static struct slist
*
1730 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1731 u_int offset
, u_int size
)
1733 struct slist
*s
, *s2
;
1735 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1738 * If "s" is non-null, it has code to arrange that the X register
1739 * contains the variable part of the absolute offset, so we
1740 * generate a load relative to that, with an offset of
1741 * abs_offset->constant_part + offset.
1743 * Otherwise, we can do an absolute load with an offset of
1744 * abs_offset->constant_part + offset.
1748 * "s" points to a list of statements that puts the
1749 * variable part of the absolute offset into the X register.
1750 * Do an indirect load, to use the X register as an offset.
1752 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1753 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1757 * There is no variable part of the absolute offset, so
1758 * just do an absolute load.
1760 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1761 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1767 * Load a value relative to the beginning of the specified header.
1769 static struct slist
*
1770 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1773 struct slist
*s
, *s2
;
1776 * Squelch warnings from compilers that *don't* assume that
1777 * offrel always has a valid enum value and therefore don't
1778 * assume that we'll always go through one of the case arms.
1780 * If we have a default case, compilers that *do* assume that
1781 * will then complain about the default case code being
1784 * Damned if you do, damned if you don't.
1791 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1796 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1799 case OR_PREVLINKHDR
:
1800 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1804 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1807 case OR_PREVMPLSHDR
:
1808 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1812 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1815 case OR_LINKPL_NOSNAP
:
1816 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1820 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1825 * Load the X register with the length of the IPv4 header
1826 * (plus the offset of the link-layer header, if it's
1827 * preceded by a variable-length header such as a radio
1828 * header), in bytes.
1830 s
= gen_loadx_iphdrlen(cstate
);
1833 * Load the item at {offset of the link-layer payload} +
1834 * {offset, relative to the start of the link-layer
1835 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1836 * {specified offset}.
1838 * If the offset of the link-layer payload is variable,
1839 * the variable part of that offset is included in the
1840 * value in the X register, and we include the constant
1841 * part in the offset of the load.
1843 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1844 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1849 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1856 * Generate code to load into the X register the sum of the length of
1857 * the IPv4 header and the variable part of the offset of the link-layer
1860 static struct slist
*
1861 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1863 struct slist
*s
, *s2
;
1865 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1868 * The offset of the link-layer payload has a variable
1869 * part. "s" points to a list of statements that put
1870 * the variable part of that offset into the X register.
1872 * The 4*([k]&0xf) addressing mode can't be used, as we
1873 * don't have a constant offset, so we have to load the
1874 * value in question into the A register and add to it
1875 * the value from the X register.
1877 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1878 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1880 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1883 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1888 * The A register now contains the length of the IP header.
1889 * We need to add to it the variable part of the offset of
1890 * the link-layer payload, which is still in the X
1891 * register, and move the result into the X register.
1893 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1894 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1897 * The offset of the link-layer payload is a constant,
1898 * so no code was generated to load the (non-existent)
1899 * variable part of that offset.
1901 * This means we can use the 4*([k]&0xf) addressing
1902 * mode. Load the length of the IPv4 header, which
1903 * is at an offset of cstate->off_nl from the beginning of
1904 * the link-layer payload, and thus at an offset of
1905 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1906 * of the raw packet data, using that addressing mode.
1908 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1909 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1915 static struct block
*
1916 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1921 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1923 b
= new_block(cstate
, JMP(BPF_JEQ
));
1929 static inline struct block
*
1930 gen_true(compiler_state_t
*cstate
)
1932 return gen_uncond(cstate
, 1);
1935 static inline struct block
*
1936 gen_false(compiler_state_t
*cstate
)
1938 return gen_uncond(cstate
, 0);
1942 * Byte-swap a 32-bit number.
1943 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1944 * big-endian platforms.)
1946 #define SWAPLONG(y) \
1947 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1950 * Generate code to match a particular packet type.
1952 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1953 * value, if <= ETHERMTU. We use that to determine whether to
1954 * match the type/length field or to check the type/length field for
1955 * a value <= ETHERMTU to see whether it's a type field and then do
1956 * the appropriate test.
1958 static struct block
*
1959 gen_ether_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
1961 struct block
*b0
, *b1
;
1967 case LLCSAP_NETBEUI
:
1969 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1970 * so we check the DSAP and SSAP.
1972 * LLCSAP_IP checks for IP-over-802.2, rather
1973 * than IP-over-Ethernet or IP-over-SNAP.
1975 * XXX - should we check both the DSAP and the
1976 * SSAP, like this, or should we check just the
1977 * DSAP, as we do for other types <= ETHERMTU
1978 * (i.e., other SAP values)?
1980 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1982 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
1990 * Ethernet_II frames, which are Ethernet
1991 * frames with a frame type of ETHERTYPE_IPX;
1993 * Ethernet_802.3 frames, which are 802.3
1994 * frames (i.e., the type/length field is
1995 * a length field, <= ETHERMTU, rather than
1996 * a type field) with the first two bytes
1997 * after the Ethernet/802.3 header being
2000 * Ethernet_802.2 frames, which are 802.3
2001 * frames with an 802.2 LLC header and
2002 * with the IPX LSAP as the DSAP in the LLC
2005 * Ethernet_SNAP frames, which are 802.3
2006 * frames with an LLC header and a SNAP
2007 * header and with an OUI of 0x000000
2008 * (encapsulated Ethernet) and a protocol
2009 * ID of ETHERTYPE_IPX in the SNAP header.
2011 * XXX - should we generate the same code both
2012 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2016 * This generates code to check both for the
2017 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2019 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2020 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
2024 * Now we add code to check for SNAP frames with
2025 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2027 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2031 * Now we generate code to check for 802.3
2032 * frames in general.
2034 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2038 * Now add the check for 802.3 frames before the
2039 * check for Ethernet_802.2 and Ethernet_802.3,
2040 * as those checks should only be done on 802.3
2041 * frames, not on Ethernet frames.
2046 * Now add the check for Ethernet_II frames, and
2047 * do that before checking for the other frame
2050 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2054 case ETHERTYPE_ATALK
:
2055 case ETHERTYPE_AARP
:
2057 * EtherTalk (AppleTalk protocols on Ethernet link
2058 * layer) may use 802.2 encapsulation.
2062 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2063 * we check for an Ethernet type field less than
2064 * 1500, which means it's an 802.3 length field.
2066 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2070 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2071 * SNAP packets with an organization code of
2072 * 0x080007 (Apple, for Appletalk) and a protocol
2073 * type of ETHERTYPE_ATALK (Appletalk).
2075 * 802.2-encapsulated ETHERTYPE_AARP packets are
2076 * SNAP packets with an organization code of
2077 * 0x000000 (encapsulated Ethernet) and a protocol
2078 * type of ETHERTYPE_AARP (Appletalk ARP).
2080 if (ll_proto
== ETHERTYPE_ATALK
)
2081 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2082 else /* ll_proto == ETHERTYPE_AARP */
2083 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2087 * Check for Ethernet encapsulation (Ethertalk
2088 * phase 1?); we just check for the Ethernet
2091 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2097 if (ll_proto
<= ETHERMTU
) {
2099 * This is an LLC SAP value, so the frames
2100 * that match would be 802.2 frames.
2101 * Check that the frame is an 802.2 frame
2102 * (i.e., that the length/type field is
2103 * a length field, <= ETHERMTU) and
2104 * then check the DSAP.
2106 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2108 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, ll_proto
);
2113 * This is an Ethernet type, so compare
2114 * the length/type field with it (if
2115 * the frame is an 802.2 frame, the length
2116 * field will be <= ETHERMTU, and, as
2117 * "ll_proto" is > ETHERMTU, this test
2118 * will fail and the frame won't match,
2119 * which is what we want).
2121 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2126 static struct block
*
2127 gen_loopback_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2130 * For DLT_NULL, the link-layer header is a 32-bit word
2131 * containing an AF_ value in *host* byte order, and for
2132 * DLT_ENC, the link-layer header begins with a 32-bit
2133 * word containing an AF_ value in host byte order.
2135 * In addition, if we're reading a saved capture file,
2136 * the host byte order in the capture may not be the
2137 * same as the host byte order on this machine.
2139 * For DLT_LOOP, the link-layer header is a 32-bit
2140 * word containing an AF_ value in *network* byte order.
2142 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2144 * The AF_ value is in host byte order, but the BPF
2145 * interpreter will convert it to network byte order.
2147 * If this is a save file, and it's from a machine
2148 * with the opposite byte order to ours, we byte-swap
2151 * Then we run it through "htonl()", and generate
2152 * code to compare against the result.
2154 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2155 ll_proto
= SWAPLONG(ll_proto
);
2156 ll_proto
= htonl(ll_proto
);
2158 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, ll_proto
));
2162 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2163 * or IPv6 then we have an error.
2165 static struct block
*
2166 gen_ipnet_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2171 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET
);
2174 case ETHERTYPE_IPV6
:
2175 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, IPH_AF_INET6
);
2182 return gen_false(cstate
);
2186 * Generate code to match a particular packet type.
2188 * "ll_proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2189 * value, if <= ETHERMTU. We use that to determine whether to
2190 * match the type field or to check the type field for the special
2191 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2193 static struct block
*
2194 gen_linux_sll_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
2196 struct block
*b0
, *b1
;
2202 case LLCSAP_NETBEUI
:
2204 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2205 * so we check the DSAP and SSAP.
2207 * LLCSAP_IP checks for IP-over-802.2, rather
2208 * than IP-over-Ethernet or IP-over-SNAP.
2210 * XXX - should we check both the DSAP and the
2211 * SSAP, like this, or should we check just the
2212 * DSAP, as we do for other types <= ETHERMTU
2213 * (i.e., other SAP values)?
2215 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2216 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (ll_proto
<< 8) | ll_proto
);
2222 * Ethernet_II frames, which are Ethernet
2223 * frames with a frame type of ETHERTYPE_IPX;
2225 * Ethernet_802.3 frames, which have a frame
2226 * type of LINUX_SLL_P_802_3;
2228 * Ethernet_802.2 frames, which are 802.3
2229 * frames with an 802.2 LLC header (i.e, have
2230 * a frame type of LINUX_SLL_P_802_2) and
2231 * with the IPX LSAP as the DSAP in the LLC
2234 * Ethernet_SNAP frames, which are 802.3
2235 * frames with an LLC header and a SNAP
2236 * header and with an OUI of 0x000000
2237 * (encapsulated Ethernet) and a protocol
2238 * ID of ETHERTYPE_IPX in the SNAP header.
2240 * First, do the checks on LINUX_SLL_P_802_2
2241 * frames; generate the check for either
2242 * Ethernet_802.2 or Ethernet_SNAP frames, and
2243 * then put a check for LINUX_SLL_P_802_2 frames
2246 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
2247 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2249 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2253 * Now check for 802.3 frames and OR that with
2254 * the previous test.
2256 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2260 * Now add the check for Ethernet_II frames, and
2261 * do that before checking for the other frame
2264 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERTYPE_IPX
);
2268 case ETHERTYPE_ATALK
:
2269 case ETHERTYPE_AARP
:
2271 * EtherTalk (AppleTalk protocols on Ethernet link
2272 * layer) may use 802.2 encapsulation.
2276 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2277 * we check for the 802.2 protocol type in the
2278 * "Ethernet type" field.
2280 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2283 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2284 * SNAP packets with an organization code of
2285 * 0x080007 (Apple, for Appletalk) and a protocol
2286 * type of ETHERTYPE_ATALK (Appletalk).
2288 * 802.2-encapsulated ETHERTYPE_AARP packets are
2289 * SNAP packets with an organization code of
2290 * 0x000000 (encapsulated Ethernet) and a protocol
2291 * type of ETHERTYPE_AARP (Appletalk ARP).
2293 if (ll_proto
== ETHERTYPE_ATALK
)
2294 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2295 else /* ll_proto == ETHERTYPE_AARP */
2296 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2300 * Check for Ethernet encapsulation (Ethertalk
2301 * phase 1?); we just check for the Ethernet
2304 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2310 if (ll_proto
<= ETHERMTU
) {
2312 * This is an LLC SAP value, so the frames
2313 * that match would be 802.2 frames.
2314 * Check for the 802.2 protocol type
2315 * in the "Ethernet type" field, and
2316 * then check the DSAP.
2318 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2319 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2325 * This is an Ethernet type, so compare
2326 * the length/type field with it (if
2327 * the frame is an 802.2 frame, the length
2328 * field will be <= ETHERMTU, and, as
2329 * "ll_proto" is > ETHERMTU, this test
2330 * will fail and the frame won't match,
2331 * which is what we want).
2333 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
2339 * Load a value relative to the beginning of the link-layer header after the
2342 static struct slist
*
2343 gen_load_pflog_llprefixlen(compiler_state_t
*cstate
)
2345 struct slist
*s1
, *s2
;
2348 * Generate code to load the length of the pflog header into
2349 * the register assigned to hold that length, if one has been
2350 * assigned. (If one hasn't been assigned, no code we've
2351 * generated uses that prefix, so we don't need to generate any
2354 if (cstate
->off_linkpl
.reg
!= -1) {
2356 * The length is in the first byte of the header.
2358 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2362 * Round it up to a multiple of 4.
2363 * Add 3, and clear the lower 2 bits.
2365 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2368 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2369 s2
->s
.k
= 0xfffffffc;
2373 * Now allocate a register to hold that value and store
2376 s2
= new_stmt(cstate
, BPF_ST
);
2377 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2381 * Now move it into the X register.
2383 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2391 static struct slist
*
2392 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2394 struct slist
*s1
, *s2
;
2395 struct slist
*sjeq_avs_cookie
;
2396 struct slist
*sjcommon
;
2399 * This code is not compatible with the optimizer, as
2400 * we are generating jmp instructions within a normal
2401 * slist of instructions
2403 cstate
->no_optimize
= 1;
2406 * Generate code to load the length of the radio header into
2407 * the register assigned to hold that length, if one has been
2408 * assigned. (If one hasn't been assigned, no code we've
2409 * generated uses that prefix, so we don't need to generate any
2412 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2413 * or always use the AVS header rather than the Prism header.
2414 * We load a 4-byte big-endian value at the beginning of the
2415 * raw packet data, and see whether, when masked with 0xFFFFF000,
2416 * it's equal to 0x80211000. If so, that indicates that it's
2417 * an AVS header (the masked-out bits are the version number).
2418 * Otherwise, it's a Prism header.
2420 * XXX - the Prism header is also, in theory, variable-length,
2421 * but no known software generates headers that aren't 144
2424 if (cstate
->off_linkhdr
.reg
!= -1) {
2428 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2432 * AND it with 0xFFFFF000.
2434 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2435 s2
->s
.k
= 0xFFFFF000;
2439 * Compare with 0x80211000.
2441 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2442 sjeq_avs_cookie
->s
.k
= 0x80211000;
2443 sappend(s1
, sjeq_avs_cookie
);
2448 * The 4 bytes at an offset of 4 from the beginning of
2449 * the AVS header are the length of the AVS header.
2450 * That field is big-endian.
2452 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2455 sjeq_avs_cookie
->s
.jt
= s2
;
2458 * Now jump to the code to allocate a register
2459 * into which to save the header length and
2460 * store the length there. (The "jump always"
2461 * instruction needs to have the k field set;
2462 * it's added to the PC, so, as we're jumping
2463 * over a single instruction, it should be 1.)
2465 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2467 sappend(s1
, sjcommon
);
2470 * Now for the code that handles the Prism header.
2471 * Just load the length of the Prism header (144)
2472 * into the A register. Have the test for an AVS
2473 * header branch here if we don't have an AVS header.
2475 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2478 sjeq_avs_cookie
->s
.jf
= s2
;
2481 * Now allocate a register to hold that value and store
2482 * it. The code for the AVS header will jump here after
2483 * loading the length of the AVS header.
2485 s2
= new_stmt(cstate
, BPF_ST
);
2486 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2488 sjcommon
->s
.jf
= s2
;
2491 * Now move it into the X register.
2493 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2501 static struct slist
*
2502 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2504 struct slist
*s1
, *s2
;
2507 * Generate code to load the length of the AVS header into
2508 * the register assigned to hold that length, if one has been
2509 * assigned. (If one hasn't been assigned, no code we've
2510 * generated uses that prefix, so we don't need to generate any
2513 if (cstate
->off_linkhdr
.reg
!= -1) {
2515 * The 4 bytes at an offset of 4 from the beginning of
2516 * the AVS header are the length of the AVS header.
2517 * That field is big-endian.
2519 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2523 * Now allocate a register to hold that value and store
2526 s2
= new_stmt(cstate
, BPF_ST
);
2527 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2531 * Now move it into the X register.
2533 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2541 static struct slist
*
2542 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2544 struct slist
*s1
, *s2
;
2547 * Generate code to load the length of the radiotap header into
2548 * the register assigned to hold that length, if one has been
2549 * assigned. (If one hasn't been assigned, no code we've
2550 * generated uses that prefix, so we don't need to generate any
2553 if (cstate
->off_linkhdr
.reg
!= -1) {
2555 * The 2 bytes at offsets of 2 and 3 from the beginning
2556 * of the radiotap header are the length of the radiotap
2557 * header; unfortunately, it's little-endian, so we have
2558 * to load it a byte at a time and construct the value.
2562 * Load the high-order byte, at an offset of 3, shift it
2563 * left a byte, and put the result in the X register.
2565 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2567 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2570 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2574 * Load the next byte, at an offset of 2, and OR the
2575 * value from the X register into it.
2577 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2580 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2584 * Now allocate a register to hold that value and store
2587 s2
= new_stmt(cstate
, BPF_ST
);
2588 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2592 * Now move it into the X register.
2594 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2603 * At the moment we treat PPI as normal Radiotap encoded
2604 * packets. The difference is in the function that generates
2605 * the code at the beginning to compute the header length.
2606 * Since this code generator of PPI supports bare 802.11
2607 * encapsulation only (i.e. the encapsulated DLT should be
2608 * DLT_IEEE802_11) we generate code to check for this too;
2609 * that's done in finish_parse().
2611 static struct slist
*
2612 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2614 struct slist
*s1
, *s2
;
2617 * Generate code to load the length of the radiotap header
2618 * into the register assigned to hold that length, if one has
2621 if (cstate
->off_linkhdr
.reg
!= -1) {
2623 * The 2 bytes at offsets of 2 and 3 from the beginning
2624 * of the radiotap header are the length of the radiotap
2625 * header; unfortunately, it's little-endian, so we have
2626 * to load it a byte at a time and construct the value.
2630 * Load the high-order byte, at an offset of 3, shift it
2631 * left a byte, and put the result in the X register.
2633 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2635 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2638 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2642 * Load the next byte, at an offset of 2, and OR the
2643 * value from the X register into it.
2645 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2648 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2652 * Now allocate a register to hold that value and store
2655 s2
= new_stmt(cstate
, BPF_ST
);
2656 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2660 * Now move it into the X register.
2662 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2671 * Load a value relative to the beginning of the link-layer header after the 802.11
2672 * header, i.e. LLC_SNAP.
2673 * The link-layer header doesn't necessarily begin at the beginning
2674 * of the packet data; there might be a variable-length prefix containing
2675 * radio information.
2677 static struct slist
*
2678 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2681 struct slist
*sjset_data_frame_1
;
2682 struct slist
*sjset_data_frame_2
;
2683 struct slist
*sjset_qos
;
2684 struct slist
*sjset_radiotap_flags_present
;
2685 struct slist
*sjset_radiotap_ext_present
;
2686 struct slist
*sjset_radiotap_tsft_present
;
2687 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2688 struct slist
*s_roundup
;
2690 if (cstate
->off_linkpl
.reg
== -1) {
2692 * No register has been assigned to the offset of
2693 * the link-layer payload, which means nobody needs
2694 * it; don't bother computing it - just return
2695 * what we already have.
2701 * This code is not compatible with the optimizer, as
2702 * we are generating jmp instructions within a normal
2703 * slist of instructions
2705 cstate
->no_optimize
= 1;
2708 * If "s" is non-null, it has code to arrange that the X register
2709 * contains the length of the prefix preceding the link-layer
2712 * Otherwise, the length of the prefix preceding the link-layer
2713 * header is "off_outermostlinkhdr.constant_part".
2717 * There is no variable-length header preceding the
2718 * link-layer header.
2720 * Load the length of the fixed-length prefix preceding
2721 * the link-layer header (if any) into the X register,
2722 * and store it in the cstate->off_linkpl.reg register.
2723 * That length is off_outermostlinkhdr.constant_part.
2725 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2726 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2730 * The X register contains the offset of the beginning of the
2731 * link-layer header; add 24, which is the minimum length
2732 * of the MAC header for a data frame, to that, and store it
2733 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2734 * which is at the offset in the X register, with an indexed load.
2736 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2738 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2741 s2
= new_stmt(cstate
, BPF_ST
);
2742 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2745 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2750 * Check the Frame Control field to see if this is a data frame;
2751 * a data frame has the 0x08 bit (b3) in that field set and the
2752 * 0x04 bit (b2) clear.
2754 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2755 sjset_data_frame_1
->s
.k
= 0x08;
2756 sappend(s
, sjset_data_frame_1
);
2759 * If b3 is set, test b2, otherwise go to the first statement of
2760 * the rest of the program.
2762 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2763 sjset_data_frame_2
->s
.k
= 0x04;
2764 sappend(s
, sjset_data_frame_2
);
2765 sjset_data_frame_1
->s
.jf
= snext
;
2768 * If b2 is not set, this is a data frame; test the QoS bit.
2769 * Otherwise, go to the first statement of the rest of the
2772 sjset_data_frame_2
->s
.jt
= snext
;
2773 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2774 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2775 sappend(s
, sjset_qos
);
2778 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2780 * Otherwise, go to the first statement of the rest of the
2783 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2784 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2786 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2789 s2
= new_stmt(cstate
, BPF_ST
);
2790 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2794 * If we have a radiotap header, look at it to see whether
2795 * there's Atheros padding between the MAC-layer header
2798 * Note: all of the fields in the radiotap header are
2799 * little-endian, so we byte-swap all of the values
2800 * we test against, as they will be loaded as big-endian
2803 * XXX - in the general case, we would have to scan through
2804 * *all* the presence bits, if there's more than one word of
2805 * presence bits. That would require a loop, meaning that
2806 * we wouldn't be able to run the filter in the kernel.
2808 * We assume here that the Atheros adapters that insert the
2809 * annoying padding don't have multiple antennae and therefore
2810 * do not generate radiotap headers with multiple presence words.
2812 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2814 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2815 * in the first presence flag word?
2817 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2821 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2822 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2823 sappend(s
, sjset_radiotap_flags_present
);
2826 * If not, skip all of this.
2828 sjset_radiotap_flags_present
->s
.jf
= snext
;
2831 * Otherwise, is the "extension" bit set in that word?
2833 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2834 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2835 sappend(s
, sjset_radiotap_ext_present
);
2836 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2839 * If so, skip all of this.
2841 sjset_radiotap_ext_present
->s
.jt
= snext
;
2844 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2846 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2847 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2848 sappend(s
, sjset_radiotap_tsft_present
);
2849 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2852 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2853 * at an offset of 16 from the beginning of the raw packet
2854 * data (8 bytes for the radiotap header and 8 bytes for
2857 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2860 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2863 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2865 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2866 sjset_tsft_datapad
->s
.k
= 0x20;
2867 sappend(s
, sjset_tsft_datapad
);
2870 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2871 * at an offset of 8 from the beginning of the raw packet
2872 * data (8 bytes for the radiotap header).
2874 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2877 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2880 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2882 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2883 sjset_notsft_datapad
->s
.k
= 0x20;
2884 sappend(s
, sjset_notsft_datapad
);
2887 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2888 * set, round the length of the 802.11 header to
2889 * a multiple of 4. Do that by adding 3 and then
2890 * dividing by and multiplying by 4, which we do by
2893 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2894 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2895 sappend(s
, s_roundup
);
2896 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2899 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2900 s2
->s
.k
= (bpf_u_int32
)~3;
2902 s2
= new_stmt(cstate
, BPF_ST
);
2903 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2906 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2907 sjset_tsft_datapad
->s
.jf
= snext
;
2908 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2909 sjset_notsft_datapad
->s
.jf
= snext
;
2911 sjset_qos
->s
.jf
= snext
;
2917 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2921 /* There is an implicit dependency between the link
2922 * payload and link header since the payload computation
2923 * includes the variable part of the header. Therefore,
2924 * if nobody else has allocated a register for the link
2925 * header and we need it, do it now. */
2926 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2927 cstate
->off_linkhdr
.reg
== -1)
2928 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2931 * For link-layer types that have a variable-length header
2932 * preceding the link-layer header, generate code to load
2933 * the offset of the link-layer header into the register
2934 * assigned to that offset, if any.
2936 * XXX - this, and the next switch statement, won't handle
2937 * encapsulation of 802.11 or 802.11+radio information in
2938 * some other protocol stack. That's significantly more
2941 switch (cstate
->outermostlinktype
) {
2943 case DLT_PRISM_HEADER
:
2944 s
= gen_load_prism_llprefixlen(cstate
);
2947 case DLT_IEEE802_11_RADIO_AVS
:
2948 s
= gen_load_avs_llprefixlen(cstate
);
2951 case DLT_IEEE802_11_RADIO
:
2952 s
= gen_load_radiotap_llprefixlen(cstate
);
2956 s
= gen_load_ppi_llprefixlen(cstate
);
2965 * For link-layer types that have a variable-length link-layer
2966 * header, generate code to load the offset of the link-layer
2967 * payload into the register assigned to that offset, if any.
2969 switch (cstate
->outermostlinktype
) {
2971 case DLT_IEEE802_11
:
2972 case DLT_PRISM_HEADER
:
2973 case DLT_IEEE802_11_RADIO_AVS
:
2974 case DLT_IEEE802_11_RADIO
:
2976 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2980 s
= gen_load_pflog_llprefixlen(cstate
);
2985 * If there is no initialization yet and we need variable
2986 * length offsets for VLAN, initialize them to zero
2988 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2991 if (cstate
->off_linkpl
.reg
== -1)
2992 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2993 if (cstate
->off_linktype
.reg
== -1)
2994 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2996 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2998 s2
= new_stmt(cstate
, BPF_ST
);
2999 s2
->s
.k
= cstate
->off_linkpl
.reg
;
3001 s2
= new_stmt(cstate
, BPF_ST
);
3002 s2
->s
.k
= cstate
->off_linktype
.reg
;
3007 * If we have any offset-loading code, append all the
3008 * existing statements in the block to those statements,
3009 * and make the resulting list the list of statements
3013 sappend(s
, b
->stmts
);
3018 static struct block
*
3019 gen_ppi_dlt_check(compiler_state_t
*cstate
)
3021 struct slist
*s_load_dlt
;
3024 if (cstate
->linktype
== DLT_PPI
)
3026 /* Create the statements that check for the DLT
3028 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
3029 s_load_dlt
->s
.k
= 4;
3031 b
= new_block(cstate
, JMP(BPF_JEQ
));
3033 b
->stmts
= s_load_dlt
;
3034 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
3045 * Take an absolute offset, and:
3047 * if it has no variable part, return NULL;
3049 * if it has a variable part, generate code to load the register
3050 * containing that variable part into the X register, returning
3051 * a pointer to that code - if no register for that offset has
3052 * been allocated, allocate it first.
3054 * (The code to set that register will be generated later, but will
3055 * be placed earlier in the code sequence.)
3057 static struct slist
*
3058 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3062 if (off
->is_variable
) {
3063 if (off
->reg
== -1) {
3065 * We haven't yet assigned a register for the
3066 * variable part of the offset of the link-layer
3067 * header; allocate one.
3069 off
->reg
= alloc_reg(cstate
);
3073 * Load the register containing the variable part of the
3074 * offset of the link-layer header into the X register.
3076 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3081 * That offset isn't variable, there's no variable part,
3082 * so we don't need to generate any code.
3089 * Map an Ethernet type to the equivalent PPP type.
3092 ethertype_to_ppptype(bpf_u_int32 ll_proto
)
3100 case ETHERTYPE_IPV6
:
3101 ll_proto
= PPP_IPV6
;
3105 ll_proto
= PPP_DECNET
;
3108 case ETHERTYPE_ATALK
:
3109 ll_proto
= PPP_APPLE
;
3122 * I'm assuming the "Bridging PDU"s that go
3123 * over PPP are Spanning Tree Protocol
3126 ll_proto
= PPP_BRPDU
;
3137 * Generate any tests that, for encapsulation of a link-layer packet
3138 * inside another protocol stack, need to be done to check for those
3139 * link-layer packets (and that haven't already been done by a check
3140 * for that encapsulation).
3142 static struct block
*
3143 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3147 if (cstate
->is_geneve
)
3148 return gen_geneve_ll_check(cstate
);
3150 switch (cstate
->prevlinktype
) {
3154 * This is LANE-encapsulated Ethernet; check that the LANE
3155 * packet doesn't begin with an LE Control marker, i.e.
3156 * that it's data, not a control message.
3158 * (We've already generated a test for LANE.)
3160 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3166 * No such tests are necessary.
3174 * The three different values we should check for when checking for an
3175 * IPv6 packet with DLT_NULL.
3177 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3178 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3179 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3182 * Generate code to match a particular packet type by matching the
3183 * link-layer type field or fields in the 802.2 LLC header.
3185 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3186 * value, if <= ETHERMTU.
3188 static struct block
*
3189 gen_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3191 struct block
*b0
, *b1
, *b2
;
3192 const char *description
;
3194 /* are we checking MPLS-encapsulated packets? */
3195 if (cstate
->label_stack_depth
> 0)
3196 return gen_mpls_linktype(cstate
, ll_proto
);
3198 switch (cstate
->linktype
) {
3201 case DLT_NETANALYZER
:
3202 case DLT_NETANALYZER_TRANSPARENT
:
3203 /* Geneve has an EtherType regardless of whether there is an
3205 if (!cstate
->is_geneve
)
3206 b0
= gen_prevlinkhdr_check(cstate
);
3210 b1
= gen_ether_linktype(cstate
, ll_proto
);
3221 ll_proto
= (ll_proto
<< 8 | LLCSAP_ISONS
);
3225 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3229 case DLT_IEEE802_11
:
3230 case DLT_PRISM_HEADER
:
3231 case DLT_IEEE802_11_RADIO_AVS
:
3232 case DLT_IEEE802_11_RADIO
:
3235 * Check that we have a data frame.
3237 b0
= gen_check_802_11_data_frame(cstate
);
3240 * Now check for the specified link-layer type.
3242 b1
= gen_llc_linktype(cstate
, ll_proto
);
3249 * XXX - check for LLC frames.
3251 return gen_llc_linktype(cstate
, ll_proto
);
3256 * XXX - check for LLC PDUs, as per IEEE 802.5.
3258 return gen_llc_linktype(cstate
, ll_proto
);
3261 case DLT_ATM_RFC1483
:
3263 case DLT_IP_OVER_FC
:
3264 return gen_llc_linktype(cstate
, ll_proto
);
3269 * Check for an LLC-encapsulated version of this protocol;
3270 * if we were checking for LANE, linktype would no longer
3273 * Check for LLC encapsulation and then check the protocol.
3275 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3276 b1
= gen_llc_linktype(cstate
, ll_proto
);
3282 return gen_linux_sll_linktype(cstate
, ll_proto
);
3286 case DLT_SLIP_BSDOS
:
3289 * These types don't provide any type field; packets
3290 * are always IPv4 or IPv6.
3292 * XXX - for IPv4, check for a version number of 4, and,
3293 * for IPv6, check for a version number of 6?
3298 /* Check for a version number of 4. */
3299 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3301 case ETHERTYPE_IPV6
:
3302 /* Check for a version number of 6. */
3303 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3306 return gen_false(cstate
); /* always false */
3312 * Raw IPv4, so no type field.
3314 if (ll_proto
== ETHERTYPE_IP
)
3315 return gen_true(cstate
); /* always true */
3317 /* Checking for something other than IPv4; always false */
3318 return gen_false(cstate
);
3323 * Raw IPv6, so no type field.
3325 if (ll_proto
== ETHERTYPE_IPV6
)
3326 return gen_true(cstate
); /* always true */
3328 /* Checking for something other than IPv6; always false */
3329 return gen_false(cstate
);
3334 case DLT_PPP_SERIAL
:
3337 * We use Ethernet protocol types inside libpcap;
3338 * map them to the corresponding PPP protocol types.
3340 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3341 ethertype_to_ppptype(ll_proto
));
3346 * We use Ethernet protocol types inside libpcap;
3347 * map them to the corresponding PPP protocol types.
3353 * Also check for Van Jacobson-compressed IP.
3354 * XXX - do this for other forms of PPP?
3356 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3357 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3359 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3364 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3365 ethertype_to_ppptype(ll_proto
));
3375 return (gen_loopback_linktype(cstate
, AF_INET
));
3377 case ETHERTYPE_IPV6
:
3379 * AF_ values may, unfortunately, be platform-
3380 * dependent; AF_INET isn't, because everybody
3381 * used 4.2BSD's value, but AF_INET6 is, because
3382 * 4.2BSD didn't have a value for it (given that
3383 * IPv6 didn't exist back in the early 1980's),
3384 * and they all picked their own values.
3386 * This means that, if we're reading from a
3387 * savefile, we need to check for all the
3390 * If we're doing a live capture, we only need
3391 * to check for this platform's value; however,
3392 * Npcap uses 24, which isn't Windows's AF_INET6
3393 * value. (Given the multiple different values,
3394 * programs that read pcap files shouldn't be
3395 * checking for their platform's AF_INET6 value
3396 * anyway, they should check for all of the
3397 * possible values. and they might as well do
3398 * that even for live captures.)
3400 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3402 * Savefile - check for all three
3403 * possible IPv6 values.
3405 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3406 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3408 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3413 * Live capture, so we only need to
3414 * check for the value used on this
3419 * Npcap doesn't use Windows's AF_INET6,
3420 * as that collides with AF_IPX on
3421 * some BSDs (both have the value 23).
3422 * Instead, it uses 24.
3424 return (gen_loopback_linktype(cstate
, 24));
3427 return (gen_loopback_linktype(cstate
, AF_INET6
));
3428 #else /* AF_INET6 */
3430 * I guess this platform doesn't support
3431 * IPv6, so we just reject all packets.
3433 return gen_false(cstate
);
3434 #endif /* AF_INET6 */
3440 * Not a type on which we support filtering.
3441 * XXX - support those that have AF_ values
3442 * #defined on this platform, at least?
3444 return gen_false(cstate
);
3449 * af field is host byte order in contrast to the rest of
3452 if (ll_proto
== ETHERTYPE_IP
)
3453 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3455 else if (ll_proto
== ETHERTYPE_IPV6
)
3456 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3459 return gen_false(cstate
);
3463 case DLT_ARCNET_LINUX
:
3465 * XXX should we check for first fragment if the protocol
3471 return gen_false(cstate
);
3473 case ETHERTYPE_IPV6
:
3474 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3478 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3480 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3486 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3488 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3493 case ETHERTYPE_REVARP
:
3494 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3497 case ETHERTYPE_ATALK
:
3498 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3505 case ETHERTYPE_ATALK
:
3506 return gen_true(cstate
);
3508 return gen_false(cstate
);
3514 * XXX - assumes a 2-byte Frame Relay header with
3515 * DLCI and flags. What if the address is longer?
3521 * Check for the special NLPID for IP.
3523 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3525 case ETHERTYPE_IPV6
:
3527 * Check for the special NLPID for IPv6.
3529 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3533 * Check for several OSI protocols.
3535 * Frame Relay packets typically have an OSI
3536 * NLPID at the beginning; we check for each
3539 * What we check for is the NLPID and a frame
3540 * control field of UI, i.e. 0x03 followed
3543 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3544 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3545 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3551 return gen_false(cstate
);
3556 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3558 case DLT_JUNIPER_MFR
:
3559 case DLT_JUNIPER_MLFR
:
3560 case DLT_JUNIPER_MLPPP
:
3561 case DLT_JUNIPER_ATM1
:
3562 case DLT_JUNIPER_ATM2
:
3563 case DLT_JUNIPER_PPPOE
:
3564 case DLT_JUNIPER_PPPOE_ATM
:
3565 case DLT_JUNIPER_GGSN
:
3566 case DLT_JUNIPER_ES
:
3567 case DLT_JUNIPER_MONITOR
:
3568 case DLT_JUNIPER_SERVICES
:
3569 case DLT_JUNIPER_ETHER
:
3570 case DLT_JUNIPER_PPP
:
3571 case DLT_JUNIPER_FRELAY
:
3572 case DLT_JUNIPER_CHDLC
:
3573 case DLT_JUNIPER_VP
:
3574 case DLT_JUNIPER_ST
:
3575 case DLT_JUNIPER_ISM
:
3576 case DLT_JUNIPER_VS
:
3577 case DLT_JUNIPER_SRX_E2E
:
3578 case DLT_JUNIPER_FIBRECHANNEL
:
3579 case DLT_JUNIPER_ATM_CEMIC
:
3581 /* just lets verify the magic number for now -
3582 * on ATM we may have up to 6 different encapsulations on the wire
3583 * and need a lot of heuristics to figure out that the payload
3586 * FIXME encapsulation specific BPF_ filters
3588 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3590 case DLT_BACNET_MS_TP
:
3591 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3594 return gen_ipnet_linktype(cstate
, ll_proto
);
3596 case DLT_LINUX_IRDA
:
3597 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3600 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3603 case DLT_MTP2_WITH_PHDR
:
3604 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3607 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3610 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3612 case DLT_LINUX_LAPD
:
3613 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3615 case DLT_USB_FREEBSD
:
3617 case DLT_USB_LINUX_MMAPPED
:
3619 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3621 case DLT_BLUETOOTH_HCI_H4
:
3622 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3623 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3626 case DLT_CAN_SOCKETCAN
:
3627 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3629 case DLT_IEEE802_15_4
:
3630 case DLT_IEEE802_15_4_LINUX
:
3631 case DLT_IEEE802_15_4_NONASK_PHY
:
3632 case DLT_IEEE802_15_4_NOFCS
:
3633 case DLT_IEEE802_15_4_TAP
:
3634 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3636 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3637 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3640 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3643 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3645 case DLT_IPMB_KONTRON
:
3646 case DLT_IPMB_LINUX
:
3647 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3650 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3653 /* Using the fixed-size NFLOG header it is possible to tell only
3654 * the address family of the packet, other meaningful data is
3655 * either missing or behind TLVs.
3657 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3661 * Does this link-layer header type have a field
3662 * indicating the type of the next protocol? If
3663 * so, off_linktype.constant_part will be the offset of that
3664 * field in the packet; if not, it will be OFFSET_NOT_SET.
3666 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3668 * Yes; assume it's an Ethernet type. (If
3669 * it's not, it needs to be handled specially
3672 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, ll_proto
);
3676 * No; report an error.
3678 description
= pcap_datalink_val_to_description_or_dlt(cstate
->linktype
);
3679 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3687 * Check for an LLC SNAP packet with a given organization code and
3688 * protocol type; we check the entire contents of the 802.2 LLC and
3689 * snap headers, checking for DSAP and SSAP of SNAP and a control
3690 * field of 0x03 in the LLC header, and for the specified organization
3691 * code and protocol type in the SNAP header.
3693 static struct block
*
3694 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3696 u_char snapblock
[8];
3698 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3699 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3700 snapblock
[2] = 0x03; /* control = UI */
3701 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3702 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3703 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3704 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3705 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3706 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3710 * Generate code to match frames with an LLC header.
3712 static struct block
*
3713 gen_llc_internal(compiler_state_t
*cstate
)
3715 struct block
*b0
, *b1
;
3717 switch (cstate
->linktype
) {
3721 * We check for an Ethernet type field less than
3722 * 1500, which means it's an 802.3 length field.
3724 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3728 * Now check for the purported DSAP and SSAP not being
3729 * 0xFF, to rule out NetWare-over-802.3.
3731 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, 0xFFFF);
3738 * We check for LLC traffic.
3740 b0
= gen_atmtype_llc(cstate
);
3743 case DLT_IEEE802
: /* Token Ring */
3745 * XXX - check for LLC frames.
3747 return gen_true(cstate
);
3751 * XXX - check for LLC frames.
3753 return gen_true(cstate
);
3755 case DLT_ATM_RFC1483
:
3757 * For LLC encapsulation, these are defined to have an
3760 * For VC encapsulation, they don't, but there's no
3761 * way to check for that; the protocol used on the VC
3762 * is negotiated out of band.
3764 return gen_true(cstate
);
3766 case DLT_IEEE802_11
:
3767 case DLT_PRISM_HEADER
:
3768 case DLT_IEEE802_11_RADIO
:
3769 case DLT_IEEE802_11_RADIO_AVS
:
3772 * Check that we have a data frame.
3774 b0
= gen_check_802_11_data_frame(cstate
);
3778 bpf_error(cstate
, "'llc' not supported for %s",
3779 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
3785 gen_llc(compiler_state_t
*cstate
)
3788 * Catch errors reported by us and routines below us, and return NULL
3791 if (setjmp(cstate
->top_ctx
))
3794 return gen_llc_internal(cstate
);
3798 gen_llc_i(compiler_state_t
*cstate
)
3800 struct block
*b0
, *b1
;
3804 * Catch errors reported by us and routines below us, and return NULL
3807 if (setjmp(cstate
->top_ctx
))
3811 * Check whether this is an LLC frame.
3813 b0
= gen_llc_internal(cstate
);
3816 * Load the control byte and test the low-order bit; it must
3817 * be clear for I frames.
3819 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3820 b1
= new_block(cstate
, JMP(BPF_JSET
));
3829 gen_llc_s(compiler_state_t
*cstate
)
3831 struct block
*b0
, *b1
;
3834 * Catch errors reported by us and routines below us, and return NULL
3837 if (setjmp(cstate
->top_ctx
))
3841 * Check whether this is an LLC frame.
3843 b0
= gen_llc_internal(cstate
);
3846 * Now compare the low-order 2 bit of the control byte against
3847 * the appropriate value for S frames.
3849 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3855 gen_llc_u(compiler_state_t
*cstate
)
3857 struct block
*b0
, *b1
;
3860 * Catch errors reported by us and routines below us, and return NULL
3863 if (setjmp(cstate
->top_ctx
))
3867 * Check whether this is an LLC frame.
3869 b0
= gen_llc_internal(cstate
);
3872 * Now compare the low-order 2 bit of the control byte against
3873 * the appropriate value for U frames.
3875 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3881 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3883 struct block
*b0
, *b1
;
3886 * Catch errors reported by us and routines below us, and return NULL
3889 if (setjmp(cstate
->top_ctx
))
3893 * Check whether this is an LLC frame.
3895 b0
= gen_llc_internal(cstate
);
3898 * Now check for an S frame with the appropriate type.
3900 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3906 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3908 struct block
*b0
, *b1
;
3911 * Catch errors reported by us and routines below us, and return NULL
3914 if (setjmp(cstate
->top_ctx
))
3918 * Check whether this is an LLC frame.
3920 b0
= gen_llc_internal(cstate
);
3923 * Now check for a U frame with the appropriate type.
3925 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3931 * Generate code to match a particular packet type, for link-layer types
3932 * using 802.2 LLC headers.
3934 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3935 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3937 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3938 * value, if <= ETHERMTU. We use that to determine whether to
3939 * match the DSAP or both DSAP and LSAP or to check the OUI and
3940 * protocol ID in a SNAP header.
3942 static struct block
*
3943 gen_llc_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
3946 * XXX - handle token-ring variable-length header.
3952 case LLCSAP_NETBEUI
:
3954 * XXX - should we check both the DSAP and the
3955 * SSAP, like this, or should we check just the
3956 * DSAP, as we do for other SAP values?
3958 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3959 ((ll_proto
<< 8) | ll_proto
));
3963 * XXX - are there ever SNAP frames for IPX on
3964 * non-Ethernet 802.x networks?
3966 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, LLCSAP_IPX
);
3968 case ETHERTYPE_ATALK
:
3970 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3971 * SNAP packets with an organization code of
3972 * 0x080007 (Apple, for Appletalk) and a protocol
3973 * type of ETHERTYPE_ATALK (Appletalk).
3975 * XXX - check for an organization code of
3976 * encapsulated Ethernet as well?
3978 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3982 * XXX - we don't have to check for IPX 802.3
3983 * here, but should we check for the IPX Ethertype?
3985 if (ll_proto
<= ETHERMTU
) {
3987 * This is an LLC SAP value, so check
3990 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, ll_proto
);
3993 * This is an Ethernet type; we assume that it's
3994 * unlikely that it'll appear in the right place
3995 * at random, and therefore check only the
3996 * location that would hold the Ethernet type
3997 * in a SNAP frame with an organization code of
3998 * 0x000000 (encapsulated Ethernet).
4000 * XXX - if we were to check for the SNAP DSAP and
4001 * LSAP, as per XXX, and were also to check for an
4002 * organization code of 0x000000 (encapsulated
4003 * Ethernet), we'd do
4005 * return gen_snap(cstate, 0x000000, ll_proto);
4007 * here; for now, we don't, as per the above.
4008 * I don't know whether it's worth the extra CPU
4009 * time to do the right check or not.
4011 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, ll_proto
);
4016 static struct block
*
4017 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4018 int dir
, bpf_u_int32 ll_proto
, u_int src_off
, u_int dst_off
)
4020 struct block
*b0
, *b1
;
4034 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4035 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4041 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4042 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4047 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4051 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4055 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4059 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4063 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4067 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4074 b0
= gen_linktype(cstate
, ll_proto
);
4075 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, addr
, mask
);
4081 static struct block
*
4082 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4083 struct in6_addr
*mask
, int dir
, bpf_u_int32 ll_proto
, u_int src_off
,
4086 struct block
*b0
, *b1
;
4101 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4102 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4108 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, ll_proto
, src_off
, dst_off
);
4109 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, ll_proto
, src_off
, dst_off
);
4114 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4118 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4122 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4126 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4130 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4134 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4141 /* this order is important */
4142 a
= (uint32_t *)addr
;
4143 m
= (uint32_t *)mask
;
4144 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4145 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4147 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4149 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4151 b0
= gen_linktype(cstate
, ll_proto
);
4157 static struct block
*
4158 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4160 register struct block
*b0
, *b1
;
4164 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4167 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4170 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4171 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4177 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4178 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4183 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4187 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4191 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4195 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4199 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4203 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4211 * Like gen_ehostop, but for DLT_FDDI
4213 static struct block
*
4214 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4216 struct block
*b0
, *b1
;
4220 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4223 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4226 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4227 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4233 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4234 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4239 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4243 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4247 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4251 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4255 bpf_error(cstate
, "'ra' is only supported on 802.11");
4259 bpf_error(cstate
, "'ta' is only supported on 802.11");
4267 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4269 static struct block
*
4270 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4272 register struct block
*b0
, *b1
;
4276 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4279 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4282 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4283 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4289 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4290 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4295 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4299 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4303 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4307 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4311 bpf_error(cstate
, "'ra' is only supported on 802.11");
4315 bpf_error(cstate
, "'ta' is only supported on 802.11");
4323 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4324 * various 802.11 + radio headers.
4326 static struct block
*
4327 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4329 register struct block
*b0
, *b1
, *b2
;
4330 register struct slist
*s
;
4332 #ifdef ENABLE_WLAN_FILTERING_PATCH
4335 * We need to disable the optimizer because the optimizer is buggy
4336 * and wipes out some LD instructions generated by the below
4337 * code to validate the Frame Control bits
4339 cstate
->no_optimize
= 1;
4340 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4347 * For control frames, there is no SA.
4349 * For management frames, SA is at an
4350 * offset of 10 from the beginning of
4353 * For data frames, SA is at an offset
4354 * of 10 from the beginning of the packet
4355 * if From DS is clear, at an offset of
4356 * 16 from the beginning of the packet
4357 * if From DS is set and To DS is clear,
4358 * and an offset of 24 from the beginning
4359 * of the packet if From DS is set and To DS
4364 * Generate the tests to be done for data frames
4367 * First, check for To DS set, i.e. check "link[1] & 0x01".
4369 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4370 b1
= new_block(cstate
, JMP(BPF_JSET
));
4371 b1
->s
.k
= 0x01; /* To DS */
4375 * If To DS is set, the SA is at 24.
4377 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4381 * Now, check for To DS not set, i.e. check
4382 * "!(link[1] & 0x01)".
4384 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4385 b2
= new_block(cstate
, JMP(BPF_JSET
));
4386 b2
->s
.k
= 0x01; /* To DS */
4391 * If To DS is not set, the SA is at 16.
4393 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4397 * Now OR together the last two checks. That gives
4398 * the complete set of checks for data frames with
4404 * Now check for From DS being set, and AND that with
4405 * the ORed-together checks.
4407 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4408 b1
= new_block(cstate
, JMP(BPF_JSET
));
4409 b1
->s
.k
= 0x02; /* From DS */
4414 * Now check for data frames with From DS not set.
4416 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4417 b2
= new_block(cstate
, JMP(BPF_JSET
));
4418 b2
->s
.k
= 0x02; /* From DS */
4423 * If From DS isn't set, the SA is at 10.
4425 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4429 * Now OR together the checks for data frames with
4430 * From DS not set and for data frames with From DS
4431 * set; that gives the checks done for data frames.
4436 * Now check for a data frame.
4437 * I.e, check "link[0] & 0x08".
4439 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4440 b1
= new_block(cstate
, JMP(BPF_JSET
));
4445 * AND that with the checks done for data frames.
4450 * If the high-order bit of the type value is 0, this
4451 * is a management frame.
4452 * I.e, check "!(link[0] & 0x08)".
4454 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4455 b2
= new_block(cstate
, JMP(BPF_JSET
));
4461 * For management frames, the SA is at 10.
4463 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4467 * OR that with the checks done for data frames.
4468 * That gives the checks done for management and
4474 * If the low-order bit of the type value is 1,
4475 * this is either a control frame or a frame
4476 * with a reserved type, and thus not a
4479 * I.e., check "!(link[0] & 0x04)".
4481 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4482 b1
= new_block(cstate
, JMP(BPF_JSET
));
4488 * AND that with the checks for data and management
4498 * For control frames, there is no DA.
4500 * For management frames, DA is at an
4501 * offset of 4 from the beginning of
4504 * For data frames, DA is at an offset
4505 * of 4 from the beginning of the packet
4506 * if To DS is clear and at an offset of
4507 * 16 from the beginning of the packet
4512 * Generate the tests to be done for data frames.
4514 * First, check for To DS set, i.e. "link[1] & 0x01".
4516 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4517 b1
= new_block(cstate
, JMP(BPF_JSET
));
4518 b1
->s
.k
= 0x01; /* To DS */
4522 * If To DS is set, the DA is at 16.
4524 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4528 * Now, check for To DS not set, i.e. check
4529 * "!(link[1] & 0x01)".
4531 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4532 b2
= new_block(cstate
, JMP(BPF_JSET
));
4533 b2
->s
.k
= 0x01; /* To DS */
4538 * If To DS is not set, the DA is at 4.
4540 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4544 * Now OR together the last two checks. That gives
4545 * the complete set of checks for data frames.
4550 * Now check for a data frame.
4551 * I.e, check "link[0] & 0x08".
4553 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4554 b1
= new_block(cstate
, JMP(BPF_JSET
));
4559 * AND that with the checks done for data frames.
4564 * If the high-order bit of the type value is 0, this
4565 * is a management frame.
4566 * I.e, check "!(link[0] & 0x08)".
4568 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4569 b2
= new_block(cstate
, JMP(BPF_JSET
));
4575 * For management frames, the DA is at 4.
4577 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4581 * OR that with the checks done for data frames.
4582 * That gives the checks done for management and
4588 * If the low-order bit of the type value is 1,
4589 * this is either a control frame or a frame
4590 * with a reserved type, and thus not a
4593 * I.e., check "!(link[0] & 0x04)".
4595 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4596 b1
= new_block(cstate
, JMP(BPF_JSET
));
4602 * AND that with the checks for data and management
4609 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4610 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4616 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4617 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4622 * XXX - add BSSID keyword?
4625 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4629 * Not present in CTS or ACK control frames.
4631 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4632 IEEE80211_FC0_TYPE_MASK
);
4634 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4635 IEEE80211_FC0_SUBTYPE_MASK
);
4637 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4638 IEEE80211_FC0_SUBTYPE_MASK
);
4642 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4648 * Not present in control frames.
4650 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4651 IEEE80211_FC0_TYPE_MASK
);
4653 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4659 * Present only if the direction mask has both "From DS"
4660 * and "To DS" set. Neither control frames nor management
4661 * frames should have both of those set, so we don't
4662 * check the frame type.
4664 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4665 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4666 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4672 * Not present in management frames; addr1 in other
4677 * If the high-order bit of the type value is 0, this
4678 * is a management frame.
4679 * I.e, check "(link[0] & 0x08)".
4681 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4682 b1
= new_block(cstate
, JMP(BPF_JSET
));
4689 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4692 * AND that with the check of addr1.
4699 * Not present in management frames; addr2, if present,
4704 * Not present in CTS or ACK control frames.
4706 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4707 IEEE80211_FC0_TYPE_MASK
);
4709 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4710 IEEE80211_FC0_SUBTYPE_MASK
);
4712 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4713 IEEE80211_FC0_SUBTYPE_MASK
);
4719 * If the high-order bit of the type value is 0, this
4720 * is a management frame.
4721 * I.e, check "(link[0] & 0x08)".
4723 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4724 b1
= new_block(cstate
, JMP(BPF_JSET
));
4729 * AND that with the check for frames other than
4730 * CTS and ACK frames.
4737 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4746 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4747 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4748 * as the RFC states.)
4750 static struct block
*
4751 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4753 register struct block
*b0
, *b1
;
4757 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4760 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4763 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4764 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4770 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4771 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4776 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4780 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4784 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4788 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4792 bpf_error(cstate
, "'ra' is only supported on 802.11");
4796 bpf_error(cstate
, "'ta' is only supported on 802.11");
4804 * This is quite tricky because there may be pad bytes in front of the
4805 * DECNET header, and then there are two possible data packet formats that
4806 * carry both src and dst addresses, plus 5 packet types in a format that
4807 * carries only the src node, plus 2 types that use a different format and
4808 * also carry just the src node.
4812 * Instead of doing those all right, we just look for data packets with
4813 * 0 or 1 bytes of padding. If you want to look at other packets, that
4814 * will require a lot more hacking.
4816 * To add support for filtering on DECNET "areas" (network numbers)
4817 * one would want to add a "mask" argument to this routine. That would
4818 * make the filter even more inefficient, although one could be clever
4819 * and not generate masking instructions if the mask is 0xFFFF.
4821 static struct block
*
4822 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4824 struct block
*b0
, *b1
, *b2
, *tmp
;
4825 u_int offset_lh
; /* offset if long header is received */
4826 u_int offset_sh
; /* offset if short header is received */
4831 offset_sh
= 1; /* follows flags */
4832 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4836 offset_sh
= 3; /* follows flags, dstnode */
4837 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4841 /* Inefficient because we do our Calvinball dance twice */
4842 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4843 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4849 /* Inefficient because we do our Calvinball dance twice */
4850 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4851 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4856 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4860 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4864 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4868 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4872 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4876 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4883 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4884 /* Check for pad = 1, long header case */
4885 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4886 (bpf_u_int32
)ntohs(0x0681), (bpf_u_int32
)ntohs(0x07FF));
4887 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4888 BPF_H
, (bpf_u_int32
)ntohs((u_short
)addr
));
4890 /* Check for pad = 0, long header case */
4891 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x06,
4893 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
,
4894 (bpf_u_int32
)ntohs((u_short
)addr
));
4897 /* Check for pad = 1, short header case */
4898 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4899 (bpf_u_int32
)ntohs(0x0281), (bpf_u_int32
)ntohs(0x07FF));
4900 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
,
4901 (bpf_u_int32
)ntohs((u_short
)addr
));
4904 /* Check for pad = 0, short header case */
4905 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_u_int32
)0x02,
4907 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
,
4908 (bpf_u_int32
)ntohs((u_short
)addr
));
4912 /* Combine with test for cstate->linktype */
4918 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4919 * test the bottom-of-stack bit, and then check the version number
4920 * field in the IP header.
4922 static struct block
*
4923 gen_mpls_linktype(compiler_state_t
*cstate
, bpf_u_int32 ll_proto
)
4925 struct block
*b0
, *b1
;
4930 /* match the bottom-of-stack bit */
4931 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4932 /* match the IPv4 version number */
4933 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4937 case ETHERTYPE_IPV6
:
4938 /* match the bottom-of-stack bit */
4939 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4940 /* match the IPv4 version number */
4941 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4946 /* FIXME add other L3 proto IDs */
4947 bpf_error(cstate
, "unsupported protocol over mpls");
4952 static struct block
*
4953 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4954 int proto
, int dir
, int type
)
4956 struct block
*b0
, *b1
;
4957 const char *typestr
;
4967 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4969 * Only check for non-IPv4 addresses if we're not
4970 * checking MPLS-encapsulated packets.
4972 if (cstate
->label_stack_depth
== 0) {
4973 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4975 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4981 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4984 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4987 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4990 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4993 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4996 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4999 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
5002 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
5005 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
5008 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
5011 bpf_error(cstate
, "AppleTalk host filtering not implemented");
5014 return gen_dnhostop(cstate
, addr
, dir
);
5017 bpf_error(cstate
, "LAT host filtering not implemented");
5020 bpf_error(cstate
, "SCA host filtering not implemented");
5023 bpf_error(cstate
, "MOPRC host filtering not implemented");
5026 bpf_error(cstate
, "MOPDL host filtering not implemented");
5029 bpf_error(cstate
, "'ip6' modifier applied to ip host");
5032 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
5035 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5038 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5041 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5044 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5047 bpf_error(cstate
, "AARP host filtering not implemented");
5050 bpf_error(cstate
, "ISO host filtering not implemented");
5053 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5056 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5059 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5062 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5065 bpf_error(cstate
, "IPX host filtering not implemented");
5068 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5071 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5074 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5077 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5080 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5083 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5086 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5089 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5092 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5095 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5104 static struct block
*
5105 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5106 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5108 const char *typestr
;
5118 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5121 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5124 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5127 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5130 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5133 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5136 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5139 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5142 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5145 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5148 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5151 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5154 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5157 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5160 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5163 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5166 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5169 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5172 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5175 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5178 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5181 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5184 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5187 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5190 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5193 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5196 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5199 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5202 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5205 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5208 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5211 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5214 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5217 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5220 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5223 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5226 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5229 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5232 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5235 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5245 static struct block
*
5246 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5247 struct addrinfo
*alist
, int proto
, int dir
)
5249 struct block
*b0
, *b1
, *tmp
;
5250 struct addrinfo
*ai
;
5251 struct sockaddr_in
*sin
;
5254 bpf_error(cstate
, "direction applied to 'gateway'");
5261 switch (cstate
->linktype
) {
5263 case DLT_NETANALYZER
:
5264 case DLT_NETANALYZER_TRANSPARENT
:
5265 b1
= gen_prevlinkhdr_check(cstate
);
5266 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5271 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5274 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5276 case DLT_IEEE802_11
:
5277 case DLT_PRISM_HEADER
:
5278 case DLT_IEEE802_11_RADIO_AVS
:
5279 case DLT_IEEE802_11_RADIO
:
5281 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5285 * This is LLC-multiplexed traffic; if it were
5286 * LANE, cstate->linktype would have been set to
5290 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5292 case DLT_IP_OVER_FC
:
5293 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5297 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5300 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5302 * Does it have an address?
5304 if (ai
->ai_addr
!= NULL
) {
5306 * Yes. Is it an IPv4 address?
5308 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5310 * Generate an entry for it.
5312 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5313 tmp
= gen_host(cstate
,
5314 ntohl(sin
->sin_addr
.s_addr
),
5315 0xffffffff, proto
, Q_OR
, Q_HOST
);
5317 * Is it the *first* IPv4 address?
5321 * Yes, so start with it.
5326 * No, so OR it into the
5338 * No IPv4 addresses found.
5346 bpf_error(cstate
, "illegal modifier of 'gateway'");
5351 static struct block
*
5352 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5360 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_DEFAULT
, Q_DEFAULT
);
5364 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_DEFAULT
, Q_DEFAULT
);
5368 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_DEFAULT
, Q_DEFAULT
);
5372 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5375 #ifndef IPPROTO_IGMP
5376 #define IPPROTO_IGMP 2
5380 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5383 #ifndef IPPROTO_IGRP
5384 #define IPPROTO_IGRP 9
5387 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5391 #define IPPROTO_PIM 103
5395 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_DEFAULT
, Q_DEFAULT
);
5398 #ifndef IPPROTO_VRRP
5399 #define IPPROTO_VRRP 112
5403 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5406 #ifndef IPPROTO_CARP
5407 #define IPPROTO_CARP 112
5411 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5415 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5419 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5423 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5427 bpf_error(cstate
, "link layer applied in wrong context");
5430 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5434 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5438 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5442 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5446 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5450 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5454 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5458 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5461 #ifndef IPPROTO_ICMPV6
5462 #define IPPROTO_ICMPV6 58
5465 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5469 #define IPPROTO_AH 51
5472 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_DEFAULT
, Q_DEFAULT
);
5476 #define IPPROTO_ESP 50
5479 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_DEFAULT
, Q_DEFAULT
);
5483 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5487 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5491 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5494 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5495 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5496 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5498 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5500 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5502 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5506 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5507 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5508 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5510 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5512 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5514 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5518 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5519 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5520 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5522 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5527 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5528 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5533 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5534 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5536 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5538 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5543 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5544 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5549 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5550 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5555 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5559 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5563 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5567 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5571 bpf_error(cstate
, "'radio' is not a valid protocol type");
5580 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5583 * Catch errors reported by us and routines below us, and return NULL
5586 if (setjmp(cstate
->top_ctx
))
5589 return gen_proto_abbrev_internal(cstate
, proto
);
5592 static struct block
*
5593 gen_ipfrag(compiler_state_t
*cstate
)
5598 /* not IPv4 frag other than the first frag */
5599 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5600 b
= new_block(cstate
, JMP(BPF_JSET
));
5609 * Generate a comparison to a port value in the transport-layer header
5610 * at the specified offset from the beginning of that header.
5612 * XXX - this handles a variable-length prefix preceding the link-layer
5613 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5614 * variable-length link-layer headers (such as Token Ring or 802.11
5617 static struct block
*
5618 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5620 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5623 static struct block
*
5624 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_u_int32 v
)
5626 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5629 static struct block
*
5630 gen_portop(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5632 struct block
*b0
, *b1
, *tmp
;
5634 /* ip proto 'proto' and not a fragment other than the first fragment */
5635 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5636 b0
= gen_ipfrag(cstate
);
5641 b1
= gen_portatom(cstate
, 0, port
);
5645 b1
= gen_portatom(cstate
, 2, port
);
5649 tmp
= gen_portatom(cstate
, 0, port
);
5650 b1
= gen_portatom(cstate
, 2, port
);
5656 tmp
= gen_portatom(cstate
, 0, port
);
5657 b1
= gen_portatom(cstate
, 2, port
);
5662 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5666 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5670 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5674 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5678 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5682 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5694 static struct block
*
5695 gen_port(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5697 struct block
*b0
, *b1
, *tmp
;
5702 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5703 * not LLC encapsulation with LLCSAP_IP.
5705 * For IEEE 802 networks - which includes 802.5 token ring
5706 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5707 * says that SNAP encapsulation is used, not LLC encapsulation
5710 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5711 * RFC 2225 say that SNAP encapsulation is used, not LLC
5712 * encapsulation with LLCSAP_IP.
5714 * So we always check for ETHERTYPE_IP.
5716 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5722 b1
= gen_portop(cstate
, port
, (u_int
)ip_proto
, dir
);
5726 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5727 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5729 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5741 gen_portop6(compiler_state_t
*cstate
, u_int port
, u_int proto
, int dir
)
5743 struct block
*b0
, *b1
, *tmp
;
5745 /* ip6 proto 'proto' */
5746 /* XXX - catch the first fragment of a fragmented packet? */
5747 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5751 b1
= gen_portatom6(cstate
, 0, port
);
5755 b1
= gen_portatom6(cstate
, 2, port
);
5759 tmp
= gen_portatom6(cstate
, 0, port
);
5760 b1
= gen_portatom6(cstate
, 2, port
);
5766 tmp
= gen_portatom6(cstate
, 0, port
);
5767 b1
= gen_portatom6(cstate
, 2, port
);
5779 static struct block
*
5780 gen_port6(compiler_state_t
*cstate
, u_int port
, int ip_proto
, int dir
)
5782 struct block
*b0
, *b1
, *tmp
;
5784 /* link proto ip6 */
5785 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5791 b1
= gen_portop6(cstate
, port
, (u_int
)ip_proto
, dir
);
5795 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5796 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5798 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5809 /* gen_portrange code */
5810 static struct block
*
5811 gen_portrangeatom(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5814 struct block
*b1
, *b2
;
5818 * Reverse the order of the ports, so v1 is the lower one.
5827 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5828 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5835 static struct block
*
5836 gen_portrangeop(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5837 bpf_u_int32 proto
, int dir
)
5839 struct block
*b0
, *b1
, *tmp
;
5841 /* ip proto 'proto' and not a fragment other than the first fragment */
5842 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, proto
);
5843 b0
= gen_ipfrag(cstate
);
5848 b1
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5852 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5856 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5857 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5863 tmp
= gen_portrangeatom(cstate
, 0, port1
, port2
);
5864 b1
= gen_portrangeatom(cstate
, 2, port1
, port2
);
5869 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5873 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5877 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5881 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5885 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5889 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5901 static struct block
*
5902 gen_portrange(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
5905 struct block
*b0
, *b1
, *tmp
;
5908 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5914 b1
= gen_portrangeop(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
5919 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5920 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5922 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5933 static struct block
*
5934 gen_portrangeatom6(compiler_state_t
*cstate
, u_int off
, bpf_u_int32 v1
,
5937 struct block
*b1
, *b2
;
5941 * Reverse the order of the ports, so v1 is the lower one.
5950 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5951 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5958 static struct block
*
5959 gen_portrangeop6(compiler_state_t
*cstate
, u_int port1
, u_int port2
,
5960 bpf_u_int32 proto
, int dir
)
5962 struct block
*b0
, *b1
, *tmp
;
5964 /* ip6 proto 'proto' */
5965 /* XXX - catch the first fragment of a fragmented packet? */
5966 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, proto
);
5970 b1
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5974 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5978 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5979 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5985 tmp
= gen_portrangeatom6(cstate
, 0, port1
, port2
);
5986 b1
= gen_portrangeatom6(cstate
, 2, port1
, port2
);
5998 static struct block
*
5999 gen_portrange6(compiler_state_t
*cstate
, u_int port1
, u_int port2
, int ip_proto
,
6002 struct block
*b0
, *b1
, *tmp
;
6004 /* link proto ip6 */
6005 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6011 b1
= gen_portrangeop6(cstate
, port1
, port2
, (bpf_u_int32
)ip_proto
,
6016 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
6017 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
6019 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6031 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6040 v
= pcap_nametoproto(name
);
6041 if (v
== PROTO_UNDEF
)
6042 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6046 /* XXX should look up h/w protocol type based on cstate->linktype */
6047 v
= pcap_nametoeproto(name
);
6048 if (v
== PROTO_UNDEF
) {
6049 v
= pcap_nametollc(name
);
6050 if (v
== PROTO_UNDEF
)
6051 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6056 if (strcmp(name
, "esis") == 0)
6058 else if (strcmp(name
, "isis") == 0)
6060 else if (strcmp(name
, "clnp") == 0)
6063 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6073 static struct block
*
6074 gen_protochain(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
)
6076 #ifdef NO_PROTOCHAIN
6077 return gen_proto(cstate
, v
, proto
);
6079 struct block
*b0
, *b
;
6080 struct slist
*s
[100];
6081 int fix2
, fix3
, fix4
, fix5
;
6082 int ahcheck
, again
, end
;
6084 int reg2
= alloc_reg(cstate
);
6086 memset(s
, 0, sizeof(s
));
6087 fix3
= fix4
= fix5
= 0;
6094 b0
= gen_protochain(cstate
, v
, Q_IP
);
6095 b
= gen_protochain(cstate
, v
, Q_IPV6
);
6099 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6104 * We don't handle variable-length prefixes before the link-layer
6105 * header, or variable-length link-layer headers, here yet.
6106 * We might want to add BPF instructions to do the protochain
6107 * work, to simplify that and, on platforms that have a BPF
6108 * interpreter with the new instructions, let the filtering
6109 * be done in the kernel. (We already require a modified BPF
6110 * engine to do the protochain stuff, to support backward
6111 * branches, and backward branch support is unlikely to appear
6112 * in kernel BPF engines.)
6114 if (cstate
->off_linkpl
.is_variable
)
6115 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6118 * To quote a comment in optimize.c:
6120 * "These data structures are used in a Cocke and Shwarz style
6121 * value numbering scheme. Since the flowgraph is acyclic,
6122 * exit values can be propagated from a node's predecessors
6123 * provided it is uniquely defined."
6125 * "Acyclic" means "no backward branches", which means "no
6126 * loops", so we have to turn the optimizer off.
6128 cstate
->no_optimize
= 1;
6131 * s[0] is a dummy entry to protect other BPF insn from damage
6132 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6133 * hard to find interdependency made by jump table fixup.
6136 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6141 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6144 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6145 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6147 /* X = ip->ip_hl << 2 */
6148 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6149 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6154 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6156 /* A = ip6->ip_nxt */
6157 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6158 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6160 /* X = sizeof(struct ip6_hdr) */
6161 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6167 bpf_error(cstate
, "unsupported proto to gen_protochain");
6171 /* again: if (A == v) goto end; else fall through; */
6173 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6175 s
[i
]->s
.jt
= NULL
; /*later*/
6176 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6180 #ifndef IPPROTO_NONE
6181 #define IPPROTO_NONE 59
6183 /* if (A == IPPROTO_NONE) goto end */
6184 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6185 s
[i
]->s
.jt
= NULL
; /*later*/
6186 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6187 s
[i
]->s
.k
= IPPROTO_NONE
;
6188 s
[fix5
]->s
.jf
= s
[i
];
6192 if (proto
== Q_IPV6
) {
6193 int v6start
, v6end
, v6advance
, j
;
6196 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6197 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6198 s
[i
]->s
.jt
= NULL
; /*later*/
6199 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6200 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6201 s
[fix2
]->s
.jf
= s
[i
];
6203 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6204 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6205 s
[i
]->s
.jt
= NULL
; /*later*/
6206 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6207 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6209 /* if (A == IPPROTO_ROUTING) goto v6advance */
6210 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6211 s
[i
]->s
.jt
= NULL
; /*later*/
6212 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6213 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6215 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6216 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6217 s
[i
]->s
.jt
= NULL
; /*later*/
6218 s
[i
]->s
.jf
= NULL
; /*later*/
6219 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6229 * A = P[X + packet head];
6230 * X = X + (P[X + packet head + 1] + 1) * 8;
6232 /* A = P[X + packet head] */
6233 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6234 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6237 s
[i
] = new_stmt(cstate
, BPF_ST
);
6240 /* A = P[X + packet head + 1]; */
6241 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6242 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6245 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6249 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6253 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6257 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6260 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6264 /* goto again; (must use BPF_JA for backward jump) */
6265 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6266 s
[i
]->s
.k
= again
- i
- 1;
6267 s
[i
- 1]->s
.jf
= s
[i
];
6271 for (j
= v6start
; j
<= v6end
; j
++)
6272 s
[j
]->s
.jt
= s
[v6advance
];
6275 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6277 s
[fix2
]->s
.jf
= s
[i
];
6283 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6284 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6285 s
[i
]->s
.jt
= NULL
; /*later*/
6286 s
[i
]->s
.jf
= NULL
; /*later*/
6287 s
[i
]->s
.k
= IPPROTO_AH
;
6289 s
[fix3
]->s
.jf
= s
[ahcheck
];
6296 * X = X + (P[X + 1] + 2) * 4;
6299 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6301 /* A = P[X + packet head]; */
6302 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6303 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6306 s
[i
] = new_stmt(cstate
, BPF_ST
);
6310 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6313 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6317 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6319 /* A = P[X + packet head] */
6320 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6321 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6324 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6328 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6332 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6335 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6339 /* goto again; (must use BPF_JA for backward jump) */
6340 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6341 s
[i
]->s
.k
= again
- i
- 1;
6346 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6348 s
[fix2
]->s
.jt
= s
[end
];
6349 s
[fix4
]->s
.jf
= s
[end
];
6350 s
[fix5
]->s
.jt
= s
[end
];
6357 for (i
= 0; i
< max
- 1; i
++)
6358 s
[i
]->next
= s
[i
+ 1];
6359 s
[max
- 1]->next
= NULL
;
6364 b
= new_block(cstate
, JMP(BPF_JEQ
));
6365 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6368 free_reg(cstate
, reg2
);
6375 static struct block
*
6376 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6379 struct block
*b0
, *b1
;
6382 * A data frame has the 0x08 bit (b3) in the frame control field set
6383 * and the 0x04 bit (b2) clear.
6385 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6386 b0
= new_block(cstate
, JMP(BPF_JSET
));
6390 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6391 b1
= new_block(cstate
, JMP(BPF_JSET
));
6402 * Generate code that checks whether the packet is a packet for protocol
6403 * <proto> and whether the type field in that protocol's header has
6404 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6405 * IP packet and checks the protocol number in the IP header against <v>.
6407 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6408 * against Q_IP and Q_IPV6.
6410 static struct block
*
6411 gen_proto(compiler_state_t
*cstate
, bpf_u_int32 v
, int proto
, int dir
)
6413 struct block
*b0
, *b1
;
6416 if (dir
!= Q_DEFAULT
)
6417 bpf_error(cstate
, "direction applied to 'proto'");
6421 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6422 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6427 return gen_linktype(cstate
, v
);
6431 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6432 * not LLC encapsulation with LLCSAP_IP.
6434 * For IEEE 802 networks - which includes 802.5 token ring
6435 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6436 * says that SNAP encapsulation is used, not LLC encapsulation
6439 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6440 * RFC 2225 say that SNAP encapsulation is used, not LLC
6441 * encapsulation with LLCSAP_IP.
6443 * So we always check for ETHERTYPE_IP.
6445 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6446 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, v
);
6451 bpf_error(cstate
, "arp does not encapsulate another protocol");
6455 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6459 bpf_error(cstate
, "'sctp proto' is bogus");
6463 bpf_error(cstate
, "'tcp proto' is bogus");
6467 bpf_error(cstate
, "'udp proto' is bogus");
6471 bpf_error(cstate
, "'icmp proto' is bogus");
6475 bpf_error(cstate
, "'igmp proto' is bogus");
6479 bpf_error(cstate
, "'igrp proto' is bogus");
6483 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6487 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6491 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6495 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6499 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6503 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6507 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6509 * Also check for a fragment header before the final
6512 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6513 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, v
);
6515 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, v
);
6521 bpf_error(cstate
, "'icmp6 proto' is bogus");
6525 bpf_error(cstate
, "'ah proto' is bogus");
6529 bpf_error(cstate
, "'esp proto' is bogus");
6533 bpf_error(cstate
, "'pim proto' is bogus");
6537 bpf_error(cstate
, "'vrrp proto' is bogus");
6541 bpf_error(cstate
, "'aarp proto' is bogus");
6545 switch (cstate
->linktype
) {
6549 * Frame Relay packets typically have an OSI
6550 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6551 * generates code to check for all the OSI
6552 * NLPIDs, so calling it and then adding a check
6553 * for the particular NLPID for which we're
6554 * looking is bogus, as we can just check for
6557 * What we check for is the NLPID and a frame
6558 * control field value of UI, i.e. 0x03 followed
6561 * XXX - assumes a 2-byte Frame Relay header with
6562 * DLCI and flags. What if the address is longer?
6564 * XXX - what about SNAP-encapsulated frames?
6566 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6572 * Cisco uses an Ethertype lookalike - for OSI,
6575 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6576 /* OSI in C-HDLC is stuffed with a fudge byte */
6577 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, v
);
6582 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6583 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, v
);
6589 bpf_error(cstate
, "'esis proto' is bogus");
6593 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6595 * 4 is the offset of the PDU type relative to the IS-IS
6598 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, v
);
6603 bpf_error(cstate
, "'clnp proto' is not supported");
6607 bpf_error(cstate
, "'stp proto' is bogus");
6611 bpf_error(cstate
, "'ipx proto' is bogus");
6615 bpf_error(cstate
, "'netbeui proto' is bogus");
6619 bpf_error(cstate
, "'l1 proto' is bogus");
6623 bpf_error(cstate
, "'l2 proto' is bogus");
6627 bpf_error(cstate
, "'iih proto' is bogus");
6631 bpf_error(cstate
, "'snp proto' is bogus");
6635 bpf_error(cstate
, "'csnp proto' is bogus");
6639 bpf_error(cstate
, "'psnp proto' is bogus");
6643 bpf_error(cstate
, "'lsp proto' is bogus");
6647 bpf_error(cstate
, "'radio proto' is bogus");
6651 bpf_error(cstate
, "'carp proto' is bogus");
6662 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6664 int proto
= q
.proto
;
6668 bpf_u_int32 mask
, addr
;
6669 struct addrinfo
*res
, *res0
;
6670 struct sockaddr_in
*sin4
;
6673 struct sockaddr_in6
*sin6
;
6674 struct in6_addr mask128
;
6676 struct block
*b
, *tmp
;
6677 int port
, real_proto
;
6681 * Catch errors reported by us and routines below us, and return NULL
6684 if (setjmp(cstate
->top_ctx
))
6690 addr
= pcap_nametonetaddr(name
);
6692 bpf_error(cstate
, "unknown network '%s'", name
);
6693 /* Left justify network addr and calculate its network mask */
6695 while (addr
&& (addr
& 0xff000000) == 0) {
6699 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6703 if (proto
== Q_LINK
) {
6704 switch (cstate
->linktype
) {
6707 case DLT_NETANALYZER
:
6708 case DLT_NETANALYZER_TRANSPARENT
:
6709 eaddr
= pcap_ether_hostton(name
);
6712 "unknown ether host '%s'", name
);
6713 tmp
= gen_prevlinkhdr_check(cstate
);
6714 b
= gen_ehostop(cstate
, eaddr
, dir
);
6721 eaddr
= pcap_ether_hostton(name
);
6724 "unknown FDDI host '%s'", name
);
6725 b
= gen_fhostop(cstate
, eaddr
, dir
);
6730 eaddr
= pcap_ether_hostton(name
);
6733 "unknown token ring host '%s'", name
);
6734 b
= gen_thostop(cstate
, eaddr
, dir
);
6738 case DLT_IEEE802_11
:
6739 case DLT_PRISM_HEADER
:
6740 case DLT_IEEE802_11_RADIO_AVS
:
6741 case DLT_IEEE802_11_RADIO
:
6743 eaddr
= pcap_ether_hostton(name
);
6746 "unknown 802.11 host '%s'", name
);
6747 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6751 case DLT_IP_OVER_FC
:
6752 eaddr
= pcap_ether_hostton(name
);
6755 "unknown Fibre Channel host '%s'", name
);
6756 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6761 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6762 } else if (proto
== Q_DECNET
) {
6763 unsigned short dn_addr
;
6765 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6767 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6769 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6774 * I don't think DECNET hosts can be multihomed, so
6775 * there is no need to build up a list of addresses
6777 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6780 memset(&mask128
, 0xff, sizeof(mask128
));
6782 res0
= res
= pcap_nametoaddrinfo(name
);
6784 bpf_error(cstate
, "unknown host '%s'", name
);
6791 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6792 tproto
== Q_DEFAULT
) {
6798 for (res
= res0
; res
; res
= res
->ai_next
) {
6799 switch (res
->ai_family
) {
6802 if (tproto
== Q_IPV6
)
6806 sin4
= (struct sockaddr_in
*)
6808 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6809 0xffffffff, tproto
, dir
, q
.addr
);
6813 if (tproto6
== Q_IP
)
6816 sin6
= (struct sockaddr_in6
*)
6818 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6819 &mask128
, tproto6
, dir
, q
.addr
);
6832 bpf_error(cstate
, "unknown host '%s'%s", name
,
6833 (proto
== Q_DEFAULT
)
6835 : " for specified address family");
6841 if (proto
!= Q_DEFAULT
&&
6842 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6843 bpf_error(cstate
, "illegal qualifier of 'port'");
6844 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6845 bpf_error(cstate
, "unknown port '%s'", name
);
6846 if (proto
== Q_UDP
) {
6847 if (real_proto
== IPPROTO_TCP
)
6848 bpf_error(cstate
, "port '%s' is tcp", name
);
6849 else if (real_proto
== IPPROTO_SCTP
)
6850 bpf_error(cstate
, "port '%s' is sctp", name
);
6852 /* override PROTO_UNDEF */
6853 real_proto
= IPPROTO_UDP
;
6855 if (proto
== Q_TCP
) {
6856 if (real_proto
== IPPROTO_UDP
)
6857 bpf_error(cstate
, "port '%s' is udp", name
);
6859 else if (real_proto
== IPPROTO_SCTP
)
6860 bpf_error(cstate
, "port '%s' is sctp", name
);
6862 /* override PROTO_UNDEF */
6863 real_proto
= IPPROTO_TCP
;
6865 if (proto
== Q_SCTP
) {
6866 if (real_proto
== IPPROTO_UDP
)
6867 bpf_error(cstate
, "port '%s' is udp", name
);
6869 else if (real_proto
== IPPROTO_TCP
)
6870 bpf_error(cstate
, "port '%s' is tcp", name
);
6872 /* override PROTO_UNDEF */
6873 real_proto
= IPPROTO_SCTP
;
6876 bpf_error(cstate
, "illegal port number %d < 0", port
);
6878 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6879 b
= gen_port(cstate
, port
, real_proto
, dir
);
6880 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6884 if (proto
!= Q_DEFAULT
&&
6885 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6886 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6887 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6888 bpf_error(cstate
, "unknown port in range '%s'", name
);
6889 if (proto
== Q_UDP
) {
6890 if (real_proto
== IPPROTO_TCP
)
6891 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6892 else if (real_proto
== IPPROTO_SCTP
)
6893 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6895 /* override PROTO_UNDEF */
6896 real_proto
= IPPROTO_UDP
;
6898 if (proto
== Q_TCP
) {
6899 if (real_proto
== IPPROTO_UDP
)
6900 bpf_error(cstate
, "port in range '%s' is udp", name
);
6901 else if (real_proto
== IPPROTO_SCTP
)
6902 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6904 /* override PROTO_UNDEF */
6905 real_proto
= IPPROTO_TCP
;
6907 if (proto
== Q_SCTP
) {
6908 if (real_proto
== IPPROTO_UDP
)
6909 bpf_error(cstate
, "port in range '%s' is udp", name
);
6910 else if (real_proto
== IPPROTO_TCP
)
6911 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6913 /* override PROTO_UNDEF */
6914 real_proto
= IPPROTO_SCTP
;
6917 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6919 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6921 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6923 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6925 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6926 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6931 eaddr
= pcap_ether_hostton(name
);
6933 bpf_error(cstate
, "unknown ether host: %s", name
);
6935 res
= pcap_nametoaddrinfo(name
);
6938 bpf_error(cstate
, "unknown host '%s'", name
);
6939 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6943 bpf_error(cstate
, "unknown host '%s'", name
);
6946 bpf_error(cstate
, "'gateway' not supported in this configuration");
6950 real_proto
= lookup_proto(cstate
, name
, proto
);
6951 if (real_proto
>= 0)
6952 return gen_proto(cstate
, real_proto
, proto
, dir
);
6954 bpf_error(cstate
, "unknown protocol: %s", name
);
6957 real_proto
= lookup_proto(cstate
, name
, proto
);
6958 if (real_proto
>= 0)
6959 return gen_protochain(cstate
, real_proto
, proto
);
6961 bpf_error(cstate
, "unknown protocol: %s", name
);
6972 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6973 bpf_u_int32 masklen
, struct qual q
)
6975 register int nlen
, mlen
;
6979 * Catch errors reported by us and routines below us, and return NULL
6982 if (setjmp(cstate
->top_ctx
))
6985 nlen
= __pcap_atoin(s1
, &n
);
6987 bpf_error(cstate
, "invalid IPv4 address '%s'", s1
);
6988 /* Promote short ipaddr */
6992 mlen
= __pcap_atoin(s2
, &m
);
6994 bpf_error(cstate
, "invalid IPv4 address '%s'", s2
);
6995 /* Promote short ipaddr */
6998 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
7001 /* Convert mask len to mask */
7003 bpf_error(cstate
, "mask length must be <= 32");
7006 * X << 32 is not guaranteed by C to be 0; it's
7011 m
= 0xffffffff << (32 - masklen
);
7013 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
7020 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
7023 bpf_error(cstate
, "Mask syntax for networks only");
7030 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7038 * Catch errors reported by us and routines below us, and return NULL
7041 if (setjmp(cstate
->top_ctx
))
7048 else if (q
.proto
== Q_DECNET
) {
7049 vlen
= __pcap_atodn(s
, &v
);
7051 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7053 vlen
= __pcap_atoin(s
, &v
);
7055 bpf_error(cstate
, "invalid IPv4 address '%s'", s
);
7063 if (proto
== Q_DECNET
)
7064 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7065 else if (proto
== Q_LINK
) {
7066 bpf_error(cstate
, "illegal link layer address");
7069 if (s
== NULL
&& q
.addr
== Q_NET
) {
7070 /* Promote short net number */
7071 while (v
&& (v
& 0xff000000) == 0) {
7076 /* Promote short ipaddr */
7078 mask
<<= 32 - vlen
;
7080 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7085 proto
= IPPROTO_UDP
;
7086 else if (proto
== Q_TCP
)
7087 proto
= IPPROTO_TCP
;
7088 else if (proto
== Q_SCTP
)
7089 proto
= IPPROTO_SCTP
;
7090 else if (proto
== Q_DEFAULT
)
7091 proto
= PROTO_UNDEF
;
7093 bpf_error(cstate
, "illegal qualifier of 'port'");
7096 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7100 b
= gen_port(cstate
, v
, proto
, dir
);
7101 gen_or(gen_port6(cstate
, v
, proto
, dir
), b
);
7107 proto
= IPPROTO_UDP
;
7108 else if (proto
== Q_TCP
)
7109 proto
= IPPROTO_TCP
;
7110 else if (proto
== Q_SCTP
)
7111 proto
= IPPROTO_SCTP
;
7112 else if (proto
== Q_DEFAULT
)
7113 proto
= PROTO_UNDEF
;
7115 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7118 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7122 b
= gen_portrange(cstate
, v
, v
, proto
, dir
);
7123 gen_or(gen_portrange6(cstate
, v
, v
, proto
, dir
), b
);
7128 bpf_error(cstate
, "'gateway' requires a name");
7132 return gen_proto(cstate
, v
, proto
, dir
);
7135 return gen_protochain(cstate
, v
, proto
);
7150 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7151 bpf_u_int32 masklen
, struct qual q
)
7153 struct addrinfo
*res
;
7154 struct in6_addr
*addr
;
7155 struct in6_addr mask
;
7160 * Catch errors reported by us and routines below us, and return NULL
7163 if (setjmp(cstate
->top_ctx
))
7167 bpf_error(cstate
, "no mask %s supported", s2
);
7169 res
= pcap_nametoaddrinfo(s1
);
7171 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7174 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7175 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7177 if (masklen
> sizeof(mask
.s6_addr
) * 8)
7178 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
.s6_addr
) * 8));
7179 memset(&mask
, 0, sizeof(mask
));
7180 memset(&mask
.s6_addr
, 0xff, masklen
/ 8);
7182 mask
.s6_addr
[masklen
/ 8] =
7183 (0xff << (8 - masklen
% 8)) & 0xff;
7186 a
= (uint32_t *)addr
;
7187 m
= (uint32_t *)&mask
;
7188 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7189 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7190 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7198 bpf_error(cstate
, "Mask syntax for networks only");
7202 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7208 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7215 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7217 struct block
*b
, *tmp
;
7220 * Catch errors reported by us and routines below us, and return NULL
7223 if (setjmp(cstate
->top_ctx
))
7226 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7227 cstate
->e
= pcap_ether_aton(s
);
7228 if (cstate
->e
== NULL
)
7229 bpf_error(cstate
, "malloc");
7230 switch (cstate
->linktype
) {
7232 case DLT_NETANALYZER
:
7233 case DLT_NETANALYZER_TRANSPARENT
:
7234 tmp
= gen_prevlinkhdr_check(cstate
);
7235 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7240 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7243 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7245 case DLT_IEEE802_11
:
7246 case DLT_PRISM_HEADER
:
7247 case DLT_IEEE802_11_RADIO_AVS
:
7248 case DLT_IEEE802_11_RADIO
:
7250 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7252 case DLT_IP_OVER_FC
:
7253 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7258 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7265 bpf_error(cstate
, "ethernet address used in non-ether expression");
7270 sappend(struct slist
*s0
, struct slist
*s1
)
7273 * This is definitely not the best way to do this, but the
7274 * lists will rarely get long.
7281 static struct slist
*
7282 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7286 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7291 static struct slist
*
7292 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7296 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7302 * Modify "index" to use the value stored into its register as an
7303 * offset relative to the beginning of the header for the protocol
7304 * "proto", and allocate a register and put an item "size" bytes long
7305 * (1, 2, or 4) at that offset into that register, making it the register
7308 static struct arth
*
7309 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7313 struct slist
*s
, *tmp
;
7315 int regno
= alloc_reg(cstate
);
7317 free_reg(cstate
, inst
->regno
);
7321 bpf_error(cstate
, "data size must be 1, 2, or 4");
7338 bpf_error(cstate
, "unsupported index operation");
7342 * The offset is relative to the beginning of the packet
7343 * data, if we have a radio header. (If we don't, this
7346 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7347 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7348 cstate
->linktype
!= DLT_PRISM_HEADER
)
7349 bpf_error(cstate
, "radio information not present in capture");
7352 * Load into the X register the offset computed into the
7353 * register specified by "index".
7355 s
= xfer_to_x(cstate
, inst
);
7358 * Load the item at that offset.
7360 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7362 sappend(inst
->s
, s
);
7367 * The offset is relative to the beginning of
7368 * the link-layer header.
7370 * XXX - what about ATM LANE? Should the index be
7371 * relative to the beginning of the AAL5 frame, so
7372 * that 0 refers to the beginning of the LE Control
7373 * field, or relative to the beginning of the LAN
7374 * frame, so that 0 refers, for Ethernet LANE, to
7375 * the beginning of the destination address?
7377 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7380 * If "s" is non-null, it has code to arrange that the
7381 * X register contains the length of the prefix preceding
7382 * the link-layer header. Add to it the offset computed
7383 * into the register specified by "index", and move that
7384 * into the X register. Otherwise, just load into the X
7385 * register the offset computed into the register specified
7389 sappend(s
, xfer_to_a(cstate
, inst
));
7390 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7391 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7393 s
= xfer_to_x(cstate
, inst
);
7396 * Load the item at the sum of the offset we've put in the
7397 * X register and the offset of the start of the link
7398 * layer header (which is 0 if the radio header is
7399 * variable-length; that header length is what we put
7400 * into the X register and then added to the index).
7402 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7403 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7405 sappend(inst
->s
, s
);
7419 * The offset is relative to the beginning of
7420 * the network-layer header.
7421 * XXX - are there any cases where we want
7422 * cstate->off_nl_nosnap?
7424 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7427 * If "s" is non-null, it has code to arrange that the
7428 * X register contains the variable part of the offset
7429 * of the link-layer payload. Add to it the offset
7430 * computed into the register specified by "index",
7431 * and move that into the X register. Otherwise, just
7432 * load into the X register the offset computed into
7433 * the register specified by "index".
7436 sappend(s
, xfer_to_a(cstate
, inst
));
7437 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7438 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7440 s
= xfer_to_x(cstate
, inst
);
7443 * Load the item at the sum of the offset we've put in the
7444 * X register, the offset of the start of the network
7445 * layer header from the beginning of the link-layer
7446 * payload, and the constant part of the offset of the
7447 * start of the link-layer payload.
7449 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7450 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7452 sappend(inst
->s
, s
);
7455 * Do the computation only if the packet contains
7456 * the protocol in question.
7458 b
= gen_proto_abbrev_internal(cstate
, proto
);
7460 gen_and(inst
->b
, b
);
7474 * The offset is relative to the beginning of
7475 * the transport-layer header.
7477 * Load the X register with the length of the IPv4 header
7478 * (plus the offset of the link-layer header, if it's
7479 * a variable-length header), in bytes.
7481 * XXX - are there any cases where we want
7482 * cstate->off_nl_nosnap?
7483 * XXX - we should, if we're built with
7484 * IPv6 support, generate code to load either
7485 * IPv4, IPv6, or both, as appropriate.
7487 s
= gen_loadx_iphdrlen(cstate
);
7490 * The X register now contains the sum of the variable
7491 * part of the offset of the link-layer payload and the
7492 * length of the network-layer header.
7494 * Load into the A register the offset relative to
7495 * the beginning of the transport layer header,
7496 * add the X register to that, move that to the
7497 * X register, and load with an offset from the
7498 * X register equal to the sum of the constant part of
7499 * the offset of the link-layer payload and the offset,
7500 * relative to the beginning of the link-layer payload,
7501 * of the network-layer header.
7503 sappend(s
, xfer_to_a(cstate
, inst
));
7504 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7505 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7506 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
));
7507 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7508 sappend(inst
->s
, s
);
7511 * Do the computation only if the packet contains
7512 * the protocol in question - which is true only
7513 * if this is an IP datagram and is the first or
7514 * only fragment of that datagram.
7516 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7518 gen_and(inst
->b
, b
);
7519 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7524 * Do the computation only if the packet contains
7525 * the protocol in question.
7527 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7529 gen_and(inst
->b
, b
);
7534 * Check if we have an icmp6 next header
7536 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7538 gen_and(inst
->b
, b
);
7543 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7545 * If "s" is non-null, it has code to arrange that the
7546 * X register contains the variable part of the offset
7547 * of the link-layer payload. Add to it the offset
7548 * computed into the register specified by "index",
7549 * and move that into the X register. Otherwise, just
7550 * load into the X register the offset computed into
7551 * the register specified by "index".
7554 sappend(s
, xfer_to_a(cstate
, inst
));
7555 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7556 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7558 s
= xfer_to_x(cstate
, inst
);
7562 * Load the item at the sum of the offset we've put in the
7563 * X register, the offset of the start of the network
7564 * layer header from the beginning of the link-layer
7565 * payload, and the constant part of the offset of the
7566 * start of the link-layer payload.
7568 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size_code
);
7569 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7572 sappend(inst
->s
, s
);
7576 inst
->regno
= regno
;
7577 s
= new_stmt(cstate
, BPF_ST
);
7579 sappend(inst
->s
, s
);
7585 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
,
7589 * Catch errors reported by us and routines below us, and return NULL
7592 if (setjmp(cstate
->top_ctx
))
7595 return gen_load_internal(cstate
, proto
, inst
, size
);
7598 static struct block
*
7599 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7600 struct arth
*a1
, int reversed
)
7602 struct slist
*s0
, *s1
, *s2
;
7603 struct block
*b
, *tmp
;
7605 s0
= xfer_to_x(cstate
, a1
);
7606 s1
= xfer_to_a(cstate
, a0
);
7607 if (code
== BPF_JEQ
) {
7608 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7609 b
= new_block(cstate
, JMP(code
));
7613 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7619 sappend(a0
->s
, a1
->s
);
7623 free_reg(cstate
, a0
->regno
);
7624 free_reg(cstate
, a1
->regno
);
7626 /* 'and' together protocol checks */
7629 gen_and(a0
->b
, tmp
= a1
->b
);
7643 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7644 struct arth
*a1
, int reversed
)
7647 * Catch errors reported by us and routines below us, and return NULL
7650 if (setjmp(cstate
->top_ctx
))
7653 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7657 gen_loadlen(compiler_state_t
*cstate
)
7664 * Catch errors reported by us and routines below us, and return NULL
7667 if (setjmp(cstate
->top_ctx
))
7670 regno
= alloc_reg(cstate
);
7671 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7672 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7673 s
->next
= new_stmt(cstate
, BPF_ST
);
7674 s
->next
->s
.k
= regno
;
7681 static struct arth
*
7682 gen_loadi_internal(compiler_state_t
*cstate
, bpf_u_int32 val
)
7688 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7690 reg
= alloc_reg(cstate
);
7692 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7694 s
->next
= new_stmt(cstate
, BPF_ST
);
7703 gen_loadi(compiler_state_t
*cstate
, bpf_u_int32 val
)
7706 * Catch errors reported by us and routines below us, and return NULL
7709 if (setjmp(cstate
->top_ctx
))
7712 return gen_loadi_internal(cstate
, val
);
7716 * The a_arg dance is to avoid annoying whining by compilers that
7717 * a might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7718 * It's not *used* after setjmp returns.
7721 gen_neg(compiler_state_t
*cstate
, struct arth
*a_arg
)
7723 struct arth
*a
= a_arg
;
7727 * Catch errors reported by us and routines below us, and return NULL
7730 if (setjmp(cstate
->top_ctx
))
7733 s
= xfer_to_a(cstate
, a
);
7735 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7738 s
= new_stmt(cstate
, BPF_ST
);
7746 * The a0_arg dance is to avoid annoying whining by compilers that
7747 * a0 might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
7748 * It's not *used* after setjmp returns.
7751 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0_arg
,
7754 struct arth
*a0
= a0_arg
;
7755 struct slist
*s0
, *s1
, *s2
;
7758 * Catch errors reported by us and routines below us, and return NULL
7761 if (setjmp(cstate
->top_ctx
))
7765 * Disallow division by, or modulus by, zero; we do this here
7766 * so that it gets done even if the optimizer is disabled.
7768 * Also disallow shifts by a value greater than 31; we do this
7769 * here, for the same reason.
7771 if (code
== BPF_DIV
) {
7772 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7773 bpf_error(cstate
, "division by zero");
7774 } else if (code
== BPF_MOD
) {
7775 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7776 bpf_error(cstate
, "modulus by zero");
7777 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7778 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
> 31)
7779 bpf_error(cstate
, "shift by more than 31 bits");
7781 s0
= xfer_to_x(cstate
, a1
);
7782 s1
= xfer_to_a(cstate
, a0
);
7783 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7788 sappend(a0
->s
, a1
->s
);
7790 free_reg(cstate
, a0
->regno
);
7791 free_reg(cstate
, a1
->regno
);
7793 s0
= new_stmt(cstate
, BPF_ST
);
7794 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7801 * Initialize the table of used registers and the current register.
7804 init_regs(compiler_state_t
*cstate
)
7807 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7811 * Return the next free register.
7814 alloc_reg(compiler_state_t
*cstate
)
7816 int n
= BPF_MEMWORDS
;
7819 if (cstate
->regused
[cstate
->curreg
])
7820 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7822 cstate
->regused
[cstate
->curreg
] = 1;
7823 return cstate
->curreg
;
7826 bpf_error(cstate
, "too many registers needed to evaluate expression");
7831 * Return a register to the table so it can
7835 free_reg(compiler_state_t
*cstate
, int n
)
7837 cstate
->regused
[n
] = 0;
7840 static struct block
*
7841 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7846 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7847 b
= new_block(cstate
, JMP(jmp
));
7855 gen_greater(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 return gen_len(cstate
, BPF_JGE
, n
);
7868 * Actually, this is less than or equal.
7871 gen_less(compiler_state_t
*cstate
, int n
)
7876 * Catch errors reported by us and routines below us, and return NULL
7879 if (setjmp(cstate
->top_ctx
))
7882 b
= gen_len(cstate
, BPF_JGT
, n
);
7889 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7890 * the beginning of the link-layer header.
7891 * XXX - that means you can't test values in the radiotap header, but
7892 * as that header is difficult if not impossible to parse generally
7893 * without a loop, that might not be a severe problem. A new keyword
7894 * "radio" could be added for that, although what you'd really want
7895 * would be a way of testing particular radio header values, which
7896 * would generate code appropriate to the radio header in question.
7899 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, bpf_u_int32 val
)
7905 * Catch errors reported by us and routines below us, and return NULL
7908 if (setjmp(cstate
->top_ctx
))
7916 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7919 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7923 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, val
);
7927 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7931 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7935 b
= new_block(cstate
, JMP(BPF_JEQ
));
7942 static const u_char abroadcast
[] = { 0x0 };
7945 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7947 bpf_u_int32 hostmask
;
7948 struct block
*b0
, *b1
, *b2
;
7949 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7952 * Catch errors reported by us and routines below us, and return NULL
7955 if (setjmp(cstate
->top_ctx
))
7962 switch (cstate
->linktype
) {
7964 case DLT_ARCNET_LINUX
:
7965 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7967 case DLT_NETANALYZER
:
7968 case DLT_NETANALYZER_TRANSPARENT
:
7969 b1
= gen_prevlinkhdr_check(cstate
);
7970 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7975 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7977 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7978 case DLT_IEEE802_11
:
7979 case DLT_PRISM_HEADER
:
7980 case DLT_IEEE802_11_RADIO_AVS
:
7981 case DLT_IEEE802_11_RADIO
:
7983 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7984 case DLT_IP_OVER_FC
:
7985 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7987 bpf_error(cstate
, "not a broadcast link");
7993 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7994 * as an indication that we don't know the netmask, and fail
7997 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7998 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7999 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8000 hostmask
= ~cstate
->netmask
;
8001 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, 0, hostmask
);
8002 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
8003 ~0 & hostmask
, hostmask
);
8008 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
8013 * Generate code to test the low-order bit of a MAC address (that's
8014 * the bottom bit of the *first* byte).
8016 static struct block
*
8017 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
8019 register struct block
*b0
;
8020 register struct slist
*s
;
8022 /* link[offset] & 1 != 0 */
8023 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
8024 b0
= new_block(cstate
, JMP(BPF_JSET
));
8031 gen_multicast(compiler_state_t
*cstate
, int proto
)
8033 register struct block
*b0
, *b1
, *b2
;
8034 register struct slist
*s
;
8037 * Catch errors reported by us and routines below us, and return NULL
8040 if (setjmp(cstate
->top_ctx
))
8047 switch (cstate
->linktype
) {
8049 case DLT_ARCNET_LINUX
:
8050 /* all ARCnet multicasts use the same address */
8051 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8053 case DLT_NETANALYZER
:
8054 case DLT_NETANALYZER_TRANSPARENT
:
8055 b1
= gen_prevlinkhdr_check(cstate
);
8056 /* ether[0] & 1 != 0 */
8057 b0
= gen_mac_multicast(cstate
, 0);
8063 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8065 * XXX - was that referring to bit-order issues?
8067 /* fddi[1] & 1 != 0 */
8068 return gen_mac_multicast(cstate
, 1);
8070 /* tr[2] & 1 != 0 */
8071 return gen_mac_multicast(cstate
, 2);
8072 case DLT_IEEE802_11
:
8073 case DLT_PRISM_HEADER
:
8074 case DLT_IEEE802_11_RADIO_AVS
:
8075 case DLT_IEEE802_11_RADIO
:
8080 * For control frames, there is no DA.
8082 * For management frames, DA is at an
8083 * offset of 4 from the beginning of
8086 * For data frames, DA is at an offset
8087 * of 4 from the beginning of the packet
8088 * if To DS is clear and at an offset of
8089 * 16 from the beginning of the packet
8094 * Generate the tests to be done for data frames.
8096 * First, check for To DS set, i.e. "link[1] & 0x01".
8098 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8099 b1
= new_block(cstate
, JMP(BPF_JSET
));
8100 b1
->s
.k
= 0x01; /* To DS */
8104 * If To DS is set, the DA is at 16.
8106 b0
= gen_mac_multicast(cstate
, 16);
8110 * Now, check for To DS not set, i.e. check
8111 * "!(link[1] & 0x01)".
8113 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8114 b2
= new_block(cstate
, JMP(BPF_JSET
));
8115 b2
->s
.k
= 0x01; /* To DS */
8120 * If To DS is not set, the DA is at 4.
8122 b1
= gen_mac_multicast(cstate
, 4);
8126 * Now OR together the last two checks. That gives
8127 * the complete set of checks for data frames.
8132 * Now check for a data frame.
8133 * I.e, check "link[0] & 0x08".
8135 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8136 b1
= new_block(cstate
, JMP(BPF_JSET
));
8141 * AND that with the checks done for data frames.
8146 * If the high-order bit of the type value is 0, this
8147 * is a management frame.
8148 * I.e, check "!(link[0] & 0x08)".
8150 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8151 b2
= new_block(cstate
, JMP(BPF_JSET
));
8157 * For management frames, the DA is at 4.
8159 b1
= gen_mac_multicast(cstate
, 4);
8163 * OR that with the checks done for data frames.
8164 * That gives the checks done for management and
8170 * If the low-order bit of the type value is 1,
8171 * this is either a control frame or a frame
8172 * with a reserved type, and thus not a
8175 * I.e., check "!(link[0] & 0x04)".
8177 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8178 b1
= new_block(cstate
, JMP(BPF_JSET
));
8184 * AND that with the checks for data and management
8189 case DLT_IP_OVER_FC
:
8190 b0
= gen_mac_multicast(cstate
, 2);
8195 /* Link not known to support multicasts */
8199 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8200 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, 224);
8205 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8206 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, 255);
8210 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8215 gen_ifindex(compiler_state_t
*cstate
, int ifindex
)
8217 register struct block
*b0
;
8220 * Catch errors reported by us and routines below us, and return NULL
8223 if (setjmp(cstate
->top_ctx
))
8227 * Only some data link types support ifindex qualifiers.
8229 switch (cstate
->linktype
) {
8230 case DLT_LINUX_SLL2
:
8231 /* match packets on this interface */
8232 b0
= gen_cmp(cstate
, OR_LINKHDR
, 4, BPF_W
, ifindex
);
8237 * This is Linux; we require PF_PACKET support.
8238 * If this is a *live* capture, we can look at
8239 * special meta-data in the filter expression;
8240 * if it's a savefile, we can't.
8242 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8243 /* We have a FILE *, so this is a savefile */
8244 bpf_error(cstate
, "ifindex not supported on %s when reading savefiles",
8245 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8250 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_IFINDEX
, BPF_W
,
8252 #else /* defined(linux) */
8253 bpf_error(cstate
, "ifindex not supported on %s",
8254 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8256 #endif /* defined(linux) */
8262 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8263 * Outbound traffic is sent by this machine, while inbound traffic is
8264 * sent by a remote machine (and may include packets destined for a
8265 * unicast or multicast link-layer address we are not subscribing to).
8266 * These are the same definitions implemented by pcap_setdirection().
8267 * Capturing only unicast traffic destined for this host is probably
8268 * better accomplished using a higher-layer filter.
8271 gen_inbound(compiler_state_t
*cstate
, int dir
)
8273 register struct block
*b0
;
8276 * Catch errors reported by us and routines below us, and return NULL
8279 if (setjmp(cstate
->top_ctx
))
8283 * Only some data link types support inbound/outbound qualifiers.
8285 switch (cstate
->linktype
) {
8287 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8288 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8289 gen_loadi_internal(cstate
, 0),
8295 /* match outgoing packets */
8296 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8298 /* match incoming packets */
8299 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8304 /* match outgoing packets */
8305 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8307 /* to filter on inbound traffic, invert the match */
8312 case DLT_LINUX_SLL2
:
8313 /* match outgoing packets */
8314 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8316 /* to filter on inbound traffic, invert the match */
8322 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8323 ((dir
== 0) ? PF_IN
: PF_OUT
));
8328 /* match outgoing packets */
8329 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8331 /* match incoming packets */
8332 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8336 case DLT_JUNIPER_MFR
:
8337 case DLT_JUNIPER_MLFR
:
8338 case DLT_JUNIPER_MLPPP
:
8339 case DLT_JUNIPER_ATM1
:
8340 case DLT_JUNIPER_ATM2
:
8341 case DLT_JUNIPER_PPPOE
:
8342 case DLT_JUNIPER_PPPOE_ATM
:
8343 case DLT_JUNIPER_GGSN
:
8344 case DLT_JUNIPER_ES
:
8345 case DLT_JUNIPER_MONITOR
:
8346 case DLT_JUNIPER_SERVICES
:
8347 case DLT_JUNIPER_ETHER
:
8348 case DLT_JUNIPER_PPP
:
8349 case DLT_JUNIPER_FRELAY
:
8350 case DLT_JUNIPER_CHDLC
:
8351 case DLT_JUNIPER_VP
:
8352 case DLT_JUNIPER_ST
:
8353 case DLT_JUNIPER_ISM
:
8354 case DLT_JUNIPER_VS
:
8355 case DLT_JUNIPER_SRX_E2E
:
8356 case DLT_JUNIPER_FIBRECHANNEL
:
8357 case DLT_JUNIPER_ATM_CEMIC
:
8359 /* juniper flags (including direction) are stored
8360 * the byte after the 3-byte magic number */
8362 /* match outgoing packets */
8363 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8365 /* match incoming packets */
8366 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8372 * If we have packet meta-data indicating a direction,
8373 * and that metadata can be checked by BPF code, check
8374 * it. Otherwise, give up, as this link-layer type has
8375 * nothing in the packet data.
8377 * Currently, the only platform where a BPF filter can
8378 * check that metadata is Linux with the in-kernel
8379 * BPF interpreter. If other packet capture mechanisms
8380 * and BPF filters also supported this, it would be
8381 * nice. It would be even better if they made that
8382 * metadata available so that we could provide it
8383 * with newer capture APIs, allowing it to be saved
8388 * This is Linux; we require PF_PACKET support.
8389 * If this is a *live* capture, we can look at
8390 * special meta-data in the filter expression;
8391 * if it's a savefile, we can't.
8393 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8394 /* We have a FILE *, so this is a savefile */
8395 bpf_error(cstate
, "inbound/outbound not supported on %s when reading savefiles",
8396 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8399 /* match outgoing packets */
8400 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8403 /* to filter on inbound traffic, invert the match */
8406 #else /* defined(linux) */
8407 bpf_error(cstate
, "inbound/outbound not supported on %s",
8408 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8410 #endif /* defined(linux) */
8415 /* PF firewall log matched interface */
8417 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8423 * Catch errors reported by us and routines below us, and return NULL
8426 if (setjmp(cstate
->top_ctx
))
8429 if (cstate
->linktype
!= DLT_PFLOG
) {
8430 bpf_error(cstate
, "ifname supported only on PF linktype");
8433 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8434 off
= offsetof(struct pfloghdr
, ifname
);
8435 if (strlen(ifname
) >= len
) {
8436 bpf_error(cstate
, "ifname interface names can only be %d characters",
8440 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, (u_int
)strlen(ifname
),
8441 (const u_char
*)ifname
);
8445 /* PF firewall log ruleset name */
8447 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8452 * Catch errors reported by us and routines below us, and return NULL
8455 if (setjmp(cstate
->top_ctx
))
8458 if (cstate
->linktype
!= DLT_PFLOG
) {
8459 bpf_error(cstate
, "ruleset supported only on PF linktype");
8463 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8464 bpf_error(cstate
, "ruleset names can only be %ld characters",
8465 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8469 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8470 (u_int
)strlen(ruleset
), (const u_char
*)ruleset
);
8474 /* PF firewall log rule number */
8476 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8481 * Catch errors reported by us and routines below us, and return NULL
8484 if (setjmp(cstate
->top_ctx
))
8487 if (cstate
->linktype
!= DLT_PFLOG
) {
8488 bpf_error(cstate
, "rnr supported only on PF linktype");
8492 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8497 /* PF firewall log sub-rule number */
8499 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8504 * Catch errors reported by us and routines below us, and return NULL
8507 if (setjmp(cstate
->top_ctx
))
8510 if (cstate
->linktype
!= DLT_PFLOG
) {
8511 bpf_error(cstate
, "srnr supported only on PF linktype");
8515 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8520 /* PF firewall log reason code */
8522 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8527 * Catch errors reported by us and routines below us, and return NULL
8530 if (setjmp(cstate
->top_ctx
))
8533 if (cstate
->linktype
!= DLT_PFLOG
) {
8534 bpf_error(cstate
, "reason supported only on PF linktype");
8538 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8539 (bpf_u_int32
)reason
);
8543 /* PF firewall log action */
8545 gen_pf_action(compiler_state_t
*cstate
, int action
)
8550 * Catch errors reported by us and routines below us, and return NULL
8553 if (setjmp(cstate
->top_ctx
))
8556 if (cstate
->linktype
!= DLT_PFLOG
) {
8557 bpf_error(cstate
, "action supported only on PF linktype");
8561 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8562 (bpf_u_int32
)action
);
8566 /* IEEE 802.11 wireless header */
8568 gen_p80211_type(compiler_state_t
*cstate
, bpf_u_int32 type
, bpf_u_int32 mask
)
8573 * Catch errors reported by us and routines below us, and return NULL
8576 if (setjmp(cstate
->top_ctx
))
8579 switch (cstate
->linktype
) {
8581 case DLT_IEEE802_11
:
8582 case DLT_PRISM_HEADER
:
8583 case DLT_IEEE802_11_RADIO_AVS
:
8584 case DLT_IEEE802_11_RADIO
:
8585 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, type
, mask
);
8589 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8597 gen_p80211_fcdir(compiler_state_t
*cstate
, bpf_u_int32 fcdir
)
8602 * Catch errors reported by us and routines below us, and return NULL
8605 if (setjmp(cstate
->top_ctx
))
8608 switch (cstate
->linktype
) {
8610 case DLT_IEEE802_11
:
8611 case DLT_PRISM_HEADER
:
8612 case DLT_IEEE802_11_RADIO_AVS
:
8613 case DLT_IEEE802_11_RADIO
:
8617 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8621 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, fcdir
,
8622 IEEE80211_FC1_DIR_MASK
);
8628 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8633 * Catch errors reported by us and routines below us, and return NULL
8636 if (setjmp(cstate
->top_ctx
))
8639 switch (cstate
->linktype
) {
8642 case DLT_ARCNET_LINUX
:
8643 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8644 q
.proto
== Q_LINK
) {
8645 cstate
->e
= pcap_ether_aton(s
);
8646 if (cstate
->e
== NULL
)
8647 bpf_error(cstate
, "malloc");
8648 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8653 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8657 bpf_error(cstate
, "aid supported only on ARCnet");
8662 static struct block
*
8663 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8665 register struct block
*b0
, *b1
;
8668 /* src comes first, different from Ethernet */
8670 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8673 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8676 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8677 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8683 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8684 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8689 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8693 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8697 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8701 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8705 bpf_error(cstate
, "'ra' is only supported on 802.11");
8709 bpf_error(cstate
, "'ta' is only supported on 802.11");
8716 static struct block
*
8717 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8719 struct block
*b0
, *b1
;
8721 /* check for VLAN, including QinQ */
8722 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8723 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8726 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8732 static struct block
*
8733 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8735 if (vlan_num
> 0x0fff) {
8736 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8739 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, vlan_num
, 0x0fff);
8742 static struct block
*
8743 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8746 struct block
*b0
, *b1
;
8748 b0
= gen_vlan_tpid_test(cstate
);
8751 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8757 * Both payload and link header type follow the VLAN tags so that
8758 * both need to be updated.
8760 cstate
->off_linkpl
.constant_part
+= 4;
8761 cstate
->off_linktype
.constant_part
+= 4;
8766 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8767 /* add v to variable part of off */
8769 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
,
8770 bpf_u_int32 v
, struct slist
*s
)
8774 if (!off
->is_variable
)
8775 off
->is_variable
= 1;
8777 off
->reg
= alloc_reg(cstate
);
8779 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8782 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8785 s2
= new_stmt(cstate
, BPF_ST
);
8791 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8792 * and link type offsets first
8795 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8799 /* offset determined at run time, shift variable part */
8801 cstate
->is_vlan_vloffset
= 1;
8802 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8803 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8805 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8806 sappend(s
.next
, b_tpid
->head
->stmts
);
8807 b_tpid
->head
->stmts
= s
.next
;
8811 * patch block b_vid (VLAN id test) to load VID value either from packet
8812 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8815 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8817 struct slist
*s
, *s2
, *sjeq
;
8820 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8821 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8823 /* true -> next instructions, false -> beginning of b_vid */
8824 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8826 sjeq
->s
.jf
= b_vid
->stmts
;
8829 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8830 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8834 /* Jump to the test in b_vid. We need to jump one instruction before
8835 * the end of the b_vid block so that we only skip loading the TCI
8836 * from packet data and not the 'and' instruction extractging VID.
8839 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8841 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8845 /* insert our statements at the beginning of b_vid */
8846 sappend(s
, b_vid
->stmts
);
8851 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8852 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8853 * tag can be either in metadata or in packet data; therefore if the
8854 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8855 * header for VLAN tag. As the decision is done at run time, we need
8856 * update variable part of the offsets
8858 static struct block
*
8859 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8862 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8865 /* generate new filter code based on extracting packet
8867 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8868 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8870 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8875 * This is tricky. We need to insert the statements updating variable
8876 * parts of offsets before the traditional TPID and VID tests so
8877 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8878 * we do not want this update to affect those checks. That's why we
8879 * generate both test blocks first and insert the statements updating
8880 * variable parts of both offsets after that. This wouldn't work if
8881 * there already were variable length link header when entering this
8882 * function but gen_vlan_bpf_extensions() isn't called in that case.
8884 b_tpid
= gen_vlan_tpid_test(cstate
);
8886 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8888 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8893 gen_vlan_patch_vid_test(cstate
, b_vid
);
8903 * support IEEE 802.1Q VLAN trunk over ethernet
8906 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8911 * Catch errors reported by us and routines below us, and return NULL
8914 if (setjmp(cstate
->top_ctx
))
8917 /* can't check for VLAN-encapsulated packets inside MPLS */
8918 if (cstate
->label_stack_depth
> 0)
8919 bpf_error(cstate
, "no VLAN match after MPLS");
8922 * Check for a VLAN packet, and then change the offsets to point
8923 * to the type and data fields within the VLAN packet. Just
8924 * increment the offsets, so that we can support a hierarchy, e.g.
8925 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8928 * XXX - this is a bit of a kludge. If we were to split the
8929 * compiler into a parser that parses an expression and
8930 * generates an expression tree, and a code generator that
8931 * takes an expression tree (which could come from our
8932 * parser or from some other parser) and generates BPF code,
8933 * we could perhaps make the offsets parameters of routines
8934 * and, in the handler for an "AND" node, pass to subnodes
8935 * other than the VLAN node the adjusted offsets.
8937 * This would mean that "vlan" would, instead of changing the
8938 * behavior of *all* tests after it, change only the behavior
8939 * of tests ANDed with it. That would change the documented
8940 * semantics of "vlan", which might break some expressions.
8941 * However, it would mean that "(vlan and ip) or ip" would check
8942 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8943 * checking only for VLAN-encapsulated IP, so that could still
8944 * be considered worth doing; it wouldn't break expressions
8945 * that are of the form "vlan and ..." or "vlan N and ...",
8946 * which I suspect are the most common expressions involving
8947 * "vlan". "vlan or ..." doesn't necessarily do what the user
8948 * would really want, now, as all the "or ..." tests would
8949 * be done assuming a VLAN, even though the "or" could be viewed
8950 * as meaning "or, if this isn't a VLAN packet...".
8952 switch (cstate
->linktype
) {
8955 case DLT_NETANALYZER
:
8956 case DLT_NETANALYZER_TRANSPARENT
:
8957 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8958 /* Verify that this is the outer part of the packet and
8959 * not encapsulated somehow. */
8960 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8961 cstate
->off_linkhdr
.constant_part
==
8962 cstate
->off_outermostlinkhdr
.constant_part
) {
8964 * Do we need special VLAN handling?
8966 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8967 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8970 b0
= gen_vlan_no_bpf_extensions(cstate
,
8971 vlan_num
, has_vlan_tag
);
8974 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8978 case DLT_IEEE802_11
:
8979 case DLT_PRISM_HEADER
:
8980 case DLT_IEEE802_11_RADIO_AVS
:
8981 case DLT_IEEE802_11_RADIO
:
8982 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8986 bpf_error(cstate
, "no VLAN support for %s",
8987 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
8991 cstate
->vlan_stack_depth
++;
8999 * The label_num_arg dance is to avoid annoying whining by compilers that
9000 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9001 * It's not *used* after setjmp returns.
9004 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9007 volatile bpf_u_int32 label_num
= label_num_arg
;
9008 struct block
*b0
, *b1
;
9011 * Catch errors reported by us and routines below us, and return NULL
9014 if (setjmp(cstate
->top_ctx
))
9017 if (cstate
->label_stack_depth
> 0) {
9018 /* just match the bottom-of-stack bit clear */
9019 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9022 * We're not in an MPLS stack yet, so check the link-layer
9023 * type against MPLS.
9025 switch (cstate
->linktype
) {
9027 case DLT_C_HDLC
: /* fall through */
9030 case DLT_NETANALYZER
:
9031 case DLT_NETANALYZER_TRANSPARENT
:
9032 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9036 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9039 /* FIXME add other DLT_s ...
9040 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9041 * leave it for now */
9044 bpf_error(cstate
, "no MPLS support for %s",
9045 pcap_datalink_val_to_description_or_dlt(cstate
->linktype
));
9050 /* If a specific MPLS label is requested, check it */
9051 if (has_label_num
) {
9052 if (label_num
> 0xFFFFF) {
9053 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9054 label_num
, 0xFFFFF);
9056 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9057 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, label_num
,
9058 0xfffff000); /* only compare the first 20 bits */
9064 * Change the offsets to point to the type and data fields within
9065 * the MPLS packet. Just increment the offsets, so that we
9066 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9067 * capture packets with an outer label of 100000 and an inner
9070 * Increment the MPLS stack depth as well; this indicates that
9071 * we're checking MPLS-encapsulated headers, to make sure higher
9072 * level code generators don't try to match against IP-related
9073 * protocols such as Q_ARP, Q_RARP etc.
9075 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9077 cstate
->off_nl_nosnap
+= 4;
9078 cstate
->off_nl
+= 4;
9079 cstate
->label_stack_depth
++;
9084 * Support PPPOE discovery and session.
9087 gen_pppoed(compiler_state_t
*cstate
)
9090 * Catch errors reported by us and routines below us, and return NULL
9093 if (setjmp(cstate
->top_ctx
))
9096 /* check for PPPoE discovery */
9097 return gen_linktype(cstate
, ETHERTYPE_PPPOED
);
9101 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9103 struct block
*b0
, *b1
;
9106 * Catch errors reported by us and routines below us, and return NULL
9109 if (setjmp(cstate
->top_ctx
))
9113 * Test against the PPPoE session link-layer type.
9115 b0
= gen_linktype(cstate
, ETHERTYPE_PPPOES
);
9117 /* If a specific session is requested, check PPPoE session id */
9119 if (sess_num
> 0x0000ffff) {
9120 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9121 sess_num
, 0x0000ffff);
9123 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, sess_num
, 0x0000ffff);
9129 * Change the offsets to point to the type and data fields within
9130 * the PPP packet, and note that this is PPPoE rather than
9133 * XXX - this is a bit of a kludge. See the comments in
9136 * The "network-layer" protocol is PPPoE, which has a 6-byte
9137 * PPPoE header, followed by a PPP packet.
9139 * There is no HDLC encapsulation for the PPP packet (it's
9140 * encapsulated in PPPoES instead), so the link-layer type
9141 * starts at the first byte of the PPP packet. For PPPoE,
9142 * that offset is relative to the beginning of the total
9143 * link-layer payload, including any 802.2 LLC header, so
9144 * it's 6 bytes past cstate->off_nl.
9146 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9147 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9148 cstate
->off_linkpl
.reg
);
9150 cstate
->off_linktype
= cstate
->off_linkhdr
;
9151 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9154 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9159 /* Check that this is Geneve and the VNI is correct if
9160 * specified. Parameterized to handle both IPv4 and IPv6. */
9161 static struct block
*
9162 gen_geneve_check(compiler_state_t
*cstate
,
9163 struct block
*(*gen_portfn
)(compiler_state_t
*, u_int
, int, int),
9164 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9166 struct block
*b0
, *b1
;
9168 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9170 /* Check that we are operating on version 0. Otherwise, we
9171 * can't decode the rest of the fields. The version is 2 bits
9172 * in the first byte of the Geneve header. */
9173 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, 0, 0xc0);
9178 if (vni
> 0xffffff) {
9179 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9182 vni
<<= 8; /* VNI is in the upper 3 bytes */
9183 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, vni
, 0xffffff00);
9191 /* The IPv4 and IPv6 Geneve checks need to do two things:
9192 * - Verify that this actually is Geneve with the right VNI.
9193 * - Place the IP header length (plus variable link prefix if
9194 * needed) into register A to be used later to compute
9195 * the inner packet offsets. */
9196 static struct block
*
9197 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9199 struct block
*b0
, *b1
;
9200 struct slist
*s
, *s1
;
9202 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9204 /* Load the IP header length into A. */
9205 s
= gen_loadx_iphdrlen(cstate
);
9207 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9210 /* Forcibly append these statements to the true condition
9211 * of the protocol check by creating a new block that is
9212 * always true and ANDing them. */
9213 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9222 static struct block
*
9223 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9225 struct block
*b0
, *b1
;
9226 struct slist
*s
, *s1
;
9228 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9230 /* Load the IP header length. We need to account for a
9231 * variable length link prefix if there is one. */
9232 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9234 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9238 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9242 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9246 /* Forcibly append these statements to the true condition
9247 * of the protocol check by creating a new block that is
9248 * always true and ANDing them. */
9249 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9252 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9261 /* We need to store three values based on the Geneve header::
9262 * - The offset of the linktype.
9263 * - The offset of the end of the Geneve header.
9264 * - The offset of the end of the encapsulated MAC header. */
9265 static struct slist
*
9266 gen_geneve_offsets(compiler_state_t
*cstate
)
9268 struct slist
*s
, *s1
, *s_proto
;
9270 /* First we need to calculate the offset of the Geneve header
9271 * itself. This is composed of the IP header previously calculated
9272 * (include any variable link prefix) and stored in A plus the
9273 * fixed sized headers (fixed link prefix, MAC length, and UDP
9275 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9276 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9278 /* Stash this in X since we'll need it later. */
9279 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9282 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9284 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9288 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9289 cstate
->off_linktype
.is_variable
= 1;
9290 cstate
->off_linktype
.constant_part
= 0;
9292 s1
= new_stmt(cstate
, BPF_ST
);
9293 s1
->s
.k
= cstate
->off_linktype
.reg
;
9296 /* Load the Geneve option length and mask and shift to get the
9297 * number of bytes. It is stored in the first byte of the Geneve
9299 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9303 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9307 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9311 /* Add in the rest of the Geneve base header. */
9312 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9316 /* Add the Geneve header length to its offset and store. */
9317 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9321 /* Set the encapsulated type as Ethernet. Even though we may
9322 * not actually have Ethernet inside there are two reasons this
9324 * - The linktype field is always in EtherType format regardless
9325 * of whether it is in Geneve or an inner Ethernet frame.
9326 * - The only link layer that we have specific support for is
9327 * Ethernet. We will confirm that the packet actually is
9328 * Ethernet at runtime before executing these checks. */
9329 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9331 s1
= new_stmt(cstate
, BPF_ST
);
9332 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9335 /* Calculate whether we have an Ethernet header or just raw IP/
9336 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9337 * and linktype by 14 bytes so that the network header can be found
9338 * seamlessly. Otherwise, keep what we've calculated already. */
9340 /* We have a bare jmp so we can't use the optimizer. */
9341 cstate
->no_optimize
= 1;
9343 /* Load the EtherType in the Geneve header, 2 bytes in. */
9344 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9348 /* Load X with the end of the Geneve header. */
9349 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9350 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9353 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9354 * end of this check, we should have the total length in X. In
9355 * the non-Ethernet case, it's already there. */
9356 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9357 s_proto
->s
.k
= ETHERTYPE_TEB
;
9358 sappend(s
, s_proto
);
9360 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9364 /* Since this is Ethernet, use the EtherType of the payload
9365 * directly as the linktype. Overwrite what we already have. */
9366 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9370 s1
= new_stmt(cstate
, BPF_ST
);
9371 s1
->s
.k
= cstate
->off_linktype
.reg
;
9374 /* Advance two bytes further to get the end of the Ethernet
9376 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9380 /* Move the result to X. */
9381 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9384 /* Store the final result of our linkpl calculation. */
9385 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9386 cstate
->off_linkpl
.is_variable
= 1;
9387 cstate
->off_linkpl
.constant_part
= 0;
9389 s1
= new_stmt(cstate
, BPF_STX
);
9390 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9399 /* Check to see if this is a Geneve packet. */
9401 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9403 struct block
*b0
, *b1
;
9407 * Catch errors reported by us and routines below us, and return NULL
9410 if (setjmp(cstate
->top_ctx
))
9413 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9414 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9419 /* Later filters should act on the payload of the Geneve frame,
9420 * update all of the header pointers. Attach this code so that
9421 * it gets executed in the event that the Geneve filter matches. */
9422 s
= gen_geneve_offsets(cstate
);
9424 b1
= gen_true(cstate
);
9425 sappend(s
, b1
->stmts
);
9430 cstate
->is_geneve
= 1;
9435 /* Check that the encapsulated frame has a link layer header
9436 * for Ethernet filters. */
9437 static struct block
*
9438 gen_geneve_ll_check(compiler_state_t
*cstate
)
9441 struct slist
*s
, *s1
;
9443 /* The easiest way to see if there is a link layer present
9444 * is to check if the link layer header and payload are not
9447 /* Geneve always generates pure variable offsets so we can
9448 * compare only the registers. */
9449 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9450 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9452 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9453 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9456 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9464 static struct block
*
9465 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9466 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9473 if (!cstate
->is_atm
)
9474 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9475 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9477 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
,
9478 0xffffffffU
, jtype
, reverse
, jvalue
);
9482 if (!cstate
->is_atm
)
9483 bpf_error(cstate
, "'vci' supported only on raw ATM");
9484 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9486 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
,
9487 0xffffffffU
, jtype
, reverse
, jvalue
);
9491 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9492 abort(); /* XXX - this isn't on FreeBSD */
9493 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9494 0x0fU
, jtype
, reverse
, jvalue
);
9498 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9500 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9501 0xffffffffU
, jtype
, reverse
, jvalue
);
9505 if (!cstate
->is_atm
)
9506 bpf_error(cstate
, "'callref' supported only on raw ATM");
9507 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9509 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
,
9510 0xffffffffU
, jtype
, reverse
, jvalue
);
9519 static struct block
*
9520 gen_atmtype_metac(compiler_state_t
*cstate
)
9522 struct block
*b0
, *b1
;
9524 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9525 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9530 static struct block
*
9531 gen_atmtype_sc(compiler_state_t
*cstate
)
9533 struct block
*b0
, *b1
;
9535 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9536 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9541 static struct block
*
9542 gen_atmtype_llc(compiler_state_t
*cstate
)
9546 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9547 cstate
->linktype
= cstate
->prevlinktype
;
9552 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9553 bpf_u_int32 jvalue
, int jtype
, int reverse
)
9556 * Catch errors reported by us and routines below us, and return NULL
9559 if (setjmp(cstate
->top_ctx
))
9562 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9567 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9569 struct block
*b0
, *b1
;
9572 * Catch errors reported by us and routines below us, and return NULL
9575 if (setjmp(cstate
->top_ctx
))
9581 /* Get all packets in Meta signalling Circuit */
9582 if (!cstate
->is_atm
)
9583 bpf_error(cstate
, "'metac' supported only on raw ATM");
9584 b1
= gen_atmtype_metac(cstate
);
9588 /* Get all packets in Broadcast Circuit*/
9589 if (!cstate
->is_atm
)
9590 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9591 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9592 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9597 /* Get all cells in Segment OAM F4 circuit*/
9598 if (!cstate
->is_atm
)
9599 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9600 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9601 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9606 /* Get all cells in End-to-End OAM F4 Circuit*/
9607 if (!cstate
->is_atm
)
9608 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9609 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9610 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9615 /* Get all packets in connection Signalling Circuit */
9616 if (!cstate
->is_atm
)
9617 bpf_error(cstate
, "'sc' supported only on raw ATM");
9618 b1
= gen_atmtype_sc(cstate
);
9622 /* Get all packets in ILMI Circuit */
9623 if (!cstate
->is_atm
)
9624 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9625 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9626 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9631 /* Get all LANE packets */
9632 if (!cstate
->is_atm
)
9633 bpf_error(cstate
, "'lane' supported only on raw ATM");
9634 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9637 * Arrange that all subsequent tests assume LANE
9638 * rather than LLC-encapsulated packets, and set
9639 * the offsets appropriately for LANE-encapsulated
9642 * We assume LANE means Ethernet, not Token Ring.
9644 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9645 cstate
->off_payload
+ 2, /* Ethernet header */
9647 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9648 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9649 cstate
->off_nl
= 0; /* Ethernet II */
9650 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9654 /* Get all LLC-encapsulated packets */
9655 if (!cstate
->is_atm
)
9656 bpf_error(cstate
, "'llc' supported only on raw ATM");
9657 b1
= gen_atmtype_llc(cstate
);
9667 * Filtering for MTP2 messages based on li value
9668 * FISU, length is null
9669 * LSSU, length is 1 or 2
9670 * MSU, length is 3 or more
9671 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9674 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9676 struct block
*b0
, *b1
;
9679 * Catch errors reported by us and routines below us, and return NULL
9682 if (setjmp(cstate
->top_ctx
))
9688 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9689 (cstate
->linktype
!= DLT_ERF
) &&
9690 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9691 bpf_error(cstate
, "'fisu' supported only on MTP2");
9692 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9693 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9694 0x3fU
, BPF_JEQ
, 0, 0U);
9698 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9699 (cstate
->linktype
!= DLT_ERF
) &&
9700 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9701 bpf_error(cstate
, "'lssu' supported only on MTP2");
9702 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9703 0x3fU
, BPF_JGT
, 1, 2U);
9704 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9705 0x3fU
, BPF_JGT
, 0, 0U);
9710 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9711 (cstate
->linktype
!= DLT_ERF
) &&
9712 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9713 bpf_error(cstate
, "'msu' supported only on MTP2");
9714 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
,
9715 0x3fU
, BPF_JGT
, 0, 2U);
9719 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9720 (cstate
->linktype
!= DLT_ERF
) &&
9721 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9722 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9723 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9724 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9725 0xff80U
, BPF_JEQ
, 0, 0U);
9729 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9730 (cstate
->linktype
!= DLT_ERF
) &&
9731 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9732 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9733 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9734 0xff80U
, BPF_JGT
, 1, 0x0100U
);
9735 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9736 0xff80U
, BPF_JGT
, 0, 0U);
9741 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9742 (cstate
->linktype
!= DLT_ERF
) &&
9743 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9744 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9745 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
,
9746 0xff80U
, BPF_JGT
, 0, 0x0100U
);
9756 * The jvalue_arg dance is to avoid annoying whining by compilers that
9757 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9758 * It's not *used* after setjmp returns.
9761 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9762 bpf_u_int32 jvalue_arg
, int jtype
, int reverse
)
9764 volatile bpf_u_int32 jvalue
= jvalue_arg
;
9766 bpf_u_int32 val1
, val2
, val3
;
9773 * Catch errors reported by us and routines below us, and return NULL
9776 if (setjmp(cstate
->top_ctx
))
9779 newoff_sio
= cstate
->off_sio
;
9780 newoff_opc
= cstate
->off_opc
;
9781 newoff_dpc
= cstate
->off_dpc
;
9782 newoff_sls
= cstate
->off_sls
;
9783 switch (mtp3field
) {
9786 newoff_sio
+= 3; /* offset for MTP2_HSL */
9790 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9791 bpf_error(cstate
, "'sio' supported only on SS7");
9792 /* sio coded on 1 byte so max value 255 */
9794 bpf_error(cstate
, "sio value %u too big; max value = 255",
9796 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffffU
,
9797 jtype
, reverse
, jvalue
);
9805 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9806 bpf_error(cstate
, "'opc' supported only on SS7");
9807 /* opc coded on 14 bits so max value 16383 */
9809 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9811 /* the following instructions are made to convert jvalue
9812 * to the form used to write opc in an ss7 message*/
9813 val1
= jvalue
& 0x00003c00;
9815 val2
= jvalue
& 0x000003fc;
9817 val3
= jvalue
& 0x00000003;
9819 jvalue
= val1
+ val2
+ val3
;
9820 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0fU
,
9821 jtype
, reverse
, jvalue
);
9829 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9830 bpf_error(cstate
, "'dpc' supported only on SS7");
9831 /* dpc coded on 14 bits so max value 16383 */
9833 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9835 /* the following instructions are made to convert jvalue
9836 * to the forme used to write dpc in an ss7 message*/
9837 val1
= jvalue
& 0x000000ff;
9839 val2
= jvalue
& 0x00003f00;
9841 jvalue
= val1
+ val2
;
9842 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000U
,
9843 jtype
, reverse
, jvalue
);
9851 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9852 bpf_error(cstate
, "'sls' supported only on SS7");
9853 /* sls coded on 4 bits so max value 15 */
9855 bpf_error(cstate
, "sls value %u too big; max value = 15",
9857 /* the following instruction is made to convert jvalue
9858 * to the forme used to write sls in an ss7 message*/
9859 jvalue
= jvalue
<< 4;
9860 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0U
,
9861 jtype
, reverse
, jvalue
);
9870 static struct block
*
9871 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9876 * Q.2931 signalling protocol messages for handling virtual circuits
9877 * establishment and teardown
9882 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9886 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9890 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9894 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9898 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9901 case A_RELEASE_DONE
:
9902 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9912 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9914 struct block
*b0
, *b1
;
9917 * Catch errors reported by us and routines below us, and return NULL
9920 if (setjmp(cstate
->top_ctx
))
9926 if (!cstate
->is_atm
)
9927 bpf_error(cstate
, "'oam' supported only on raw ATM");
9929 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9930 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9932 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9937 if (!cstate
->is_atm
)
9938 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9940 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9941 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9943 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9949 * Get Q.2931 signalling messages for switched
9950 * virtual connection
9952 if (!cstate
->is_atm
)
9953 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9954 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9955 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9957 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9959 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9961 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9963 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9965 b0
= gen_atmtype_sc(cstate
);
9970 if (!cstate
->is_atm
)
9971 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9972 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9973 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9975 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9977 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9979 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9981 b0
= gen_atmtype_metac(cstate
);