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>
54 #include "ethertype.h"
58 #include "ieee80211.h"
60 #include "sunatmpos.h"
63 #include "pcap/ipnet.h"
69 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
70 #include <linux/types.h>
71 #include <linux/if_packet.h>
72 #include <linux/filter.h>
75 #ifdef HAVE_NET_PFVAR_H
76 #include <sys/socket.h>
78 #include <net/pfvar.h>
79 #include <net/if_pflog.h>
83 #define offsetof(s, e) ((size_t)&((s *)0)->e)
88 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
95 uint16_t u6_addr16
[8];
96 uint32_t u6_addr32
[4];
98 #define s6_addr in6_u.u6_addr8
99 #define s6_addr16 in6_u.u6_addr16
100 #define s6_addr32 in6_u.u6_addr32
101 #define s6_addr64 in6_u.u6_addr64
104 typedef unsigned short sa_family_t
;
106 #define __SOCKADDR_COMMON(sa_prefix) \
107 sa_family_t sa_prefix##family
109 /* Ditto, for IPv6. */
112 __SOCKADDR_COMMON (sin6_
);
113 uint16_t sin6_port
; /* Transport layer port # */
114 uint32_t sin6_flowinfo
; /* IPv6 flow information */
115 struct in6_addr sin6_addr
; /* IPv6 address */
118 #ifndef EAI_ADDRFAMILY
120 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
121 int ai_family
; /* PF_xxx */
122 int ai_socktype
; /* SOCK_xxx */
123 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
124 size_t ai_addrlen
; /* length of ai_addr */
125 char *ai_canonname
; /* canonical name for hostname */
126 struct sockaddr
*ai_addr
; /* binary address */
127 struct addrinfo
*ai_next
; /* next structure in linked list */
129 #endif /* EAI_ADDRFAMILY */
130 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
133 #include <netdb.h> /* for "struct addrinfo" */
135 #include <pcap/namedb.h>
137 #include "nametoaddr.h"
139 #define ETHERMTU 1500
141 #ifndef ETHERTYPE_TEB
142 #define ETHERTYPE_TEB 0x6558
145 #ifndef IPPROTO_HOPOPTS
146 #define IPPROTO_HOPOPTS 0
148 #ifndef IPPROTO_ROUTING
149 #define IPPROTO_ROUTING 43
151 #ifndef IPPROTO_FRAGMENT
152 #define IPPROTO_FRAGMENT 44
154 #ifndef IPPROTO_DSTOPTS
155 #define IPPROTO_DSTOPTS 60
158 #define IPPROTO_SCTP 132
161 #define GENEVE_PORT 6081
163 #ifdef HAVE_OS_PROTO_H
164 #include "os-proto.h"
167 #define JMP(c) ((c)|BPF_JMP|BPF_K)
170 * "Push" the current value of the link-layer header type and link-layer
171 * header offset onto a "stack", and set a new value. (It's not a
172 * full-blown stack; we keep only the top two items.)
174 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
176 (cs)->cgstate->prevlinktype = (cs)->cgstate->linktype; \
177 (cs)->cgstate->off_prevlinkhdr = (cs)->cgstate->off_linkhdr; \
178 (cs)->cgstate->linktype = (new_linktype); \
179 (cs)->cgstate->off_linkhdr.is_variable = (new_is_variable); \
180 (cs)->cgstate->off_linkhdr.constant_part = (new_constant_part); \
181 (cs)->cgstate->off_linkhdr.reg = (new_reg); \
182 (cs)->cgstate->is_geneve = 0; \
186 * Offset "not set" value.
188 #define OFFSET_NOT_SET 0xffffffffU
191 * Absolute offsets, which are offsets from the beginning of the raw
192 * packet data, are, in the general case, the sum of a variable value
193 * and a constant value; the variable value may be absent, in which
194 * case the offset is only the constant value, and the constant value
195 * may be zero, in which case the offset is only the variable value.
197 * bpf_abs_offset is a structure containing all that information:
199 * is_variable is 1 if there's a variable part.
201 * constant_part is the constant part of the value, possibly zero;
203 * if is_variable is 1, reg is the register number for a register
204 * containing the variable value if the register has been assigned,
214 * Value passed to gen_load_a() to indicate what the offset argument
215 * is relative to the beginning of.
218 OR_PACKET
, /* full packet data */
219 OR_LINKHDR
, /* link-layer header */
220 OR_PREVLINKHDR
, /* previous link-layer header */
221 OR_LLC
, /* 802.2 LLC header */
222 OR_PREVMPLSHDR
, /* previous MPLS header */
223 OR_LINKTYPE
, /* link-layer type */
224 OR_LINKPL
, /* link-layer payload */
225 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
226 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
227 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
231 * We divy out chunks of memory rather than call malloc each time so
232 * we don't have to worry about leaking memory. It's probably
233 * not a big deal if all this memory was wasted but if this ever
234 * goes into a library that would probably not be a good idea.
236 * XXX - this *is* in a library....
239 #define CHUNK0SIZE 1024
245 /* Code generator state */
247 struct _codegen_state
{
254 int outermostlinktype
;
259 /* Hack for handling VLAN and MPLS stacks. */
260 u_int label_stack_depth
;
261 u_int vlan_stack_depth
;
267 * As errors are handled by a longjmp, anything allocated must
268 * be freed in the longjmp handler, so it must be reachable
271 * One thing that's allocated is the result of pcap_nametoaddrinfo();
272 * it must be freed with freeaddrinfo(). This variable points to
273 * any addrinfo structure that would need to be freed.
278 * Another thing that's allocated is the result of pcap_ether_aton();
279 * it must be freed with free(). This variable points to any
280 * address that would need to be freed.
285 * Various code constructs need to know the layout of the packet.
286 * These values give the necessary offsets from the beginning
287 * of the packet data.
291 * Absolute offset of the beginning of the link-layer header.
293 bpf_abs_offset off_linkhdr
;
296 * If we're checking a link-layer header for a packet encapsulated
297 * in another protocol layer, this is the equivalent information
298 * for the previous layers' link-layer header from the beginning
299 * of the raw packet data.
301 bpf_abs_offset off_prevlinkhdr
;
304 * This is the equivalent information for the outermost layers'
307 bpf_abs_offset off_outermostlinkhdr
;
310 * Absolute offset of the beginning of the link-layer payload.
312 bpf_abs_offset off_linkpl
;
315 * "off_linktype" is the offset to information in the link-layer
316 * header giving the packet type. This is an absolute offset
317 * from the beginning of the packet.
319 * For Ethernet, it's the offset of the Ethernet type field; this
320 * means that it must have a value that skips VLAN tags.
322 * For link-layer types that always use 802.2 headers, it's the
323 * offset of the LLC header; this means that it must have a value
324 * that skips VLAN tags.
326 * For PPP, it's the offset of the PPP type field.
328 * For Cisco HDLC, it's the offset of the CHDLC type field.
330 * For BSD loopback, it's the offset of the AF_ value.
332 * For Linux cooked sockets, it's the offset of the type field.
334 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
335 * encapsulation, in which case, IP is assumed.
337 bpf_abs_offset off_linktype
;
340 * TRUE if the link layer includes an ATM pseudo-header.
345 * TRUE if "geneve" appeared in the filter; it causes us to
346 * generate code that checks for a Geneve header and assume
347 * that later filters apply to the encapsulated payload.
352 * TRUE if we need variable length part of VLAN offset
354 int is_vlan_vloffset
;
357 * These are offsets for the ATM pseudo-header.
364 * These are offsets for the MTP2 fields.
370 * These are offsets for the MTP3 fields.
378 * This is the offset of the first byte after the ATM pseudo_header,
379 * or -1 if there is no ATM pseudo-header.
384 * These are offsets to the beginning of the network-layer header.
385 * They are relative to the beginning of the link-layer payload
386 * (i.e., they don't include off_linkhdr.constant_part or
387 * off_linkpl.constant_part).
389 * If the link layer never uses 802.2 LLC:
391 * "off_nl" and "off_nl_nosnap" are the same.
393 * If the link layer always uses 802.2 LLC:
395 * "off_nl" is the offset if there's a SNAP header following
398 * "off_nl_nosnap" is the offset if there's no SNAP header.
400 * If the link layer is Ethernet:
402 * "off_nl" is the offset if the packet is an Ethernet II packet
403 * (we assume no 802.3+802.2+SNAP);
405 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
406 * with an 802.2 header following it.
412 * Here we handle simple allocation of the scratch registers.
413 * If too many registers are alloc'd, the allocator punts.
415 int regused
[BPF_MEMWORDS
];
421 struct chunk chunks
[NCHUNKS
];
426 bpf_parser_error(compiler_state_t
*cstate
, const char *msg
)
428 bpf_error(cstate
, "can't parse filter expression: %s", msg
);
434 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
439 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
442 longjmp(cstate
->top_ctx
, 1);
446 static void init_linktype(compiler_state_t
*, pcap_t
*);
448 static void init_regs(compiler_state_t
*);
449 static int alloc_reg(compiler_state_t
*);
450 static void free_reg(compiler_state_t
*, int);
452 static void initchunks(compiler_state_t
*cstate
);
453 static void *newchunk(compiler_state_t
*cstate
, size_t);
454 static void freechunks(compiler_state_t
*cstate
);
455 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
456 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
457 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
458 static inline void syntax(compiler_state_t
*cstate
);
460 static void backpatch(struct block
*, struct block
*);
461 static void merge(struct block
*, struct block
*);
462 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
464 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
466 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
468 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
470 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
472 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
473 u_int
, bpf_int32
, bpf_u_int32
);
474 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
475 u_int
, const u_char
*);
476 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
477 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
478 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
480 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
482 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
483 static struct block
*gen_uncond(compiler_state_t
*, int);
484 static inline struct block
*gen_true(compiler_state_t
*);
485 static inline struct block
*gen_false(compiler_state_t
*);
486 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
487 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
488 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
489 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
490 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
491 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
492 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
493 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
494 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
496 static int ethertype_to_ppptype(int);
497 static struct block
*gen_linktype(compiler_state_t
*, int);
498 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
499 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
500 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
501 int, int, u_int
, u_int
);
503 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
504 struct in6_addr
*, int, int, u_int
, u_int
);
506 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
507 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
508 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
509 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
510 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
511 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
512 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
513 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
514 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
517 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
518 struct in6_addr
*, int, int, int);
521 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
522 struct addrinfo
*, int, int);
524 static struct block
*gen_ipfrag(compiler_state_t
*);
525 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
526 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
528 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
529 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
531 struct block
*gen_portop(compiler_state_t
*, int, int, int);
532 static struct block
*gen_port(compiler_state_t
*, int, int, int);
533 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
534 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
535 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
536 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
537 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
538 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
539 static int lookup_proto(compiler_state_t
*, const char *, int);
540 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
541 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
542 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
543 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
544 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
545 static struct block
*gen_len(compiler_state_t
*, int, int);
546 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
547 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
549 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
550 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
553 initchunks(compiler_state_t
*cstate
)
557 for (i
= 0; i
< NCHUNKS
; i
++) {
558 cstate
->cgstate
->chunks
[i
].n_left
= 0;
559 cstate
->cgstate
->chunks
[i
].m
= NULL
;
561 cstate
->cgstate
->cur_chunk
= 0;
565 newchunk(compiler_state_t
*cstate
, size_t n
)
572 /* XXX Round up to nearest long. */
573 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
575 /* XXX Round up to structure boundary. */
579 cp
= &cstate
->cgstate
->chunks
[cstate
->cgstate
->cur_chunk
];
580 if (n
> cp
->n_left
) {
582 k
= ++cstate
->cgstate
->cur_chunk
;
584 bpf_error(cstate
, "out of memory");
585 size
= CHUNK0SIZE
<< k
;
586 cp
->m
= (void *)malloc(size
);
588 bpf_error(cstate
, "out of memory");
589 memset((char *)cp
->m
, 0, size
);
592 bpf_error(cstate
, "out of memory");
595 return (void *)((char *)cp
->m
+ cp
->n_left
);
599 freechunks(compiler_state_t
*cstate
)
603 for (i
= 0; i
< NCHUNKS
; ++i
)
604 if (cstate
->cgstate
->chunks
[i
].m
!= NULL
)
605 free(cstate
->cgstate
->chunks
[i
].m
);
609 * A strdup whose allocations are freed after code generation is over.
612 sdup(compiler_state_t
*cstate
, const char *s
)
614 size_t n
= strlen(s
) + 1;
615 char *cp
= newchunk(cstate
, n
);
617 pcap_strlcpy(cp
, s
, n
);
621 static inline struct block
*
622 new_block(compiler_state_t
*cstate
, int code
)
626 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
633 static inline struct slist
*
634 new_stmt(compiler_state_t
*cstate
, int code
)
638 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
644 static struct block
*
645 gen_retblk(compiler_state_t
*cstate
, int v
)
647 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
653 static inline PCAP_NORETURN_DEF
void
654 syntax(compiler_state_t
*cstate
)
656 bpf_error(cstate
, "syntax error in filter expression");
660 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
661 const char *buf
, int optimize
, bpf_u_int32 mask
)
666 compiler_state_t cstate
;
667 codegen_state_t cgstate
;
668 const char * volatile xbuf
= buf
;
669 yyscan_t scanner
= NULL
;
670 volatile YY_BUFFER_STATE in_buffer
= NULL
;
675 * If this pcap_t hasn't been activated, it doesn't have a
676 * link-layer type, so we can't use it.
679 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
680 "not-yet-activated pcap_t passed to pcap_compile");
692 * If the device on which we're capturing need to be notified
693 * that a new filter is being compiled, do so.
695 * This allows them to save a copy of it, in case, for example,
696 * they're implementing a form of remote packet capture, and
697 * want the remote machine to filter out the packets in which
698 * it's sending the packets it's captured.
700 * XXX - the fact that we happen to be compiling a filter
701 * doesn't necessarily mean we'll be installing it as the
702 * filter for this pcap_t; we might be running it from userland
703 * on captured packets to do packet classification. We really
704 * need a better way of handling this, but this is all that
705 * the WinPcap code did.
707 if (p
->save_current_filter_op
!= NULL
)
708 (p
->save_current_filter_op
)(p
, buf
);
711 cstate
.cgstate
= &cgstate
;
713 cstate
.cgstate
->no_optimize
= 0;
715 cstate
.cgstate
->ai
= NULL
;
717 cstate
.cgstate
->e
= NULL
;
718 cstate
.cgstate
->ic
.root
= NULL
;
719 cstate
.cgstate
->ic
.cur_mark
= 0;
723 cstate
.cgstate
->netmask
= mask
;
725 cstate
.cgstate
->snaplen
= pcap_snapshot(p
);
726 if (cstate
.cgstate
->snaplen
== 0) {
727 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
728 "snaplen of 0 rejects all packets");
733 if (pcap_lex_init(&scanner
) != 0)
734 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
735 errno
, "can't initialize scanner");
736 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
739 * Associate the compiler state with the lexical analyzer
742 pcap_set_extra(&cstate
, scanner
);
744 init_linktype(&cstate
, p
);
745 if (pcap_parse(scanner
, &cstate
) != 0) {
747 if (cstate
.cgstate
->ai
!= NULL
)
748 freeaddrinfo(cstate
.cgstate
->ai
);
750 if (cstate
.cgstate
->e
!= NULL
)
751 free(cstate
.cgstate
->e
);
756 if (cstate
.cgstate
->ic
.root
== NULL
)
757 cstate
.cgstate
->ic
.root
= gen_retblk(&cstate
, cstate
.cgstate
->snaplen
);
759 if (optimize
&& !cstate
.cgstate
->no_optimize
) {
760 if (bpf_optimize(&cstate
.cgstate
->ic
, p
->errbuf
) == -1) {
765 if (cstate
.cgstate
->ic
.root
== NULL
||
766 (cstate
.cgstate
->ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.cgstate
->ic
.root
->s
.k
== 0)) {
767 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
768 "expression rejects all packets");
773 program
->bf_insns
= icode_to_fcode(&cstate
.cgstate
->ic
,
774 cstate
.cgstate
->ic
.root
, &len
, p
->errbuf
);
775 if (program
->bf_insns
== NULL
) {
780 program
->bf_len
= len
;
782 rc
= 0; /* We're all okay */
786 * Clean up everything for the lexical analyzer.
788 if (in_buffer
!= NULL
)
789 pcap__delete_buffer(in_buffer
, scanner
);
791 pcap_lex_destroy(scanner
);
794 * Clean up our own allocated memory.
802 * entry point for using the compiler with no pcap open
803 * pass in all the stuff that is needed explicitly instead.
806 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
807 struct bpf_program
*program
,
808 const char *buf
, int optimize
, bpf_u_int32 mask
)
813 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
816 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
822 * Clean up a "struct bpf_program" by freeing all the memory allocated
826 pcap_freecode(struct bpf_program
*program
)
829 if (program
->bf_insns
!= NULL
) {
830 free((char *)program
->bf_insns
);
831 program
->bf_insns
= NULL
;
836 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
837 * which of the jt and jf fields has been resolved and which is a pointer
838 * back to another unresolved block (or nil). At least one of the fields
839 * in each block is already resolved.
842 backpatch(struct block
*list
, struct block
*target
)
859 * Merge the lists in b0 and b1, using the 'sense' field to indicate
860 * which of jt and jf is the link.
863 merge(struct block
*b0
, struct block
*b1
)
865 register struct block
**p
= &b0
;
867 /* Find end of list. */
869 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
871 /* Concatenate the lists. */
876 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
878 struct block
*ppi_dlt_check
;
881 * Insert before the statements of the first (root) block any
882 * statements needed to load the lengths of any variable-length
883 * headers into registers.
885 * XXX - a fancier strategy would be to insert those before the
886 * statements of all blocks that use those lengths and that
887 * have no predecessors that use them, so that we only compute
888 * the lengths if we need them. There might be even better
889 * approaches than that.
891 * However, those strategies would be more complicated, and
892 * as we don't generate code to compute a length if the
893 * program has no tests that use the length, and as most
894 * tests will probably use those lengths, we would just
895 * postpone computing the lengths so that it's not done
896 * for tests that fail early, and it's not clear that's
899 insert_compute_vloffsets(cstate
, p
->head
);
902 * For DLT_PPI captures, generate a check of the per-packet
903 * DLT value to make sure it's DLT_IEEE802_11.
905 * XXX - TurboCap cards use DLT_PPI for Ethernet.
906 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
907 * with appropriate Ethernet information and use that rather
908 * than using something such as DLT_PPI where you don't know
909 * the link-layer header type until runtime, which, in the
910 * general case, would force us to generate both Ethernet *and*
911 * 802.11 code (*and* anything else for which PPI is used)
912 * and choose between them early in the BPF program?
914 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
915 if (ppi_dlt_check
!= NULL
)
916 gen_and(ppi_dlt_check
, p
);
918 backpatch(p
, gen_retblk(cstate
, cstate
->cgstate
->snaplen
));
919 p
->sense
= !p
->sense
;
920 backpatch(p
, gen_retblk(cstate
, 0));
921 cstate
->cgstate
->ic
.root
= p
->head
;
925 gen_and(struct block
*b0
, struct block
*b1
)
927 backpatch(b0
, b1
->head
);
928 b0
->sense
= !b0
->sense
;
929 b1
->sense
= !b1
->sense
;
931 b1
->sense
= !b1
->sense
;
936 gen_or(struct block
*b0
, struct block
*b1
)
938 b0
->sense
= !b0
->sense
;
939 backpatch(b0
, b1
->head
);
940 b0
->sense
= !b0
->sense
;
946 gen_not(struct block
*b
)
948 b
->sense
= !b
->sense
;
951 static struct block
*
952 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
953 u_int size
, bpf_int32 v
)
955 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
958 static struct block
*
959 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
960 u_int size
, bpf_int32 v
)
962 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
965 static struct block
*
966 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
967 u_int size
, bpf_int32 v
)
969 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
972 static struct block
*
973 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
974 u_int size
, bpf_int32 v
)
976 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
979 static struct block
*
980 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
981 u_int size
, bpf_int32 v
)
983 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
986 static struct block
*
987 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
988 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
990 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
993 static struct block
*
994 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
995 u_int size
, const u_char
*v
)
997 register struct block
*b
, *tmp
;
1000 * XXX - the actual *instructions* do unsigned comparisons on
1001 * most platforms, and the load instructions don't do sign
1002 * extension, so gen_cmp() should really take an unsigned
1005 * As the load instructons also don't do sign-extension, we
1006 * fetch the values from the byte array as unsigned. We don't
1007 * want to use the signed versions of the extract calls.
1011 register const u_char
*p
= &v
[size
- 4];
1013 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1014 (bpf_int32
)EXTRACT_BE_U_4(p
));
1021 register const u_char
*p
= &v
[size
- 2];
1023 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1024 (bpf_int32
)EXTRACT_BE_U_2(p
));
1031 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1040 * AND the field of size "size" at offset "offset" relative to the header
1041 * specified by "offrel" with "mask", and compare it with the value "v"
1042 * with the test specified by "jtype"; if "reverse" is true, the test
1043 * should test the opposite of "jtype".
1045 static struct block
*
1046 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1047 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1050 struct slist
*s
, *s2
;
1053 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1055 if (mask
!= 0xffffffff) {
1056 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1061 b
= new_block(cstate
, JMP(jtype
));
1064 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1070 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1072 cstate
->cgstate
->pcap_fddipad
= p
->fddipad
;
1075 * We start out with only one link-layer header.
1077 cstate
->cgstate
->outermostlinktype
= pcap_datalink(p
);
1078 cstate
->cgstate
->off_outermostlinkhdr
.constant_part
= 0;
1079 cstate
->cgstate
->off_outermostlinkhdr
.is_variable
= 0;
1080 cstate
->cgstate
->off_outermostlinkhdr
.reg
= -1;
1082 cstate
->cgstate
->prevlinktype
= cstate
->cgstate
->outermostlinktype
;
1083 cstate
->cgstate
->off_prevlinkhdr
.constant_part
= 0;
1084 cstate
->cgstate
->off_prevlinkhdr
.is_variable
= 0;
1085 cstate
->cgstate
->off_prevlinkhdr
.reg
= -1;
1087 cstate
->cgstate
->linktype
= cstate
->cgstate
->outermostlinktype
;
1088 cstate
->cgstate
->off_linkhdr
.constant_part
= 0;
1089 cstate
->cgstate
->off_linkhdr
.is_variable
= 0;
1090 cstate
->cgstate
->off_linkhdr
.reg
= -1;
1095 cstate
->cgstate
->off_linkpl
.constant_part
= 0;
1096 cstate
->cgstate
->off_linkpl
.is_variable
= 0;
1097 cstate
->cgstate
->off_linkpl
.reg
= -1;
1099 cstate
->cgstate
->off_linktype
.constant_part
= 0;
1100 cstate
->cgstate
->off_linktype
.is_variable
= 0;
1101 cstate
->cgstate
->off_linktype
.reg
= -1;
1104 * Assume it's not raw ATM with a pseudo-header, for now.
1106 cstate
->cgstate
->is_atm
= 0;
1107 cstate
->cgstate
->off_vpi
= OFFSET_NOT_SET
;
1108 cstate
->cgstate
->off_vci
= OFFSET_NOT_SET
;
1109 cstate
->cgstate
->off_proto
= OFFSET_NOT_SET
;
1110 cstate
->cgstate
->off_payload
= OFFSET_NOT_SET
;
1115 cstate
->cgstate
->is_geneve
= 0;
1118 * No variable length VLAN offset by default
1120 cstate
->cgstate
->is_vlan_vloffset
= 0;
1123 * And assume we're not doing SS7.
1125 cstate
->cgstate
->off_li
= OFFSET_NOT_SET
;
1126 cstate
->cgstate
->off_li_hsl
= OFFSET_NOT_SET
;
1127 cstate
->cgstate
->off_sio
= OFFSET_NOT_SET
;
1128 cstate
->cgstate
->off_opc
= OFFSET_NOT_SET
;
1129 cstate
->cgstate
->off_dpc
= OFFSET_NOT_SET
;
1130 cstate
->cgstate
->off_sls
= OFFSET_NOT_SET
;
1132 cstate
->cgstate
->label_stack_depth
= 0;
1133 cstate
->cgstate
->vlan_stack_depth
= 0;
1135 switch (cstate
->cgstate
->linktype
) {
1138 cstate
->cgstate
->off_linktype
.constant_part
= 2;
1139 cstate
->cgstate
->off_linkpl
.constant_part
= 6;
1140 cstate
->cgstate
->off_nl
= 0; /* XXX in reality, variable! */
1141 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1144 case DLT_ARCNET_LINUX
:
1145 cstate
->cgstate
->off_linktype
.constant_part
= 4;
1146 cstate
->cgstate
->off_linkpl
.constant_part
= 8;
1147 cstate
->cgstate
->off_nl
= 0; /* XXX in reality, variable! */
1148 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1152 cstate
->cgstate
->off_linktype
.constant_part
= 12;
1153 cstate
->cgstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1154 cstate
->cgstate
->off_nl
= 0; /* Ethernet II */
1155 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1160 * SLIP doesn't have a link level type. The 16 byte
1161 * header is hacked into our SLIP driver.
1163 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1164 cstate
->cgstate
->off_linkpl
.constant_part
= 16;
1165 cstate
->cgstate
->off_nl
= 0;
1166 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1169 case DLT_SLIP_BSDOS
:
1170 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1171 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1173 cstate
->cgstate
->off_linkpl
.constant_part
= 24;
1174 cstate
->cgstate
->off_nl
= 0;
1175 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1180 cstate
->cgstate
->off_linktype
.constant_part
= 0;
1181 cstate
->cgstate
->off_linkpl
.constant_part
= 4;
1182 cstate
->cgstate
->off_nl
= 0;
1183 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1187 cstate
->cgstate
->off_linktype
.constant_part
= 0;
1188 cstate
->cgstate
->off_linkpl
.constant_part
= 12;
1189 cstate
->cgstate
->off_nl
= 0;
1190 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1195 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1196 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1197 cstate
->cgstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1198 cstate
->cgstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1199 cstate
->cgstate
->off_nl
= 0;
1200 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1205 * This does no include the Ethernet header, and
1206 * only covers session state.
1208 cstate
->cgstate
->off_linktype
.constant_part
= 6;
1209 cstate
->cgstate
->off_linkpl
.constant_part
= 8;
1210 cstate
->cgstate
->off_nl
= 0;
1211 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1215 cstate
->cgstate
->off_linktype
.constant_part
= 5;
1216 cstate
->cgstate
->off_linkpl
.constant_part
= 24;
1217 cstate
->cgstate
->off_nl
= 0;
1218 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 * FDDI doesn't really have a link-level type field.
1224 * We set "off_linktype" to the offset of the LLC header.
1226 * To check for Ethernet types, we assume that SSAP = SNAP
1227 * is being used and pick out the encapsulated Ethernet type.
1228 * XXX - should we generate code to check for SNAP?
1230 cstate
->cgstate
->off_linktype
.constant_part
= 13;
1231 cstate
->cgstate
->off_linktype
.constant_part
+= cstate
->cgstate
->pcap_fddipad
;
1232 cstate
->cgstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1233 cstate
->cgstate
->off_linkpl
.constant_part
+= cstate
->cgstate
->pcap_fddipad
;
1234 cstate
->cgstate
->off_nl
= 8; /* 802.2+SNAP */
1235 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.2 */
1240 * Token Ring doesn't really have a link-level type field.
1241 * We set "off_linktype" to the offset of the LLC header.
1243 * To check for Ethernet types, we assume that SSAP = SNAP
1244 * is being used and pick out the encapsulated Ethernet type.
1245 * XXX - should we generate code to check for SNAP?
1247 * XXX - the header is actually variable-length.
1248 * Some various Linux patched versions gave 38
1249 * as "off_linktype" and 40 as "off_nl"; however,
1250 * if a token ring packet has *no* routing
1251 * information, i.e. is not source-routed, the correct
1252 * values are 20 and 22, as they are in the vanilla code.
1254 * A packet is source-routed iff the uppermost bit
1255 * of the first byte of the source address, at an
1256 * offset of 8, has the uppermost bit set. If the
1257 * packet is source-routed, the total number of bytes
1258 * of routing information is 2 plus bits 0x1F00 of
1259 * the 16-bit value at an offset of 14 (shifted right
1260 * 8 - figure out which byte that is).
1262 cstate
->cgstate
->off_linktype
.constant_part
= 14;
1263 cstate
->cgstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1264 cstate
->cgstate
->off_nl
= 8; /* 802.2+SNAP */
1265 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.2 */
1268 case DLT_PRISM_HEADER
:
1269 case DLT_IEEE802_11_RADIO_AVS
:
1270 case DLT_IEEE802_11_RADIO
:
1271 cstate
->cgstate
->off_linkhdr
.is_variable
= 1;
1272 /* Fall through, 802.11 doesn't have a variable link
1273 * prefix but is otherwise the same. */
1275 case DLT_IEEE802_11
:
1277 * 802.11 doesn't really have a link-level type field.
1278 * We set "off_linktype.constant_part" to the offset of
1281 * To check for Ethernet types, we assume that SSAP = SNAP
1282 * is being used and pick out the encapsulated Ethernet type.
1283 * XXX - should we generate code to check for SNAP?
1285 * We also handle variable-length radio headers here.
1286 * The Prism header is in theory variable-length, but in
1287 * practice it's always 144 bytes long. However, some
1288 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1289 * sometimes or always supply an AVS header, so we
1290 * have to check whether the radio header is a Prism
1291 * header or an AVS header, so, in practice, it's
1294 cstate
->cgstate
->off_linktype
.constant_part
= 24;
1295 cstate
->cgstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1296 cstate
->cgstate
->off_linkpl
.is_variable
= 1;
1297 cstate
->cgstate
->off_nl
= 8; /* 802.2+SNAP */
1298 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.2 */
1303 * At the moment we treat PPI the same way that we treat
1304 * normal Radiotap encoded packets. The difference is in
1305 * the function that generates the code at the beginning
1306 * to compute the header length. Since this code generator
1307 * of PPI supports bare 802.11 encapsulation only (i.e.
1308 * the encapsulated DLT should be DLT_IEEE802_11) we
1309 * generate code to check for this too.
1311 cstate
->cgstate
->off_linktype
.constant_part
= 24;
1312 cstate
->cgstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1313 cstate
->cgstate
->off_linkpl
.is_variable
= 1;
1314 cstate
->cgstate
->off_linkhdr
.is_variable
= 1;
1315 cstate
->cgstate
->off_nl
= 8; /* 802.2+SNAP */
1316 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.2 */
1319 case DLT_ATM_RFC1483
:
1320 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1322 * assume routed, non-ISO PDUs
1323 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1325 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1326 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1327 * latter would presumably be treated the way PPPoE
1328 * should be, so you can do "pppoe and udp port 2049"
1329 * or "pppoa and tcp port 80" and have it check for
1330 * PPPo{A,E} and a PPP protocol of IP and....
1332 cstate
->cgstate
->off_linktype
.constant_part
= 0;
1333 cstate
->cgstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1334 cstate
->cgstate
->off_nl
= 8; /* 802.2+SNAP */
1335 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.2 */
1340 * Full Frontal ATM; you get AALn PDUs with an ATM
1343 cstate
->cgstate
->is_atm
= 1;
1344 cstate
->cgstate
->off_vpi
= SUNATM_VPI_POS
;
1345 cstate
->cgstate
->off_vci
= SUNATM_VCI_POS
;
1346 cstate
->cgstate
->off_proto
= PROTO_POS
;
1347 cstate
->cgstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1348 cstate
->cgstate
->off_linktype
.constant_part
= cstate
->cgstate
->off_payload
;
1349 cstate
->cgstate
->off_linkpl
.constant_part
= cstate
->cgstate
->off_payload
; /* if LLC-encapsulated */
1350 cstate
->cgstate
->off_nl
= 8; /* 802.2+SNAP */
1351 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.2 */
1357 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1358 cstate
->cgstate
->off_linkpl
.constant_part
= 0;
1359 cstate
->cgstate
->off_nl
= 0;
1360 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1363 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1364 cstate
->cgstate
->off_linktype
.constant_part
= 14;
1365 cstate
->cgstate
->off_linkpl
.constant_part
= 16;
1366 cstate
->cgstate
->off_nl
= 0;
1367 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1370 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1371 cstate
->cgstate
->off_linktype
.constant_part
= 0;
1372 cstate
->cgstate
->off_linkpl
.constant_part
= 20;
1373 cstate
->cgstate
->off_nl
= 0;
1374 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1379 * LocalTalk does have a 1-byte type field in the LLAP header,
1380 * but really it just indicates whether there is a "short" or
1381 * "long" DDP packet following.
1383 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1384 cstate
->cgstate
->off_linkpl
.constant_part
= 0;
1385 cstate
->cgstate
->off_nl
= 0;
1386 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1389 case DLT_IP_OVER_FC
:
1391 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1392 * link-level type field. We set "off_linktype" to the
1393 * offset of the LLC header.
1395 * To check for Ethernet types, we assume that SSAP = SNAP
1396 * is being used and pick out the encapsulated Ethernet type.
1397 * XXX - should we generate code to check for SNAP? RFC
1398 * 2625 says SNAP should be used.
1400 cstate
->cgstate
->off_linktype
.constant_part
= 16;
1401 cstate
->cgstate
->off_linkpl
.constant_part
= 16;
1402 cstate
->cgstate
->off_nl
= 8; /* 802.2+SNAP */
1403 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.2 */
1408 * XXX - we should set this to handle SNAP-encapsulated
1409 * frames (NLPID of 0x80).
1411 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1412 cstate
->cgstate
->off_linkpl
.constant_part
= 0;
1413 cstate
->cgstate
->off_nl
= 0;
1414 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1418 * the only BPF-interesting FRF.16 frames are non-control frames;
1419 * Frame Relay has a variable length link-layer
1420 * so lets start with offset 4 for now and increments later on (FIXME);
1423 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1424 cstate
->cgstate
->off_linkpl
.constant_part
= 0;
1425 cstate
->cgstate
->off_nl
= 4;
1426 cstate
->cgstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1429 case DLT_APPLE_IP_OVER_IEEE1394
:
1430 cstate
->cgstate
->off_linktype
.constant_part
= 16;
1431 cstate
->cgstate
->off_linkpl
.constant_part
= 18;
1432 cstate
->cgstate
->off_nl
= 0;
1433 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1436 case DLT_SYMANTEC_FIREWALL
:
1437 cstate
->cgstate
->off_linktype
.constant_part
= 6;
1438 cstate
->cgstate
->off_linkpl
.constant_part
= 44;
1439 cstate
->cgstate
->off_nl
= 0; /* Ethernet II */
1440 cstate
->cgstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1443 #ifdef HAVE_NET_PFVAR_H
1445 cstate
->cgstate
->off_linktype
.constant_part
= 0;
1446 cstate
->cgstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1447 cstate
->cgstate
->off_nl
= 0;
1448 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1452 case DLT_JUNIPER_MFR
:
1453 case DLT_JUNIPER_MLFR
:
1454 case DLT_JUNIPER_MLPPP
:
1455 case DLT_JUNIPER_PPP
:
1456 case DLT_JUNIPER_CHDLC
:
1457 case DLT_JUNIPER_FRELAY
:
1458 cstate
->cgstate
->off_linktype
.constant_part
= 4;
1459 cstate
->cgstate
->off_linkpl
.constant_part
= 4;
1460 cstate
->cgstate
->off_nl
= 0;
1461 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1464 case DLT_JUNIPER_ATM1
:
1465 cstate
->cgstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1466 cstate
->cgstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1467 cstate
->cgstate
->off_nl
= 0;
1468 cstate
->cgstate
->off_nl_nosnap
= 10;
1471 case DLT_JUNIPER_ATM2
:
1472 cstate
->cgstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1473 cstate
->cgstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1474 cstate
->cgstate
->off_nl
= 0;
1475 cstate
->cgstate
->off_nl_nosnap
= 10;
1478 /* frames captured on a Juniper PPPoE service PIC
1479 * contain raw ethernet frames */
1480 case DLT_JUNIPER_PPPOE
:
1481 case DLT_JUNIPER_ETHER
:
1482 cstate
->cgstate
->off_linkpl
.constant_part
= 14;
1483 cstate
->cgstate
->off_linktype
.constant_part
= 16;
1484 cstate
->cgstate
->off_nl
= 18; /* Ethernet II */
1485 cstate
->cgstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1488 case DLT_JUNIPER_PPPOE_ATM
:
1489 cstate
->cgstate
->off_linktype
.constant_part
= 4;
1490 cstate
->cgstate
->off_linkpl
.constant_part
= 6;
1491 cstate
->cgstate
->off_nl
= 0;
1492 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1495 case DLT_JUNIPER_GGSN
:
1496 cstate
->cgstate
->off_linktype
.constant_part
= 6;
1497 cstate
->cgstate
->off_linkpl
.constant_part
= 12;
1498 cstate
->cgstate
->off_nl
= 0;
1499 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1502 case DLT_JUNIPER_ES
:
1503 cstate
->cgstate
->off_linktype
.constant_part
= 6;
1504 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1505 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1506 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1509 case DLT_JUNIPER_MONITOR
:
1510 cstate
->cgstate
->off_linktype
.constant_part
= 12;
1511 cstate
->cgstate
->off_linkpl
.constant_part
= 12;
1512 cstate
->cgstate
->off_nl
= 0; /* raw IP/IP6 header */
1513 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1516 case DLT_BACNET_MS_TP
:
1517 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1518 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1519 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1520 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1523 case DLT_JUNIPER_SERVICES
:
1524 cstate
->cgstate
->off_linktype
.constant_part
= 12;
1525 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1526 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1527 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1530 case DLT_JUNIPER_VP
:
1531 cstate
->cgstate
->off_linktype
.constant_part
= 18;
1532 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1533 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1534 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1537 case DLT_JUNIPER_ST
:
1538 cstate
->cgstate
->off_linktype
.constant_part
= 18;
1539 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1540 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1541 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1544 case DLT_JUNIPER_ISM
:
1545 cstate
->cgstate
->off_linktype
.constant_part
= 8;
1546 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1547 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1548 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1551 case DLT_JUNIPER_VS
:
1552 case DLT_JUNIPER_SRX_E2E
:
1553 case DLT_JUNIPER_FIBRECHANNEL
:
1554 case DLT_JUNIPER_ATM_CEMIC
:
1555 cstate
->cgstate
->off_linktype
.constant_part
= 8;
1556 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1557 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1558 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1562 cstate
->cgstate
->off_li
= 2;
1563 cstate
->cgstate
->off_li_hsl
= 4;
1564 cstate
->cgstate
->off_sio
= 3;
1565 cstate
->cgstate
->off_opc
= 4;
1566 cstate
->cgstate
->off_dpc
= 4;
1567 cstate
->cgstate
->off_sls
= 7;
1568 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1569 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1570 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1571 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1574 case DLT_MTP2_WITH_PHDR
:
1575 cstate
->cgstate
->off_li
= 6;
1576 cstate
->cgstate
->off_li_hsl
= 8;
1577 cstate
->cgstate
->off_sio
= 7;
1578 cstate
->cgstate
->off_opc
= 8;
1579 cstate
->cgstate
->off_dpc
= 8;
1580 cstate
->cgstate
->off_sls
= 11;
1581 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1582 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1583 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1584 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1588 cstate
->cgstate
->off_li
= 22;
1589 cstate
->cgstate
->off_li_hsl
= 24;
1590 cstate
->cgstate
->off_sio
= 23;
1591 cstate
->cgstate
->off_opc
= 24;
1592 cstate
->cgstate
->off_dpc
= 24;
1593 cstate
->cgstate
->off_sls
= 27;
1594 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1595 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1596 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1597 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1601 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1602 cstate
->cgstate
->off_linkpl
.constant_part
= 4;
1603 cstate
->cgstate
->off_nl
= 0;
1604 cstate
->cgstate
->off_nl_nosnap
= 0;
1609 * Currently, only raw "link[N:M]" filtering is supported.
1611 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1612 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1613 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1614 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1618 cstate
->cgstate
->off_linktype
.constant_part
= 1;
1619 cstate
->cgstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1620 cstate
->cgstate
->off_nl
= 0;
1621 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1624 case DLT_NETANALYZER
:
1625 cstate
->cgstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1626 cstate
->cgstate
->off_linktype
.constant_part
= cstate
->cgstate
->off_linkhdr
.constant_part
+ 12;
1627 cstate
->cgstate
->off_linkpl
.constant_part
= cstate
->cgstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1628 cstate
->cgstate
->off_nl
= 0; /* Ethernet II */
1629 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1632 case DLT_NETANALYZER_TRANSPARENT
:
1633 cstate
->cgstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1634 cstate
->cgstate
->off_linktype
.constant_part
= cstate
->cgstate
->off_linkhdr
.constant_part
+ 12;
1635 cstate
->cgstate
->off_linkpl
.constant_part
= cstate
->cgstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1636 cstate
->cgstate
->off_nl
= 0; /* Ethernet II */
1637 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1642 * For values in the range in which we've assigned new
1643 * DLT_ values, only raw "link[N:M]" filtering is supported.
1645 if (cstate
->cgstate
->linktype
>= DLT_MATCHING_MIN
&&
1646 cstate
->cgstate
->linktype
<= DLT_MATCHING_MAX
) {
1647 cstate
->cgstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1648 cstate
->cgstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1649 cstate
->cgstate
->off_nl
= OFFSET_NOT_SET
;
1650 cstate
->cgstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1652 bpf_error(cstate
, "unknown data link type %d", cstate
->cgstate
->linktype
);
1657 cstate
->cgstate
->off_outermostlinkhdr
= cstate
->cgstate
->off_prevlinkhdr
= cstate
->cgstate
->off_linkhdr
;
1661 * Load a value relative to the specified absolute offset.
1663 static struct slist
*
1664 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1665 u_int offset
, u_int size
)
1667 struct slist
*s
, *s2
;
1669 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1672 * If "s" is non-null, it has code to arrange that the X register
1673 * contains the variable part of the absolute offset, so we
1674 * generate a load relative to that, with an offset of
1675 * abs_offset->constant_part + offset.
1677 * Otherwise, we can do an absolute load with an offset of
1678 * abs_offset->constant_part + offset.
1682 * "s" points to a list of statements that puts the
1683 * variable part of the absolute offset into the X register.
1684 * Do an indirect load, to use the X register as an offset.
1686 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1687 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1691 * There is no variable part of the absolute offset, so
1692 * just do an absolute load.
1694 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1695 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1701 * Load a value relative to the beginning of the specified header.
1703 static struct slist
*
1704 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1707 struct slist
*s
, *s2
;
1712 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1717 s
= gen_load_absoffsetrel(cstate
, &cstate
->cgstate
->off_linkhdr
, offset
, size
);
1720 case OR_PREVLINKHDR
:
1721 s
= gen_load_absoffsetrel(cstate
, &cstate
->cgstate
->off_prevlinkhdr
, offset
, size
);
1725 s
= gen_load_absoffsetrel(cstate
, &cstate
->cgstate
->off_linkpl
, offset
, size
);
1728 case OR_PREVMPLSHDR
:
1729 s
= gen_load_absoffsetrel(cstate
, &cstate
->cgstate
->off_linkpl
, cstate
->cgstate
->off_nl
- 4 + offset
, size
);
1733 s
= gen_load_absoffsetrel(cstate
, &cstate
->cgstate
->off_linkpl
, cstate
->cgstate
->off_nl
+ offset
, size
);
1736 case OR_LINKPL_NOSNAP
:
1737 s
= gen_load_absoffsetrel(cstate
, &cstate
->cgstate
->off_linkpl
, cstate
->cgstate
->off_nl_nosnap
+ offset
, size
);
1741 s
= gen_load_absoffsetrel(cstate
, &cstate
->cgstate
->off_linktype
, offset
, size
);
1746 * Load the X register with the length of the IPv4 header
1747 * (plus the offset of the link-layer header, if it's
1748 * preceded by a variable-length header such as a radio
1749 * header), in bytes.
1751 s
= gen_loadx_iphdrlen(cstate
);
1754 * Load the item at {offset of the link-layer payload} +
1755 * {offset, relative to the start of the link-layer
1756 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1757 * {specified offset}.
1759 * If the offset of the link-layer payload is variable,
1760 * the variable part of that offset is included in the
1761 * value in the X register, and we include the constant
1762 * part in the offset of the load.
1764 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1765 s2
->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
+ offset
;
1770 s
= gen_load_absoffsetrel(cstate
, &cstate
->cgstate
->off_linkpl
, cstate
->cgstate
->off_nl
+ 40 + offset
, size
);
1781 * Generate code to load into the X register the sum of the length of
1782 * the IPv4 header and the variable part of the offset of the link-layer
1785 static struct slist
*
1786 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1788 struct slist
*s
, *s2
;
1790 s
= gen_abs_offset_varpart(cstate
, &cstate
->cgstate
->off_linkpl
);
1793 * The offset of the link-layer payload has a variable
1794 * part. "s" points to a list of statements that put
1795 * the variable part of that offset into the X register.
1797 * The 4*([k]&0xf) addressing mode can't be used, as we
1798 * don't have a constant offset, so we have to load the
1799 * value in question into the A register and add to it
1800 * the value from the X register.
1802 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1803 s2
->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
;
1805 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1808 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1813 * The A register now contains the length of the IP header.
1814 * We need to add to it the variable part of the offset of
1815 * the link-layer payload, which is still in the X
1816 * register, and move the result into the X register.
1818 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1819 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1822 * The offset of the link-layer payload is a constant,
1823 * so no code was generated to load the (non-existent)
1824 * variable part of that offset.
1826 * This means we can use the 4*([k]&0xf) addressing
1827 * mode. Load the length of the IPv4 header, which
1828 * is at an offset of cstate->cgstate->off_nl from the beginning of
1829 * the link-layer payload, and thus at an offset of
1830 * cstate->cgstate->off_linkpl.constant_part + cstate->cgstate->off_nl from the beginning
1831 * of the raw packet data, using that addressing mode.
1833 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1834 s
->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
;
1840 static struct block
*
1841 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1846 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1848 b
= new_block(cstate
, JMP(BPF_JEQ
));
1854 static inline struct block
*
1855 gen_true(compiler_state_t
*cstate
)
1857 return gen_uncond(cstate
, 1);
1860 static inline struct block
*
1861 gen_false(compiler_state_t
*cstate
)
1863 return gen_uncond(cstate
, 0);
1867 * Byte-swap a 32-bit number.
1868 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1869 * big-endian platforms.)
1871 #define SWAPLONG(y) \
1872 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1875 * Generate code to match a particular packet type.
1877 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1878 * value, if <= ETHERMTU. We use that to determine whether to
1879 * match the type/length field or to check the type/length field for
1880 * a value <= ETHERMTU to see whether it's a type field and then do
1881 * the appropriate test.
1883 static struct block
*
1884 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1886 struct block
*b0
, *b1
;
1892 case LLCSAP_NETBEUI
:
1894 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1895 * so we check the DSAP and SSAP.
1897 * LLCSAP_IP checks for IP-over-802.2, rather
1898 * than IP-over-Ethernet or IP-over-SNAP.
1900 * XXX - should we check both the DSAP and the
1901 * SSAP, like this, or should we check just the
1902 * DSAP, as we do for other types <= ETHERMTU
1903 * (i.e., other SAP values)?
1905 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1907 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1908 ((proto
<< 8) | proto
));
1916 * Ethernet_II frames, which are Ethernet
1917 * frames with a frame type of ETHERTYPE_IPX;
1919 * Ethernet_802.3 frames, which are 802.3
1920 * frames (i.e., the type/length field is
1921 * a length field, <= ETHERMTU, rather than
1922 * a type field) with the first two bytes
1923 * after the Ethernet/802.3 header being
1926 * Ethernet_802.2 frames, which are 802.3
1927 * frames with an 802.2 LLC header and
1928 * with the IPX LSAP as the DSAP in the LLC
1931 * Ethernet_SNAP frames, which are 802.3
1932 * frames with an LLC header and a SNAP
1933 * header and with an OUI of 0x000000
1934 * (encapsulated Ethernet) and a protocol
1935 * ID of ETHERTYPE_IPX in the SNAP header.
1937 * XXX - should we generate the same code both
1938 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1942 * This generates code to check both for the
1943 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1945 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1946 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1950 * Now we add code to check for SNAP frames with
1951 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1953 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1957 * Now we generate code to check for 802.3
1958 * frames in general.
1960 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1964 * Now add the check for 802.3 frames before the
1965 * check for Ethernet_802.2 and Ethernet_802.3,
1966 * as those checks should only be done on 802.3
1967 * frames, not on Ethernet frames.
1972 * Now add the check for Ethernet_II frames, and
1973 * do that before checking for the other frame
1976 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1980 case ETHERTYPE_ATALK
:
1981 case ETHERTYPE_AARP
:
1983 * EtherTalk (AppleTalk protocols on Ethernet link
1984 * layer) may use 802.2 encapsulation.
1988 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1989 * we check for an Ethernet type field less than
1990 * 1500, which means it's an 802.3 length field.
1992 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1996 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1997 * SNAP packets with an organization code of
1998 * 0x080007 (Apple, for Appletalk) and a protocol
1999 * type of ETHERTYPE_ATALK (Appletalk).
2001 * 802.2-encapsulated ETHERTYPE_AARP packets are
2002 * SNAP packets with an organization code of
2003 * 0x000000 (encapsulated Ethernet) and a protocol
2004 * type of ETHERTYPE_AARP (Appletalk ARP).
2006 if (proto
== ETHERTYPE_ATALK
)
2007 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2008 else /* proto == ETHERTYPE_AARP */
2009 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2013 * Check for Ethernet encapsulation (Ethertalk
2014 * phase 1?); we just check for the Ethernet
2017 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2023 if (proto
<= ETHERMTU
) {
2025 * This is an LLC SAP value, so the frames
2026 * that match would be 802.2 frames.
2027 * Check that the frame is an 802.2 frame
2028 * (i.e., that the length/type field is
2029 * a length field, <= ETHERMTU) and
2030 * then check the DSAP.
2032 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2034 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2039 * This is an Ethernet type, so compare
2040 * the length/type field with it (if
2041 * the frame is an 802.2 frame, the length
2042 * field will be <= ETHERMTU, and, as
2043 * "proto" is > ETHERMTU, this test
2044 * will fail and the frame won't match,
2045 * which is what we want).
2047 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2053 static struct block
*
2054 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2057 * For DLT_NULL, the link-layer header is a 32-bit word
2058 * containing an AF_ value in *host* byte order, and for
2059 * DLT_ENC, the link-layer header begins with a 32-bit
2060 * word containing an AF_ value in host byte order.
2062 * In addition, if we're reading a saved capture file,
2063 * the host byte order in the capture may not be the
2064 * same as the host byte order on this machine.
2066 * For DLT_LOOP, the link-layer header is a 32-bit
2067 * word containing an AF_ value in *network* byte order.
2069 if (cstate
->cgstate
->linktype
== DLT_NULL
|| cstate
->cgstate
->linktype
== DLT_ENC
) {
2071 * The AF_ value is in host byte order, but the BPF
2072 * interpreter will convert it to network byte order.
2074 * If this is a save file, and it's from a machine
2075 * with the opposite byte order to ours, we byte-swap
2078 * Then we run it through "htonl()", and generate
2079 * code to compare against the result.
2081 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2082 proto
= SWAPLONG(proto
);
2083 proto
= htonl(proto
);
2085 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2089 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2090 * or IPv6 then we have an error.
2092 static struct block
*
2093 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2098 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2101 case ETHERTYPE_IPV6
:
2102 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2103 (bpf_int32
)IPH_AF_INET6
);
2110 return gen_false(cstate
);
2114 * Generate code to match a particular packet type.
2116 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2117 * value, if <= ETHERMTU. We use that to determine whether to
2118 * match the type field or to check the type field for the special
2119 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2121 static struct block
*
2122 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2124 struct block
*b0
, *b1
;
2130 case LLCSAP_NETBEUI
:
2132 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2133 * so we check the DSAP and SSAP.
2135 * LLCSAP_IP checks for IP-over-802.2, rather
2136 * than IP-over-Ethernet or IP-over-SNAP.
2138 * XXX - should we check both the DSAP and the
2139 * SSAP, like this, or should we check just the
2140 * DSAP, as we do for other types <= ETHERMTU
2141 * (i.e., other SAP values)?
2143 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2144 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2145 ((proto
<< 8) | proto
));
2151 * Ethernet_II frames, which are Ethernet
2152 * frames with a frame type of ETHERTYPE_IPX;
2154 * Ethernet_802.3 frames, which have a frame
2155 * type of LINUX_SLL_P_802_3;
2157 * Ethernet_802.2 frames, which are 802.3
2158 * frames with an 802.2 LLC header (i.e, have
2159 * a frame type of LINUX_SLL_P_802_2) and
2160 * with the IPX LSAP as the DSAP in the LLC
2163 * Ethernet_SNAP frames, which are 802.3
2164 * frames with an LLC header and a SNAP
2165 * header and with an OUI of 0x000000
2166 * (encapsulated Ethernet) and a protocol
2167 * ID of ETHERTYPE_IPX in the SNAP header.
2169 * First, do the checks on LINUX_SLL_P_802_2
2170 * frames; generate the check for either
2171 * Ethernet_802.2 or Ethernet_SNAP frames, and
2172 * then put a check for LINUX_SLL_P_802_2 frames
2175 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2176 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2178 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2182 * Now check for 802.3 frames and OR that with
2183 * the previous test.
2185 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2189 * Now add the check for Ethernet_II frames, and
2190 * do that before checking for the other frame
2193 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2197 case ETHERTYPE_ATALK
:
2198 case ETHERTYPE_AARP
:
2200 * EtherTalk (AppleTalk protocols on Ethernet link
2201 * layer) may use 802.2 encapsulation.
2205 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2206 * we check for the 802.2 protocol type in the
2207 * "Ethernet type" field.
2209 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2212 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2213 * SNAP packets with an organization code of
2214 * 0x080007 (Apple, for Appletalk) and a protocol
2215 * type of ETHERTYPE_ATALK (Appletalk).
2217 * 802.2-encapsulated ETHERTYPE_AARP packets are
2218 * SNAP packets with an organization code of
2219 * 0x000000 (encapsulated Ethernet) and a protocol
2220 * type of ETHERTYPE_AARP (Appletalk ARP).
2222 if (proto
== ETHERTYPE_ATALK
)
2223 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2224 else /* proto == ETHERTYPE_AARP */
2225 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2229 * Check for Ethernet encapsulation (Ethertalk
2230 * phase 1?); we just check for the Ethernet
2233 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2239 if (proto
<= ETHERMTU
) {
2241 * This is an LLC SAP value, so the frames
2242 * that match would be 802.2 frames.
2243 * Check for the 802.2 protocol type
2244 * in the "Ethernet type" field, and
2245 * then check the DSAP.
2247 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2248 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->cgstate
->off_linkpl
.constant_part
, BPF_B
,
2254 * This is an Ethernet type, so compare
2255 * the length/type field with it (if
2256 * the frame is an 802.2 frame, the length
2257 * field will be <= ETHERMTU, and, as
2258 * "proto" is > ETHERMTU, this test
2259 * will fail and the frame won't match,
2260 * which is what we want).
2262 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2267 static struct slist
*
2268 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2270 struct slist
*s1
, *s2
;
2271 struct slist
*sjeq_avs_cookie
;
2272 struct slist
*sjcommon
;
2275 * This code is not compatible with the optimizer, as
2276 * we are generating jmp instructions within a normal
2277 * slist of instructions
2279 cstate
->cgstate
->no_optimize
= 1;
2282 * Generate code to load the length of the radio header into
2283 * the register assigned to hold that length, if one has been
2284 * assigned. (If one hasn't been assigned, no code we've
2285 * generated uses that prefix, so we don't need to generate any
2288 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2289 * or always use the AVS header rather than the Prism header.
2290 * We load a 4-byte big-endian value at the beginning of the
2291 * raw packet data, and see whether, when masked with 0xFFFFF000,
2292 * it's equal to 0x80211000. If so, that indicates that it's
2293 * an AVS header (the masked-out bits are the version number).
2294 * Otherwise, it's a Prism header.
2296 * XXX - the Prism header is also, in theory, variable-length,
2297 * but no known software generates headers that aren't 144
2300 if (cstate
->cgstate
->off_linkhdr
.reg
!= -1) {
2304 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2308 * AND it with 0xFFFFF000.
2310 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2311 s2
->s
.k
= 0xFFFFF000;
2315 * Compare with 0x80211000.
2317 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2318 sjeq_avs_cookie
->s
.k
= 0x80211000;
2319 sappend(s1
, sjeq_avs_cookie
);
2324 * The 4 bytes at an offset of 4 from the beginning of
2325 * the AVS header are the length of the AVS header.
2326 * That field is big-endian.
2328 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2331 sjeq_avs_cookie
->s
.jt
= s2
;
2334 * Now jump to the code to allocate a register
2335 * into which to save the header length and
2336 * store the length there. (The "jump always"
2337 * instruction needs to have the k field set;
2338 * it's added to the PC, so, as we're jumping
2339 * over a single instruction, it should be 1.)
2341 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2343 sappend(s1
, sjcommon
);
2346 * Now for the code that handles the Prism header.
2347 * Just load the length of the Prism header (144)
2348 * into the A register. Have the test for an AVS
2349 * header branch here if we don't have an AVS header.
2351 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2354 sjeq_avs_cookie
->s
.jf
= s2
;
2357 * Now allocate a register to hold that value and store
2358 * it. The code for the AVS header will jump here after
2359 * loading the length of the AVS header.
2361 s2
= new_stmt(cstate
, BPF_ST
);
2362 s2
->s
.k
= cstate
->cgstate
->off_linkhdr
.reg
;
2364 sjcommon
->s
.jf
= s2
;
2367 * Now move it into the X register.
2369 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2377 static struct slist
*
2378 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2380 struct slist
*s1
, *s2
;
2383 * Generate code to load the length of the AVS header into
2384 * the register assigned to hold that length, if one has been
2385 * assigned. (If one hasn't been assigned, no code we've
2386 * generated uses that prefix, so we don't need to generate any
2389 if (cstate
->cgstate
->off_linkhdr
.reg
!= -1) {
2391 * The 4 bytes at an offset of 4 from the beginning of
2392 * the AVS header are the length of the AVS header.
2393 * That field is big-endian.
2395 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2399 * Now allocate a register to hold that value and store
2402 s2
= new_stmt(cstate
, BPF_ST
);
2403 s2
->s
.k
= cstate
->cgstate
->off_linkhdr
.reg
;
2407 * Now move it into the X register.
2409 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2417 static struct slist
*
2418 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2420 struct slist
*s1
, *s2
;
2423 * Generate code to load the length of the radiotap header into
2424 * the register assigned to hold that length, if one has been
2425 * assigned. (If one hasn't been assigned, no code we've
2426 * generated uses that prefix, so we don't need to generate any
2429 if (cstate
->cgstate
->off_linkhdr
.reg
!= -1) {
2431 * The 2 bytes at offsets of 2 and 3 from the beginning
2432 * of the radiotap header are the length of the radiotap
2433 * header; unfortunately, it's little-endian, so we have
2434 * to load it a byte at a time and construct the value.
2438 * Load the high-order byte, at an offset of 3, shift it
2439 * left a byte, and put the result in the X register.
2441 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2443 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2446 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2450 * Load the next byte, at an offset of 2, and OR the
2451 * value from the X register into it.
2453 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2456 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2460 * Now allocate a register to hold that value and store
2463 s2
= new_stmt(cstate
, BPF_ST
);
2464 s2
->s
.k
= cstate
->cgstate
->off_linkhdr
.reg
;
2468 * Now move it into the X register.
2470 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2479 * At the moment we treat PPI as normal Radiotap encoded
2480 * packets. The difference is in the function that generates
2481 * the code at the beginning to compute the header length.
2482 * Since this code generator of PPI supports bare 802.11
2483 * encapsulation only (i.e. the encapsulated DLT should be
2484 * DLT_IEEE802_11) we generate code to check for this too;
2485 * that's done in finish_parse().
2487 static struct slist
*
2488 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2490 struct slist
*s1
, *s2
;
2493 * Generate code to load the length of the radiotap header
2494 * into the register assigned to hold that length, if one has
2497 if (cstate
->cgstate
->off_linkhdr
.reg
!= -1) {
2499 * The 2 bytes at offsets of 2 and 3 from the beginning
2500 * of the radiotap header are the length of the radiotap
2501 * header; unfortunately, it's little-endian, so we have
2502 * to load it a byte at a time and construct the value.
2506 * Load the high-order byte, at an offset of 3, shift it
2507 * left a byte, and put the result in the X register.
2509 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2511 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2514 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2518 * Load the next byte, at an offset of 2, and OR the
2519 * value from the X register into it.
2521 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2524 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2528 * Now allocate a register to hold that value and store
2531 s2
= new_stmt(cstate
, BPF_ST
);
2532 s2
->s
.k
= cstate
->cgstate
->off_linkhdr
.reg
;
2536 * Now move it into the X register.
2538 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2547 * Load a value relative to the beginning of the link-layer header after the 802.11
2548 * header, i.e. LLC_SNAP.
2549 * The link-layer header doesn't necessarily begin at the beginning
2550 * of the packet data; there might be a variable-length prefix containing
2551 * radio information.
2553 static struct slist
*
2554 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2557 struct slist
*sjset_data_frame_1
;
2558 struct slist
*sjset_data_frame_2
;
2559 struct slist
*sjset_qos
;
2560 struct slist
*sjset_radiotap_flags_present
;
2561 struct slist
*sjset_radiotap_ext_present
;
2562 struct slist
*sjset_radiotap_tsft_present
;
2563 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2564 struct slist
*s_roundup
;
2566 if (cstate
->cgstate
->off_linkpl
.reg
== -1) {
2568 * No register has been assigned to the offset of
2569 * the link-layer payload, which means nobody needs
2570 * it; don't bother computing it - just return
2571 * what we already have.
2577 * This code is not compatible with the optimizer, as
2578 * we are generating jmp instructions within a normal
2579 * slist of instructions
2581 cstate
->cgstate
->no_optimize
= 1;
2584 * If "s" is non-null, it has code to arrange that the X register
2585 * contains the length of the prefix preceding the link-layer
2588 * Otherwise, the length of the prefix preceding the link-layer
2589 * header is "off_outermostlinkhdr.constant_part".
2593 * There is no variable-length header preceding the
2594 * link-layer header.
2596 * Load the length of the fixed-length prefix preceding
2597 * the link-layer header (if any) into the X register,
2598 * and store it in the cstate->cgstate->off_linkpl.reg register.
2599 * That length is off_outermostlinkhdr.constant_part.
2601 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2602 s
->s
.k
= cstate
->cgstate
->off_outermostlinkhdr
.constant_part
;
2606 * The X register contains the offset of the beginning of the
2607 * link-layer header; add 24, which is the minimum length
2608 * of the MAC header for a data frame, to that, and store it
2609 * in cstate->cgstate->off_linkpl.reg, and then load the Frame Control field,
2610 * which is at the offset in the X register, with an indexed load.
2612 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2614 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2617 s2
= new_stmt(cstate
, BPF_ST
);
2618 s2
->s
.k
= cstate
->cgstate
->off_linkpl
.reg
;
2621 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2626 * Check the Frame Control field to see if this is a data frame;
2627 * a data frame has the 0x08 bit (b3) in that field set and the
2628 * 0x04 bit (b2) clear.
2630 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2631 sjset_data_frame_1
->s
.k
= 0x08;
2632 sappend(s
, sjset_data_frame_1
);
2635 * If b3 is set, test b2, otherwise go to the first statement of
2636 * the rest of the program.
2638 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2639 sjset_data_frame_2
->s
.k
= 0x04;
2640 sappend(s
, sjset_data_frame_2
);
2641 sjset_data_frame_1
->s
.jf
= snext
;
2644 * If b2 is not set, this is a data frame; test the QoS bit.
2645 * Otherwise, go to the first statement of the rest of the
2648 sjset_data_frame_2
->s
.jt
= snext
;
2649 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2650 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2651 sappend(s
, sjset_qos
);
2654 * If it's set, add 2 to cstate->cgstate->off_linkpl.reg, to skip the QoS
2656 * Otherwise, go to the first statement of the rest of the
2659 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2660 s2
->s
.k
= cstate
->cgstate
->off_linkpl
.reg
;
2662 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2665 s2
= new_stmt(cstate
, BPF_ST
);
2666 s2
->s
.k
= cstate
->cgstate
->off_linkpl
.reg
;
2670 * If we have a radiotap header, look at it to see whether
2671 * there's Atheros padding between the MAC-layer header
2674 * Note: all of the fields in the radiotap header are
2675 * little-endian, so we byte-swap all of the values
2676 * we test against, as they will be loaded as big-endian
2679 * XXX - in the general case, we would have to scan through
2680 * *all* the presence bits, if there's more than one word of
2681 * presence bits. That would require a loop, meaning that
2682 * we wouldn't be able to run the filter in the kernel.
2684 * We assume here that the Atheros adapters that insert the
2685 * annoying padding don't have multiple antennae and therefore
2686 * do not generate radiotap headers with multiple presence words.
2688 if (cstate
->cgstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2690 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2691 * in the first presence flag word?
2693 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2697 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2698 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2699 sappend(s
, sjset_radiotap_flags_present
);
2702 * If not, skip all of this.
2704 sjset_radiotap_flags_present
->s
.jf
= snext
;
2707 * Otherwise, is the "extension" bit set in that word?
2709 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2710 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2711 sappend(s
, sjset_radiotap_ext_present
);
2712 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2715 * If so, skip all of this.
2717 sjset_radiotap_ext_present
->s
.jt
= snext
;
2720 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2722 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2723 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2724 sappend(s
, sjset_radiotap_tsft_present
);
2725 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2728 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2729 * at an offset of 16 from the beginning of the raw packet
2730 * data (8 bytes for the radiotap header and 8 bytes for
2733 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2736 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2739 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2741 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2742 sjset_tsft_datapad
->s
.k
= 0x20;
2743 sappend(s
, sjset_tsft_datapad
);
2746 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2747 * at an offset of 8 from the beginning of the raw packet
2748 * data (8 bytes for the radiotap header).
2750 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2753 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2756 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2758 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2759 sjset_notsft_datapad
->s
.k
= 0x20;
2760 sappend(s
, sjset_notsft_datapad
);
2763 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2764 * set, round the length of the 802.11 header to
2765 * a multiple of 4. Do that by adding 3 and then
2766 * dividing by and multiplying by 4, which we do by
2769 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2770 s_roundup
->s
.k
= cstate
->cgstate
->off_linkpl
.reg
;
2771 sappend(s
, s_roundup
);
2772 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2775 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2778 s2
= new_stmt(cstate
, BPF_ST
);
2779 s2
->s
.k
= cstate
->cgstate
->off_linkpl
.reg
;
2782 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2783 sjset_tsft_datapad
->s
.jf
= snext
;
2784 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2785 sjset_notsft_datapad
->s
.jf
= snext
;
2787 sjset_qos
->s
.jf
= snext
;
2793 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2797 /* There is an implicit dependency between the link
2798 * payload and link header since the payload computation
2799 * includes the variable part of the header. Therefore,
2800 * if nobody else has allocated a register for the link
2801 * header and we need it, do it now. */
2802 if (cstate
->cgstate
->off_linkpl
.reg
!= -1 && cstate
->cgstate
->off_linkhdr
.is_variable
&&
2803 cstate
->cgstate
->off_linkhdr
.reg
== -1)
2804 cstate
->cgstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2807 * For link-layer types that have a variable-length header
2808 * preceding the link-layer header, generate code to load
2809 * the offset of the link-layer header into the register
2810 * assigned to that offset, if any.
2812 * XXX - this, and the next switch statement, won't handle
2813 * encapsulation of 802.11 or 802.11+radio information in
2814 * some other protocol stack. That's significantly more
2817 switch (cstate
->cgstate
->outermostlinktype
) {
2819 case DLT_PRISM_HEADER
:
2820 s
= gen_load_prism_llprefixlen(cstate
);
2823 case DLT_IEEE802_11_RADIO_AVS
:
2824 s
= gen_load_avs_llprefixlen(cstate
);
2827 case DLT_IEEE802_11_RADIO
:
2828 s
= gen_load_radiotap_llprefixlen(cstate
);
2832 s
= gen_load_ppi_llprefixlen(cstate
);
2841 * For link-layer types that have a variable-length link-layer
2842 * header, generate code to load the offset of the link-layer
2843 * payload into the register assigned to that offset, if any.
2845 switch (cstate
->cgstate
->outermostlinktype
) {
2847 case DLT_IEEE802_11
:
2848 case DLT_PRISM_HEADER
:
2849 case DLT_IEEE802_11_RADIO_AVS
:
2850 case DLT_IEEE802_11_RADIO
:
2852 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2857 * If there there is no initialization yet and we need variable
2858 * length offsets for VLAN, initialize them to zero
2860 if (s
== NULL
&& cstate
->cgstate
->is_vlan_vloffset
) {
2863 if (cstate
->cgstate
->off_linkpl
.reg
== -1)
2864 cstate
->cgstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2865 if (cstate
->cgstate
->off_linktype
.reg
== -1)
2866 cstate
->cgstate
->off_linktype
.reg
= alloc_reg(cstate
);
2868 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2870 s2
= new_stmt(cstate
, BPF_ST
);
2871 s2
->s
.k
= cstate
->cgstate
->off_linkpl
.reg
;
2873 s2
= new_stmt(cstate
, BPF_ST
);
2874 s2
->s
.k
= cstate
->cgstate
->off_linktype
.reg
;
2879 * If we have any offset-loading code, append all the
2880 * existing statements in the block to those statements,
2881 * and make the resulting list the list of statements
2885 sappend(s
, b
->stmts
);
2890 static struct block
*
2891 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2893 struct slist
*s_load_dlt
;
2896 if (cstate
->cgstate
->linktype
== DLT_PPI
)
2898 /* Create the statements that check for the DLT
2900 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2901 s_load_dlt
->s
.k
= 4;
2903 b
= new_block(cstate
, JMP(BPF_JEQ
));
2905 b
->stmts
= s_load_dlt
;
2906 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2917 * Take an absolute offset, and:
2919 * if it has no variable part, return NULL;
2921 * if it has a variable part, generate code to load the register
2922 * containing that variable part into the X register, returning
2923 * a pointer to that code - if no register for that offset has
2924 * been allocated, allocate it first.
2926 * (The code to set that register will be generated later, but will
2927 * be placed earlier in the code sequence.)
2929 static struct slist
*
2930 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2934 if (off
->is_variable
) {
2935 if (off
->reg
== -1) {
2937 * We haven't yet assigned a register for the
2938 * variable part of the offset of the link-layer
2939 * header; allocate one.
2941 off
->reg
= alloc_reg(cstate
);
2945 * Load the register containing the variable part of the
2946 * offset of the link-layer header into the X register.
2948 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2953 * That offset isn't variable, there's no variable part,
2954 * so we don't need to generate any code.
2961 * Map an Ethernet type to the equivalent PPP type.
2964 ethertype_to_ppptype(int proto
)
2972 case ETHERTYPE_IPV6
:
2980 case ETHERTYPE_ATALK
:
2994 * I'm assuming the "Bridging PDU"s that go
2995 * over PPP are Spanning Tree Protocol
3009 * Generate any tests that, for encapsulation of a link-layer packet
3010 * inside another protocol stack, need to be done to check for those
3011 * link-layer packets (and that haven't already been done by a check
3012 * for that encapsulation).
3014 static struct block
*
3015 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3019 if (cstate
->cgstate
->is_geneve
)
3020 return gen_geneve_ll_check(cstate
);
3022 switch (cstate
->cgstate
->prevlinktype
) {
3026 * This is LANE-encapsulated Ethernet; check that the LANE
3027 * packet doesn't begin with an LE Control marker, i.e.
3028 * that it's data, not a control message.
3030 * (We've already generated a test for LANE.)
3032 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3038 * No such tests are necessary.
3046 * The three different values we should check for when checking for an
3047 * IPv6 packet with DLT_NULL.
3049 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3050 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3051 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3054 * Generate code to match a particular packet type by matching the
3055 * link-layer type field or fields in the 802.2 LLC header.
3057 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3058 * value, if <= ETHERMTU.
3060 static struct block
*
3061 gen_linktype(compiler_state_t
*cstate
, int proto
)
3063 struct block
*b0
, *b1
, *b2
;
3064 const char *description
;
3066 /* are we checking MPLS-encapsulated packets? */
3067 if (cstate
->cgstate
->label_stack_depth
> 0) {
3071 /* FIXME add other L3 proto IDs */
3072 return gen_mpls_linktype(cstate
, Q_IP
);
3074 case ETHERTYPE_IPV6
:
3076 /* FIXME add other L3 proto IDs */
3077 return gen_mpls_linktype(cstate
, Q_IPV6
);
3080 bpf_error(cstate
, "unsupported protocol over mpls");
3085 switch (cstate
->cgstate
->linktype
) {
3088 case DLT_NETANALYZER
:
3089 case DLT_NETANALYZER_TRANSPARENT
:
3090 /* Geneve has an EtherType regardless of whether there is an
3092 if (!cstate
->cgstate
->is_geneve
)
3093 b0
= gen_prevlinkhdr_check(cstate
);
3097 b1
= gen_ether_linktype(cstate
, proto
);
3108 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3112 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3118 case DLT_IEEE802_11
:
3119 case DLT_PRISM_HEADER
:
3120 case DLT_IEEE802_11_RADIO_AVS
:
3121 case DLT_IEEE802_11_RADIO
:
3124 * Check that we have a data frame.
3126 b0
= gen_check_802_11_data_frame(cstate
);
3129 * Now check for the specified link-layer type.
3131 b1
= gen_llc_linktype(cstate
, proto
);
3139 * XXX - check for LLC frames.
3141 return gen_llc_linktype(cstate
, proto
);
3147 * XXX - check for LLC PDUs, as per IEEE 802.5.
3149 return gen_llc_linktype(cstate
, proto
);
3153 case DLT_ATM_RFC1483
:
3155 case DLT_IP_OVER_FC
:
3156 return gen_llc_linktype(cstate
, proto
);
3162 * Check for an LLC-encapsulated version of this protocol;
3163 * if we were checking for LANE, linktype would no longer
3166 * Check for LLC encapsulation and then check the protocol.
3168 b0
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3169 b1
= gen_llc_linktype(cstate
, proto
);
3176 return gen_linux_sll_linktype(cstate
, proto
);
3181 case DLT_SLIP_BSDOS
:
3184 * These types don't provide any type field; packets
3185 * are always IPv4 or IPv6.
3187 * XXX - for IPv4, check for a version number of 4, and,
3188 * for IPv6, check for a version number of 6?
3193 /* Check for a version number of 4. */
3194 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3196 case ETHERTYPE_IPV6
:
3197 /* Check for a version number of 6. */
3198 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3201 return gen_false(cstate
); /* always false */
3208 * Raw IPv4, so no type field.
3210 if (proto
== ETHERTYPE_IP
)
3211 return gen_true(cstate
); /* always true */
3213 /* Checking for something other than IPv4; always false */
3214 return gen_false(cstate
);
3220 * Raw IPv6, so no type field.
3222 if (proto
== ETHERTYPE_IPV6
)
3223 return gen_true(cstate
); /* always true */
3225 /* Checking for something other than IPv6; always false */
3226 return gen_false(cstate
);
3232 case DLT_PPP_SERIAL
:
3235 * We use Ethernet protocol types inside libpcap;
3236 * map them to the corresponding PPP protocol types.
3238 proto
= ethertype_to_ppptype(proto
);
3239 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3245 * We use Ethernet protocol types inside libpcap;
3246 * map them to the corresponding PPP protocol types.
3252 * Also check for Van Jacobson-compressed IP.
3253 * XXX - do this for other forms of PPP?
3255 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3256 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3258 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3263 proto
= ethertype_to_ppptype(proto
);
3264 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3276 return (gen_loopback_linktype(cstate
, AF_INET
));
3278 case ETHERTYPE_IPV6
:
3280 * AF_ values may, unfortunately, be platform-
3281 * dependent; AF_INET isn't, because everybody
3282 * used 4.2BSD's value, but AF_INET6 is, because
3283 * 4.2BSD didn't have a value for it (given that
3284 * IPv6 didn't exist back in the early 1980's),
3285 * and they all picked their own values.
3287 * This means that, if we're reading from a
3288 * savefile, we need to check for all the
3291 * If we're doing a live capture, we only need
3292 * to check for this platform's value; however,
3293 * Npcap uses 24, which isn't Windows's AF_INET6
3294 * value. (Given the multiple different values,
3295 * programs that read pcap files shouldn't be
3296 * checking for their platform's AF_INET6 value
3297 * anyway, they should check for all of the
3298 * possible values. and they might as well do
3299 * that even for live captures.)
3301 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3303 * Savefile - check for all three
3304 * possible IPv6 values.
3306 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3307 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3309 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3314 * Live capture, so we only need to
3315 * check for the value used on this
3320 * Npcap doesn't use Windows's AF_INET6,
3321 * as that collides with AF_IPX on
3322 * some BSDs (both have the value 23).
3323 * Instead, it uses 24.
3325 return (gen_loopback_linktype(cstate
, 24));
3328 return (gen_loopback_linktype(cstate
, AF_INET6
));
3329 #else /* AF_INET6 */
3331 * I guess this platform doesn't support
3332 * IPv6, so we just reject all packets.
3334 return gen_false(cstate
);
3335 #endif /* AF_INET6 */
3341 * Not a type on which we support filtering.
3342 * XXX - support those that have AF_ values
3343 * #defined on this platform, at least?
3345 return gen_false(cstate
);
3348 #ifdef HAVE_NET_PFVAR_H
3351 * af field is host byte order in contrast to the rest of
3354 if (proto
== ETHERTYPE_IP
)
3355 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3356 BPF_B
, (bpf_int32
)AF_INET
));
3357 else if (proto
== ETHERTYPE_IPV6
)
3358 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3359 BPF_B
, (bpf_int32
)AF_INET6
));
3361 return gen_false(cstate
);
3364 #endif /* HAVE_NET_PFVAR_H */
3367 case DLT_ARCNET_LINUX
:
3369 * XXX should we check for first fragment if the protocol
3375 return gen_false(cstate
);
3377 case ETHERTYPE_IPV6
:
3378 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3379 (bpf_int32
)ARCTYPE_INET6
));
3382 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3383 (bpf_int32
)ARCTYPE_IP
);
3384 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3385 (bpf_int32
)ARCTYPE_IP_OLD
);
3390 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3391 (bpf_int32
)ARCTYPE_ARP
);
3392 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3393 (bpf_int32
)ARCTYPE_ARP_OLD
);
3397 case ETHERTYPE_REVARP
:
3398 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3399 (bpf_int32
)ARCTYPE_REVARP
));
3401 case ETHERTYPE_ATALK
:
3402 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3403 (bpf_int32
)ARCTYPE_ATALK
));
3410 case ETHERTYPE_ATALK
:
3411 return gen_true(cstate
);
3413 return gen_false(cstate
);
3420 * XXX - assumes a 2-byte Frame Relay header with
3421 * DLCI and flags. What if the address is longer?
3427 * Check for the special NLPID for IP.
3429 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3431 case ETHERTYPE_IPV6
:
3433 * Check for the special NLPID for IPv6.
3435 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3439 * Check for several OSI protocols.
3441 * Frame Relay packets typically have an OSI
3442 * NLPID at the beginning; we check for each
3445 * What we check for is the NLPID and a frame
3446 * control field of UI, i.e. 0x03 followed
3449 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3450 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3451 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3457 return gen_false(cstate
);
3463 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3465 case DLT_JUNIPER_MFR
:
3466 case DLT_JUNIPER_MLFR
:
3467 case DLT_JUNIPER_MLPPP
:
3468 case DLT_JUNIPER_ATM1
:
3469 case DLT_JUNIPER_ATM2
:
3470 case DLT_JUNIPER_PPPOE
:
3471 case DLT_JUNIPER_PPPOE_ATM
:
3472 case DLT_JUNIPER_GGSN
:
3473 case DLT_JUNIPER_ES
:
3474 case DLT_JUNIPER_MONITOR
:
3475 case DLT_JUNIPER_SERVICES
:
3476 case DLT_JUNIPER_ETHER
:
3477 case DLT_JUNIPER_PPP
:
3478 case DLT_JUNIPER_FRELAY
:
3479 case DLT_JUNIPER_CHDLC
:
3480 case DLT_JUNIPER_VP
:
3481 case DLT_JUNIPER_ST
:
3482 case DLT_JUNIPER_ISM
:
3483 case DLT_JUNIPER_VS
:
3484 case DLT_JUNIPER_SRX_E2E
:
3485 case DLT_JUNIPER_FIBRECHANNEL
:
3486 case DLT_JUNIPER_ATM_CEMIC
:
3488 /* just lets verify the magic number for now -
3489 * on ATM we may have up to 6 different encapsulations on the wire
3490 * and need a lot of heuristics to figure out that the payload
3493 * FIXME encapsulation specific BPF_ filters
3495 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3497 case DLT_BACNET_MS_TP
:
3498 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3501 return gen_ipnet_linktype(cstate
, proto
);
3503 case DLT_LINUX_IRDA
:
3504 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3507 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3510 case DLT_MTP2_WITH_PHDR
:
3511 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3514 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3517 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3519 case DLT_LINUX_LAPD
:
3520 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3522 case DLT_USB_FREEBSD
:
3524 case DLT_USB_LINUX_MMAPPED
:
3526 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3528 case DLT_BLUETOOTH_HCI_H4
:
3529 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3530 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3533 case DLT_CAN_SOCKETCAN
:
3534 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3536 case DLT_IEEE802_15_4
:
3537 case DLT_IEEE802_15_4_LINUX
:
3538 case DLT_IEEE802_15_4_NONASK_PHY
:
3539 case DLT_IEEE802_15_4_NOFCS
:
3540 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3542 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3543 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3546 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3549 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3552 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3555 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3558 /* Using the fixed-size NFLOG header it is possible to tell only
3559 * the address family of the packet, other meaningful data is
3560 * either missing or behind TLVs.
3562 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3566 * Does this link-layer header type have a field
3567 * indicating the type of the next protocol? If
3568 * so, off_linktype.constant_part will be the offset of that
3569 * field in the packet; if not, it will be OFFSET_NOT_SET.
3571 if (cstate
->cgstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3573 * Yes; assume it's an Ethernet type. (If
3574 * it's not, it needs to be handled specially
3577 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3580 * No; report an error.
3582 description
= pcap_datalink_val_to_description(cstate
->cgstate
->linktype
);
3583 if (description
!= NULL
) {
3584 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3587 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3588 cstate
->cgstate
->linktype
);
3596 * Check for an LLC SNAP packet with a given organization code and
3597 * protocol type; we check the entire contents of the 802.2 LLC and
3598 * snap headers, checking for DSAP and SSAP of SNAP and a control
3599 * field of 0x03 in the LLC header, and for the specified organization
3600 * code and protocol type in the SNAP header.
3602 static struct block
*
3603 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3605 u_char snapblock
[8];
3607 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3608 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3609 snapblock
[2] = 0x03; /* control = UI */
3610 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3611 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3612 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3613 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3614 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3615 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3619 * Generate code to match frames with an LLC header.
3622 gen_llc(compiler_state_t
*cstate
)
3624 struct block
*b0
, *b1
;
3626 switch (cstate
->cgstate
->linktype
) {
3630 * We check for an Ethernet type field less than
3631 * 1500, which means it's an 802.3 length field.
3633 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3637 * Now check for the purported DSAP and SSAP not being
3638 * 0xFF, to rule out NetWare-over-802.3.
3640 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3647 * We check for LLC traffic.
3649 b0
= gen_atmtype_abbrev(cstate
, A_LLC
);
3652 case DLT_IEEE802
: /* Token Ring */
3654 * XXX - check for LLC frames.
3656 return gen_true(cstate
);
3660 * XXX - check for LLC frames.
3662 return gen_true(cstate
);
3664 case DLT_ATM_RFC1483
:
3666 * For LLC encapsulation, these are defined to have an
3669 * For VC encapsulation, they don't, but there's no
3670 * way to check for that; the protocol used on the VC
3671 * is negotiated out of band.
3673 return gen_true(cstate
);
3675 case DLT_IEEE802_11
:
3676 case DLT_PRISM_HEADER
:
3677 case DLT_IEEE802_11_RADIO
:
3678 case DLT_IEEE802_11_RADIO_AVS
:
3681 * Check that we have a data frame.
3683 b0
= gen_check_802_11_data_frame(cstate
);
3687 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->cgstate
->linktype
);
3693 gen_llc_i(compiler_state_t
*cstate
)
3695 struct block
*b0
, *b1
;
3699 * Check whether this is an LLC frame.
3701 b0
= gen_llc(cstate
);
3704 * Load the control byte and test the low-order bit; it must
3705 * be clear for I frames.
3707 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3708 b1
= new_block(cstate
, JMP(BPF_JSET
));
3717 gen_llc_s(compiler_state_t
*cstate
)
3719 struct block
*b0
, *b1
;
3722 * Check whether this is an LLC frame.
3724 b0
= gen_llc(cstate
);
3727 * Now compare the low-order 2 bit of the control byte against
3728 * the appropriate value for S frames.
3730 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3736 gen_llc_u(compiler_state_t
*cstate
)
3738 struct block
*b0
, *b1
;
3741 * Check whether this is an LLC frame.
3743 b0
= gen_llc(cstate
);
3746 * Now compare the low-order 2 bit of the control byte against
3747 * the appropriate value for U frames.
3749 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3755 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3757 struct block
*b0
, *b1
;
3760 * Check whether this is an LLC frame.
3762 b0
= gen_llc(cstate
);
3765 * Now check for an S frame with the appropriate type.
3767 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3773 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3775 struct block
*b0
, *b1
;
3778 * Check whether this is an LLC frame.
3780 b0
= gen_llc(cstate
);
3783 * Now check for a U frame with the appropriate type.
3785 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3791 * Generate code to match a particular packet type, for link-layer types
3792 * using 802.2 LLC headers.
3794 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3795 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3797 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3798 * value, if <= ETHERMTU. We use that to determine whether to
3799 * match the DSAP or both DSAP and LSAP or to check the OUI and
3800 * protocol ID in a SNAP header.
3802 static struct block
*
3803 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3806 * XXX - handle token-ring variable-length header.
3812 case LLCSAP_NETBEUI
:
3814 * XXX - should we check both the DSAP and the
3815 * SSAP, like this, or should we check just the
3816 * DSAP, as we do for other SAP values?
3818 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3819 ((proto
<< 8) | proto
));
3823 * XXX - are there ever SNAP frames for IPX on
3824 * non-Ethernet 802.x networks?
3826 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3827 (bpf_int32
)LLCSAP_IPX
);
3829 case ETHERTYPE_ATALK
:
3831 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3832 * SNAP packets with an organization code of
3833 * 0x080007 (Apple, for Appletalk) and a protocol
3834 * type of ETHERTYPE_ATALK (Appletalk).
3836 * XXX - check for an organization code of
3837 * encapsulated Ethernet as well?
3839 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3843 * XXX - we don't have to check for IPX 802.3
3844 * here, but should we check for the IPX Ethertype?
3846 if (proto
<= ETHERMTU
) {
3848 * This is an LLC SAP value, so check
3851 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3854 * This is an Ethernet type; we assume that it's
3855 * unlikely that it'll appear in the right place
3856 * at random, and therefore check only the
3857 * location that would hold the Ethernet type
3858 * in a SNAP frame with an organization code of
3859 * 0x000000 (encapsulated Ethernet).
3861 * XXX - if we were to check for the SNAP DSAP and
3862 * LSAP, as per XXX, and were also to check for an
3863 * organization code of 0x000000 (encapsulated
3864 * Ethernet), we'd do
3866 * return gen_snap(cstate, 0x000000, proto);
3868 * here; for now, we don't, as per the above.
3869 * I don't know whether it's worth the extra CPU
3870 * time to do the right check or not.
3872 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3877 static struct block
*
3878 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3879 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3881 struct block
*b0
, *b1
;
3895 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3896 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3902 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3903 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3908 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3912 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3916 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3920 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3924 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3928 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3934 b0
= gen_linktype(cstate
, proto
);
3935 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3941 static struct block
*
3942 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
3943 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
3945 struct block
*b0
, *b1
;
3960 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3961 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3967 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3968 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3973 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3977 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3981 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3985 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3989 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3993 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3999 /* this order is important */
4000 a
= (uint32_t *)addr
;
4001 m
= (uint32_t *)mask
;
4002 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4003 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4005 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4007 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4009 b0
= gen_linktype(cstate
, proto
);
4015 static struct block
*
4016 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4018 register struct block
*b0
, *b1
;
4022 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4025 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4028 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4029 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4035 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4036 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4041 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4045 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4049 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4053 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4057 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4061 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4069 * Like gen_ehostop, but for DLT_FDDI
4071 static struct block
*
4072 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4074 struct block
*b0
, *b1
;
4078 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->cgstate
->pcap_fddipad
, 6, eaddr
);
4081 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->cgstate
->pcap_fddipad
, 6, eaddr
);
4084 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4085 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4091 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4092 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4097 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4101 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4105 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4109 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4113 bpf_error(cstate
, "'ra' is only supported on 802.11");
4117 bpf_error(cstate
, "'ta' is only supported on 802.11");
4125 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4127 static struct block
*
4128 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4130 register struct block
*b0
, *b1
;
4134 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4137 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4140 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4141 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4147 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4148 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4153 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4157 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4161 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4165 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4169 bpf_error(cstate
, "'ra' is only supported on 802.11");
4173 bpf_error(cstate
, "'ta' is only supported on 802.11");
4181 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4182 * various 802.11 + radio headers.
4184 static struct block
*
4185 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4187 register struct block
*b0
, *b1
, *b2
;
4188 register struct slist
*s
;
4190 #ifdef ENABLE_WLAN_FILTERING_PATCH
4193 * We need to disable the optimizer because the optimizer is buggy
4194 * and wipes out some LD instructions generated by the below
4195 * code to validate the Frame Control bits
4197 cstate
->cgstate
->no_optimize
= 1;
4198 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4205 * For control frames, there is no SA.
4207 * For management frames, SA is at an
4208 * offset of 10 from the beginning of
4211 * For data frames, SA is at an offset
4212 * of 10 from the beginning of the packet
4213 * if From DS is clear, at an offset of
4214 * 16 from the beginning of the packet
4215 * if From DS is set and To DS is clear,
4216 * and an offset of 24 from the beginning
4217 * of the packet if From DS is set and To DS
4222 * Generate the tests to be done for data frames
4225 * First, check for To DS set, i.e. check "link[1] & 0x01".
4227 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4228 b1
= new_block(cstate
, JMP(BPF_JSET
));
4229 b1
->s
.k
= 0x01; /* To DS */
4233 * If To DS is set, the SA is at 24.
4235 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4239 * Now, check for To DS not set, i.e. check
4240 * "!(link[1] & 0x01)".
4242 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4243 b2
= new_block(cstate
, JMP(BPF_JSET
));
4244 b2
->s
.k
= 0x01; /* To DS */
4249 * If To DS is not set, the SA is at 16.
4251 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4255 * Now OR together the last two checks. That gives
4256 * the complete set of checks for data frames with
4262 * Now check for From DS being set, and AND that with
4263 * the ORed-together checks.
4265 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4266 b1
= new_block(cstate
, JMP(BPF_JSET
));
4267 b1
->s
.k
= 0x02; /* From DS */
4272 * Now check for data frames with From DS not set.
4274 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4275 b2
= new_block(cstate
, JMP(BPF_JSET
));
4276 b2
->s
.k
= 0x02; /* From DS */
4281 * If From DS isn't set, the SA is at 10.
4283 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4287 * Now OR together the checks for data frames with
4288 * From DS not set and for data frames with From DS
4289 * set; that gives the checks done for data frames.
4294 * Now check for a data frame.
4295 * I.e, check "link[0] & 0x08".
4297 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4298 b1
= new_block(cstate
, JMP(BPF_JSET
));
4303 * AND that with the checks done for data frames.
4308 * If the high-order bit of the type value is 0, this
4309 * is a management frame.
4310 * I.e, check "!(link[0] & 0x08)".
4312 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4313 b2
= new_block(cstate
, JMP(BPF_JSET
));
4319 * For management frames, the SA is at 10.
4321 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4325 * OR that with the checks done for data frames.
4326 * That gives the checks done for management and
4332 * If the low-order bit of the type value is 1,
4333 * this is either a control frame or a frame
4334 * with a reserved type, and thus not a
4337 * I.e., check "!(link[0] & 0x04)".
4339 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4340 b1
= new_block(cstate
, JMP(BPF_JSET
));
4346 * AND that with the checks for data and management
4356 * For control frames, there is no DA.
4358 * For management frames, DA is at an
4359 * offset of 4 from the beginning of
4362 * For data frames, DA is at an offset
4363 * of 4 from the beginning of the packet
4364 * if To DS is clear and at an offset of
4365 * 16 from the beginning of the packet
4370 * Generate the tests to be done for data frames.
4372 * First, check for To DS set, i.e. "link[1] & 0x01".
4374 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4375 b1
= new_block(cstate
, JMP(BPF_JSET
));
4376 b1
->s
.k
= 0x01; /* To DS */
4380 * If To DS is set, the DA is at 16.
4382 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4386 * Now, check for To DS not set, i.e. check
4387 * "!(link[1] & 0x01)".
4389 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4390 b2
= new_block(cstate
, JMP(BPF_JSET
));
4391 b2
->s
.k
= 0x01; /* To DS */
4396 * If To DS is not set, the DA is at 4.
4398 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4402 * Now OR together the last two checks. That gives
4403 * the complete set of checks for data frames.
4408 * Now check for a data frame.
4409 * I.e, check "link[0] & 0x08".
4411 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4412 b1
= new_block(cstate
, JMP(BPF_JSET
));
4417 * AND that with the checks done for data frames.
4422 * If the high-order bit of the type value is 0, this
4423 * is a management frame.
4424 * I.e, check "!(link[0] & 0x08)".
4426 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4427 b2
= new_block(cstate
, JMP(BPF_JSET
));
4433 * For management frames, the DA is at 4.
4435 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4439 * OR that with the checks done for data frames.
4440 * That gives the checks done for management and
4446 * If the low-order bit of the type value is 1,
4447 * this is either a control frame or a frame
4448 * with a reserved type, and thus not a
4451 * I.e., check "!(link[0] & 0x04)".
4453 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4454 b1
= new_block(cstate
, JMP(BPF_JSET
));
4460 * AND that with the checks for data and management
4467 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4468 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4474 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4475 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4480 * XXX - add BSSID keyword?
4483 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4487 * Not present in CTS or ACK control frames.
4489 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4490 IEEE80211_FC0_TYPE_MASK
);
4492 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4493 IEEE80211_FC0_SUBTYPE_MASK
);
4495 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4496 IEEE80211_FC0_SUBTYPE_MASK
);
4500 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4506 * Not present in control frames.
4508 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4509 IEEE80211_FC0_TYPE_MASK
);
4511 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4517 * Present only if the direction mask has both "From DS"
4518 * and "To DS" set. Neither control frames nor management
4519 * frames should have both of those set, so we don't
4520 * check the frame type.
4522 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4523 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4524 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4530 * Not present in management frames; addr1 in other
4535 * If the high-order bit of the type value is 0, this
4536 * is a management frame.
4537 * I.e, check "(link[0] & 0x08)".
4539 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4540 b1
= new_block(cstate
, JMP(BPF_JSET
));
4547 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4550 * AND that with the check of addr1.
4557 * Not present in management frames; addr2, if present,
4562 * Not present in CTS or ACK control frames.
4564 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4565 IEEE80211_FC0_TYPE_MASK
);
4567 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4568 IEEE80211_FC0_SUBTYPE_MASK
);
4570 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4571 IEEE80211_FC0_SUBTYPE_MASK
);
4577 * If the high-order bit of the type value is 0, this
4578 * is a management frame.
4579 * I.e, check "(link[0] & 0x08)".
4581 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4582 b1
= new_block(cstate
, JMP(BPF_JSET
));
4587 * AND that with the check for frames other than
4588 * CTS and ACK frames.
4595 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4604 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4605 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4606 * as the RFC states.)
4608 static struct block
*
4609 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4611 register struct block
*b0
, *b1
;
4615 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4618 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4621 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4622 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4628 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4629 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4634 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4638 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4642 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4646 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4650 bpf_error(cstate
, "'ra' is only supported on 802.11");
4654 bpf_error(cstate
, "'ta' is only supported on 802.11");
4662 * This is quite tricky because there may be pad bytes in front of the
4663 * DECNET header, and then there are two possible data packet formats that
4664 * carry both src and dst addresses, plus 5 packet types in a format that
4665 * carries only the src node, plus 2 types that use a different format and
4666 * also carry just the src node.
4670 * Instead of doing those all right, we just look for data packets with
4671 * 0 or 1 bytes of padding. If you want to look at other packets, that
4672 * will require a lot more hacking.
4674 * To add support for filtering on DECNET "areas" (network numbers)
4675 * one would want to add a "mask" argument to this routine. That would
4676 * make the filter even more inefficient, although one could be clever
4677 * and not generate masking instructions if the mask is 0xFFFF.
4679 static struct block
*
4680 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4682 struct block
*b0
, *b1
, *b2
, *tmp
;
4683 u_int offset_lh
; /* offset if long header is received */
4684 u_int offset_sh
; /* offset if short header is received */
4689 offset_sh
= 1; /* follows flags */
4690 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4694 offset_sh
= 3; /* follows flags, dstnode */
4695 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4699 /* Inefficient because we do our Calvinball dance twice */
4700 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4701 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4707 /* Inefficient because we do our Calvinball dance twice */
4708 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4709 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4714 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4718 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4722 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4726 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4730 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4734 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4740 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4741 /* Check for pad = 1, long header case */
4742 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4743 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4744 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4745 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4747 /* Check for pad = 0, long header case */
4748 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4749 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4752 /* Check for pad = 1, short header case */
4753 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4754 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4755 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4758 /* Check for pad = 0, short header case */
4759 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4760 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4764 /* Combine with test for cstate->cgstate->linktype */
4770 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4771 * test the bottom-of-stack bit, and then check the version number
4772 * field in the IP header.
4774 static struct block
*
4775 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4777 struct block
*b0
, *b1
;
4782 /* match the bottom-of-stack bit */
4783 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4784 /* match the IPv4 version number */
4785 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4790 /* match the bottom-of-stack bit */
4791 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4792 /* match the IPv4 version number */
4793 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4802 static struct block
*
4803 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4804 int proto
, int dir
, int type
)
4806 struct block
*b0
, *b1
;
4807 const char *typestr
;
4817 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4819 * Only check for non-IPv4 addresses if we're not
4820 * checking MPLS-encapsulated packets.
4822 if (cstate
->cgstate
->label_stack_depth
== 0) {
4823 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4825 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4831 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4834 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4837 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4840 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4843 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4846 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4849 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4852 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4855 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4858 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4861 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4864 return gen_dnhostop(cstate
, addr
, dir
);
4867 bpf_error(cstate
, "LAT host filtering not implemented");
4870 bpf_error(cstate
, "SCA host filtering not implemented");
4873 bpf_error(cstate
, "MOPRC host filtering not implemented");
4876 bpf_error(cstate
, "MOPDL host filtering not implemented");
4879 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4882 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4885 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4888 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4891 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4894 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4897 bpf_error(cstate
, "AARP host filtering not implemented");
4900 bpf_error(cstate
, "ISO host filtering not implemented");
4903 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4906 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4909 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4912 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4915 bpf_error(cstate
, "IPX host filtering not implemented");
4918 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4921 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
4924 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
4927 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
4930 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
4933 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
4936 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
4939 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
4942 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
4945 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
4954 static struct block
*
4955 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4956 struct in6_addr
*mask
, int proto
, int dir
, int type
)
4958 const char *typestr
;
4968 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
4971 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
4974 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
4977 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
4980 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
4983 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
4986 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
4989 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
4992 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
4995 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
4998 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5001 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5004 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5007 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5010 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5013 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5016 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5019 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5022 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5025 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5028 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5031 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5034 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5037 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5040 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5043 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5046 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5049 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5052 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5055 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5058 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5061 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5064 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5067 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5070 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5073 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5076 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5079 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5082 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5085 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5095 static struct block
*
5096 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5097 struct addrinfo
*alist
, int proto
, int dir
)
5099 struct block
*b0
, *b1
, *tmp
;
5100 struct addrinfo
*ai
;
5101 struct sockaddr_in
*sin
;
5104 bpf_error(cstate
, "direction applied to 'gateway'");
5111 switch (cstate
->cgstate
->linktype
) {
5113 case DLT_NETANALYZER
:
5114 case DLT_NETANALYZER_TRANSPARENT
:
5115 b1
= gen_prevlinkhdr_check(cstate
);
5116 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5121 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5124 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5126 case DLT_IEEE802_11
:
5127 case DLT_PRISM_HEADER
:
5128 case DLT_IEEE802_11_RADIO_AVS
:
5129 case DLT_IEEE802_11_RADIO
:
5131 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5135 * This is LLC-multiplexed traffic; if it were
5136 * LANE, cstate->cgstate->linktype would have been set to
5140 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5142 case DLT_IP_OVER_FC
:
5143 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5147 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5150 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5152 * Does it have an address?
5154 if (ai
->ai_addr
!= NULL
) {
5156 * Yes. Is it an IPv4 address?
5158 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5160 * Generate an entry for it.
5162 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5163 tmp
= gen_host(cstate
,
5164 ntohl(sin
->sin_addr
.s_addr
),
5165 0xffffffff, proto
, Q_OR
, Q_HOST
);
5167 * Is it the *first* IPv4 address?
5171 * Yes, so start with it.
5176 * No, so OR it into the
5188 * No IPv4 addresses found.
5196 bpf_error(cstate
, "illegal modifier of 'gateway'");
5202 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5210 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5211 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5216 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5217 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5222 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5223 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5228 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5231 #ifndef IPPROTO_IGMP
5232 #define IPPROTO_IGMP 2
5236 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5239 #ifndef IPPROTO_IGRP
5240 #define IPPROTO_IGRP 9
5243 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5247 #define IPPROTO_PIM 103
5251 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5252 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5256 #ifndef IPPROTO_VRRP
5257 #define IPPROTO_VRRP 112
5261 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5264 #ifndef IPPROTO_CARP
5265 #define IPPROTO_CARP 112
5269 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5273 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5277 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5281 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5285 bpf_error(cstate
, "link layer applied in wrong context");
5288 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5292 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5296 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5300 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5304 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5308 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5312 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5316 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5319 #ifndef IPPROTO_ICMPV6
5320 #define IPPROTO_ICMPV6 58
5323 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5327 #define IPPROTO_AH 51
5330 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5331 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5336 #define IPPROTO_ESP 50
5339 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5340 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5345 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5349 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5353 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5356 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5357 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5358 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5360 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5362 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5364 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5368 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5369 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5370 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5372 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5374 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5376 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5380 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5381 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5382 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5384 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5389 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5390 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5395 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5396 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5398 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5400 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5405 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5406 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5411 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5412 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5417 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5421 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5425 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5429 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5433 bpf_error(cstate
, "'radio' is not a valid protocol type");
5441 static struct block
*
5442 gen_ipfrag(compiler_state_t
*cstate
)
5447 /* not IPv4 frag other than the first frag */
5448 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5449 b
= new_block(cstate
, JMP(BPF_JSET
));
5458 * Generate a comparison to a port value in the transport-layer header
5459 * at the specified offset from the beginning of that header.
5461 * XXX - this handles a variable-length prefix preceding the link-layer
5462 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5463 * variable-length link-layer headers (such as Token Ring or 802.11
5466 static struct block
*
5467 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5469 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5472 static struct block
*
5473 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5475 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5479 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5481 struct block
*b0
, *b1
, *tmp
;
5483 /* ip proto 'proto' and not a fragment other than the first fragment */
5484 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5485 b0
= gen_ipfrag(cstate
);
5490 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5494 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5498 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5499 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5505 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5506 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5511 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5515 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5519 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5523 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5527 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5531 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5542 static struct block
*
5543 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5545 struct block
*b0
, *b1
, *tmp
;
5550 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5551 * not LLC encapsulation with LLCSAP_IP.
5553 * For IEEE 802 networks - which includes 802.5 token ring
5554 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5555 * says that SNAP encapsulation is used, not LLC encapsulation
5558 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5559 * RFC 2225 say that SNAP encapsulation is used, not LLC
5560 * encapsulation with LLCSAP_IP.
5562 * So we always check for ETHERTYPE_IP.
5564 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5570 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5574 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5575 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5577 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5589 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5591 struct block
*b0
, *b1
, *tmp
;
5593 /* ip6 proto 'proto' */
5594 /* XXX - catch the first fragment of a fragmented packet? */
5595 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5599 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5603 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5607 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5608 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5614 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5615 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5627 static struct block
*
5628 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5630 struct block
*b0
, *b1
, *tmp
;
5632 /* link proto ip6 */
5633 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5639 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5643 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5644 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5646 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5657 /* gen_portrange code */
5658 static struct block
*
5659 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5662 struct block
*b1
, *b2
;
5666 * Reverse the order of the ports, so v1 is the lower one.
5675 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5676 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5684 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5687 struct block
*b0
, *b1
, *tmp
;
5689 /* ip proto 'proto' and not a fragment other than the first fragment */
5690 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5691 b0
= gen_ipfrag(cstate
);
5696 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5700 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5704 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5705 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5711 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5712 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5717 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5721 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5725 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5729 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5733 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5737 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5748 static struct block
*
5749 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5752 struct block
*b0
, *b1
, *tmp
;
5755 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5761 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5765 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5766 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5768 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5779 static struct block
*
5780 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5783 struct block
*b1
, *b2
;
5787 * Reverse the order of the ports, so v1 is the lower one.
5796 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5797 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5805 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5808 struct block
*b0
, *b1
, *tmp
;
5810 /* ip6 proto 'proto' */
5811 /* XXX - catch the first fragment of a fragmented packet? */
5812 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5816 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5820 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5824 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5825 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5831 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5832 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5844 static struct block
*
5845 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5848 struct block
*b0
, *b1
, *tmp
;
5850 /* link proto ip6 */
5851 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5857 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5861 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5862 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5864 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5876 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5885 v
= pcap_nametoproto(name
);
5886 if (v
== PROTO_UNDEF
)
5887 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5891 /* XXX should look up h/w protocol type based on cstate->cgstate->linktype */
5892 v
= pcap_nametoeproto(name
);
5893 if (v
== PROTO_UNDEF
) {
5894 v
= pcap_nametollc(name
);
5895 if (v
== PROTO_UNDEF
)
5896 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5901 if (strcmp(name
, "esis") == 0)
5903 else if (strcmp(name
, "isis") == 0)
5905 else if (strcmp(name
, "clnp") == 0)
5908 bpf_error(cstate
, "unknown osi proto '%s'", name
);
5920 gen_joinsp(struct stmt
**s
, int n
)
5926 static struct block
*
5927 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
5929 #ifdef NO_PROTOCHAIN
5930 return gen_proto(cstate
, v
, proto
, dir
);
5932 struct block
*b0
, *b
;
5933 struct slist
*s
[100];
5934 int fix2
, fix3
, fix4
, fix5
;
5935 int ahcheck
, again
, end
;
5937 int reg2
= alloc_reg(cstate
);
5939 memset(s
, 0, sizeof(s
));
5940 fix3
= fix4
= fix5
= 0;
5947 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
5948 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
5952 bpf_error(cstate
, "bad protocol applied for 'protochain'");
5957 * We don't handle variable-length prefixes before the link-layer
5958 * header, or variable-length link-layer headers, here yet.
5959 * We might want to add BPF instructions to do the protochain
5960 * work, to simplify that and, on platforms that have a BPF
5961 * interpreter with the new instructions, let the filtering
5962 * be done in the kernel. (We already require a modified BPF
5963 * engine to do the protochain stuff, to support backward
5964 * branches, and backward branch support is unlikely to appear
5965 * in kernel BPF engines.)
5967 if (cstate
->cgstate
->off_linkpl
.is_variable
)
5968 bpf_error(cstate
, "'protochain' not supported with variable length headers");
5970 cstate
->cgstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
5973 * s[0] is a dummy entry to protect other BPF insn from damage
5974 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5975 * hard to find interdependency made by jump table fixup.
5978 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
5983 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5986 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
5987 s
[i
]->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
+ 9;
5989 /* X = ip->ip_hl << 2 */
5990 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
5991 s
[i
]->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
;
5996 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5998 /* A = ip6->ip_nxt */
5999 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6000 s
[i
]->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
+ 6;
6002 /* X = sizeof(struct ip6_hdr) */
6003 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6009 bpf_error(cstate
, "unsupported proto to gen_protochain");
6013 /* again: if (A == v) goto end; else fall through; */
6015 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6017 s
[i
]->s
.jt
= NULL
; /*later*/
6018 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6022 #ifndef IPPROTO_NONE
6023 #define IPPROTO_NONE 59
6025 /* if (A == IPPROTO_NONE) goto end */
6026 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6027 s
[i
]->s
.jt
= NULL
; /*later*/
6028 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6029 s
[i
]->s
.k
= IPPROTO_NONE
;
6030 s
[fix5
]->s
.jf
= s
[i
];
6034 if (proto
== Q_IPV6
) {
6035 int v6start
, v6end
, v6advance
, j
;
6038 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6039 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6040 s
[i
]->s
.jt
= NULL
; /*later*/
6041 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6042 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6043 s
[fix2
]->s
.jf
= s
[i
];
6045 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6046 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6047 s
[i
]->s
.jt
= NULL
; /*later*/
6048 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6049 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6051 /* if (A == IPPROTO_ROUTING) goto v6advance */
6052 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6053 s
[i
]->s
.jt
= NULL
; /*later*/
6054 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6055 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6057 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6058 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6059 s
[i
]->s
.jt
= NULL
; /*later*/
6060 s
[i
]->s
.jf
= NULL
; /*later*/
6061 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6071 * A = P[X + packet head];
6072 * X = X + (P[X + packet head + 1] + 1) * 8;
6074 /* A = P[X + packet head] */
6075 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6076 s
[i
]->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
;
6079 s
[i
] = new_stmt(cstate
, BPF_ST
);
6082 /* A = P[X + packet head + 1]; */
6083 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6084 s
[i
]->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
+ 1;
6087 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6091 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6095 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6099 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6102 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6106 /* goto again; (must use BPF_JA for backward jump) */
6107 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6108 s
[i
]->s
.k
= again
- i
- 1;
6109 s
[i
- 1]->s
.jf
= s
[i
];
6113 for (j
= v6start
; j
<= v6end
; j
++)
6114 s
[j
]->s
.jt
= s
[v6advance
];
6117 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6119 s
[fix2
]->s
.jf
= s
[i
];
6125 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6126 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6127 s
[i
]->s
.jt
= NULL
; /*later*/
6128 s
[i
]->s
.jf
= NULL
; /*later*/
6129 s
[i
]->s
.k
= IPPROTO_AH
;
6131 s
[fix3
]->s
.jf
= s
[ahcheck
];
6138 * X = X + (P[X + 1] + 2) * 4;
6141 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6143 /* A = P[X + packet head]; */
6144 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6145 s
[i
]->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
;
6148 s
[i
] = new_stmt(cstate
, BPF_ST
);
6152 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6155 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6159 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6161 /* A = P[X + packet head] */
6162 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6163 s
[i
]->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
;
6166 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6170 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6174 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6177 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6181 /* goto again; (must use BPF_JA for backward jump) */
6182 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6183 s
[i
]->s
.k
= again
- i
- 1;
6188 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6190 s
[fix2
]->s
.jt
= s
[end
];
6191 s
[fix4
]->s
.jf
= s
[end
];
6192 s
[fix5
]->s
.jt
= s
[end
];
6199 for (i
= 0; i
< max
- 1; i
++)
6200 s
[i
]->next
= s
[i
+ 1];
6201 s
[max
- 1]->next
= NULL
;
6206 b
= new_block(cstate
, JMP(BPF_JEQ
));
6207 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6210 free_reg(cstate
, reg2
);
6217 static struct block
*
6218 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6221 struct block
*b0
, *b1
;
6224 * A data frame has the 0x08 bit (b3) in the frame control field set
6225 * and the 0x04 bit (b2) clear.
6227 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6228 b0
= new_block(cstate
, JMP(BPF_JSET
));
6232 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6233 b1
= new_block(cstate
, JMP(BPF_JSET
));
6244 * Generate code that checks whether the packet is a packet for protocol
6245 * <proto> and whether the type field in that protocol's header has
6246 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6247 * IP packet and checks the protocol number in the IP header against <v>.
6249 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6250 * against Q_IP and Q_IPV6.
6252 static struct block
*
6253 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6255 struct block
*b0
, *b1
;
6260 if (dir
!= Q_DEFAULT
)
6261 bpf_error(cstate
, "direction applied to 'proto'");
6265 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6266 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6271 return gen_linktype(cstate
, v
);
6275 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6276 * not LLC encapsulation with LLCSAP_IP.
6278 * For IEEE 802 networks - which includes 802.5 token ring
6279 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6280 * says that SNAP encapsulation is used, not LLC encapsulation
6283 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6284 * RFC 2225 say that SNAP encapsulation is used, not LLC
6285 * encapsulation with LLCSAP_IP.
6287 * So we always check for ETHERTYPE_IP.
6289 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6291 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6293 b1
= gen_protochain(cstate
, v
, Q_IP
);
6299 bpf_error(cstate
, "arp does not encapsulate another protocol");
6303 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6307 bpf_error(cstate
, "'sctp proto' is bogus");
6311 bpf_error(cstate
, "'tcp proto' is bogus");
6315 bpf_error(cstate
, "'udp proto' is bogus");
6319 bpf_error(cstate
, "'icmp proto' is bogus");
6323 bpf_error(cstate
, "'igmp proto' is bogus");
6327 bpf_error(cstate
, "'igrp proto' is bogus");
6331 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6335 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6339 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6343 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6347 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6351 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6355 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6358 * Also check for a fragment header before the final
6361 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6362 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6364 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6367 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6373 bpf_error(cstate
, "'icmp6 proto' is bogus");
6377 bpf_error(cstate
, "'ah proto' is bogus");
6381 bpf_error(cstate
, "'ah proto' is bogus");
6385 bpf_error(cstate
, "'pim proto' is bogus");
6389 bpf_error(cstate
, "'vrrp proto' is bogus");
6393 bpf_error(cstate
, "'aarp proto' is bogus");
6397 switch (cstate
->cgstate
->linktype
) {
6401 * Frame Relay packets typically have an OSI
6402 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6403 * generates code to check for all the OSI
6404 * NLPIDs, so calling it and then adding a check
6405 * for the particular NLPID for which we're
6406 * looking is bogus, as we can just check for
6409 * What we check for is the NLPID and a frame
6410 * control field value of UI, i.e. 0x03 followed
6413 * XXX - assumes a 2-byte Frame Relay header with
6414 * DLCI and flags. What if the address is longer?
6416 * XXX - what about SNAP-encapsulated frames?
6418 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6424 * Cisco uses an Ethertype lookalike - for OSI,
6427 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6428 /* OSI in C-HDLC is stuffed with a fudge byte */
6429 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (bpf_int32
)v
);
6434 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6435 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (bpf_int32
)v
);
6441 bpf_error(cstate
, "'esis proto' is bogus");
6445 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6447 * 4 is the offset of the PDU type relative to the IS-IS
6450 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (bpf_int32
)v
);
6455 bpf_error(cstate
, "'clnp proto' is not supported");
6459 bpf_error(cstate
, "'stp proto' is bogus");
6463 bpf_error(cstate
, "'ipx proto' is bogus");
6467 bpf_error(cstate
, "'netbeui proto' is bogus");
6471 bpf_error(cstate
, "'l1 proto' is bogus");
6475 bpf_error(cstate
, "'l2 proto' is bogus");
6479 bpf_error(cstate
, "'iih proto' is bogus");
6483 bpf_error(cstate
, "'snp proto' is bogus");
6487 bpf_error(cstate
, "'csnp proto' is bogus");
6491 bpf_error(cstate
, "'psnp proto' is bogus");
6495 bpf_error(cstate
, "'lsp proto' is bogus");
6499 bpf_error(cstate
, "'radio proto' is bogus");
6503 bpf_error(cstate
, "'carp proto' is bogus");
6514 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6516 int proto
= q
.proto
;
6520 bpf_u_int32 mask
, addr
;
6521 struct addrinfo
*res
, *res0
;
6522 struct sockaddr_in
*sin4
;
6525 struct sockaddr_in6
*sin6
;
6526 struct in6_addr mask128
;
6528 struct block
*b
, *tmp
;
6529 int port
, real_proto
;
6535 addr
= pcap_nametonetaddr(name
);
6537 bpf_error(cstate
, "unknown network '%s'", name
);
6538 /* Left justify network addr and calculate its network mask */
6540 while (addr
&& (addr
& 0xff000000) == 0) {
6544 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6548 if (proto
== Q_LINK
) {
6549 switch (cstate
->cgstate
->linktype
) {
6552 case DLT_NETANALYZER
:
6553 case DLT_NETANALYZER_TRANSPARENT
:
6554 eaddr
= pcap_ether_hostton(name
);
6557 "unknown ether host '%s'", name
);
6558 tmp
= gen_prevlinkhdr_check(cstate
);
6559 b
= gen_ehostop(cstate
, eaddr
, dir
);
6566 eaddr
= pcap_ether_hostton(name
);
6569 "unknown FDDI host '%s'", name
);
6570 b
= gen_fhostop(cstate
, eaddr
, dir
);
6575 eaddr
= pcap_ether_hostton(name
);
6578 "unknown token ring host '%s'", name
);
6579 b
= gen_thostop(cstate
, eaddr
, dir
);
6583 case DLT_IEEE802_11
:
6584 case DLT_PRISM_HEADER
:
6585 case DLT_IEEE802_11_RADIO_AVS
:
6586 case DLT_IEEE802_11_RADIO
:
6588 eaddr
= pcap_ether_hostton(name
);
6591 "unknown 802.11 host '%s'", name
);
6592 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6596 case DLT_IP_OVER_FC
:
6597 eaddr
= pcap_ether_hostton(name
);
6600 "unknown Fibre Channel host '%s'", name
);
6601 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6606 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6607 } else if (proto
== Q_DECNET
) {
6608 unsigned short dn_addr
;
6610 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6612 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6614 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6619 * I don't think DECNET hosts can be multihomed, so
6620 * there is no need to build up a list of addresses
6622 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6625 memset(&mask128
, 0xff, sizeof(mask128
));
6627 res0
= res
= pcap_nametoaddrinfo(name
);
6629 bpf_error(cstate
, "unknown host '%s'", name
);
6630 cstate
->cgstate
->ai
= res
;
6636 if (cstate
->cgstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6637 tproto
== Q_DEFAULT
) {
6643 for (res
= res0
; res
; res
= res
->ai_next
) {
6644 switch (res
->ai_family
) {
6647 if (tproto
== Q_IPV6
)
6651 sin4
= (struct sockaddr_in
*)
6653 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6654 0xffffffff, tproto
, dir
, q
.addr
);
6658 if (tproto6
== Q_IP
)
6661 sin6
= (struct sockaddr_in6
*)
6663 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6664 &mask128
, tproto6
, dir
, q
.addr
);
6674 cstate
->cgstate
->ai
= NULL
;
6677 bpf_error(cstate
, "unknown host '%s'%s", name
,
6678 (proto
== Q_DEFAULT
)
6680 : " for specified address family");
6686 if (proto
!= Q_DEFAULT
&&
6687 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6688 bpf_error(cstate
, "illegal qualifier of 'port'");
6689 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6690 bpf_error(cstate
, "unknown port '%s'", name
);
6691 if (proto
== Q_UDP
) {
6692 if (real_proto
== IPPROTO_TCP
)
6693 bpf_error(cstate
, "port '%s' is tcp", name
);
6694 else if (real_proto
== IPPROTO_SCTP
)
6695 bpf_error(cstate
, "port '%s' is sctp", name
);
6697 /* override PROTO_UNDEF */
6698 real_proto
= IPPROTO_UDP
;
6700 if (proto
== Q_TCP
) {
6701 if (real_proto
== IPPROTO_UDP
)
6702 bpf_error(cstate
, "port '%s' is udp", name
);
6704 else if (real_proto
== IPPROTO_SCTP
)
6705 bpf_error(cstate
, "port '%s' is sctp", name
);
6707 /* override PROTO_UNDEF */
6708 real_proto
= IPPROTO_TCP
;
6710 if (proto
== Q_SCTP
) {
6711 if (real_proto
== IPPROTO_UDP
)
6712 bpf_error(cstate
, "port '%s' is udp", name
);
6714 else if (real_proto
== IPPROTO_TCP
)
6715 bpf_error(cstate
, "port '%s' is tcp", name
);
6717 /* override PROTO_UNDEF */
6718 real_proto
= IPPROTO_SCTP
;
6721 bpf_error(cstate
, "illegal port number %d < 0", port
);
6723 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6724 b
= gen_port(cstate
, port
, real_proto
, dir
);
6725 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6729 if (proto
!= Q_DEFAULT
&&
6730 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6731 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6732 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6733 bpf_error(cstate
, "unknown port in range '%s'", name
);
6734 if (proto
== Q_UDP
) {
6735 if (real_proto
== IPPROTO_TCP
)
6736 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6737 else if (real_proto
== IPPROTO_SCTP
)
6738 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6740 /* override PROTO_UNDEF */
6741 real_proto
= IPPROTO_UDP
;
6743 if (proto
== Q_TCP
) {
6744 if (real_proto
== IPPROTO_UDP
)
6745 bpf_error(cstate
, "port in range '%s' is udp", name
);
6746 else if (real_proto
== IPPROTO_SCTP
)
6747 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6749 /* override PROTO_UNDEF */
6750 real_proto
= IPPROTO_TCP
;
6752 if (proto
== Q_SCTP
) {
6753 if (real_proto
== IPPROTO_UDP
)
6754 bpf_error(cstate
, "port in range '%s' is udp", name
);
6755 else if (real_proto
== IPPROTO_TCP
)
6756 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6758 /* override PROTO_UNDEF */
6759 real_proto
= IPPROTO_SCTP
;
6762 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6764 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6766 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6768 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6770 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6771 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6776 eaddr
= pcap_ether_hostton(name
);
6778 bpf_error(cstate
, "unknown ether host: %s", name
);
6780 res
= pcap_nametoaddrinfo(name
);
6781 cstate
->cgstate
->ai
= res
;
6783 bpf_error(cstate
, "unknown host '%s'", name
);
6784 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6785 cstate
->cgstate
->ai
= NULL
;
6788 bpf_error(cstate
, "unknown host '%s'", name
);
6791 bpf_error(cstate
, "'gateway' not supported in this configuration");
6795 real_proto
= lookup_proto(cstate
, name
, proto
);
6796 if (real_proto
>= 0)
6797 return gen_proto(cstate
, real_proto
, proto
, dir
);
6799 bpf_error(cstate
, "unknown protocol: %s", name
);
6802 real_proto
= lookup_proto(cstate
, name
, proto
);
6803 if (real_proto
>= 0)
6804 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6806 bpf_error(cstate
, "unknown protocol: %s", name
);
6817 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6818 unsigned int masklen
, struct qual q
)
6820 register int nlen
, mlen
;
6823 nlen
= __pcap_atoin(s1
, &n
);
6824 /* Promote short ipaddr */
6828 mlen
= __pcap_atoin(s2
, &m
);
6829 /* Promote short ipaddr */
6832 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6835 /* Convert mask len to mask */
6837 bpf_error(cstate
, "mask length must be <= 32");
6840 * X << 32 is not guaranteed by C to be 0; it's
6845 m
= 0xffffffff << (32 - masklen
);
6847 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6854 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6857 bpf_error(cstate
, "Mask syntax for networks only");
6864 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6867 int proto
= q
.proto
;
6873 else if (q
.proto
== Q_DECNET
) {
6874 vlen
= __pcap_atodn(s
, &v
);
6876 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6878 vlen
= __pcap_atoin(s
, &v
);
6885 if (proto
== Q_DECNET
)
6886 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6887 else if (proto
== Q_LINK
) {
6888 bpf_error(cstate
, "illegal link layer address");
6891 if (s
== NULL
&& q
.addr
== Q_NET
) {
6892 /* Promote short net number */
6893 while (v
&& (v
& 0xff000000) == 0) {
6898 /* Promote short ipaddr */
6900 mask
<<= 32 - vlen
;
6902 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
6907 proto
= IPPROTO_UDP
;
6908 else if (proto
== Q_TCP
)
6909 proto
= IPPROTO_TCP
;
6910 else if (proto
== Q_SCTP
)
6911 proto
= IPPROTO_SCTP
;
6912 else if (proto
== Q_DEFAULT
)
6913 proto
= PROTO_UNDEF
;
6915 bpf_error(cstate
, "illegal qualifier of 'port'");
6918 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6922 b
= gen_port(cstate
, (int)v
, proto
, dir
);
6923 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
6929 proto
= IPPROTO_UDP
;
6930 else if (proto
== Q_TCP
)
6931 proto
= IPPROTO_TCP
;
6932 else if (proto
== Q_SCTP
)
6933 proto
= IPPROTO_SCTP
;
6934 else if (proto
== Q_DEFAULT
)
6935 proto
= PROTO_UNDEF
;
6937 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6940 bpf_error(cstate
, "illegal port number %u > 65535", v
);
6944 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
6945 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
6950 bpf_error(cstate
, "'gateway' requires a name");
6954 return gen_proto(cstate
, (int)v
, proto
, dir
);
6957 return gen_protochain(cstate
, (int)v
, proto
, dir
);
6972 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6973 unsigned int masklen
, struct qual q
)
6975 struct addrinfo
*res
;
6976 struct in6_addr
*addr
;
6977 struct in6_addr mask
;
6982 bpf_error(cstate
, "no mask %s supported", s2
);
6984 res
= pcap_nametoaddrinfo(s1
);
6986 bpf_error(cstate
, "invalid ip6 address %s", s1
);
6987 cstate
->cgstate
->ai
= res
;
6989 bpf_error(cstate
, "%s resolved to multiple address", s1
);
6990 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6992 if (sizeof(mask
) * 8 < masklen
)
6993 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6994 memset(&mask
, 0, sizeof(mask
));
6995 memset(&mask
, 0xff, masklen
/ 8);
6997 mask
.s6_addr
[masklen
/ 8] =
6998 (0xff << (8 - masklen
% 8)) & 0xff;
7001 a
= (uint32_t *)addr
;
7002 m
= (uint32_t *)&mask
;
7003 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7004 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7005 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7013 bpf_error(cstate
, "Mask syntax for networks only");
7017 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7018 cstate
->cgstate
->ai
= NULL
;
7023 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7030 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7032 struct block
*b
, *tmp
;
7034 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7035 cstate
->cgstate
->e
= pcap_ether_aton(s
);
7036 if (cstate
->cgstate
->e
== NULL
)
7037 bpf_error(cstate
, "malloc");
7038 switch (cstate
->cgstate
->linktype
) {
7040 case DLT_NETANALYZER
:
7041 case DLT_NETANALYZER_TRANSPARENT
:
7042 tmp
= gen_prevlinkhdr_check(cstate
);
7043 b
= gen_ehostop(cstate
, cstate
->cgstate
->e
, (int)q
.dir
);
7048 b
= gen_fhostop(cstate
, cstate
->cgstate
->e
, (int)q
.dir
);
7051 b
= gen_thostop(cstate
, cstate
->cgstate
->e
, (int)q
.dir
);
7053 case DLT_IEEE802_11
:
7054 case DLT_PRISM_HEADER
:
7055 case DLT_IEEE802_11_RADIO_AVS
:
7056 case DLT_IEEE802_11_RADIO
:
7058 b
= gen_wlanhostop(cstate
, cstate
->cgstate
->e
, (int)q
.dir
);
7060 case DLT_IP_OVER_FC
:
7061 b
= gen_ipfchostop(cstate
, cstate
->cgstate
->e
, (int)q
.dir
);
7064 free(cstate
->cgstate
->e
);
7065 cstate
->cgstate
->e
= NULL
;
7066 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7070 free(cstate
->cgstate
->e
);
7071 cstate
->cgstate
->e
= NULL
;
7074 bpf_error(cstate
, "ethernet address used in non-ether expression");
7079 sappend(struct slist
*s0
, struct slist
*s1
)
7082 * This is definitely not the best way to do this, but the
7083 * lists will rarely get long.
7090 static struct slist
*
7091 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7095 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7100 static struct slist
*
7101 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7105 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7111 * Modify "index" to use the value stored into its register as an
7112 * offset relative to the beginning of the header for the protocol
7113 * "proto", and allocate a register and put an item "size" bytes long
7114 * (1, 2, or 4) at that offset into that register, making it the register
7118 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7120 struct slist
*s
, *tmp
;
7122 int regno
= alloc_reg(cstate
);
7124 free_reg(cstate
, inst
->regno
);
7128 bpf_error(cstate
, "data size must be 1, 2, or 4");
7144 bpf_error(cstate
, "unsupported index operation");
7148 * The offset is relative to the beginning of the packet
7149 * data, if we have a radio header. (If we don't, this
7152 if (cstate
->cgstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7153 cstate
->cgstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7154 cstate
->cgstate
->linktype
!= DLT_PRISM_HEADER
)
7155 bpf_error(cstate
, "radio information not present in capture");
7158 * Load into the X register the offset computed into the
7159 * register specified by "index".
7161 s
= xfer_to_x(cstate
, inst
);
7164 * Load the item at that offset.
7166 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7168 sappend(inst
->s
, s
);
7173 * The offset is relative to the beginning of
7174 * the link-layer header.
7176 * XXX - what about ATM LANE? Should the index be
7177 * relative to the beginning of the AAL5 frame, so
7178 * that 0 refers to the beginning of the LE Control
7179 * field, or relative to the beginning of the LAN
7180 * frame, so that 0 refers, for Ethernet LANE, to
7181 * the beginning of the destination address?
7183 s
= gen_abs_offset_varpart(cstate
, &cstate
->cgstate
->off_linkhdr
);
7186 * If "s" is non-null, it has code to arrange that the
7187 * X register contains the length of the prefix preceding
7188 * the link-layer header. Add to it the offset computed
7189 * into the register specified by "index", and move that
7190 * into the X register. Otherwise, just load into the X
7191 * register the offset computed into the register specified
7195 sappend(s
, xfer_to_a(cstate
, inst
));
7196 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7197 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7199 s
= xfer_to_x(cstate
, inst
);
7202 * Load the item at the sum of the offset we've put in the
7203 * X register and the offset of the start of the link
7204 * layer header (which is 0 if the radio header is
7205 * variable-length; that header length is what we put
7206 * into the X register and then added to the index).
7208 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7209 tmp
->s
.k
= cstate
->cgstate
->off_linkhdr
.constant_part
;
7211 sappend(inst
->s
, s
);
7225 * The offset is relative to the beginning of
7226 * the network-layer header.
7227 * XXX - are there any cases where we want
7228 * cstate->cgstate->off_nl_nosnap?
7230 s
= gen_abs_offset_varpart(cstate
, &cstate
->cgstate
->off_linkpl
);
7233 * If "s" is non-null, it has code to arrange that the
7234 * X register contains the variable part of the offset
7235 * of the link-layer payload. Add to it the offset
7236 * computed into the register specified by "index",
7237 * and move that into the X register. Otherwise, just
7238 * load into the X register the offset computed into
7239 * the register specified by "index".
7242 sappend(s
, xfer_to_a(cstate
, inst
));
7243 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7244 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7246 s
= xfer_to_x(cstate
, inst
);
7249 * Load the item at the sum of the offset we've put in the
7250 * X register, the offset of the start of the network
7251 * layer header from the beginning of the link-layer
7252 * payload, and the constant part of the offset of the
7253 * start of the link-layer payload.
7255 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7256 tmp
->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
;
7258 sappend(inst
->s
, s
);
7261 * Do the computation only if the packet contains
7262 * the protocol in question.
7264 b
= gen_proto_abbrev(cstate
, proto
);
7266 gen_and(inst
->b
, b
);
7280 * The offset is relative to the beginning of
7281 * the transport-layer header.
7283 * Load the X register with the length of the IPv4 header
7284 * (plus the offset of the link-layer header, if it's
7285 * a variable-length header), in bytes.
7287 * XXX - are there any cases where we want
7288 * cstate->cgstate->off_nl_nosnap?
7289 * XXX - we should, if we're built with
7290 * IPv6 support, generate code to load either
7291 * IPv4, IPv6, or both, as appropriate.
7293 s
= gen_loadx_iphdrlen(cstate
);
7296 * The X register now contains the sum of the variable
7297 * part of the offset of the link-layer payload and the
7298 * length of the network-layer header.
7300 * Load into the A register the offset relative to
7301 * the beginning of the transport layer header,
7302 * add the X register to that, move that to the
7303 * X register, and load with an offset from the
7304 * X register equal to the sum of the constant part of
7305 * the offset of the link-layer payload and the offset,
7306 * relative to the beginning of the link-layer payload,
7307 * of the network-layer header.
7309 sappend(s
, xfer_to_a(cstate
, inst
));
7310 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7311 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7312 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7313 tmp
->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
;
7314 sappend(inst
->s
, s
);
7317 * Do the computation only if the packet contains
7318 * the protocol in question - which is true only
7319 * if this is an IP datagram and is the first or
7320 * only fragment of that datagram.
7322 gen_and(gen_proto_abbrev(cstate
, proto
), b
= gen_ipfrag(cstate
));
7324 gen_and(inst
->b
, b
);
7325 gen_and(gen_proto_abbrev(cstate
, Q_IP
), b
);
7330 * Do the computation only if the packet contains
7331 * the protocol in question.
7333 b
= gen_proto_abbrev(cstate
, Q_IPV6
);
7335 gen_and(inst
->b
, b
);
7340 * Check if we have an icmp6 next header
7342 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7344 gen_and(inst
->b
, b
);
7349 s
= gen_abs_offset_varpart(cstate
, &cstate
->cgstate
->off_linkpl
);
7351 * If "s" is non-null, it has code to arrange that the
7352 * X register contains the variable part of the offset
7353 * of the link-layer payload. Add to it the offset
7354 * computed into the register specified by "index",
7355 * and move that into the X register. Otherwise, just
7356 * load into the X register the offset computed into
7357 * the register specified by "index".
7360 sappend(s
, xfer_to_a(cstate
, inst
));
7361 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7362 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7364 s
= xfer_to_x(cstate
, inst
);
7368 * Load the item at the sum of the offset we've put in the
7369 * X register, the offset of the start of the network
7370 * layer header from the beginning of the link-layer
7371 * payload, and the constant part of the offset of the
7372 * start of the link-layer payload.
7374 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7375 tmp
->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
+ 40;
7378 sappend(inst
->s
, s
);
7382 inst
->regno
= regno
;
7383 s
= new_stmt(cstate
, BPF_ST
);
7385 sappend(inst
->s
, s
);
7391 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7392 struct arth
*a1
, int reversed
)
7394 struct slist
*s0
, *s1
, *s2
;
7395 struct block
*b
, *tmp
;
7397 s0
= xfer_to_x(cstate
, a1
);
7398 s1
= xfer_to_a(cstate
, a0
);
7399 if (code
== BPF_JEQ
) {
7400 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7401 b
= new_block(cstate
, JMP(code
));
7405 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7411 sappend(a0
->s
, a1
->s
);
7415 free_reg(cstate
, a0
->regno
);
7416 free_reg(cstate
, a1
->regno
);
7418 /* 'and' together protocol checks */
7421 gen_and(a0
->b
, tmp
= a1
->b
);
7435 gen_loadlen(compiler_state_t
*cstate
)
7437 int regno
= alloc_reg(cstate
);
7438 struct arth
*a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7441 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7442 s
->next
= new_stmt(cstate
, BPF_ST
);
7443 s
->next
->s
.k
= regno
;
7451 gen_loadi(compiler_state_t
*cstate
, int val
)
7457 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7459 reg
= alloc_reg(cstate
);
7461 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7463 s
->next
= new_stmt(cstate
, BPF_ST
);
7472 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7476 s
= xfer_to_a(cstate
, a
);
7478 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7481 s
= new_stmt(cstate
, BPF_ST
);
7489 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7492 struct slist
*s0
, *s1
, *s2
;
7495 * Disallow division by, or modulus by, zero; we do this here
7496 * so that it gets done even if the optimizer is disabled.
7498 * Also disallow shifts by a value greater than 31; we do this
7499 * here, for the same reason.
7501 if (code
== BPF_DIV
) {
7502 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7503 bpf_error(cstate
, "division by zero");
7504 } else if (code
== BPF_MOD
) {
7505 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7506 bpf_error(cstate
, "modulus by zero");
7507 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7509 * XXX - we need to make up our minds as to what integers
7510 * are signed and what integers are unsigned in BPF programs
7513 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) &&
7514 (a1
->s
->s
.k
< 0 || a1
->s
->s
.k
> 31))
7515 bpf_error(cstate
, "shift by more than 31 bits");
7517 s0
= xfer_to_x(cstate
, a1
);
7518 s1
= xfer_to_a(cstate
, a0
);
7519 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7524 sappend(a0
->s
, a1
->s
);
7526 free_reg(cstate
, a0
->regno
);
7527 free_reg(cstate
, a1
->regno
);
7529 s0
= new_stmt(cstate
, BPF_ST
);
7530 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7537 * Initialize the table of used registers and the current register.
7540 init_regs(compiler_state_t
*cstate
)
7542 cstate
->cgstate
->curreg
= 0;
7543 memset(cstate
->cgstate
->regused
, 0, sizeof cstate
->cgstate
->regused
);
7547 * Return the next free register.
7550 alloc_reg(compiler_state_t
*cstate
)
7552 int n
= BPF_MEMWORDS
;
7555 if (cstate
->cgstate
->regused
[cstate
->cgstate
->curreg
])
7556 cstate
->cgstate
->curreg
= (cstate
->cgstate
->curreg
+ 1) % BPF_MEMWORDS
;
7558 cstate
->cgstate
->regused
[cstate
->cgstate
->curreg
] = 1;
7559 return cstate
->cgstate
->curreg
;
7562 bpf_error(cstate
, "too many registers needed to evaluate expression");
7567 * Return a register to the table so it can
7571 free_reg(compiler_state_t
*cstate
, int n
)
7573 cstate
->cgstate
->regused
[n
] = 0;
7576 static struct block
*
7577 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7582 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7583 b
= new_block(cstate
, JMP(jmp
));
7591 gen_greater(compiler_state_t
*cstate
, int n
)
7593 return gen_len(cstate
, BPF_JGE
, n
);
7597 * Actually, this is less than or equal.
7600 gen_less(compiler_state_t
*cstate
, int n
)
7604 b
= gen_len(cstate
, BPF_JGT
, n
);
7611 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7612 * the beginning of the link-layer header.
7613 * XXX - that means you can't test values in the radiotap header, but
7614 * as that header is difficult if not impossible to parse generally
7615 * without a loop, that might not be a severe problem. A new keyword
7616 * "radio" could be added for that, although what you'd really want
7617 * would be a way of testing particular radio header values, which
7618 * would generate code appropriate to the radio header in question.
7621 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7631 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7634 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7638 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7642 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7646 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7650 b
= new_block(cstate
, JMP(BPF_JEQ
));
7657 static const u_char abroadcast
[] = { 0x0 };
7660 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7662 bpf_u_int32 hostmask
;
7663 struct block
*b0
, *b1
, *b2
;
7664 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7670 switch (cstate
->cgstate
->linktype
) {
7672 case DLT_ARCNET_LINUX
:
7673 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7675 case DLT_NETANALYZER
:
7676 case DLT_NETANALYZER_TRANSPARENT
:
7677 b1
= gen_prevlinkhdr_check(cstate
);
7678 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7683 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7685 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7686 case DLT_IEEE802_11
:
7687 case DLT_PRISM_HEADER
:
7688 case DLT_IEEE802_11_RADIO_AVS
:
7689 case DLT_IEEE802_11_RADIO
:
7691 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7692 case DLT_IP_OVER_FC
:
7693 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7695 bpf_error(cstate
, "not a broadcast link");
7701 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7702 * as an indication that we don't know the netmask, and fail
7705 if (cstate
->cgstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7706 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7707 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7708 hostmask
= ~cstate
->cgstate
->netmask
;
7709 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7710 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7711 (bpf_int32
)(~0 & hostmask
), hostmask
);
7716 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7721 * Generate code to test the low-order bit of a MAC address (that's
7722 * the bottom bit of the *first* byte).
7724 static struct block
*
7725 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7727 register struct block
*b0
;
7728 register struct slist
*s
;
7730 /* link[offset] & 1 != 0 */
7731 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7732 b0
= new_block(cstate
, JMP(BPF_JSET
));
7739 gen_multicast(compiler_state_t
*cstate
, int proto
)
7741 register struct block
*b0
, *b1
, *b2
;
7742 register struct slist
*s
;
7748 switch (cstate
->cgstate
->linktype
) {
7750 case DLT_ARCNET_LINUX
:
7751 /* all ARCnet multicasts use the same address */
7752 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7754 case DLT_NETANALYZER
:
7755 case DLT_NETANALYZER_TRANSPARENT
:
7756 b1
= gen_prevlinkhdr_check(cstate
);
7757 /* ether[0] & 1 != 0 */
7758 b0
= gen_mac_multicast(cstate
, 0);
7764 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7766 * XXX - was that referring to bit-order issues?
7768 /* fddi[1] & 1 != 0 */
7769 return gen_mac_multicast(cstate
, 1);
7771 /* tr[2] & 1 != 0 */
7772 return gen_mac_multicast(cstate
, 2);
7773 case DLT_IEEE802_11
:
7774 case DLT_PRISM_HEADER
:
7775 case DLT_IEEE802_11_RADIO_AVS
:
7776 case DLT_IEEE802_11_RADIO
:
7781 * For control frames, there is no DA.
7783 * For management frames, DA is at an
7784 * offset of 4 from the beginning of
7787 * For data frames, DA is at an offset
7788 * of 4 from the beginning of the packet
7789 * if To DS is clear and at an offset of
7790 * 16 from the beginning of the packet
7795 * Generate the tests to be done for data frames.
7797 * First, check for To DS set, i.e. "link[1] & 0x01".
7799 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7800 b1
= new_block(cstate
, JMP(BPF_JSET
));
7801 b1
->s
.k
= 0x01; /* To DS */
7805 * If To DS is set, the DA is at 16.
7807 b0
= gen_mac_multicast(cstate
, 16);
7811 * Now, check for To DS not set, i.e. check
7812 * "!(link[1] & 0x01)".
7814 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
7815 b2
= new_block(cstate
, JMP(BPF_JSET
));
7816 b2
->s
.k
= 0x01; /* To DS */
7821 * If To DS is not set, the DA is at 4.
7823 b1
= gen_mac_multicast(cstate
, 4);
7827 * Now OR together the last two checks. That gives
7828 * the complete set of checks for data frames.
7833 * Now check for a data frame.
7834 * I.e, check "link[0] & 0x08".
7836 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7837 b1
= new_block(cstate
, JMP(BPF_JSET
));
7842 * AND that with the checks done for data frames.
7847 * If the high-order bit of the type value is 0, this
7848 * is a management frame.
7849 * I.e, check "!(link[0] & 0x08)".
7851 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7852 b2
= new_block(cstate
, JMP(BPF_JSET
));
7858 * For management frames, the DA is at 4.
7860 b1
= gen_mac_multicast(cstate
, 4);
7864 * OR that with the checks done for data frames.
7865 * That gives the checks done for management and
7871 * If the low-order bit of the type value is 1,
7872 * this is either a control frame or a frame
7873 * with a reserved type, and thus not a
7876 * I.e., check "!(link[0] & 0x04)".
7878 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
7879 b1
= new_block(cstate
, JMP(BPF_JSET
));
7885 * AND that with the checks for data and management
7890 case DLT_IP_OVER_FC
:
7891 b0
= gen_mac_multicast(cstate
, 2);
7896 /* Link not known to support multicasts */
7900 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7901 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7906 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
7907 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7911 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7916 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7917 * Outbound traffic is sent by this machine, while inbound traffic is
7918 * sent by a remote machine (and may include packets destined for a
7919 * unicast or multicast link-layer address we are not subscribing to).
7920 * These are the same definitions implemented by pcap_setdirection().
7921 * Capturing only unicast traffic destined for this host is probably
7922 * better accomplished using a higher-layer filter.
7925 gen_inbound(compiler_state_t
*cstate
, int dir
)
7927 register struct block
*b0
;
7930 * Only some data link types support inbound/outbound qualifiers.
7932 switch (cstate
->cgstate
->linktype
) {
7934 b0
= gen_relation(cstate
, BPF_JEQ
,
7935 gen_load(cstate
, Q_LINK
, gen_loadi(cstate
, 0), 1),
7936 gen_loadi(cstate
, 0),
7942 /* match outgoing packets */
7943 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7945 /* match incoming packets */
7946 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7951 /* match outgoing packets */
7952 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7954 /* to filter on inbound traffic, invert the match */
7959 case DLT_LINUX_SLL2
:
7960 /* match outgoing packets */
7961 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
7963 /* to filter on inbound traffic, invert the match */
7968 #ifdef HAVE_NET_PFVAR_H
7970 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7971 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7977 /* match outgoing packets */
7978 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7980 /* match incoming packets */
7981 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7985 case DLT_JUNIPER_MFR
:
7986 case DLT_JUNIPER_MLFR
:
7987 case DLT_JUNIPER_MLPPP
:
7988 case DLT_JUNIPER_ATM1
:
7989 case DLT_JUNIPER_ATM2
:
7990 case DLT_JUNIPER_PPPOE
:
7991 case DLT_JUNIPER_PPPOE_ATM
:
7992 case DLT_JUNIPER_GGSN
:
7993 case DLT_JUNIPER_ES
:
7994 case DLT_JUNIPER_MONITOR
:
7995 case DLT_JUNIPER_SERVICES
:
7996 case DLT_JUNIPER_ETHER
:
7997 case DLT_JUNIPER_PPP
:
7998 case DLT_JUNIPER_FRELAY
:
7999 case DLT_JUNIPER_CHDLC
:
8000 case DLT_JUNIPER_VP
:
8001 case DLT_JUNIPER_ST
:
8002 case DLT_JUNIPER_ISM
:
8003 case DLT_JUNIPER_VS
:
8004 case DLT_JUNIPER_SRX_E2E
:
8005 case DLT_JUNIPER_FIBRECHANNEL
:
8006 case DLT_JUNIPER_ATM_CEMIC
:
8008 /* juniper flags (including direction) are stored
8009 * the byte after the 3-byte magic number */
8011 /* match outgoing packets */
8012 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8014 /* match incoming packets */
8015 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8021 * If we have packet meta-data indicating a direction,
8022 * and that metadata can be checked by BPF code, check
8023 * it. Otherwise, give up, as this link-layer type has
8024 * nothing in the packet data.
8026 * Currently, the only platform where a BPF filter can
8027 * check that metadata is Linux with the in-kernel
8028 * BPF interpreter. If other packet capture mechanisms
8029 * and BPF filters also supported this, it would be
8030 * nice. It would be even better if they made that
8031 * metadata available so that we could provide it
8032 * with newer capture APIs, allowing it to be saved
8035 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
8037 * This is Linux with PF_PACKET support.
8038 * If this is a *live* capture, we can look at
8039 * special meta-data in the filter expression;
8040 * if it's a savefile, we can't.
8042 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8043 /* We have a FILE *, so this is a savefile */
8044 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
8045 cstate
->cgstate
->linktype
);
8049 /* match outgoing packets */
8050 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8053 /* to filter on inbound traffic, invert the match */
8056 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8057 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
8058 cstate
->cgstate
->linktype
);
8060 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8065 #ifdef HAVE_NET_PFVAR_H
8066 /* PF firewall log matched interface */
8068 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8073 if (cstate
->cgstate
->linktype
!= DLT_PFLOG
) {
8074 bpf_error(cstate
, "ifname supported only on PF linktype");
8077 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8078 off
= offsetof(struct pfloghdr
, ifname
);
8079 if (strlen(ifname
) >= len
) {
8080 bpf_error(cstate
, "ifname interface names can only be %d characters",
8084 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
8088 /* PF firewall log ruleset name */
8090 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8094 if (cstate
->cgstate
->linktype
!= DLT_PFLOG
) {
8095 bpf_error(cstate
, "ruleset supported only on PF linktype");
8099 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8100 bpf_error(cstate
, "ruleset names can only be %ld characters",
8101 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8105 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8106 strlen(ruleset
), (const u_char
*)ruleset
);
8110 /* PF firewall log rule number */
8112 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8116 if (cstate
->cgstate
->linktype
!= DLT_PFLOG
) {
8117 bpf_error(cstate
, "rnr supported only on PF linktype");
8121 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8126 /* PF firewall log sub-rule number */
8128 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8132 if (cstate
->cgstate
->linktype
!= DLT_PFLOG
) {
8133 bpf_error(cstate
, "srnr supported only on PF linktype");
8137 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8142 /* PF firewall log reason code */
8144 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8148 if (cstate
->cgstate
->linktype
!= DLT_PFLOG
) {
8149 bpf_error(cstate
, "reason supported only on PF linktype");
8153 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8158 /* PF firewall log action */
8160 gen_pf_action(compiler_state_t
*cstate
, int action
)
8164 if (cstate
->cgstate
->linktype
!= DLT_PFLOG
) {
8165 bpf_error(cstate
, "action supported only on PF linktype");
8169 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8173 #else /* !HAVE_NET_PFVAR_H */
8175 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8177 bpf_error(cstate
, "libpcap was compiled without pf support");
8182 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8184 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8189 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8191 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8196 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8198 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8203 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8205 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8210 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8212 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8215 #endif /* HAVE_NET_PFVAR_H */
8217 /* IEEE 802.11 wireless header */
8219 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
8223 switch (cstate
->cgstate
->linktype
) {
8225 case DLT_IEEE802_11
:
8226 case DLT_PRISM_HEADER
:
8227 case DLT_IEEE802_11_RADIO_AVS
:
8228 case DLT_IEEE802_11_RADIO
:
8229 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8234 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8242 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8246 switch (cstate
->cgstate
->linktype
) {
8248 case DLT_IEEE802_11
:
8249 case DLT_PRISM_HEADER
:
8250 case DLT_IEEE802_11_RADIO_AVS
:
8251 case DLT_IEEE802_11_RADIO
:
8255 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8259 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8260 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8266 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8270 switch (cstate
->cgstate
->linktype
) {
8273 case DLT_ARCNET_LINUX
:
8274 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8275 q
.proto
== Q_LINK
) {
8276 cstate
->cgstate
->e
= pcap_ether_aton(s
);
8277 if (cstate
->cgstate
->e
== NULL
)
8278 bpf_error(cstate
, "malloc");
8279 b
= gen_ahostop(cstate
, cstate
->cgstate
->e
, (int)q
.dir
);
8280 free(cstate
->cgstate
->e
);
8281 cstate
->cgstate
->e
= NULL
;
8284 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8290 bpf_error(cstate
, "aid supported only on ARCnet");
8295 static struct block
*
8296 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8298 register struct block
*b0
, *b1
;
8301 /* src comes first, different from Ethernet */
8303 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8306 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8309 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8310 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8316 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8317 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8322 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8326 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8330 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8334 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8338 bpf_error(cstate
, "'ra' is only supported on 802.11");
8342 bpf_error(cstate
, "'ta' is only supported on 802.11");
8349 static struct block
*
8350 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8352 struct block
*b0
, *b1
;
8354 /* check for VLAN, including QinQ */
8355 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8356 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8359 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8365 static struct block
*
8366 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8368 if (vlan_num
> 0x0fff) {
8369 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8372 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8375 static struct block
*
8376 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8379 struct block
*b0
, *b1
;
8381 b0
= gen_vlan_tpid_test(cstate
);
8384 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8390 * Both payload and link header type follow the VLAN tags so that
8391 * both need to be updated.
8393 cstate
->cgstate
->off_linkpl
.constant_part
+= 4;
8394 cstate
->cgstate
->off_linktype
.constant_part
+= 4;
8399 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8400 /* add v to variable part of off */
8402 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8406 if (!off
->is_variable
)
8407 off
->is_variable
= 1;
8409 off
->reg
= alloc_reg(cstate
);
8411 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8414 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8417 s2
= new_stmt(cstate
, BPF_ST
);
8423 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8424 * and link type offsets first
8427 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8431 /* offset determined at run time, shift variable part */
8433 cstate
->cgstate
->is_vlan_vloffset
= 1;
8434 gen_vlan_vloffset_add(cstate
, &cstate
->cgstate
->off_linkpl
, 4, &s
);
8435 gen_vlan_vloffset_add(cstate
, &cstate
->cgstate
->off_linktype
, 4, &s
);
8437 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8438 sappend(s
.next
, b_tpid
->head
->stmts
);
8439 b_tpid
->head
->stmts
= s
.next
;
8443 * patch block b_vid (VLAN id test) to load VID value either from packet
8444 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8447 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8449 struct slist
*s
, *s2
, *sjeq
;
8452 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8453 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8455 /* true -> next instructions, false -> beginning of b_vid */
8456 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8458 sjeq
->s
.jf
= b_vid
->stmts
;
8461 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8462 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8466 /* Jump to the test in b_vid. We need to jump one instruction before
8467 * the end of the b_vid block so that we only skip loading the TCI
8468 * from packet data and not the 'and' instruction extractging VID.
8471 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8473 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8477 /* insert our statements at the beginning of b_vid */
8478 sappend(s
, b_vid
->stmts
);
8483 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8484 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8485 * tag can be either in metadata or in packet data; therefore if the
8486 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8487 * header for VLAN tag. As the decision is done at run time, we need
8488 * update variable part of the offsets
8490 static struct block
*
8491 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8494 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8497 /* generate new filter code based on extracting packet
8499 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8500 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8502 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8507 * This is tricky. We need to insert the statements updating variable
8508 * parts of offsets before the the traditional TPID and VID tests so
8509 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8510 * we do not want this update to affect those checks. That's why we
8511 * generate both test blocks first and insert the statements updating
8512 * variable parts of both offsets after that. This wouldn't work if
8513 * there already were variable length link header when entering this
8514 * function but gen_vlan_bpf_extensions() isn't called in that case.
8516 b_tpid
= gen_vlan_tpid_test(cstate
);
8518 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8520 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8525 gen_vlan_patch_vid_test(cstate
, b_vid
);
8535 * support IEEE 802.1Q VLAN trunk over ethernet
8538 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8542 /* can't check for VLAN-encapsulated packets inside MPLS */
8543 if (cstate
->cgstate
->label_stack_depth
> 0)
8544 bpf_error(cstate
, "no VLAN match after MPLS");
8547 * Check for a VLAN packet, and then change the offsets to point
8548 * to the type and data fields within the VLAN packet. Just
8549 * increment the offsets, so that we can support a hierarchy, e.g.
8550 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8553 * XXX - this is a bit of a kludge. If we were to split the
8554 * compiler into a parser that parses an expression and
8555 * generates an expression tree, and a code generator that
8556 * takes an expression tree (which could come from our
8557 * parser or from some other parser) and generates BPF code,
8558 * we could perhaps make the offsets parameters of routines
8559 * and, in the handler for an "AND" node, pass to subnodes
8560 * other than the VLAN node the adjusted offsets.
8562 * This would mean that "vlan" would, instead of changing the
8563 * behavior of *all* tests after it, change only the behavior
8564 * of tests ANDed with it. That would change the documented
8565 * semantics of "vlan", which might break some expressions.
8566 * However, it would mean that "(vlan and ip) or ip" would check
8567 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8568 * checking only for VLAN-encapsulated IP, so that could still
8569 * be considered worth doing; it wouldn't break expressions
8570 * that are of the form "vlan and ..." or "vlan N and ...",
8571 * which I suspect are the most common expressions involving
8572 * "vlan". "vlan or ..." doesn't necessarily do what the user
8573 * would really want, now, as all the "or ..." tests would
8574 * be done assuming a VLAN, even though the "or" could be viewed
8575 * as meaning "or, if this isn't a VLAN packet...".
8577 switch (cstate
->cgstate
->linktype
) {
8580 case DLT_NETANALYZER
:
8581 case DLT_NETANALYZER_TRANSPARENT
:
8582 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8583 /* Verify that this is the outer part of the packet and
8584 * not encapsulated somehow. */
8585 if (cstate
->cgstate
->vlan_stack_depth
== 0 && !cstate
->cgstate
->off_linkhdr
.is_variable
&&
8586 cstate
->cgstate
->off_linkhdr
.constant_part
==
8587 cstate
->cgstate
->off_outermostlinkhdr
.constant_part
) {
8589 * Do we need special VLAN handling?
8591 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8592 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8595 b0
= gen_vlan_no_bpf_extensions(cstate
,
8596 vlan_num
, has_vlan_tag
);
8599 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8603 case DLT_IEEE802_11
:
8604 case DLT_PRISM_HEADER
:
8605 case DLT_IEEE802_11_RADIO_AVS
:
8606 case DLT_IEEE802_11_RADIO
:
8607 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8611 bpf_error(cstate
, "no VLAN support for data link type %d",
8612 cstate
->cgstate
->linktype
);
8616 cstate
->cgstate
->vlan_stack_depth
++;
8625 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
8627 struct block
*b0
, *b1
;
8629 if (cstate
->cgstate
->label_stack_depth
> 0) {
8630 /* just match the bottom-of-stack bit clear */
8631 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8634 * We're not in an MPLS stack yet, so check the link-layer
8635 * type against MPLS.
8637 switch (cstate
->cgstate
->linktype
) {
8639 case DLT_C_HDLC
: /* fall through */
8641 case DLT_NETANALYZER
:
8642 case DLT_NETANALYZER_TRANSPARENT
:
8643 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8647 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8650 /* FIXME add other DLT_s ...
8651 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8652 * leave it for now */
8655 bpf_error(cstate
, "no MPLS support for data link type %d",
8656 cstate
->cgstate
->linktype
);
8662 /* If a specific MPLS label is requested, check it */
8663 if (has_label_num
) {
8664 if (label_num
> 0xFFFFF) {
8665 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
8666 label_num
, 0xFFFFF);
8668 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8669 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8670 0xfffff000); /* only compare the first 20 bits */
8676 * Change the offsets to point to the type and data fields within
8677 * the MPLS packet. Just increment the offsets, so that we
8678 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8679 * capture packets with an outer label of 100000 and an inner
8682 * Increment the MPLS stack depth as well; this indicates that
8683 * we're checking MPLS-encapsulated headers, to make sure higher
8684 * level code generators don't try to match against IP-related
8685 * protocols such as Q_ARP, Q_RARP etc.
8687 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8689 cstate
->cgstate
->off_nl_nosnap
+= 4;
8690 cstate
->cgstate
->off_nl
+= 4;
8691 cstate
->cgstate
->label_stack_depth
++;
8696 * Support PPPOE discovery and session.
8699 gen_pppoed(compiler_state_t
*cstate
)
8701 /* check for PPPoE discovery */
8702 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
8706 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
8708 struct block
*b0
, *b1
;
8711 * Test against the PPPoE session link-layer type.
8713 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
8715 /* If a specific session is requested, check PPPoE session id */
8717 if (sess_num
> 0x0000ffff) {
8718 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
8719 sess_num
, 0x0000ffff);
8721 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
8722 (bpf_int32
)sess_num
, 0x0000ffff);
8728 * Change the offsets to point to the type and data fields within
8729 * the PPP packet, and note that this is PPPoE rather than
8732 * XXX - this is a bit of a kludge. If we were to split the
8733 * compiler into a parser that parses an expression and
8734 * generates an expression tree, and a code generator that
8735 * takes an expression tree (which could come from our
8736 * parser or from some other parser) and generates BPF code,
8737 * we could perhaps make the offsets parameters of routines
8738 * and, in the handler for an "AND" node, pass to subnodes
8739 * other than the PPPoE node the adjusted offsets.
8741 * This would mean that "pppoes" would, instead of changing the
8742 * behavior of *all* tests after it, change only the behavior
8743 * of tests ANDed with it. That would change the documented
8744 * semantics of "pppoes", which might break some expressions.
8745 * However, it would mean that "(pppoes and ip) or ip" would check
8746 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8747 * checking only for VLAN-encapsulated IP, so that could still
8748 * be considered worth doing; it wouldn't break expressions
8749 * that are of the form "pppoes and ..." which I suspect are the
8750 * most common expressions involving "pppoes". "pppoes or ..."
8751 * doesn't necessarily do what the user would really want, now,
8752 * as all the "or ..." tests would be done assuming PPPoE, even
8753 * though the "or" could be viewed as meaning "or, if this isn't
8754 * a PPPoE packet...".
8756 * The "network-layer" protocol is PPPoE, which has a 6-byte
8757 * PPPoE header, followed by a PPP packet.
8759 * There is no HDLC encapsulation for the PPP packet (it's
8760 * encapsulated in PPPoES instead), so the link-layer type
8761 * starts at the first byte of the PPP packet. For PPPoE,
8762 * that offset is relative to the beginning of the total
8763 * link-layer payload, including any 802.2 LLC header, so
8764 * it's 6 bytes past cstate->cgstate->off_nl.
8766 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->cgstate
->off_linkpl
.is_variable
,
8767 cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
8768 cstate
->cgstate
->off_linkpl
.reg
);
8770 cstate
->cgstate
->off_linktype
= cstate
->cgstate
->off_linkhdr
;
8771 cstate
->cgstate
->off_linkpl
.constant_part
= cstate
->cgstate
->off_linkhdr
.constant_part
+ 2;
8773 cstate
->cgstate
->off_nl
= 0;
8774 cstate
->cgstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
8779 /* Check that this is Geneve and the VNI is correct if
8780 * specified. Parameterized to handle both IPv4 and IPv6. */
8781 static struct block
*
8782 gen_geneve_check(compiler_state_t
*cstate
,
8783 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
8784 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
8786 struct block
*b0
, *b1
;
8788 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8790 /* Check that we are operating on version 0. Otherwise, we
8791 * can't decode the rest of the fields. The version is 2 bits
8792 * in the first byte of the Geneve header. */
8793 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8798 if (vni
> 0xffffff) {
8799 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
8802 vni
<<= 8; /* VNI is in the upper 3 bytes */
8803 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8812 /* The IPv4 and IPv6 Geneve checks need to do two things:
8813 * - Verify that this actually is Geneve with the right VNI.
8814 * - Place the IP header length (plus variable link prefix if
8815 * needed) into register A to be used later to compute
8816 * the inner packet offsets. */
8817 static struct block
*
8818 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
8820 struct block
*b0
, *b1
;
8821 struct slist
*s
, *s1
;
8823 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
8825 /* Load the IP header length into A. */
8826 s
= gen_loadx_iphdrlen(cstate
);
8828 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8831 /* Forcibly append these statements to the true condition
8832 * of the protocol check by creating a new block that is
8833 * always true and ANDing them. */
8834 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8843 static struct block
*
8844 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
8846 struct block
*b0
, *b1
;
8847 struct slist
*s
, *s1
;
8849 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
8851 /* Load the IP header length. We need to account for a
8852 * variable length link prefix if there is one. */
8853 s
= gen_abs_offset_varpart(cstate
, &cstate
->cgstate
->off_linkpl
);
8855 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8859 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8863 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
8867 /* Forcibly append these statements to the true condition
8868 * of the protocol check by creating a new block that is
8869 * always true and ANDing them. */
8870 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8873 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
8882 /* We need to store three values based on the Geneve header::
8883 * - The offset of the linktype.
8884 * - The offset of the end of the Geneve header.
8885 * - The offset of the end of the encapsulated MAC header. */
8886 static struct slist
*
8887 gen_geneve_offsets(compiler_state_t
*cstate
)
8889 struct slist
*s
, *s1
, *s_proto
;
8891 /* First we need to calculate the offset of the Geneve header
8892 * itself. This is composed of the IP header previously calculated
8893 * (include any variable link prefix) and stored in A plus the
8894 * fixed sized headers (fixed link prefix, MAC length, and UDP
8896 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8897 s
->s
.k
= cstate
->cgstate
->off_linkpl
.constant_part
+ cstate
->cgstate
->off_nl
+ 8;
8899 /* Stash this in X since we'll need it later. */
8900 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
8903 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8905 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8909 cstate
->cgstate
->off_linktype
.reg
= alloc_reg(cstate
);
8910 cstate
->cgstate
->off_linktype
.is_variable
= 1;
8911 cstate
->cgstate
->off_linktype
.constant_part
= 0;
8913 s1
= new_stmt(cstate
, BPF_ST
);
8914 s1
->s
.k
= cstate
->cgstate
->off_linktype
.reg
;
8917 /* Load the Geneve option length and mask and shift to get the
8918 * number of bytes. It is stored in the first byte of the Geneve
8920 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
8924 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
8928 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
8932 /* Add in the rest of the Geneve base header. */
8933 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8937 /* Add the Geneve header length to its offset and store. */
8938 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
8942 /* Set the encapsulated type as Ethernet. Even though we may
8943 * not actually have Ethernet inside there are two reasons this
8945 * - The linktype field is always in EtherType format regardless
8946 * of whether it is in Geneve or an inner Ethernet frame.
8947 * - The only link layer that we have specific support for is
8948 * Ethernet. We will confirm that the packet actually is
8949 * Ethernet at runtime before executing these checks. */
8950 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
8952 s1
= new_stmt(cstate
, BPF_ST
);
8953 s1
->s
.k
= cstate
->cgstate
->off_linkhdr
.reg
;
8956 /* Calculate whether we have an Ethernet header or just raw IP/
8957 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8958 * and linktype by 14 bytes so that the network header can be found
8959 * seamlessly. Otherwise, keep what we've calculated already. */
8961 /* We have a bare jmp so we can't use the optimizer. */
8962 cstate
->cgstate
->no_optimize
= 1;
8964 /* Load the EtherType in the Geneve header, 2 bytes in. */
8965 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
8969 /* Load X with the end of the Geneve header. */
8970 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
8971 s1
->s
.k
= cstate
->cgstate
->off_linkhdr
.reg
;
8974 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8975 * end of this check, we should have the total length in X. In
8976 * the non-Ethernet case, it's already there. */
8977 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
8978 s_proto
->s
.k
= ETHERTYPE_TEB
;
8979 sappend(s
, s_proto
);
8981 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
8985 /* Since this is Ethernet, use the EtherType of the payload
8986 * directly as the linktype. Overwrite what we already have. */
8987 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
8991 s1
= new_stmt(cstate
, BPF_ST
);
8992 s1
->s
.k
= cstate
->cgstate
->off_linktype
.reg
;
8995 /* Advance two bytes further to get the end of the Ethernet
8997 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9001 /* Move the result to X. */
9002 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9005 /* Store the final result of our linkpl calculation. */
9006 cstate
->cgstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9007 cstate
->cgstate
->off_linkpl
.is_variable
= 1;
9008 cstate
->cgstate
->off_linkpl
.constant_part
= 0;
9010 s1
= new_stmt(cstate
, BPF_STX
);
9011 s1
->s
.k
= cstate
->cgstate
->off_linkpl
.reg
;
9015 cstate
->cgstate
->off_nl
= 0;
9020 /* Check to see if this is a Geneve packet. */
9022 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9024 struct block
*b0
, *b1
;
9027 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9028 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9033 /* Later filters should act on the payload of the Geneve frame,
9034 * update all of the header pointers. Attach this code so that
9035 * it gets executed in the event that the Geneve filter matches. */
9036 s
= gen_geneve_offsets(cstate
);
9038 b1
= gen_true(cstate
);
9039 sappend(s
, b1
->stmts
);
9044 cstate
->cgstate
->is_geneve
= 1;
9049 /* Check that the encapsulated frame has a link layer header
9050 * for Ethernet filters. */
9051 static struct block
*
9052 gen_geneve_ll_check(compiler_state_t
*cstate
)
9055 struct slist
*s
, *s1
;
9057 /* The easiest way to see if there is a link layer present
9058 * is to check if the link layer header and payload are not
9061 /* Geneve always generates pure variable offsets so we can
9062 * compare only the registers. */
9063 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9064 s
->s
.k
= cstate
->cgstate
->off_linkhdr
.reg
;
9066 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9067 s1
->s
.k
= cstate
->cgstate
->off_linkpl
.reg
;
9070 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9079 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
, bpf_int32 jvalue
,
9080 bpf_u_int32 jtype
, int reverse
)
9087 if (!cstate
->cgstate
->is_atm
)
9088 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9089 if (cstate
->cgstate
->off_vpi
== OFFSET_NOT_SET
)
9091 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->cgstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
9096 if (!cstate
->cgstate
->is_atm
)
9097 bpf_error(cstate
, "'vci' supported only on raw ATM");
9098 if (cstate
->cgstate
->off_vci
== OFFSET_NOT_SET
)
9100 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->cgstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
9105 if (cstate
->cgstate
->off_proto
== OFFSET_NOT_SET
)
9106 abort(); /* XXX - this isn't on FreeBSD */
9107 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->cgstate
->off_proto
, BPF_B
, 0x0f, jtype
,
9112 if (cstate
->cgstate
->off_payload
== OFFSET_NOT_SET
)
9114 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->cgstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9115 0xffffffff, jtype
, reverse
, jvalue
);
9119 if (!cstate
->cgstate
->is_atm
)
9120 bpf_error(cstate
, "'callref' supported only on raw ATM");
9121 if (cstate
->cgstate
->off_proto
== OFFSET_NOT_SET
)
9123 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->cgstate
->off_proto
, BPF_B
, 0xffffffff,
9124 jtype
, reverse
, jvalue
);
9134 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9136 struct block
*b0
, *b1
;
9141 /* Get all packets in Meta signalling Circuit */
9142 if (!cstate
->cgstate
->is_atm
)
9143 bpf_error(cstate
, "'metac' supported only on raw ATM");
9144 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9145 b1
= gen_atmfield_code(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9150 /* Get all packets in Broadcast Circuit*/
9151 if (!cstate
->cgstate
->is_atm
)
9152 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9153 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9154 b1
= gen_atmfield_code(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9159 /* Get all cells in Segment OAM F4 circuit*/
9160 if (!cstate
->cgstate
->is_atm
)
9161 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9162 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9163 b1
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9168 /* Get all cells in End-to-End OAM F4 Circuit*/
9169 if (!cstate
->cgstate
->is_atm
)
9170 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9171 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9172 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9177 /* Get all packets in connection Signalling Circuit */
9178 if (!cstate
->cgstate
->is_atm
)
9179 bpf_error(cstate
, "'sc' supported only on raw ATM");
9180 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9181 b1
= gen_atmfield_code(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9186 /* Get all packets in ILMI Circuit */
9187 if (!cstate
->cgstate
->is_atm
)
9188 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9189 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9190 b1
= gen_atmfield_code(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9195 /* Get all LANE packets */
9196 if (!cstate
->cgstate
->is_atm
)
9197 bpf_error(cstate
, "'lane' supported only on raw ATM");
9198 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9201 * Arrange that all subsequent tests assume LANE
9202 * rather than LLC-encapsulated packets, and set
9203 * the offsets appropriately for LANE-encapsulated
9206 * We assume LANE means Ethernet, not Token Ring.
9208 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9209 cstate
->cgstate
->off_payload
+ 2, /* Ethernet header */
9211 cstate
->cgstate
->off_linktype
.constant_part
= cstate
->cgstate
->off_linkhdr
.constant_part
+ 12;
9212 cstate
->cgstate
->off_linkpl
.constant_part
= cstate
->cgstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9213 cstate
->cgstate
->off_nl
= 0; /* Ethernet II */
9214 cstate
->cgstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9218 /* Get all LLC-encapsulated packets */
9219 if (!cstate
->cgstate
->is_atm
)
9220 bpf_error(cstate
, "'llc' supported only on raw ATM");
9221 b1
= gen_atmfield_code(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9222 cstate
->cgstate
->linktype
= cstate
->cgstate
->prevlinktype
;
9232 * Filtering for MTP2 messages based on li value
9233 * FISU, length is null
9234 * LSSU, length is 1 or 2
9235 * MSU, length is 3 or more
9236 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9239 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9241 struct block
*b0
, *b1
;
9246 if ( (cstate
->cgstate
->linktype
!= DLT_MTP2
) &&
9247 (cstate
->cgstate
->linktype
!= DLT_ERF
) &&
9248 (cstate
->cgstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9249 bpf_error(cstate
, "'fisu' supported only on MTP2");
9250 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9251 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->cgstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
9255 if ( (cstate
->cgstate
->linktype
!= DLT_MTP2
) &&
9256 (cstate
->cgstate
->linktype
!= DLT_ERF
) &&
9257 (cstate
->cgstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9258 bpf_error(cstate
, "'lssu' supported only on MTP2");
9259 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->cgstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
9260 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->cgstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9265 if ( (cstate
->cgstate
->linktype
!= DLT_MTP2
) &&
9266 (cstate
->cgstate
->linktype
!= DLT_ERF
) &&
9267 (cstate
->cgstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9268 bpf_error(cstate
, "'msu' supported only on MTP2");
9269 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->cgstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9273 if ( (cstate
->cgstate
->linktype
!= DLT_MTP2
) &&
9274 (cstate
->cgstate
->linktype
!= DLT_ERF
) &&
9275 (cstate
->cgstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9276 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9277 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9278 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->cgstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9282 if ( (cstate
->cgstate
->linktype
!= DLT_MTP2
) &&
9283 (cstate
->cgstate
->linktype
!= DLT_ERF
) &&
9284 (cstate
->cgstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9285 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9286 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->cgstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9287 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->cgstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9292 if ( (cstate
->cgstate
->linktype
!= DLT_MTP2
) &&
9293 (cstate
->cgstate
->linktype
!= DLT_ERF
) &&
9294 (cstate
->cgstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9295 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9296 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->cgstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9306 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
9307 bpf_u_int32 jtype
, int reverse
)
9310 bpf_u_int32 val1
, val2
, val3
;
9311 u_int newoff_sio
= cstate
->cgstate
->off_sio
;
9312 u_int newoff_opc
= cstate
->cgstate
->off_opc
;
9313 u_int newoff_dpc
= cstate
->cgstate
->off_dpc
;
9314 u_int newoff_sls
= cstate
->cgstate
->off_sls
;
9316 switch (mtp3field
) {
9319 newoff_sio
+= 3; /* offset for MTP2_HSL */
9323 if (cstate
->cgstate
->off_sio
== OFFSET_NOT_SET
)
9324 bpf_error(cstate
, "'sio' supported only on SS7");
9325 /* sio coded on 1 byte so max value 255 */
9327 bpf_error(cstate
, "sio value %u too big; max value = 255",
9329 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9330 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9336 if (cstate
->cgstate
->off_opc
== OFFSET_NOT_SET
)
9337 bpf_error(cstate
, "'opc' supported only on SS7");
9338 /* opc coded on 14 bits so max value 16383 */
9340 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9342 /* the following instructions are made to convert jvalue
9343 * to the form used to write opc in an ss7 message*/
9344 val1
= jvalue
& 0x00003c00;
9346 val2
= jvalue
& 0x000003fc;
9348 val3
= jvalue
& 0x00000003;
9350 jvalue
= val1
+ val2
+ val3
;
9351 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9352 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9360 if (cstate
->cgstate
->off_dpc
== OFFSET_NOT_SET
)
9361 bpf_error(cstate
, "'dpc' supported only on SS7");
9362 /* dpc coded on 14 bits so max value 16383 */
9364 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9366 /* the following instructions are made to convert jvalue
9367 * to the forme used to write dpc in an ss7 message*/
9368 val1
= jvalue
& 0x000000ff;
9370 val2
= jvalue
& 0x00003f00;
9372 jvalue
= val1
+ val2
;
9373 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9374 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9380 if (cstate
->cgstate
->off_sls
== OFFSET_NOT_SET
)
9381 bpf_error(cstate
, "'sls' supported only on SS7");
9382 /* sls coded on 4 bits so max value 15 */
9384 bpf_error(cstate
, "sls value %u too big; max value = 15",
9386 /* the following instruction is made to convert jvalue
9387 * to the forme used to write sls in an ss7 message*/
9388 jvalue
= jvalue
<< 4;
9389 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9390 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9399 static struct block
*
9400 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9405 * Q.2931 signalling protocol messages for handling virtual circuits
9406 * establishment and teardown
9411 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9415 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9419 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9423 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9427 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9430 case A_RELEASE_DONE
:
9431 b1
= gen_atmfield_code(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9441 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9443 struct block
*b0
, *b1
;
9448 if (!cstate
->cgstate
->is_atm
)
9449 bpf_error(cstate
, "'oam' supported only on raw ATM");
9450 b1
= gen_atmmulti_abbrev(cstate
, A_OAMF4
);
9454 if (!cstate
->cgstate
->is_atm
)
9455 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9457 b0
= gen_atmfield_code(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9458 b1
= gen_atmfield_code(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9460 b0
= gen_atmfield_code(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9466 * Get Q.2931 signalling messages for switched
9467 * virtual connection
9469 if (!cstate
->cgstate
->is_atm
)
9470 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9471 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9472 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9474 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9476 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9478 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9480 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9482 b0
= gen_atmtype_abbrev(cstate
, A_SC
);
9487 if (!cstate
->cgstate
->is_atm
)
9488 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9489 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9490 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9492 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9494 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9496 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9498 b0
= gen_atmtype_abbrev(cstate
, A_METAC
);