1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <pcap-types.h>
31 #include <sys/socket.h>
34 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
55 #include "ethertype.h"
59 #include "ieee80211.h"
61 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
70 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
96 uint16_t u6_addr16
[8];
97 uint32_t u6_addr32
[4];
99 #define s6_addr in6_u.u6_addr8
100 #define s6_addr16 in6_u.u6_addr16
101 #define s6_addr32 in6_u.u6_addr32
102 #define s6_addr64 in6_u.u6_addr64
105 typedef unsigned short sa_family_t
;
107 #define __SOCKADDR_COMMON(sa_prefix) \
108 sa_family_t sa_prefix##family
110 /* Ditto, for IPv6. */
113 __SOCKADDR_COMMON (sin6_
);
114 uint16_t sin6_port
; /* Transport layer port # */
115 uint32_t sin6_flowinfo
; /* IPv6 flow information */
116 struct in6_addr sin6_addr
; /* IPv6 address */
119 #ifndef EAI_ADDRFAMILY
121 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
122 int ai_family
; /* PF_xxx */
123 int ai_socktype
; /* SOCK_xxx */
124 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
125 size_t ai_addrlen
; /* length of ai_addr */
126 char *ai_canonname
; /* canonical name for hostname */
127 struct sockaddr
*ai_addr
; /* binary address */
128 struct addrinfo
*ai_next
; /* next structure in linked list */
130 #endif /* EAI_ADDRFAMILY */
131 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
134 #include <netdb.h> /* for "struct addrinfo" */
136 #include <pcap/namedb.h>
138 #include "nametoaddr.h"
140 #define ETHERMTU 1500
142 #ifndef ETHERTYPE_TEB
143 #define ETHERTYPE_TEB 0x6558
146 #ifndef IPPROTO_HOPOPTS
147 #define IPPROTO_HOPOPTS 0
149 #ifndef IPPROTO_ROUTING
150 #define IPPROTO_ROUTING 43
152 #ifndef IPPROTO_FRAGMENT
153 #define IPPROTO_FRAGMENT 44
155 #ifndef IPPROTO_DSTOPTS
156 #define IPPROTO_DSTOPTS 60
159 #define IPPROTO_SCTP 132
162 #define GENEVE_PORT 6081
164 #ifdef HAVE_OS_PROTO_H
165 #include "os-proto.h"
168 #define JMP(c) ((c)|BPF_JMP|BPF_K)
171 * "Push" the current value of the link-layer header type and link-layer
172 * header offset onto a "stack", and set a new value. (It's not a
173 * full-blown stack; we keep only the top two items.)
175 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
177 (cs)->prevlinktype = (cs)->linktype; \
178 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
179 (cs)->linktype = (new_linktype); \
180 (cs)->off_linkhdr.is_variable = (new_is_variable); \
181 (cs)->off_linkhdr.constant_part = (new_constant_part); \
182 (cs)->off_linkhdr.reg = (new_reg); \
183 (cs)->is_geneve = 0; \
187 * Offset "not set" value.
189 #define OFFSET_NOT_SET 0xffffffffU
192 * Absolute offsets, which are offsets from the beginning of the raw
193 * packet data, are, in the general case, the sum of a variable value
194 * and a constant value; the variable value may be absent, in which
195 * case the offset is only the constant value, and the constant value
196 * may be zero, in which case the offset is only the variable value.
198 * bpf_abs_offset is a structure containing all that information:
200 * is_variable is 1 if there's a variable part.
202 * constant_part is the constant part of the value, possibly zero;
204 * if is_variable is 1, reg is the register number for a register
205 * containing the variable value if the register has been assigned,
215 * Value passed to gen_load_a() to indicate what the offset argument
216 * is relative to the beginning of.
219 OR_PACKET
, /* full packet data */
220 OR_LINKHDR
, /* link-layer header */
221 OR_PREVLINKHDR
, /* previous link-layer header */
222 OR_LLC
, /* 802.2 LLC header */
223 OR_PREVMPLSHDR
, /* previous MPLS header */
224 OR_LINKTYPE
, /* link-layer type */
225 OR_LINKPL
, /* link-layer payload */
226 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
227 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
228 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
232 * We divy out chunks of memory rather than call malloc each time so
233 * we don't have to worry about leaking memory. It's probably
234 * not a big deal if all this memory was wasted but if this ever
235 * goes into a library that would probably not be a good idea.
237 * XXX - this *is* in a library....
240 #define CHUNK0SIZE 1024
246 /* Code generator state */
248 struct _compiler_state
{
258 int outermostlinktype
;
263 /* Hack for handling VLAN and MPLS stacks. */
264 u_int label_stack_depth
;
265 u_int vlan_stack_depth
;
271 * As errors are handled by a longjmp, anything allocated must
272 * be freed in the longjmp handler, so it must be reachable
275 * One thing that's allocated is the result of pcap_nametoaddrinfo();
276 * it must be freed with freeaddrinfo(). This variable points to
277 * any addrinfo structure that would need to be freed.
282 * Another thing that's allocated is the result of pcap_ether_aton();
283 * it must be freed with free(). This variable points to any
284 * address that would need to be freed.
289 * Various code constructs need to know the layout of the packet.
290 * These values give the necessary offsets from the beginning
291 * of the packet data.
295 * Absolute offset of the beginning of the link-layer header.
297 bpf_abs_offset off_linkhdr
;
300 * If we're checking a link-layer header for a packet encapsulated
301 * in another protocol layer, this is the equivalent information
302 * for the previous layers' link-layer header from the beginning
303 * of the raw packet data.
305 bpf_abs_offset off_prevlinkhdr
;
308 * This is the equivalent information for the outermost layers'
311 bpf_abs_offset off_outermostlinkhdr
;
314 * Absolute offset of the beginning of the link-layer payload.
316 bpf_abs_offset off_linkpl
;
319 * "off_linktype" is the offset to information in the link-layer
320 * header giving the packet type. This is an absolute offset
321 * from the beginning of the packet.
323 * For Ethernet, it's the offset of the Ethernet type field; this
324 * means that it must have a value that skips VLAN tags.
326 * For link-layer types that always use 802.2 headers, it's the
327 * offset of the LLC header; this means that it must have a value
328 * that skips VLAN tags.
330 * For PPP, it's the offset of the PPP type field.
332 * For Cisco HDLC, it's the offset of the CHDLC type field.
334 * For BSD loopback, it's the offset of the AF_ value.
336 * For Linux cooked sockets, it's the offset of the type field.
338 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
339 * encapsulation, in which case, IP is assumed.
341 bpf_abs_offset off_linktype
;
344 * TRUE if the link layer includes an ATM pseudo-header.
349 * TRUE if "geneve" appeared in the filter; it causes us to
350 * generate code that checks for a Geneve header and assume
351 * that later filters apply to the encapsulated payload.
356 * TRUE if we need variable length part of VLAN offset
358 int is_vlan_vloffset
;
361 * These are offsets for the ATM pseudo-header.
368 * These are offsets for the MTP2 fields.
374 * These are offsets for the MTP3 fields.
382 * This is the offset of the first byte after the ATM pseudo_header,
383 * or -1 if there is no ATM pseudo-header.
388 * These are offsets to the beginning of the network-layer header.
389 * They are relative to the beginning of the link-layer payload
390 * (i.e., they don't include off_linkhdr.constant_part or
391 * off_linkpl.constant_part).
393 * If the link layer never uses 802.2 LLC:
395 * "off_nl" and "off_nl_nosnap" are the same.
397 * If the link layer always uses 802.2 LLC:
399 * "off_nl" is the offset if there's a SNAP header following
402 * "off_nl_nosnap" is the offset if there's no SNAP header.
404 * If the link layer is Ethernet:
406 * "off_nl" is the offset if the packet is an Ethernet II packet
407 * (we assume no 802.3+802.2+SNAP);
409 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
410 * with an 802.2 header following it.
416 * Here we handle simple allocation of the scratch registers.
417 * If too many registers are alloc'd, the allocator punts.
419 int regused
[BPF_MEMWORDS
];
425 struct chunk chunks
[NCHUNKS
];
430 * For use by routines outside this file.
434 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
439 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
445 * For use *ONLY* in routines in this file.
447 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
448 PCAP_PRINTFLIKE(2, 3);
451 static void PCAP_NORETURN
452 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
457 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
460 longjmp(cstate
->top_ctx
, 1);
464 static int init_linktype(compiler_state_t
*, pcap_t
*);
466 static void init_regs(compiler_state_t
*);
467 static int alloc_reg(compiler_state_t
*);
468 static void free_reg(compiler_state_t
*, int);
470 static void initchunks(compiler_state_t
*cstate
);
471 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
472 static void *newchunk(compiler_state_t
*cstate
, size_t);
473 static void freechunks(compiler_state_t
*cstate
);
474 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
475 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
476 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
477 static inline void syntax(compiler_state_t
*cstate
);
479 static void backpatch(struct block
*, struct block
*);
480 static void merge(struct block
*, struct block
*);
481 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
483 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
485 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
487 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
489 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
491 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
492 u_int
, bpf_int32
, bpf_u_int32
);
493 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
494 u_int
, const u_char
*);
495 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
496 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
497 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
499 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
501 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
502 static struct block
*gen_uncond(compiler_state_t
*, int);
503 static inline struct block
*gen_true(compiler_state_t
*);
504 static inline struct block
*gen_false(compiler_state_t
*);
505 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
506 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
507 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
508 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
509 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
510 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
511 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
512 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
513 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
515 static int ethertype_to_ppptype(int);
516 static struct block
*gen_linktype(compiler_state_t
*, int);
517 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
518 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
519 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
520 int, int, u_int
, u_int
);
522 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
523 struct in6_addr
*, int, int, u_int
, u_int
);
525 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
526 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
527 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
528 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
529 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
530 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
531 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
532 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
533 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
536 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
537 struct in6_addr
*, int, int, int);
540 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
541 struct addrinfo
*, int, int);
543 static struct block
*gen_ipfrag(compiler_state_t
*);
544 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
545 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
547 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
548 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
550 struct block
*gen_portop(compiler_state_t
*, int, int, int);
551 static struct block
*gen_port(compiler_state_t
*, int, int, int);
552 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
553 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
554 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
555 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
556 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
557 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
558 static int lookup_proto(compiler_state_t
*, const char *, int);
559 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
560 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
561 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
562 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
563 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
564 static struct block
*gen_len(compiler_state_t
*, int, int);
565 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
566 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
568 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
569 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
570 bpf_int32
, bpf_u_int32
, int);
571 static struct block
*gen_atmtype_llc(compiler_state_t
*);
572 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
575 initchunks(compiler_state_t
*cstate
)
579 for (i
= 0; i
< NCHUNKS
; i
++) {
580 cstate
->chunks
[i
].n_left
= 0;
581 cstate
->chunks
[i
].m
= NULL
;
583 cstate
->cur_chunk
= 0;
587 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
594 /* XXX Round up to nearest long. */
595 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
597 /* XXX Round up to structure boundary. */
601 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
602 if (n
> cp
->n_left
) {
604 k
= ++cstate
->cur_chunk
;
606 bpf_set_error(cstate
, "out of memory");
609 size
= CHUNK0SIZE
<< k
;
610 cp
->m
= (void *)malloc(size
);
612 bpf_set_error(cstate
, "out of memory");
615 memset((char *)cp
->m
, 0, size
);
618 bpf_set_error(cstate
, "out of memory");
623 return (void *)((char *)cp
->m
+ cp
->n_left
);
627 newchunk(compiler_state_t
*cstate
, size_t n
)
631 p
= newchunk_nolongjmp(cstate
, n
);
633 longjmp(cstate
->top_ctx
, 1);
640 freechunks(compiler_state_t
*cstate
)
644 for (i
= 0; i
< NCHUNKS
; ++i
)
645 if (cstate
->chunks
[i
].m
!= NULL
)
646 free(cstate
->chunks
[i
].m
);
650 * A strdup whose allocations are freed after code generation is over.
651 * This is used by the lexical analyzer, so it can't longjmp; it just
652 * returns NULL on an allocation error, and the callers must check
656 sdup(compiler_state_t
*cstate
, const char *s
)
658 size_t n
= strlen(s
) + 1;
659 char *cp
= newchunk_nolongjmp(cstate
, n
);
663 pcap_strlcpy(cp
, s
, n
);
667 static inline struct block
*
668 new_block(compiler_state_t
*cstate
, int code
)
672 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
679 static inline struct slist
*
680 new_stmt(compiler_state_t
*cstate
, int code
)
684 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
690 static struct block
*
691 gen_retblk(compiler_state_t
*cstate
, int v
)
693 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
699 static inline PCAP_NORETURN_DEF
void
700 syntax(compiler_state_t
*cstate
)
702 bpf_error(cstate
, "syntax error in filter expression");
706 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
707 const char *buf
, int optimize
, bpf_u_int32 mask
)
712 compiler_state_t cstate
;
713 const char * volatile xbuf
= buf
;
714 yyscan_t scanner
= NULL
;
715 volatile YY_BUFFER_STATE in_buffer
= NULL
;
720 * If this pcap_t hasn't been activated, it doesn't have a
721 * link-layer type, so we can't use it.
724 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
725 "not-yet-activated pcap_t passed to pcap_compile");
737 * If the device on which we're capturing need to be notified
738 * that a new filter is being compiled, do so.
740 * This allows them to save a copy of it, in case, for example,
741 * they're implementing a form of remote packet capture, and
742 * want the remote machine to filter out the packets in which
743 * it's sending the packets it's captured.
745 * XXX - the fact that we happen to be compiling a filter
746 * doesn't necessarily mean we'll be installing it as the
747 * filter for this pcap_t; we might be running it from userland
748 * on captured packets to do packet classification. We really
749 * need a better way of handling this, but this is all that
750 * the WinPcap code did.
752 if (p
->save_current_filter_op
!= NULL
)
753 (p
->save_current_filter_op
)(p
, buf
);
757 cstate
.no_optimize
= 0;
762 cstate
.ic
.root
= NULL
;
763 cstate
.ic
.cur_mark
= 0;
767 cstate
.netmask
= mask
;
769 cstate
.snaplen
= pcap_snapshot(p
);
770 if (cstate
.snaplen
== 0) {
771 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
772 "snaplen of 0 rejects all packets");
777 if (pcap_lex_init(&scanner
) != 0)
778 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
779 errno
, "can't initialize scanner");
780 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
783 * Associate the compiler state with the lexical analyzer
786 pcap_set_extra(&cstate
, scanner
);
788 if (init_linktype(&cstate
, p
) == -1) {
792 if (pcap_parse(scanner
, &cstate
) != 0) {
794 if (cstate
.ai
!= NULL
)
795 freeaddrinfo(cstate
.ai
);
797 if (cstate
.e
!= NULL
)
803 if (cstate
.ic
.root
== NULL
) {
805 * Catch errors reported by gen_retblk().
807 if (setjmp(cstate
.top_ctx
)) {
811 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
814 if (optimize
&& !cstate
.no_optimize
) {
815 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
820 if (cstate
.ic
.root
== NULL
||
821 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
822 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
823 "expression rejects all packets");
828 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
829 cstate
.ic
.root
, &len
, p
->errbuf
);
830 if (program
->bf_insns
== NULL
) {
835 program
->bf_len
= len
;
837 rc
= 0; /* We're all okay */
841 * Clean up everything for the lexical analyzer.
843 if (in_buffer
!= NULL
)
844 pcap__delete_buffer(in_buffer
, scanner
);
846 pcap_lex_destroy(scanner
);
849 * Clean up our own allocated memory.
857 * entry point for using the compiler with no pcap open
858 * pass in all the stuff that is needed explicitly instead.
861 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
862 struct bpf_program
*program
,
863 const char *buf
, int optimize
, bpf_u_int32 mask
)
868 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
871 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
877 * Clean up a "struct bpf_program" by freeing all the memory allocated
881 pcap_freecode(struct bpf_program
*program
)
884 if (program
->bf_insns
!= NULL
) {
885 free((char *)program
->bf_insns
);
886 program
->bf_insns
= NULL
;
891 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
892 * which of the jt and jf fields has been resolved and which is a pointer
893 * back to another unresolved block (or nil). At least one of the fields
894 * in each block is already resolved.
897 backpatch(struct block
*list
, struct block
*target
)
914 * Merge the lists in b0 and b1, using the 'sense' field to indicate
915 * which of jt and jf is the link.
918 merge(struct block
*b0
, struct block
*b1
)
920 register struct block
**p
= &b0
;
922 /* Find end of list. */
924 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
926 /* Concatenate the lists. */
931 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
933 struct block
*ppi_dlt_check
;
936 * Catch errors reported by us and routines below us, and return -1
939 if (setjmp(cstate
->top_ctx
))
943 * Insert before the statements of the first (root) block any
944 * statements needed to load the lengths of any variable-length
945 * headers into registers.
947 * XXX - a fancier strategy would be to insert those before the
948 * statements of all blocks that use those lengths and that
949 * have no predecessors that use them, so that we only compute
950 * the lengths if we need them. There might be even better
951 * approaches than that.
953 * However, those strategies would be more complicated, and
954 * as we don't generate code to compute a length if the
955 * program has no tests that use the length, and as most
956 * tests will probably use those lengths, we would just
957 * postpone computing the lengths so that it's not done
958 * for tests that fail early, and it's not clear that's
961 insert_compute_vloffsets(cstate
, p
->head
);
964 * For DLT_PPI captures, generate a check of the per-packet
965 * DLT value to make sure it's DLT_IEEE802_11.
967 * XXX - TurboCap cards use DLT_PPI for Ethernet.
968 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
969 * with appropriate Ethernet information and use that rather
970 * than using something such as DLT_PPI where you don't know
971 * the link-layer header type until runtime, which, in the
972 * general case, would force us to generate both Ethernet *and*
973 * 802.11 code (*and* anything else for which PPI is used)
974 * and choose between them early in the BPF program?
976 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
977 if (ppi_dlt_check
!= NULL
)
978 gen_and(ppi_dlt_check
, p
);
980 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
981 p
->sense
= !p
->sense
;
982 backpatch(p
, gen_retblk(cstate
, 0));
983 cstate
->ic
.root
= p
->head
;
988 gen_and(struct block
*b0
, struct block
*b1
)
990 backpatch(b0
, b1
->head
);
991 b0
->sense
= !b0
->sense
;
992 b1
->sense
= !b1
->sense
;
994 b1
->sense
= !b1
->sense
;
999 gen_or(struct block
*b0
, struct block
*b1
)
1001 b0
->sense
= !b0
->sense
;
1002 backpatch(b0
, b1
->head
);
1003 b0
->sense
= !b0
->sense
;
1005 b1
->head
= b0
->head
;
1009 gen_not(struct block
*b
)
1011 b
->sense
= !b
->sense
;
1014 static struct block
*
1015 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1016 u_int size
, bpf_int32 v
)
1018 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1021 static struct block
*
1022 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1023 u_int size
, bpf_int32 v
)
1025 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1028 static struct block
*
1029 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1030 u_int size
, bpf_int32 v
)
1032 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1035 static struct block
*
1036 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1037 u_int size
, bpf_int32 v
)
1039 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1042 static struct block
*
1043 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1044 u_int size
, bpf_int32 v
)
1046 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1049 static struct block
*
1050 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1051 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
1053 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1056 static struct block
*
1057 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1058 u_int size
, const u_char
*v
)
1060 register struct block
*b
, *tmp
;
1063 * XXX - the actual *instructions* do unsigned comparisons on
1064 * most platforms, and the load instructions don't do sign
1065 * extension, so gen_cmp() should really take an unsigned
1068 * As the load instructons also don't do sign-extension, we
1069 * fetch the values from the byte array as unsigned. We don't
1070 * want to use the signed versions of the extract calls.
1074 register const u_char
*p
= &v
[size
- 4];
1076 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1077 (bpf_int32
)EXTRACT_BE_U_4(p
));
1084 register const u_char
*p
= &v
[size
- 2];
1086 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1087 (bpf_int32
)EXTRACT_BE_U_2(p
));
1094 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1103 * AND the field of size "size" at offset "offset" relative to the header
1104 * specified by "offrel" with "mask", and compare it with the value "v"
1105 * with the test specified by "jtype"; if "reverse" is true, the test
1106 * should test the opposite of "jtype".
1108 static struct block
*
1109 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1110 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1113 struct slist
*s
, *s2
;
1116 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1118 if (mask
!= 0xffffffff) {
1119 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1124 b
= new_block(cstate
, JMP(jtype
));
1127 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1133 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1135 cstate
->pcap_fddipad
= p
->fddipad
;
1138 * We start out with only one link-layer header.
1140 cstate
->outermostlinktype
= pcap_datalink(p
);
1141 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1142 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1143 cstate
->off_outermostlinkhdr
.reg
= -1;
1145 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1146 cstate
->off_prevlinkhdr
.constant_part
= 0;
1147 cstate
->off_prevlinkhdr
.is_variable
= 0;
1148 cstate
->off_prevlinkhdr
.reg
= -1;
1150 cstate
->linktype
= cstate
->outermostlinktype
;
1151 cstate
->off_linkhdr
.constant_part
= 0;
1152 cstate
->off_linkhdr
.is_variable
= 0;
1153 cstate
->off_linkhdr
.reg
= -1;
1158 cstate
->off_linkpl
.constant_part
= 0;
1159 cstate
->off_linkpl
.is_variable
= 0;
1160 cstate
->off_linkpl
.reg
= -1;
1162 cstate
->off_linktype
.constant_part
= 0;
1163 cstate
->off_linktype
.is_variable
= 0;
1164 cstate
->off_linktype
.reg
= -1;
1167 * Assume it's not raw ATM with a pseudo-header, for now.
1170 cstate
->off_vpi
= OFFSET_NOT_SET
;
1171 cstate
->off_vci
= OFFSET_NOT_SET
;
1172 cstate
->off_proto
= OFFSET_NOT_SET
;
1173 cstate
->off_payload
= OFFSET_NOT_SET
;
1178 cstate
->is_geneve
= 0;
1181 * No variable length VLAN offset by default
1183 cstate
->is_vlan_vloffset
= 0;
1186 * And assume we're not doing SS7.
1188 cstate
->off_li
= OFFSET_NOT_SET
;
1189 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1190 cstate
->off_sio
= OFFSET_NOT_SET
;
1191 cstate
->off_opc
= OFFSET_NOT_SET
;
1192 cstate
->off_dpc
= OFFSET_NOT_SET
;
1193 cstate
->off_sls
= OFFSET_NOT_SET
;
1195 cstate
->label_stack_depth
= 0;
1196 cstate
->vlan_stack_depth
= 0;
1198 switch (cstate
->linktype
) {
1201 cstate
->off_linktype
.constant_part
= 2;
1202 cstate
->off_linkpl
.constant_part
= 6;
1203 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1204 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1207 case DLT_ARCNET_LINUX
:
1208 cstate
->off_linktype
.constant_part
= 4;
1209 cstate
->off_linkpl
.constant_part
= 8;
1210 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1211 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1215 cstate
->off_linktype
.constant_part
= 12;
1216 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1217 cstate
->off_nl
= 0; /* Ethernet II */
1218 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1223 * SLIP doesn't have a link level type. The 16 byte
1224 * header is hacked into our SLIP driver.
1226 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1227 cstate
->off_linkpl
.constant_part
= 16;
1229 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1232 case DLT_SLIP_BSDOS
:
1233 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1234 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1236 cstate
->off_linkpl
.constant_part
= 24;
1238 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1243 cstate
->off_linktype
.constant_part
= 0;
1244 cstate
->off_linkpl
.constant_part
= 4;
1246 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1250 cstate
->off_linktype
.constant_part
= 0;
1251 cstate
->off_linkpl
.constant_part
= 12;
1253 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1258 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1259 case DLT_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. */
1338 case DLT_IEEE802_11
:
1340 * 802.11 doesn't really have a link-level type field.
1341 * We set "off_linktype.constant_part" to the offset of
1344 * To check for Ethernet types, we assume that SSAP = SNAP
1345 * is being used and pick out the encapsulated Ethernet type.
1346 * XXX - should we generate code to check for SNAP?
1348 * We also handle variable-length radio headers here.
1349 * The Prism header is in theory variable-length, but in
1350 * practice it's always 144 bytes long. However, some
1351 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1352 * sometimes or always supply an AVS header, so we
1353 * have to check whether the radio header is a Prism
1354 * header or an AVS header, so, in practice, it's
1357 cstate
->off_linktype
.constant_part
= 24;
1358 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1359 cstate
->off_linkpl
.is_variable
= 1;
1360 cstate
->off_nl
= 8; /* 802.2+SNAP */
1361 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1366 * At the moment we treat PPI the same way that we treat
1367 * normal Radiotap encoded packets. The difference is in
1368 * the function that generates the code at the beginning
1369 * to compute the header length. Since this code generator
1370 * of PPI supports bare 802.11 encapsulation only (i.e.
1371 * the encapsulated DLT should be DLT_IEEE802_11) we
1372 * generate code to check for this too.
1374 cstate
->off_linktype
.constant_part
= 24;
1375 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1376 cstate
->off_linkpl
.is_variable
= 1;
1377 cstate
->off_linkhdr
.is_variable
= 1;
1378 cstate
->off_nl
= 8; /* 802.2+SNAP */
1379 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1382 case DLT_ATM_RFC1483
:
1383 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1385 * assume routed, non-ISO PDUs
1386 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1388 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1389 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1390 * latter would presumably be treated the way PPPoE
1391 * should be, so you can do "pppoe and udp port 2049"
1392 * or "pppoa and tcp port 80" and have it check for
1393 * PPPo{A,E} and a PPP protocol of IP and....
1395 cstate
->off_linktype
.constant_part
= 0;
1396 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1397 cstate
->off_nl
= 8; /* 802.2+SNAP */
1398 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1403 * Full Frontal ATM; you get AALn PDUs with an ATM
1407 cstate
->off_vpi
= SUNATM_VPI_POS
;
1408 cstate
->off_vci
= SUNATM_VCI_POS
;
1409 cstate
->off_proto
= PROTO_POS
;
1410 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1411 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1412 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1413 cstate
->off_nl
= 8; /* 802.2+SNAP */
1414 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1420 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1421 cstate
->off_linkpl
.constant_part
= 0;
1423 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1426 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1427 cstate
->off_linktype
.constant_part
= 14;
1428 cstate
->off_linkpl
.constant_part
= 16;
1430 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1433 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1434 cstate
->off_linktype
.constant_part
= 0;
1435 cstate
->off_linkpl
.constant_part
= 20;
1437 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1442 * LocalTalk does have a 1-byte type field in the LLAP header,
1443 * but really it just indicates whether there is a "short" or
1444 * "long" DDP packet following.
1446 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1447 cstate
->off_linkpl
.constant_part
= 0;
1449 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1452 case DLT_IP_OVER_FC
:
1454 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1455 * link-level type field. We set "off_linktype" to the
1456 * offset of the LLC header.
1458 * To check for Ethernet types, we assume that SSAP = SNAP
1459 * is being used and pick out the encapsulated Ethernet type.
1460 * XXX - should we generate code to check for SNAP? RFC
1461 * 2625 says SNAP should be used.
1463 cstate
->off_linktype
.constant_part
= 16;
1464 cstate
->off_linkpl
.constant_part
= 16;
1465 cstate
->off_nl
= 8; /* 802.2+SNAP */
1466 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1471 * XXX - we should set this to handle SNAP-encapsulated
1472 * frames (NLPID of 0x80).
1474 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1475 cstate
->off_linkpl
.constant_part
= 0;
1477 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1481 * the only BPF-interesting FRF.16 frames are non-control frames;
1482 * Frame Relay has a variable length link-layer
1483 * so lets start with offset 4 for now and increments later on (FIXME);
1486 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1487 cstate
->off_linkpl
.constant_part
= 0;
1489 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1492 case DLT_APPLE_IP_OVER_IEEE1394
:
1493 cstate
->off_linktype
.constant_part
= 16;
1494 cstate
->off_linkpl
.constant_part
= 18;
1496 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1499 case DLT_SYMANTEC_FIREWALL
:
1500 cstate
->off_linktype
.constant_part
= 6;
1501 cstate
->off_linkpl
.constant_part
= 44;
1502 cstate
->off_nl
= 0; /* Ethernet II */
1503 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1506 #ifdef HAVE_NET_PFVAR_H
1508 cstate
->off_linktype
.constant_part
= 0;
1509 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1511 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", cstate
->linktype
);
1721 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1726 * Load a value relative to the specified absolute offset.
1728 static struct slist
*
1729 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1730 u_int offset
, u_int size
)
1732 struct slist
*s
, *s2
;
1734 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1737 * If "s" is non-null, it has code to arrange that the X register
1738 * contains the variable part of the absolute offset, so we
1739 * generate a load relative to that, with an offset of
1740 * abs_offset->constant_part + offset.
1742 * Otherwise, we can do an absolute load with an offset of
1743 * abs_offset->constant_part + offset.
1747 * "s" points to a list of statements that puts the
1748 * variable part of the absolute offset into the X register.
1749 * Do an indirect load, to use the X register as an offset.
1751 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1752 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1756 * There is no variable part of the absolute offset, so
1757 * just do an absolute load.
1759 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1760 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1766 * Load a value relative to the beginning of the specified header.
1768 static struct slist
*
1769 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1772 struct slist
*s
, *s2
;
1775 * Squelch warnings from compilers that *don't* assume that
1776 * offrel always has a valid enum value and therefore don't
1777 * assume that we'll always go through one of the case arms.
1779 * If we have a default case, compilers that *do* assume that
1780 * will then complain about the default case code being
1783 * Damned if you do, damned if you don't.
1790 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1795 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1798 case OR_PREVLINKHDR
:
1799 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1803 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1806 case OR_PREVMPLSHDR
:
1807 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1811 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1814 case OR_LINKPL_NOSNAP
:
1815 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1819 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1824 * Load the X register with the length of the IPv4 header
1825 * (plus the offset of the link-layer header, if it's
1826 * preceded by a variable-length header such as a radio
1827 * header), in bytes.
1829 s
= gen_loadx_iphdrlen(cstate
);
1832 * Load the item at {offset of the link-layer payload} +
1833 * {offset, relative to the start of the link-layer
1834 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1835 * {specified offset}.
1837 * If the offset of the link-layer payload is variable,
1838 * the variable part of that offset is included in the
1839 * value in the X register, and we include the constant
1840 * part in the offset of the load.
1842 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1843 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1848 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1855 * Generate code to load into the X register the sum of the length of
1856 * the IPv4 header and the variable part of the offset of the link-layer
1859 static struct slist
*
1860 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1862 struct slist
*s
, *s2
;
1864 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1867 * The offset of the link-layer payload has a variable
1868 * part. "s" points to a list of statements that put
1869 * the variable part of that offset into the X register.
1871 * The 4*([k]&0xf) addressing mode can't be used, as we
1872 * don't have a constant offset, so we have to load the
1873 * value in question into the A register and add to it
1874 * the value from the X register.
1876 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1877 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1879 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1882 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1887 * The A register now contains the length of the IP header.
1888 * We need to add to it the variable part of the offset of
1889 * the link-layer payload, which is still in the X
1890 * register, and move the result into the X register.
1892 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1893 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1896 * The offset of the link-layer payload is a constant,
1897 * so no code was generated to load the (non-existent)
1898 * variable part of that offset.
1900 * This means we can use the 4*([k]&0xf) addressing
1901 * mode. Load the length of the IPv4 header, which
1902 * is at an offset of cstate->off_nl from the beginning of
1903 * the link-layer payload, and thus at an offset of
1904 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1905 * of the raw packet data, using that addressing mode.
1907 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1908 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1914 static struct block
*
1915 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1920 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1922 b
= new_block(cstate
, JMP(BPF_JEQ
));
1928 static inline struct block
*
1929 gen_true(compiler_state_t
*cstate
)
1931 return gen_uncond(cstate
, 1);
1934 static inline struct block
*
1935 gen_false(compiler_state_t
*cstate
)
1937 return gen_uncond(cstate
, 0);
1941 * Byte-swap a 32-bit number.
1942 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1943 * big-endian platforms.)
1945 #define SWAPLONG(y) \
1946 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1949 * Generate code to match a particular packet type.
1951 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1952 * value, if <= ETHERMTU. We use that to determine whether to
1953 * match the type/length field or to check the type/length field for
1954 * a value <= ETHERMTU to see whether it's a type field and then do
1955 * the appropriate test.
1957 static struct block
*
1958 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1960 struct block
*b0
, *b1
;
1966 case LLCSAP_NETBEUI
:
1968 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1969 * so we check the DSAP and SSAP.
1971 * LLCSAP_IP checks for IP-over-802.2, rather
1972 * than IP-over-Ethernet or IP-over-SNAP.
1974 * XXX - should we check both the DSAP and the
1975 * SSAP, like this, or should we check just the
1976 * DSAP, as we do for other types <= ETHERMTU
1977 * (i.e., other SAP values)?
1979 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1981 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1982 ((proto
<< 8) | 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
, (bpf_int32
)LLCSAP_IPX
);
2020 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)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
, (bpf_int32
)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 (proto
== ETHERTYPE_ATALK
)
2081 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2082 else /* 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
, (bpf_int32
)proto
);
2097 if (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
, (bpf_int32
)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 * "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
,
2127 static struct block
*
2128 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2131 * For DLT_NULL, the link-layer header is a 32-bit word
2132 * containing an AF_ value in *host* byte order, and for
2133 * DLT_ENC, the link-layer header begins with a 32-bit
2134 * word containing an AF_ value in host byte order.
2136 * In addition, if we're reading a saved capture file,
2137 * the host byte order in the capture may not be the
2138 * same as the host byte order on this machine.
2140 * For DLT_LOOP, the link-layer header is a 32-bit
2141 * word containing an AF_ value in *network* byte order.
2143 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2145 * The AF_ value is in host byte order, but the BPF
2146 * interpreter will convert it to network byte order.
2148 * If this is a save file, and it's from a machine
2149 * with the opposite byte order to ours, we byte-swap
2152 * Then we run it through "htonl()", and generate
2153 * code to compare against the result.
2155 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2156 proto
= SWAPLONG(proto
);
2157 proto
= htonl(proto
);
2159 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2163 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2164 * or IPv6 then we have an error.
2166 static struct block
*
2167 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2172 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2175 case ETHERTYPE_IPV6
:
2176 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2177 (bpf_int32
)IPH_AF_INET6
);
2184 return gen_false(cstate
);
2188 * Generate code to match a particular packet type.
2190 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2191 * value, if <= ETHERMTU. We use that to determine whether to
2192 * match the type field or to check the type field for the special
2193 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2195 static struct block
*
2196 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2198 struct block
*b0
, *b1
;
2204 case LLCSAP_NETBEUI
:
2206 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2207 * so we check the DSAP and SSAP.
2209 * LLCSAP_IP checks for IP-over-802.2, rather
2210 * than IP-over-Ethernet or IP-over-SNAP.
2212 * XXX - should we check both the DSAP and the
2213 * SSAP, like this, or should we check just the
2214 * DSAP, as we do for other types <= ETHERMTU
2215 * (i.e., other SAP values)?
2217 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2218 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2219 ((proto
<< 8) | proto
));
2225 * Ethernet_II frames, which are Ethernet
2226 * frames with a frame type of ETHERTYPE_IPX;
2228 * Ethernet_802.3 frames, which have a frame
2229 * type of LINUX_SLL_P_802_3;
2231 * Ethernet_802.2 frames, which are 802.3
2232 * frames with an 802.2 LLC header (i.e, have
2233 * a frame type of LINUX_SLL_P_802_2) and
2234 * with the IPX LSAP as the DSAP in the LLC
2237 * Ethernet_SNAP frames, which are 802.3
2238 * frames with an LLC header and a SNAP
2239 * header and with an OUI of 0x000000
2240 * (encapsulated Ethernet) and a protocol
2241 * ID of ETHERTYPE_IPX in the SNAP header.
2243 * First, do the checks on LINUX_SLL_P_802_2
2244 * frames; generate the check for either
2245 * Ethernet_802.2 or Ethernet_SNAP frames, and
2246 * then put a check for LINUX_SLL_P_802_2 frames
2249 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2250 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2252 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2256 * Now check for 802.3 frames and OR that with
2257 * the previous test.
2259 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2263 * Now add the check for Ethernet_II frames, and
2264 * do that before checking for the other frame
2267 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2271 case ETHERTYPE_ATALK
:
2272 case ETHERTYPE_AARP
:
2274 * EtherTalk (AppleTalk protocols on Ethernet link
2275 * layer) may use 802.2 encapsulation.
2279 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2280 * we check for the 802.2 protocol type in the
2281 * "Ethernet type" field.
2283 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2286 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2287 * SNAP packets with an organization code of
2288 * 0x080007 (Apple, for Appletalk) and a protocol
2289 * type of ETHERTYPE_ATALK (Appletalk).
2291 * 802.2-encapsulated ETHERTYPE_AARP packets are
2292 * SNAP packets with an organization code of
2293 * 0x000000 (encapsulated Ethernet) and a protocol
2294 * type of ETHERTYPE_AARP (Appletalk ARP).
2296 if (proto
== ETHERTYPE_ATALK
)
2297 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2298 else /* proto == ETHERTYPE_AARP */
2299 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2303 * Check for Ethernet encapsulation (Ethertalk
2304 * phase 1?); we just check for the Ethernet
2307 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2313 if (proto
<= ETHERMTU
) {
2315 * This is an LLC SAP value, so the frames
2316 * that match would be 802.2 frames.
2317 * Check for the 802.2 protocol type
2318 * in the "Ethernet type" field, and
2319 * then check the DSAP.
2321 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2322 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2328 * This is an Ethernet type, so compare
2329 * the length/type field with it (if
2330 * the frame is an 802.2 frame, the length
2331 * field will be <= ETHERMTU, and, as
2332 * "proto" is > ETHERMTU, this test
2333 * will fail and the frame won't match,
2334 * which is what we want).
2336 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2341 static struct slist
*
2342 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2344 struct slist
*s1
, *s2
;
2345 struct slist
*sjeq_avs_cookie
;
2346 struct slist
*sjcommon
;
2349 * This code is not compatible with the optimizer, as
2350 * we are generating jmp instructions within a normal
2351 * slist of instructions
2353 cstate
->no_optimize
= 1;
2356 * Generate code to load the length of the radio header into
2357 * the register assigned to hold that length, if one has been
2358 * assigned. (If one hasn't been assigned, no code we've
2359 * generated uses that prefix, so we don't need to generate any
2362 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2363 * or always use the AVS header rather than the Prism header.
2364 * We load a 4-byte big-endian value at the beginning of the
2365 * raw packet data, and see whether, when masked with 0xFFFFF000,
2366 * it's equal to 0x80211000. If so, that indicates that it's
2367 * an AVS header (the masked-out bits are the version number).
2368 * Otherwise, it's a Prism header.
2370 * XXX - the Prism header is also, in theory, variable-length,
2371 * but no known software generates headers that aren't 144
2374 if (cstate
->off_linkhdr
.reg
!= -1) {
2378 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2382 * AND it with 0xFFFFF000.
2384 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2385 s2
->s
.k
= 0xFFFFF000;
2389 * Compare with 0x80211000.
2391 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2392 sjeq_avs_cookie
->s
.k
= 0x80211000;
2393 sappend(s1
, sjeq_avs_cookie
);
2398 * The 4 bytes at an offset of 4 from the beginning of
2399 * the AVS header are the length of the AVS header.
2400 * That field is big-endian.
2402 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2405 sjeq_avs_cookie
->s
.jt
= s2
;
2408 * Now jump to the code to allocate a register
2409 * into which to save the header length and
2410 * store the length there. (The "jump always"
2411 * instruction needs to have the k field set;
2412 * it's added to the PC, so, as we're jumping
2413 * over a single instruction, it should be 1.)
2415 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2417 sappend(s1
, sjcommon
);
2420 * Now for the code that handles the Prism header.
2421 * Just load the length of the Prism header (144)
2422 * into the A register. Have the test for an AVS
2423 * header branch here if we don't have an AVS header.
2425 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2428 sjeq_avs_cookie
->s
.jf
= s2
;
2431 * Now allocate a register to hold that value and store
2432 * it. The code for the AVS header will jump here after
2433 * loading the length of the AVS header.
2435 s2
= new_stmt(cstate
, BPF_ST
);
2436 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2438 sjcommon
->s
.jf
= s2
;
2441 * Now move it into the X register.
2443 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2451 static struct slist
*
2452 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2454 struct slist
*s1
, *s2
;
2457 * Generate code to load the length of the AVS header into
2458 * the register assigned to hold that length, if one has been
2459 * assigned. (If one hasn't been assigned, no code we've
2460 * generated uses that prefix, so we don't need to generate any
2463 if (cstate
->off_linkhdr
.reg
!= -1) {
2465 * The 4 bytes at an offset of 4 from the beginning of
2466 * the AVS header are the length of the AVS header.
2467 * That field is big-endian.
2469 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2473 * Now allocate a register to hold that value and store
2476 s2
= new_stmt(cstate
, BPF_ST
);
2477 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2481 * Now move it into the X register.
2483 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2491 static struct slist
*
2492 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2494 struct slist
*s1
, *s2
;
2497 * Generate code to load the length of the radiotap header into
2498 * the register assigned to hold that length, if one has been
2499 * assigned. (If one hasn't been assigned, no code we've
2500 * generated uses that prefix, so we don't need to generate any
2503 if (cstate
->off_linkhdr
.reg
!= -1) {
2505 * The 2 bytes at offsets of 2 and 3 from the beginning
2506 * of the radiotap header are the length of the radiotap
2507 * header; unfortunately, it's little-endian, so we have
2508 * to load it a byte at a time and construct the value.
2512 * Load the high-order byte, at an offset of 3, shift it
2513 * left a byte, and put the result in the X register.
2515 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2517 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2520 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2524 * Load the next byte, at an offset of 2, and OR the
2525 * value from the X register into it.
2527 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2530 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2534 * Now allocate a register to hold that value and store
2537 s2
= new_stmt(cstate
, BPF_ST
);
2538 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2542 * Now move it into the X register.
2544 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2553 * At the moment we treat PPI as normal Radiotap encoded
2554 * packets. The difference is in the function that generates
2555 * the code at the beginning to compute the header length.
2556 * Since this code generator of PPI supports bare 802.11
2557 * encapsulation only (i.e. the encapsulated DLT should be
2558 * DLT_IEEE802_11) we generate code to check for this too;
2559 * that's done in finish_parse().
2561 static struct slist
*
2562 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2564 struct slist
*s1
, *s2
;
2567 * Generate code to load the length of the radiotap header
2568 * into the register assigned to hold that length, if one has
2571 if (cstate
->off_linkhdr
.reg
!= -1) {
2573 * The 2 bytes at offsets of 2 and 3 from the beginning
2574 * of the radiotap header are the length of the radiotap
2575 * header; unfortunately, it's little-endian, so we have
2576 * to load it a byte at a time and construct the value.
2580 * Load the high-order byte, at an offset of 3, shift it
2581 * left a byte, and put the result in the X register.
2583 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2585 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2588 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2592 * Load the next byte, at an offset of 2, and OR the
2593 * value from the X register into it.
2595 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2598 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2602 * Now allocate a register to hold that value and store
2605 s2
= new_stmt(cstate
, BPF_ST
);
2606 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2610 * Now move it into the X register.
2612 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2621 * Load a value relative to the beginning of the link-layer header after the 802.11
2622 * header, i.e. LLC_SNAP.
2623 * The link-layer header doesn't necessarily begin at the beginning
2624 * of the packet data; there might be a variable-length prefix containing
2625 * radio information.
2627 static struct slist
*
2628 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2631 struct slist
*sjset_data_frame_1
;
2632 struct slist
*sjset_data_frame_2
;
2633 struct slist
*sjset_qos
;
2634 struct slist
*sjset_radiotap_flags_present
;
2635 struct slist
*sjset_radiotap_ext_present
;
2636 struct slist
*sjset_radiotap_tsft_present
;
2637 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2638 struct slist
*s_roundup
;
2640 if (cstate
->off_linkpl
.reg
== -1) {
2642 * No register has been assigned to the offset of
2643 * the link-layer payload, which means nobody needs
2644 * it; don't bother computing it - just return
2645 * what we already have.
2651 * This code is not compatible with the optimizer, as
2652 * we are generating jmp instructions within a normal
2653 * slist of instructions
2655 cstate
->no_optimize
= 1;
2658 * If "s" is non-null, it has code to arrange that the X register
2659 * contains the length of the prefix preceding the link-layer
2662 * Otherwise, the length of the prefix preceding the link-layer
2663 * header is "off_outermostlinkhdr.constant_part".
2667 * There is no variable-length header preceding the
2668 * link-layer header.
2670 * Load the length of the fixed-length prefix preceding
2671 * the link-layer header (if any) into the X register,
2672 * and store it in the cstate->off_linkpl.reg register.
2673 * That length is off_outermostlinkhdr.constant_part.
2675 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2676 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2680 * The X register contains the offset of the beginning of the
2681 * link-layer header; add 24, which is the minimum length
2682 * of the MAC header for a data frame, to that, and store it
2683 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2684 * which is at the offset in the X register, with an indexed load.
2686 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2688 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2691 s2
= new_stmt(cstate
, BPF_ST
);
2692 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2695 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2700 * Check the Frame Control field to see if this is a data frame;
2701 * a data frame has the 0x08 bit (b3) in that field set and the
2702 * 0x04 bit (b2) clear.
2704 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2705 sjset_data_frame_1
->s
.k
= 0x08;
2706 sappend(s
, sjset_data_frame_1
);
2709 * If b3 is set, test b2, otherwise go to the first statement of
2710 * the rest of the program.
2712 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2713 sjset_data_frame_2
->s
.k
= 0x04;
2714 sappend(s
, sjset_data_frame_2
);
2715 sjset_data_frame_1
->s
.jf
= snext
;
2718 * If b2 is not set, this is a data frame; test the QoS bit.
2719 * Otherwise, go to the first statement of the rest of the
2722 sjset_data_frame_2
->s
.jt
= snext
;
2723 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2724 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2725 sappend(s
, sjset_qos
);
2728 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2730 * Otherwise, go to the first statement of the rest of the
2733 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2734 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2736 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2739 s2
= new_stmt(cstate
, BPF_ST
);
2740 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2744 * If we have a radiotap header, look at it to see whether
2745 * there's Atheros padding between the MAC-layer header
2748 * Note: all of the fields in the radiotap header are
2749 * little-endian, so we byte-swap all of the values
2750 * we test against, as they will be loaded as big-endian
2753 * XXX - in the general case, we would have to scan through
2754 * *all* the presence bits, if there's more than one word of
2755 * presence bits. That would require a loop, meaning that
2756 * we wouldn't be able to run the filter in the kernel.
2758 * We assume here that the Atheros adapters that insert the
2759 * annoying padding don't have multiple antennae and therefore
2760 * do not generate radiotap headers with multiple presence words.
2762 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2764 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2765 * in the first presence flag word?
2767 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2771 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2772 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2773 sappend(s
, sjset_radiotap_flags_present
);
2776 * If not, skip all of this.
2778 sjset_radiotap_flags_present
->s
.jf
= snext
;
2781 * Otherwise, is the "extension" bit set in that word?
2783 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2784 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2785 sappend(s
, sjset_radiotap_ext_present
);
2786 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2789 * If so, skip all of this.
2791 sjset_radiotap_ext_present
->s
.jt
= snext
;
2794 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2796 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2797 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2798 sappend(s
, sjset_radiotap_tsft_present
);
2799 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2802 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2803 * at an offset of 16 from the beginning of the raw packet
2804 * data (8 bytes for the radiotap header and 8 bytes for
2807 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2810 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2813 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2815 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2816 sjset_tsft_datapad
->s
.k
= 0x20;
2817 sappend(s
, sjset_tsft_datapad
);
2820 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2821 * at an offset of 8 from the beginning of the raw packet
2822 * data (8 bytes for the radiotap header).
2824 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2827 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2830 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2832 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2833 sjset_notsft_datapad
->s
.k
= 0x20;
2834 sappend(s
, sjset_notsft_datapad
);
2837 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2838 * set, round the length of the 802.11 header to
2839 * a multiple of 4. Do that by adding 3 and then
2840 * dividing by and multiplying by 4, which we do by
2843 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2844 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2845 sappend(s
, s_roundup
);
2846 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2849 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2852 s2
= new_stmt(cstate
, BPF_ST
);
2853 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2856 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2857 sjset_tsft_datapad
->s
.jf
= snext
;
2858 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2859 sjset_notsft_datapad
->s
.jf
= snext
;
2861 sjset_qos
->s
.jf
= snext
;
2867 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2871 /* There is an implicit dependency between the link
2872 * payload and link header since the payload computation
2873 * includes the variable part of the header. Therefore,
2874 * if nobody else has allocated a register for the link
2875 * header and we need it, do it now. */
2876 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2877 cstate
->off_linkhdr
.reg
== -1)
2878 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2881 * For link-layer types that have a variable-length header
2882 * preceding the link-layer header, generate code to load
2883 * the offset of the link-layer header into the register
2884 * assigned to that offset, if any.
2886 * XXX - this, and the next switch statement, won't handle
2887 * encapsulation of 802.11 or 802.11+radio information in
2888 * some other protocol stack. That's significantly more
2891 switch (cstate
->outermostlinktype
) {
2893 case DLT_PRISM_HEADER
:
2894 s
= gen_load_prism_llprefixlen(cstate
);
2897 case DLT_IEEE802_11_RADIO_AVS
:
2898 s
= gen_load_avs_llprefixlen(cstate
);
2901 case DLT_IEEE802_11_RADIO
:
2902 s
= gen_load_radiotap_llprefixlen(cstate
);
2906 s
= gen_load_ppi_llprefixlen(cstate
);
2915 * For link-layer types that have a variable-length link-layer
2916 * header, generate code to load the offset of the link-layer
2917 * payload into the register assigned to that offset, if any.
2919 switch (cstate
->outermostlinktype
) {
2921 case DLT_IEEE802_11
:
2922 case DLT_PRISM_HEADER
:
2923 case DLT_IEEE802_11_RADIO_AVS
:
2924 case DLT_IEEE802_11_RADIO
:
2926 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2931 * If there there is no initialization yet and we need variable
2932 * length offsets for VLAN, initialize them to zero
2934 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2937 if (cstate
->off_linkpl
.reg
== -1)
2938 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2939 if (cstate
->off_linktype
.reg
== -1)
2940 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2942 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2944 s2
= new_stmt(cstate
, BPF_ST
);
2945 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2947 s2
= new_stmt(cstate
, BPF_ST
);
2948 s2
->s
.k
= cstate
->off_linktype
.reg
;
2953 * If we have any offset-loading code, append all the
2954 * existing statements in the block to those statements,
2955 * and make the resulting list the list of statements
2959 sappend(s
, b
->stmts
);
2964 static struct block
*
2965 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2967 struct slist
*s_load_dlt
;
2970 if (cstate
->linktype
== DLT_PPI
)
2972 /* Create the statements that check for the DLT
2974 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2975 s_load_dlt
->s
.k
= 4;
2977 b
= new_block(cstate
, JMP(BPF_JEQ
));
2979 b
->stmts
= s_load_dlt
;
2980 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2991 * Take an absolute offset, and:
2993 * if it has no variable part, return NULL;
2995 * if it has a variable part, generate code to load the register
2996 * containing that variable part into the X register, returning
2997 * a pointer to that code - if no register for that offset has
2998 * been allocated, allocate it first.
3000 * (The code to set that register will be generated later, but will
3001 * be placed earlier in the code sequence.)
3003 static struct slist
*
3004 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
3008 if (off
->is_variable
) {
3009 if (off
->reg
== -1) {
3011 * We haven't yet assigned a register for the
3012 * variable part of the offset of the link-layer
3013 * header; allocate one.
3015 off
->reg
= alloc_reg(cstate
);
3019 * Load the register containing the variable part of the
3020 * offset of the link-layer header into the X register.
3022 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3027 * That offset isn't variable, there's no variable part,
3028 * so we don't need to generate any code.
3035 * Map an Ethernet type to the equivalent PPP type.
3038 ethertype_to_ppptype(int proto
)
3046 case ETHERTYPE_IPV6
:
3054 case ETHERTYPE_ATALK
:
3068 * I'm assuming the "Bridging PDU"s that go
3069 * over PPP are Spanning Tree Protocol
3083 * Generate any tests that, for encapsulation of a link-layer packet
3084 * inside another protocol stack, need to be done to check for those
3085 * link-layer packets (and that haven't already been done by a check
3086 * for that encapsulation).
3088 static struct block
*
3089 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3093 if (cstate
->is_geneve
)
3094 return gen_geneve_ll_check(cstate
);
3096 switch (cstate
->prevlinktype
) {
3100 * This is LANE-encapsulated Ethernet; check that the LANE
3101 * packet doesn't begin with an LE Control marker, i.e.
3102 * that it's data, not a control message.
3104 * (We've already generated a test for LANE.)
3106 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3112 * No such tests are necessary.
3120 * The three different values we should check for when checking for an
3121 * IPv6 packet with DLT_NULL.
3123 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3124 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3125 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3128 * Generate code to match a particular packet type by matching the
3129 * link-layer type field or fields in the 802.2 LLC header.
3131 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3132 * value, if <= ETHERMTU.
3134 static struct block
*
3135 gen_linktype(compiler_state_t
*cstate
, int proto
)
3137 struct block
*b0
, *b1
, *b2
;
3138 const char *description
;
3140 /* are we checking MPLS-encapsulated packets? */
3141 if (cstate
->label_stack_depth
> 0) {
3145 /* FIXME add other L3 proto IDs */
3146 return gen_mpls_linktype(cstate
, Q_IP
);
3148 case ETHERTYPE_IPV6
:
3150 /* FIXME add other L3 proto IDs */
3151 return gen_mpls_linktype(cstate
, Q_IPV6
);
3154 bpf_error(cstate
, "unsupported protocol over mpls");
3159 switch (cstate
->linktype
) {
3162 case DLT_NETANALYZER
:
3163 case DLT_NETANALYZER_TRANSPARENT
:
3164 /* Geneve has an EtherType regardless of whether there is an
3166 if (!cstate
->is_geneve
)
3167 b0
= gen_prevlinkhdr_check(cstate
);
3171 b1
= gen_ether_linktype(cstate
, proto
);
3181 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3185 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3189 case DLT_IEEE802_11
:
3190 case DLT_PRISM_HEADER
:
3191 case DLT_IEEE802_11_RADIO_AVS
:
3192 case DLT_IEEE802_11_RADIO
:
3195 * Check that we have a data frame.
3197 b0
= gen_check_802_11_data_frame(cstate
);
3200 * Now check for the specified link-layer type.
3202 b1
= gen_llc_linktype(cstate
, proto
);
3209 * XXX - check for LLC frames.
3211 return gen_llc_linktype(cstate
, proto
);
3216 * XXX - check for LLC PDUs, as per IEEE 802.5.
3218 return gen_llc_linktype(cstate
, proto
);
3221 case DLT_ATM_RFC1483
:
3223 case DLT_IP_OVER_FC
:
3224 return gen_llc_linktype(cstate
, proto
);
3229 * Check for an LLC-encapsulated version of this protocol;
3230 * if we were checking for LANE, linktype would no longer
3233 * Check for LLC encapsulation and then check the protocol.
3235 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3236 b1
= gen_llc_linktype(cstate
, proto
);
3242 return gen_linux_sll_linktype(cstate
, proto
);
3246 case DLT_SLIP_BSDOS
:
3249 * These types don't provide any type field; packets
3250 * are always IPv4 or IPv6.
3252 * XXX - for IPv4, check for a version number of 4, and,
3253 * for IPv6, check for a version number of 6?
3258 /* Check for a version number of 4. */
3259 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3261 case ETHERTYPE_IPV6
:
3262 /* Check for a version number of 6. */
3263 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3266 return gen_false(cstate
); /* always false */
3272 * Raw IPv4, so no type field.
3274 if (proto
== ETHERTYPE_IP
)
3275 return gen_true(cstate
); /* always true */
3277 /* Checking for something other than IPv4; always false */
3278 return gen_false(cstate
);
3283 * Raw IPv6, so no type field.
3285 if (proto
== ETHERTYPE_IPV6
)
3286 return gen_true(cstate
); /* always true */
3288 /* Checking for something other than IPv6; always false */
3289 return gen_false(cstate
);
3294 case DLT_PPP_SERIAL
:
3297 * We use Ethernet protocol types inside libpcap;
3298 * map them to the corresponding PPP protocol types.
3300 proto
= ethertype_to_ppptype(proto
);
3301 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3306 * We use Ethernet protocol types inside libpcap;
3307 * map them to the corresponding PPP protocol types.
3313 * Also check for Van Jacobson-compressed IP.
3314 * XXX - do this for other forms of PPP?
3316 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3317 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3319 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3324 proto
= ethertype_to_ppptype(proto
);
3325 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3336 return (gen_loopback_linktype(cstate
, AF_INET
));
3338 case ETHERTYPE_IPV6
:
3340 * AF_ values may, unfortunately, be platform-
3341 * dependent; AF_INET isn't, because everybody
3342 * used 4.2BSD's value, but AF_INET6 is, because
3343 * 4.2BSD didn't have a value for it (given that
3344 * IPv6 didn't exist back in the early 1980's),
3345 * and they all picked their own values.
3347 * This means that, if we're reading from a
3348 * savefile, we need to check for all the
3351 * If we're doing a live capture, we only need
3352 * to check for this platform's value; however,
3353 * Npcap uses 24, which isn't Windows's AF_INET6
3354 * value. (Given the multiple different values,
3355 * programs that read pcap files shouldn't be
3356 * checking for their platform's AF_INET6 value
3357 * anyway, they should check for all of the
3358 * possible values. and they might as well do
3359 * that even for live captures.)
3361 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3363 * Savefile - check for all three
3364 * possible IPv6 values.
3366 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3367 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3369 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3374 * Live capture, so we only need to
3375 * check for the value used on this
3380 * Npcap doesn't use Windows's AF_INET6,
3381 * as that collides with AF_IPX on
3382 * some BSDs (both have the value 23).
3383 * Instead, it uses 24.
3385 return (gen_loopback_linktype(cstate
, 24));
3388 return (gen_loopback_linktype(cstate
, AF_INET6
));
3389 #else /* AF_INET6 */
3391 * I guess this platform doesn't support
3392 * IPv6, so we just reject all packets.
3394 return gen_false(cstate
);
3395 #endif /* AF_INET6 */
3401 * Not a type on which we support filtering.
3402 * XXX - support those that have AF_ values
3403 * #defined on this platform, at least?
3405 return gen_false(cstate
);
3408 #ifdef HAVE_NET_PFVAR_H
3411 * af field is host byte order in contrast to the rest of
3414 if (proto
== ETHERTYPE_IP
)
3415 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3416 BPF_B
, (bpf_int32
)AF_INET
));
3417 else if (proto
== ETHERTYPE_IPV6
)
3418 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3419 BPF_B
, (bpf_int32
)AF_INET6
));
3421 return gen_false(cstate
);
3423 #endif /* HAVE_NET_PFVAR_H */
3426 case DLT_ARCNET_LINUX
:
3428 * XXX should we check for first fragment if the protocol
3434 return gen_false(cstate
);
3436 case ETHERTYPE_IPV6
:
3437 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3438 (bpf_int32
)ARCTYPE_INET6
));
3441 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3442 (bpf_int32
)ARCTYPE_IP
);
3443 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3444 (bpf_int32
)ARCTYPE_IP_OLD
);
3449 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3450 (bpf_int32
)ARCTYPE_ARP
);
3451 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3452 (bpf_int32
)ARCTYPE_ARP_OLD
);
3456 case ETHERTYPE_REVARP
:
3457 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3458 (bpf_int32
)ARCTYPE_REVARP
));
3460 case ETHERTYPE_ATALK
:
3461 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3462 (bpf_int32
)ARCTYPE_ATALK
));
3468 case ETHERTYPE_ATALK
:
3469 return gen_true(cstate
);
3471 return gen_false(cstate
);
3477 * XXX - assumes a 2-byte Frame Relay header with
3478 * DLCI and flags. What if the address is longer?
3484 * Check for the special NLPID for IP.
3486 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3488 case ETHERTYPE_IPV6
:
3490 * Check for the special NLPID for IPv6.
3492 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3496 * Check for several OSI protocols.
3498 * Frame Relay packets typically have an OSI
3499 * NLPID at the beginning; we check for each
3502 * What we check for is the NLPID and a frame
3503 * control field of UI, i.e. 0x03 followed
3506 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3507 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3508 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3514 return gen_false(cstate
);
3519 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3521 case DLT_JUNIPER_MFR
:
3522 case DLT_JUNIPER_MLFR
:
3523 case DLT_JUNIPER_MLPPP
:
3524 case DLT_JUNIPER_ATM1
:
3525 case DLT_JUNIPER_ATM2
:
3526 case DLT_JUNIPER_PPPOE
:
3527 case DLT_JUNIPER_PPPOE_ATM
:
3528 case DLT_JUNIPER_GGSN
:
3529 case DLT_JUNIPER_ES
:
3530 case DLT_JUNIPER_MONITOR
:
3531 case DLT_JUNIPER_SERVICES
:
3532 case DLT_JUNIPER_ETHER
:
3533 case DLT_JUNIPER_PPP
:
3534 case DLT_JUNIPER_FRELAY
:
3535 case DLT_JUNIPER_CHDLC
:
3536 case DLT_JUNIPER_VP
:
3537 case DLT_JUNIPER_ST
:
3538 case DLT_JUNIPER_ISM
:
3539 case DLT_JUNIPER_VS
:
3540 case DLT_JUNIPER_SRX_E2E
:
3541 case DLT_JUNIPER_FIBRECHANNEL
:
3542 case DLT_JUNIPER_ATM_CEMIC
:
3544 /* just lets verify the magic number for now -
3545 * on ATM we may have up to 6 different encapsulations on the wire
3546 * and need a lot of heuristics to figure out that the payload
3549 * FIXME encapsulation specific BPF_ filters
3551 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3553 case DLT_BACNET_MS_TP
:
3554 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3557 return gen_ipnet_linktype(cstate
, proto
);
3559 case DLT_LINUX_IRDA
:
3560 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3563 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3566 case DLT_MTP2_WITH_PHDR
:
3567 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3570 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3573 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3575 case DLT_LINUX_LAPD
:
3576 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3578 case DLT_USB_FREEBSD
:
3580 case DLT_USB_LINUX_MMAPPED
:
3582 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3584 case DLT_BLUETOOTH_HCI_H4
:
3585 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3586 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3589 case DLT_CAN_SOCKETCAN
:
3590 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3592 case DLT_IEEE802_15_4
:
3593 case DLT_IEEE802_15_4_LINUX
:
3594 case DLT_IEEE802_15_4_NONASK_PHY
:
3595 case DLT_IEEE802_15_4_NOFCS
:
3596 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3598 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3599 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3602 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3605 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3608 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3611 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3614 /* Using the fixed-size NFLOG header it is possible to tell only
3615 * the address family of the packet, other meaningful data is
3616 * either missing or behind TLVs.
3618 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3622 * Does this link-layer header type have a field
3623 * indicating the type of the next protocol? If
3624 * so, off_linktype.constant_part will be the offset of that
3625 * field in the packet; if not, it will be OFFSET_NOT_SET.
3627 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3629 * Yes; assume it's an Ethernet type. (If
3630 * it's not, it needs to be handled specially
3633 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3637 * No; report an error.
3639 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3640 if (description
!= NULL
) {
3641 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3644 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3653 * Check for an LLC SNAP packet with a given organization code and
3654 * protocol type; we check the entire contents of the 802.2 LLC and
3655 * snap headers, checking for DSAP and SSAP of SNAP and a control
3656 * field of 0x03 in the LLC header, and for the specified organization
3657 * code and protocol type in the SNAP header.
3659 static struct block
*
3660 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3662 u_char snapblock
[8];
3664 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3665 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3666 snapblock
[2] = 0x03; /* control = UI */
3667 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3668 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3669 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3670 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3671 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3672 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3676 * Generate code to match frames with an LLC header.
3678 static struct block
*
3679 gen_llc_internal(compiler_state_t
*cstate
)
3681 struct block
*b0
, *b1
;
3683 switch (cstate
->linktype
) {
3687 * We check for an Ethernet type field less than
3688 * 1500, which means it's an 802.3 length field.
3690 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3694 * Now check for the purported DSAP and SSAP not being
3695 * 0xFF, to rule out NetWare-over-802.3.
3697 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3704 * We check for LLC traffic.
3706 b0
= gen_atmtype_llc(cstate
);
3709 case DLT_IEEE802
: /* Token Ring */
3711 * XXX - check for LLC frames.
3713 return gen_true(cstate
);
3717 * XXX - check for LLC frames.
3719 return gen_true(cstate
);
3721 case DLT_ATM_RFC1483
:
3723 * For LLC encapsulation, these are defined to have an
3726 * For VC encapsulation, they don't, but there's no
3727 * way to check for that; the protocol used on the VC
3728 * is negotiated out of band.
3730 return gen_true(cstate
);
3732 case DLT_IEEE802_11
:
3733 case DLT_PRISM_HEADER
:
3734 case DLT_IEEE802_11_RADIO
:
3735 case DLT_IEEE802_11_RADIO_AVS
:
3738 * Check that we have a data frame.
3740 b0
= gen_check_802_11_data_frame(cstate
);
3744 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3750 gen_llc(compiler_state_t
*cstate
)
3753 * Catch errors reported by us and routines below us, and return NULL
3756 if (setjmp(cstate
->top_ctx
))
3759 return gen_llc_internal(cstate
);
3763 gen_llc_i(compiler_state_t
*cstate
)
3765 struct block
*b0
, *b1
;
3769 * Catch errors reported by us and routines below us, and return NULL
3772 if (setjmp(cstate
->top_ctx
))
3776 * Check whether this is an LLC frame.
3778 b0
= gen_llc_internal(cstate
);
3781 * Load the control byte and test the low-order bit; it must
3782 * be clear for I frames.
3784 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3785 b1
= new_block(cstate
, JMP(BPF_JSET
));
3794 gen_llc_s(compiler_state_t
*cstate
)
3796 struct block
*b0
, *b1
;
3799 * Catch errors reported by us and routines below us, and return NULL
3802 if (setjmp(cstate
->top_ctx
))
3806 * Check whether this is an LLC frame.
3808 b0
= gen_llc_internal(cstate
);
3811 * Now compare the low-order 2 bit of the control byte against
3812 * the appropriate value for S frames.
3814 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3820 gen_llc_u(compiler_state_t
*cstate
)
3822 struct block
*b0
, *b1
;
3825 * Catch errors reported by us and routines below us, and return NULL
3828 if (setjmp(cstate
->top_ctx
))
3832 * Check whether this is an LLC frame.
3834 b0
= gen_llc_internal(cstate
);
3837 * Now compare the low-order 2 bit of the control byte against
3838 * the appropriate value for U frames.
3840 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3846 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3848 struct block
*b0
, *b1
;
3851 * Catch errors reported by us and routines below us, and return NULL
3854 if (setjmp(cstate
->top_ctx
))
3858 * Check whether this is an LLC frame.
3860 b0
= gen_llc_internal(cstate
);
3863 * Now check for an S frame with the appropriate type.
3865 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3871 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3873 struct block
*b0
, *b1
;
3876 * Catch errors reported by us and routines below us, and return NULL
3879 if (setjmp(cstate
->top_ctx
))
3883 * Check whether this is an LLC frame.
3885 b0
= gen_llc_internal(cstate
);
3888 * Now check for a U frame with the appropriate type.
3890 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3896 * Generate code to match a particular packet type, for link-layer types
3897 * using 802.2 LLC headers.
3899 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3900 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3902 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3903 * value, if <= ETHERMTU. We use that to determine whether to
3904 * match the DSAP or both DSAP and LSAP or to check the OUI and
3905 * protocol ID in a SNAP header.
3907 static struct block
*
3908 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3911 * XXX - handle token-ring variable-length header.
3917 case LLCSAP_NETBEUI
:
3919 * XXX - should we check both the DSAP and the
3920 * SSAP, like this, or should we check just the
3921 * DSAP, as we do for other SAP values?
3923 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3924 ((proto
<< 8) | proto
));
3928 * XXX - are there ever SNAP frames for IPX on
3929 * non-Ethernet 802.x networks?
3931 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3932 (bpf_int32
)LLCSAP_IPX
);
3934 case ETHERTYPE_ATALK
:
3936 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3937 * SNAP packets with an organization code of
3938 * 0x080007 (Apple, for Appletalk) and a protocol
3939 * type of ETHERTYPE_ATALK (Appletalk).
3941 * XXX - check for an organization code of
3942 * encapsulated Ethernet as well?
3944 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3948 * XXX - we don't have to check for IPX 802.3
3949 * here, but should we check for the IPX Ethertype?
3951 if (proto
<= ETHERMTU
) {
3953 * This is an LLC SAP value, so check
3956 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3959 * This is an Ethernet type; we assume that it's
3960 * unlikely that it'll appear in the right place
3961 * at random, and therefore check only the
3962 * location that would hold the Ethernet type
3963 * in a SNAP frame with an organization code of
3964 * 0x000000 (encapsulated Ethernet).
3966 * XXX - if we were to check for the SNAP DSAP and
3967 * LSAP, as per XXX, and were also to check for an
3968 * organization code of 0x000000 (encapsulated
3969 * Ethernet), we'd do
3971 * return gen_snap(cstate, 0x000000, proto);
3973 * here; for now, we don't, as per the above.
3974 * I don't know whether it's worth the extra CPU
3975 * time to do the right check or not.
3977 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3982 static struct block
*
3983 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3984 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3986 struct block
*b0
, *b1
;
4000 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4001 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4007 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4008 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4013 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4017 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4021 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4025 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4029 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4033 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4040 b0
= gen_linktype(cstate
, proto
);
4041 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
4047 static struct block
*
4048 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4049 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
4051 struct block
*b0
, *b1
;
4066 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4067 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4073 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4074 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4079 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4083 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4087 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4091 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4095 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4099 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4106 /* this order is important */
4107 a
= (uint32_t *)addr
;
4108 m
= (uint32_t *)mask
;
4109 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4110 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4112 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4114 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4116 b0
= gen_linktype(cstate
, proto
);
4122 static struct block
*
4123 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4125 register struct block
*b0
, *b1
;
4129 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4132 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4135 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4136 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4142 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4143 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4148 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4152 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4156 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4160 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4164 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4168 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4176 * Like gen_ehostop, but for DLT_FDDI
4178 static struct block
*
4179 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4181 struct block
*b0
, *b1
;
4185 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4188 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4191 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4192 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4198 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4199 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4204 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4208 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4212 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4216 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4220 bpf_error(cstate
, "'ra' is only supported on 802.11");
4224 bpf_error(cstate
, "'ta' is only supported on 802.11");
4232 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4234 static struct block
*
4235 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4237 register struct block
*b0
, *b1
;
4241 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4244 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4247 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4248 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4254 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4255 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4260 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4264 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4268 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4272 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4276 bpf_error(cstate
, "'ra' is only supported on 802.11");
4280 bpf_error(cstate
, "'ta' is only supported on 802.11");
4288 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4289 * various 802.11 + radio headers.
4291 static struct block
*
4292 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4294 register struct block
*b0
, *b1
, *b2
;
4295 register struct slist
*s
;
4297 #ifdef ENABLE_WLAN_FILTERING_PATCH
4300 * We need to disable the optimizer because the optimizer is buggy
4301 * and wipes out some LD instructions generated by the below
4302 * code to validate the Frame Control bits
4304 cstate
->no_optimize
= 1;
4305 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4312 * For control frames, there is no SA.
4314 * For management frames, SA is at an
4315 * offset of 10 from the beginning of
4318 * For data frames, SA is at an offset
4319 * of 10 from the beginning of the packet
4320 * if From DS is clear, at an offset of
4321 * 16 from the beginning of the packet
4322 * if From DS is set and To DS is clear,
4323 * and an offset of 24 from the beginning
4324 * of the packet if From DS is set and To DS
4329 * Generate the tests to be done for data frames
4332 * First, check for To DS set, i.e. check "link[1] & 0x01".
4334 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4335 b1
= new_block(cstate
, JMP(BPF_JSET
));
4336 b1
->s
.k
= 0x01; /* To DS */
4340 * If To DS is set, the SA is at 24.
4342 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4346 * Now, check for To DS not set, i.e. check
4347 * "!(link[1] & 0x01)".
4349 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4350 b2
= new_block(cstate
, JMP(BPF_JSET
));
4351 b2
->s
.k
= 0x01; /* To DS */
4356 * If To DS is not set, the SA is at 16.
4358 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4362 * Now OR together the last two checks. That gives
4363 * the complete set of checks for data frames with
4369 * Now check for From DS being set, and AND that with
4370 * the ORed-together checks.
4372 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4373 b1
= new_block(cstate
, JMP(BPF_JSET
));
4374 b1
->s
.k
= 0x02; /* From DS */
4379 * Now check for data frames with From DS not set.
4381 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4382 b2
= new_block(cstate
, JMP(BPF_JSET
));
4383 b2
->s
.k
= 0x02; /* From DS */
4388 * If From DS isn't set, the SA is at 10.
4390 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4394 * Now OR together the checks for data frames with
4395 * From DS not set and for data frames with From DS
4396 * set; that gives the checks done for data frames.
4401 * Now check for a data frame.
4402 * I.e, check "link[0] & 0x08".
4404 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4405 b1
= new_block(cstate
, JMP(BPF_JSET
));
4410 * AND that with the checks done for data frames.
4415 * If the high-order bit of the type value is 0, this
4416 * is a management frame.
4417 * I.e, check "!(link[0] & 0x08)".
4419 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4420 b2
= new_block(cstate
, JMP(BPF_JSET
));
4426 * For management frames, the SA is at 10.
4428 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4432 * OR that with the checks done for data frames.
4433 * That gives the checks done for management and
4439 * If the low-order bit of the type value is 1,
4440 * this is either a control frame or a frame
4441 * with a reserved type, and thus not a
4444 * I.e., check "!(link[0] & 0x04)".
4446 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4447 b1
= new_block(cstate
, JMP(BPF_JSET
));
4453 * AND that with the checks for data and management
4463 * For control frames, there is no DA.
4465 * For management frames, DA is at an
4466 * offset of 4 from the beginning of
4469 * For data frames, DA is at an offset
4470 * of 4 from the beginning of the packet
4471 * if To DS is clear and at an offset of
4472 * 16 from the beginning of the packet
4477 * Generate the tests to be done for data frames.
4479 * First, check for To DS set, i.e. "link[1] & 0x01".
4481 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4482 b1
= new_block(cstate
, JMP(BPF_JSET
));
4483 b1
->s
.k
= 0x01; /* To DS */
4487 * If To DS is set, the DA is at 16.
4489 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4493 * Now, check for To DS not set, i.e. check
4494 * "!(link[1] & 0x01)".
4496 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4497 b2
= new_block(cstate
, JMP(BPF_JSET
));
4498 b2
->s
.k
= 0x01; /* To DS */
4503 * If To DS is not set, the DA is at 4.
4505 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4509 * Now OR together the last two checks. That gives
4510 * the complete set of checks for data frames.
4515 * Now check for a data frame.
4516 * I.e, check "link[0] & 0x08".
4518 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4519 b1
= new_block(cstate
, JMP(BPF_JSET
));
4524 * AND that with the checks done for data frames.
4529 * If the high-order bit of the type value is 0, this
4530 * is a management frame.
4531 * I.e, check "!(link[0] & 0x08)".
4533 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4534 b2
= new_block(cstate
, JMP(BPF_JSET
));
4540 * For management frames, the DA is at 4.
4542 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4546 * OR that with the checks done for data frames.
4547 * That gives the checks done for management and
4553 * If the low-order bit of the type value is 1,
4554 * this is either a control frame or a frame
4555 * with a reserved type, and thus not a
4558 * I.e., check "!(link[0] & 0x04)".
4560 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4561 b1
= new_block(cstate
, JMP(BPF_JSET
));
4567 * AND that with the checks for data and management
4574 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4575 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4581 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4582 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4587 * XXX - add BSSID keyword?
4590 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4594 * Not present in CTS or ACK control frames.
4596 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4597 IEEE80211_FC0_TYPE_MASK
);
4599 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4600 IEEE80211_FC0_SUBTYPE_MASK
);
4602 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4603 IEEE80211_FC0_SUBTYPE_MASK
);
4607 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4613 * Not present in control frames.
4615 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4616 IEEE80211_FC0_TYPE_MASK
);
4618 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4624 * Present only if the direction mask has both "From DS"
4625 * and "To DS" set. Neither control frames nor management
4626 * frames should have both of those set, so we don't
4627 * check the frame type.
4629 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4630 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4631 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4637 * Not present in management frames; addr1 in other
4642 * If the high-order bit of the type value is 0, this
4643 * is a management frame.
4644 * I.e, check "(link[0] & 0x08)".
4646 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4647 b1
= new_block(cstate
, JMP(BPF_JSET
));
4654 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4657 * AND that with the check of addr1.
4664 * Not present in management frames; addr2, if present,
4669 * Not present in CTS or ACK control frames.
4671 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4672 IEEE80211_FC0_TYPE_MASK
);
4674 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4675 IEEE80211_FC0_SUBTYPE_MASK
);
4677 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4678 IEEE80211_FC0_SUBTYPE_MASK
);
4684 * If the high-order bit of the type value is 0, this
4685 * is a management frame.
4686 * I.e, check "(link[0] & 0x08)".
4688 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4689 b1
= new_block(cstate
, JMP(BPF_JSET
));
4694 * AND that with the check for frames other than
4695 * CTS and ACK frames.
4702 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4711 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4712 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4713 * as the RFC states.)
4715 static struct block
*
4716 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4718 register struct block
*b0
, *b1
;
4722 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4725 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4728 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4729 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4735 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4736 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4741 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4745 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4749 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4753 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4757 bpf_error(cstate
, "'ra' is only supported on 802.11");
4761 bpf_error(cstate
, "'ta' is only supported on 802.11");
4769 * This is quite tricky because there may be pad bytes in front of the
4770 * DECNET header, and then there are two possible data packet formats that
4771 * carry both src and dst addresses, plus 5 packet types in a format that
4772 * carries only the src node, plus 2 types that use a different format and
4773 * also carry just the src node.
4777 * Instead of doing those all right, we just look for data packets with
4778 * 0 or 1 bytes of padding. If you want to look at other packets, that
4779 * will require a lot more hacking.
4781 * To add support for filtering on DECNET "areas" (network numbers)
4782 * one would want to add a "mask" argument to this routine. That would
4783 * make the filter even more inefficient, although one could be clever
4784 * and not generate masking instructions if the mask is 0xFFFF.
4786 static struct block
*
4787 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4789 struct block
*b0
, *b1
, *b2
, *tmp
;
4790 u_int offset_lh
; /* offset if long header is received */
4791 u_int offset_sh
; /* offset if short header is received */
4796 offset_sh
= 1; /* follows flags */
4797 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4801 offset_sh
= 3; /* follows flags, dstnode */
4802 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4806 /* Inefficient because we do our Calvinball dance twice */
4807 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4808 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4814 /* Inefficient because we do our Calvinball dance twice */
4815 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4816 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4821 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4825 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4829 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4833 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4837 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4841 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4848 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4849 /* Check for pad = 1, long header case */
4850 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4851 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4852 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4853 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4855 /* Check for pad = 0, long header case */
4856 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4857 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4860 /* Check for pad = 1, short header case */
4861 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4862 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4863 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4866 /* Check for pad = 0, short header case */
4867 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4868 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4872 /* Combine with test for cstate->linktype */
4878 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4879 * test the bottom-of-stack bit, and then check the version number
4880 * field in the IP header.
4882 static struct block
*
4883 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4885 struct block
*b0
, *b1
;
4890 /* match the bottom-of-stack bit */
4891 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4892 /* match the IPv4 version number */
4893 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4898 /* match the bottom-of-stack bit */
4899 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4900 /* match the IPv4 version number */
4901 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4910 static struct block
*
4911 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4912 int proto
, int dir
, int type
)
4914 struct block
*b0
, *b1
;
4915 const char *typestr
;
4925 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4927 * Only check for non-IPv4 addresses if we're not
4928 * checking MPLS-encapsulated packets.
4930 if (cstate
->label_stack_depth
== 0) {
4931 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4933 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4939 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4942 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4945 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4948 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4951 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4954 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4957 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4960 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4963 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4966 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4969 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4972 return gen_dnhostop(cstate
, addr
, dir
);
4975 bpf_error(cstate
, "LAT host filtering not implemented");
4978 bpf_error(cstate
, "SCA host filtering not implemented");
4981 bpf_error(cstate
, "MOPRC host filtering not implemented");
4984 bpf_error(cstate
, "MOPDL host filtering not implemented");
4987 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4990 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4993 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4996 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4999 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5002 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5005 bpf_error(cstate
, "AARP host filtering not implemented");
5008 bpf_error(cstate
, "ISO host filtering not implemented");
5011 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5014 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5017 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5020 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5023 bpf_error(cstate
, "IPX host filtering not implemented");
5026 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5029 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5032 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5035 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5038 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5041 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5044 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5047 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5050 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5053 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5062 static struct block
*
5063 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5064 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5066 const char *typestr
;
5076 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5079 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5082 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5085 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5088 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5091 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5094 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5097 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5100 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5103 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5106 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5109 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5112 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5115 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5118 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5121 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5124 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5127 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5130 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5133 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5136 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5139 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5142 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5145 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5148 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5151 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5154 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5157 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5160 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5163 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5166 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5169 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5172 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5175 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5178 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5181 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5184 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5187 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5190 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5193 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5203 static struct block
*
5204 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5205 struct addrinfo
*alist
, int proto
, int dir
)
5207 struct block
*b0
, *b1
, *tmp
;
5208 struct addrinfo
*ai
;
5209 struct sockaddr_in
*sin
;
5212 bpf_error(cstate
, "direction applied to 'gateway'");
5219 switch (cstate
->linktype
) {
5221 case DLT_NETANALYZER
:
5222 case DLT_NETANALYZER_TRANSPARENT
:
5223 b1
= gen_prevlinkhdr_check(cstate
);
5224 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5229 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5232 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5234 case DLT_IEEE802_11
:
5235 case DLT_PRISM_HEADER
:
5236 case DLT_IEEE802_11_RADIO_AVS
:
5237 case DLT_IEEE802_11_RADIO
:
5239 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5243 * This is LLC-multiplexed traffic; if it were
5244 * LANE, cstate->linktype would have been set to
5248 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5250 case DLT_IP_OVER_FC
:
5251 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5255 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5258 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5260 * Does it have an address?
5262 if (ai
->ai_addr
!= NULL
) {
5264 * Yes. Is it an IPv4 address?
5266 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5268 * Generate an entry for it.
5270 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5271 tmp
= gen_host(cstate
,
5272 ntohl(sin
->sin_addr
.s_addr
),
5273 0xffffffff, proto
, Q_OR
, Q_HOST
);
5275 * Is it the *first* IPv4 address?
5279 * Yes, so start with it.
5284 * No, so OR it into the
5296 * No IPv4 addresses found.
5304 bpf_error(cstate
, "illegal modifier of 'gateway'");
5309 static struct block
*
5310 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5318 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5319 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5324 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5325 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5330 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5331 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5336 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5339 #ifndef IPPROTO_IGMP
5340 #define IPPROTO_IGMP 2
5344 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5347 #ifndef IPPROTO_IGRP
5348 #define IPPROTO_IGRP 9
5351 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5355 #define IPPROTO_PIM 103
5359 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5360 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5364 #ifndef IPPROTO_VRRP
5365 #define IPPROTO_VRRP 112
5369 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5372 #ifndef IPPROTO_CARP
5373 #define IPPROTO_CARP 112
5377 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5381 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5385 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5389 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5393 bpf_error(cstate
, "link layer applied in wrong context");
5396 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5400 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5404 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5408 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5412 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5416 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5420 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5424 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5427 #ifndef IPPROTO_ICMPV6
5428 #define IPPROTO_ICMPV6 58
5431 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5435 #define IPPROTO_AH 51
5438 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5439 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5444 #define IPPROTO_ESP 50
5447 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5448 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5453 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5457 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5461 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5464 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5465 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5466 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5468 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5470 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5472 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5476 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5477 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5478 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5480 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5482 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5484 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5488 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5489 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5490 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5492 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5497 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5498 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5503 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5504 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5506 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5508 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5513 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5514 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5519 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5520 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5525 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5529 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5533 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5537 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5541 bpf_error(cstate
, "'radio' is not a valid protocol type");
5550 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5553 * Catch errors reported by us and routines below us, and return NULL
5556 if (setjmp(cstate
->top_ctx
))
5559 return gen_proto_abbrev_internal(cstate
, proto
);
5562 static struct block
*
5563 gen_ipfrag(compiler_state_t
*cstate
)
5568 /* not IPv4 frag other than the first frag */
5569 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5570 b
= new_block(cstate
, JMP(BPF_JSET
));
5579 * Generate a comparison to a port value in the transport-layer header
5580 * at the specified offset from the beginning of that header.
5582 * XXX - this handles a variable-length prefix preceding the link-layer
5583 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5584 * variable-length link-layer headers (such as Token Ring or 802.11
5587 static struct block
*
5588 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5590 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5593 static struct block
*
5594 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5596 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5600 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5602 struct block
*b0
, *b1
, *tmp
;
5604 /* ip proto 'proto' and not a fragment other than the first fragment */
5605 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5606 b0
= gen_ipfrag(cstate
);
5611 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5615 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5619 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5620 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5626 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5627 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5632 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5636 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5640 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5644 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5648 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5652 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5664 static struct block
*
5665 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5667 struct block
*b0
, *b1
, *tmp
;
5672 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5673 * not LLC encapsulation with LLCSAP_IP.
5675 * For IEEE 802 networks - which includes 802.5 token ring
5676 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5677 * says that SNAP encapsulation is used, not LLC encapsulation
5680 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5681 * RFC 2225 say that SNAP encapsulation is used, not LLC
5682 * encapsulation with LLCSAP_IP.
5684 * So we always check for ETHERTYPE_IP.
5686 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5692 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5696 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5697 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5699 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5711 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5713 struct block
*b0
, *b1
, *tmp
;
5715 /* ip6 proto 'proto' */
5716 /* XXX - catch the first fragment of a fragmented packet? */
5717 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5721 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5725 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5729 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5730 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5736 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5737 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5749 static struct block
*
5750 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5752 struct block
*b0
, *b1
, *tmp
;
5754 /* link proto ip6 */
5755 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5761 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5765 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5766 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5768 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5779 /* gen_portrange code */
5780 static struct block
*
5781 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5784 struct block
*b1
, *b2
;
5788 * Reverse the order of the ports, so v1 is the lower one.
5797 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5798 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5806 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5809 struct block
*b0
, *b1
, *tmp
;
5811 /* ip proto 'proto' and not a fragment other than the first fragment */
5812 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5813 b0
= gen_ipfrag(cstate
);
5818 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5822 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5826 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5827 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5833 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5834 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5839 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5843 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5847 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5851 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5855 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5859 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5871 static struct block
*
5872 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5875 struct block
*b0
, *b1
, *tmp
;
5878 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5884 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5888 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5889 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5891 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5902 static struct block
*
5903 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5906 struct block
*b1
, *b2
;
5910 * Reverse the order of the ports, so v1 is the lower one.
5919 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5920 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5928 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5931 struct block
*b0
, *b1
, *tmp
;
5933 /* ip6 proto 'proto' */
5934 /* XXX - catch the first fragment of a fragmented packet? */
5935 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5939 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5943 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5947 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5948 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5954 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5955 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5967 static struct block
*
5968 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5971 struct block
*b0
, *b1
, *tmp
;
5973 /* link proto ip6 */
5974 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5980 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5984 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5985 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5987 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5999 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6008 v
= pcap_nametoproto(name
);
6009 if (v
== PROTO_UNDEF
)
6010 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6014 /* XXX should look up h/w protocol type based on cstate->linktype */
6015 v
= pcap_nametoeproto(name
);
6016 if (v
== PROTO_UNDEF
) {
6017 v
= pcap_nametollc(name
);
6018 if (v
== PROTO_UNDEF
)
6019 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6024 if (strcmp(name
, "esis") == 0)
6026 else if (strcmp(name
, "isis") == 0)
6028 else if (strcmp(name
, "clnp") == 0)
6031 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6043 gen_joinsp(struct stmt
**s
, int n
)
6049 static struct block
*
6050 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6052 #ifdef NO_PROTOCHAIN
6053 return gen_proto(cstate
, v
, proto
, dir
);
6055 struct block
*b0
, *b
;
6056 struct slist
*s
[100];
6057 int fix2
, fix3
, fix4
, fix5
;
6058 int ahcheck
, again
, end
;
6060 int reg2
= alloc_reg(cstate
);
6062 memset(s
, 0, sizeof(s
));
6063 fix3
= fix4
= fix5
= 0;
6070 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
6071 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
6075 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6080 * We don't handle variable-length prefixes before the link-layer
6081 * header, or variable-length link-layer headers, here yet.
6082 * We might want to add BPF instructions to do the protochain
6083 * work, to simplify that and, on platforms that have a BPF
6084 * interpreter with the new instructions, let the filtering
6085 * be done in the kernel. (We already require a modified BPF
6086 * engine to do the protochain stuff, to support backward
6087 * branches, and backward branch support is unlikely to appear
6088 * in kernel BPF engines.)
6090 if (cstate
->off_linkpl
.is_variable
)
6091 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6093 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
6096 * s[0] is a dummy entry to protect other BPF insn from damage
6097 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6098 * hard to find interdependency made by jump table fixup.
6101 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6106 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6109 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6110 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6112 /* X = ip->ip_hl << 2 */
6113 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6114 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6119 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6121 /* A = ip6->ip_nxt */
6122 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6123 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6125 /* X = sizeof(struct ip6_hdr) */
6126 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6132 bpf_error(cstate
, "unsupported proto to gen_protochain");
6136 /* again: if (A == v) goto end; else fall through; */
6138 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6140 s
[i
]->s
.jt
= NULL
; /*later*/
6141 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6145 #ifndef IPPROTO_NONE
6146 #define IPPROTO_NONE 59
6148 /* if (A == IPPROTO_NONE) goto end */
6149 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6150 s
[i
]->s
.jt
= NULL
; /*later*/
6151 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6152 s
[i
]->s
.k
= IPPROTO_NONE
;
6153 s
[fix5
]->s
.jf
= s
[i
];
6157 if (proto
== Q_IPV6
) {
6158 int v6start
, v6end
, v6advance
, j
;
6161 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6162 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6163 s
[i
]->s
.jt
= NULL
; /*later*/
6164 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6165 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6166 s
[fix2
]->s
.jf
= s
[i
];
6168 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6169 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6170 s
[i
]->s
.jt
= NULL
; /*later*/
6171 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6172 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6174 /* if (A == IPPROTO_ROUTING) goto v6advance */
6175 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6176 s
[i
]->s
.jt
= NULL
; /*later*/
6177 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6178 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6180 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6181 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6182 s
[i
]->s
.jt
= NULL
; /*later*/
6183 s
[i
]->s
.jf
= NULL
; /*later*/
6184 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6194 * A = P[X + packet head];
6195 * X = X + (P[X + packet head + 1] + 1) * 8;
6197 /* A = P[X + packet head] */
6198 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6199 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6202 s
[i
] = new_stmt(cstate
, BPF_ST
);
6205 /* A = P[X + packet head + 1]; */
6206 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6207 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6210 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6214 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6218 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6222 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6225 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6229 /* goto again; (must use BPF_JA for backward jump) */
6230 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6231 s
[i
]->s
.k
= again
- i
- 1;
6232 s
[i
- 1]->s
.jf
= s
[i
];
6236 for (j
= v6start
; j
<= v6end
; j
++)
6237 s
[j
]->s
.jt
= s
[v6advance
];
6240 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6242 s
[fix2
]->s
.jf
= s
[i
];
6248 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6249 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6250 s
[i
]->s
.jt
= NULL
; /*later*/
6251 s
[i
]->s
.jf
= NULL
; /*later*/
6252 s
[i
]->s
.k
= IPPROTO_AH
;
6254 s
[fix3
]->s
.jf
= s
[ahcheck
];
6261 * X = X + (P[X + 1] + 2) * 4;
6264 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6266 /* A = P[X + packet head]; */
6267 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6268 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6271 s
[i
] = new_stmt(cstate
, BPF_ST
);
6275 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6278 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6282 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6284 /* A = P[X + packet head] */
6285 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6286 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6289 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6293 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6297 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6300 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6304 /* goto again; (must use BPF_JA for backward jump) */
6305 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6306 s
[i
]->s
.k
= again
- i
- 1;
6311 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6313 s
[fix2
]->s
.jt
= s
[end
];
6314 s
[fix4
]->s
.jf
= s
[end
];
6315 s
[fix5
]->s
.jt
= s
[end
];
6322 for (i
= 0; i
< max
- 1; i
++)
6323 s
[i
]->next
= s
[i
+ 1];
6324 s
[max
- 1]->next
= NULL
;
6329 b
= new_block(cstate
, JMP(BPF_JEQ
));
6330 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6333 free_reg(cstate
, reg2
);
6340 static struct block
*
6341 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6344 struct block
*b0
, *b1
;
6347 * A data frame has the 0x08 bit (b3) in the frame control field set
6348 * and the 0x04 bit (b2) clear.
6350 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6351 b0
= new_block(cstate
, JMP(BPF_JSET
));
6355 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6356 b1
= new_block(cstate
, JMP(BPF_JSET
));
6367 * Generate code that checks whether the packet is a packet for protocol
6368 * <proto> and whether the type field in that protocol's header has
6369 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6370 * IP packet and checks the protocol number in the IP header against <v>.
6372 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6373 * against Q_IP and Q_IPV6.
6375 static struct block
*
6376 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6378 struct block
*b0
, *b1
;
6383 if (dir
!= Q_DEFAULT
)
6384 bpf_error(cstate
, "direction applied to 'proto'");
6388 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6389 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6394 return gen_linktype(cstate
, v
);
6398 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6399 * not LLC encapsulation with LLCSAP_IP.
6401 * For IEEE 802 networks - which includes 802.5 token ring
6402 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6403 * says that SNAP encapsulation is used, not LLC encapsulation
6406 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6407 * RFC 2225 say that SNAP encapsulation is used, not LLC
6408 * encapsulation with LLCSAP_IP.
6410 * So we always check for ETHERTYPE_IP.
6412 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6414 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6416 b1
= gen_protochain(cstate
, v
, Q_IP
);
6422 bpf_error(cstate
, "arp does not encapsulate another protocol");
6426 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6430 bpf_error(cstate
, "'sctp proto' is bogus");
6434 bpf_error(cstate
, "'tcp proto' is bogus");
6438 bpf_error(cstate
, "'udp proto' is bogus");
6442 bpf_error(cstate
, "'icmp proto' is bogus");
6446 bpf_error(cstate
, "'igmp proto' is bogus");
6450 bpf_error(cstate
, "'igrp proto' is bogus");
6454 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6458 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6462 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6466 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6470 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6474 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6478 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6481 * Also check for a fragment header before the final
6484 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6485 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6487 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6490 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6496 bpf_error(cstate
, "'icmp6 proto' is bogus");
6500 bpf_error(cstate
, "'ah proto' is bogus");
6504 bpf_error(cstate
, "'ah proto' is bogus");
6508 bpf_error(cstate
, "'pim proto' is bogus");
6512 bpf_error(cstate
, "'vrrp proto' is bogus");
6516 bpf_error(cstate
, "'aarp proto' is bogus");
6520 switch (cstate
->linktype
) {
6524 * Frame Relay packets typically have an OSI
6525 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6526 * generates code to check for all the OSI
6527 * NLPIDs, so calling it and then adding a check
6528 * for the particular NLPID for which we're
6529 * looking is bogus, as we can just check for
6532 * What we check for is the NLPID and a frame
6533 * control field value of UI, i.e. 0x03 followed
6536 * XXX - assumes a 2-byte Frame Relay header with
6537 * DLCI and flags. What if the address is longer?
6539 * XXX - what about SNAP-encapsulated frames?
6541 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6546 * Cisco uses an Ethertype lookalike - for OSI,
6549 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6550 /* OSI in C-HDLC is stuffed with a fudge byte */
6551 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (bpf_int32
)v
);
6556 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6557 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (bpf_int32
)v
);
6563 bpf_error(cstate
, "'esis proto' is bogus");
6567 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6569 * 4 is the offset of the PDU type relative to the IS-IS
6572 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (bpf_int32
)v
);
6577 bpf_error(cstate
, "'clnp proto' is not supported");
6581 bpf_error(cstate
, "'stp proto' is bogus");
6585 bpf_error(cstate
, "'ipx proto' is bogus");
6589 bpf_error(cstate
, "'netbeui proto' is bogus");
6593 bpf_error(cstate
, "'l1 proto' is bogus");
6597 bpf_error(cstate
, "'l2 proto' is bogus");
6601 bpf_error(cstate
, "'iih proto' is bogus");
6605 bpf_error(cstate
, "'snp proto' is bogus");
6609 bpf_error(cstate
, "'csnp proto' is bogus");
6613 bpf_error(cstate
, "'psnp proto' is bogus");
6617 bpf_error(cstate
, "'lsp proto' is bogus");
6621 bpf_error(cstate
, "'radio proto' is bogus");
6625 bpf_error(cstate
, "'carp proto' is bogus");
6636 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6638 int proto
= q
.proto
;
6642 bpf_u_int32 mask
, addr
;
6643 struct addrinfo
*res
, *res0
;
6644 struct sockaddr_in
*sin4
;
6647 struct sockaddr_in6
*sin6
;
6648 struct in6_addr mask128
;
6650 struct block
*b
, *tmp
;
6651 int port
, real_proto
;
6655 * Catch errors reported by us and routines below us, and return NULL
6658 if (setjmp(cstate
->top_ctx
))
6664 addr
= pcap_nametonetaddr(name
);
6666 bpf_error(cstate
, "unknown network '%s'", name
);
6667 /* Left justify network addr and calculate its network mask */
6669 while (addr
&& (addr
& 0xff000000) == 0) {
6673 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6677 if (proto
== Q_LINK
) {
6678 switch (cstate
->linktype
) {
6681 case DLT_NETANALYZER
:
6682 case DLT_NETANALYZER_TRANSPARENT
:
6683 eaddr
= pcap_ether_hostton(name
);
6686 "unknown ether host '%s'", name
);
6687 tmp
= gen_prevlinkhdr_check(cstate
);
6688 b
= gen_ehostop(cstate
, eaddr
, dir
);
6695 eaddr
= pcap_ether_hostton(name
);
6698 "unknown FDDI host '%s'", name
);
6699 b
= gen_fhostop(cstate
, eaddr
, dir
);
6704 eaddr
= pcap_ether_hostton(name
);
6707 "unknown token ring host '%s'", name
);
6708 b
= gen_thostop(cstate
, eaddr
, dir
);
6712 case DLT_IEEE802_11
:
6713 case DLT_PRISM_HEADER
:
6714 case DLT_IEEE802_11_RADIO_AVS
:
6715 case DLT_IEEE802_11_RADIO
:
6717 eaddr
= pcap_ether_hostton(name
);
6720 "unknown 802.11 host '%s'", name
);
6721 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6725 case DLT_IP_OVER_FC
:
6726 eaddr
= pcap_ether_hostton(name
);
6729 "unknown Fibre Channel host '%s'", name
);
6730 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6735 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6736 } else if (proto
== Q_DECNET
) {
6737 unsigned short dn_addr
;
6739 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6741 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6743 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6748 * I don't think DECNET hosts can be multihomed, so
6749 * there is no need to build up a list of addresses
6751 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6754 memset(&mask128
, 0xff, sizeof(mask128
));
6756 res0
= res
= pcap_nametoaddrinfo(name
);
6758 bpf_error(cstate
, "unknown host '%s'", name
);
6765 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6766 tproto
== Q_DEFAULT
) {
6772 for (res
= res0
; res
; res
= res
->ai_next
) {
6773 switch (res
->ai_family
) {
6776 if (tproto
== Q_IPV6
)
6780 sin4
= (struct sockaddr_in
*)
6782 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6783 0xffffffff, tproto
, dir
, q
.addr
);
6787 if (tproto6
== Q_IP
)
6790 sin6
= (struct sockaddr_in6
*)
6792 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6793 &mask128
, tproto6
, dir
, q
.addr
);
6806 bpf_error(cstate
, "unknown host '%s'%s", name
,
6807 (proto
== Q_DEFAULT
)
6809 : " for specified address family");
6815 if (proto
!= Q_DEFAULT
&&
6816 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6817 bpf_error(cstate
, "illegal qualifier of 'port'");
6818 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6819 bpf_error(cstate
, "unknown port '%s'", name
);
6820 if (proto
== Q_UDP
) {
6821 if (real_proto
== IPPROTO_TCP
)
6822 bpf_error(cstate
, "port '%s' is tcp", name
);
6823 else if (real_proto
== IPPROTO_SCTP
)
6824 bpf_error(cstate
, "port '%s' is sctp", name
);
6826 /* override PROTO_UNDEF */
6827 real_proto
= IPPROTO_UDP
;
6829 if (proto
== Q_TCP
) {
6830 if (real_proto
== IPPROTO_UDP
)
6831 bpf_error(cstate
, "port '%s' is udp", name
);
6833 else if (real_proto
== IPPROTO_SCTP
)
6834 bpf_error(cstate
, "port '%s' is sctp", name
);
6836 /* override PROTO_UNDEF */
6837 real_proto
= IPPROTO_TCP
;
6839 if (proto
== Q_SCTP
) {
6840 if (real_proto
== IPPROTO_UDP
)
6841 bpf_error(cstate
, "port '%s' is udp", name
);
6843 else if (real_proto
== IPPROTO_TCP
)
6844 bpf_error(cstate
, "port '%s' is tcp", name
);
6846 /* override PROTO_UNDEF */
6847 real_proto
= IPPROTO_SCTP
;
6850 bpf_error(cstate
, "illegal port number %d < 0", port
);
6852 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6853 b
= gen_port(cstate
, port
, real_proto
, dir
);
6854 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6858 if (proto
!= Q_DEFAULT
&&
6859 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6860 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6861 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6862 bpf_error(cstate
, "unknown port in range '%s'", name
);
6863 if (proto
== Q_UDP
) {
6864 if (real_proto
== IPPROTO_TCP
)
6865 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6866 else if (real_proto
== IPPROTO_SCTP
)
6867 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6869 /* override PROTO_UNDEF */
6870 real_proto
= IPPROTO_UDP
;
6872 if (proto
== Q_TCP
) {
6873 if (real_proto
== IPPROTO_UDP
)
6874 bpf_error(cstate
, "port in range '%s' is udp", name
);
6875 else if (real_proto
== IPPROTO_SCTP
)
6876 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6878 /* override PROTO_UNDEF */
6879 real_proto
= IPPROTO_TCP
;
6881 if (proto
== Q_SCTP
) {
6882 if (real_proto
== IPPROTO_UDP
)
6883 bpf_error(cstate
, "port in range '%s' is udp", name
);
6884 else if (real_proto
== IPPROTO_TCP
)
6885 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6887 /* override PROTO_UNDEF */
6888 real_proto
= IPPROTO_SCTP
;
6891 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6893 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6895 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6897 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6899 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6900 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6905 eaddr
= pcap_ether_hostton(name
);
6907 bpf_error(cstate
, "unknown ether host: %s", name
);
6909 res
= pcap_nametoaddrinfo(name
);
6912 bpf_error(cstate
, "unknown host '%s'", name
);
6913 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6917 bpf_error(cstate
, "unknown host '%s'", name
);
6920 bpf_error(cstate
, "'gateway' not supported in this configuration");
6924 real_proto
= lookup_proto(cstate
, name
, proto
);
6925 if (real_proto
>= 0)
6926 return gen_proto(cstate
, real_proto
, proto
, dir
);
6928 bpf_error(cstate
, "unknown protocol: %s", name
);
6931 real_proto
= lookup_proto(cstate
, name
, proto
);
6932 if (real_proto
>= 0)
6933 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6935 bpf_error(cstate
, "unknown protocol: %s", name
);
6946 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6947 unsigned int masklen
, struct qual q
)
6949 register int nlen
, mlen
;
6953 * Catch errors reported by us and routines below us, and return NULL
6956 if (setjmp(cstate
->top_ctx
))
6959 nlen
= __pcap_atoin(s1
, &n
);
6960 /* Promote short ipaddr */
6964 mlen
= __pcap_atoin(s2
, &m
);
6965 /* Promote short ipaddr */
6968 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6971 /* Convert mask len to mask */
6973 bpf_error(cstate
, "mask length must be <= 32");
6976 * X << 32 is not guaranteed by C to be 0; it's
6981 m
= 0xffffffff << (32 - masklen
);
6983 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6990 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6993 bpf_error(cstate
, "Mask syntax for networks only");
7000 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7008 * Catch errors reported by us and routines below us, and return NULL
7011 if (setjmp(cstate
->top_ctx
))
7018 else if (q
.proto
== Q_DECNET
) {
7019 vlen
= __pcap_atodn(s
, &v
);
7021 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7023 vlen
= __pcap_atoin(s
, &v
);
7030 if (proto
== Q_DECNET
)
7031 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7032 else if (proto
== Q_LINK
) {
7033 bpf_error(cstate
, "illegal link layer address");
7036 if (s
== NULL
&& q
.addr
== Q_NET
) {
7037 /* Promote short net number */
7038 while (v
&& (v
& 0xff000000) == 0) {
7043 /* Promote short ipaddr */
7045 mask
<<= 32 - vlen
;
7047 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7052 proto
= IPPROTO_UDP
;
7053 else if (proto
== Q_TCP
)
7054 proto
= IPPROTO_TCP
;
7055 else if (proto
== Q_SCTP
)
7056 proto
= IPPROTO_SCTP
;
7057 else if (proto
== Q_DEFAULT
)
7058 proto
= PROTO_UNDEF
;
7060 bpf_error(cstate
, "illegal qualifier of 'port'");
7063 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7067 b
= gen_port(cstate
, (int)v
, proto
, dir
);
7068 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
7074 proto
= IPPROTO_UDP
;
7075 else if (proto
== Q_TCP
)
7076 proto
= IPPROTO_TCP
;
7077 else if (proto
== Q_SCTP
)
7078 proto
= IPPROTO_SCTP
;
7079 else if (proto
== Q_DEFAULT
)
7080 proto
= PROTO_UNDEF
;
7082 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7085 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7089 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
7090 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
7095 bpf_error(cstate
, "'gateway' requires a name");
7099 return gen_proto(cstate
, (int)v
, proto
, dir
);
7102 return gen_protochain(cstate
, (int)v
, proto
, dir
);
7117 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7118 unsigned int masklen
, struct qual q
)
7120 struct addrinfo
*res
;
7121 struct in6_addr
*addr
;
7122 struct in6_addr mask
;
7127 * Catch errors reported by us and routines below us, and return NULL
7130 if (setjmp(cstate
->top_ctx
))
7134 bpf_error(cstate
, "no mask %s supported", s2
);
7136 res
= pcap_nametoaddrinfo(s1
);
7138 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7141 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7142 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7144 if (sizeof(mask
) * 8 < masklen
)
7145 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
7146 memset(&mask
, 0, sizeof(mask
));
7147 memset(&mask
, 0xff, masklen
/ 8);
7149 mask
.s6_addr
[masklen
/ 8] =
7150 (0xff << (8 - masklen
% 8)) & 0xff;
7153 a
= (uint32_t *)addr
;
7154 m
= (uint32_t *)&mask
;
7155 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7156 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7157 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7165 bpf_error(cstate
, "Mask syntax for networks only");
7169 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7175 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7182 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7184 struct block
*b
, *tmp
;
7187 * Catch errors reported by us and routines below us, and return NULL
7190 if (setjmp(cstate
->top_ctx
))
7193 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7194 cstate
->e
= pcap_ether_aton(s
);
7195 if (cstate
->e
== NULL
)
7196 bpf_error(cstate
, "malloc");
7197 switch (cstate
->linktype
) {
7199 case DLT_NETANALYZER
:
7200 case DLT_NETANALYZER_TRANSPARENT
:
7201 tmp
= gen_prevlinkhdr_check(cstate
);
7202 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7207 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7210 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7212 case DLT_IEEE802_11
:
7213 case DLT_PRISM_HEADER
:
7214 case DLT_IEEE802_11_RADIO_AVS
:
7215 case DLT_IEEE802_11_RADIO
:
7217 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7219 case DLT_IP_OVER_FC
:
7220 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7225 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7232 bpf_error(cstate
, "ethernet address used in non-ether expression");
7237 sappend(struct slist
*s0
, struct slist
*s1
)
7240 * This is definitely not the best way to do this, but the
7241 * lists will rarely get long.
7248 static struct slist
*
7249 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7253 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7258 static struct slist
*
7259 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7263 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7269 * Modify "index" to use the value stored into its register as an
7270 * offset relative to the beginning of the header for the protocol
7271 * "proto", and allocate a register and put an item "size" bytes long
7272 * (1, 2, or 4) at that offset into that register, making it the register
7275 static struct arth
*
7276 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7278 struct slist
*s
, *tmp
;
7280 int regno
= alloc_reg(cstate
);
7282 free_reg(cstate
, inst
->regno
);
7286 bpf_error(cstate
, "data size must be 1, 2, or 4");
7302 bpf_error(cstate
, "unsupported index operation");
7306 * The offset is relative to the beginning of the packet
7307 * data, if we have a radio header. (If we don't, this
7310 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7311 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7312 cstate
->linktype
!= DLT_PRISM_HEADER
)
7313 bpf_error(cstate
, "radio information not present in capture");
7316 * Load into the X register the offset computed into the
7317 * register specified by "index".
7319 s
= xfer_to_x(cstate
, inst
);
7322 * Load the item at that offset.
7324 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7326 sappend(inst
->s
, s
);
7331 * The offset is relative to the beginning of
7332 * the link-layer header.
7334 * XXX - what about ATM LANE? Should the index be
7335 * relative to the beginning of the AAL5 frame, so
7336 * that 0 refers to the beginning of the LE Control
7337 * field, or relative to the beginning of the LAN
7338 * frame, so that 0 refers, for Ethernet LANE, to
7339 * the beginning of the destination address?
7341 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7344 * If "s" is non-null, it has code to arrange that the
7345 * X register contains the length of the prefix preceding
7346 * the link-layer header. Add to it the offset computed
7347 * into the register specified by "index", and move that
7348 * into the X register. Otherwise, just load into the X
7349 * register the offset computed into the register specified
7353 sappend(s
, xfer_to_a(cstate
, inst
));
7354 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7355 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7357 s
= xfer_to_x(cstate
, inst
);
7360 * Load the item at the sum of the offset we've put in the
7361 * X register and the offset of the start of the link
7362 * layer header (which is 0 if the radio header is
7363 * variable-length; that header length is what we put
7364 * into the X register and then added to the index).
7366 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7367 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7369 sappend(inst
->s
, s
);
7383 * The offset is relative to the beginning of
7384 * the network-layer header.
7385 * XXX - are there any cases where we want
7386 * cstate->off_nl_nosnap?
7388 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7391 * If "s" is non-null, it has code to arrange that the
7392 * X register contains the variable part of the offset
7393 * of the link-layer payload. Add to it the offset
7394 * computed into the register specified by "index",
7395 * and move that into the X register. Otherwise, just
7396 * load into the X register the offset computed into
7397 * the register specified by "index".
7400 sappend(s
, xfer_to_a(cstate
, inst
));
7401 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7402 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7404 s
= xfer_to_x(cstate
, inst
);
7407 * Load the item at the sum of the offset we've put in the
7408 * X register, the offset of the start of the network
7409 * layer header from the beginning of the link-layer
7410 * payload, and the constant part of the offset of the
7411 * start of the link-layer payload.
7413 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7414 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7416 sappend(inst
->s
, s
);
7419 * Do the computation only if the packet contains
7420 * the protocol in question.
7422 b
= gen_proto_abbrev_internal(cstate
, proto
);
7424 gen_and(inst
->b
, b
);
7438 * The offset is relative to the beginning of
7439 * the transport-layer header.
7441 * Load the X register with the length of the IPv4 header
7442 * (plus the offset of the link-layer header, if it's
7443 * a variable-length header), in bytes.
7445 * XXX - are there any cases where we want
7446 * cstate->off_nl_nosnap?
7447 * XXX - we should, if we're built with
7448 * IPv6 support, generate code to load either
7449 * IPv4, IPv6, or both, as appropriate.
7451 s
= gen_loadx_iphdrlen(cstate
);
7454 * The X register now contains the sum of the variable
7455 * part of the offset of the link-layer payload and the
7456 * length of the network-layer header.
7458 * Load into the A register the offset relative to
7459 * the beginning of the transport layer header,
7460 * add the X register to that, move that to the
7461 * X register, and load with an offset from the
7462 * X register equal to the sum of the constant part of
7463 * the offset of the link-layer payload and the offset,
7464 * relative to the beginning of the link-layer payload,
7465 * of the network-layer header.
7467 sappend(s
, xfer_to_a(cstate
, inst
));
7468 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7469 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7470 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7471 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7472 sappend(inst
->s
, s
);
7475 * Do the computation only if the packet contains
7476 * the protocol in question - which is true only
7477 * if this is an IP datagram and is the first or
7478 * only fragment of that datagram.
7480 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7482 gen_and(inst
->b
, b
);
7483 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7488 * Do the computation only if the packet contains
7489 * the protocol in question.
7491 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7493 gen_and(inst
->b
, b
);
7498 * Check if we have an icmp6 next header
7500 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7502 gen_and(inst
->b
, b
);
7507 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7509 * If "s" is non-null, it has code to arrange that the
7510 * X register contains the variable part of the offset
7511 * of the link-layer payload. Add to it the offset
7512 * computed into the register specified by "index",
7513 * and move that into the X register. Otherwise, just
7514 * load into the X register the offset computed into
7515 * the register specified by "index".
7518 sappend(s
, xfer_to_a(cstate
, inst
));
7519 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7520 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7522 s
= xfer_to_x(cstate
, inst
);
7526 * Load the item at the sum of the offset we've put in the
7527 * X register, the offset of the start of the network
7528 * layer header from the beginning of the link-layer
7529 * payload, and the constant part of the offset of the
7530 * start of the link-layer payload.
7532 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7533 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7536 sappend(inst
->s
, s
);
7540 inst
->regno
= regno
;
7541 s
= new_stmt(cstate
, BPF_ST
);
7543 sappend(inst
->s
, s
);
7549 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7552 * Catch errors reported by us and routines below us, and return NULL
7555 if (setjmp(cstate
->top_ctx
))
7558 return gen_load_internal(cstate
, proto
, inst
, size
);
7561 static struct block
*
7562 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7563 struct arth
*a1
, int reversed
)
7565 struct slist
*s0
, *s1
, *s2
;
7566 struct block
*b
, *tmp
;
7568 s0
= xfer_to_x(cstate
, a1
);
7569 s1
= xfer_to_a(cstate
, a0
);
7570 if (code
== BPF_JEQ
) {
7571 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7572 b
= new_block(cstate
, JMP(code
));
7576 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7582 sappend(a0
->s
, a1
->s
);
7586 free_reg(cstate
, a0
->regno
);
7587 free_reg(cstate
, a1
->regno
);
7589 /* 'and' together protocol checks */
7592 gen_and(a0
->b
, tmp
= a1
->b
);
7606 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7607 struct arth
*a1
, int reversed
)
7610 * Catch errors reported by us and routines below us, and return NULL
7613 if (setjmp(cstate
->top_ctx
))
7616 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7620 gen_loadlen(compiler_state_t
*cstate
)
7627 * Catch errors reported by us and routines below us, and return NULL
7630 if (setjmp(cstate
->top_ctx
))
7633 regno
= alloc_reg(cstate
);
7634 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7635 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7636 s
->next
= new_stmt(cstate
, BPF_ST
);
7637 s
->next
->s
.k
= regno
;
7644 static struct arth
*
7645 gen_loadi_internal(compiler_state_t
*cstate
, int val
)
7651 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7653 reg
= alloc_reg(cstate
);
7655 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7657 s
->next
= new_stmt(cstate
, BPF_ST
);
7666 gen_loadi(compiler_state_t
*cstate
, int val
)
7669 * Catch errors reported by us and routines below us, and return NULL
7672 if (setjmp(cstate
->top_ctx
))
7675 return gen_loadi_internal(cstate
, val
);
7679 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7684 * Catch errors reported by us and routines below us, and return NULL
7687 if (setjmp(cstate
->top_ctx
))
7690 s
= xfer_to_a(cstate
, a
);
7692 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7695 s
= new_stmt(cstate
, BPF_ST
);
7703 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7706 struct slist
*s0
, *s1
, *s2
;
7709 * Catch errors reported by us and routines below us, and return NULL
7712 if (setjmp(cstate
->top_ctx
))
7716 * Disallow division by, or modulus by, zero; we do this here
7717 * so that it gets done even if the optimizer is disabled.
7719 * Also disallow shifts by a value greater than 31; we do this
7720 * here, for the same reason.
7722 if (code
== BPF_DIV
) {
7723 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7724 bpf_error(cstate
, "division by zero");
7725 } else if (code
== BPF_MOD
) {
7726 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7727 bpf_error(cstate
, "modulus by zero");
7728 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7730 * XXX - we need to make up our minds as to what integers
7731 * are signed and what integers are unsigned in BPF programs
7734 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) &&
7735 (a1
->s
->s
.k
< 0 || a1
->s
->s
.k
> 31))
7736 bpf_error(cstate
, "shift by more than 31 bits");
7738 s0
= xfer_to_x(cstate
, a1
);
7739 s1
= xfer_to_a(cstate
, a0
);
7740 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7745 sappend(a0
->s
, a1
->s
);
7747 free_reg(cstate
, a0
->regno
);
7748 free_reg(cstate
, a1
->regno
);
7750 s0
= new_stmt(cstate
, BPF_ST
);
7751 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7758 * Initialize the table of used registers and the current register.
7761 init_regs(compiler_state_t
*cstate
)
7764 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7768 * Return the next free register.
7771 alloc_reg(compiler_state_t
*cstate
)
7773 int n
= BPF_MEMWORDS
;
7776 if (cstate
->regused
[cstate
->curreg
])
7777 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7779 cstate
->regused
[cstate
->curreg
] = 1;
7780 return cstate
->curreg
;
7783 bpf_error(cstate
, "too many registers needed to evaluate expression");
7788 * Return a register to the table so it can
7792 free_reg(compiler_state_t
*cstate
, int n
)
7794 cstate
->regused
[n
] = 0;
7797 static struct block
*
7798 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7803 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7804 b
= new_block(cstate
, JMP(jmp
));
7812 gen_greater(compiler_state_t
*cstate
, int n
)
7815 * Catch errors reported by us and routines below us, and return NULL
7818 if (setjmp(cstate
->top_ctx
))
7821 return gen_len(cstate
, BPF_JGE
, n
);
7825 * Actually, this is less than or equal.
7828 gen_less(compiler_state_t
*cstate
, int n
)
7833 * Catch errors reported by us and routines below us, and return NULL
7836 if (setjmp(cstate
->top_ctx
))
7839 b
= gen_len(cstate
, BPF_JGT
, n
);
7846 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7847 * the beginning of the link-layer header.
7848 * XXX - that means you can't test values in the radiotap header, but
7849 * as that header is difficult if not impossible to parse generally
7850 * without a loop, that might not be a severe problem. A new keyword
7851 * "radio" could be added for that, although what you'd really want
7852 * would be a way of testing particular radio header values, which
7853 * would generate code appropriate to the radio header in question.
7856 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7862 * Catch errors reported by us and routines below us, and return NULL
7865 if (setjmp(cstate
->top_ctx
))
7873 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7876 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7880 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7884 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7888 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7892 b
= new_block(cstate
, JMP(BPF_JEQ
));
7899 static const u_char abroadcast
[] = { 0x0 };
7902 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7904 bpf_u_int32 hostmask
;
7905 struct block
*b0
, *b1
, *b2
;
7906 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7909 * Catch errors reported by us and routines below us, and return NULL
7912 if (setjmp(cstate
->top_ctx
))
7919 switch (cstate
->linktype
) {
7921 case DLT_ARCNET_LINUX
:
7922 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7924 case DLT_NETANALYZER
:
7925 case DLT_NETANALYZER_TRANSPARENT
:
7926 b1
= gen_prevlinkhdr_check(cstate
);
7927 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7932 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7934 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7935 case DLT_IEEE802_11
:
7936 case DLT_PRISM_HEADER
:
7937 case DLT_IEEE802_11_RADIO_AVS
:
7938 case DLT_IEEE802_11_RADIO
:
7940 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7941 case DLT_IP_OVER_FC
:
7942 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7944 bpf_error(cstate
, "not a broadcast link");
7950 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7951 * as an indication that we don't know the netmask, and fail
7954 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7955 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7956 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7957 hostmask
= ~cstate
->netmask
;
7958 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7959 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7960 (bpf_int32
)(~0 & hostmask
), hostmask
);
7965 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7970 * Generate code to test the low-order bit of a MAC address (that's
7971 * the bottom bit of the *first* byte).
7973 static struct block
*
7974 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7976 register struct block
*b0
;
7977 register struct slist
*s
;
7979 /* link[offset] & 1 != 0 */
7980 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7981 b0
= new_block(cstate
, JMP(BPF_JSET
));
7988 gen_multicast(compiler_state_t
*cstate
, int proto
)
7990 register struct block
*b0
, *b1
, *b2
;
7991 register struct slist
*s
;
7994 * Catch errors reported by us and routines below us, and return NULL
7997 if (setjmp(cstate
->top_ctx
))
8004 switch (cstate
->linktype
) {
8006 case DLT_ARCNET_LINUX
:
8007 /* all ARCnet multicasts use the same address */
8008 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8010 case DLT_NETANALYZER
:
8011 case DLT_NETANALYZER_TRANSPARENT
:
8012 b1
= gen_prevlinkhdr_check(cstate
);
8013 /* ether[0] & 1 != 0 */
8014 b0
= gen_mac_multicast(cstate
, 0);
8020 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8022 * XXX - was that referring to bit-order issues?
8024 /* fddi[1] & 1 != 0 */
8025 return gen_mac_multicast(cstate
, 1);
8027 /* tr[2] & 1 != 0 */
8028 return gen_mac_multicast(cstate
, 2);
8029 case DLT_IEEE802_11
:
8030 case DLT_PRISM_HEADER
:
8031 case DLT_IEEE802_11_RADIO_AVS
:
8032 case DLT_IEEE802_11_RADIO
:
8037 * For control frames, there is no DA.
8039 * For management frames, DA is at an
8040 * offset of 4 from the beginning of
8043 * For data frames, DA is at an offset
8044 * of 4 from the beginning of the packet
8045 * if To DS is clear and at an offset of
8046 * 16 from the beginning of the packet
8051 * Generate the tests to be done for data frames.
8053 * First, check for To DS set, i.e. "link[1] & 0x01".
8055 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8056 b1
= new_block(cstate
, JMP(BPF_JSET
));
8057 b1
->s
.k
= 0x01; /* To DS */
8061 * If To DS is set, the DA is at 16.
8063 b0
= gen_mac_multicast(cstate
, 16);
8067 * Now, check for To DS not set, i.e. check
8068 * "!(link[1] & 0x01)".
8070 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8071 b2
= new_block(cstate
, JMP(BPF_JSET
));
8072 b2
->s
.k
= 0x01; /* To DS */
8077 * If To DS is not set, the DA is at 4.
8079 b1
= gen_mac_multicast(cstate
, 4);
8083 * Now OR together the last two checks. That gives
8084 * the complete set of checks for data frames.
8089 * Now check for a data frame.
8090 * I.e, check "link[0] & 0x08".
8092 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8093 b1
= new_block(cstate
, JMP(BPF_JSET
));
8098 * AND that with the checks done for data frames.
8103 * If the high-order bit of the type value is 0, this
8104 * is a management frame.
8105 * I.e, check "!(link[0] & 0x08)".
8107 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8108 b2
= new_block(cstate
, JMP(BPF_JSET
));
8114 * For management frames, the DA is at 4.
8116 b1
= gen_mac_multicast(cstate
, 4);
8120 * OR that with the checks done for data frames.
8121 * That gives the checks done for management and
8127 * If the low-order bit of the type value is 1,
8128 * this is either a control frame or a frame
8129 * with a reserved type, and thus not a
8132 * I.e., check "!(link[0] & 0x04)".
8134 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8135 b1
= new_block(cstate
, JMP(BPF_JSET
));
8141 * AND that with the checks for data and management
8146 case DLT_IP_OVER_FC
:
8147 b0
= gen_mac_multicast(cstate
, 2);
8152 /* Link not known to support multicasts */
8156 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8157 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
8162 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8163 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
8167 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8172 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8173 * Outbound traffic is sent by this machine, while inbound traffic is
8174 * sent by a remote machine (and may include packets destined for a
8175 * unicast or multicast link-layer address we are not subscribing to).
8176 * These are the same definitions implemented by pcap_setdirection().
8177 * Capturing only unicast traffic destined for this host is probably
8178 * better accomplished using a higher-layer filter.
8181 gen_inbound(compiler_state_t
*cstate
, int dir
)
8183 register struct block
*b0
;
8186 * Catch errors reported by us and routines below us, and return NULL
8189 if (setjmp(cstate
->top_ctx
))
8193 * Only some data link types support inbound/outbound qualifiers.
8195 switch (cstate
->linktype
) {
8197 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8198 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8199 gen_loadi_internal(cstate
, 0),
8205 /* match outgoing packets */
8206 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8208 /* match incoming packets */
8209 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8214 /* match outgoing packets */
8215 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8217 /* to filter on inbound traffic, invert the match */
8222 case DLT_LINUX_SLL2
:
8223 /* match outgoing packets */
8224 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8226 /* to filter on inbound traffic, invert the match */
8231 #ifdef HAVE_NET_PFVAR_H
8233 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8234 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
8240 /* match outgoing packets */
8241 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8243 /* match incoming packets */
8244 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8248 case DLT_JUNIPER_MFR
:
8249 case DLT_JUNIPER_MLFR
:
8250 case DLT_JUNIPER_MLPPP
:
8251 case DLT_JUNIPER_ATM1
:
8252 case DLT_JUNIPER_ATM2
:
8253 case DLT_JUNIPER_PPPOE
:
8254 case DLT_JUNIPER_PPPOE_ATM
:
8255 case DLT_JUNIPER_GGSN
:
8256 case DLT_JUNIPER_ES
:
8257 case DLT_JUNIPER_MONITOR
:
8258 case DLT_JUNIPER_SERVICES
:
8259 case DLT_JUNIPER_ETHER
:
8260 case DLT_JUNIPER_PPP
:
8261 case DLT_JUNIPER_FRELAY
:
8262 case DLT_JUNIPER_CHDLC
:
8263 case DLT_JUNIPER_VP
:
8264 case DLT_JUNIPER_ST
:
8265 case DLT_JUNIPER_ISM
:
8266 case DLT_JUNIPER_VS
:
8267 case DLT_JUNIPER_SRX_E2E
:
8268 case DLT_JUNIPER_FIBRECHANNEL
:
8269 case DLT_JUNIPER_ATM_CEMIC
:
8271 /* juniper flags (including direction) are stored
8272 * the byte after the 3-byte magic number */
8274 /* match outgoing packets */
8275 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8277 /* match incoming packets */
8278 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8284 * If we have packet meta-data indicating a direction,
8285 * and that metadata can be checked by BPF code, check
8286 * it. Otherwise, give up, as this link-layer type has
8287 * nothing in the packet data.
8289 * Currently, the only platform where a BPF filter can
8290 * check that metadata is Linux with the in-kernel
8291 * BPF interpreter. If other packet capture mechanisms
8292 * and BPF filters also supported this, it would be
8293 * nice. It would be even better if they made that
8294 * metadata available so that we could provide it
8295 * with newer capture APIs, allowing it to be saved
8298 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
8300 * This is Linux with PF_PACKET support.
8301 * If this is a *live* capture, we can look at
8302 * special meta-data in the filter expression;
8303 * if it's a savefile, we can't.
8305 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8306 /* We have a FILE *, so this is a savefile */
8307 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
8312 /* match outgoing packets */
8313 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8316 /* to filter on inbound traffic, invert the match */
8319 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8320 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
8323 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8328 #ifdef HAVE_NET_PFVAR_H
8329 /* PF firewall log matched interface */
8331 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8337 * Catch errors reported by us and routines below us, and return NULL
8340 if (setjmp(cstate
->top_ctx
))
8343 if (cstate
->linktype
!= DLT_PFLOG
) {
8344 bpf_error(cstate
, "ifname supported only on PF linktype");
8347 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8348 off
= offsetof(struct pfloghdr
, ifname
);
8349 if (strlen(ifname
) >= len
) {
8350 bpf_error(cstate
, "ifname interface names can only be %d characters",
8354 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
8358 /* PF firewall log ruleset name */
8360 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8365 * Catch errors reported by us and routines below us, and return NULL
8368 if (setjmp(cstate
->top_ctx
))
8371 if (cstate
->linktype
!= DLT_PFLOG
) {
8372 bpf_error(cstate
, "ruleset supported only on PF linktype");
8376 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8377 bpf_error(cstate
, "ruleset names can only be %ld characters",
8378 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8382 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8383 strlen(ruleset
), (const u_char
*)ruleset
);
8387 /* PF firewall log rule number */
8389 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8394 * Catch errors reported by us and routines below us, and return NULL
8397 if (setjmp(cstate
->top_ctx
))
8400 if (cstate
->linktype
!= DLT_PFLOG
) {
8401 bpf_error(cstate
, "rnr supported only on PF linktype");
8405 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8410 /* PF firewall log sub-rule number */
8412 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8417 * Catch errors reported by us and routines below us, and return NULL
8420 if (setjmp(cstate
->top_ctx
))
8423 if (cstate
->linktype
!= DLT_PFLOG
) {
8424 bpf_error(cstate
, "srnr supported only on PF linktype");
8428 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8433 /* PF firewall log reason code */
8435 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8440 * Catch errors reported by us and routines below us, and return NULL
8443 if (setjmp(cstate
->top_ctx
))
8446 if (cstate
->linktype
!= DLT_PFLOG
) {
8447 bpf_error(cstate
, "reason supported only on PF linktype");
8451 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8456 /* PF firewall log action */
8458 gen_pf_action(compiler_state_t
*cstate
, int action
)
8463 * Catch errors reported by us and routines below us, and return NULL
8466 if (setjmp(cstate
->top_ctx
))
8469 if (cstate
->linktype
!= DLT_PFLOG
) {
8470 bpf_error(cstate
, "action supported only on PF linktype");
8474 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8478 #else /* !HAVE_NET_PFVAR_H */
8480 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8483 * Catch errors reported by us and routines below us, and return NULL
8486 if (setjmp(cstate
->top_ctx
))
8489 bpf_error(cstate
, "libpcap was compiled without pf support");
8494 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8497 * Catch errors reported by us and routines below us, and return NULL
8500 if (setjmp(cstate
->top_ctx
))
8503 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8508 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8511 * Catch errors reported by us and routines below us, and return NULL
8514 if (setjmp(cstate
->top_ctx
))
8517 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8522 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8525 * Catch errors reported by us and routines below us, and return NULL
8528 if (setjmp(cstate
->top_ctx
))
8531 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8536 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8539 * Catch errors reported by us and routines below us, and return NULL
8542 if (setjmp(cstate
->top_ctx
))
8545 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8550 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8553 * Catch errors reported by us and routines below us, and return NULL
8556 if (setjmp(cstate
->top_ctx
))
8559 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8562 #endif /* HAVE_NET_PFVAR_H */
8564 /* IEEE 802.11 wireless header */
8566 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
8571 * Catch errors reported by us and routines below us, and return NULL
8574 if (setjmp(cstate
->top_ctx
))
8577 switch (cstate
->linktype
) {
8579 case DLT_IEEE802_11
:
8580 case DLT_PRISM_HEADER
:
8581 case DLT_IEEE802_11_RADIO_AVS
:
8582 case DLT_IEEE802_11_RADIO
:
8583 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8588 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8596 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8601 * Catch errors reported by us and routines below us, and return NULL
8604 if (setjmp(cstate
->top_ctx
))
8607 switch (cstate
->linktype
) {
8609 case DLT_IEEE802_11
:
8610 case DLT_PRISM_HEADER
:
8611 case DLT_IEEE802_11_RADIO_AVS
:
8612 case DLT_IEEE802_11_RADIO
:
8616 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8620 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8621 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8627 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8632 * Catch errors reported by us and routines below us, and return NULL
8635 if (setjmp(cstate
->top_ctx
))
8638 switch (cstate
->linktype
) {
8641 case DLT_ARCNET_LINUX
:
8642 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8643 q
.proto
== Q_LINK
) {
8644 cstate
->e
= pcap_ether_aton(s
);
8645 if (cstate
->e
== NULL
)
8646 bpf_error(cstate
, "malloc");
8647 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8652 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8656 bpf_error(cstate
, "aid supported only on ARCnet");
8661 static struct block
*
8662 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8664 register struct block
*b0
, *b1
;
8667 /* src comes first, different from Ethernet */
8669 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8672 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8675 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8676 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8682 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8683 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8688 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8692 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8696 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8700 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8704 bpf_error(cstate
, "'ra' is only supported on 802.11");
8708 bpf_error(cstate
, "'ta' is only supported on 802.11");
8715 static struct block
*
8716 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8718 struct block
*b0
, *b1
;
8720 /* check for VLAN, including QinQ */
8721 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8722 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8725 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8731 static struct block
*
8732 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8734 if (vlan_num
> 0x0fff) {
8735 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8738 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8741 static struct block
*
8742 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8745 struct block
*b0
, *b1
;
8747 b0
= gen_vlan_tpid_test(cstate
);
8750 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8756 * Both payload and link header type follow the VLAN tags so that
8757 * both need to be updated.
8759 cstate
->off_linkpl
.constant_part
+= 4;
8760 cstate
->off_linktype
.constant_part
+= 4;
8765 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8766 /* add v to variable part of off */
8768 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8772 if (!off
->is_variable
)
8773 off
->is_variable
= 1;
8775 off
->reg
= alloc_reg(cstate
);
8777 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8780 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8783 s2
= new_stmt(cstate
, BPF_ST
);
8789 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8790 * and link type offsets first
8793 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8797 /* offset determined at run time, shift variable part */
8799 cstate
->is_vlan_vloffset
= 1;
8800 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8801 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8803 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8804 sappend(s
.next
, b_tpid
->head
->stmts
);
8805 b_tpid
->head
->stmts
= s
.next
;
8809 * patch block b_vid (VLAN id test) to load VID value either from packet
8810 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8813 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8815 struct slist
*s
, *s2
, *sjeq
;
8818 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8819 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8821 /* true -> next instructions, false -> beginning of b_vid */
8822 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8824 sjeq
->s
.jf
= b_vid
->stmts
;
8827 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8828 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8832 /* Jump to the test in b_vid. We need to jump one instruction before
8833 * the end of the b_vid block so that we only skip loading the TCI
8834 * from packet data and not the 'and' instruction extractging VID.
8837 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8839 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8843 /* insert our statements at the beginning of b_vid */
8844 sappend(s
, b_vid
->stmts
);
8849 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8850 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8851 * tag can be either in metadata or in packet data; therefore if the
8852 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8853 * header for VLAN tag. As the decision is done at run time, we need
8854 * update variable part of the offsets
8856 static struct block
*
8857 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8860 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8863 /* generate new filter code based on extracting packet
8865 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8866 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8868 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8873 * This is tricky. We need to insert the statements updating variable
8874 * parts of offsets before the the traditional TPID and VID tests so
8875 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8876 * we do not want this update to affect those checks. That's why we
8877 * generate both test blocks first and insert the statements updating
8878 * variable parts of both offsets after that. This wouldn't work if
8879 * there already were variable length link header when entering this
8880 * function but gen_vlan_bpf_extensions() isn't called in that case.
8882 b_tpid
= gen_vlan_tpid_test(cstate
);
8884 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8886 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8891 gen_vlan_patch_vid_test(cstate
, b_vid
);
8901 * support IEEE 802.1Q VLAN trunk over ethernet
8904 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8909 * Catch errors reported by us and routines below us, and return NULL
8912 if (setjmp(cstate
->top_ctx
))
8915 /* can't check for VLAN-encapsulated packets inside MPLS */
8916 if (cstate
->label_stack_depth
> 0)
8917 bpf_error(cstate
, "no VLAN match after MPLS");
8920 * Check for a VLAN packet, and then change the offsets to point
8921 * to the type and data fields within the VLAN packet. Just
8922 * increment the offsets, so that we can support a hierarchy, e.g.
8923 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8926 * XXX - this is a bit of a kludge. If we were to split the
8927 * compiler into a parser that parses an expression and
8928 * generates an expression tree, and a code generator that
8929 * takes an expression tree (which could come from our
8930 * parser or from some other parser) and generates BPF code,
8931 * we could perhaps make the offsets parameters of routines
8932 * and, in the handler for an "AND" node, pass to subnodes
8933 * other than the VLAN node the adjusted offsets.
8935 * This would mean that "vlan" would, instead of changing the
8936 * behavior of *all* tests after it, change only the behavior
8937 * of tests ANDed with it. That would change the documented
8938 * semantics of "vlan", which might break some expressions.
8939 * However, it would mean that "(vlan and ip) or ip" would check
8940 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8941 * checking only for VLAN-encapsulated IP, so that could still
8942 * be considered worth doing; it wouldn't break expressions
8943 * that are of the form "vlan and ..." or "vlan N and ...",
8944 * which I suspect are the most common expressions involving
8945 * "vlan". "vlan or ..." doesn't necessarily do what the user
8946 * would really want, now, as all the "or ..." tests would
8947 * be done assuming a VLAN, even though the "or" could be viewed
8948 * as meaning "or, if this isn't a VLAN packet...".
8950 switch (cstate
->linktype
) {
8953 case DLT_NETANALYZER
:
8954 case DLT_NETANALYZER_TRANSPARENT
:
8955 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8956 /* Verify that this is the outer part of the packet and
8957 * not encapsulated somehow. */
8958 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8959 cstate
->off_linkhdr
.constant_part
==
8960 cstate
->off_outermostlinkhdr
.constant_part
) {
8962 * Do we need special VLAN handling?
8964 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8965 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8968 b0
= gen_vlan_no_bpf_extensions(cstate
,
8969 vlan_num
, has_vlan_tag
);
8972 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8976 case DLT_IEEE802_11
:
8977 case DLT_PRISM_HEADER
:
8978 case DLT_IEEE802_11_RADIO_AVS
:
8979 case DLT_IEEE802_11_RADIO
:
8980 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8984 bpf_error(cstate
, "no VLAN support for data link type %d",
8989 cstate
->vlan_stack_depth
++;
8997 * The label_num_arg dance is to avoid annoying whining by compilers that
8998 * label_num might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
8999 * It's not *used* after setjmp returns.
9002 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num_arg
,
9005 struct block
*b0
, *b1
;
9006 bpf_u_int32 label_num
= label_num_arg
;
9009 * Catch errors reported by us and routines below us, and return NULL
9012 if (setjmp(cstate
->top_ctx
))
9015 if (cstate
->label_stack_depth
> 0) {
9016 /* just match the bottom-of-stack bit clear */
9017 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9020 * We're not in an MPLS stack yet, so check the link-layer
9021 * type against MPLS.
9023 switch (cstate
->linktype
) {
9025 case DLT_C_HDLC
: /* fall through */
9027 case DLT_NETANALYZER
:
9028 case DLT_NETANALYZER_TRANSPARENT
:
9029 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9033 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9036 /* FIXME add other DLT_s ...
9037 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9038 * leave it for now */
9041 bpf_error(cstate
, "no MPLS support for data link type %d",
9047 /* If a specific MPLS label is requested, check it */
9048 if (has_label_num
) {
9049 if (label_num
> 0xFFFFF) {
9050 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9051 label_num
, 0xFFFFF);
9053 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9054 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
9055 0xfffff000); /* only compare the first 20 bits */
9061 * Change the offsets to point to the type and data fields within
9062 * the MPLS packet. Just increment the offsets, so that we
9063 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9064 * capture packets with an outer label of 100000 and an inner
9067 * Increment the MPLS stack depth as well; this indicates that
9068 * we're checking MPLS-encapsulated headers, to make sure higher
9069 * level code generators don't try to match against IP-related
9070 * protocols such as Q_ARP, Q_RARP etc.
9072 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9074 cstate
->off_nl_nosnap
+= 4;
9075 cstate
->off_nl
+= 4;
9076 cstate
->label_stack_depth
++;
9081 * Support PPPOE discovery and session.
9084 gen_pppoed(compiler_state_t
*cstate
)
9087 * Catch errors reported by us and routines below us, and return NULL
9090 if (setjmp(cstate
->top_ctx
))
9093 /* check for PPPoE discovery */
9094 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
9098 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9100 struct block
*b0
, *b1
;
9103 * Catch errors reported by us and routines below us, and return NULL
9106 if (setjmp(cstate
->top_ctx
))
9110 * Test against the PPPoE session link-layer type.
9112 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
9114 /* If a specific session is requested, check PPPoE session id */
9116 if (sess_num
> 0x0000ffff) {
9117 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9118 sess_num
, 0x0000ffff);
9120 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
9121 (bpf_int32
)sess_num
, 0x0000ffff);
9127 * Change the offsets to point to the type and data fields within
9128 * the PPP packet, and note that this is PPPoE rather than
9131 * XXX - this is a bit of a kludge. If we were to split the
9132 * compiler into a parser that parses an expression and
9133 * generates an expression tree, and a code generator that
9134 * takes an expression tree (which could come from our
9135 * parser or from some other parser) and generates BPF code,
9136 * we could perhaps make the offsets parameters of routines
9137 * and, in the handler for an "AND" node, pass to subnodes
9138 * other than the PPPoE node the adjusted offsets.
9140 * This would mean that "pppoes" would, instead of changing the
9141 * behavior of *all* tests after it, change only the behavior
9142 * of tests ANDed with it. That would change the documented
9143 * semantics of "pppoes", which might break some expressions.
9144 * However, it would mean that "(pppoes and ip) or ip" would check
9145 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9146 * checking only for VLAN-encapsulated IP, so that could still
9147 * be considered worth doing; it wouldn't break expressions
9148 * that are of the form "pppoes and ..." which I suspect are the
9149 * most common expressions involving "pppoes". "pppoes or ..."
9150 * doesn't necessarily do what the user would really want, now,
9151 * as all the "or ..." tests would be done assuming PPPoE, even
9152 * though the "or" could be viewed as meaning "or, if this isn't
9153 * a PPPoE packet...".
9155 * The "network-layer" protocol is PPPoE, which has a 6-byte
9156 * PPPoE header, followed by a PPP packet.
9158 * There is no HDLC encapsulation for the PPP packet (it's
9159 * encapsulated in PPPoES instead), so the link-layer type
9160 * starts at the first byte of the PPP packet. For PPPoE,
9161 * that offset is relative to the beginning of the total
9162 * link-layer payload, including any 802.2 LLC header, so
9163 * it's 6 bytes past cstate->off_nl.
9165 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9166 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9167 cstate
->off_linkpl
.reg
);
9169 cstate
->off_linktype
= cstate
->off_linkhdr
;
9170 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9173 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9178 /* Check that this is Geneve and the VNI is correct if
9179 * specified. Parameterized to handle both IPv4 and IPv6. */
9180 static struct block
*
9181 gen_geneve_check(compiler_state_t
*cstate
,
9182 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
9183 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9185 struct block
*b0
, *b1
;
9187 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9189 /* Check that we are operating on version 0. Otherwise, we
9190 * can't decode the rest of the fields. The version is 2 bits
9191 * in the first byte of the Geneve header. */
9192 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
9197 if (vni
> 0xffffff) {
9198 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9201 vni
<<= 8; /* VNI is in the upper 3 bytes */
9202 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
9211 /* The IPv4 and IPv6 Geneve checks need to do two things:
9212 * - Verify that this actually is Geneve with the right VNI.
9213 * - Place the IP header length (plus variable link prefix if
9214 * needed) into register A to be used later to compute
9215 * the inner packet offsets. */
9216 static struct block
*
9217 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9219 struct block
*b0
, *b1
;
9220 struct slist
*s
, *s1
;
9222 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9224 /* Load the IP header length into A. */
9225 s
= gen_loadx_iphdrlen(cstate
);
9227 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9230 /* Forcibly append these statements to the true condition
9231 * of the protocol check by creating a new block that is
9232 * always true and ANDing them. */
9233 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9242 static struct block
*
9243 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9245 struct block
*b0
, *b1
;
9246 struct slist
*s
, *s1
;
9248 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9250 /* Load the IP header length. We need to account for a
9251 * variable length link prefix if there is one. */
9252 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9254 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9258 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9262 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9266 /* Forcibly append these statements to the true condition
9267 * of the protocol check by creating a new block that is
9268 * always true and ANDing them. */
9269 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9272 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9281 /* We need to store three values based on the Geneve header::
9282 * - The offset of the linktype.
9283 * - The offset of the end of the Geneve header.
9284 * - The offset of the end of the encapsulated MAC header. */
9285 static struct slist
*
9286 gen_geneve_offsets(compiler_state_t
*cstate
)
9288 struct slist
*s
, *s1
, *s_proto
;
9290 /* First we need to calculate the offset of the Geneve header
9291 * itself. This is composed of the IP header previously calculated
9292 * (include any variable link prefix) and stored in A plus the
9293 * fixed sized headers (fixed link prefix, MAC length, and UDP
9295 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9296 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9298 /* Stash this in X since we'll need it later. */
9299 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9302 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9304 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9308 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9309 cstate
->off_linktype
.is_variable
= 1;
9310 cstate
->off_linktype
.constant_part
= 0;
9312 s1
= new_stmt(cstate
, BPF_ST
);
9313 s1
->s
.k
= cstate
->off_linktype
.reg
;
9316 /* Load the Geneve option length and mask and shift to get the
9317 * number of bytes. It is stored in the first byte of the Geneve
9319 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9323 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9327 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9331 /* Add in the rest of the Geneve base header. */
9332 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9336 /* Add the Geneve header length to its offset and store. */
9337 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9341 /* Set the encapsulated type as Ethernet. Even though we may
9342 * not actually have Ethernet inside there are two reasons this
9344 * - The linktype field is always in EtherType format regardless
9345 * of whether it is in Geneve or an inner Ethernet frame.
9346 * - The only link layer that we have specific support for is
9347 * Ethernet. We will confirm that the packet actually is
9348 * Ethernet at runtime before executing these checks. */
9349 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9351 s1
= new_stmt(cstate
, BPF_ST
);
9352 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9355 /* Calculate whether we have an Ethernet header or just raw IP/
9356 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9357 * and linktype by 14 bytes so that the network header can be found
9358 * seamlessly. Otherwise, keep what we've calculated already. */
9360 /* We have a bare jmp so we can't use the optimizer. */
9361 cstate
->no_optimize
= 1;
9363 /* Load the EtherType in the Geneve header, 2 bytes in. */
9364 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9368 /* Load X with the end of the Geneve header. */
9369 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9370 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9373 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9374 * end of this check, we should have the total length in X. In
9375 * the non-Ethernet case, it's already there. */
9376 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9377 s_proto
->s
.k
= ETHERTYPE_TEB
;
9378 sappend(s
, s_proto
);
9380 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9384 /* Since this is Ethernet, use the EtherType of the payload
9385 * directly as the linktype. Overwrite what we already have. */
9386 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9390 s1
= new_stmt(cstate
, BPF_ST
);
9391 s1
->s
.k
= cstate
->off_linktype
.reg
;
9394 /* Advance two bytes further to get the end of the Ethernet
9396 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9400 /* Move the result to X. */
9401 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9404 /* Store the final result of our linkpl calculation. */
9405 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9406 cstate
->off_linkpl
.is_variable
= 1;
9407 cstate
->off_linkpl
.constant_part
= 0;
9409 s1
= new_stmt(cstate
, BPF_STX
);
9410 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9419 /* Check to see if this is a Geneve packet. */
9421 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9423 struct block
*b0
, *b1
;
9427 * Catch errors reported by us and routines below us, and return NULL
9430 if (setjmp(cstate
->top_ctx
))
9433 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9434 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9439 /* Later filters should act on the payload of the Geneve frame,
9440 * update all of the header pointers. Attach this code so that
9441 * it gets executed in the event that the Geneve filter matches. */
9442 s
= gen_geneve_offsets(cstate
);
9444 b1
= gen_true(cstate
);
9445 sappend(s
, b1
->stmts
);
9450 cstate
->is_geneve
= 1;
9455 /* Check that the encapsulated frame has a link layer header
9456 * for Ethernet filters. */
9457 static struct block
*
9458 gen_geneve_ll_check(compiler_state_t
*cstate
)
9461 struct slist
*s
, *s1
;
9463 /* The easiest way to see if there is a link layer present
9464 * is to check if the link layer header and payload are not
9467 /* Geneve always generates pure variable offsets so we can
9468 * compare only the registers. */
9469 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9470 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9472 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9473 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9476 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9484 static struct block
*
9485 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9486 bpf_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9493 if (!cstate
->is_atm
)
9494 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9495 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9497 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
9502 if (!cstate
->is_atm
)
9503 bpf_error(cstate
, "'vci' supported only on raw ATM");
9504 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9506 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
9511 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9512 abort(); /* XXX - this isn't on FreeBSD */
9513 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
9518 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9520 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9521 0xffffffff, jtype
, reverse
, jvalue
);
9525 if (!cstate
->is_atm
)
9526 bpf_error(cstate
, "'callref' supported only on raw ATM");
9527 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9529 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
9530 jtype
, reverse
, jvalue
);
9539 static struct block
*
9540 gen_atmtype_metac(compiler_state_t
*cstate
)
9542 struct block
*b0
, *b1
;
9544 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9545 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9550 static struct block
*
9551 gen_atmtype_sc(compiler_state_t
*cstate
)
9553 struct block
*b0
, *b1
;
9555 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9556 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9561 static struct block
*
9562 gen_atmtype_llc(compiler_state_t
*cstate
)
9566 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9567 cstate
->linktype
= cstate
->prevlinktype
;
9572 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9573 bpf_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9576 * Catch errors reported by us and routines below us, and return NULL
9579 if (setjmp(cstate
->top_ctx
))
9582 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9587 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9589 struct block
*b0
, *b1
;
9592 * Catch errors reported by us and routines below us, and return NULL
9595 if (setjmp(cstate
->top_ctx
))
9601 /* Get all packets in Meta signalling Circuit */
9602 if (!cstate
->is_atm
)
9603 bpf_error(cstate
, "'metac' supported only on raw ATM");
9604 b1
= gen_atmtype_metac(cstate
);
9608 /* Get all packets in Broadcast Circuit*/
9609 if (!cstate
->is_atm
)
9610 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9611 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9612 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9617 /* Get all cells in Segment OAM F4 circuit*/
9618 if (!cstate
->is_atm
)
9619 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9620 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9621 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9626 /* Get all cells in End-to-End OAM F4 Circuit*/
9627 if (!cstate
->is_atm
)
9628 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9629 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9630 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9635 /* Get all packets in connection Signalling Circuit */
9636 if (!cstate
->is_atm
)
9637 bpf_error(cstate
, "'sc' supported only on raw ATM");
9638 b1
= gen_atmtype_sc(cstate
);
9642 /* Get all packets in ILMI Circuit */
9643 if (!cstate
->is_atm
)
9644 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9645 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9646 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9651 /* Get all LANE packets */
9652 if (!cstate
->is_atm
)
9653 bpf_error(cstate
, "'lane' supported only on raw ATM");
9654 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9657 * Arrange that all subsequent tests assume LANE
9658 * rather than LLC-encapsulated packets, and set
9659 * the offsets appropriately for LANE-encapsulated
9662 * We assume LANE means Ethernet, not Token Ring.
9664 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9665 cstate
->off_payload
+ 2, /* Ethernet header */
9667 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9668 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9669 cstate
->off_nl
= 0; /* Ethernet II */
9670 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9674 /* Get all LLC-encapsulated packets */
9675 if (!cstate
->is_atm
)
9676 bpf_error(cstate
, "'llc' supported only on raw ATM");
9677 b1
= gen_atmtype_llc(cstate
);
9687 * Filtering for MTP2 messages based on li value
9688 * FISU, length is null
9689 * LSSU, length is 1 or 2
9690 * MSU, length is 3 or more
9691 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9694 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9696 struct block
*b0
, *b1
;
9699 * Catch errors reported by us and routines below us, and return NULL
9702 if (setjmp(cstate
->top_ctx
))
9708 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9709 (cstate
->linktype
!= DLT_ERF
) &&
9710 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9711 bpf_error(cstate
, "'fisu' supported only on MTP2");
9712 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9713 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
9717 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9718 (cstate
->linktype
!= DLT_ERF
) &&
9719 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9720 bpf_error(cstate
, "'lssu' supported only on MTP2");
9721 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
9722 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9727 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9728 (cstate
->linktype
!= DLT_ERF
) &&
9729 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9730 bpf_error(cstate
, "'msu' supported only on MTP2");
9731 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9735 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9736 (cstate
->linktype
!= DLT_ERF
) &&
9737 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9738 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9739 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9740 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9744 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9745 (cstate
->linktype
!= DLT_ERF
) &&
9746 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9747 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9748 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9749 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9754 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9755 (cstate
->linktype
!= DLT_ERF
) &&
9756 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9757 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9758 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9768 * The jvalue_arg dance is to avoid annoying whining by compilers that
9769 * jvalue might be clobbered by longjmp - yeah, it might, but *WHO CARES*?
9770 * It's not *used* after setjmp returns.
9773 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9774 bpf_u_int32 jvalue_arg
, bpf_u_int32 jtype
, int reverse
)
9777 bpf_u_int32 val1
, val2
, val3
;
9778 bpf_u_int32 jvalue
= jvalue_arg
;
9785 * Catch errors reported by us and routines below us, and return NULL
9788 if (setjmp(cstate
->top_ctx
))
9791 newoff_sio
= cstate
->off_sio
;
9792 newoff_opc
= cstate
->off_opc
;
9793 newoff_dpc
= cstate
->off_dpc
;
9794 newoff_sls
= cstate
->off_sls
;
9795 switch (mtp3field
) {
9798 newoff_sio
+= 3; /* offset for MTP2_HSL */
9802 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9803 bpf_error(cstate
, "'sio' supported only on SS7");
9804 /* sio coded on 1 byte so max value 255 */
9806 bpf_error(cstate
, "sio value %u too big; max value = 255",
9808 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9809 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9815 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9816 bpf_error(cstate
, "'opc' supported only on SS7");
9817 /* opc coded on 14 bits so max value 16383 */
9819 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9821 /* the following instructions are made to convert jvalue
9822 * to the form used to write opc in an ss7 message*/
9823 val1
= jvalue
& 0x00003c00;
9825 val2
= jvalue
& 0x000003fc;
9827 val3
= jvalue
& 0x00000003;
9829 jvalue
= val1
+ val2
+ val3
;
9830 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9831 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9839 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9840 bpf_error(cstate
, "'dpc' supported only on SS7");
9841 /* dpc coded on 14 bits so max value 16383 */
9843 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9845 /* the following instructions are made to convert jvalue
9846 * to the forme used to write dpc in an ss7 message*/
9847 val1
= jvalue
& 0x000000ff;
9849 val2
= jvalue
& 0x00003f00;
9851 jvalue
= val1
+ val2
;
9852 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9853 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9859 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9860 bpf_error(cstate
, "'sls' supported only on SS7");
9861 /* sls coded on 4 bits so max value 15 */
9863 bpf_error(cstate
, "sls value %u too big; max value = 15",
9865 /* the following instruction is made to convert jvalue
9866 * to the forme used to write sls in an ss7 message*/
9867 jvalue
= jvalue
<< 4;
9868 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9869 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9878 static struct block
*
9879 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9884 * Q.2931 signalling protocol messages for handling virtual circuits
9885 * establishment and teardown
9890 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9894 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9898 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9902 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9906 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9909 case A_RELEASE_DONE
:
9910 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9920 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9922 struct block
*b0
, *b1
;
9925 * Catch errors reported by us and routines below us, and return NULL
9928 if (setjmp(cstate
->top_ctx
))
9934 if (!cstate
->is_atm
)
9935 bpf_error(cstate
, "'oam' supported only on raw ATM");
9937 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9938 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9940 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9945 if (!cstate
->is_atm
)
9946 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9948 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9949 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9951 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9957 * Get Q.2931 signalling messages for switched
9958 * virtual connection
9960 if (!cstate
->is_atm
)
9961 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9962 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9963 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9965 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9967 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9969 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9971 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9973 b0
= gen_atmtype_sc(cstate
);
9978 if (!cstate
->is_atm
)
9979 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9980 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9981 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9983 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9985 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9987 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9989 b0
= gen_atmtype_metac(cstate
);