1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <pcap-types.h>
31 #include <sys/socket.h>
34 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
55 #include "ethertype.h"
59 #include "ieee80211.h"
61 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
70 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
96 uint16_t u6_addr16
[8];
97 uint32_t u6_addr32
[4];
99 #define s6_addr in6_u.u6_addr8
100 #define s6_addr16 in6_u.u6_addr16
101 #define s6_addr32 in6_u.u6_addr32
102 #define s6_addr64 in6_u.u6_addr64
105 typedef unsigned short sa_family_t
;
107 #define __SOCKADDR_COMMON(sa_prefix) \
108 sa_family_t sa_prefix##family
110 /* Ditto, for IPv6. */
113 __SOCKADDR_COMMON (sin6_
);
114 uint16_t sin6_port
; /* Transport layer port # */
115 uint32_t sin6_flowinfo
; /* IPv6 flow information */
116 struct in6_addr sin6_addr
; /* IPv6 address */
119 #ifndef EAI_ADDRFAMILY
121 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
122 int ai_family
; /* PF_xxx */
123 int ai_socktype
; /* SOCK_xxx */
124 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
125 size_t ai_addrlen
; /* length of ai_addr */
126 char *ai_canonname
; /* canonical name for hostname */
127 struct sockaddr
*ai_addr
; /* binary address */
128 struct addrinfo
*ai_next
; /* next structure in linked list */
130 #endif /* EAI_ADDRFAMILY */
131 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
134 #include <netdb.h> /* for "struct addrinfo" */
136 #include <pcap/namedb.h>
138 #include "nametoaddr.h"
140 #define ETHERMTU 1500
142 #ifndef ETHERTYPE_TEB
143 #define ETHERTYPE_TEB 0x6558
146 #ifndef IPPROTO_HOPOPTS
147 #define IPPROTO_HOPOPTS 0
149 #ifndef IPPROTO_ROUTING
150 #define IPPROTO_ROUTING 43
152 #ifndef IPPROTO_FRAGMENT
153 #define IPPROTO_FRAGMENT 44
155 #ifndef IPPROTO_DSTOPTS
156 #define IPPROTO_DSTOPTS 60
159 #define IPPROTO_SCTP 132
162 #define GENEVE_PORT 6081
164 #ifdef HAVE_OS_PROTO_H
165 #include "os-proto.h"
168 #define JMP(c) ((c)|BPF_JMP|BPF_K)
171 * "Push" the current value of the link-layer header type and link-layer
172 * header offset onto a "stack", and set a new value. (It's not a
173 * full-blown stack; we keep only the top two items.)
175 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
177 (cs)->prevlinktype = (cs)->linktype; \
178 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
179 (cs)->linktype = (new_linktype); \
180 (cs)->off_linkhdr.is_variable = (new_is_variable); \
181 (cs)->off_linkhdr.constant_part = (new_constant_part); \
182 (cs)->off_linkhdr.reg = (new_reg); \
183 (cs)->is_geneve = 0; \
187 * Offset "not set" value.
189 #define OFFSET_NOT_SET 0xffffffffU
192 * Absolute offsets, which are offsets from the beginning of the raw
193 * packet data, are, in the general case, the sum of a variable value
194 * and a constant value; the variable value may be absent, in which
195 * case the offset is only the constant value, and the constant value
196 * may be zero, in which case the offset is only the variable value.
198 * bpf_abs_offset is a structure containing all that information:
200 * is_variable is 1 if there's a variable part.
202 * constant_part is the constant part of the value, possibly zero;
204 * if is_variable is 1, reg is the register number for a register
205 * containing the variable value if the register has been assigned,
215 * Value passed to gen_load_a() to indicate what the offset argument
216 * is relative to the beginning of.
219 OR_PACKET
, /* full packet data */
220 OR_LINKHDR
, /* link-layer header */
221 OR_PREVLINKHDR
, /* previous link-layer header */
222 OR_LLC
, /* 802.2 LLC header */
223 OR_PREVMPLSHDR
, /* previous MPLS header */
224 OR_LINKTYPE
, /* link-layer type */
225 OR_LINKPL
, /* link-layer payload */
226 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
227 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
228 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
232 * We divy out chunks of memory rather than call malloc each time so
233 * we don't have to worry about leaking memory. It's probably
234 * not a big deal if all this memory was wasted but if this ever
235 * goes into a library that would probably not be a good idea.
237 * XXX - this *is* in a library....
240 #define CHUNK0SIZE 1024
246 /* Code generator state */
248 struct _compiler_state
{
258 int outermostlinktype
;
263 /* Hack for handling VLAN and MPLS stacks. */
264 u_int label_stack_depth
;
265 u_int vlan_stack_depth
;
271 * As errors are handled by a longjmp, anything allocated must
272 * be freed in the longjmp handler, so it must be reachable
275 * One thing that's allocated is the result of pcap_nametoaddrinfo();
276 * it must be freed with freeaddrinfo(). This variable points to
277 * any addrinfo structure that would need to be freed.
282 * Another thing that's allocated is the result of pcap_ether_aton();
283 * it must be freed with free(). This variable points to any
284 * address that would need to be freed.
289 * Various code constructs need to know the layout of the packet.
290 * These values give the necessary offsets from the beginning
291 * of the packet data.
295 * Absolute offset of the beginning of the link-layer header.
297 bpf_abs_offset off_linkhdr
;
300 * If we're checking a link-layer header for a packet encapsulated
301 * in another protocol layer, this is the equivalent information
302 * for the previous layers' link-layer header from the beginning
303 * of the raw packet data.
305 bpf_abs_offset off_prevlinkhdr
;
308 * This is the equivalent information for the outermost layers'
311 bpf_abs_offset off_outermostlinkhdr
;
314 * Absolute offset of the beginning of the link-layer payload.
316 bpf_abs_offset off_linkpl
;
319 * "off_linktype" is the offset to information in the link-layer
320 * header giving the packet type. This is an absolute offset
321 * from the beginning of the packet.
323 * For Ethernet, it's the offset of the Ethernet type field; this
324 * means that it must have a value that skips VLAN tags.
326 * For link-layer types that always use 802.2 headers, it's the
327 * offset of the LLC header; this means that it must have a value
328 * that skips VLAN tags.
330 * For PPP, it's the offset of the PPP type field.
332 * For Cisco HDLC, it's the offset of the CHDLC type field.
334 * For BSD loopback, it's the offset of the AF_ value.
336 * For Linux cooked sockets, it's the offset of the type field.
338 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
339 * encapsulation, in which case, IP is assumed.
341 bpf_abs_offset off_linktype
;
344 * TRUE if the link layer includes an ATM pseudo-header.
349 * TRUE if "geneve" appeared in the filter; it causes us to
350 * generate code that checks for a Geneve header and assume
351 * that later filters apply to the encapsulated payload.
356 * TRUE if we need variable length part of VLAN offset
358 int is_vlan_vloffset
;
361 * These are offsets for the ATM pseudo-header.
368 * These are offsets for the MTP2 fields.
374 * These are offsets for the MTP3 fields.
382 * This is the offset of the first byte after the ATM pseudo_header,
383 * or -1 if there is no ATM pseudo-header.
388 * These are offsets to the beginning of the network-layer header.
389 * They are relative to the beginning of the link-layer payload
390 * (i.e., they don't include off_linkhdr.constant_part or
391 * off_linkpl.constant_part).
393 * If the link layer never uses 802.2 LLC:
395 * "off_nl" and "off_nl_nosnap" are the same.
397 * If the link layer always uses 802.2 LLC:
399 * "off_nl" is the offset if there's a SNAP header following
402 * "off_nl_nosnap" is the offset if there's no SNAP header.
404 * If the link layer is Ethernet:
406 * "off_nl" is the offset if the packet is an Ethernet II packet
407 * (we assume no 802.3+802.2+SNAP);
409 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
410 * with an 802.2 header following it.
416 * Here we handle simple allocation of the scratch registers.
417 * If too many registers are alloc'd, the allocator punts.
419 int regused
[BPF_MEMWORDS
];
425 struct chunk chunks
[NCHUNKS
];
430 * For use by routines outside this file.
434 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
439 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
445 * For use *ONLY* in routines in this file.
447 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
448 PCAP_PRINTFLIKE(2, 3);
451 static void PCAP_NORETURN
452 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
457 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
460 longjmp(cstate
->top_ctx
, 1);
464 static void init_linktype(compiler_state_t
*, pcap_t
*);
466 static void init_regs(compiler_state_t
*);
467 static int alloc_reg(compiler_state_t
*);
468 static void free_reg(compiler_state_t
*, int);
470 static void initchunks(compiler_state_t
*cstate
);
471 static void *newchunk(compiler_state_t
*cstate
, size_t);
472 static void freechunks(compiler_state_t
*cstate
);
473 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
474 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
475 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
476 static inline void syntax(compiler_state_t
*cstate
);
478 static void backpatch(struct block
*, struct block
*);
479 static void merge(struct block
*, struct block
*);
480 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
482 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
484 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
486 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
488 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
490 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
491 u_int
, bpf_int32
, bpf_u_int32
);
492 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
493 u_int
, const u_char
*);
494 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
495 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
496 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
498 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
500 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
501 static struct block
*gen_uncond(compiler_state_t
*, int);
502 static inline struct block
*gen_true(compiler_state_t
*);
503 static inline struct block
*gen_false(compiler_state_t
*);
504 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
505 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
506 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
507 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
508 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
509 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
510 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
511 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
512 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
514 static int ethertype_to_ppptype(int);
515 static struct block
*gen_linktype(compiler_state_t
*, int);
516 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
517 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
518 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
519 int, int, u_int
, u_int
);
521 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
522 struct in6_addr
*, int, int, u_int
, u_int
);
524 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
525 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
526 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
527 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
528 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
529 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
530 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
531 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
532 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
535 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
536 struct in6_addr
*, int, int, int);
539 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
540 struct addrinfo
*, int, int);
542 static struct block
*gen_ipfrag(compiler_state_t
*);
543 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
544 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
546 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
547 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
549 struct block
*gen_portop(compiler_state_t
*, int, int, int);
550 static struct block
*gen_port(compiler_state_t
*, int, int, int);
551 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
552 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
553 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
554 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
555 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
556 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
557 static int lookup_proto(compiler_state_t
*, const char *, int);
558 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
559 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
560 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
561 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
562 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
563 static struct block
*gen_len(compiler_state_t
*, int, int);
564 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
565 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
567 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
568 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
569 bpf_int32
, bpf_u_int32
, int);
570 static struct block
*gen_atmtype_llc(compiler_state_t
*);
571 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
574 initchunks(compiler_state_t
*cstate
)
578 for (i
= 0; i
< NCHUNKS
; i
++) {
579 cstate
->chunks
[i
].n_left
= 0;
580 cstate
->chunks
[i
].m
= NULL
;
582 cstate
->cur_chunk
= 0;
586 newchunk(compiler_state_t
*cstate
, size_t n
)
593 /* XXX Round up to nearest long. */
594 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
596 /* XXX Round up to structure boundary. */
600 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
601 if (n
> cp
->n_left
) {
603 k
= ++cstate
->cur_chunk
;
605 bpf_error(cstate
, "out of memory");
606 size
= CHUNK0SIZE
<< k
;
607 cp
->m
= (void *)malloc(size
);
609 bpf_error(cstate
, "out of memory");
610 memset((char *)cp
->m
, 0, size
);
613 bpf_error(cstate
, "out of memory");
616 return (void *)((char *)cp
->m
+ cp
->n_left
);
620 freechunks(compiler_state_t
*cstate
)
624 for (i
= 0; i
< NCHUNKS
; ++i
)
625 if (cstate
->chunks
[i
].m
!= NULL
)
626 free(cstate
->chunks
[i
].m
);
630 * A strdup whose allocations are freed after code generation is over.
633 sdup(compiler_state_t
*cstate
, const char *s
)
635 size_t n
= strlen(s
) + 1;
636 char *cp
= newchunk(cstate
, n
);
638 pcap_strlcpy(cp
, s
, n
);
642 static inline struct block
*
643 new_block(compiler_state_t
*cstate
, int code
)
647 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
654 static inline struct slist
*
655 new_stmt(compiler_state_t
*cstate
, int code
)
659 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
665 static struct block
*
666 gen_retblk(compiler_state_t
*cstate
, int v
)
668 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
674 static inline PCAP_NORETURN_DEF
void
675 syntax(compiler_state_t
*cstate
)
677 bpf_error(cstate
, "syntax error in filter expression");
681 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
682 const char *buf
, int optimize
, bpf_u_int32 mask
)
687 compiler_state_t cstate
;
688 const char * volatile xbuf
= buf
;
689 yyscan_t scanner
= NULL
;
690 volatile YY_BUFFER_STATE in_buffer
= NULL
;
695 * If this pcap_t hasn't been activated, it doesn't have a
696 * link-layer type, so we can't use it.
699 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
700 "not-yet-activated pcap_t passed to pcap_compile");
712 * If the device on which we're capturing need to be notified
713 * that a new filter is being compiled, do so.
715 * This allows them to save a copy of it, in case, for example,
716 * they're implementing a form of remote packet capture, and
717 * want the remote machine to filter out the packets in which
718 * it's sending the packets it's captured.
720 * XXX - the fact that we happen to be compiling a filter
721 * doesn't necessarily mean we'll be installing it as the
722 * filter for this pcap_t; we might be running it from userland
723 * on captured packets to do packet classification. We really
724 * need a better way of handling this, but this is all that
725 * the WinPcap code did.
727 if (p
->save_current_filter_op
!= NULL
)
728 (p
->save_current_filter_op
)(p
, buf
);
732 cstate
.no_optimize
= 0;
737 cstate
.ic
.root
= NULL
;
738 cstate
.ic
.cur_mark
= 0;
742 cstate
.netmask
= mask
;
744 cstate
.snaplen
= pcap_snapshot(p
);
745 if (cstate
.snaplen
== 0) {
746 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
747 "snaplen of 0 rejects all packets");
752 if (pcap_lex_init(&scanner
) != 0)
753 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
754 errno
, "can't initialize scanner");
755 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
758 * Associate the compiler state with the lexical analyzer
761 pcap_set_extra(&cstate
, scanner
);
763 init_linktype(&cstate
, p
);
764 if (pcap_parse(scanner
, &cstate
) != 0) {
766 if (cstate
.ai
!= NULL
)
767 freeaddrinfo(cstate
.ai
);
769 if (cstate
.e
!= NULL
)
775 if (cstate
.ic
.root
== NULL
)
776 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
778 if (optimize
&& !cstate
.no_optimize
) {
779 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
784 if (cstate
.ic
.root
== NULL
||
785 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
786 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
787 "expression rejects all packets");
792 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
793 cstate
.ic
.root
, &len
, p
->errbuf
);
794 if (program
->bf_insns
== NULL
) {
799 program
->bf_len
= len
;
801 rc
= 0; /* We're all okay */
805 * Clean up everything for the lexical analyzer.
807 if (in_buffer
!= NULL
)
808 pcap__delete_buffer(in_buffer
, scanner
);
810 pcap_lex_destroy(scanner
);
813 * Clean up our own allocated memory.
821 * entry point for using the compiler with no pcap open
822 * pass in all the stuff that is needed explicitly instead.
825 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
826 struct bpf_program
*program
,
827 const char *buf
, int optimize
, bpf_u_int32 mask
)
832 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
835 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
841 * Clean up a "struct bpf_program" by freeing all the memory allocated
845 pcap_freecode(struct bpf_program
*program
)
848 if (program
->bf_insns
!= NULL
) {
849 free((char *)program
->bf_insns
);
850 program
->bf_insns
= NULL
;
855 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
856 * which of the jt and jf fields has been resolved and which is a pointer
857 * back to another unresolved block (or nil). At least one of the fields
858 * in each block is already resolved.
861 backpatch(struct block
*list
, struct block
*target
)
878 * Merge the lists in b0 and b1, using the 'sense' field to indicate
879 * which of jt and jf is the link.
882 merge(struct block
*b0
, struct block
*b1
)
884 register struct block
**p
= &b0
;
886 /* Find end of list. */
888 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
890 /* Concatenate the lists. */
895 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
897 struct block
*ppi_dlt_check
;
900 * Insert before the statements of the first (root) block any
901 * statements needed to load the lengths of any variable-length
902 * headers into registers.
904 * XXX - a fancier strategy would be to insert those before the
905 * statements of all blocks that use those lengths and that
906 * have no predecessors that use them, so that we only compute
907 * the lengths if we need them. There might be even better
908 * approaches than that.
910 * However, those strategies would be more complicated, and
911 * as we don't generate code to compute a length if the
912 * program has no tests that use the length, and as most
913 * tests will probably use those lengths, we would just
914 * postpone computing the lengths so that it's not done
915 * for tests that fail early, and it's not clear that's
918 insert_compute_vloffsets(cstate
, p
->head
);
921 * For DLT_PPI captures, generate a check of the per-packet
922 * DLT value to make sure it's DLT_IEEE802_11.
924 * XXX - TurboCap cards use DLT_PPI for Ethernet.
925 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
926 * with appropriate Ethernet information and use that rather
927 * than using something such as DLT_PPI where you don't know
928 * the link-layer header type until runtime, which, in the
929 * general case, would force us to generate both Ethernet *and*
930 * 802.11 code (*and* anything else for which PPI is used)
931 * and choose between them early in the BPF program?
933 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
934 if (ppi_dlt_check
!= NULL
)
935 gen_and(ppi_dlt_check
, p
);
937 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
938 p
->sense
= !p
->sense
;
939 backpatch(p
, gen_retblk(cstate
, 0));
940 cstate
->ic
.root
= p
->head
;
944 gen_and(struct block
*b0
, struct block
*b1
)
946 backpatch(b0
, b1
->head
);
947 b0
->sense
= !b0
->sense
;
948 b1
->sense
= !b1
->sense
;
950 b1
->sense
= !b1
->sense
;
955 gen_or(struct block
*b0
, struct block
*b1
)
957 b0
->sense
= !b0
->sense
;
958 backpatch(b0
, b1
->head
);
959 b0
->sense
= !b0
->sense
;
965 gen_not(struct block
*b
)
967 b
->sense
= !b
->sense
;
970 static struct block
*
971 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
972 u_int size
, bpf_int32 v
)
974 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
977 static struct block
*
978 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
979 u_int size
, bpf_int32 v
)
981 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
984 static struct block
*
985 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
986 u_int size
, bpf_int32 v
)
988 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
991 static struct block
*
992 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
993 u_int size
, bpf_int32 v
)
995 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
998 static struct block
*
999 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1000 u_int size
, bpf_int32 v
)
1002 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1005 static struct block
*
1006 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1007 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
1009 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1012 static struct block
*
1013 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1014 u_int size
, const u_char
*v
)
1016 register struct block
*b
, *tmp
;
1019 * XXX - the actual *instructions* do unsigned comparisons on
1020 * most platforms, and the load instructions don't do sign
1021 * extension, so gen_cmp() should really take an unsigned
1024 * As the load instructons also don't do sign-extension, we
1025 * fetch the values from the byte array as unsigned. We don't
1026 * want to use the signed versions of the extract calls.
1030 register const u_char
*p
= &v
[size
- 4];
1032 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1033 (bpf_int32
)EXTRACT_BE_U_4(p
));
1040 register const u_char
*p
= &v
[size
- 2];
1042 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1043 (bpf_int32
)EXTRACT_BE_U_2(p
));
1050 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1059 * AND the field of size "size" at offset "offset" relative to the header
1060 * specified by "offrel" with "mask", and compare it with the value "v"
1061 * with the test specified by "jtype"; if "reverse" is true, the test
1062 * should test the opposite of "jtype".
1064 static struct block
*
1065 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1066 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1069 struct slist
*s
, *s2
;
1072 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1074 if (mask
!= 0xffffffff) {
1075 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1080 b
= new_block(cstate
, JMP(jtype
));
1083 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1089 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1091 cstate
->pcap_fddipad
= p
->fddipad
;
1094 * We start out with only one link-layer header.
1096 cstate
->outermostlinktype
= pcap_datalink(p
);
1097 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1098 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1099 cstate
->off_outermostlinkhdr
.reg
= -1;
1101 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1102 cstate
->off_prevlinkhdr
.constant_part
= 0;
1103 cstate
->off_prevlinkhdr
.is_variable
= 0;
1104 cstate
->off_prevlinkhdr
.reg
= -1;
1106 cstate
->linktype
= cstate
->outermostlinktype
;
1107 cstate
->off_linkhdr
.constant_part
= 0;
1108 cstate
->off_linkhdr
.is_variable
= 0;
1109 cstate
->off_linkhdr
.reg
= -1;
1114 cstate
->off_linkpl
.constant_part
= 0;
1115 cstate
->off_linkpl
.is_variable
= 0;
1116 cstate
->off_linkpl
.reg
= -1;
1118 cstate
->off_linktype
.constant_part
= 0;
1119 cstate
->off_linktype
.is_variable
= 0;
1120 cstate
->off_linktype
.reg
= -1;
1123 * Assume it's not raw ATM with a pseudo-header, for now.
1126 cstate
->off_vpi
= OFFSET_NOT_SET
;
1127 cstate
->off_vci
= OFFSET_NOT_SET
;
1128 cstate
->off_proto
= OFFSET_NOT_SET
;
1129 cstate
->off_payload
= OFFSET_NOT_SET
;
1134 cstate
->is_geneve
= 0;
1137 * No variable length VLAN offset by default
1139 cstate
->is_vlan_vloffset
= 0;
1142 * And assume we're not doing SS7.
1144 cstate
->off_li
= OFFSET_NOT_SET
;
1145 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1146 cstate
->off_sio
= OFFSET_NOT_SET
;
1147 cstate
->off_opc
= OFFSET_NOT_SET
;
1148 cstate
->off_dpc
= OFFSET_NOT_SET
;
1149 cstate
->off_sls
= OFFSET_NOT_SET
;
1151 cstate
->label_stack_depth
= 0;
1152 cstate
->vlan_stack_depth
= 0;
1154 switch (cstate
->linktype
) {
1157 cstate
->off_linktype
.constant_part
= 2;
1158 cstate
->off_linkpl
.constant_part
= 6;
1159 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1160 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1163 case DLT_ARCNET_LINUX
:
1164 cstate
->off_linktype
.constant_part
= 4;
1165 cstate
->off_linkpl
.constant_part
= 8;
1166 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1167 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1171 cstate
->off_linktype
.constant_part
= 12;
1172 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1173 cstate
->off_nl
= 0; /* Ethernet II */
1174 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1179 * SLIP doesn't have a link level type. The 16 byte
1180 * header is hacked into our SLIP driver.
1182 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1183 cstate
->off_linkpl
.constant_part
= 16;
1185 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1188 case DLT_SLIP_BSDOS
:
1189 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1190 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1192 cstate
->off_linkpl
.constant_part
= 24;
1194 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1199 cstate
->off_linktype
.constant_part
= 0;
1200 cstate
->off_linkpl
.constant_part
= 4;
1202 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1206 cstate
->off_linktype
.constant_part
= 0;
1207 cstate
->off_linkpl
.constant_part
= 12;
1209 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1214 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1215 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1216 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1217 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1219 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1224 * This does no include the Ethernet header, and
1225 * only covers session state.
1227 cstate
->off_linktype
.constant_part
= 6;
1228 cstate
->off_linkpl
.constant_part
= 8;
1230 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1234 cstate
->off_linktype
.constant_part
= 5;
1235 cstate
->off_linkpl
.constant_part
= 24;
1237 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1242 * FDDI doesn't really have a link-level type field.
1243 * We set "off_linktype" to the offset of the LLC header.
1245 * To check for Ethernet types, we assume that SSAP = SNAP
1246 * is being used and pick out the encapsulated Ethernet type.
1247 * XXX - should we generate code to check for SNAP?
1249 cstate
->off_linktype
.constant_part
= 13;
1250 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1251 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1252 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1253 cstate
->off_nl
= 8; /* 802.2+SNAP */
1254 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1259 * Token Ring doesn't really have a link-level type field.
1260 * We set "off_linktype" to the offset of the LLC header.
1262 * To check for Ethernet types, we assume that SSAP = SNAP
1263 * is being used and pick out the encapsulated Ethernet type.
1264 * XXX - should we generate code to check for SNAP?
1266 * XXX - the header is actually variable-length.
1267 * Some various Linux patched versions gave 38
1268 * as "off_linktype" and 40 as "off_nl"; however,
1269 * if a token ring packet has *no* routing
1270 * information, i.e. is not source-routed, the correct
1271 * values are 20 and 22, as they are in the vanilla code.
1273 * A packet is source-routed iff the uppermost bit
1274 * of the first byte of the source address, at an
1275 * offset of 8, has the uppermost bit set. If the
1276 * packet is source-routed, the total number of bytes
1277 * of routing information is 2 plus bits 0x1F00 of
1278 * the 16-bit value at an offset of 14 (shifted right
1279 * 8 - figure out which byte that is).
1281 cstate
->off_linktype
.constant_part
= 14;
1282 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1283 cstate
->off_nl
= 8; /* 802.2+SNAP */
1284 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1287 case DLT_PRISM_HEADER
:
1288 case DLT_IEEE802_11_RADIO_AVS
:
1289 case DLT_IEEE802_11_RADIO
:
1290 cstate
->off_linkhdr
.is_variable
= 1;
1291 /* Fall through, 802.11 doesn't have a variable link
1292 * prefix but is otherwise the same. */
1294 case DLT_IEEE802_11
:
1296 * 802.11 doesn't really have a link-level type field.
1297 * We set "off_linktype.constant_part" to the offset of
1300 * To check for Ethernet types, we assume that SSAP = SNAP
1301 * is being used and pick out the encapsulated Ethernet type.
1302 * XXX - should we generate code to check for SNAP?
1304 * We also handle variable-length radio headers here.
1305 * The Prism header is in theory variable-length, but in
1306 * practice it's always 144 bytes long. However, some
1307 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1308 * sometimes or always supply an AVS header, so we
1309 * have to check whether the radio header is a Prism
1310 * header or an AVS header, so, in practice, it's
1313 cstate
->off_linktype
.constant_part
= 24;
1314 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1315 cstate
->off_linkpl
.is_variable
= 1;
1316 cstate
->off_nl
= 8; /* 802.2+SNAP */
1317 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1322 * At the moment we treat PPI the same way that we treat
1323 * normal Radiotap encoded packets. The difference is in
1324 * the function that generates the code at the beginning
1325 * to compute the header length. Since this code generator
1326 * of PPI supports bare 802.11 encapsulation only (i.e.
1327 * the encapsulated DLT should be DLT_IEEE802_11) we
1328 * generate code to check for this too.
1330 cstate
->off_linktype
.constant_part
= 24;
1331 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1332 cstate
->off_linkpl
.is_variable
= 1;
1333 cstate
->off_linkhdr
.is_variable
= 1;
1334 cstate
->off_nl
= 8; /* 802.2+SNAP */
1335 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1338 case DLT_ATM_RFC1483
:
1339 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1341 * assume routed, non-ISO PDUs
1342 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1344 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1345 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1346 * latter would presumably be treated the way PPPoE
1347 * should be, so you can do "pppoe and udp port 2049"
1348 * or "pppoa and tcp port 80" and have it check for
1349 * PPPo{A,E} and a PPP protocol of IP and....
1351 cstate
->off_linktype
.constant_part
= 0;
1352 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1353 cstate
->off_nl
= 8; /* 802.2+SNAP */
1354 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1359 * Full Frontal ATM; you get AALn PDUs with an ATM
1363 cstate
->off_vpi
= SUNATM_VPI_POS
;
1364 cstate
->off_vci
= SUNATM_VCI_POS
;
1365 cstate
->off_proto
= PROTO_POS
;
1366 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1367 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1368 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1369 cstate
->off_nl
= 8; /* 802.2+SNAP */
1370 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1376 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1377 cstate
->off_linkpl
.constant_part
= 0;
1379 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1382 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1383 cstate
->off_linktype
.constant_part
= 14;
1384 cstate
->off_linkpl
.constant_part
= 16;
1386 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1389 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1390 cstate
->off_linktype
.constant_part
= 0;
1391 cstate
->off_linkpl
.constant_part
= 20;
1393 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1398 * LocalTalk does have a 1-byte type field in the LLAP header,
1399 * but really it just indicates whether there is a "short" or
1400 * "long" DDP packet following.
1402 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1403 cstate
->off_linkpl
.constant_part
= 0;
1405 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1408 case DLT_IP_OVER_FC
:
1410 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1411 * link-level type field. We set "off_linktype" to the
1412 * offset of the LLC header.
1414 * To check for Ethernet types, we assume that SSAP = SNAP
1415 * is being used and pick out the encapsulated Ethernet type.
1416 * XXX - should we generate code to check for SNAP? RFC
1417 * 2625 says SNAP should be used.
1419 cstate
->off_linktype
.constant_part
= 16;
1420 cstate
->off_linkpl
.constant_part
= 16;
1421 cstate
->off_nl
= 8; /* 802.2+SNAP */
1422 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1427 * XXX - we should set this to handle SNAP-encapsulated
1428 * frames (NLPID of 0x80).
1430 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1431 cstate
->off_linkpl
.constant_part
= 0;
1433 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1437 * the only BPF-interesting FRF.16 frames are non-control frames;
1438 * Frame Relay has a variable length link-layer
1439 * so lets start with offset 4 for now and increments later on (FIXME);
1442 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1443 cstate
->off_linkpl
.constant_part
= 0;
1445 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1448 case DLT_APPLE_IP_OVER_IEEE1394
:
1449 cstate
->off_linktype
.constant_part
= 16;
1450 cstate
->off_linkpl
.constant_part
= 18;
1452 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1455 case DLT_SYMANTEC_FIREWALL
:
1456 cstate
->off_linktype
.constant_part
= 6;
1457 cstate
->off_linkpl
.constant_part
= 44;
1458 cstate
->off_nl
= 0; /* Ethernet II */
1459 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1462 #ifdef HAVE_NET_PFVAR_H
1464 cstate
->off_linktype
.constant_part
= 0;
1465 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1467 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1471 case DLT_JUNIPER_MFR
:
1472 case DLT_JUNIPER_MLFR
:
1473 case DLT_JUNIPER_MLPPP
:
1474 case DLT_JUNIPER_PPP
:
1475 case DLT_JUNIPER_CHDLC
:
1476 case DLT_JUNIPER_FRELAY
:
1477 cstate
->off_linktype
.constant_part
= 4;
1478 cstate
->off_linkpl
.constant_part
= 4;
1480 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1483 case DLT_JUNIPER_ATM1
:
1484 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1485 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1487 cstate
->off_nl_nosnap
= 10;
1490 case DLT_JUNIPER_ATM2
:
1491 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1492 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1494 cstate
->off_nl_nosnap
= 10;
1497 /* frames captured on a Juniper PPPoE service PIC
1498 * contain raw ethernet frames */
1499 case DLT_JUNIPER_PPPOE
:
1500 case DLT_JUNIPER_ETHER
:
1501 cstate
->off_linkpl
.constant_part
= 14;
1502 cstate
->off_linktype
.constant_part
= 16;
1503 cstate
->off_nl
= 18; /* Ethernet II */
1504 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1507 case DLT_JUNIPER_PPPOE_ATM
:
1508 cstate
->off_linktype
.constant_part
= 4;
1509 cstate
->off_linkpl
.constant_part
= 6;
1511 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1514 case DLT_JUNIPER_GGSN
:
1515 cstate
->off_linktype
.constant_part
= 6;
1516 cstate
->off_linkpl
.constant_part
= 12;
1518 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1521 case DLT_JUNIPER_ES
:
1522 cstate
->off_linktype
.constant_part
= 6;
1523 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1524 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1525 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1528 case DLT_JUNIPER_MONITOR
:
1529 cstate
->off_linktype
.constant_part
= 12;
1530 cstate
->off_linkpl
.constant_part
= 12;
1531 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1532 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1535 case DLT_BACNET_MS_TP
:
1536 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1537 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1538 cstate
->off_nl
= OFFSET_NOT_SET
;
1539 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1542 case DLT_JUNIPER_SERVICES
:
1543 cstate
->off_linktype
.constant_part
= 12;
1544 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1545 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1546 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1549 case DLT_JUNIPER_VP
:
1550 cstate
->off_linktype
.constant_part
= 18;
1551 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1552 cstate
->off_nl
= OFFSET_NOT_SET
;
1553 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1556 case DLT_JUNIPER_ST
:
1557 cstate
->off_linktype
.constant_part
= 18;
1558 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1559 cstate
->off_nl
= OFFSET_NOT_SET
;
1560 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1563 case DLT_JUNIPER_ISM
:
1564 cstate
->off_linktype
.constant_part
= 8;
1565 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1566 cstate
->off_nl
= OFFSET_NOT_SET
;
1567 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1570 case DLT_JUNIPER_VS
:
1571 case DLT_JUNIPER_SRX_E2E
:
1572 case DLT_JUNIPER_FIBRECHANNEL
:
1573 case DLT_JUNIPER_ATM_CEMIC
:
1574 cstate
->off_linktype
.constant_part
= 8;
1575 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1576 cstate
->off_nl
= OFFSET_NOT_SET
;
1577 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1582 cstate
->off_li_hsl
= 4;
1583 cstate
->off_sio
= 3;
1584 cstate
->off_opc
= 4;
1585 cstate
->off_dpc
= 4;
1586 cstate
->off_sls
= 7;
1587 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1588 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1589 cstate
->off_nl
= OFFSET_NOT_SET
;
1590 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1593 case DLT_MTP2_WITH_PHDR
:
1595 cstate
->off_li_hsl
= 8;
1596 cstate
->off_sio
= 7;
1597 cstate
->off_opc
= 8;
1598 cstate
->off_dpc
= 8;
1599 cstate
->off_sls
= 11;
1600 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1601 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1602 cstate
->off_nl
= OFFSET_NOT_SET
;
1603 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1607 cstate
->off_li
= 22;
1608 cstate
->off_li_hsl
= 24;
1609 cstate
->off_sio
= 23;
1610 cstate
->off_opc
= 24;
1611 cstate
->off_dpc
= 24;
1612 cstate
->off_sls
= 27;
1613 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1614 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1615 cstate
->off_nl
= OFFSET_NOT_SET
;
1616 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1620 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1621 cstate
->off_linkpl
.constant_part
= 4;
1623 cstate
->off_nl_nosnap
= 0;
1628 * Currently, only raw "link[N:M]" filtering is supported.
1630 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1631 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1632 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1633 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1637 cstate
->off_linktype
.constant_part
= 1;
1638 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1640 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1643 case DLT_NETANALYZER
:
1644 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1645 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1646 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1647 cstate
->off_nl
= 0; /* Ethernet II */
1648 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1651 case DLT_NETANALYZER_TRANSPARENT
:
1652 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1653 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1654 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1655 cstate
->off_nl
= 0; /* Ethernet II */
1656 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1661 * For values in the range in which we've assigned new
1662 * DLT_ values, only raw "link[N:M]" filtering is supported.
1664 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1665 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1666 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1667 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1668 cstate
->off_nl
= OFFSET_NOT_SET
;
1669 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1671 bpf_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1676 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1680 * Load a value relative to the specified absolute offset.
1682 static struct slist
*
1683 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1684 u_int offset
, u_int size
)
1686 struct slist
*s
, *s2
;
1688 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1691 * If "s" is non-null, it has code to arrange that the X register
1692 * contains the variable part of the absolute offset, so we
1693 * generate a load relative to that, with an offset of
1694 * abs_offset->constant_part + offset.
1696 * Otherwise, we can do an absolute load with an offset of
1697 * abs_offset->constant_part + offset.
1701 * "s" points to a list of statements that puts the
1702 * variable part of the absolute offset into the X register.
1703 * Do an indirect load, to use the X register as an offset.
1705 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1706 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1710 * There is no variable part of the absolute offset, so
1711 * just do an absolute load.
1713 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1714 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1720 * Load a value relative to the beginning of the specified header.
1722 static struct slist
*
1723 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1726 struct slist
*s
, *s2
;
1731 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1736 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1739 case OR_PREVLINKHDR
:
1740 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1744 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1747 case OR_PREVMPLSHDR
:
1748 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1752 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1755 case OR_LINKPL_NOSNAP
:
1756 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1760 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1765 * Load the X register with the length of the IPv4 header
1766 * (plus the offset of the link-layer header, if it's
1767 * preceded by a variable-length header such as a radio
1768 * header), in bytes.
1770 s
= gen_loadx_iphdrlen(cstate
);
1773 * Load the item at {offset of the link-layer payload} +
1774 * {offset, relative to the start of the link-layer
1775 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1776 * {specified offset}.
1778 * If the offset of the link-layer payload is variable,
1779 * the variable part of that offset is included in the
1780 * value in the X register, and we include the constant
1781 * part in the offset of the load.
1783 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1784 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1789 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1800 * Generate code to load into the X register the sum of the length of
1801 * the IPv4 header and the variable part of the offset of the link-layer
1804 static struct slist
*
1805 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1807 struct slist
*s
, *s2
;
1809 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1812 * The offset of the link-layer payload has a variable
1813 * part. "s" points to a list of statements that put
1814 * the variable part of that offset into the X register.
1816 * The 4*([k]&0xf) addressing mode can't be used, as we
1817 * don't have a constant offset, so we have to load the
1818 * value in question into the A register and add to it
1819 * the value from the X register.
1821 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1822 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1824 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1827 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1832 * The A register now contains the length of the IP header.
1833 * We need to add to it the variable part of the offset of
1834 * the link-layer payload, which is still in the X
1835 * register, and move the result into the X register.
1837 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1838 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1841 * The offset of the link-layer payload is a constant,
1842 * so no code was generated to load the (non-existent)
1843 * variable part of that offset.
1845 * This means we can use the 4*([k]&0xf) addressing
1846 * mode. Load the length of the IPv4 header, which
1847 * is at an offset of cstate->off_nl from the beginning of
1848 * the link-layer payload, and thus at an offset of
1849 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1850 * of the raw packet data, using that addressing mode.
1852 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1853 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1859 static struct block
*
1860 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1865 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1867 b
= new_block(cstate
, JMP(BPF_JEQ
));
1873 static inline struct block
*
1874 gen_true(compiler_state_t
*cstate
)
1876 return gen_uncond(cstate
, 1);
1879 static inline struct block
*
1880 gen_false(compiler_state_t
*cstate
)
1882 return gen_uncond(cstate
, 0);
1886 * Byte-swap a 32-bit number.
1887 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1888 * big-endian platforms.)
1890 #define SWAPLONG(y) \
1891 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1894 * Generate code to match a particular packet type.
1896 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1897 * value, if <= ETHERMTU. We use that to determine whether to
1898 * match the type/length field or to check the type/length field for
1899 * a value <= ETHERMTU to see whether it's a type field and then do
1900 * the appropriate test.
1902 static struct block
*
1903 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1905 struct block
*b0
, *b1
;
1911 case LLCSAP_NETBEUI
:
1913 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1914 * so we check the DSAP and SSAP.
1916 * LLCSAP_IP checks for IP-over-802.2, rather
1917 * than IP-over-Ethernet or IP-over-SNAP.
1919 * XXX - should we check both the DSAP and the
1920 * SSAP, like this, or should we check just the
1921 * DSAP, as we do for other types <= ETHERMTU
1922 * (i.e., other SAP values)?
1924 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1926 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1927 ((proto
<< 8) | proto
));
1935 * Ethernet_II frames, which are Ethernet
1936 * frames with a frame type of ETHERTYPE_IPX;
1938 * Ethernet_802.3 frames, which are 802.3
1939 * frames (i.e., the type/length field is
1940 * a length field, <= ETHERMTU, rather than
1941 * a type field) with the first two bytes
1942 * after the Ethernet/802.3 header being
1945 * Ethernet_802.2 frames, which are 802.3
1946 * frames with an 802.2 LLC header and
1947 * with the IPX LSAP as the DSAP in the LLC
1950 * Ethernet_SNAP frames, which are 802.3
1951 * frames with an LLC header and a SNAP
1952 * header and with an OUI of 0x000000
1953 * (encapsulated Ethernet) and a protocol
1954 * ID of ETHERTYPE_IPX in the SNAP header.
1956 * XXX - should we generate the same code both
1957 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1961 * This generates code to check both for the
1962 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1964 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1965 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1969 * Now we add code to check for SNAP frames with
1970 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1972 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
1976 * Now we generate code to check for 802.3
1977 * frames in general.
1979 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1983 * Now add the check for 802.3 frames before the
1984 * check for Ethernet_802.2 and Ethernet_802.3,
1985 * as those checks should only be done on 802.3
1986 * frames, not on Ethernet frames.
1991 * Now add the check for Ethernet_II frames, and
1992 * do that before checking for the other frame
1995 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1999 case ETHERTYPE_ATALK
:
2000 case ETHERTYPE_AARP
:
2002 * EtherTalk (AppleTalk protocols on Ethernet link
2003 * layer) may use 802.2 encapsulation.
2007 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2008 * we check for an Ethernet type field less than
2009 * 1500, which means it's an 802.3 length field.
2011 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2015 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2016 * SNAP packets with an organization code of
2017 * 0x080007 (Apple, for Appletalk) and a protocol
2018 * type of ETHERTYPE_ATALK (Appletalk).
2020 * 802.2-encapsulated ETHERTYPE_AARP packets are
2021 * SNAP packets with an organization code of
2022 * 0x000000 (encapsulated Ethernet) and a protocol
2023 * type of ETHERTYPE_AARP (Appletalk ARP).
2025 if (proto
== ETHERTYPE_ATALK
)
2026 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2027 else /* proto == ETHERTYPE_AARP */
2028 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2032 * Check for Ethernet encapsulation (Ethertalk
2033 * phase 1?); we just check for the Ethernet
2036 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2042 if (proto
<= ETHERMTU
) {
2044 * This is an LLC SAP value, so the frames
2045 * that match would be 802.2 frames.
2046 * Check that the frame is an 802.2 frame
2047 * (i.e., that the length/type field is
2048 * a length field, <= ETHERMTU) and
2049 * then check the DSAP.
2051 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2053 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2058 * This is an Ethernet type, so compare
2059 * the length/type field with it (if
2060 * the frame is an 802.2 frame, the length
2061 * field will be <= ETHERMTU, and, as
2062 * "proto" is > ETHERMTU, this test
2063 * will fail and the frame won't match,
2064 * which is what we want).
2066 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2072 static struct block
*
2073 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2076 * For DLT_NULL, the link-layer header is a 32-bit word
2077 * containing an AF_ value in *host* byte order, and for
2078 * DLT_ENC, the link-layer header begins with a 32-bit
2079 * word containing an AF_ value in host byte order.
2081 * In addition, if we're reading a saved capture file,
2082 * the host byte order in the capture may not be the
2083 * same as the host byte order on this machine.
2085 * For DLT_LOOP, the link-layer header is a 32-bit
2086 * word containing an AF_ value in *network* byte order.
2088 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2090 * The AF_ value is in host byte order, but the BPF
2091 * interpreter will convert it to network byte order.
2093 * If this is a save file, and it's from a machine
2094 * with the opposite byte order to ours, we byte-swap
2097 * Then we run it through "htonl()", and generate
2098 * code to compare against the result.
2100 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2101 proto
= SWAPLONG(proto
);
2102 proto
= htonl(proto
);
2104 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2108 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2109 * or IPv6 then we have an error.
2111 static struct block
*
2112 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2117 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2120 case ETHERTYPE_IPV6
:
2121 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2122 (bpf_int32
)IPH_AF_INET6
);
2129 return gen_false(cstate
);
2133 * Generate code to match a particular packet type.
2135 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2136 * value, if <= ETHERMTU. We use that to determine whether to
2137 * match the type field or to check the type field for the special
2138 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2140 static struct block
*
2141 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2143 struct block
*b0
, *b1
;
2149 case LLCSAP_NETBEUI
:
2151 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2152 * so we check the DSAP and SSAP.
2154 * LLCSAP_IP checks for IP-over-802.2, rather
2155 * than IP-over-Ethernet or IP-over-SNAP.
2157 * XXX - should we check both the DSAP and the
2158 * SSAP, like this, or should we check just the
2159 * DSAP, as we do for other types <= ETHERMTU
2160 * (i.e., other SAP values)?
2162 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2163 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2164 ((proto
<< 8) | proto
));
2170 * Ethernet_II frames, which are Ethernet
2171 * frames with a frame type of ETHERTYPE_IPX;
2173 * Ethernet_802.3 frames, which have a frame
2174 * type of LINUX_SLL_P_802_3;
2176 * Ethernet_802.2 frames, which are 802.3
2177 * frames with an 802.2 LLC header (i.e, have
2178 * a frame type of LINUX_SLL_P_802_2) and
2179 * with the IPX LSAP as the DSAP in the LLC
2182 * Ethernet_SNAP frames, which are 802.3
2183 * frames with an LLC header and a SNAP
2184 * header and with an OUI of 0x000000
2185 * (encapsulated Ethernet) and a protocol
2186 * ID of ETHERTYPE_IPX in the SNAP header.
2188 * First, do the checks on LINUX_SLL_P_802_2
2189 * frames; generate the check for either
2190 * Ethernet_802.2 or Ethernet_SNAP frames, and
2191 * then put a check for LINUX_SLL_P_802_2 frames
2194 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2195 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2197 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2201 * Now check for 802.3 frames and OR that with
2202 * the previous test.
2204 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2208 * Now add the check for Ethernet_II frames, and
2209 * do that before checking for the other frame
2212 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2216 case ETHERTYPE_ATALK
:
2217 case ETHERTYPE_AARP
:
2219 * EtherTalk (AppleTalk protocols on Ethernet link
2220 * layer) may use 802.2 encapsulation.
2224 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2225 * we check for the 802.2 protocol type in the
2226 * "Ethernet type" field.
2228 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2231 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2232 * SNAP packets with an organization code of
2233 * 0x080007 (Apple, for Appletalk) and a protocol
2234 * type of ETHERTYPE_ATALK (Appletalk).
2236 * 802.2-encapsulated ETHERTYPE_AARP packets are
2237 * SNAP packets with an organization code of
2238 * 0x000000 (encapsulated Ethernet) and a protocol
2239 * type of ETHERTYPE_AARP (Appletalk ARP).
2241 if (proto
== ETHERTYPE_ATALK
)
2242 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2243 else /* proto == ETHERTYPE_AARP */
2244 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2248 * Check for Ethernet encapsulation (Ethertalk
2249 * phase 1?); we just check for the Ethernet
2252 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2258 if (proto
<= ETHERMTU
) {
2260 * This is an LLC SAP value, so the frames
2261 * that match would be 802.2 frames.
2262 * Check for the 802.2 protocol type
2263 * in the "Ethernet type" field, and
2264 * then check the DSAP.
2266 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2267 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2273 * This is an Ethernet type, so compare
2274 * the length/type field with it (if
2275 * the frame is an 802.2 frame, the length
2276 * field will be <= ETHERMTU, and, as
2277 * "proto" is > ETHERMTU, this test
2278 * will fail and the frame won't match,
2279 * which is what we want).
2281 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2286 static struct slist
*
2287 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2289 struct slist
*s1
, *s2
;
2290 struct slist
*sjeq_avs_cookie
;
2291 struct slist
*sjcommon
;
2294 * This code is not compatible with the optimizer, as
2295 * we are generating jmp instructions within a normal
2296 * slist of instructions
2298 cstate
->no_optimize
= 1;
2301 * Generate code to load the length of the radio header into
2302 * the register assigned to hold that length, if one has been
2303 * assigned. (If one hasn't been assigned, no code we've
2304 * generated uses that prefix, so we don't need to generate any
2307 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2308 * or always use the AVS header rather than the Prism header.
2309 * We load a 4-byte big-endian value at the beginning of the
2310 * raw packet data, and see whether, when masked with 0xFFFFF000,
2311 * it's equal to 0x80211000. If so, that indicates that it's
2312 * an AVS header (the masked-out bits are the version number).
2313 * Otherwise, it's a Prism header.
2315 * XXX - the Prism header is also, in theory, variable-length,
2316 * but no known software generates headers that aren't 144
2319 if (cstate
->off_linkhdr
.reg
!= -1) {
2323 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2327 * AND it with 0xFFFFF000.
2329 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2330 s2
->s
.k
= 0xFFFFF000;
2334 * Compare with 0x80211000.
2336 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2337 sjeq_avs_cookie
->s
.k
= 0x80211000;
2338 sappend(s1
, sjeq_avs_cookie
);
2343 * The 4 bytes at an offset of 4 from the beginning of
2344 * the AVS header are the length of the AVS header.
2345 * That field is big-endian.
2347 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2350 sjeq_avs_cookie
->s
.jt
= s2
;
2353 * Now jump to the code to allocate a register
2354 * into which to save the header length and
2355 * store the length there. (The "jump always"
2356 * instruction needs to have the k field set;
2357 * it's added to the PC, so, as we're jumping
2358 * over a single instruction, it should be 1.)
2360 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2362 sappend(s1
, sjcommon
);
2365 * Now for the code that handles the Prism header.
2366 * Just load the length of the Prism header (144)
2367 * into the A register. Have the test for an AVS
2368 * header branch here if we don't have an AVS header.
2370 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2373 sjeq_avs_cookie
->s
.jf
= s2
;
2376 * Now allocate a register to hold that value and store
2377 * it. The code for the AVS header will jump here after
2378 * loading the length of the AVS header.
2380 s2
= new_stmt(cstate
, BPF_ST
);
2381 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2383 sjcommon
->s
.jf
= s2
;
2386 * Now move it into the X register.
2388 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2396 static struct slist
*
2397 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2399 struct slist
*s1
, *s2
;
2402 * Generate code to load the length of the AVS header into
2403 * the register assigned to hold that length, if one has been
2404 * assigned. (If one hasn't been assigned, no code we've
2405 * generated uses that prefix, so we don't need to generate any
2408 if (cstate
->off_linkhdr
.reg
!= -1) {
2410 * The 4 bytes at an offset of 4 from the beginning of
2411 * the AVS header are the length of the AVS header.
2412 * That field is big-endian.
2414 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2418 * Now allocate a register to hold that value and store
2421 s2
= new_stmt(cstate
, BPF_ST
);
2422 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2426 * Now move it into the X register.
2428 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2436 static struct slist
*
2437 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2439 struct slist
*s1
, *s2
;
2442 * Generate code to load the length of the radiotap header into
2443 * the register assigned to hold that length, if one has been
2444 * assigned. (If one hasn't been assigned, no code we've
2445 * generated uses that prefix, so we don't need to generate any
2448 if (cstate
->off_linkhdr
.reg
!= -1) {
2450 * The 2 bytes at offsets of 2 and 3 from the beginning
2451 * of the radiotap header are the length of the radiotap
2452 * header; unfortunately, it's little-endian, so we have
2453 * to load it a byte at a time and construct the value.
2457 * Load the high-order byte, at an offset of 3, shift it
2458 * left a byte, and put the result in the X register.
2460 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2462 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2465 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2469 * Load the next byte, at an offset of 2, and OR the
2470 * value from the X register into it.
2472 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2475 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2479 * Now allocate a register to hold that value and store
2482 s2
= new_stmt(cstate
, BPF_ST
);
2483 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2487 * Now move it into the X register.
2489 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2498 * At the moment we treat PPI as normal Radiotap encoded
2499 * packets. The difference is in the function that generates
2500 * the code at the beginning to compute the header length.
2501 * Since this code generator of PPI supports bare 802.11
2502 * encapsulation only (i.e. the encapsulated DLT should be
2503 * DLT_IEEE802_11) we generate code to check for this too;
2504 * that's done in finish_parse().
2506 static struct slist
*
2507 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2509 struct slist
*s1
, *s2
;
2512 * Generate code to load the length of the radiotap header
2513 * into the register assigned to hold that length, if one has
2516 if (cstate
->off_linkhdr
.reg
!= -1) {
2518 * The 2 bytes at offsets of 2 and 3 from the beginning
2519 * of the radiotap header are the length of the radiotap
2520 * header; unfortunately, it's little-endian, so we have
2521 * to load it a byte at a time and construct the value.
2525 * Load the high-order byte, at an offset of 3, shift it
2526 * left a byte, and put the result in the X register.
2528 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2530 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2533 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2537 * Load the next byte, at an offset of 2, and OR the
2538 * value from the X register into it.
2540 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2543 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2547 * Now allocate a register to hold that value and store
2550 s2
= new_stmt(cstate
, BPF_ST
);
2551 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2555 * Now move it into the X register.
2557 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2566 * Load a value relative to the beginning of the link-layer header after the 802.11
2567 * header, i.e. LLC_SNAP.
2568 * The link-layer header doesn't necessarily begin at the beginning
2569 * of the packet data; there might be a variable-length prefix containing
2570 * radio information.
2572 static struct slist
*
2573 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2576 struct slist
*sjset_data_frame_1
;
2577 struct slist
*sjset_data_frame_2
;
2578 struct slist
*sjset_qos
;
2579 struct slist
*sjset_radiotap_flags_present
;
2580 struct slist
*sjset_radiotap_ext_present
;
2581 struct slist
*sjset_radiotap_tsft_present
;
2582 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2583 struct slist
*s_roundup
;
2585 if (cstate
->off_linkpl
.reg
== -1) {
2587 * No register has been assigned to the offset of
2588 * the link-layer payload, which means nobody needs
2589 * it; don't bother computing it - just return
2590 * what we already have.
2596 * This code is not compatible with the optimizer, as
2597 * we are generating jmp instructions within a normal
2598 * slist of instructions
2600 cstate
->no_optimize
= 1;
2603 * If "s" is non-null, it has code to arrange that the X register
2604 * contains the length of the prefix preceding the link-layer
2607 * Otherwise, the length of the prefix preceding the link-layer
2608 * header is "off_outermostlinkhdr.constant_part".
2612 * There is no variable-length header preceding the
2613 * link-layer header.
2615 * Load the length of the fixed-length prefix preceding
2616 * the link-layer header (if any) into the X register,
2617 * and store it in the cstate->off_linkpl.reg register.
2618 * That length is off_outermostlinkhdr.constant_part.
2620 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2621 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2625 * The X register contains the offset of the beginning of the
2626 * link-layer header; add 24, which is the minimum length
2627 * of the MAC header for a data frame, to that, and store it
2628 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2629 * which is at the offset in the X register, with an indexed load.
2631 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2633 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2636 s2
= new_stmt(cstate
, BPF_ST
);
2637 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2640 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2645 * Check the Frame Control field to see if this is a data frame;
2646 * a data frame has the 0x08 bit (b3) in that field set and the
2647 * 0x04 bit (b2) clear.
2649 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2650 sjset_data_frame_1
->s
.k
= 0x08;
2651 sappend(s
, sjset_data_frame_1
);
2654 * If b3 is set, test b2, otherwise go to the first statement of
2655 * the rest of the program.
2657 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2658 sjset_data_frame_2
->s
.k
= 0x04;
2659 sappend(s
, sjset_data_frame_2
);
2660 sjset_data_frame_1
->s
.jf
= snext
;
2663 * If b2 is not set, this is a data frame; test the QoS bit.
2664 * Otherwise, go to the first statement of the rest of the
2667 sjset_data_frame_2
->s
.jt
= snext
;
2668 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2669 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2670 sappend(s
, sjset_qos
);
2673 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2675 * Otherwise, go to the first statement of the rest of the
2678 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2679 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2681 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2684 s2
= new_stmt(cstate
, BPF_ST
);
2685 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2689 * If we have a radiotap header, look at it to see whether
2690 * there's Atheros padding between the MAC-layer header
2693 * Note: all of the fields in the radiotap header are
2694 * little-endian, so we byte-swap all of the values
2695 * we test against, as they will be loaded as big-endian
2698 * XXX - in the general case, we would have to scan through
2699 * *all* the presence bits, if there's more than one word of
2700 * presence bits. That would require a loop, meaning that
2701 * we wouldn't be able to run the filter in the kernel.
2703 * We assume here that the Atheros adapters that insert the
2704 * annoying padding don't have multiple antennae and therefore
2705 * do not generate radiotap headers with multiple presence words.
2707 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2709 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2710 * in the first presence flag word?
2712 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2716 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2717 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2718 sappend(s
, sjset_radiotap_flags_present
);
2721 * If not, skip all of this.
2723 sjset_radiotap_flags_present
->s
.jf
= snext
;
2726 * Otherwise, is the "extension" bit set in that word?
2728 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2729 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2730 sappend(s
, sjset_radiotap_ext_present
);
2731 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2734 * If so, skip all of this.
2736 sjset_radiotap_ext_present
->s
.jt
= snext
;
2739 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2741 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2742 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2743 sappend(s
, sjset_radiotap_tsft_present
);
2744 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2747 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2748 * at an offset of 16 from the beginning of the raw packet
2749 * data (8 bytes for the radiotap header and 8 bytes for
2752 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2755 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2758 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2760 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2761 sjset_tsft_datapad
->s
.k
= 0x20;
2762 sappend(s
, sjset_tsft_datapad
);
2765 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2766 * at an offset of 8 from the beginning of the raw packet
2767 * data (8 bytes for the radiotap header).
2769 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2772 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2775 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2777 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2778 sjset_notsft_datapad
->s
.k
= 0x20;
2779 sappend(s
, sjset_notsft_datapad
);
2782 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2783 * set, round the length of the 802.11 header to
2784 * a multiple of 4. Do that by adding 3 and then
2785 * dividing by and multiplying by 4, which we do by
2788 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2789 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2790 sappend(s
, s_roundup
);
2791 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2794 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2797 s2
= new_stmt(cstate
, BPF_ST
);
2798 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2801 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2802 sjset_tsft_datapad
->s
.jf
= snext
;
2803 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2804 sjset_notsft_datapad
->s
.jf
= snext
;
2806 sjset_qos
->s
.jf
= snext
;
2812 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2816 /* There is an implicit dependency between the link
2817 * payload and link header since the payload computation
2818 * includes the variable part of the header. Therefore,
2819 * if nobody else has allocated a register for the link
2820 * header and we need it, do it now. */
2821 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2822 cstate
->off_linkhdr
.reg
== -1)
2823 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2826 * For link-layer types that have a variable-length header
2827 * preceding the link-layer header, generate code to load
2828 * the offset of the link-layer header into the register
2829 * assigned to that offset, if any.
2831 * XXX - this, and the next switch statement, won't handle
2832 * encapsulation of 802.11 or 802.11+radio information in
2833 * some other protocol stack. That's significantly more
2836 switch (cstate
->outermostlinktype
) {
2838 case DLT_PRISM_HEADER
:
2839 s
= gen_load_prism_llprefixlen(cstate
);
2842 case DLT_IEEE802_11_RADIO_AVS
:
2843 s
= gen_load_avs_llprefixlen(cstate
);
2846 case DLT_IEEE802_11_RADIO
:
2847 s
= gen_load_radiotap_llprefixlen(cstate
);
2851 s
= gen_load_ppi_llprefixlen(cstate
);
2860 * For link-layer types that have a variable-length link-layer
2861 * header, generate code to load the offset of the link-layer
2862 * payload into the register assigned to that offset, if any.
2864 switch (cstate
->outermostlinktype
) {
2866 case DLT_IEEE802_11
:
2867 case DLT_PRISM_HEADER
:
2868 case DLT_IEEE802_11_RADIO_AVS
:
2869 case DLT_IEEE802_11_RADIO
:
2871 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2876 * If there there is no initialization yet and we need variable
2877 * length offsets for VLAN, initialize them to zero
2879 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2882 if (cstate
->off_linkpl
.reg
== -1)
2883 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2884 if (cstate
->off_linktype
.reg
== -1)
2885 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2887 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2889 s2
= new_stmt(cstate
, BPF_ST
);
2890 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2892 s2
= new_stmt(cstate
, BPF_ST
);
2893 s2
->s
.k
= cstate
->off_linktype
.reg
;
2898 * If we have any offset-loading code, append all the
2899 * existing statements in the block to those statements,
2900 * and make the resulting list the list of statements
2904 sappend(s
, b
->stmts
);
2909 static struct block
*
2910 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2912 struct slist
*s_load_dlt
;
2915 if (cstate
->linktype
== DLT_PPI
)
2917 /* Create the statements that check for the DLT
2919 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2920 s_load_dlt
->s
.k
= 4;
2922 b
= new_block(cstate
, JMP(BPF_JEQ
));
2924 b
->stmts
= s_load_dlt
;
2925 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2936 * Take an absolute offset, and:
2938 * if it has no variable part, return NULL;
2940 * if it has a variable part, generate code to load the register
2941 * containing that variable part into the X register, returning
2942 * a pointer to that code - if no register for that offset has
2943 * been allocated, allocate it first.
2945 * (The code to set that register will be generated later, but will
2946 * be placed earlier in the code sequence.)
2948 static struct slist
*
2949 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2953 if (off
->is_variable
) {
2954 if (off
->reg
== -1) {
2956 * We haven't yet assigned a register for the
2957 * variable part of the offset of the link-layer
2958 * header; allocate one.
2960 off
->reg
= alloc_reg(cstate
);
2964 * Load the register containing the variable part of the
2965 * offset of the link-layer header into the X register.
2967 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
2972 * That offset isn't variable, there's no variable part,
2973 * so we don't need to generate any code.
2980 * Map an Ethernet type to the equivalent PPP type.
2983 ethertype_to_ppptype(int proto
)
2991 case ETHERTYPE_IPV6
:
2999 case ETHERTYPE_ATALK
:
3013 * I'm assuming the "Bridging PDU"s that go
3014 * over PPP are Spanning Tree Protocol
3028 * Generate any tests that, for encapsulation of a link-layer packet
3029 * inside another protocol stack, need to be done to check for those
3030 * link-layer packets (and that haven't already been done by a check
3031 * for that encapsulation).
3033 static struct block
*
3034 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3038 if (cstate
->is_geneve
)
3039 return gen_geneve_ll_check(cstate
);
3041 switch (cstate
->prevlinktype
) {
3045 * This is LANE-encapsulated Ethernet; check that the LANE
3046 * packet doesn't begin with an LE Control marker, i.e.
3047 * that it's data, not a control message.
3049 * (We've already generated a test for LANE.)
3051 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3057 * No such tests are necessary.
3065 * The three different values we should check for when checking for an
3066 * IPv6 packet with DLT_NULL.
3068 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3069 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3070 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3073 * Generate code to match a particular packet type by matching the
3074 * link-layer type field or fields in the 802.2 LLC header.
3076 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3077 * value, if <= ETHERMTU.
3079 static struct block
*
3080 gen_linktype(compiler_state_t
*cstate
, int proto
)
3082 struct block
*b0
, *b1
, *b2
;
3083 const char *description
;
3085 /* are we checking MPLS-encapsulated packets? */
3086 if (cstate
->label_stack_depth
> 0) {
3090 /* FIXME add other L3 proto IDs */
3091 return gen_mpls_linktype(cstate
, Q_IP
);
3093 case ETHERTYPE_IPV6
:
3095 /* FIXME add other L3 proto IDs */
3096 return gen_mpls_linktype(cstate
, Q_IPV6
);
3099 bpf_error(cstate
, "unsupported protocol over mpls");
3104 switch (cstate
->linktype
) {
3107 case DLT_NETANALYZER
:
3108 case DLT_NETANALYZER_TRANSPARENT
:
3109 /* Geneve has an EtherType regardless of whether there is an
3111 if (!cstate
->is_geneve
)
3112 b0
= gen_prevlinkhdr_check(cstate
);
3116 b1
= gen_ether_linktype(cstate
, proto
);
3127 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3131 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3137 case DLT_IEEE802_11
:
3138 case DLT_PRISM_HEADER
:
3139 case DLT_IEEE802_11_RADIO_AVS
:
3140 case DLT_IEEE802_11_RADIO
:
3143 * Check that we have a data frame.
3145 b0
= gen_check_802_11_data_frame(cstate
);
3148 * Now check for the specified link-layer type.
3150 b1
= gen_llc_linktype(cstate
, proto
);
3158 * XXX - check for LLC frames.
3160 return gen_llc_linktype(cstate
, proto
);
3166 * XXX - check for LLC PDUs, as per IEEE 802.5.
3168 return gen_llc_linktype(cstate
, proto
);
3172 case DLT_ATM_RFC1483
:
3174 case DLT_IP_OVER_FC
:
3175 return gen_llc_linktype(cstate
, proto
);
3181 * Check for an LLC-encapsulated version of this protocol;
3182 * if we were checking for LANE, linktype would no longer
3185 * Check for LLC encapsulation and then check the protocol.
3187 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3188 b1
= gen_llc_linktype(cstate
, proto
);
3195 return gen_linux_sll_linktype(cstate
, proto
);
3200 case DLT_SLIP_BSDOS
:
3203 * These types don't provide any type field; packets
3204 * are always IPv4 or IPv6.
3206 * XXX - for IPv4, check for a version number of 4, and,
3207 * for IPv6, check for a version number of 6?
3212 /* Check for a version number of 4. */
3213 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3215 case ETHERTYPE_IPV6
:
3216 /* Check for a version number of 6. */
3217 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3220 return gen_false(cstate
); /* always false */
3227 * Raw IPv4, so no type field.
3229 if (proto
== ETHERTYPE_IP
)
3230 return gen_true(cstate
); /* always true */
3232 /* Checking for something other than IPv4; always false */
3233 return gen_false(cstate
);
3239 * Raw IPv6, so no type field.
3241 if (proto
== ETHERTYPE_IPV6
)
3242 return gen_true(cstate
); /* always true */
3244 /* Checking for something other than IPv6; always false */
3245 return gen_false(cstate
);
3251 case DLT_PPP_SERIAL
:
3254 * We use Ethernet protocol types inside libpcap;
3255 * map them to the corresponding PPP protocol types.
3257 proto
= ethertype_to_ppptype(proto
);
3258 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3264 * We use Ethernet protocol types inside libpcap;
3265 * map them to the corresponding PPP protocol types.
3271 * Also check for Van Jacobson-compressed IP.
3272 * XXX - do this for other forms of PPP?
3274 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3275 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3277 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3282 proto
= ethertype_to_ppptype(proto
);
3283 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3295 return (gen_loopback_linktype(cstate
, AF_INET
));
3297 case ETHERTYPE_IPV6
:
3299 * AF_ values may, unfortunately, be platform-
3300 * dependent; AF_INET isn't, because everybody
3301 * used 4.2BSD's value, but AF_INET6 is, because
3302 * 4.2BSD didn't have a value for it (given that
3303 * IPv6 didn't exist back in the early 1980's),
3304 * and they all picked their own values.
3306 * This means that, if we're reading from a
3307 * savefile, we need to check for all the
3310 * If we're doing a live capture, we only need
3311 * to check for this platform's value; however,
3312 * Npcap uses 24, which isn't Windows's AF_INET6
3313 * value. (Given the multiple different values,
3314 * programs that read pcap files shouldn't be
3315 * checking for their platform's AF_INET6 value
3316 * anyway, they should check for all of the
3317 * possible values. and they might as well do
3318 * that even for live captures.)
3320 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3322 * Savefile - check for all three
3323 * possible IPv6 values.
3325 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3326 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3328 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3333 * Live capture, so we only need to
3334 * check for the value used on this
3339 * Npcap doesn't use Windows's AF_INET6,
3340 * as that collides with AF_IPX on
3341 * some BSDs (both have the value 23).
3342 * Instead, it uses 24.
3344 return (gen_loopback_linktype(cstate
, 24));
3347 return (gen_loopback_linktype(cstate
, AF_INET6
));
3348 #else /* AF_INET6 */
3350 * I guess this platform doesn't support
3351 * IPv6, so we just reject all packets.
3353 return gen_false(cstate
);
3354 #endif /* AF_INET6 */
3360 * Not a type on which we support filtering.
3361 * XXX - support those that have AF_ values
3362 * #defined on this platform, at least?
3364 return gen_false(cstate
);
3367 #ifdef HAVE_NET_PFVAR_H
3370 * af field is host byte order in contrast to the rest of
3373 if (proto
== ETHERTYPE_IP
)
3374 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3375 BPF_B
, (bpf_int32
)AF_INET
));
3376 else if (proto
== ETHERTYPE_IPV6
)
3377 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3378 BPF_B
, (bpf_int32
)AF_INET6
));
3380 return gen_false(cstate
);
3383 #endif /* HAVE_NET_PFVAR_H */
3386 case DLT_ARCNET_LINUX
:
3388 * XXX should we check for first fragment if the protocol
3394 return gen_false(cstate
);
3396 case ETHERTYPE_IPV6
:
3397 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3398 (bpf_int32
)ARCTYPE_INET6
));
3401 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3402 (bpf_int32
)ARCTYPE_IP
);
3403 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3404 (bpf_int32
)ARCTYPE_IP_OLD
);
3409 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3410 (bpf_int32
)ARCTYPE_ARP
);
3411 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3412 (bpf_int32
)ARCTYPE_ARP_OLD
);
3416 case ETHERTYPE_REVARP
:
3417 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3418 (bpf_int32
)ARCTYPE_REVARP
));
3420 case ETHERTYPE_ATALK
:
3421 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3422 (bpf_int32
)ARCTYPE_ATALK
));
3429 case ETHERTYPE_ATALK
:
3430 return gen_true(cstate
);
3432 return gen_false(cstate
);
3439 * XXX - assumes a 2-byte Frame Relay header with
3440 * DLCI and flags. What if the address is longer?
3446 * Check for the special NLPID for IP.
3448 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3450 case ETHERTYPE_IPV6
:
3452 * Check for the special NLPID for IPv6.
3454 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3458 * Check for several OSI protocols.
3460 * Frame Relay packets typically have an OSI
3461 * NLPID at the beginning; we check for each
3464 * What we check for is the NLPID and a frame
3465 * control field of UI, i.e. 0x03 followed
3468 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3469 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3470 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3476 return gen_false(cstate
);
3482 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3484 case DLT_JUNIPER_MFR
:
3485 case DLT_JUNIPER_MLFR
:
3486 case DLT_JUNIPER_MLPPP
:
3487 case DLT_JUNIPER_ATM1
:
3488 case DLT_JUNIPER_ATM2
:
3489 case DLT_JUNIPER_PPPOE
:
3490 case DLT_JUNIPER_PPPOE_ATM
:
3491 case DLT_JUNIPER_GGSN
:
3492 case DLT_JUNIPER_ES
:
3493 case DLT_JUNIPER_MONITOR
:
3494 case DLT_JUNIPER_SERVICES
:
3495 case DLT_JUNIPER_ETHER
:
3496 case DLT_JUNIPER_PPP
:
3497 case DLT_JUNIPER_FRELAY
:
3498 case DLT_JUNIPER_CHDLC
:
3499 case DLT_JUNIPER_VP
:
3500 case DLT_JUNIPER_ST
:
3501 case DLT_JUNIPER_ISM
:
3502 case DLT_JUNIPER_VS
:
3503 case DLT_JUNIPER_SRX_E2E
:
3504 case DLT_JUNIPER_FIBRECHANNEL
:
3505 case DLT_JUNIPER_ATM_CEMIC
:
3507 /* just lets verify the magic number for now -
3508 * on ATM we may have up to 6 different encapsulations on the wire
3509 * and need a lot of heuristics to figure out that the payload
3512 * FIXME encapsulation specific BPF_ filters
3514 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3516 case DLT_BACNET_MS_TP
:
3517 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3520 return gen_ipnet_linktype(cstate
, proto
);
3522 case DLT_LINUX_IRDA
:
3523 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3526 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3529 case DLT_MTP2_WITH_PHDR
:
3530 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3533 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3536 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3538 case DLT_LINUX_LAPD
:
3539 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3541 case DLT_USB_FREEBSD
:
3543 case DLT_USB_LINUX_MMAPPED
:
3545 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3547 case DLT_BLUETOOTH_HCI_H4
:
3548 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3549 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3552 case DLT_CAN_SOCKETCAN
:
3553 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3555 case DLT_IEEE802_15_4
:
3556 case DLT_IEEE802_15_4_LINUX
:
3557 case DLT_IEEE802_15_4_NONASK_PHY
:
3558 case DLT_IEEE802_15_4_NOFCS
:
3559 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3561 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3562 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3565 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3568 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3571 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3574 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3577 /* Using the fixed-size NFLOG header it is possible to tell only
3578 * the address family of the packet, other meaningful data is
3579 * either missing or behind TLVs.
3581 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3585 * Does this link-layer header type have a field
3586 * indicating the type of the next protocol? If
3587 * so, off_linktype.constant_part will be the offset of that
3588 * field in the packet; if not, it will be OFFSET_NOT_SET.
3590 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3592 * Yes; assume it's an Ethernet type. (If
3593 * it's not, it needs to be handled specially
3596 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3599 * No; report an error.
3601 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3602 if (description
!= NULL
) {
3603 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3606 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3615 * Check for an LLC SNAP packet with a given organization code and
3616 * protocol type; we check the entire contents of the 802.2 LLC and
3617 * snap headers, checking for DSAP and SSAP of SNAP and a control
3618 * field of 0x03 in the LLC header, and for the specified organization
3619 * code and protocol type in the SNAP header.
3621 static struct block
*
3622 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3624 u_char snapblock
[8];
3626 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3627 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3628 snapblock
[2] = 0x03; /* control = UI */
3629 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3630 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3631 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3632 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3633 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3634 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3638 * Generate code to match frames with an LLC header.
3640 static struct block
*
3641 gen_llc_internal(compiler_state_t
*cstate
)
3643 struct block
*b0
, *b1
;
3645 switch (cstate
->linktype
) {
3649 * We check for an Ethernet type field less than
3650 * 1500, which means it's an 802.3 length field.
3652 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3656 * Now check for the purported DSAP and SSAP not being
3657 * 0xFF, to rule out NetWare-over-802.3.
3659 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3666 * We check for LLC traffic.
3668 b0
= gen_atmtype_llc(cstate
);
3671 case DLT_IEEE802
: /* Token Ring */
3673 * XXX - check for LLC frames.
3675 return gen_true(cstate
);
3679 * XXX - check for LLC frames.
3681 return gen_true(cstate
);
3683 case DLT_ATM_RFC1483
:
3685 * For LLC encapsulation, these are defined to have an
3688 * For VC encapsulation, they don't, but there's no
3689 * way to check for that; the protocol used on the VC
3690 * is negotiated out of band.
3692 return gen_true(cstate
);
3694 case DLT_IEEE802_11
:
3695 case DLT_PRISM_HEADER
:
3696 case DLT_IEEE802_11_RADIO
:
3697 case DLT_IEEE802_11_RADIO_AVS
:
3700 * Check that we have a data frame.
3702 b0
= gen_check_802_11_data_frame(cstate
);
3706 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3712 gen_llc(compiler_state_t
*cstate
)
3715 * Catch errors reported by us and routines below us, and return NULL
3718 if (setjmp(cstate
->top_ctx
))
3721 return gen_llc_internal(cstate
);
3725 gen_llc_i(compiler_state_t
*cstate
)
3727 struct block
*b0
, *b1
;
3731 * Catch errors reported by us and routines below us, and return NULL
3734 if (setjmp(cstate
->top_ctx
))
3738 * Check whether this is an LLC frame.
3740 b0
= gen_llc_internal(cstate
);
3743 * Load the control byte and test the low-order bit; it must
3744 * be clear for I frames.
3746 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3747 b1
= new_block(cstate
, JMP(BPF_JSET
));
3756 gen_llc_s(compiler_state_t
*cstate
)
3758 struct block
*b0
, *b1
;
3761 * Catch errors reported by us and routines below us, and return NULL
3764 if (setjmp(cstate
->top_ctx
))
3768 * Check whether this is an LLC frame.
3770 b0
= gen_llc_internal(cstate
);
3773 * Now compare the low-order 2 bit of the control byte against
3774 * the appropriate value for S frames.
3776 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3782 gen_llc_u(compiler_state_t
*cstate
)
3784 struct block
*b0
, *b1
;
3787 * Catch errors reported by us and routines below us, and return NULL
3790 if (setjmp(cstate
->top_ctx
))
3794 * Check whether this is an LLC frame.
3796 b0
= gen_llc_internal(cstate
);
3799 * Now compare the low-order 2 bit of the control byte against
3800 * the appropriate value for U frames.
3802 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3808 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3810 struct block
*b0
, *b1
;
3813 * Catch errors reported by us and routines below us, and return NULL
3816 if (setjmp(cstate
->top_ctx
))
3820 * Check whether this is an LLC frame.
3822 b0
= gen_llc_internal(cstate
);
3825 * Now check for an S frame with the appropriate type.
3827 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3833 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3835 struct block
*b0
, *b1
;
3838 * Catch errors reported by us and routines below us, and return NULL
3841 if (setjmp(cstate
->top_ctx
))
3845 * Check whether this is an LLC frame.
3847 b0
= gen_llc_internal(cstate
);
3850 * Now check for a U frame with the appropriate type.
3852 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3858 * Generate code to match a particular packet type, for link-layer types
3859 * using 802.2 LLC headers.
3861 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3862 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3864 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3865 * value, if <= ETHERMTU. We use that to determine whether to
3866 * match the DSAP or both DSAP and LSAP or to check the OUI and
3867 * protocol ID in a SNAP header.
3869 static struct block
*
3870 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3873 * XXX - handle token-ring variable-length header.
3879 case LLCSAP_NETBEUI
:
3881 * XXX - should we check both the DSAP and the
3882 * SSAP, like this, or should we check just the
3883 * DSAP, as we do for other SAP values?
3885 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3886 ((proto
<< 8) | proto
));
3890 * XXX - are there ever SNAP frames for IPX on
3891 * non-Ethernet 802.x networks?
3893 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3894 (bpf_int32
)LLCSAP_IPX
);
3896 case ETHERTYPE_ATALK
:
3898 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3899 * SNAP packets with an organization code of
3900 * 0x080007 (Apple, for Appletalk) and a protocol
3901 * type of ETHERTYPE_ATALK (Appletalk).
3903 * XXX - check for an organization code of
3904 * encapsulated Ethernet as well?
3906 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3910 * XXX - we don't have to check for IPX 802.3
3911 * here, but should we check for the IPX Ethertype?
3913 if (proto
<= ETHERMTU
) {
3915 * This is an LLC SAP value, so check
3918 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3921 * This is an Ethernet type; we assume that it's
3922 * unlikely that it'll appear in the right place
3923 * at random, and therefore check only the
3924 * location that would hold the Ethernet type
3925 * in a SNAP frame with an organization code of
3926 * 0x000000 (encapsulated Ethernet).
3928 * XXX - if we were to check for the SNAP DSAP and
3929 * LSAP, as per XXX, and were also to check for an
3930 * organization code of 0x000000 (encapsulated
3931 * Ethernet), we'd do
3933 * return gen_snap(cstate, 0x000000, proto);
3935 * here; for now, we don't, as per the above.
3936 * I don't know whether it's worth the extra CPU
3937 * time to do the right check or not.
3939 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3944 static struct block
*
3945 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3946 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3948 struct block
*b0
, *b1
;
3962 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3963 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3969 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3970 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3975 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3979 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3983 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3987 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
3991 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
3995 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4001 b0
= gen_linktype(cstate
, proto
);
4002 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
4008 static struct block
*
4009 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4010 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
4012 struct block
*b0
, *b1
;
4027 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4028 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4034 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4035 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4040 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4044 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4048 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4052 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4056 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4060 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4066 /* this order is important */
4067 a
= (uint32_t *)addr
;
4068 m
= (uint32_t *)mask
;
4069 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4070 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4072 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4074 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4076 b0
= gen_linktype(cstate
, proto
);
4082 static struct block
*
4083 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4085 register struct block
*b0
, *b1
;
4089 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4092 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4095 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4096 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4102 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4103 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4108 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4112 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4116 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4120 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4124 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4128 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4136 * Like gen_ehostop, but for DLT_FDDI
4138 static struct block
*
4139 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4141 struct block
*b0
, *b1
;
4145 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4148 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4151 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4152 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4158 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4159 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4164 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4168 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4172 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4176 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4180 bpf_error(cstate
, "'ra' is only supported on 802.11");
4184 bpf_error(cstate
, "'ta' is only supported on 802.11");
4192 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4194 static struct block
*
4195 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4197 register struct block
*b0
, *b1
;
4201 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4204 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4207 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4208 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4214 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4215 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4220 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4224 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4228 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4232 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4236 bpf_error(cstate
, "'ra' is only supported on 802.11");
4240 bpf_error(cstate
, "'ta' is only supported on 802.11");
4248 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4249 * various 802.11 + radio headers.
4251 static struct block
*
4252 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4254 register struct block
*b0
, *b1
, *b2
;
4255 register struct slist
*s
;
4257 #ifdef ENABLE_WLAN_FILTERING_PATCH
4260 * We need to disable the optimizer because the optimizer is buggy
4261 * and wipes out some LD instructions generated by the below
4262 * code to validate the Frame Control bits
4264 cstate
->no_optimize
= 1;
4265 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4272 * For control frames, there is no SA.
4274 * For management frames, SA is at an
4275 * offset of 10 from the beginning of
4278 * For data frames, SA is at an offset
4279 * of 10 from the beginning of the packet
4280 * if From DS is clear, at an offset of
4281 * 16 from the beginning of the packet
4282 * if From DS is set and To DS is clear,
4283 * and an offset of 24 from the beginning
4284 * of the packet if From DS is set and To DS
4289 * Generate the tests to be done for data frames
4292 * First, check for To DS set, i.e. check "link[1] & 0x01".
4294 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4295 b1
= new_block(cstate
, JMP(BPF_JSET
));
4296 b1
->s
.k
= 0x01; /* To DS */
4300 * If To DS is set, the SA is at 24.
4302 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4306 * Now, check for To DS not set, i.e. check
4307 * "!(link[1] & 0x01)".
4309 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4310 b2
= new_block(cstate
, JMP(BPF_JSET
));
4311 b2
->s
.k
= 0x01; /* To DS */
4316 * If To DS is not set, the SA is at 16.
4318 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4322 * Now OR together the last two checks. That gives
4323 * the complete set of checks for data frames with
4329 * Now check for From DS being set, and AND that with
4330 * the ORed-together checks.
4332 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4333 b1
= new_block(cstate
, JMP(BPF_JSET
));
4334 b1
->s
.k
= 0x02; /* From DS */
4339 * Now check for data frames with From DS not set.
4341 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4342 b2
= new_block(cstate
, JMP(BPF_JSET
));
4343 b2
->s
.k
= 0x02; /* From DS */
4348 * If From DS isn't set, the SA is at 10.
4350 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4354 * Now OR together the checks for data frames with
4355 * From DS not set and for data frames with From DS
4356 * set; that gives the checks done for data frames.
4361 * Now check for a data frame.
4362 * I.e, check "link[0] & 0x08".
4364 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4365 b1
= new_block(cstate
, JMP(BPF_JSET
));
4370 * AND that with the checks done for data frames.
4375 * If the high-order bit of the type value is 0, this
4376 * is a management frame.
4377 * I.e, check "!(link[0] & 0x08)".
4379 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4380 b2
= new_block(cstate
, JMP(BPF_JSET
));
4386 * For management frames, the SA is at 10.
4388 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4392 * OR that with the checks done for data frames.
4393 * That gives the checks done for management and
4399 * If the low-order bit of the type value is 1,
4400 * this is either a control frame or a frame
4401 * with a reserved type, and thus not a
4404 * I.e., check "!(link[0] & 0x04)".
4406 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4407 b1
= new_block(cstate
, JMP(BPF_JSET
));
4413 * AND that with the checks for data and management
4423 * For control frames, there is no DA.
4425 * For management frames, DA is at an
4426 * offset of 4 from the beginning of
4429 * For data frames, DA is at an offset
4430 * of 4 from the beginning of the packet
4431 * if To DS is clear and at an offset of
4432 * 16 from the beginning of the packet
4437 * Generate the tests to be done for data frames.
4439 * First, check for To DS set, i.e. "link[1] & 0x01".
4441 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4442 b1
= new_block(cstate
, JMP(BPF_JSET
));
4443 b1
->s
.k
= 0x01; /* To DS */
4447 * If To DS is set, the DA is at 16.
4449 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4453 * Now, check for To DS not set, i.e. check
4454 * "!(link[1] & 0x01)".
4456 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4457 b2
= new_block(cstate
, JMP(BPF_JSET
));
4458 b2
->s
.k
= 0x01; /* To DS */
4463 * If To DS is not set, the DA is at 4.
4465 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4469 * Now OR together the last two checks. That gives
4470 * the complete set of checks for data frames.
4475 * Now check for a data frame.
4476 * I.e, check "link[0] & 0x08".
4478 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4479 b1
= new_block(cstate
, JMP(BPF_JSET
));
4484 * AND that with the checks done for data frames.
4489 * If the high-order bit of the type value is 0, this
4490 * is a management frame.
4491 * I.e, check "!(link[0] & 0x08)".
4493 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4494 b2
= new_block(cstate
, JMP(BPF_JSET
));
4500 * For management frames, the DA is at 4.
4502 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4506 * OR that with the checks done for data frames.
4507 * That gives the checks done for management and
4513 * If the low-order bit of the type value is 1,
4514 * this is either a control frame or a frame
4515 * with a reserved type, and thus not a
4518 * I.e., check "!(link[0] & 0x04)".
4520 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4521 b1
= new_block(cstate
, JMP(BPF_JSET
));
4527 * AND that with the checks for data and management
4534 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4535 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4541 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4542 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4547 * XXX - add BSSID keyword?
4550 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4554 * Not present in CTS or ACK control frames.
4556 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4557 IEEE80211_FC0_TYPE_MASK
);
4559 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4560 IEEE80211_FC0_SUBTYPE_MASK
);
4562 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4563 IEEE80211_FC0_SUBTYPE_MASK
);
4567 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4573 * Not present in control frames.
4575 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4576 IEEE80211_FC0_TYPE_MASK
);
4578 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4584 * Present only if the direction mask has both "From DS"
4585 * and "To DS" set. Neither control frames nor management
4586 * frames should have both of those set, so we don't
4587 * check the frame type.
4589 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4590 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4591 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4597 * Not present in management frames; addr1 in other
4602 * If the high-order bit of the type value is 0, this
4603 * is a management frame.
4604 * I.e, check "(link[0] & 0x08)".
4606 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4607 b1
= new_block(cstate
, JMP(BPF_JSET
));
4614 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4617 * AND that with the check of addr1.
4624 * Not present in management frames; addr2, if present,
4629 * Not present in CTS or ACK control frames.
4631 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4632 IEEE80211_FC0_TYPE_MASK
);
4634 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4635 IEEE80211_FC0_SUBTYPE_MASK
);
4637 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4638 IEEE80211_FC0_SUBTYPE_MASK
);
4644 * If the high-order bit of the type value is 0, this
4645 * is a management frame.
4646 * I.e, check "(link[0] & 0x08)".
4648 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4649 b1
= new_block(cstate
, JMP(BPF_JSET
));
4654 * AND that with the check for frames other than
4655 * CTS and ACK frames.
4662 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4671 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4672 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4673 * as the RFC states.)
4675 static struct block
*
4676 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4678 register struct block
*b0
, *b1
;
4682 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4685 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4688 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4689 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4695 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4696 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4701 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4705 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4709 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4713 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4717 bpf_error(cstate
, "'ra' is only supported on 802.11");
4721 bpf_error(cstate
, "'ta' is only supported on 802.11");
4729 * This is quite tricky because there may be pad bytes in front of the
4730 * DECNET header, and then there are two possible data packet formats that
4731 * carry both src and dst addresses, plus 5 packet types in a format that
4732 * carries only the src node, plus 2 types that use a different format and
4733 * also carry just the src node.
4737 * Instead of doing those all right, we just look for data packets with
4738 * 0 or 1 bytes of padding. If you want to look at other packets, that
4739 * will require a lot more hacking.
4741 * To add support for filtering on DECNET "areas" (network numbers)
4742 * one would want to add a "mask" argument to this routine. That would
4743 * make the filter even more inefficient, although one could be clever
4744 * and not generate masking instructions if the mask is 0xFFFF.
4746 static struct block
*
4747 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4749 struct block
*b0
, *b1
, *b2
, *tmp
;
4750 u_int offset_lh
; /* offset if long header is received */
4751 u_int offset_sh
; /* offset if short header is received */
4756 offset_sh
= 1; /* follows flags */
4757 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4761 offset_sh
= 3; /* follows flags, dstnode */
4762 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4766 /* Inefficient because we do our Calvinball dance twice */
4767 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4768 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4774 /* Inefficient because we do our Calvinball dance twice */
4775 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4776 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4781 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4785 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4789 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4793 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4797 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4801 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4807 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4808 /* Check for pad = 1, long header case */
4809 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4810 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4811 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4812 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4814 /* Check for pad = 0, long header case */
4815 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4816 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4819 /* Check for pad = 1, short header case */
4820 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4821 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4822 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4825 /* Check for pad = 0, short header case */
4826 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4827 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4831 /* Combine with test for cstate->linktype */
4837 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4838 * test the bottom-of-stack bit, and then check the version number
4839 * field in the IP header.
4841 static struct block
*
4842 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4844 struct block
*b0
, *b1
;
4849 /* match the bottom-of-stack bit */
4850 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4851 /* match the IPv4 version number */
4852 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4857 /* match the bottom-of-stack bit */
4858 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4859 /* match the IPv4 version number */
4860 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4869 static struct block
*
4870 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4871 int proto
, int dir
, int type
)
4873 struct block
*b0
, *b1
;
4874 const char *typestr
;
4884 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4886 * Only check for non-IPv4 addresses if we're not
4887 * checking MPLS-encapsulated packets.
4889 if (cstate
->label_stack_depth
== 0) {
4890 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4892 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4898 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4901 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4904 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4907 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4910 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4913 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4916 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4919 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4922 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4925 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4928 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4931 return gen_dnhostop(cstate
, addr
, dir
);
4934 bpf_error(cstate
, "LAT host filtering not implemented");
4937 bpf_error(cstate
, "SCA host filtering not implemented");
4940 bpf_error(cstate
, "MOPRC host filtering not implemented");
4943 bpf_error(cstate
, "MOPDL host filtering not implemented");
4946 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4949 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4952 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4955 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4958 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4961 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4964 bpf_error(cstate
, "AARP host filtering not implemented");
4967 bpf_error(cstate
, "ISO host filtering not implemented");
4970 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
4973 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
4976 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
4979 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
4982 bpf_error(cstate
, "IPX host filtering not implemented");
4985 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
4988 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
4991 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
4994 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
4997 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5000 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5003 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5006 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5009 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5012 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5021 static struct block
*
5022 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5023 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5025 const char *typestr
;
5035 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5038 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5041 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5044 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5047 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5050 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5053 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5056 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5059 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5062 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5065 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5068 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5071 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5074 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5077 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5080 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5083 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5086 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5089 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5092 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5095 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5098 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5101 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5104 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5107 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5110 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5113 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5116 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5119 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5122 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5125 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5128 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5131 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5134 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5137 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5140 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5143 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5146 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5149 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5152 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5162 static struct block
*
5163 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5164 struct addrinfo
*alist
, int proto
, int dir
)
5166 struct block
*b0
, *b1
, *tmp
;
5167 struct addrinfo
*ai
;
5168 struct sockaddr_in
*sin
;
5171 bpf_error(cstate
, "direction applied to 'gateway'");
5178 switch (cstate
->linktype
) {
5180 case DLT_NETANALYZER
:
5181 case DLT_NETANALYZER_TRANSPARENT
:
5182 b1
= gen_prevlinkhdr_check(cstate
);
5183 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5188 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5191 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5193 case DLT_IEEE802_11
:
5194 case DLT_PRISM_HEADER
:
5195 case DLT_IEEE802_11_RADIO_AVS
:
5196 case DLT_IEEE802_11_RADIO
:
5198 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5202 * This is LLC-multiplexed traffic; if it were
5203 * LANE, cstate->linktype would have been set to
5207 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5209 case DLT_IP_OVER_FC
:
5210 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5214 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5217 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5219 * Does it have an address?
5221 if (ai
->ai_addr
!= NULL
) {
5223 * Yes. Is it an IPv4 address?
5225 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5227 * Generate an entry for it.
5229 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5230 tmp
= gen_host(cstate
,
5231 ntohl(sin
->sin_addr
.s_addr
),
5232 0xffffffff, proto
, Q_OR
, Q_HOST
);
5234 * Is it the *first* IPv4 address?
5238 * Yes, so start with it.
5243 * No, so OR it into the
5255 * No IPv4 addresses found.
5263 bpf_error(cstate
, "illegal modifier of 'gateway'");
5268 static struct block
*
5269 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5277 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5278 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5283 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5284 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5289 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5290 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5295 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5298 #ifndef IPPROTO_IGMP
5299 #define IPPROTO_IGMP 2
5303 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5306 #ifndef IPPROTO_IGRP
5307 #define IPPROTO_IGRP 9
5310 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5314 #define IPPROTO_PIM 103
5318 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5319 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5323 #ifndef IPPROTO_VRRP
5324 #define IPPROTO_VRRP 112
5328 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5331 #ifndef IPPROTO_CARP
5332 #define IPPROTO_CARP 112
5336 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5340 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5344 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5348 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5352 bpf_error(cstate
, "link layer applied in wrong context");
5355 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5359 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5363 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5367 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5371 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5375 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5379 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5383 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5386 #ifndef IPPROTO_ICMPV6
5387 #define IPPROTO_ICMPV6 58
5390 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5394 #define IPPROTO_AH 51
5397 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5398 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5403 #define IPPROTO_ESP 50
5406 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5407 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5412 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5416 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5420 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5423 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5424 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5425 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5427 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5429 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5431 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5435 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5436 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5437 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5439 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5441 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5443 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5447 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5448 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5449 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5451 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5456 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5457 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5462 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5463 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5465 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5467 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5472 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5473 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5478 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5479 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5484 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5488 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5492 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5496 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5500 bpf_error(cstate
, "'radio' is not a valid protocol type");
5509 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5512 * Catch errors reported by us and routines below us, and return NULL
5515 if (setjmp(cstate
->top_ctx
))
5518 return gen_proto_abbrev_internal(cstate
, proto
);
5521 static struct block
*
5522 gen_ipfrag(compiler_state_t
*cstate
)
5527 /* not IPv4 frag other than the first frag */
5528 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5529 b
= new_block(cstate
, JMP(BPF_JSET
));
5538 * Generate a comparison to a port value in the transport-layer header
5539 * at the specified offset from the beginning of that header.
5541 * XXX - this handles a variable-length prefix preceding the link-layer
5542 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5543 * variable-length link-layer headers (such as Token Ring or 802.11
5546 static struct block
*
5547 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5549 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5552 static struct block
*
5553 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5555 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5559 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5561 struct block
*b0
, *b1
, *tmp
;
5563 /* ip proto 'proto' and not a fragment other than the first fragment */
5564 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5565 b0
= gen_ipfrag(cstate
);
5570 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5574 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5578 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5579 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5585 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5586 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5591 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5595 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5599 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5603 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5607 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5611 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5622 static struct block
*
5623 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5625 struct block
*b0
, *b1
, *tmp
;
5630 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5631 * not LLC encapsulation with LLCSAP_IP.
5633 * For IEEE 802 networks - which includes 802.5 token ring
5634 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5635 * says that SNAP encapsulation is used, not LLC encapsulation
5638 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5639 * RFC 2225 say that SNAP encapsulation is used, not LLC
5640 * encapsulation with LLCSAP_IP.
5642 * So we always check for ETHERTYPE_IP.
5644 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5650 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5654 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5655 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5657 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5669 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5671 struct block
*b0
, *b1
, *tmp
;
5673 /* ip6 proto 'proto' */
5674 /* XXX - catch the first fragment of a fragmented packet? */
5675 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5679 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5683 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5687 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5688 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5694 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5695 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5707 static struct block
*
5708 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5710 struct block
*b0
, *b1
, *tmp
;
5712 /* link proto ip6 */
5713 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5719 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5723 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5724 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5726 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5737 /* gen_portrange code */
5738 static struct block
*
5739 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5742 struct block
*b1
, *b2
;
5746 * Reverse the order of the ports, so v1 is the lower one.
5755 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5756 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5764 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5767 struct block
*b0
, *b1
, *tmp
;
5769 /* ip proto 'proto' and not a fragment other than the first fragment */
5770 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5771 b0
= gen_ipfrag(cstate
);
5776 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5780 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5784 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5785 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5791 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5792 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5797 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5801 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5805 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5809 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5813 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5817 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5828 static struct block
*
5829 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5832 struct block
*b0
, *b1
, *tmp
;
5835 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5841 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5845 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5846 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5848 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5859 static struct block
*
5860 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5863 struct block
*b1
, *b2
;
5867 * Reverse the order of the ports, so v1 is the lower one.
5876 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5877 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5885 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5888 struct block
*b0
, *b1
, *tmp
;
5890 /* ip6 proto 'proto' */
5891 /* XXX - catch the first fragment of a fragmented packet? */
5892 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5896 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5900 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5904 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5905 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5911 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5912 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5924 static struct block
*
5925 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5928 struct block
*b0
, *b1
, *tmp
;
5930 /* link proto ip6 */
5931 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5937 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5941 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5942 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5944 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5956 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5965 v
= pcap_nametoproto(name
);
5966 if (v
== PROTO_UNDEF
)
5967 bpf_error(cstate
, "unknown ip proto '%s'", name
);
5971 /* XXX should look up h/w protocol type based on cstate->linktype */
5972 v
= pcap_nametoeproto(name
);
5973 if (v
== PROTO_UNDEF
) {
5974 v
= pcap_nametollc(name
);
5975 if (v
== PROTO_UNDEF
)
5976 bpf_error(cstate
, "unknown ether proto '%s'", name
);
5981 if (strcmp(name
, "esis") == 0)
5983 else if (strcmp(name
, "isis") == 0)
5985 else if (strcmp(name
, "clnp") == 0)
5988 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6000 gen_joinsp(struct stmt
**s
, int n
)
6006 static struct block
*
6007 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6009 #ifdef NO_PROTOCHAIN
6010 return gen_proto(cstate
, v
, proto
, dir
);
6012 struct block
*b0
, *b
;
6013 struct slist
*s
[100];
6014 int fix2
, fix3
, fix4
, fix5
;
6015 int ahcheck
, again
, end
;
6017 int reg2
= alloc_reg(cstate
);
6019 memset(s
, 0, sizeof(s
));
6020 fix3
= fix4
= fix5
= 0;
6027 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
6028 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
6032 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6037 * We don't handle variable-length prefixes before the link-layer
6038 * header, or variable-length link-layer headers, here yet.
6039 * We might want to add BPF instructions to do the protochain
6040 * work, to simplify that and, on platforms that have a BPF
6041 * interpreter with the new instructions, let the filtering
6042 * be done in the kernel. (We already require a modified BPF
6043 * engine to do the protochain stuff, to support backward
6044 * branches, and backward branch support is unlikely to appear
6045 * in kernel BPF engines.)
6047 if (cstate
->off_linkpl
.is_variable
)
6048 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6050 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
6053 * s[0] is a dummy entry to protect other BPF insn from damage
6054 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6055 * hard to find interdependency made by jump table fixup.
6058 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6063 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6066 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6067 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6069 /* X = ip->ip_hl << 2 */
6070 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6071 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6076 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6078 /* A = ip6->ip_nxt */
6079 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6080 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6082 /* X = sizeof(struct ip6_hdr) */
6083 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6089 bpf_error(cstate
, "unsupported proto to gen_protochain");
6093 /* again: if (A == v) goto end; else fall through; */
6095 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6097 s
[i
]->s
.jt
= NULL
; /*later*/
6098 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6102 #ifndef IPPROTO_NONE
6103 #define IPPROTO_NONE 59
6105 /* if (A == IPPROTO_NONE) goto end */
6106 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6107 s
[i
]->s
.jt
= NULL
; /*later*/
6108 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6109 s
[i
]->s
.k
= IPPROTO_NONE
;
6110 s
[fix5
]->s
.jf
= s
[i
];
6114 if (proto
== Q_IPV6
) {
6115 int v6start
, v6end
, v6advance
, j
;
6118 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6119 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6120 s
[i
]->s
.jt
= NULL
; /*later*/
6121 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6122 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6123 s
[fix2
]->s
.jf
= s
[i
];
6125 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6126 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6127 s
[i
]->s
.jt
= NULL
; /*later*/
6128 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6129 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6131 /* if (A == IPPROTO_ROUTING) goto v6advance */
6132 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6133 s
[i
]->s
.jt
= NULL
; /*later*/
6134 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6135 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6137 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6138 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6139 s
[i
]->s
.jt
= NULL
; /*later*/
6140 s
[i
]->s
.jf
= NULL
; /*later*/
6141 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6151 * A = P[X + packet head];
6152 * X = X + (P[X + packet head + 1] + 1) * 8;
6154 /* A = P[X + packet head] */
6155 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6156 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6159 s
[i
] = new_stmt(cstate
, BPF_ST
);
6162 /* A = P[X + packet head + 1]; */
6163 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6164 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6167 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6171 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6175 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6179 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6182 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6186 /* goto again; (must use BPF_JA for backward jump) */
6187 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6188 s
[i
]->s
.k
= again
- i
- 1;
6189 s
[i
- 1]->s
.jf
= s
[i
];
6193 for (j
= v6start
; j
<= v6end
; j
++)
6194 s
[j
]->s
.jt
= s
[v6advance
];
6197 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6199 s
[fix2
]->s
.jf
= s
[i
];
6205 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6206 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6207 s
[i
]->s
.jt
= NULL
; /*later*/
6208 s
[i
]->s
.jf
= NULL
; /*later*/
6209 s
[i
]->s
.k
= IPPROTO_AH
;
6211 s
[fix3
]->s
.jf
= s
[ahcheck
];
6218 * X = X + (P[X + 1] + 2) * 4;
6221 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6223 /* A = P[X + packet head]; */
6224 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6225 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6228 s
[i
] = new_stmt(cstate
, BPF_ST
);
6232 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6235 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6239 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6241 /* A = P[X + packet head] */
6242 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6243 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6246 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6250 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6254 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6257 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6261 /* goto again; (must use BPF_JA for backward jump) */
6262 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6263 s
[i
]->s
.k
= again
- i
- 1;
6268 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6270 s
[fix2
]->s
.jt
= s
[end
];
6271 s
[fix4
]->s
.jf
= s
[end
];
6272 s
[fix5
]->s
.jt
= s
[end
];
6279 for (i
= 0; i
< max
- 1; i
++)
6280 s
[i
]->next
= s
[i
+ 1];
6281 s
[max
- 1]->next
= NULL
;
6286 b
= new_block(cstate
, JMP(BPF_JEQ
));
6287 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6290 free_reg(cstate
, reg2
);
6297 static struct block
*
6298 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6301 struct block
*b0
, *b1
;
6304 * A data frame has the 0x08 bit (b3) in the frame control field set
6305 * and the 0x04 bit (b2) clear.
6307 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6308 b0
= new_block(cstate
, JMP(BPF_JSET
));
6312 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6313 b1
= new_block(cstate
, JMP(BPF_JSET
));
6324 * Generate code that checks whether the packet is a packet for protocol
6325 * <proto> and whether the type field in that protocol's header has
6326 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6327 * IP packet and checks the protocol number in the IP header against <v>.
6329 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6330 * against Q_IP and Q_IPV6.
6332 static struct block
*
6333 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6335 struct block
*b0
, *b1
;
6340 if (dir
!= Q_DEFAULT
)
6341 bpf_error(cstate
, "direction applied to 'proto'");
6345 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6346 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6351 return gen_linktype(cstate
, v
);
6355 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6356 * not LLC encapsulation with LLCSAP_IP.
6358 * For IEEE 802 networks - which includes 802.5 token ring
6359 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6360 * says that SNAP encapsulation is used, not LLC encapsulation
6363 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6364 * RFC 2225 say that SNAP encapsulation is used, not LLC
6365 * encapsulation with LLCSAP_IP.
6367 * So we always check for ETHERTYPE_IP.
6369 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6371 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6373 b1
= gen_protochain(cstate
, v
, Q_IP
);
6379 bpf_error(cstate
, "arp does not encapsulate another protocol");
6383 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6387 bpf_error(cstate
, "'sctp proto' is bogus");
6391 bpf_error(cstate
, "'tcp proto' is bogus");
6395 bpf_error(cstate
, "'udp proto' is bogus");
6399 bpf_error(cstate
, "'icmp proto' is bogus");
6403 bpf_error(cstate
, "'igmp proto' is bogus");
6407 bpf_error(cstate
, "'igrp proto' is bogus");
6411 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6415 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6419 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6423 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6427 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6431 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6435 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6438 * Also check for a fragment header before the final
6441 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6442 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6444 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6447 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6453 bpf_error(cstate
, "'icmp6 proto' is bogus");
6457 bpf_error(cstate
, "'ah proto' is bogus");
6461 bpf_error(cstate
, "'ah proto' is bogus");
6465 bpf_error(cstate
, "'pim proto' is bogus");
6469 bpf_error(cstate
, "'vrrp proto' is bogus");
6473 bpf_error(cstate
, "'aarp proto' is bogus");
6477 switch (cstate
->linktype
) {
6481 * Frame Relay packets typically have an OSI
6482 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6483 * generates code to check for all the OSI
6484 * NLPIDs, so calling it and then adding a check
6485 * for the particular NLPID for which we're
6486 * looking is bogus, as we can just check for
6489 * What we check for is the NLPID and a frame
6490 * control field value of UI, i.e. 0x03 followed
6493 * XXX - assumes a 2-byte Frame Relay header with
6494 * DLCI and flags. What if the address is longer?
6496 * XXX - what about SNAP-encapsulated frames?
6498 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6504 * Cisco uses an Ethertype lookalike - for OSI,
6507 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6508 /* OSI in C-HDLC is stuffed with a fudge byte */
6509 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (bpf_int32
)v
);
6514 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6515 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (bpf_int32
)v
);
6521 bpf_error(cstate
, "'esis proto' is bogus");
6525 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6527 * 4 is the offset of the PDU type relative to the IS-IS
6530 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (bpf_int32
)v
);
6535 bpf_error(cstate
, "'clnp proto' is not supported");
6539 bpf_error(cstate
, "'stp proto' is bogus");
6543 bpf_error(cstate
, "'ipx proto' is bogus");
6547 bpf_error(cstate
, "'netbeui proto' is bogus");
6551 bpf_error(cstate
, "'l1 proto' is bogus");
6555 bpf_error(cstate
, "'l2 proto' is bogus");
6559 bpf_error(cstate
, "'iih proto' is bogus");
6563 bpf_error(cstate
, "'snp proto' is bogus");
6567 bpf_error(cstate
, "'csnp proto' is bogus");
6571 bpf_error(cstate
, "'psnp proto' is bogus");
6575 bpf_error(cstate
, "'lsp proto' is bogus");
6579 bpf_error(cstate
, "'radio proto' is bogus");
6583 bpf_error(cstate
, "'carp proto' is bogus");
6594 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6596 int proto
= q
.proto
;
6600 bpf_u_int32 mask
, addr
;
6601 struct addrinfo
*res
, *res0
;
6602 struct sockaddr_in
*sin4
;
6605 struct sockaddr_in6
*sin6
;
6606 struct in6_addr mask128
;
6608 struct block
*b
, *tmp
;
6609 int port
, real_proto
;
6613 * Catch errors reported by us and routines below us, and return NULL
6616 if (setjmp(cstate
->top_ctx
))
6622 addr
= pcap_nametonetaddr(name
);
6624 bpf_error(cstate
, "unknown network '%s'", name
);
6625 /* Left justify network addr and calculate its network mask */
6627 while (addr
&& (addr
& 0xff000000) == 0) {
6631 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6635 if (proto
== Q_LINK
) {
6636 switch (cstate
->linktype
) {
6639 case DLT_NETANALYZER
:
6640 case DLT_NETANALYZER_TRANSPARENT
:
6641 eaddr
= pcap_ether_hostton(name
);
6644 "unknown ether host '%s'", name
);
6645 tmp
= gen_prevlinkhdr_check(cstate
);
6646 b
= gen_ehostop(cstate
, eaddr
, dir
);
6653 eaddr
= pcap_ether_hostton(name
);
6656 "unknown FDDI host '%s'", name
);
6657 b
= gen_fhostop(cstate
, eaddr
, dir
);
6662 eaddr
= pcap_ether_hostton(name
);
6665 "unknown token ring host '%s'", name
);
6666 b
= gen_thostop(cstate
, eaddr
, dir
);
6670 case DLT_IEEE802_11
:
6671 case DLT_PRISM_HEADER
:
6672 case DLT_IEEE802_11_RADIO_AVS
:
6673 case DLT_IEEE802_11_RADIO
:
6675 eaddr
= pcap_ether_hostton(name
);
6678 "unknown 802.11 host '%s'", name
);
6679 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6683 case DLT_IP_OVER_FC
:
6684 eaddr
= pcap_ether_hostton(name
);
6687 "unknown Fibre Channel host '%s'", name
);
6688 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6693 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6694 } else if (proto
== Q_DECNET
) {
6695 unsigned short dn_addr
;
6697 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6699 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6701 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6706 * I don't think DECNET hosts can be multihomed, so
6707 * there is no need to build up a list of addresses
6709 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6712 memset(&mask128
, 0xff, sizeof(mask128
));
6714 res0
= res
= pcap_nametoaddrinfo(name
);
6716 bpf_error(cstate
, "unknown host '%s'", name
);
6723 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6724 tproto
== Q_DEFAULT
) {
6730 for (res
= res0
; res
; res
= res
->ai_next
) {
6731 switch (res
->ai_family
) {
6734 if (tproto
== Q_IPV6
)
6738 sin4
= (struct sockaddr_in
*)
6740 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6741 0xffffffff, tproto
, dir
, q
.addr
);
6745 if (tproto6
== Q_IP
)
6748 sin6
= (struct sockaddr_in6
*)
6750 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6751 &mask128
, tproto6
, dir
, q
.addr
);
6764 bpf_error(cstate
, "unknown host '%s'%s", name
,
6765 (proto
== Q_DEFAULT
)
6767 : " for specified address family");
6773 if (proto
!= Q_DEFAULT
&&
6774 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6775 bpf_error(cstate
, "illegal qualifier of 'port'");
6776 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6777 bpf_error(cstate
, "unknown port '%s'", name
);
6778 if (proto
== Q_UDP
) {
6779 if (real_proto
== IPPROTO_TCP
)
6780 bpf_error(cstate
, "port '%s' is tcp", name
);
6781 else if (real_proto
== IPPROTO_SCTP
)
6782 bpf_error(cstate
, "port '%s' is sctp", name
);
6784 /* override PROTO_UNDEF */
6785 real_proto
= IPPROTO_UDP
;
6787 if (proto
== Q_TCP
) {
6788 if (real_proto
== IPPROTO_UDP
)
6789 bpf_error(cstate
, "port '%s' is udp", name
);
6791 else if (real_proto
== IPPROTO_SCTP
)
6792 bpf_error(cstate
, "port '%s' is sctp", name
);
6794 /* override PROTO_UNDEF */
6795 real_proto
= IPPROTO_TCP
;
6797 if (proto
== Q_SCTP
) {
6798 if (real_proto
== IPPROTO_UDP
)
6799 bpf_error(cstate
, "port '%s' is udp", name
);
6801 else if (real_proto
== IPPROTO_TCP
)
6802 bpf_error(cstate
, "port '%s' is tcp", name
);
6804 /* override PROTO_UNDEF */
6805 real_proto
= IPPROTO_SCTP
;
6808 bpf_error(cstate
, "illegal port number %d < 0", port
);
6810 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6811 b
= gen_port(cstate
, port
, real_proto
, dir
);
6812 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6816 if (proto
!= Q_DEFAULT
&&
6817 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6818 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6819 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6820 bpf_error(cstate
, "unknown port in range '%s'", name
);
6821 if (proto
== Q_UDP
) {
6822 if (real_proto
== IPPROTO_TCP
)
6823 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6824 else if (real_proto
== IPPROTO_SCTP
)
6825 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6827 /* override PROTO_UNDEF */
6828 real_proto
= IPPROTO_UDP
;
6830 if (proto
== Q_TCP
) {
6831 if (real_proto
== IPPROTO_UDP
)
6832 bpf_error(cstate
, "port in range '%s' is udp", name
);
6833 else if (real_proto
== IPPROTO_SCTP
)
6834 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6836 /* override PROTO_UNDEF */
6837 real_proto
= IPPROTO_TCP
;
6839 if (proto
== Q_SCTP
) {
6840 if (real_proto
== IPPROTO_UDP
)
6841 bpf_error(cstate
, "port in range '%s' is udp", name
);
6842 else if (real_proto
== IPPROTO_TCP
)
6843 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6845 /* override PROTO_UNDEF */
6846 real_proto
= IPPROTO_SCTP
;
6849 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6851 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6853 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6855 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6857 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6858 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6863 eaddr
= pcap_ether_hostton(name
);
6865 bpf_error(cstate
, "unknown ether host: %s", name
);
6867 res
= pcap_nametoaddrinfo(name
);
6870 bpf_error(cstate
, "unknown host '%s'", name
);
6871 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6875 bpf_error(cstate
, "unknown host '%s'", name
);
6878 bpf_error(cstate
, "'gateway' not supported in this configuration");
6882 real_proto
= lookup_proto(cstate
, name
, proto
);
6883 if (real_proto
>= 0)
6884 return gen_proto(cstate
, real_proto
, proto
, dir
);
6886 bpf_error(cstate
, "unknown protocol: %s", name
);
6889 real_proto
= lookup_proto(cstate
, name
, proto
);
6890 if (real_proto
>= 0)
6891 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6893 bpf_error(cstate
, "unknown protocol: %s", name
);
6904 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6905 unsigned int masklen
, struct qual q
)
6907 register int nlen
, mlen
;
6911 * Catch errors reported by us and routines below us, and return NULL
6914 if (setjmp(cstate
->top_ctx
))
6917 nlen
= __pcap_atoin(s1
, &n
);
6918 /* Promote short ipaddr */
6922 mlen
= __pcap_atoin(s2
, &m
);
6923 /* Promote short ipaddr */
6926 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6929 /* Convert mask len to mask */
6931 bpf_error(cstate
, "mask length must be <= 32");
6934 * X << 32 is not guaranteed by C to be 0; it's
6939 m
= 0xffffffff << (32 - masklen
);
6941 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6948 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6951 bpf_error(cstate
, "Mask syntax for networks only");
6958 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6961 int proto
= q
.proto
;
6966 * Catch errors reported by us and routines below us, and return NULL
6969 if (setjmp(cstate
->top_ctx
))
6974 else if (q
.proto
== Q_DECNET
) {
6975 vlen
= __pcap_atodn(s
, &v
);
6977 bpf_error(cstate
, "malformed decnet address '%s'", s
);
6979 vlen
= __pcap_atoin(s
, &v
);
6986 if (proto
== Q_DECNET
)
6987 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
6988 else if (proto
== Q_LINK
) {
6989 bpf_error(cstate
, "illegal link layer address");
6992 if (s
== NULL
&& q
.addr
== Q_NET
) {
6993 /* Promote short net number */
6994 while (v
&& (v
& 0xff000000) == 0) {
6999 /* Promote short ipaddr */
7001 mask
<<= 32 - vlen
;
7003 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7008 proto
= IPPROTO_UDP
;
7009 else if (proto
== Q_TCP
)
7010 proto
= IPPROTO_TCP
;
7011 else if (proto
== Q_SCTP
)
7012 proto
= IPPROTO_SCTP
;
7013 else if (proto
== Q_DEFAULT
)
7014 proto
= PROTO_UNDEF
;
7016 bpf_error(cstate
, "illegal qualifier of 'port'");
7019 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7023 b
= gen_port(cstate
, (int)v
, proto
, dir
);
7024 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
7030 proto
= IPPROTO_UDP
;
7031 else if (proto
== Q_TCP
)
7032 proto
= IPPROTO_TCP
;
7033 else if (proto
== Q_SCTP
)
7034 proto
= IPPROTO_SCTP
;
7035 else if (proto
== Q_DEFAULT
)
7036 proto
= PROTO_UNDEF
;
7038 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7041 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7045 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
7046 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
7051 bpf_error(cstate
, "'gateway' requires a name");
7055 return gen_proto(cstate
, (int)v
, proto
, dir
);
7058 return gen_protochain(cstate
, (int)v
, proto
, dir
);
7073 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7074 unsigned int masklen
, struct qual q
)
7076 struct addrinfo
*res
;
7077 struct in6_addr
*addr
;
7078 struct in6_addr mask
;
7083 * Catch errors reported by us and routines below us, and return NULL
7086 if (setjmp(cstate
->top_ctx
))
7090 bpf_error(cstate
, "no mask %s supported", s2
);
7092 res
= pcap_nametoaddrinfo(s1
);
7094 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7097 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7098 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7100 if (sizeof(mask
) * 8 < masklen
)
7101 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
7102 memset(&mask
, 0, sizeof(mask
));
7103 memset(&mask
, 0xff, masklen
/ 8);
7105 mask
.s6_addr
[masklen
/ 8] =
7106 (0xff << (8 - masklen
% 8)) & 0xff;
7109 a
= (uint32_t *)addr
;
7110 m
= (uint32_t *)&mask
;
7111 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7112 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7113 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7121 bpf_error(cstate
, "Mask syntax for networks only");
7125 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7131 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7138 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7140 struct block
*b
, *tmp
;
7143 * Catch errors reported by us and routines below us, and return NULL
7146 if (setjmp(cstate
->top_ctx
))
7149 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7150 cstate
->e
= pcap_ether_aton(s
);
7151 if (cstate
->e
== NULL
)
7152 bpf_error(cstate
, "malloc");
7153 switch (cstate
->linktype
) {
7155 case DLT_NETANALYZER
:
7156 case DLT_NETANALYZER_TRANSPARENT
:
7157 tmp
= gen_prevlinkhdr_check(cstate
);
7158 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7163 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7166 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7168 case DLT_IEEE802_11
:
7169 case DLT_PRISM_HEADER
:
7170 case DLT_IEEE802_11_RADIO_AVS
:
7171 case DLT_IEEE802_11_RADIO
:
7173 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7175 case DLT_IP_OVER_FC
:
7176 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7181 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7189 bpf_error(cstate
, "ethernet address used in non-ether expression");
7194 sappend(struct slist
*s0
, struct slist
*s1
)
7197 * This is definitely not the best way to do this, but the
7198 * lists will rarely get long.
7205 static struct slist
*
7206 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7210 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7215 static struct slist
*
7216 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7220 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7226 * Modify "index" to use the value stored into its register as an
7227 * offset relative to the beginning of the header for the protocol
7228 * "proto", and allocate a register and put an item "size" bytes long
7229 * (1, 2, or 4) at that offset into that register, making it the register
7232 static struct arth
*
7233 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7235 struct slist
*s
, *tmp
;
7237 int regno
= alloc_reg(cstate
);
7239 free_reg(cstate
, inst
->regno
);
7243 bpf_error(cstate
, "data size must be 1, 2, or 4");
7259 bpf_error(cstate
, "unsupported index operation");
7263 * The offset is relative to the beginning of the packet
7264 * data, if we have a radio header. (If we don't, this
7267 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7268 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7269 cstate
->linktype
!= DLT_PRISM_HEADER
)
7270 bpf_error(cstate
, "radio information not present in capture");
7273 * Load into the X register the offset computed into the
7274 * register specified by "index".
7276 s
= xfer_to_x(cstate
, inst
);
7279 * Load the item at that offset.
7281 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7283 sappend(inst
->s
, s
);
7288 * The offset is relative to the beginning of
7289 * the link-layer header.
7291 * XXX - what about ATM LANE? Should the index be
7292 * relative to the beginning of the AAL5 frame, so
7293 * that 0 refers to the beginning of the LE Control
7294 * field, or relative to the beginning of the LAN
7295 * frame, so that 0 refers, for Ethernet LANE, to
7296 * the beginning of the destination address?
7298 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7301 * If "s" is non-null, it has code to arrange that the
7302 * X register contains the length of the prefix preceding
7303 * the link-layer header. Add to it the offset computed
7304 * into the register specified by "index", and move that
7305 * into the X register. Otherwise, just load into the X
7306 * register the offset computed into the register specified
7310 sappend(s
, xfer_to_a(cstate
, inst
));
7311 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7312 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7314 s
= xfer_to_x(cstate
, inst
);
7317 * Load the item at the sum of the offset we've put in the
7318 * X register and the offset of the start of the link
7319 * layer header (which is 0 if the radio header is
7320 * variable-length; that header length is what we put
7321 * into the X register and then added to the index).
7323 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7324 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7326 sappend(inst
->s
, s
);
7340 * The offset is relative to the beginning of
7341 * the network-layer header.
7342 * XXX - are there any cases where we want
7343 * cstate->off_nl_nosnap?
7345 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7348 * If "s" is non-null, it has code to arrange that the
7349 * X register contains the variable part of the offset
7350 * of the link-layer payload. Add to it the offset
7351 * computed into the register specified by "index",
7352 * and move that into the X register. Otherwise, just
7353 * load into the X register the offset computed into
7354 * the register specified by "index".
7357 sappend(s
, xfer_to_a(cstate
, inst
));
7358 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7359 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7361 s
= xfer_to_x(cstate
, inst
);
7364 * Load the item at the sum of the offset we've put in the
7365 * X register, the offset of the start of the network
7366 * layer header from the beginning of the link-layer
7367 * payload, and the constant part of the offset of the
7368 * start of the link-layer payload.
7370 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7371 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7373 sappend(inst
->s
, s
);
7376 * Do the computation only if the packet contains
7377 * the protocol in question.
7379 b
= gen_proto_abbrev_internal(cstate
, proto
);
7381 gen_and(inst
->b
, b
);
7395 * The offset is relative to the beginning of
7396 * the transport-layer header.
7398 * Load the X register with the length of the IPv4 header
7399 * (plus the offset of the link-layer header, if it's
7400 * a variable-length header), in bytes.
7402 * XXX - are there any cases where we want
7403 * cstate->off_nl_nosnap?
7404 * XXX - we should, if we're built with
7405 * IPv6 support, generate code to load either
7406 * IPv4, IPv6, or both, as appropriate.
7408 s
= gen_loadx_iphdrlen(cstate
);
7411 * The X register now contains the sum of the variable
7412 * part of the offset of the link-layer payload and the
7413 * length of the network-layer header.
7415 * Load into the A register the offset relative to
7416 * the beginning of the transport layer header,
7417 * add the X register to that, move that to the
7418 * X register, and load with an offset from the
7419 * X register equal to the sum of the constant part of
7420 * the offset of the link-layer payload and the offset,
7421 * relative to the beginning of the link-layer payload,
7422 * of the network-layer header.
7424 sappend(s
, xfer_to_a(cstate
, inst
));
7425 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7426 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7427 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7428 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7429 sappend(inst
->s
, s
);
7432 * Do the computation only if the packet contains
7433 * the protocol in question - which is true only
7434 * if this is an IP datagram and is the first or
7435 * only fragment of that datagram.
7437 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7439 gen_and(inst
->b
, b
);
7440 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7445 * Do the computation only if the packet contains
7446 * the protocol in question.
7448 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7450 gen_and(inst
->b
, b
);
7455 * Check if we have an icmp6 next header
7457 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7459 gen_and(inst
->b
, b
);
7464 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7466 * If "s" is non-null, it has code to arrange that the
7467 * X register contains the variable part of the offset
7468 * of the link-layer payload. Add to it the offset
7469 * computed into the register specified by "index",
7470 * and move that into the X register. Otherwise, just
7471 * load into the X register the offset computed into
7472 * the register specified by "index".
7475 sappend(s
, xfer_to_a(cstate
, inst
));
7476 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7477 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7479 s
= xfer_to_x(cstate
, inst
);
7483 * Load the item at the sum of the offset we've put in the
7484 * X register, the offset of the start of the network
7485 * layer header from the beginning of the link-layer
7486 * payload, and the constant part of the offset of the
7487 * start of the link-layer payload.
7489 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7490 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7493 sappend(inst
->s
, s
);
7497 inst
->regno
= regno
;
7498 s
= new_stmt(cstate
, BPF_ST
);
7500 sappend(inst
->s
, s
);
7506 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7509 * Catch errors reported by us and routines below us, and return NULL
7512 if (setjmp(cstate
->top_ctx
))
7515 return gen_load_internal(cstate
, proto
, inst
, size
);
7518 static struct block
*
7519 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7520 struct arth
*a1
, int reversed
)
7522 struct slist
*s0
, *s1
, *s2
;
7523 struct block
*b
, *tmp
;
7525 s0
= xfer_to_x(cstate
, a1
);
7526 s1
= xfer_to_a(cstate
, a0
);
7527 if (code
== BPF_JEQ
) {
7528 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7529 b
= new_block(cstate
, JMP(code
));
7533 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7539 sappend(a0
->s
, a1
->s
);
7543 free_reg(cstate
, a0
->regno
);
7544 free_reg(cstate
, a1
->regno
);
7546 /* 'and' together protocol checks */
7549 gen_and(a0
->b
, tmp
= a1
->b
);
7563 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7564 struct arth
*a1
, int reversed
)
7567 * Catch errors reported by us and routines below us, and return NULL
7570 if (setjmp(cstate
->top_ctx
))
7573 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7577 gen_loadlen(compiler_state_t
*cstate
)
7584 * Catch errors reported by us and routines below us, and return NULL
7587 if (setjmp(cstate
->top_ctx
))
7590 regno
= alloc_reg(cstate
);
7591 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7592 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7593 s
->next
= new_stmt(cstate
, BPF_ST
);
7594 s
->next
->s
.k
= regno
;
7601 static struct arth
*
7602 gen_loadi_internal(compiler_state_t
*cstate
, int val
)
7608 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7610 reg
= alloc_reg(cstate
);
7612 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7614 s
->next
= new_stmt(cstate
, BPF_ST
);
7623 gen_loadi(compiler_state_t
*cstate
, int val
)
7626 * Catch errors reported by us and routines below us, and return NULL
7629 if (setjmp(cstate
->top_ctx
))
7632 return gen_loadi_internal(cstate
, val
);
7636 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7641 * Catch errors reported by us and routines below us, and return NULL
7644 if (setjmp(cstate
->top_ctx
))
7647 s
= xfer_to_a(cstate
, a
);
7649 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7652 s
= new_stmt(cstate
, BPF_ST
);
7660 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7663 struct slist
*s0
, *s1
, *s2
;
7666 * Catch errors reported by us and routines below us, and return NULL
7669 if (setjmp(cstate
->top_ctx
))
7673 * Disallow division by, or modulus by, zero; we do this here
7674 * so that it gets done even if the optimizer is disabled.
7676 * Also disallow shifts by a value greater than 31; we do this
7677 * here, for the same reason.
7679 if (code
== BPF_DIV
) {
7680 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7681 bpf_error(cstate
, "division by zero");
7682 } else if (code
== BPF_MOD
) {
7683 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7684 bpf_error(cstate
, "modulus by zero");
7685 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7687 * XXX - we need to make up our minds as to what integers
7688 * are signed and what integers are unsigned in BPF programs
7691 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) &&
7692 (a1
->s
->s
.k
< 0 || a1
->s
->s
.k
> 31))
7693 bpf_error(cstate
, "shift by more than 31 bits");
7695 s0
= xfer_to_x(cstate
, a1
);
7696 s1
= xfer_to_a(cstate
, a0
);
7697 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7702 sappend(a0
->s
, a1
->s
);
7704 free_reg(cstate
, a0
->regno
);
7705 free_reg(cstate
, a1
->regno
);
7707 s0
= new_stmt(cstate
, BPF_ST
);
7708 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7715 * Initialize the table of used registers and the current register.
7718 init_regs(compiler_state_t
*cstate
)
7721 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7725 * Return the next free register.
7728 alloc_reg(compiler_state_t
*cstate
)
7730 int n
= BPF_MEMWORDS
;
7733 if (cstate
->regused
[cstate
->curreg
])
7734 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7736 cstate
->regused
[cstate
->curreg
] = 1;
7737 return cstate
->curreg
;
7740 bpf_error(cstate
, "too many registers needed to evaluate expression");
7745 * Return a register to the table so it can
7749 free_reg(compiler_state_t
*cstate
, int n
)
7751 cstate
->regused
[n
] = 0;
7754 static struct block
*
7755 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7760 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7761 b
= new_block(cstate
, JMP(jmp
));
7769 gen_greater(compiler_state_t
*cstate
, int n
)
7772 * Catch errors reported by us and routines below us, and return NULL
7775 if (setjmp(cstate
->top_ctx
))
7778 return gen_len(cstate
, BPF_JGE
, n
);
7782 * Actually, this is less than or equal.
7785 gen_less(compiler_state_t
*cstate
, int n
)
7790 * Catch errors reported by us and routines below us, and return NULL
7793 if (setjmp(cstate
->top_ctx
))
7796 b
= gen_len(cstate
, BPF_JGT
, n
);
7803 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7804 * the beginning of the link-layer header.
7805 * XXX - that means you can't test values in the radiotap header, but
7806 * as that header is difficult if not impossible to parse generally
7807 * without a loop, that might not be a severe problem. A new keyword
7808 * "radio" could be added for that, although what you'd really want
7809 * would be a way of testing particular radio header values, which
7810 * would generate code appropriate to the radio header in question.
7813 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7819 * Catch errors reported by us and routines below us, and return NULL
7822 if (setjmp(cstate
->top_ctx
))
7830 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7833 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7837 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7841 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7845 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7849 b
= new_block(cstate
, JMP(BPF_JEQ
));
7856 static const u_char abroadcast
[] = { 0x0 };
7859 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7861 bpf_u_int32 hostmask
;
7862 struct block
*b0
, *b1
, *b2
;
7863 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7866 * Catch errors reported by us and routines below us, and return NULL
7869 if (setjmp(cstate
->top_ctx
))
7876 switch (cstate
->linktype
) {
7878 case DLT_ARCNET_LINUX
:
7879 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7881 case DLT_NETANALYZER
:
7882 case DLT_NETANALYZER_TRANSPARENT
:
7883 b1
= gen_prevlinkhdr_check(cstate
);
7884 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7889 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7891 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7892 case DLT_IEEE802_11
:
7893 case DLT_PRISM_HEADER
:
7894 case DLT_IEEE802_11_RADIO_AVS
:
7895 case DLT_IEEE802_11_RADIO
:
7897 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7898 case DLT_IP_OVER_FC
:
7899 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7901 bpf_error(cstate
, "not a broadcast link");
7907 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7908 * as an indication that we don't know the netmask, and fail
7911 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7912 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7913 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7914 hostmask
= ~cstate
->netmask
;
7915 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7916 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7917 (bpf_int32
)(~0 & hostmask
), hostmask
);
7922 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7927 * Generate code to test the low-order bit of a MAC address (that's
7928 * the bottom bit of the *first* byte).
7930 static struct block
*
7931 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7933 register struct block
*b0
;
7934 register struct slist
*s
;
7936 /* link[offset] & 1 != 0 */
7937 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7938 b0
= new_block(cstate
, JMP(BPF_JSET
));
7945 gen_multicast(compiler_state_t
*cstate
, int proto
)
7947 register struct block
*b0
, *b1
, *b2
;
7948 register struct slist
*s
;
7951 * Catch errors reported by us and routines below us, and return NULL
7954 if (setjmp(cstate
->top_ctx
))
7961 switch (cstate
->linktype
) {
7963 case DLT_ARCNET_LINUX
:
7964 /* all ARCnet multicasts use the same address */
7965 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7967 case DLT_NETANALYZER
:
7968 case DLT_NETANALYZER_TRANSPARENT
:
7969 b1
= gen_prevlinkhdr_check(cstate
);
7970 /* ether[0] & 1 != 0 */
7971 b0
= gen_mac_multicast(cstate
, 0);
7977 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7979 * XXX - was that referring to bit-order issues?
7981 /* fddi[1] & 1 != 0 */
7982 return gen_mac_multicast(cstate
, 1);
7984 /* tr[2] & 1 != 0 */
7985 return gen_mac_multicast(cstate
, 2);
7986 case DLT_IEEE802_11
:
7987 case DLT_PRISM_HEADER
:
7988 case DLT_IEEE802_11_RADIO_AVS
:
7989 case DLT_IEEE802_11_RADIO
:
7994 * For control frames, there is no DA.
7996 * For management frames, DA is at an
7997 * offset of 4 from the beginning of
8000 * For data frames, DA is at an offset
8001 * of 4 from the beginning of the packet
8002 * if To DS is clear and at an offset of
8003 * 16 from the beginning of the packet
8008 * Generate the tests to be done for data frames.
8010 * First, check for To DS set, i.e. "link[1] & 0x01".
8012 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8013 b1
= new_block(cstate
, JMP(BPF_JSET
));
8014 b1
->s
.k
= 0x01; /* To DS */
8018 * If To DS is set, the DA is at 16.
8020 b0
= gen_mac_multicast(cstate
, 16);
8024 * Now, check for To DS not set, i.e. check
8025 * "!(link[1] & 0x01)".
8027 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8028 b2
= new_block(cstate
, JMP(BPF_JSET
));
8029 b2
->s
.k
= 0x01; /* To DS */
8034 * If To DS is not set, the DA is at 4.
8036 b1
= gen_mac_multicast(cstate
, 4);
8040 * Now OR together the last two checks. That gives
8041 * the complete set of checks for data frames.
8046 * Now check for a data frame.
8047 * I.e, check "link[0] & 0x08".
8049 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8050 b1
= new_block(cstate
, JMP(BPF_JSET
));
8055 * AND that with the checks done for data frames.
8060 * If the high-order bit of the type value is 0, this
8061 * is a management frame.
8062 * I.e, check "!(link[0] & 0x08)".
8064 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8065 b2
= new_block(cstate
, JMP(BPF_JSET
));
8071 * For management frames, the DA is at 4.
8073 b1
= gen_mac_multicast(cstate
, 4);
8077 * OR that with the checks done for data frames.
8078 * That gives the checks done for management and
8084 * If the low-order bit of the type value is 1,
8085 * this is either a control frame or a frame
8086 * with a reserved type, and thus not a
8089 * I.e., check "!(link[0] & 0x04)".
8091 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8092 b1
= new_block(cstate
, JMP(BPF_JSET
));
8098 * AND that with the checks for data and management
8103 case DLT_IP_OVER_FC
:
8104 b0
= gen_mac_multicast(cstate
, 2);
8109 /* Link not known to support multicasts */
8113 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8114 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
8119 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8120 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
8124 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8129 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8130 * Outbound traffic is sent by this machine, while inbound traffic is
8131 * sent by a remote machine (and may include packets destined for a
8132 * unicast or multicast link-layer address we are not subscribing to).
8133 * These are the same definitions implemented by pcap_setdirection().
8134 * Capturing only unicast traffic destined for this host is probably
8135 * better accomplished using a higher-layer filter.
8138 gen_inbound(compiler_state_t
*cstate
, int dir
)
8140 register struct block
*b0
;
8143 * Catch errors reported by us and routines below us, and return NULL
8146 if (setjmp(cstate
->top_ctx
))
8150 * Only some data link types support inbound/outbound qualifiers.
8152 switch (cstate
->linktype
) {
8154 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8155 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8156 gen_loadi_internal(cstate
, 0),
8162 /* match outgoing packets */
8163 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8165 /* match incoming packets */
8166 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8171 /* match outgoing packets */
8172 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8174 /* to filter on inbound traffic, invert the match */
8179 case DLT_LINUX_SLL2
:
8180 /* match outgoing packets */
8181 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8183 /* to filter on inbound traffic, invert the match */
8188 #ifdef HAVE_NET_PFVAR_H
8190 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8191 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
8197 /* match outgoing packets */
8198 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8200 /* match incoming packets */
8201 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8205 case DLT_JUNIPER_MFR
:
8206 case DLT_JUNIPER_MLFR
:
8207 case DLT_JUNIPER_MLPPP
:
8208 case DLT_JUNIPER_ATM1
:
8209 case DLT_JUNIPER_ATM2
:
8210 case DLT_JUNIPER_PPPOE
:
8211 case DLT_JUNIPER_PPPOE_ATM
:
8212 case DLT_JUNIPER_GGSN
:
8213 case DLT_JUNIPER_ES
:
8214 case DLT_JUNIPER_MONITOR
:
8215 case DLT_JUNIPER_SERVICES
:
8216 case DLT_JUNIPER_ETHER
:
8217 case DLT_JUNIPER_PPP
:
8218 case DLT_JUNIPER_FRELAY
:
8219 case DLT_JUNIPER_CHDLC
:
8220 case DLT_JUNIPER_VP
:
8221 case DLT_JUNIPER_ST
:
8222 case DLT_JUNIPER_ISM
:
8223 case DLT_JUNIPER_VS
:
8224 case DLT_JUNIPER_SRX_E2E
:
8225 case DLT_JUNIPER_FIBRECHANNEL
:
8226 case DLT_JUNIPER_ATM_CEMIC
:
8228 /* juniper flags (including direction) are stored
8229 * the byte after the 3-byte magic number */
8231 /* match outgoing packets */
8232 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8234 /* match incoming packets */
8235 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8241 * If we have packet meta-data indicating a direction,
8242 * and that metadata can be checked by BPF code, check
8243 * it. Otherwise, give up, as this link-layer type has
8244 * nothing in the packet data.
8246 * Currently, the only platform where a BPF filter can
8247 * check that metadata is Linux with the in-kernel
8248 * BPF interpreter. If other packet capture mechanisms
8249 * and BPF filters also supported this, it would be
8250 * nice. It would be even better if they made that
8251 * metadata available so that we could provide it
8252 * with newer capture APIs, allowing it to be saved
8255 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
8257 * This is Linux with PF_PACKET support.
8258 * If this is a *live* capture, we can look at
8259 * special meta-data in the filter expression;
8260 * if it's a savefile, we can't.
8262 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8263 /* We have a FILE *, so this is a savefile */
8264 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
8269 /* match outgoing packets */
8270 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8273 /* to filter on inbound traffic, invert the match */
8276 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8277 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
8280 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8285 #ifdef HAVE_NET_PFVAR_H
8286 /* PF firewall log matched interface */
8288 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8294 * Catch errors reported by us and routines below us, and return NULL
8297 if (setjmp(cstate
->top_ctx
))
8300 if (cstate
->linktype
!= DLT_PFLOG
) {
8301 bpf_error(cstate
, "ifname supported only on PF linktype");
8304 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8305 off
= offsetof(struct pfloghdr
, ifname
);
8306 if (strlen(ifname
) >= len
) {
8307 bpf_error(cstate
, "ifname interface names can only be %d characters",
8311 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
8315 /* PF firewall log ruleset name */
8317 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8322 * Catch errors reported by us and routines below us, and return NULL
8325 if (setjmp(cstate
->top_ctx
))
8328 if (cstate
->linktype
!= DLT_PFLOG
) {
8329 bpf_error(cstate
, "ruleset supported only on PF linktype");
8333 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8334 bpf_error(cstate
, "ruleset names can only be %ld characters",
8335 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8339 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8340 strlen(ruleset
), (const u_char
*)ruleset
);
8344 /* PF firewall log rule number */
8346 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8351 * Catch errors reported by us and routines below us, and return NULL
8354 if (setjmp(cstate
->top_ctx
))
8357 if (cstate
->linktype
!= DLT_PFLOG
) {
8358 bpf_error(cstate
, "rnr supported only on PF linktype");
8362 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8367 /* PF firewall log sub-rule number */
8369 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8374 * Catch errors reported by us and routines below us, and return NULL
8377 if (setjmp(cstate
->top_ctx
))
8380 if (cstate
->linktype
!= DLT_PFLOG
) {
8381 bpf_error(cstate
, "srnr supported only on PF linktype");
8385 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8390 /* PF firewall log reason code */
8392 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8397 * Catch errors reported by us and routines below us, and return NULL
8400 if (setjmp(cstate
->top_ctx
))
8403 if (cstate
->linktype
!= DLT_PFLOG
) {
8404 bpf_error(cstate
, "reason supported only on PF linktype");
8408 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8413 /* PF firewall log action */
8415 gen_pf_action(compiler_state_t
*cstate
, int action
)
8420 * Catch errors reported by us and routines below us, and return NULL
8423 if (setjmp(cstate
->top_ctx
))
8426 if (cstate
->linktype
!= DLT_PFLOG
) {
8427 bpf_error(cstate
, "action supported only on PF linktype");
8431 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8435 #else /* !HAVE_NET_PFVAR_H */
8437 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8440 * Catch errors reported by us and routines below us, and return NULL
8443 if (setjmp(cstate
->top_ctx
))
8446 bpf_error(cstate
, "libpcap was compiled without pf support");
8451 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8454 * Catch errors reported by us and routines below us, and return NULL
8457 if (setjmp(cstate
->top_ctx
))
8460 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8465 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8468 * Catch errors reported by us and routines below us, and return NULL
8471 if (setjmp(cstate
->top_ctx
))
8474 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8479 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8482 * Catch errors reported by us and routines below us, and return NULL
8485 if (setjmp(cstate
->top_ctx
))
8488 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8493 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8496 * Catch errors reported by us and routines below us, and return NULL
8499 if (setjmp(cstate
->top_ctx
))
8502 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8507 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8510 * Catch errors reported by us and routines below us, and return NULL
8513 if (setjmp(cstate
->top_ctx
))
8516 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8519 #endif /* HAVE_NET_PFVAR_H */
8521 /* IEEE 802.11 wireless header */
8523 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
8528 * Catch errors reported by us and routines below us, and return NULL
8531 if (setjmp(cstate
->top_ctx
))
8534 switch (cstate
->linktype
) {
8536 case DLT_IEEE802_11
:
8537 case DLT_PRISM_HEADER
:
8538 case DLT_IEEE802_11_RADIO_AVS
:
8539 case DLT_IEEE802_11_RADIO
:
8540 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8545 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8553 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8558 * Catch errors reported by us and routines below us, and return NULL
8561 if (setjmp(cstate
->top_ctx
))
8564 switch (cstate
->linktype
) {
8566 case DLT_IEEE802_11
:
8567 case DLT_PRISM_HEADER
:
8568 case DLT_IEEE802_11_RADIO_AVS
:
8569 case DLT_IEEE802_11_RADIO
:
8573 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8577 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8578 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8584 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8589 * Catch errors reported by us and routines below us, and return NULL
8592 if (setjmp(cstate
->top_ctx
))
8595 switch (cstate
->linktype
) {
8598 case DLT_ARCNET_LINUX
:
8599 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8600 q
.proto
== Q_LINK
) {
8601 cstate
->e
= pcap_ether_aton(s
);
8602 if (cstate
->e
== NULL
)
8603 bpf_error(cstate
, "malloc");
8604 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8609 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8615 bpf_error(cstate
, "aid supported only on ARCnet");
8620 static struct block
*
8621 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8623 register struct block
*b0
, *b1
;
8626 /* src comes first, different from Ethernet */
8628 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8631 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8634 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8635 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8641 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8642 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8647 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8651 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8655 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8659 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8663 bpf_error(cstate
, "'ra' is only supported on 802.11");
8667 bpf_error(cstate
, "'ta' is only supported on 802.11");
8674 static struct block
*
8675 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8677 struct block
*b0
, *b1
;
8679 /* check for VLAN, including QinQ */
8680 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8681 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8684 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8690 static struct block
*
8691 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8693 if (vlan_num
> 0x0fff) {
8694 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8697 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8700 static struct block
*
8701 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8704 struct block
*b0
, *b1
;
8706 b0
= gen_vlan_tpid_test(cstate
);
8709 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8715 * Both payload and link header type follow the VLAN tags so that
8716 * both need to be updated.
8718 cstate
->off_linkpl
.constant_part
+= 4;
8719 cstate
->off_linktype
.constant_part
+= 4;
8724 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8725 /* add v to variable part of off */
8727 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8731 if (!off
->is_variable
)
8732 off
->is_variable
= 1;
8734 off
->reg
= alloc_reg(cstate
);
8736 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8739 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8742 s2
= new_stmt(cstate
, BPF_ST
);
8748 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8749 * and link type offsets first
8752 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8756 /* offset determined at run time, shift variable part */
8758 cstate
->is_vlan_vloffset
= 1;
8759 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8760 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8762 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8763 sappend(s
.next
, b_tpid
->head
->stmts
);
8764 b_tpid
->head
->stmts
= s
.next
;
8768 * patch block b_vid (VLAN id test) to load VID value either from packet
8769 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8772 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8774 struct slist
*s
, *s2
, *sjeq
;
8777 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8778 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8780 /* true -> next instructions, false -> beginning of b_vid */
8781 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8783 sjeq
->s
.jf
= b_vid
->stmts
;
8786 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8787 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8791 /* Jump to the test in b_vid. We need to jump one instruction before
8792 * the end of the b_vid block so that we only skip loading the TCI
8793 * from packet data and not the 'and' instruction extractging VID.
8796 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8798 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8802 /* insert our statements at the beginning of b_vid */
8803 sappend(s
, b_vid
->stmts
);
8808 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8809 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8810 * tag can be either in metadata or in packet data; therefore if the
8811 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8812 * header for VLAN tag. As the decision is done at run time, we need
8813 * update variable part of the offsets
8815 static struct block
*
8816 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8819 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8822 /* generate new filter code based on extracting packet
8824 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8825 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8827 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8832 * This is tricky. We need to insert the statements updating variable
8833 * parts of offsets before the the traditional TPID and VID tests so
8834 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8835 * we do not want this update to affect those checks. That's why we
8836 * generate both test blocks first and insert the statements updating
8837 * variable parts of both offsets after that. This wouldn't work if
8838 * there already were variable length link header when entering this
8839 * function but gen_vlan_bpf_extensions() isn't called in that case.
8841 b_tpid
= gen_vlan_tpid_test(cstate
);
8843 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8845 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8850 gen_vlan_patch_vid_test(cstate
, b_vid
);
8860 * support IEEE 802.1Q VLAN trunk over ethernet
8863 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8868 * Catch errors reported by us and routines below us, and return NULL
8871 if (setjmp(cstate
->top_ctx
))
8874 /* can't check for VLAN-encapsulated packets inside MPLS */
8875 if (cstate
->label_stack_depth
> 0)
8876 bpf_error(cstate
, "no VLAN match after MPLS");
8879 * Check for a VLAN packet, and then change the offsets to point
8880 * to the type and data fields within the VLAN packet. Just
8881 * increment the offsets, so that we can support a hierarchy, e.g.
8882 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8885 * XXX - this is a bit of a kludge. If we were to split the
8886 * compiler into a parser that parses an expression and
8887 * generates an expression tree, and a code generator that
8888 * takes an expression tree (which could come from our
8889 * parser or from some other parser) and generates BPF code,
8890 * we could perhaps make the offsets parameters of routines
8891 * and, in the handler for an "AND" node, pass to subnodes
8892 * other than the VLAN node the adjusted offsets.
8894 * This would mean that "vlan" would, instead of changing the
8895 * behavior of *all* tests after it, change only the behavior
8896 * of tests ANDed with it. That would change the documented
8897 * semantics of "vlan", which might break some expressions.
8898 * However, it would mean that "(vlan and ip) or ip" would check
8899 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8900 * checking only for VLAN-encapsulated IP, so that could still
8901 * be considered worth doing; it wouldn't break expressions
8902 * that are of the form "vlan and ..." or "vlan N and ...",
8903 * which I suspect are the most common expressions involving
8904 * "vlan". "vlan or ..." doesn't necessarily do what the user
8905 * would really want, now, as all the "or ..." tests would
8906 * be done assuming a VLAN, even though the "or" could be viewed
8907 * as meaning "or, if this isn't a VLAN packet...".
8909 switch (cstate
->linktype
) {
8912 case DLT_NETANALYZER
:
8913 case DLT_NETANALYZER_TRANSPARENT
:
8914 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8915 /* Verify that this is the outer part of the packet and
8916 * not encapsulated somehow. */
8917 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8918 cstate
->off_linkhdr
.constant_part
==
8919 cstate
->off_outermostlinkhdr
.constant_part
) {
8921 * Do we need special VLAN handling?
8923 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8924 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8927 b0
= gen_vlan_no_bpf_extensions(cstate
,
8928 vlan_num
, has_vlan_tag
);
8931 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8935 case DLT_IEEE802_11
:
8936 case DLT_PRISM_HEADER
:
8937 case DLT_IEEE802_11_RADIO_AVS
:
8938 case DLT_IEEE802_11_RADIO
:
8939 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8943 bpf_error(cstate
, "no VLAN support for data link type %d",
8948 cstate
->vlan_stack_depth
++;
8957 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
8959 struct block
*b0
, *b1
;
8962 * Catch errors reported by us and routines below us, and return NULL
8965 if (setjmp(cstate
->top_ctx
))
8968 if (cstate
->label_stack_depth
> 0) {
8969 /* just match the bottom-of-stack bit clear */
8970 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8973 * We're not in an MPLS stack yet, so check the link-layer
8974 * type against MPLS.
8976 switch (cstate
->linktype
) {
8978 case DLT_C_HDLC
: /* fall through */
8980 case DLT_NETANALYZER
:
8981 case DLT_NETANALYZER_TRANSPARENT
:
8982 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
8986 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
8989 /* FIXME add other DLT_s ...
8990 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8991 * leave it for now */
8994 bpf_error(cstate
, "no MPLS support for data link type %d",
9001 /* If a specific MPLS label is requested, check it */
9002 if (has_label_num
) {
9003 if (label_num
> 0xFFFFF) {
9004 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9005 label_num
, 0xFFFFF);
9007 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9008 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
9009 0xfffff000); /* only compare the first 20 bits */
9015 * Change the offsets to point to the type and data fields within
9016 * the MPLS packet. Just increment the offsets, so that we
9017 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9018 * capture packets with an outer label of 100000 and an inner
9021 * Increment the MPLS stack depth as well; this indicates that
9022 * we're checking MPLS-encapsulated headers, to make sure higher
9023 * level code generators don't try to match against IP-related
9024 * protocols such as Q_ARP, Q_RARP etc.
9026 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9028 cstate
->off_nl_nosnap
+= 4;
9029 cstate
->off_nl
+= 4;
9030 cstate
->label_stack_depth
++;
9035 * Support PPPOE discovery and session.
9038 gen_pppoed(compiler_state_t
*cstate
)
9041 * Catch errors reported by us and routines below us, and return NULL
9044 if (setjmp(cstate
->top_ctx
))
9047 /* check for PPPoE discovery */
9048 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
9052 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9054 struct block
*b0
, *b1
;
9057 * Catch errors reported by us and routines below us, and return NULL
9060 if (setjmp(cstate
->top_ctx
))
9064 * Test against the PPPoE session link-layer type.
9066 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
9068 /* If a specific session is requested, check PPPoE session id */
9070 if (sess_num
> 0x0000ffff) {
9071 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9072 sess_num
, 0x0000ffff);
9074 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
9075 (bpf_int32
)sess_num
, 0x0000ffff);
9081 * Change the offsets to point to the type and data fields within
9082 * the PPP packet, and note that this is PPPoE rather than
9085 * XXX - this is a bit of a kludge. If we were to split the
9086 * compiler into a parser that parses an expression and
9087 * generates an expression tree, and a code generator that
9088 * takes an expression tree (which could come from our
9089 * parser or from some other parser) and generates BPF code,
9090 * we could perhaps make the offsets parameters of routines
9091 * and, in the handler for an "AND" node, pass to subnodes
9092 * other than the PPPoE node the adjusted offsets.
9094 * This would mean that "pppoes" would, instead of changing the
9095 * behavior of *all* tests after it, change only the behavior
9096 * of tests ANDed with it. That would change the documented
9097 * semantics of "pppoes", which might break some expressions.
9098 * However, it would mean that "(pppoes and ip) or ip" would check
9099 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9100 * checking only for VLAN-encapsulated IP, so that could still
9101 * be considered worth doing; it wouldn't break expressions
9102 * that are of the form "pppoes and ..." which I suspect are the
9103 * most common expressions involving "pppoes". "pppoes or ..."
9104 * doesn't necessarily do what the user would really want, now,
9105 * as all the "or ..." tests would be done assuming PPPoE, even
9106 * though the "or" could be viewed as meaning "or, if this isn't
9107 * a PPPoE packet...".
9109 * The "network-layer" protocol is PPPoE, which has a 6-byte
9110 * PPPoE header, followed by a PPP packet.
9112 * There is no HDLC encapsulation for the PPP packet (it's
9113 * encapsulated in PPPoES instead), so the link-layer type
9114 * starts at the first byte of the PPP packet. For PPPoE,
9115 * that offset is relative to the beginning of the total
9116 * link-layer payload, including any 802.2 LLC header, so
9117 * it's 6 bytes past cstate->off_nl.
9119 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9120 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9121 cstate
->off_linkpl
.reg
);
9123 cstate
->off_linktype
= cstate
->off_linkhdr
;
9124 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9127 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9132 /* Check that this is Geneve and the VNI is correct if
9133 * specified. Parameterized to handle both IPv4 and IPv6. */
9134 static struct block
*
9135 gen_geneve_check(compiler_state_t
*cstate
,
9136 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
9137 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9139 struct block
*b0
, *b1
;
9141 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9143 /* Check that we are operating on version 0. Otherwise, we
9144 * can't decode the rest of the fields. The version is 2 bits
9145 * in the first byte of the Geneve header. */
9146 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
9151 if (vni
> 0xffffff) {
9152 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9155 vni
<<= 8; /* VNI is in the upper 3 bytes */
9156 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
9165 /* The IPv4 and IPv6 Geneve checks need to do two things:
9166 * - Verify that this actually is Geneve with the right VNI.
9167 * - Place the IP header length (plus variable link prefix if
9168 * needed) into register A to be used later to compute
9169 * the inner packet offsets. */
9170 static struct block
*
9171 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9173 struct block
*b0
, *b1
;
9174 struct slist
*s
, *s1
;
9176 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9178 /* Load the IP header length into A. */
9179 s
= gen_loadx_iphdrlen(cstate
);
9181 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9184 /* Forcibly append these statements to the true condition
9185 * of the protocol check by creating a new block that is
9186 * always true and ANDing them. */
9187 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9196 static struct block
*
9197 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9199 struct block
*b0
, *b1
;
9200 struct slist
*s
, *s1
;
9202 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9204 /* Load the IP header length. We need to account for a
9205 * variable length link prefix if there is one. */
9206 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9208 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9212 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9216 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9220 /* Forcibly append these statements to the true condition
9221 * of the protocol check by creating a new block that is
9222 * always true and ANDing them. */
9223 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9226 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9235 /* We need to store three values based on the Geneve header::
9236 * - The offset of the linktype.
9237 * - The offset of the end of the Geneve header.
9238 * - The offset of the end of the encapsulated MAC header. */
9239 static struct slist
*
9240 gen_geneve_offsets(compiler_state_t
*cstate
)
9242 struct slist
*s
, *s1
, *s_proto
;
9244 /* First we need to calculate the offset of the Geneve header
9245 * itself. This is composed of the IP header previously calculated
9246 * (include any variable link prefix) and stored in A plus the
9247 * fixed sized headers (fixed link prefix, MAC length, and UDP
9249 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9250 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9252 /* Stash this in X since we'll need it later. */
9253 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9256 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9258 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9262 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9263 cstate
->off_linktype
.is_variable
= 1;
9264 cstate
->off_linktype
.constant_part
= 0;
9266 s1
= new_stmt(cstate
, BPF_ST
);
9267 s1
->s
.k
= cstate
->off_linktype
.reg
;
9270 /* Load the Geneve option length and mask and shift to get the
9271 * number of bytes. It is stored in the first byte of the Geneve
9273 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9277 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9281 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9285 /* Add in the rest of the Geneve base header. */
9286 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9290 /* Add the Geneve header length to its offset and store. */
9291 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9295 /* Set the encapsulated type as Ethernet. Even though we may
9296 * not actually have Ethernet inside there are two reasons this
9298 * - The linktype field is always in EtherType format regardless
9299 * of whether it is in Geneve or an inner Ethernet frame.
9300 * - The only link layer that we have specific support for is
9301 * Ethernet. We will confirm that the packet actually is
9302 * Ethernet at runtime before executing these checks. */
9303 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9305 s1
= new_stmt(cstate
, BPF_ST
);
9306 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9309 /* Calculate whether we have an Ethernet header or just raw IP/
9310 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9311 * and linktype by 14 bytes so that the network header can be found
9312 * seamlessly. Otherwise, keep what we've calculated already. */
9314 /* We have a bare jmp so we can't use the optimizer. */
9315 cstate
->no_optimize
= 1;
9317 /* Load the EtherType in the Geneve header, 2 bytes in. */
9318 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9322 /* Load X with the end of the Geneve header. */
9323 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9324 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9327 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9328 * end of this check, we should have the total length in X. In
9329 * the non-Ethernet case, it's already there. */
9330 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9331 s_proto
->s
.k
= ETHERTYPE_TEB
;
9332 sappend(s
, s_proto
);
9334 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9338 /* Since this is Ethernet, use the EtherType of the payload
9339 * directly as the linktype. Overwrite what we already have. */
9340 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9344 s1
= new_stmt(cstate
, BPF_ST
);
9345 s1
->s
.k
= cstate
->off_linktype
.reg
;
9348 /* Advance two bytes further to get the end of the Ethernet
9350 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9354 /* Move the result to X. */
9355 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9358 /* Store the final result of our linkpl calculation. */
9359 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9360 cstate
->off_linkpl
.is_variable
= 1;
9361 cstate
->off_linkpl
.constant_part
= 0;
9363 s1
= new_stmt(cstate
, BPF_STX
);
9364 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9373 /* Check to see if this is a Geneve packet. */
9375 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9377 struct block
*b0
, *b1
;
9381 * Catch errors reported by us and routines below us, and return NULL
9384 if (setjmp(cstate
->top_ctx
))
9387 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9388 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9393 /* Later filters should act on the payload of the Geneve frame,
9394 * update all of the header pointers. Attach this code so that
9395 * it gets executed in the event that the Geneve filter matches. */
9396 s
= gen_geneve_offsets(cstate
);
9398 b1
= gen_true(cstate
);
9399 sappend(s
, b1
->stmts
);
9404 cstate
->is_geneve
= 1;
9409 /* Check that the encapsulated frame has a link layer header
9410 * for Ethernet filters. */
9411 static struct block
*
9412 gen_geneve_ll_check(compiler_state_t
*cstate
)
9415 struct slist
*s
, *s1
;
9417 /* The easiest way to see if there is a link layer present
9418 * is to check if the link layer header and payload are not
9421 /* Geneve always generates pure variable offsets so we can
9422 * compare only the registers. */
9423 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9424 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9426 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9427 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9430 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9438 static struct block
*
9439 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9440 bpf_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9447 if (!cstate
->is_atm
)
9448 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9449 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9451 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
9456 if (!cstate
->is_atm
)
9457 bpf_error(cstate
, "'vci' supported only on raw ATM");
9458 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9460 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
9465 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9466 abort(); /* XXX - this isn't on FreeBSD */
9467 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
9472 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9474 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9475 0xffffffff, jtype
, reverse
, jvalue
);
9479 if (!cstate
->is_atm
)
9480 bpf_error(cstate
, "'callref' supported only on raw ATM");
9481 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9483 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
9484 jtype
, reverse
, jvalue
);
9493 static struct block
*
9494 gen_atmtype_metac(compiler_state_t
*cstate
)
9496 struct block
*b0
, *b1
;
9498 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9499 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9504 static struct block
*
9505 gen_atmtype_sc(compiler_state_t
*cstate
)
9507 struct block
*b0
, *b1
;
9509 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9510 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9515 static struct block
*
9516 gen_atmtype_llc(compiler_state_t
*cstate
)
9520 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9521 cstate
->linktype
= cstate
->prevlinktype
;
9526 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9527 bpf_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9530 * Catch errors reported by us and routines below us, and return NULL
9533 if (setjmp(cstate
->top_ctx
))
9536 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9541 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9543 struct block
*b0
, *b1
;
9546 * Catch errors reported by us and routines below us, and return NULL
9549 if (setjmp(cstate
->top_ctx
))
9555 /* Get all packets in Meta signalling Circuit */
9556 if (!cstate
->is_atm
)
9557 bpf_error(cstate
, "'metac' supported only on raw ATM");
9558 b1
= gen_atmtype_metac(cstate
);
9562 /* Get all packets in Broadcast Circuit*/
9563 if (!cstate
->is_atm
)
9564 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9565 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9566 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9571 /* Get all cells in Segment OAM F4 circuit*/
9572 if (!cstate
->is_atm
)
9573 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9574 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9575 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9580 /* Get all cells in End-to-End OAM F4 Circuit*/
9581 if (!cstate
->is_atm
)
9582 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9583 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9584 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9589 /* Get all packets in connection Signalling Circuit */
9590 if (!cstate
->is_atm
)
9591 bpf_error(cstate
, "'sc' supported only on raw ATM");
9592 b1
= gen_atmtype_sc(cstate
);
9596 /* Get all packets in ILMI Circuit */
9597 if (!cstate
->is_atm
)
9598 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9599 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9600 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9605 /* Get all LANE packets */
9606 if (!cstate
->is_atm
)
9607 bpf_error(cstate
, "'lane' supported only on raw ATM");
9608 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9611 * Arrange that all subsequent tests assume LANE
9612 * rather than LLC-encapsulated packets, and set
9613 * the offsets appropriately for LANE-encapsulated
9616 * We assume LANE means Ethernet, not Token Ring.
9618 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9619 cstate
->off_payload
+ 2, /* Ethernet header */
9621 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9622 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9623 cstate
->off_nl
= 0; /* Ethernet II */
9624 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9628 /* Get all LLC-encapsulated packets */
9629 if (!cstate
->is_atm
)
9630 bpf_error(cstate
, "'llc' supported only on raw ATM");
9631 b1
= gen_atmtype_llc(cstate
);
9641 * Filtering for MTP2 messages based on li value
9642 * FISU, length is null
9643 * LSSU, length is 1 or 2
9644 * MSU, length is 3 or more
9645 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9648 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9650 struct block
*b0
, *b1
;
9653 * Catch errors reported by us and routines below us, and return NULL
9656 if (setjmp(cstate
->top_ctx
))
9662 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9663 (cstate
->linktype
!= DLT_ERF
) &&
9664 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9665 bpf_error(cstate
, "'fisu' supported only on MTP2");
9666 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9667 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
9671 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9672 (cstate
->linktype
!= DLT_ERF
) &&
9673 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9674 bpf_error(cstate
, "'lssu' supported only on MTP2");
9675 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
9676 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9681 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9682 (cstate
->linktype
!= DLT_ERF
) &&
9683 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9684 bpf_error(cstate
, "'msu' supported only on MTP2");
9685 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9689 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9690 (cstate
->linktype
!= DLT_ERF
) &&
9691 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9692 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9693 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9694 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9698 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9699 (cstate
->linktype
!= DLT_ERF
) &&
9700 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9701 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9702 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9703 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9708 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9709 (cstate
->linktype
!= DLT_ERF
) &&
9710 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9711 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9712 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9722 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
9723 bpf_u_int32 jtype
, int reverse
)
9726 bpf_u_int32 val1
, val2
, val3
;
9733 * Catch errors reported by us and routines below us, and return NULL
9736 if (setjmp(cstate
->top_ctx
))
9739 newoff_sio
= cstate
->off_sio
;
9740 newoff_opc
= cstate
->off_opc
;
9741 newoff_dpc
= cstate
->off_dpc
;
9742 newoff_sls
= cstate
->off_sls
;
9743 switch (mtp3field
) {
9746 newoff_sio
+= 3; /* offset for MTP2_HSL */
9750 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9751 bpf_error(cstate
, "'sio' supported only on SS7");
9752 /* sio coded on 1 byte so max value 255 */
9754 bpf_error(cstate
, "sio value %u too big; max value = 255",
9756 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9757 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9763 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9764 bpf_error(cstate
, "'opc' supported only on SS7");
9765 /* opc coded on 14 bits so max value 16383 */
9767 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9769 /* the following instructions are made to convert jvalue
9770 * to the form used to write opc in an ss7 message*/
9771 val1
= jvalue
& 0x00003c00;
9773 val2
= jvalue
& 0x000003fc;
9775 val3
= jvalue
& 0x00000003;
9777 jvalue
= val1
+ val2
+ val3
;
9778 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9779 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9787 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9788 bpf_error(cstate
, "'dpc' supported only on SS7");
9789 /* dpc coded on 14 bits so max value 16383 */
9791 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9793 /* the following instructions are made to convert jvalue
9794 * to the forme used to write dpc in an ss7 message*/
9795 val1
= jvalue
& 0x000000ff;
9797 val2
= jvalue
& 0x00003f00;
9799 jvalue
= val1
+ val2
;
9800 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9801 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9807 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9808 bpf_error(cstate
, "'sls' supported only on SS7");
9809 /* sls coded on 4 bits so max value 15 */
9811 bpf_error(cstate
, "sls value %u too big; max value = 15",
9813 /* the following instruction is made to convert jvalue
9814 * to the forme used to write sls in an ss7 message*/
9815 jvalue
= jvalue
<< 4;
9816 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9817 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9826 static struct block
*
9827 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9832 * Q.2931 signalling protocol messages for handling virtual circuits
9833 * establishment and teardown
9838 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9842 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9846 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9850 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9854 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9857 case A_RELEASE_DONE
:
9858 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9868 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9870 struct block
*b0
, *b1
;
9873 * Catch errors reported by us and routines below us, and return NULL
9876 if (setjmp(cstate
->top_ctx
))
9882 if (!cstate
->is_atm
)
9883 bpf_error(cstate
, "'oam' supported only on raw ATM");
9885 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9886 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9888 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9893 if (!cstate
->is_atm
)
9894 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9896 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9897 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9899 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9905 * Get Q.2931 signalling messages for switched
9906 * virtual connection
9908 if (!cstate
->is_atm
)
9909 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9910 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9911 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9913 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9915 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9917 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9919 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9921 b0
= gen_atmtype_sc(cstate
);
9926 if (!cstate
->is_atm
)
9927 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9928 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9929 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9931 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9933 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9935 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9937 b0
= gen_atmtype_metac(cstate
);