1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <pcap-types.h>
31 #include <sys/socket.h>
34 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
55 #include "ethertype.h"
59 #include "ieee80211.h"
61 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
70 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
96 uint16_t u6_addr16
[8];
97 uint32_t u6_addr32
[4];
99 #define s6_addr in6_u.u6_addr8
100 #define s6_addr16 in6_u.u6_addr16
101 #define s6_addr32 in6_u.u6_addr32
102 #define s6_addr64 in6_u.u6_addr64
105 typedef unsigned short sa_family_t
;
107 #define __SOCKADDR_COMMON(sa_prefix) \
108 sa_family_t sa_prefix##family
110 /* Ditto, for IPv6. */
113 __SOCKADDR_COMMON (sin6_
);
114 uint16_t sin6_port
; /* Transport layer port # */
115 uint32_t sin6_flowinfo
; /* IPv6 flow information */
116 struct in6_addr sin6_addr
; /* IPv6 address */
119 #ifndef EAI_ADDRFAMILY
121 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
122 int ai_family
; /* PF_xxx */
123 int ai_socktype
; /* SOCK_xxx */
124 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
125 size_t ai_addrlen
; /* length of ai_addr */
126 char *ai_canonname
; /* canonical name for hostname */
127 struct sockaddr
*ai_addr
; /* binary address */
128 struct addrinfo
*ai_next
; /* next structure in linked list */
130 #endif /* EAI_ADDRFAMILY */
131 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
134 #include <netdb.h> /* for "struct addrinfo" */
136 #include <pcap/namedb.h>
138 #include "nametoaddr.h"
140 #define ETHERMTU 1500
142 #ifndef ETHERTYPE_TEB
143 #define ETHERTYPE_TEB 0x6558
146 #ifndef IPPROTO_HOPOPTS
147 #define IPPROTO_HOPOPTS 0
149 #ifndef IPPROTO_ROUTING
150 #define IPPROTO_ROUTING 43
152 #ifndef IPPROTO_FRAGMENT
153 #define IPPROTO_FRAGMENT 44
155 #ifndef IPPROTO_DSTOPTS
156 #define IPPROTO_DSTOPTS 60
159 #define IPPROTO_SCTP 132
162 #define GENEVE_PORT 6081
164 #ifdef HAVE_OS_PROTO_H
165 #include "os-proto.h"
168 #define JMP(c) ((c)|BPF_JMP|BPF_K)
171 * "Push" the current value of the link-layer header type and link-layer
172 * header offset onto a "stack", and set a new value. (It's not a
173 * full-blown stack; we keep only the top two items.)
175 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
177 (cs)->prevlinktype = (cs)->linktype; \
178 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
179 (cs)->linktype = (new_linktype); \
180 (cs)->off_linkhdr.is_variable = (new_is_variable); \
181 (cs)->off_linkhdr.constant_part = (new_constant_part); \
182 (cs)->off_linkhdr.reg = (new_reg); \
183 (cs)->is_geneve = 0; \
187 * Offset "not set" value.
189 #define OFFSET_NOT_SET 0xffffffffU
192 * Absolute offsets, which are offsets from the beginning of the raw
193 * packet data, are, in the general case, the sum of a variable value
194 * and a constant value; the variable value may be absent, in which
195 * case the offset is only the constant value, and the constant value
196 * may be zero, in which case the offset is only the variable value.
198 * bpf_abs_offset is a structure containing all that information:
200 * is_variable is 1 if there's a variable part.
202 * constant_part is the constant part of the value, possibly zero;
204 * if is_variable is 1, reg is the register number for a register
205 * containing the variable value if the register has been assigned,
215 * Value passed to gen_load_a() to indicate what the offset argument
216 * is relative to the beginning of.
219 OR_PACKET
, /* full packet data */
220 OR_LINKHDR
, /* link-layer header */
221 OR_PREVLINKHDR
, /* previous link-layer header */
222 OR_LLC
, /* 802.2 LLC header */
223 OR_PREVMPLSHDR
, /* previous MPLS header */
224 OR_LINKTYPE
, /* link-layer type */
225 OR_LINKPL
, /* link-layer payload */
226 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
227 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
228 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
232 * We divy out chunks of memory rather than call malloc each time so
233 * we don't have to worry about leaking memory. It's probably
234 * not a big deal if all this memory was wasted but if this ever
235 * goes into a library that would probably not be a good idea.
237 * XXX - this *is* in a library....
240 #define CHUNK0SIZE 1024
246 /* Code generator state */
248 struct _compiler_state
{
258 int outermostlinktype
;
263 /* Hack for handling VLAN and MPLS stacks. */
264 u_int label_stack_depth
;
265 u_int vlan_stack_depth
;
271 * As errors are handled by a longjmp, anything allocated must
272 * be freed in the longjmp handler, so it must be reachable
275 * One thing that's allocated is the result of pcap_nametoaddrinfo();
276 * it must be freed with freeaddrinfo(). This variable points to
277 * any addrinfo structure that would need to be freed.
282 * Another thing that's allocated is the result of pcap_ether_aton();
283 * it must be freed with free(). This variable points to any
284 * address that would need to be freed.
289 * Various code constructs need to know the layout of the packet.
290 * These values give the necessary offsets from the beginning
291 * of the packet data.
295 * Absolute offset of the beginning of the link-layer header.
297 bpf_abs_offset off_linkhdr
;
300 * If we're checking a link-layer header for a packet encapsulated
301 * in another protocol layer, this is the equivalent information
302 * for the previous layers' link-layer header from the beginning
303 * of the raw packet data.
305 bpf_abs_offset off_prevlinkhdr
;
308 * This is the equivalent information for the outermost layers'
311 bpf_abs_offset off_outermostlinkhdr
;
314 * Absolute offset of the beginning of the link-layer payload.
316 bpf_abs_offset off_linkpl
;
319 * "off_linktype" is the offset to information in the link-layer
320 * header giving the packet type. This is an absolute offset
321 * from the beginning of the packet.
323 * For Ethernet, it's the offset of the Ethernet type field; this
324 * means that it must have a value that skips VLAN tags.
326 * For link-layer types that always use 802.2 headers, it's the
327 * offset of the LLC header; this means that it must have a value
328 * that skips VLAN tags.
330 * For PPP, it's the offset of the PPP type field.
332 * For Cisco HDLC, it's the offset of the CHDLC type field.
334 * For BSD loopback, it's the offset of the AF_ value.
336 * For Linux cooked sockets, it's the offset of the type field.
338 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
339 * encapsulation, in which case, IP is assumed.
341 bpf_abs_offset off_linktype
;
344 * TRUE if the link layer includes an ATM pseudo-header.
349 * TRUE if "geneve" appeared in the filter; it causes us to
350 * generate code that checks for a Geneve header and assume
351 * that later filters apply to the encapsulated payload.
356 * TRUE if we need variable length part of VLAN offset
358 int is_vlan_vloffset
;
361 * These are offsets for the ATM pseudo-header.
368 * These are offsets for the MTP2 fields.
374 * These are offsets for the MTP3 fields.
382 * This is the offset of the first byte after the ATM pseudo_header,
383 * or -1 if there is no ATM pseudo-header.
388 * These are offsets to the beginning of the network-layer header.
389 * They are relative to the beginning of the link-layer payload
390 * (i.e., they don't include off_linkhdr.constant_part or
391 * off_linkpl.constant_part).
393 * If the link layer never uses 802.2 LLC:
395 * "off_nl" and "off_nl_nosnap" are the same.
397 * If the link layer always uses 802.2 LLC:
399 * "off_nl" is the offset if there's a SNAP header following
402 * "off_nl_nosnap" is the offset if there's no SNAP header.
404 * If the link layer is Ethernet:
406 * "off_nl" is the offset if the packet is an Ethernet II packet
407 * (we assume no 802.3+802.2+SNAP);
409 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
410 * with an 802.2 header following it.
416 * Here we handle simple allocation of the scratch registers.
417 * If too many registers are alloc'd, the allocator punts.
419 int regused
[BPF_MEMWORDS
];
425 struct chunk chunks
[NCHUNKS
];
430 * For use by routines outside this file.
434 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
439 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
445 * For use *ONLY* in routines in this file.
447 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
448 PCAP_PRINTFLIKE(2, 3);
451 static void PCAP_NORETURN
452 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
457 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
460 longjmp(cstate
->top_ctx
, 1);
464 static int init_linktype(compiler_state_t
*, pcap_t
*);
466 static void init_regs(compiler_state_t
*);
467 static int alloc_reg(compiler_state_t
*);
468 static void free_reg(compiler_state_t
*, int);
470 static void initchunks(compiler_state_t
*cstate
);
471 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
472 static void *newchunk(compiler_state_t
*cstate
, size_t);
473 static void freechunks(compiler_state_t
*cstate
);
474 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
475 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
476 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
477 static inline void syntax(compiler_state_t
*cstate
);
479 static void backpatch(struct block
*, struct block
*);
480 static void merge(struct block
*, struct block
*);
481 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
483 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
485 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
487 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
489 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
491 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
492 u_int
, bpf_int32
, bpf_u_int32
);
493 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
494 u_int
, const u_char
*);
495 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
496 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
497 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
499 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
501 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
502 static struct block
*gen_uncond(compiler_state_t
*, int);
503 static inline struct block
*gen_true(compiler_state_t
*);
504 static inline struct block
*gen_false(compiler_state_t
*);
505 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
506 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
507 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
508 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
509 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
510 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
511 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
512 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
513 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
515 static int ethertype_to_ppptype(int);
516 static struct block
*gen_linktype(compiler_state_t
*, int);
517 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
518 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
519 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
520 int, int, u_int
, u_int
);
522 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
523 struct in6_addr
*, int, int, u_int
, u_int
);
525 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
526 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
527 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
528 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
529 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
530 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
531 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
532 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
533 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
536 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
537 struct in6_addr
*, int, int, int);
540 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
541 struct addrinfo
*, int, int);
543 static struct block
*gen_ipfrag(compiler_state_t
*);
544 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
545 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
547 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
548 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
550 struct block
*gen_portop(compiler_state_t
*, int, int, int);
551 static struct block
*gen_port(compiler_state_t
*, int, int, int);
552 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
553 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
554 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
555 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
556 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
557 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
558 static int lookup_proto(compiler_state_t
*, const char *, int);
559 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
560 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
561 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
562 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
563 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
564 static struct block
*gen_len(compiler_state_t
*, int, int);
565 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
566 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
568 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
569 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
570 bpf_int32
, bpf_u_int32
, int);
571 static struct block
*gen_atmtype_llc(compiler_state_t
*);
572 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
575 initchunks(compiler_state_t
*cstate
)
579 for (i
= 0; i
< NCHUNKS
; i
++) {
580 cstate
->chunks
[i
].n_left
= 0;
581 cstate
->chunks
[i
].m
= NULL
;
583 cstate
->cur_chunk
= 0;
587 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
594 /* XXX Round up to nearest long. */
595 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
597 /* XXX Round up to structure boundary. */
601 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
602 if (n
> cp
->n_left
) {
604 k
= ++cstate
->cur_chunk
;
606 bpf_set_error(cstate
, "out of memory");
609 size
= CHUNK0SIZE
<< k
;
610 cp
->m
= (void *)malloc(size
);
612 bpf_set_error(cstate
, "out of memory");
615 memset((char *)cp
->m
, 0, size
);
618 bpf_set_error(cstate
, "out of memory");
623 return (void *)((char *)cp
->m
+ cp
->n_left
);
627 newchunk(compiler_state_t
*cstate
, size_t n
)
631 p
= newchunk_nolongjmp(cstate
, n
);
633 longjmp(cstate
->top_ctx
, 1);
640 freechunks(compiler_state_t
*cstate
)
644 for (i
= 0; i
< NCHUNKS
; ++i
)
645 if (cstate
->chunks
[i
].m
!= NULL
)
646 free(cstate
->chunks
[i
].m
);
650 * A strdup whose allocations are freed after code generation is over.
651 * This is used by the lexical analyzer, so it can't longjmp; it just
652 * returns NULL on an allocation error, and the callers must check
656 sdup(compiler_state_t
*cstate
, const char *s
)
658 size_t n
= strlen(s
) + 1;
659 char *cp
= newchunk_nolongjmp(cstate
, n
);
663 pcap_strlcpy(cp
, s
, n
);
667 static inline struct block
*
668 new_block(compiler_state_t
*cstate
, int code
)
672 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
679 static inline struct slist
*
680 new_stmt(compiler_state_t
*cstate
, int code
)
684 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
690 static struct block
*
691 gen_retblk(compiler_state_t
*cstate
, int v
)
693 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
699 static inline PCAP_NORETURN_DEF
void
700 syntax(compiler_state_t
*cstate
)
702 bpf_error(cstate
, "syntax error in filter expression");
706 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
707 const char *buf
, int optimize
, bpf_u_int32 mask
)
712 compiler_state_t cstate
;
713 const char * volatile xbuf
= buf
;
714 yyscan_t scanner
= NULL
;
715 volatile YY_BUFFER_STATE in_buffer
= NULL
;
720 * If this pcap_t hasn't been activated, it doesn't have a
721 * link-layer type, so we can't use it.
724 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
725 "not-yet-activated pcap_t passed to pcap_compile");
737 * If the device on which we're capturing need to be notified
738 * that a new filter is being compiled, do so.
740 * This allows them to save a copy of it, in case, for example,
741 * they're implementing a form of remote packet capture, and
742 * want the remote machine to filter out the packets in which
743 * it's sending the packets it's captured.
745 * XXX - the fact that we happen to be compiling a filter
746 * doesn't necessarily mean we'll be installing it as the
747 * filter for this pcap_t; we might be running it from userland
748 * on captured packets to do packet classification. We really
749 * need a better way of handling this, but this is all that
750 * the WinPcap code did.
752 if (p
->save_current_filter_op
!= NULL
)
753 (p
->save_current_filter_op
)(p
, buf
);
757 cstate
.no_optimize
= 0;
762 cstate
.ic
.root
= NULL
;
763 cstate
.ic
.cur_mark
= 0;
767 cstate
.netmask
= mask
;
769 cstate
.snaplen
= pcap_snapshot(p
);
770 if (cstate
.snaplen
== 0) {
771 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
772 "snaplen of 0 rejects all packets");
777 if (pcap_lex_init(&scanner
) != 0)
778 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
779 errno
, "can't initialize scanner");
780 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
783 * Associate the compiler state with the lexical analyzer
786 pcap_set_extra(&cstate
, scanner
);
788 if (init_linktype(&cstate
, p
) == -1) {
792 if (pcap_parse(scanner
, &cstate
) != 0) {
794 if (cstate
.ai
!= NULL
)
795 freeaddrinfo(cstate
.ai
);
797 if (cstate
.e
!= NULL
)
803 if (cstate
.ic
.root
== NULL
)
804 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
806 if (optimize
&& !cstate
.no_optimize
) {
807 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
812 if (cstate
.ic
.root
== NULL
||
813 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
814 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
815 "expression rejects all packets");
820 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
821 cstate
.ic
.root
, &len
, p
->errbuf
);
822 if (program
->bf_insns
== NULL
) {
827 program
->bf_len
= len
;
829 rc
= 0; /* We're all okay */
833 * Clean up everything for the lexical analyzer.
835 if (in_buffer
!= NULL
)
836 pcap__delete_buffer(in_buffer
, scanner
);
838 pcap_lex_destroy(scanner
);
841 * Clean up our own allocated memory.
849 * entry point for using the compiler with no pcap open
850 * pass in all the stuff that is needed explicitly instead.
853 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
854 struct bpf_program
*program
,
855 const char *buf
, int optimize
, bpf_u_int32 mask
)
860 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
863 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
869 * Clean up a "struct bpf_program" by freeing all the memory allocated
873 pcap_freecode(struct bpf_program
*program
)
876 if (program
->bf_insns
!= NULL
) {
877 free((char *)program
->bf_insns
);
878 program
->bf_insns
= NULL
;
883 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
884 * which of the jt and jf fields has been resolved and which is a pointer
885 * back to another unresolved block (or nil). At least one of the fields
886 * in each block is already resolved.
889 backpatch(struct block
*list
, struct block
*target
)
906 * Merge the lists in b0 and b1, using the 'sense' field to indicate
907 * which of jt and jf is the link.
910 merge(struct block
*b0
, struct block
*b1
)
912 register struct block
**p
= &b0
;
914 /* Find end of list. */
916 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
918 /* Concatenate the lists. */
923 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
925 struct block
*ppi_dlt_check
;
928 * Insert before the statements of the first (root) block any
929 * statements needed to load the lengths of any variable-length
930 * headers into registers.
932 * XXX - a fancier strategy would be to insert those before the
933 * statements of all blocks that use those lengths and that
934 * have no predecessors that use them, so that we only compute
935 * the lengths if we need them. There might be even better
936 * approaches than that.
938 * However, those strategies would be more complicated, and
939 * as we don't generate code to compute a length if the
940 * program has no tests that use the length, and as most
941 * tests will probably use those lengths, we would just
942 * postpone computing the lengths so that it's not done
943 * for tests that fail early, and it's not clear that's
946 insert_compute_vloffsets(cstate
, p
->head
);
949 * For DLT_PPI captures, generate a check of the per-packet
950 * DLT value to make sure it's DLT_IEEE802_11.
952 * XXX - TurboCap cards use DLT_PPI for Ethernet.
953 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
954 * with appropriate Ethernet information and use that rather
955 * than using something such as DLT_PPI where you don't know
956 * the link-layer header type until runtime, which, in the
957 * general case, would force us to generate both Ethernet *and*
958 * 802.11 code (*and* anything else for which PPI is used)
959 * and choose between them early in the BPF program?
961 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
962 if (ppi_dlt_check
!= NULL
)
963 gen_and(ppi_dlt_check
, p
);
965 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
966 p
->sense
= !p
->sense
;
967 backpatch(p
, gen_retblk(cstate
, 0));
968 cstate
->ic
.root
= p
->head
;
972 gen_and(struct block
*b0
, struct block
*b1
)
974 backpatch(b0
, b1
->head
);
975 b0
->sense
= !b0
->sense
;
976 b1
->sense
= !b1
->sense
;
978 b1
->sense
= !b1
->sense
;
983 gen_or(struct block
*b0
, struct block
*b1
)
985 b0
->sense
= !b0
->sense
;
986 backpatch(b0
, b1
->head
);
987 b0
->sense
= !b0
->sense
;
993 gen_not(struct block
*b
)
995 b
->sense
= !b
->sense
;
998 static struct block
*
999 gen_cmp(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_JEQ
, 0, v
);
1005 static struct block
*
1006 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1007 u_int size
, bpf_int32 v
)
1009 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1012 static struct block
*
1013 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1014 u_int size
, bpf_int32 v
)
1016 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1019 static struct block
*
1020 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1021 u_int size
, bpf_int32 v
)
1023 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1026 static struct block
*
1027 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1028 u_int size
, bpf_int32 v
)
1030 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1033 static struct block
*
1034 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1035 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
1037 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1040 static struct block
*
1041 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1042 u_int size
, const u_char
*v
)
1044 register struct block
*b
, *tmp
;
1047 * XXX - the actual *instructions* do unsigned comparisons on
1048 * most platforms, and the load instructions don't do sign
1049 * extension, so gen_cmp() should really take an unsigned
1052 * As the load instructons also don't do sign-extension, we
1053 * fetch the values from the byte array as unsigned. We don't
1054 * want to use the signed versions of the extract calls.
1058 register const u_char
*p
= &v
[size
- 4];
1060 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1061 (bpf_int32
)EXTRACT_BE_U_4(p
));
1068 register const u_char
*p
= &v
[size
- 2];
1070 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1071 (bpf_int32
)EXTRACT_BE_U_2(p
));
1078 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1087 * AND the field of size "size" at offset "offset" relative to the header
1088 * specified by "offrel" with "mask", and compare it with the value "v"
1089 * with the test specified by "jtype"; if "reverse" is true, the test
1090 * should test the opposite of "jtype".
1092 static struct block
*
1093 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1094 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1097 struct slist
*s
, *s2
;
1100 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1102 if (mask
!= 0xffffffff) {
1103 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1108 b
= new_block(cstate
, JMP(jtype
));
1111 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1117 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1119 cstate
->pcap_fddipad
= p
->fddipad
;
1122 * We start out with only one link-layer header.
1124 cstate
->outermostlinktype
= pcap_datalink(p
);
1125 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1126 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1127 cstate
->off_outermostlinkhdr
.reg
= -1;
1129 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1130 cstate
->off_prevlinkhdr
.constant_part
= 0;
1131 cstate
->off_prevlinkhdr
.is_variable
= 0;
1132 cstate
->off_prevlinkhdr
.reg
= -1;
1134 cstate
->linktype
= cstate
->outermostlinktype
;
1135 cstate
->off_linkhdr
.constant_part
= 0;
1136 cstate
->off_linkhdr
.is_variable
= 0;
1137 cstate
->off_linkhdr
.reg
= -1;
1142 cstate
->off_linkpl
.constant_part
= 0;
1143 cstate
->off_linkpl
.is_variable
= 0;
1144 cstate
->off_linkpl
.reg
= -1;
1146 cstate
->off_linktype
.constant_part
= 0;
1147 cstate
->off_linktype
.is_variable
= 0;
1148 cstate
->off_linktype
.reg
= -1;
1151 * Assume it's not raw ATM with a pseudo-header, for now.
1154 cstate
->off_vpi
= OFFSET_NOT_SET
;
1155 cstate
->off_vci
= OFFSET_NOT_SET
;
1156 cstate
->off_proto
= OFFSET_NOT_SET
;
1157 cstate
->off_payload
= OFFSET_NOT_SET
;
1162 cstate
->is_geneve
= 0;
1165 * No variable length VLAN offset by default
1167 cstate
->is_vlan_vloffset
= 0;
1170 * And assume we're not doing SS7.
1172 cstate
->off_li
= OFFSET_NOT_SET
;
1173 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1174 cstate
->off_sio
= OFFSET_NOT_SET
;
1175 cstate
->off_opc
= OFFSET_NOT_SET
;
1176 cstate
->off_dpc
= OFFSET_NOT_SET
;
1177 cstate
->off_sls
= OFFSET_NOT_SET
;
1179 cstate
->label_stack_depth
= 0;
1180 cstate
->vlan_stack_depth
= 0;
1182 switch (cstate
->linktype
) {
1185 cstate
->off_linktype
.constant_part
= 2;
1186 cstate
->off_linkpl
.constant_part
= 6;
1187 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1188 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1191 case DLT_ARCNET_LINUX
:
1192 cstate
->off_linktype
.constant_part
= 4;
1193 cstate
->off_linkpl
.constant_part
= 8;
1194 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1195 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1199 cstate
->off_linktype
.constant_part
= 12;
1200 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1201 cstate
->off_nl
= 0; /* Ethernet II */
1202 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1207 * SLIP doesn't have a link level type. The 16 byte
1208 * header is hacked into our SLIP driver.
1210 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1211 cstate
->off_linkpl
.constant_part
= 16;
1213 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1216 case DLT_SLIP_BSDOS
:
1217 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1218 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1220 cstate
->off_linkpl
.constant_part
= 24;
1222 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1227 cstate
->off_linktype
.constant_part
= 0;
1228 cstate
->off_linkpl
.constant_part
= 4;
1230 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1234 cstate
->off_linktype
.constant_part
= 0;
1235 cstate
->off_linkpl
.constant_part
= 12;
1237 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1242 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1243 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1244 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1245 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1247 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1252 * This does no include the Ethernet header, and
1253 * only covers session state.
1255 cstate
->off_linktype
.constant_part
= 6;
1256 cstate
->off_linkpl
.constant_part
= 8;
1258 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1262 cstate
->off_linktype
.constant_part
= 5;
1263 cstate
->off_linkpl
.constant_part
= 24;
1265 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1270 * FDDI doesn't really have a link-level type field.
1271 * We set "off_linktype" to the offset of the LLC header.
1273 * To check for Ethernet types, we assume that SSAP = SNAP
1274 * is being used and pick out the encapsulated Ethernet type.
1275 * XXX - should we generate code to check for SNAP?
1277 cstate
->off_linktype
.constant_part
= 13;
1278 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1279 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1280 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1281 cstate
->off_nl
= 8; /* 802.2+SNAP */
1282 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1287 * Token Ring doesn't really have a link-level type field.
1288 * We set "off_linktype" to the offset of the LLC header.
1290 * To check for Ethernet types, we assume that SSAP = SNAP
1291 * is being used and pick out the encapsulated Ethernet type.
1292 * XXX - should we generate code to check for SNAP?
1294 * XXX - the header is actually variable-length.
1295 * Some various Linux patched versions gave 38
1296 * as "off_linktype" and 40 as "off_nl"; however,
1297 * if a token ring packet has *no* routing
1298 * information, i.e. is not source-routed, the correct
1299 * values are 20 and 22, as they are in the vanilla code.
1301 * A packet is source-routed iff the uppermost bit
1302 * of the first byte of the source address, at an
1303 * offset of 8, has the uppermost bit set. If the
1304 * packet is source-routed, the total number of bytes
1305 * of routing information is 2 plus bits 0x1F00 of
1306 * the 16-bit value at an offset of 14 (shifted right
1307 * 8 - figure out which byte that is).
1309 cstate
->off_linktype
.constant_part
= 14;
1310 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1311 cstate
->off_nl
= 8; /* 802.2+SNAP */
1312 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1315 case DLT_PRISM_HEADER
:
1316 case DLT_IEEE802_11_RADIO_AVS
:
1317 case DLT_IEEE802_11_RADIO
:
1318 cstate
->off_linkhdr
.is_variable
= 1;
1319 /* Fall through, 802.11 doesn't have a variable link
1320 * prefix but is otherwise the same. */
1322 case DLT_IEEE802_11
:
1324 * 802.11 doesn't really have a link-level type field.
1325 * We set "off_linktype.constant_part" to the offset of
1328 * To check for Ethernet types, we assume that SSAP = SNAP
1329 * is being used and pick out the encapsulated Ethernet type.
1330 * XXX - should we generate code to check for SNAP?
1332 * We also handle variable-length radio headers here.
1333 * The Prism header is in theory variable-length, but in
1334 * practice it's always 144 bytes long. However, some
1335 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1336 * sometimes or always supply an AVS header, so we
1337 * have to check whether the radio header is a Prism
1338 * header or an AVS header, so, in practice, it's
1341 cstate
->off_linktype
.constant_part
= 24;
1342 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1343 cstate
->off_linkpl
.is_variable
= 1;
1344 cstate
->off_nl
= 8; /* 802.2+SNAP */
1345 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1350 * At the moment we treat PPI the same way that we treat
1351 * normal Radiotap encoded packets. The difference is in
1352 * the function that generates the code at the beginning
1353 * to compute the header length. Since this code generator
1354 * of PPI supports bare 802.11 encapsulation only (i.e.
1355 * the encapsulated DLT should be DLT_IEEE802_11) we
1356 * generate code to check for this too.
1358 cstate
->off_linktype
.constant_part
= 24;
1359 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1360 cstate
->off_linkpl
.is_variable
= 1;
1361 cstate
->off_linkhdr
.is_variable
= 1;
1362 cstate
->off_nl
= 8; /* 802.2+SNAP */
1363 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1366 case DLT_ATM_RFC1483
:
1367 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1369 * assume routed, non-ISO PDUs
1370 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1372 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1373 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1374 * latter would presumably be treated the way PPPoE
1375 * should be, so you can do "pppoe and udp port 2049"
1376 * or "pppoa and tcp port 80" and have it check for
1377 * PPPo{A,E} and a PPP protocol of IP and....
1379 cstate
->off_linktype
.constant_part
= 0;
1380 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1381 cstate
->off_nl
= 8; /* 802.2+SNAP */
1382 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1387 * Full Frontal ATM; you get AALn PDUs with an ATM
1391 cstate
->off_vpi
= SUNATM_VPI_POS
;
1392 cstate
->off_vci
= SUNATM_VCI_POS
;
1393 cstate
->off_proto
= PROTO_POS
;
1394 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1395 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1396 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1397 cstate
->off_nl
= 8; /* 802.2+SNAP */
1398 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1404 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1405 cstate
->off_linkpl
.constant_part
= 0;
1407 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1410 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1411 cstate
->off_linktype
.constant_part
= 14;
1412 cstate
->off_linkpl
.constant_part
= 16;
1414 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1417 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1418 cstate
->off_linktype
.constant_part
= 0;
1419 cstate
->off_linkpl
.constant_part
= 20;
1421 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1426 * LocalTalk does have a 1-byte type field in the LLAP header,
1427 * but really it just indicates whether there is a "short" or
1428 * "long" DDP packet following.
1430 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1431 cstate
->off_linkpl
.constant_part
= 0;
1433 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1436 case DLT_IP_OVER_FC
:
1438 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1439 * link-level type field. We set "off_linktype" to the
1440 * offset of the LLC header.
1442 * To check for Ethernet types, we assume that SSAP = SNAP
1443 * is being used and pick out the encapsulated Ethernet type.
1444 * XXX - should we generate code to check for SNAP? RFC
1445 * 2625 says SNAP should be used.
1447 cstate
->off_linktype
.constant_part
= 16;
1448 cstate
->off_linkpl
.constant_part
= 16;
1449 cstate
->off_nl
= 8; /* 802.2+SNAP */
1450 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1455 * XXX - we should set this to handle SNAP-encapsulated
1456 * frames (NLPID of 0x80).
1458 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1459 cstate
->off_linkpl
.constant_part
= 0;
1461 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1465 * the only BPF-interesting FRF.16 frames are non-control frames;
1466 * Frame Relay has a variable length link-layer
1467 * so lets start with offset 4 for now and increments later on (FIXME);
1470 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1471 cstate
->off_linkpl
.constant_part
= 0;
1473 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1476 case DLT_APPLE_IP_OVER_IEEE1394
:
1477 cstate
->off_linktype
.constant_part
= 16;
1478 cstate
->off_linkpl
.constant_part
= 18;
1480 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1483 case DLT_SYMANTEC_FIREWALL
:
1484 cstate
->off_linktype
.constant_part
= 6;
1485 cstate
->off_linkpl
.constant_part
= 44;
1486 cstate
->off_nl
= 0; /* Ethernet II */
1487 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1490 #ifdef HAVE_NET_PFVAR_H
1492 cstate
->off_linktype
.constant_part
= 0;
1493 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1495 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1499 case DLT_JUNIPER_MFR
:
1500 case DLT_JUNIPER_MLFR
:
1501 case DLT_JUNIPER_MLPPP
:
1502 case DLT_JUNIPER_PPP
:
1503 case DLT_JUNIPER_CHDLC
:
1504 case DLT_JUNIPER_FRELAY
:
1505 cstate
->off_linktype
.constant_part
= 4;
1506 cstate
->off_linkpl
.constant_part
= 4;
1508 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1511 case DLT_JUNIPER_ATM1
:
1512 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1513 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1515 cstate
->off_nl_nosnap
= 10;
1518 case DLT_JUNIPER_ATM2
:
1519 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1520 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1522 cstate
->off_nl_nosnap
= 10;
1525 /* frames captured on a Juniper PPPoE service PIC
1526 * contain raw ethernet frames */
1527 case DLT_JUNIPER_PPPOE
:
1528 case DLT_JUNIPER_ETHER
:
1529 cstate
->off_linkpl
.constant_part
= 14;
1530 cstate
->off_linktype
.constant_part
= 16;
1531 cstate
->off_nl
= 18; /* Ethernet II */
1532 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1535 case DLT_JUNIPER_PPPOE_ATM
:
1536 cstate
->off_linktype
.constant_part
= 4;
1537 cstate
->off_linkpl
.constant_part
= 6;
1539 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1542 case DLT_JUNIPER_GGSN
:
1543 cstate
->off_linktype
.constant_part
= 6;
1544 cstate
->off_linkpl
.constant_part
= 12;
1546 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1549 case DLT_JUNIPER_ES
:
1550 cstate
->off_linktype
.constant_part
= 6;
1551 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1552 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1553 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1556 case DLT_JUNIPER_MONITOR
:
1557 cstate
->off_linktype
.constant_part
= 12;
1558 cstate
->off_linkpl
.constant_part
= 12;
1559 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1560 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1563 case DLT_BACNET_MS_TP
:
1564 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
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_SERVICES
:
1571 cstate
->off_linktype
.constant_part
= 12;
1572 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1573 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1574 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1577 case DLT_JUNIPER_VP
:
1578 cstate
->off_linktype
.constant_part
= 18;
1579 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1580 cstate
->off_nl
= OFFSET_NOT_SET
;
1581 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1584 case DLT_JUNIPER_ST
:
1585 cstate
->off_linktype
.constant_part
= 18;
1586 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1587 cstate
->off_nl
= OFFSET_NOT_SET
;
1588 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1591 case DLT_JUNIPER_ISM
:
1592 cstate
->off_linktype
.constant_part
= 8;
1593 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1594 cstate
->off_nl
= OFFSET_NOT_SET
;
1595 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1598 case DLT_JUNIPER_VS
:
1599 case DLT_JUNIPER_SRX_E2E
:
1600 case DLT_JUNIPER_FIBRECHANNEL
:
1601 case DLT_JUNIPER_ATM_CEMIC
:
1602 cstate
->off_linktype
.constant_part
= 8;
1603 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1604 cstate
->off_nl
= OFFSET_NOT_SET
;
1605 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1610 cstate
->off_li_hsl
= 4;
1611 cstate
->off_sio
= 3;
1612 cstate
->off_opc
= 4;
1613 cstate
->off_dpc
= 4;
1614 cstate
->off_sls
= 7;
1615 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1616 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1617 cstate
->off_nl
= OFFSET_NOT_SET
;
1618 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1621 case DLT_MTP2_WITH_PHDR
:
1623 cstate
->off_li_hsl
= 8;
1624 cstate
->off_sio
= 7;
1625 cstate
->off_opc
= 8;
1626 cstate
->off_dpc
= 8;
1627 cstate
->off_sls
= 11;
1628 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1629 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1630 cstate
->off_nl
= OFFSET_NOT_SET
;
1631 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1635 cstate
->off_li
= 22;
1636 cstate
->off_li_hsl
= 24;
1637 cstate
->off_sio
= 23;
1638 cstate
->off_opc
= 24;
1639 cstate
->off_dpc
= 24;
1640 cstate
->off_sls
= 27;
1641 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1642 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1643 cstate
->off_nl
= OFFSET_NOT_SET
;
1644 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1648 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1649 cstate
->off_linkpl
.constant_part
= 4;
1651 cstate
->off_nl_nosnap
= 0;
1656 * Currently, only raw "link[N:M]" filtering is supported.
1658 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1659 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1660 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1661 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1665 cstate
->off_linktype
.constant_part
= 1;
1666 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1668 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1671 case DLT_NETANALYZER
:
1672 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1673 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1674 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1675 cstate
->off_nl
= 0; /* Ethernet II */
1676 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1679 case DLT_NETANALYZER_TRANSPARENT
:
1680 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1681 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1682 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1683 cstate
->off_nl
= 0; /* Ethernet II */
1684 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1689 * For values in the range in which we've assigned new
1690 * DLT_ values, only raw "link[N:M]" filtering is supported.
1692 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1693 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1694 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1695 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1696 cstate
->off_nl
= OFFSET_NOT_SET
;
1697 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1699 bpf_set_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1705 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1710 * Load a value relative to the specified absolute offset.
1712 static struct slist
*
1713 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1714 u_int offset
, u_int size
)
1716 struct slist
*s
, *s2
;
1718 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1721 * If "s" is non-null, it has code to arrange that the X register
1722 * contains the variable part of the absolute offset, so we
1723 * generate a load relative to that, with an offset of
1724 * abs_offset->constant_part + offset.
1726 * Otherwise, we can do an absolute load with an offset of
1727 * abs_offset->constant_part + offset.
1731 * "s" points to a list of statements that puts the
1732 * variable part of the absolute offset into the X register.
1733 * Do an indirect load, to use the X register as an offset.
1735 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1736 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1740 * There is no variable part of the absolute offset, so
1741 * just do an absolute load.
1743 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1744 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1750 * Load a value relative to the beginning of the specified header.
1752 static struct slist
*
1753 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1756 struct slist
*s
, *s2
;
1761 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1766 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1769 case OR_PREVLINKHDR
:
1770 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1774 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1777 case OR_PREVMPLSHDR
:
1778 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1782 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1785 case OR_LINKPL_NOSNAP
:
1786 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1790 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1795 * Load the X register with the length of the IPv4 header
1796 * (plus the offset of the link-layer header, if it's
1797 * preceded by a variable-length header such as a radio
1798 * header), in bytes.
1800 s
= gen_loadx_iphdrlen(cstate
);
1803 * Load the item at {offset of the link-layer payload} +
1804 * {offset, relative to the start of the link-layer
1805 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1806 * {specified offset}.
1808 * If the offset of the link-layer payload is variable,
1809 * the variable part of that offset is included in the
1810 * value in the X register, and we include the constant
1811 * part in the offset of the load.
1813 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1814 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1819 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1830 * Generate code to load into the X register the sum of the length of
1831 * the IPv4 header and the variable part of the offset of the link-layer
1834 static struct slist
*
1835 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1837 struct slist
*s
, *s2
;
1839 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1842 * The offset of the link-layer payload has a variable
1843 * part. "s" points to a list of statements that put
1844 * the variable part of that offset into the X register.
1846 * The 4*([k]&0xf) addressing mode can't be used, as we
1847 * don't have a constant offset, so we have to load the
1848 * value in question into the A register and add to it
1849 * the value from the X register.
1851 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1852 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1854 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1857 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1862 * The A register now contains the length of the IP header.
1863 * We need to add to it the variable part of the offset of
1864 * the link-layer payload, which is still in the X
1865 * register, and move the result into the X register.
1867 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1868 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1871 * The offset of the link-layer payload is a constant,
1872 * so no code was generated to load the (non-existent)
1873 * variable part of that offset.
1875 * This means we can use the 4*([k]&0xf) addressing
1876 * mode. Load the length of the IPv4 header, which
1877 * is at an offset of cstate->off_nl from the beginning of
1878 * the link-layer payload, and thus at an offset of
1879 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1880 * of the raw packet data, using that addressing mode.
1882 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1883 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1889 static struct block
*
1890 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1895 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1897 b
= new_block(cstate
, JMP(BPF_JEQ
));
1903 static inline struct block
*
1904 gen_true(compiler_state_t
*cstate
)
1906 return gen_uncond(cstate
, 1);
1909 static inline struct block
*
1910 gen_false(compiler_state_t
*cstate
)
1912 return gen_uncond(cstate
, 0);
1916 * Byte-swap a 32-bit number.
1917 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1918 * big-endian platforms.)
1920 #define SWAPLONG(y) \
1921 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1924 * Generate code to match a particular packet type.
1926 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1927 * value, if <= ETHERMTU. We use that to determine whether to
1928 * match the type/length field or to check the type/length field for
1929 * a value <= ETHERMTU to see whether it's a type field and then do
1930 * the appropriate test.
1932 static struct block
*
1933 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1935 struct block
*b0
, *b1
;
1941 case LLCSAP_NETBEUI
:
1943 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1944 * so we check the DSAP and SSAP.
1946 * LLCSAP_IP checks for IP-over-802.2, rather
1947 * than IP-over-Ethernet or IP-over-SNAP.
1949 * XXX - should we check both the DSAP and the
1950 * SSAP, like this, or should we check just the
1951 * DSAP, as we do for other types <= ETHERMTU
1952 * (i.e., other SAP values)?
1954 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1956 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1957 ((proto
<< 8) | proto
));
1965 * Ethernet_II frames, which are Ethernet
1966 * frames with a frame type of ETHERTYPE_IPX;
1968 * Ethernet_802.3 frames, which are 802.3
1969 * frames (i.e., the type/length field is
1970 * a length field, <= ETHERMTU, rather than
1971 * a type field) with the first two bytes
1972 * after the Ethernet/802.3 header being
1975 * Ethernet_802.2 frames, which are 802.3
1976 * frames with an 802.2 LLC header and
1977 * with the IPX LSAP as the DSAP in the LLC
1980 * Ethernet_SNAP frames, which are 802.3
1981 * frames with an LLC header and a SNAP
1982 * header and with an OUI of 0x000000
1983 * (encapsulated Ethernet) and a protocol
1984 * ID of ETHERTYPE_IPX in the SNAP header.
1986 * XXX - should we generate the same code both
1987 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1991 * This generates code to check both for the
1992 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1994 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1995 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1999 * Now we add code to check for SNAP frames with
2000 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2002 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2006 * Now we generate code to check for 802.3
2007 * frames in general.
2009 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2013 * Now add the check for 802.3 frames before the
2014 * check for Ethernet_802.2 and Ethernet_802.3,
2015 * as those checks should only be done on 802.3
2016 * frames, not on Ethernet frames.
2021 * Now add the check for Ethernet_II frames, and
2022 * do that before checking for the other frame
2025 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2029 case ETHERTYPE_ATALK
:
2030 case ETHERTYPE_AARP
:
2032 * EtherTalk (AppleTalk protocols on Ethernet link
2033 * layer) may use 802.2 encapsulation.
2037 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2038 * we check for an Ethernet type field less than
2039 * 1500, which means it's an 802.3 length field.
2041 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2045 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2046 * SNAP packets with an organization code of
2047 * 0x080007 (Apple, for Appletalk) and a protocol
2048 * type of ETHERTYPE_ATALK (Appletalk).
2050 * 802.2-encapsulated ETHERTYPE_AARP packets are
2051 * SNAP packets with an organization code of
2052 * 0x000000 (encapsulated Ethernet) and a protocol
2053 * type of ETHERTYPE_AARP (Appletalk ARP).
2055 if (proto
== ETHERTYPE_ATALK
)
2056 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2057 else /* proto == ETHERTYPE_AARP */
2058 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2062 * Check for Ethernet encapsulation (Ethertalk
2063 * phase 1?); we just check for the Ethernet
2066 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2072 if (proto
<= ETHERMTU
) {
2074 * This is an LLC SAP value, so the frames
2075 * that match would be 802.2 frames.
2076 * Check that the frame is an 802.2 frame
2077 * (i.e., that the length/type field is
2078 * a length field, <= ETHERMTU) and
2079 * then check the DSAP.
2081 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2083 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2088 * This is an Ethernet type, so compare
2089 * the length/type field with it (if
2090 * the frame is an 802.2 frame, the length
2091 * field will be <= ETHERMTU, and, as
2092 * "proto" is > ETHERMTU, this test
2093 * will fail and the frame won't match,
2094 * which is what we want).
2096 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2102 static struct block
*
2103 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2106 * For DLT_NULL, the link-layer header is a 32-bit word
2107 * containing an AF_ value in *host* byte order, and for
2108 * DLT_ENC, the link-layer header begins with a 32-bit
2109 * word containing an AF_ value in host byte order.
2111 * In addition, if we're reading a saved capture file,
2112 * the host byte order in the capture may not be the
2113 * same as the host byte order on this machine.
2115 * For DLT_LOOP, the link-layer header is a 32-bit
2116 * word containing an AF_ value in *network* byte order.
2118 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2120 * The AF_ value is in host byte order, but the BPF
2121 * interpreter will convert it to network byte order.
2123 * If this is a save file, and it's from a machine
2124 * with the opposite byte order to ours, we byte-swap
2127 * Then we run it through "htonl()", and generate
2128 * code to compare against the result.
2130 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2131 proto
= SWAPLONG(proto
);
2132 proto
= htonl(proto
);
2134 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2138 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2139 * or IPv6 then we have an error.
2141 static struct block
*
2142 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2147 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2150 case ETHERTYPE_IPV6
:
2151 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2152 (bpf_int32
)IPH_AF_INET6
);
2159 return gen_false(cstate
);
2163 * Generate code to match a particular packet type.
2165 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2166 * value, if <= ETHERMTU. We use that to determine whether to
2167 * match the type field or to check the type field for the special
2168 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2170 static struct block
*
2171 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2173 struct block
*b0
, *b1
;
2179 case LLCSAP_NETBEUI
:
2181 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2182 * so we check the DSAP and SSAP.
2184 * LLCSAP_IP checks for IP-over-802.2, rather
2185 * than IP-over-Ethernet or IP-over-SNAP.
2187 * XXX - should we check both the DSAP and the
2188 * SSAP, like this, or should we check just the
2189 * DSAP, as we do for other types <= ETHERMTU
2190 * (i.e., other SAP values)?
2192 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2193 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2194 ((proto
<< 8) | proto
));
2200 * Ethernet_II frames, which are Ethernet
2201 * frames with a frame type of ETHERTYPE_IPX;
2203 * Ethernet_802.3 frames, which have a frame
2204 * type of LINUX_SLL_P_802_3;
2206 * Ethernet_802.2 frames, which are 802.3
2207 * frames with an 802.2 LLC header (i.e, have
2208 * a frame type of LINUX_SLL_P_802_2) and
2209 * with the IPX LSAP as the DSAP in the LLC
2212 * Ethernet_SNAP frames, which are 802.3
2213 * frames with an LLC header and a SNAP
2214 * header and with an OUI of 0x000000
2215 * (encapsulated Ethernet) and a protocol
2216 * ID of ETHERTYPE_IPX in the SNAP header.
2218 * First, do the checks on LINUX_SLL_P_802_2
2219 * frames; generate the check for either
2220 * Ethernet_802.2 or Ethernet_SNAP frames, and
2221 * then put a check for LINUX_SLL_P_802_2 frames
2224 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2225 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2227 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2231 * Now check for 802.3 frames and OR that with
2232 * the previous test.
2234 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2238 * Now add the check for Ethernet_II frames, and
2239 * do that before checking for the other frame
2242 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2246 case ETHERTYPE_ATALK
:
2247 case ETHERTYPE_AARP
:
2249 * EtherTalk (AppleTalk protocols on Ethernet link
2250 * layer) may use 802.2 encapsulation.
2254 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2255 * we check for the 802.2 protocol type in the
2256 * "Ethernet type" field.
2258 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2261 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2262 * SNAP packets with an organization code of
2263 * 0x080007 (Apple, for Appletalk) and a protocol
2264 * type of ETHERTYPE_ATALK (Appletalk).
2266 * 802.2-encapsulated ETHERTYPE_AARP packets are
2267 * SNAP packets with an organization code of
2268 * 0x000000 (encapsulated Ethernet) and a protocol
2269 * type of ETHERTYPE_AARP (Appletalk ARP).
2271 if (proto
== ETHERTYPE_ATALK
)
2272 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2273 else /* proto == ETHERTYPE_AARP */
2274 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2278 * Check for Ethernet encapsulation (Ethertalk
2279 * phase 1?); we just check for the Ethernet
2282 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2288 if (proto
<= ETHERMTU
) {
2290 * This is an LLC SAP value, so the frames
2291 * that match would be 802.2 frames.
2292 * Check for the 802.2 protocol type
2293 * in the "Ethernet type" field, and
2294 * then check the DSAP.
2296 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2297 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2303 * This is an Ethernet type, so compare
2304 * the length/type field with it (if
2305 * the frame is an 802.2 frame, the length
2306 * field will be <= ETHERMTU, and, as
2307 * "proto" is > ETHERMTU, this test
2308 * will fail and the frame won't match,
2309 * which is what we want).
2311 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2316 static struct slist
*
2317 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2319 struct slist
*s1
, *s2
;
2320 struct slist
*sjeq_avs_cookie
;
2321 struct slist
*sjcommon
;
2324 * This code is not compatible with the optimizer, as
2325 * we are generating jmp instructions within a normal
2326 * slist of instructions
2328 cstate
->no_optimize
= 1;
2331 * Generate code to load the length of the radio header into
2332 * the register assigned to hold that length, if one has been
2333 * assigned. (If one hasn't been assigned, no code we've
2334 * generated uses that prefix, so we don't need to generate any
2337 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2338 * or always use the AVS header rather than the Prism header.
2339 * We load a 4-byte big-endian value at the beginning of the
2340 * raw packet data, and see whether, when masked with 0xFFFFF000,
2341 * it's equal to 0x80211000. If so, that indicates that it's
2342 * an AVS header (the masked-out bits are the version number).
2343 * Otherwise, it's a Prism header.
2345 * XXX - the Prism header is also, in theory, variable-length,
2346 * but no known software generates headers that aren't 144
2349 if (cstate
->off_linkhdr
.reg
!= -1) {
2353 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2357 * AND it with 0xFFFFF000.
2359 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2360 s2
->s
.k
= 0xFFFFF000;
2364 * Compare with 0x80211000.
2366 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2367 sjeq_avs_cookie
->s
.k
= 0x80211000;
2368 sappend(s1
, sjeq_avs_cookie
);
2373 * The 4 bytes at an offset of 4 from the beginning of
2374 * the AVS header are the length of the AVS header.
2375 * That field is big-endian.
2377 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2380 sjeq_avs_cookie
->s
.jt
= s2
;
2383 * Now jump to the code to allocate a register
2384 * into which to save the header length and
2385 * store the length there. (The "jump always"
2386 * instruction needs to have the k field set;
2387 * it's added to the PC, so, as we're jumping
2388 * over a single instruction, it should be 1.)
2390 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2392 sappend(s1
, sjcommon
);
2395 * Now for the code that handles the Prism header.
2396 * Just load the length of the Prism header (144)
2397 * into the A register. Have the test for an AVS
2398 * header branch here if we don't have an AVS header.
2400 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2403 sjeq_avs_cookie
->s
.jf
= s2
;
2406 * Now allocate a register to hold that value and store
2407 * it. The code for the AVS header will jump here after
2408 * loading the length of the AVS header.
2410 s2
= new_stmt(cstate
, BPF_ST
);
2411 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2413 sjcommon
->s
.jf
= s2
;
2416 * Now move it into the X register.
2418 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2426 static struct slist
*
2427 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2429 struct slist
*s1
, *s2
;
2432 * Generate code to load the length of the AVS header into
2433 * the register assigned to hold that length, if one has been
2434 * assigned. (If one hasn't been assigned, no code we've
2435 * generated uses that prefix, so we don't need to generate any
2438 if (cstate
->off_linkhdr
.reg
!= -1) {
2440 * The 4 bytes at an offset of 4 from the beginning of
2441 * the AVS header are the length of the AVS header.
2442 * That field is big-endian.
2444 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2448 * Now allocate a register to hold that value and store
2451 s2
= new_stmt(cstate
, BPF_ST
);
2452 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2456 * Now move it into the X register.
2458 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2466 static struct slist
*
2467 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2469 struct slist
*s1
, *s2
;
2472 * Generate code to load the length of the radiotap header into
2473 * the register assigned to hold that length, if one has been
2474 * assigned. (If one hasn't been assigned, no code we've
2475 * generated uses that prefix, so we don't need to generate any
2478 if (cstate
->off_linkhdr
.reg
!= -1) {
2480 * The 2 bytes at offsets of 2 and 3 from the beginning
2481 * of the radiotap header are the length of the radiotap
2482 * header; unfortunately, it's little-endian, so we have
2483 * to load it a byte at a time and construct the value.
2487 * Load the high-order byte, at an offset of 3, shift it
2488 * left a byte, and put the result in the X register.
2490 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2492 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2495 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2499 * Load the next byte, at an offset of 2, and OR the
2500 * value from the X register into it.
2502 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2505 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2509 * Now allocate a register to hold that value and store
2512 s2
= new_stmt(cstate
, BPF_ST
);
2513 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2517 * Now move it into the X register.
2519 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2528 * At the moment we treat PPI as normal Radiotap encoded
2529 * packets. The difference is in the function that generates
2530 * the code at the beginning to compute the header length.
2531 * Since this code generator of PPI supports bare 802.11
2532 * encapsulation only (i.e. the encapsulated DLT should be
2533 * DLT_IEEE802_11) we generate code to check for this too;
2534 * that's done in finish_parse().
2536 static struct slist
*
2537 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2539 struct slist
*s1
, *s2
;
2542 * Generate code to load the length of the radiotap header
2543 * into the register assigned to hold that length, if one has
2546 if (cstate
->off_linkhdr
.reg
!= -1) {
2548 * The 2 bytes at offsets of 2 and 3 from the beginning
2549 * of the radiotap header are the length of the radiotap
2550 * header; unfortunately, it's little-endian, so we have
2551 * to load it a byte at a time and construct the value.
2555 * Load the high-order byte, at an offset of 3, shift it
2556 * left a byte, and put the result in the X register.
2558 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2560 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2563 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2567 * Load the next byte, at an offset of 2, and OR the
2568 * value from the X register into it.
2570 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2573 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2577 * Now allocate a register to hold that value and store
2580 s2
= new_stmt(cstate
, BPF_ST
);
2581 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2585 * Now move it into the X register.
2587 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2596 * Load a value relative to the beginning of the link-layer header after the 802.11
2597 * header, i.e. LLC_SNAP.
2598 * The link-layer header doesn't necessarily begin at the beginning
2599 * of the packet data; there might be a variable-length prefix containing
2600 * radio information.
2602 static struct slist
*
2603 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2606 struct slist
*sjset_data_frame_1
;
2607 struct slist
*sjset_data_frame_2
;
2608 struct slist
*sjset_qos
;
2609 struct slist
*sjset_radiotap_flags_present
;
2610 struct slist
*sjset_radiotap_ext_present
;
2611 struct slist
*sjset_radiotap_tsft_present
;
2612 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2613 struct slist
*s_roundup
;
2615 if (cstate
->off_linkpl
.reg
== -1) {
2617 * No register has been assigned to the offset of
2618 * the link-layer payload, which means nobody needs
2619 * it; don't bother computing it - just return
2620 * what we already have.
2626 * This code is not compatible with the optimizer, as
2627 * we are generating jmp instructions within a normal
2628 * slist of instructions
2630 cstate
->no_optimize
= 1;
2633 * If "s" is non-null, it has code to arrange that the X register
2634 * contains the length of the prefix preceding the link-layer
2637 * Otherwise, the length of the prefix preceding the link-layer
2638 * header is "off_outermostlinkhdr.constant_part".
2642 * There is no variable-length header preceding the
2643 * link-layer header.
2645 * Load the length of the fixed-length prefix preceding
2646 * the link-layer header (if any) into the X register,
2647 * and store it in the cstate->off_linkpl.reg register.
2648 * That length is off_outermostlinkhdr.constant_part.
2650 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2651 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2655 * The X register contains the offset of the beginning of the
2656 * link-layer header; add 24, which is the minimum length
2657 * of the MAC header for a data frame, to that, and store it
2658 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2659 * which is at the offset in the X register, with an indexed load.
2661 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2663 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2666 s2
= new_stmt(cstate
, BPF_ST
);
2667 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2670 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2675 * Check the Frame Control field to see if this is a data frame;
2676 * a data frame has the 0x08 bit (b3) in that field set and the
2677 * 0x04 bit (b2) clear.
2679 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2680 sjset_data_frame_1
->s
.k
= 0x08;
2681 sappend(s
, sjset_data_frame_1
);
2684 * If b3 is set, test b2, otherwise go to the first statement of
2685 * the rest of the program.
2687 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2688 sjset_data_frame_2
->s
.k
= 0x04;
2689 sappend(s
, sjset_data_frame_2
);
2690 sjset_data_frame_1
->s
.jf
= snext
;
2693 * If b2 is not set, this is a data frame; test the QoS bit.
2694 * Otherwise, go to the first statement of the rest of the
2697 sjset_data_frame_2
->s
.jt
= snext
;
2698 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2699 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2700 sappend(s
, sjset_qos
);
2703 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2705 * Otherwise, go to the first statement of the rest of the
2708 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2709 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2711 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2714 s2
= new_stmt(cstate
, BPF_ST
);
2715 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2719 * If we have a radiotap header, look at it to see whether
2720 * there's Atheros padding between the MAC-layer header
2723 * Note: all of the fields in the radiotap header are
2724 * little-endian, so we byte-swap all of the values
2725 * we test against, as they will be loaded as big-endian
2728 * XXX - in the general case, we would have to scan through
2729 * *all* the presence bits, if there's more than one word of
2730 * presence bits. That would require a loop, meaning that
2731 * we wouldn't be able to run the filter in the kernel.
2733 * We assume here that the Atheros adapters that insert the
2734 * annoying padding don't have multiple antennae and therefore
2735 * do not generate radiotap headers with multiple presence words.
2737 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2739 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2740 * in the first presence flag word?
2742 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2746 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2747 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2748 sappend(s
, sjset_radiotap_flags_present
);
2751 * If not, skip all of this.
2753 sjset_radiotap_flags_present
->s
.jf
= snext
;
2756 * Otherwise, is the "extension" bit set in that word?
2758 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2759 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2760 sappend(s
, sjset_radiotap_ext_present
);
2761 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2764 * If so, skip all of this.
2766 sjset_radiotap_ext_present
->s
.jt
= snext
;
2769 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2771 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2772 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2773 sappend(s
, sjset_radiotap_tsft_present
);
2774 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2777 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2778 * at an offset of 16 from the beginning of the raw packet
2779 * data (8 bytes for the radiotap header and 8 bytes for
2782 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2785 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2788 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2790 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2791 sjset_tsft_datapad
->s
.k
= 0x20;
2792 sappend(s
, sjset_tsft_datapad
);
2795 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2796 * at an offset of 8 from the beginning of the raw packet
2797 * data (8 bytes for the radiotap header).
2799 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2802 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2805 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2807 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2808 sjset_notsft_datapad
->s
.k
= 0x20;
2809 sappend(s
, sjset_notsft_datapad
);
2812 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2813 * set, round the length of the 802.11 header to
2814 * a multiple of 4. Do that by adding 3 and then
2815 * dividing by and multiplying by 4, which we do by
2818 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2819 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2820 sappend(s
, s_roundup
);
2821 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2824 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2827 s2
= new_stmt(cstate
, BPF_ST
);
2828 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2831 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2832 sjset_tsft_datapad
->s
.jf
= snext
;
2833 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2834 sjset_notsft_datapad
->s
.jf
= snext
;
2836 sjset_qos
->s
.jf
= snext
;
2842 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2846 /* There is an implicit dependency between the link
2847 * payload and link header since the payload computation
2848 * includes the variable part of the header. Therefore,
2849 * if nobody else has allocated a register for the link
2850 * header and we need it, do it now. */
2851 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2852 cstate
->off_linkhdr
.reg
== -1)
2853 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2856 * For link-layer types that have a variable-length header
2857 * preceding the link-layer header, generate code to load
2858 * the offset of the link-layer header into the register
2859 * assigned to that offset, if any.
2861 * XXX - this, and the next switch statement, won't handle
2862 * encapsulation of 802.11 or 802.11+radio information in
2863 * some other protocol stack. That's significantly more
2866 switch (cstate
->outermostlinktype
) {
2868 case DLT_PRISM_HEADER
:
2869 s
= gen_load_prism_llprefixlen(cstate
);
2872 case DLT_IEEE802_11_RADIO_AVS
:
2873 s
= gen_load_avs_llprefixlen(cstate
);
2876 case DLT_IEEE802_11_RADIO
:
2877 s
= gen_load_radiotap_llprefixlen(cstate
);
2881 s
= gen_load_ppi_llprefixlen(cstate
);
2890 * For link-layer types that have a variable-length link-layer
2891 * header, generate code to load the offset of the link-layer
2892 * payload into the register assigned to that offset, if any.
2894 switch (cstate
->outermostlinktype
) {
2896 case DLT_IEEE802_11
:
2897 case DLT_PRISM_HEADER
:
2898 case DLT_IEEE802_11_RADIO_AVS
:
2899 case DLT_IEEE802_11_RADIO
:
2901 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2906 * If there there is no initialization yet and we need variable
2907 * length offsets for VLAN, initialize them to zero
2909 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2912 if (cstate
->off_linkpl
.reg
== -1)
2913 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2914 if (cstate
->off_linktype
.reg
== -1)
2915 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2917 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2919 s2
= new_stmt(cstate
, BPF_ST
);
2920 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2922 s2
= new_stmt(cstate
, BPF_ST
);
2923 s2
->s
.k
= cstate
->off_linktype
.reg
;
2928 * If we have any offset-loading code, append all the
2929 * existing statements in the block to those statements,
2930 * and make the resulting list the list of statements
2934 sappend(s
, b
->stmts
);
2939 static struct block
*
2940 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2942 struct slist
*s_load_dlt
;
2945 if (cstate
->linktype
== DLT_PPI
)
2947 /* Create the statements that check for the DLT
2949 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2950 s_load_dlt
->s
.k
= 4;
2952 b
= new_block(cstate
, JMP(BPF_JEQ
));
2954 b
->stmts
= s_load_dlt
;
2955 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2966 * Take an absolute offset, and:
2968 * if it has no variable part, return NULL;
2970 * if it has a variable part, generate code to load the register
2971 * containing that variable part into the X register, returning
2972 * a pointer to that code - if no register for that offset has
2973 * been allocated, allocate it first.
2975 * (The code to set that register will be generated later, but will
2976 * be placed earlier in the code sequence.)
2978 static struct slist
*
2979 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2983 if (off
->is_variable
) {
2984 if (off
->reg
== -1) {
2986 * We haven't yet assigned a register for the
2987 * variable part of the offset of the link-layer
2988 * header; allocate one.
2990 off
->reg
= alloc_reg(cstate
);
2994 * Load the register containing the variable part of the
2995 * offset of the link-layer header into the X register.
2997 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3002 * That offset isn't variable, there's no variable part,
3003 * so we don't need to generate any code.
3010 * Map an Ethernet type to the equivalent PPP type.
3013 ethertype_to_ppptype(int proto
)
3021 case ETHERTYPE_IPV6
:
3029 case ETHERTYPE_ATALK
:
3043 * I'm assuming the "Bridging PDU"s that go
3044 * over PPP are Spanning Tree Protocol
3058 * Generate any tests that, for encapsulation of a link-layer packet
3059 * inside another protocol stack, need to be done to check for those
3060 * link-layer packets (and that haven't already been done by a check
3061 * for that encapsulation).
3063 static struct block
*
3064 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3068 if (cstate
->is_geneve
)
3069 return gen_geneve_ll_check(cstate
);
3071 switch (cstate
->prevlinktype
) {
3075 * This is LANE-encapsulated Ethernet; check that the LANE
3076 * packet doesn't begin with an LE Control marker, i.e.
3077 * that it's data, not a control message.
3079 * (We've already generated a test for LANE.)
3081 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3087 * No such tests are necessary.
3095 * The three different values we should check for when checking for an
3096 * IPv6 packet with DLT_NULL.
3098 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3099 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3100 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3103 * Generate code to match a particular packet type by matching the
3104 * link-layer type field or fields in the 802.2 LLC header.
3106 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3107 * value, if <= ETHERMTU.
3109 static struct block
*
3110 gen_linktype(compiler_state_t
*cstate
, int proto
)
3112 struct block
*b0
, *b1
, *b2
;
3113 const char *description
;
3115 /* are we checking MPLS-encapsulated packets? */
3116 if (cstate
->label_stack_depth
> 0) {
3120 /* FIXME add other L3 proto IDs */
3121 return gen_mpls_linktype(cstate
, Q_IP
);
3123 case ETHERTYPE_IPV6
:
3125 /* FIXME add other L3 proto IDs */
3126 return gen_mpls_linktype(cstate
, Q_IPV6
);
3129 bpf_error(cstate
, "unsupported protocol over mpls");
3134 switch (cstate
->linktype
) {
3137 case DLT_NETANALYZER
:
3138 case DLT_NETANALYZER_TRANSPARENT
:
3139 /* Geneve has an EtherType regardless of whether there is an
3141 if (!cstate
->is_geneve
)
3142 b0
= gen_prevlinkhdr_check(cstate
);
3146 b1
= gen_ether_linktype(cstate
, proto
);
3157 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3161 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3167 case DLT_IEEE802_11
:
3168 case DLT_PRISM_HEADER
:
3169 case DLT_IEEE802_11_RADIO_AVS
:
3170 case DLT_IEEE802_11_RADIO
:
3173 * Check that we have a data frame.
3175 b0
= gen_check_802_11_data_frame(cstate
);
3178 * Now check for the specified link-layer type.
3180 b1
= gen_llc_linktype(cstate
, proto
);
3188 * XXX - check for LLC frames.
3190 return gen_llc_linktype(cstate
, proto
);
3196 * XXX - check for LLC PDUs, as per IEEE 802.5.
3198 return gen_llc_linktype(cstate
, proto
);
3202 case DLT_ATM_RFC1483
:
3204 case DLT_IP_OVER_FC
:
3205 return gen_llc_linktype(cstate
, proto
);
3211 * Check for an LLC-encapsulated version of this protocol;
3212 * if we were checking for LANE, linktype would no longer
3215 * Check for LLC encapsulation and then check the protocol.
3217 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3218 b1
= gen_llc_linktype(cstate
, proto
);
3225 return gen_linux_sll_linktype(cstate
, proto
);
3230 case DLT_SLIP_BSDOS
:
3233 * These types don't provide any type field; packets
3234 * are always IPv4 or IPv6.
3236 * XXX - for IPv4, check for a version number of 4, and,
3237 * for IPv6, check for a version number of 6?
3242 /* Check for a version number of 4. */
3243 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3245 case ETHERTYPE_IPV6
:
3246 /* Check for a version number of 6. */
3247 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3250 return gen_false(cstate
); /* always false */
3257 * Raw IPv4, so no type field.
3259 if (proto
== ETHERTYPE_IP
)
3260 return gen_true(cstate
); /* always true */
3262 /* Checking for something other than IPv4; always false */
3263 return gen_false(cstate
);
3269 * Raw IPv6, so no type field.
3271 if (proto
== ETHERTYPE_IPV6
)
3272 return gen_true(cstate
); /* always true */
3274 /* Checking for something other than IPv6; always false */
3275 return gen_false(cstate
);
3281 case DLT_PPP_SERIAL
:
3284 * We use Ethernet protocol types inside libpcap;
3285 * map them to the corresponding PPP protocol types.
3287 proto
= ethertype_to_ppptype(proto
);
3288 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3294 * We use Ethernet protocol types inside libpcap;
3295 * map them to the corresponding PPP protocol types.
3301 * Also check for Van Jacobson-compressed IP.
3302 * XXX - do this for other forms of PPP?
3304 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3305 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3307 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3312 proto
= ethertype_to_ppptype(proto
);
3313 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3325 return (gen_loopback_linktype(cstate
, AF_INET
));
3327 case ETHERTYPE_IPV6
:
3329 * AF_ values may, unfortunately, be platform-
3330 * dependent; AF_INET isn't, because everybody
3331 * used 4.2BSD's value, but AF_INET6 is, because
3332 * 4.2BSD didn't have a value for it (given that
3333 * IPv6 didn't exist back in the early 1980's),
3334 * and they all picked their own values.
3336 * This means that, if we're reading from a
3337 * savefile, we need to check for all the
3340 * If we're doing a live capture, we only need
3341 * to check for this platform's value; however,
3342 * Npcap uses 24, which isn't Windows's AF_INET6
3343 * value. (Given the multiple different values,
3344 * programs that read pcap files shouldn't be
3345 * checking for their platform's AF_INET6 value
3346 * anyway, they should check for all of the
3347 * possible values. and they might as well do
3348 * that even for live captures.)
3350 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3352 * Savefile - check for all three
3353 * possible IPv6 values.
3355 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3356 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3358 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3363 * Live capture, so we only need to
3364 * check for the value used on this
3369 * Npcap doesn't use Windows's AF_INET6,
3370 * as that collides with AF_IPX on
3371 * some BSDs (both have the value 23).
3372 * Instead, it uses 24.
3374 return (gen_loopback_linktype(cstate
, 24));
3377 return (gen_loopback_linktype(cstate
, AF_INET6
));
3378 #else /* AF_INET6 */
3380 * I guess this platform doesn't support
3381 * IPv6, so we just reject all packets.
3383 return gen_false(cstate
);
3384 #endif /* AF_INET6 */
3390 * Not a type on which we support filtering.
3391 * XXX - support those that have AF_ values
3392 * #defined on this platform, at least?
3394 return gen_false(cstate
);
3397 #ifdef HAVE_NET_PFVAR_H
3400 * af field is host byte order in contrast to the rest of
3403 if (proto
== ETHERTYPE_IP
)
3404 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3405 BPF_B
, (bpf_int32
)AF_INET
));
3406 else if (proto
== ETHERTYPE_IPV6
)
3407 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3408 BPF_B
, (bpf_int32
)AF_INET6
));
3410 return gen_false(cstate
);
3413 #endif /* HAVE_NET_PFVAR_H */
3416 case DLT_ARCNET_LINUX
:
3418 * XXX should we check for first fragment if the protocol
3424 return gen_false(cstate
);
3426 case ETHERTYPE_IPV6
:
3427 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3428 (bpf_int32
)ARCTYPE_INET6
));
3431 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3432 (bpf_int32
)ARCTYPE_IP
);
3433 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3434 (bpf_int32
)ARCTYPE_IP_OLD
);
3439 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3440 (bpf_int32
)ARCTYPE_ARP
);
3441 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3442 (bpf_int32
)ARCTYPE_ARP_OLD
);
3446 case ETHERTYPE_REVARP
:
3447 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3448 (bpf_int32
)ARCTYPE_REVARP
));
3450 case ETHERTYPE_ATALK
:
3451 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3452 (bpf_int32
)ARCTYPE_ATALK
));
3459 case ETHERTYPE_ATALK
:
3460 return gen_true(cstate
);
3462 return gen_false(cstate
);
3469 * XXX - assumes a 2-byte Frame Relay header with
3470 * DLCI and flags. What if the address is longer?
3476 * Check for the special NLPID for IP.
3478 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3480 case ETHERTYPE_IPV6
:
3482 * Check for the special NLPID for IPv6.
3484 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3488 * Check for several OSI protocols.
3490 * Frame Relay packets typically have an OSI
3491 * NLPID at the beginning; we check for each
3494 * What we check for is the NLPID and a frame
3495 * control field of UI, i.e. 0x03 followed
3498 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3499 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3500 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3506 return gen_false(cstate
);
3512 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3514 case DLT_JUNIPER_MFR
:
3515 case DLT_JUNIPER_MLFR
:
3516 case DLT_JUNIPER_MLPPP
:
3517 case DLT_JUNIPER_ATM1
:
3518 case DLT_JUNIPER_ATM2
:
3519 case DLT_JUNIPER_PPPOE
:
3520 case DLT_JUNIPER_PPPOE_ATM
:
3521 case DLT_JUNIPER_GGSN
:
3522 case DLT_JUNIPER_ES
:
3523 case DLT_JUNIPER_MONITOR
:
3524 case DLT_JUNIPER_SERVICES
:
3525 case DLT_JUNIPER_ETHER
:
3526 case DLT_JUNIPER_PPP
:
3527 case DLT_JUNIPER_FRELAY
:
3528 case DLT_JUNIPER_CHDLC
:
3529 case DLT_JUNIPER_VP
:
3530 case DLT_JUNIPER_ST
:
3531 case DLT_JUNIPER_ISM
:
3532 case DLT_JUNIPER_VS
:
3533 case DLT_JUNIPER_SRX_E2E
:
3534 case DLT_JUNIPER_FIBRECHANNEL
:
3535 case DLT_JUNIPER_ATM_CEMIC
:
3537 /* just lets verify the magic number for now -
3538 * on ATM we may have up to 6 different encapsulations on the wire
3539 * and need a lot of heuristics to figure out that the payload
3542 * FIXME encapsulation specific BPF_ filters
3544 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3546 case DLT_BACNET_MS_TP
:
3547 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3550 return gen_ipnet_linktype(cstate
, proto
);
3552 case DLT_LINUX_IRDA
:
3553 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3556 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3559 case DLT_MTP2_WITH_PHDR
:
3560 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3563 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3566 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3568 case DLT_LINUX_LAPD
:
3569 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3571 case DLT_USB_FREEBSD
:
3573 case DLT_USB_LINUX_MMAPPED
:
3575 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3577 case DLT_BLUETOOTH_HCI_H4
:
3578 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3579 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3582 case DLT_CAN_SOCKETCAN
:
3583 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3585 case DLT_IEEE802_15_4
:
3586 case DLT_IEEE802_15_4_LINUX
:
3587 case DLT_IEEE802_15_4_NONASK_PHY
:
3588 case DLT_IEEE802_15_4_NOFCS
:
3589 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3591 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3592 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3595 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3598 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3601 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3604 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3607 /* Using the fixed-size NFLOG header it is possible to tell only
3608 * the address family of the packet, other meaningful data is
3609 * either missing or behind TLVs.
3611 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3615 * Does this link-layer header type have a field
3616 * indicating the type of the next protocol? If
3617 * so, off_linktype.constant_part will be the offset of that
3618 * field in the packet; if not, it will be OFFSET_NOT_SET.
3620 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3622 * Yes; assume it's an Ethernet type. (If
3623 * it's not, it needs to be handled specially
3626 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3629 * No; report an error.
3631 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3632 if (description
!= NULL
) {
3633 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3636 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3645 * Check for an LLC SNAP packet with a given organization code and
3646 * protocol type; we check the entire contents of the 802.2 LLC and
3647 * snap headers, checking for DSAP and SSAP of SNAP and a control
3648 * field of 0x03 in the LLC header, and for the specified organization
3649 * code and protocol type in the SNAP header.
3651 static struct block
*
3652 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3654 u_char snapblock
[8];
3656 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3657 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3658 snapblock
[2] = 0x03; /* control = UI */
3659 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3660 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3661 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3662 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3663 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3664 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3668 * Generate code to match frames with an LLC header.
3670 static struct block
*
3671 gen_llc_internal(compiler_state_t
*cstate
)
3673 struct block
*b0
, *b1
;
3675 switch (cstate
->linktype
) {
3679 * We check for an Ethernet type field less than
3680 * 1500, which means it's an 802.3 length field.
3682 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3686 * Now check for the purported DSAP and SSAP not being
3687 * 0xFF, to rule out NetWare-over-802.3.
3689 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3696 * We check for LLC traffic.
3698 b0
= gen_atmtype_llc(cstate
);
3701 case DLT_IEEE802
: /* Token Ring */
3703 * XXX - check for LLC frames.
3705 return gen_true(cstate
);
3709 * XXX - check for LLC frames.
3711 return gen_true(cstate
);
3713 case DLT_ATM_RFC1483
:
3715 * For LLC encapsulation, these are defined to have an
3718 * For VC encapsulation, they don't, but there's no
3719 * way to check for that; the protocol used on the VC
3720 * is negotiated out of band.
3722 return gen_true(cstate
);
3724 case DLT_IEEE802_11
:
3725 case DLT_PRISM_HEADER
:
3726 case DLT_IEEE802_11_RADIO
:
3727 case DLT_IEEE802_11_RADIO_AVS
:
3730 * Check that we have a data frame.
3732 b0
= gen_check_802_11_data_frame(cstate
);
3736 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3742 gen_llc(compiler_state_t
*cstate
)
3745 * Catch errors reported by us and routines below us, and return NULL
3748 if (setjmp(cstate
->top_ctx
))
3751 return gen_llc_internal(cstate
);
3755 gen_llc_i(compiler_state_t
*cstate
)
3757 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 * Load the control byte and test the low-order bit; it must
3774 * be clear for I frames.
3776 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3777 b1
= new_block(cstate
, JMP(BPF_JSET
));
3786 gen_llc_s(compiler_state_t
*cstate
)
3788 struct block
*b0
, *b1
;
3791 * Catch errors reported by us and routines below us, and return NULL
3794 if (setjmp(cstate
->top_ctx
))
3798 * Check whether this is an LLC frame.
3800 b0
= gen_llc_internal(cstate
);
3803 * Now compare the low-order 2 bit of the control byte against
3804 * the appropriate value for S frames.
3806 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3812 gen_llc_u(compiler_state_t
*cstate
)
3814 struct block
*b0
, *b1
;
3817 * Catch errors reported by us and routines below us, and return NULL
3820 if (setjmp(cstate
->top_ctx
))
3824 * Check whether this is an LLC frame.
3826 b0
= gen_llc_internal(cstate
);
3829 * Now compare the low-order 2 bit of the control byte against
3830 * the appropriate value for U frames.
3832 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3838 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3840 struct block
*b0
, *b1
;
3843 * Catch errors reported by us and routines below us, and return NULL
3846 if (setjmp(cstate
->top_ctx
))
3850 * Check whether this is an LLC frame.
3852 b0
= gen_llc_internal(cstate
);
3855 * Now check for an S frame with the appropriate type.
3857 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3863 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3865 struct block
*b0
, *b1
;
3868 * Catch errors reported by us and routines below us, and return NULL
3871 if (setjmp(cstate
->top_ctx
))
3875 * Check whether this is an LLC frame.
3877 b0
= gen_llc_internal(cstate
);
3880 * Now check for a U frame with the appropriate type.
3882 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3888 * Generate code to match a particular packet type, for link-layer types
3889 * using 802.2 LLC headers.
3891 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3892 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3894 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3895 * value, if <= ETHERMTU. We use that to determine whether to
3896 * match the DSAP or both DSAP and LSAP or to check the OUI and
3897 * protocol ID in a SNAP header.
3899 static struct block
*
3900 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3903 * XXX - handle token-ring variable-length header.
3909 case LLCSAP_NETBEUI
:
3911 * XXX - should we check both the DSAP and the
3912 * SSAP, like this, or should we check just the
3913 * DSAP, as we do for other SAP values?
3915 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3916 ((proto
<< 8) | proto
));
3920 * XXX - are there ever SNAP frames for IPX on
3921 * non-Ethernet 802.x networks?
3923 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3924 (bpf_int32
)LLCSAP_IPX
);
3926 case ETHERTYPE_ATALK
:
3928 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3929 * SNAP packets with an organization code of
3930 * 0x080007 (Apple, for Appletalk) and a protocol
3931 * type of ETHERTYPE_ATALK (Appletalk).
3933 * XXX - check for an organization code of
3934 * encapsulated Ethernet as well?
3936 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3940 * XXX - we don't have to check for IPX 802.3
3941 * here, but should we check for the IPX Ethertype?
3943 if (proto
<= ETHERMTU
) {
3945 * This is an LLC SAP value, so check
3948 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3951 * This is an Ethernet type; we assume that it's
3952 * unlikely that it'll appear in the right place
3953 * at random, and therefore check only the
3954 * location that would hold the Ethernet type
3955 * in a SNAP frame with an organization code of
3956 * 0x000000 (encapsulated Ethernet).
3958 * XXX - if we were to check for the SNAP DSAP and
3959 * LSAP, as per XXX, and were also to check for an
3960 * organization code of 0x000000 (encapsulated
3961 * Ethernet), we'd do
3963 * return gen_snap(cstate, 0x000000, proto);
3965 * here; for now, we don't, as per the above.
3966 * I don't know whether it's worth the extra CPU
3967 * time to do the right check or not.
3969 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3974 static struct block
*
3975 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3976 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3978 struct block
*b0
, *b1
;
3992 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3993 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3999 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4000 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4005 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4009 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4013 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4017 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4021 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4025 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4031 b0
= gen_linktype(cstate
, proto
);
4032 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
4038 static struct block
*
4039 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4040 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
4042 struct block
*b0
, *b1
;
4057 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4058 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4064 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4065 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4070 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4074 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4078 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4082 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4086 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4090 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4096 /* this order is important */
4097 a
= (uint32_t *)addr
;
4098 m
= (uint32_t *)mask
;
4099 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4100 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4102 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4104 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4106 b0
= gen_linktype(cstate
, proto
);
4112 static struct block
*
4113 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4115 register struct block
*b0
, *b1
;
4119 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4122 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4125 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4126 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4132 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4133 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4138 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4142 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4146 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4150 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4154 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4158 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4166 * Like gen_ehostop, but for DLT_FDDI
4168 static struct block
*
4169 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4171 struct block
*b0
, *b1
;
4175 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4178 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4181 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4182 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4188 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4189 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4194 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4198 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4202 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4206 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4210 bpf_error(cstate
, "'ra' is only supported on 802.11");
4214 bpf_error(cstate
, "'ta' is only supported on 802.11");
4222 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4224 static struct block
*
4225 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4227 register struct block
*b0
, *b1
;
4231 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4234 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4237 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4238 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4244 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4245 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4250 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4254 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4258 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4262 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4266 bpf_error(cstate
, "'ra' is only supported on 802.11");
4270 bpf_error(cstate
, "'ta' is only supported on 802.11");
4278 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4279 * various 802.11 + radio headers.
4281 static struct block
*
4282 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4284 register struct block
*b0
, *b1
, *b2
;
4285 register struct slist
*s
;
4287 #ifdef ENABLE_WLAN_FILTERING_PATCH
4290 * We need to disable the optimizer because the optimizer is buggy
4291 * and wipes out some LD instructions generated by the below
4292 * code to validate the Frame Control bits
4294 cstate
->no_optimize
= 1;
4295 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4302 * For control frames, there is no SA.
4304 * For management frames, SA is at an
4305 * offset of 10 from the beginning of
4308 * For data frames, SA is at an offset
4309 * of 10 from the beginning of the packet
4310 * if From DS is clear, at an offset of
4311 * 16 from the beginning of the packet
4312 * if From DS is set and To DS is clear,
4313 * and an offset of 24 from the beginning
4314 * of the packet if From DS is set and To DS
4319 * Generate the tests to be done for data frames
4322 * First, check for To DS set, i.e. check "link[1] & 0x01".
4324 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4325 b1
= new_block(cstate
, JMP(BPF_JSET
));
4326 b1
->s
.k
= 0x01; /* To DS */
4330 * If To DS is set, the SA is at 24.
4332 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4336 * Now, check for To DS not set, i.e. check
4337 * "!(link[1] & 0x01)".
4339 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4340 b2
= new_block(cstate
, JMP(BPF_JSET
));
4341 b2
->s
.k
= 0x01; /* To DS */
4346 * If To DS is not set, the SA is at 16.
4348 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4352 * Now OR together the last two checks. That gives
4353 * the complete set of checks for data frames with
4359 * Now check for From DS being set, and AND that with
4360 * the ORed-together checks.
4362 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4363 b1
= new_block(cstate
, JMP(BPF_JSET
));
4364 b1
->s
.k
= 0x02; /* From DS */
4369 * Now check for data frames with From DS not set.
4371 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4372 b2
= new_block(cstate
, JMP(BPF_JSET
));
4373 b2
->s
.k
= 0x02; /* From DS */
4378 * If From DS isn't set, the SA is at 10.
4380 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4384 * Now OR together the checks for data frames with
4385 * From DS not set and for data frames with From DS
4386 * set; that gives the checks done for data frames.
4391 * Now check for a data frame.
4392 * I.e, check "link[0] & 0x08".
4394 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4395 b1
= new_block(cstate
, JMP(BPF_JSET
));
4400 * AND that with the checks done for data frames.
4405 * If the high-order bit of the type value is 0, this
4406 * is a management frame.
4407 * I.e, check "!(link[0] & 0x08)".
4409 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4410 b2
= new_block(cstate
, JMP(BPF_JSET
));
4416 * For management frames, the SA is at 10.
4418 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4422 * OR that with the checks done for data frames.
4423 * That gives the checks done for management and
4429 * If the low-order bit of the type value is 1,
4430 * this is either a control frame or a frame
4431 * with a reserved type, and thus not a
4434 * I.e., check "!(link[0] & 0x04)".
4436 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4437 b1
= new_block(cstate
, JMP(BPF_JSET
));
4443 * AND that with the checks for data and management
4453 * For control frames, there is no DA.
4455 * For management frames, DA is at an
4456 * offset of 4 from the beginning of
4459 * For data frames, DA is at an offset
4460 * of 4 from the beginning of the packet
4461 * if To DS is clear and at an offset of
4462 * 16 from the beginning of the packet
4467 * Generate the tests to be done for data frames.
4469 * First, check for To DS set, i.e. "link[1] & 0x01".
4471 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4472 b1
= new_block(cstate
, JMP(BPF_JSET
));
4473 b1
->s
.k
= 0x01; /* To DS */
4477 * If To DS is set, the DA is at 16.
4479 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4483 * Now, check for To DS not set, i.e. check
4484 * "!(link[1] & 0x01)".
4486 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4487 b2
= new_block(cstate
, JMP(BPF_JSET
));
4488 b2
->s
.k
= 0x01; /* To DS */
4493 * If To DS is not set, the DA is at 4.
4495 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4499 * Now OR together the last two checks. That gives
4500 * the complete set of checks for data frames.
4505 * Now check for a data frame.
4506 * I.e, check "link[0] & 0x08".
4508 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4509 b1
= new_block(cstate
, JMP(BPF_JSET
));
4514 * AND that with the checks done for data frames.
4519 * If the high-order bit of the type value is 0, this
4520 * is a management frame.
4521 * I.e, check "!(link[0] & 0x08)".
4523 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4524 b2
= new_block(cstate
, JMP(BPF_JSET
));
4530 * For management frames, the DA is at 4.
4532 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4536 * OR that with the checks done for data frames.
4537 * That gives the checks done for management and
4543 * If the low-order bit of the type value is 1,
4544 * this is either a control frame or a frame
4545 * with a reserved type, and thus not a
4548 * I.e., check "!(link[0] & 0x04)".
4550 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4551 b1
= new_block(cstate
, JMP(BPF_JSET
));
4557 * AND that with the checks for data and management
4564 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4565 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4571 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4572 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4577 * XXX - add BSSID keyword?
4580 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4584 * Not present in CTS or ACK control frames.
4586 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4587 IEEE80211_FC0_TYPE_MASK
);
4589 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4590 IEEE80211_FC0_SUBTYPE_MASK
);
4592 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4593 IEEE80211_FC0_SUBTYPE_MASK
);
4597 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4603 * Not present in control frames.
4605 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4606 IEEE80211_FC0_TYPE_MASK
);
4608 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4614 * Present only if the direction mask has both "From DS"
4615 * and "To DS" set. Neither control frames nor management
4616 * frames should have both of those set, so we don't
4617 * check the frame type.
4619 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4620 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4621 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4627 * Not present in management frames; addr1 in other
4632 * If the high-order bit of the type value is 0, this
4633 * is a management frame.
4634 * I.e, check "(link[0] & 0x08)".
4636 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4637 b1
= new_block(cstate
, JMP(BPF_JSET
));
4644 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4647 * AND that with the check of addr1.
4654 * Not present in management frames; addr2, if present,
4659 * Not present in CTS or ACK control frames.
4661 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4662 IEEE80211_FC0_TYPE_MASK
);
4664 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4665 IEEE80211_FC0_SUBTYPE_MASK
);
4667 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4668 IEEE80211_FC0_SUBTYPE_MASK
);
4674 * If the high-order bit of the type value is 0, this
4675 * is a management frame.
4676 * I.e, check "(link[0] & 0x08)".
4678 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4679 b1
= new_block(cstate
, JMP(BPF_JSET
));
4684 * AND that with the check for frames other than
4685 * CTS and ACK frames.
4692 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4701 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4702 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4703 * as the RFC states.)
4705 static struct block
*
4706 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4708 register struct block
*b0
, *b1
;
4712 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4715 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4718 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4719 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4725 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4726 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4731 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4735 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4739 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4743 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4747 bpf_error(cstate
, "'ra' is only supported on 802.11");
4751 bpf_error(cstate
, "'ta' is only supported on 802.11");
4759 * This is quite tricky because there may be pad bytes in front of the
4760 * DECNET header, and then there are two possible data packet formats that
4761 * carry both src and dst addresses, plus 5 packet types in a format that
4762 * carries only the src node, plus 2 types that use a different format and
4763 * also carry just the src node.
4767 * Instead of doing those all right, we just look for data packets with
4768 * 0 or 1 bytes of padding. If you want to look at other packets, that
4769 * will require a lot more hacking.
4771 * To add support for filtering on DECNET "areas" (network numbers)
4772 * one would want to add a "mask" argument to this routine. That would
4773 * make the filter even more inefficient, although one could be clever
4774 * and not generate masking instructions if the mask is 0xFFFF.
4776 static struct block
*
4777 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4779 struct block
*b0
, *b1
, *b2
, *tmp
;
4780 u_int offset_lh
; /* offset if long header is received */
4781 u_int offset_sh
; /* offset if short header is received */
4786 offset_sh
= 1; /* follows flags */
4787 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4791 offset_sh
= 3; /* follows flags, dstnode */
4792 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4796 /* Inefficient because we do our Calvinball dance twice */
4797 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4798 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4804 /* Inefficient because we do our Calvinball dance twice */
4805 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4806 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4811 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4815 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4819 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4823 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4827 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4831 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4837 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4838 /* Check for pad = 1, long header case */
4839 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4840 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4841 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4842 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4844 /* Check for pad = 0, long header case */
4845 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4846 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4849 /* Check for pad = 1, short header case */
4850 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4851 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4852 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4855 /* Check for pad = 0, short header case */
4856 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4857 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4861 /* Combine with test for cstate->linktype */
4867 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4868 * test the bottom-of-stack bit, and then check the version number
4869 * field in the IP header.
4871 static struct block
*
4872 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4874 struct block
*b0
, *b1
;
4879 /* match the bottom-of-stack bit */
4880 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4881 /* match the IPv4 version number */
4882 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4887 /* match the bottom-of-stack bit */
4888 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4889 /* match the IPv4 version number */
4890 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4899 static struct block
*
4900 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4901 int proto
, int dir
, int type
)
4903 struct block
*b0
, *b1
;
4904 const char *typestr
;
4914 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4916 * Only check for non-IPv4 addresses if we're not
4917 * checking MPLS-encapsulated packets.
4919 if (cstate
->label_stack_depth
== 0) {
4920 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4922 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4928 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4931 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4934 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4937 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4940 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4943 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4946 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4949 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4952 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4955 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4958 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4961 return gen_dnhostop(cstate
, addr
, dir
);
4964 bpf_error(cstate
, "LAT host filtering not implemented");
4967 bpf_error(cstate
, "SCA host filtering not implemented");
4970 bpf_error(cstate
, "MOPRC host filtering not implemented");
4973 bpf_error(cstate
, "MOPDL host filtering not implemented");
4976 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4979 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4982 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
4985 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
4988 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
4991 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
4994 bpf_error(cstate
, "AARP host filtering not implemented");
4997 bpf_error(cstate
, "ISO host filtering not implemented");
5000 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5003 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5006 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5009 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5012 bpf_error(cstate
, "IPX host filtering not implemented");
5015 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5018 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5021 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5024 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5027 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5030 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5033 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5036 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5039 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5042 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5051 static struct block
*
5052 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5053 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5055 const char *typestr
;
5065 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5068 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5071 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5074 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5077 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5080 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5083 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5086 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5089 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5092 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5095 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5098 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5101 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5104 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5107 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5110 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5113 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5116 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5119 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5122 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5125 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5128 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5131 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5134 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5137 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5140 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5143 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5146 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5149 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5152 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5155 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5158 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5161 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5164 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5167 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5170 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5173 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5176 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5179 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5182 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5192 static struct block
*
5193 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5194 struct addrinfo
*alist
, int proto
, int dir
)
5196 struct block
*b0
, *b1
, *tmp
;
5197 struct addrinfo
*ai
;
5198 struct sockaddr_in
*sin
;
5201 bpf_error(cstate
, "direction applied to 'gateway'");
5208 switch (cstate
->linktype
) {
5210 case DLT_NETANALYZER
:
5211 case DLT_NETANALYZER_TRANSPARENT
:
5212 b1
= gen_prevlinkhdr_check(cstate
);
5213 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5218 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5221 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5223 case DLT_IEEE802_11
:
5224 case DLT_PRISM_HEADER
:
5225 case DLT_IEEE802_11_RADIO_AVS
:
5226 case DLT_IEEE802_11_RADIO
:
5228 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5232 * This is LLC-multiplexed traffic; if it were
5233 * LANE, cstate->linktype would have been set to
5237 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5239 case DLT_IP_OVER_FC
:
5240 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5244 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5247 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5249 * Does it have an address?
5251 if (ai
->ai_addr
!= NULL
) {
5253 * Yes. Is it an IPv4 address?
5255 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5257 * Generate an entry for it.
5259 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5260 tmp
= gen_host(cstate
,
5261 ntohl(sin
->sin_addr
.s_addr
),
5262 0xffffffff, proto
, Q_OR
, Q_HOST
);
5264 * Is it the *first* IPv4 address?
5268 * Yes, so start with it.
5273 * No, so OR it into the
5285 * No IPv4 addresses found.
5293 bpf_error(cstate
, "illegal modifier of 'gateway'");
5298 static struct block
*
5299 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5307 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5308 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5313 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5314 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5319 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5320 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5325 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5328 #ifndef IPPROTO_IGMP
5329 #define IPPROTO_IGMP 2
5333 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5336 #ifndef IPPROTO_IGRP
5337 #define IPPROTO_IGRP 9
5340 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5344 #define IPPROTO_PIM 103
5348 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5349 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5353 #ifndef IPPROTO_VRRP
5354 #define IPPROTO_VRRP 112
5358 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5361 #ifndef IPPROTO_CARP
5362 #define IPPROTO_CARP 112
5366 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5370 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5374 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5378 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5382 bpf_error(cstate
, "link layer applied in wrong context");
5385 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5389 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5393 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5397 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5401 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5405 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5409 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5413 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5416 #ifndef IPPROTO_ICMPV6
5417 #define IPPROTO_ICMPV6 58
5420 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5424 #define IPPROTO_AH 51
5427 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5428 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5433 #define IPPROTO_ESP 50
5436 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5437 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5442 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5446 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5450 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5453 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5454 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5455 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5457 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5459 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5461 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5465 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5466 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5467 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5469 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5471 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5473 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5477 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5478 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5479 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5481 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5486 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5487 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5492 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5493 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5495 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5497 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5502 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5503 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5508 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5509 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5514 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5518 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5522 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5526 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5530 bpf_error(cstate
, "'radio' is not a valid protocol type");
5539 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5542 * Catch errors reported by us and routines below us, and return NULL
5545 if (setjmp(cstate
->top_ctx
))
5548 return gen_proto_abbrev_internal(cstate
, proto
);
5551 static struct block
*
5552 gen_ipfrag(compiler_state_t
*cstate
)
5557 /* not IPv4 frag other than the first frag */
5558 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5559 b
= new_block(cstate
, JMP(BPF_JSET
));
5568 * Generate a comparison to a port value in the transport-layer header
5569 * at the specified offset from the beginning of that header.
5571 * XXX - this handles a variable-length prefix preceding the link-layer
5572 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5573 * variable-length link-layer headers (such as Token Ring or 802.11
5576 static struct block
*
5577 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5579 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5582 static struct block
*
5583 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5585 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5589 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5591 struct block
*b0
, *b1
, *tmp
;
5593 /* ip proto 'proto' and not a fragment other than the first fragment */
5594 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5595 b0
= gen_ipfrag(cstate
);
5600 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5604 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5608 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5609 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5615 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5616 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5621 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5625 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5629 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5633 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5637 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5641 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5652 static struct block
*
5653 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5655 struct block
*b0
, *b1
, *tmp
;
5660 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5661 * not LLC encapsulation with LLCSAP_IP.
5663 * For IEEE 802 networks - which includes 802.5 token ring
5664 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5665 * says that SNAP encapsulation is used, not LLC encapsulation
5668 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5669 * RFC 2225 say that SNAP encapsulation is used, not LLC
5670 * encapsulation with LLCSAP_IP.
5672 * So we always check for ETHERTYPE_IP.
5674 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5680 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5684 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5685 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5687 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5699 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5701 struct block
*b0
, *b1
, *tmp
;
5703 /* ip6 proto 'proto' */
5704 /* XXX - catch the first fragment of a fragmented packet? */
5705 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5709 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5713 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5717 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5718 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5724 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5725 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5737 static struct block
*
5738 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5740 struct block
*b0
, *b1
, *tmp
;
5742 /* link proto ip6 */
5743 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5749 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5753 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5754 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5756 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5767 /* gen_portrange code */
5768 static struct block
*
5769 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5772 struct block
*b1
, *b2
;
5776 * Reverse the order of the ports, so v1 is the lower one.
5785 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5786 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5794 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5797 struct block
*b0
, *b1
, *tmp
;
5799 /* ip proto 'proto' and not a fragment other than the first fragment */
5800 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5801 b0
= gen_ipfrag(cstate
);
5806 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5810 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5814 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5815 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5821 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5822 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5827 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5831 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5835 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5839 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5843 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5847 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5858 static struct block
*
5859 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5862 struct block
*b0
, *b1
, *tmp
;
5865 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5871 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5875 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5876 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5878 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5889 static struct block
*
5890 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5893 struct block
*b1
, *b2
;
5897 * Reverse the order of the ports, so v1 is the lower one.
5906 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5907 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5915 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5918 struct block
*b0
, *b1
, *tmp
;
5920 /* ip6 proto 'proto' */
5921 /* XXX - catch the first fragment of a fragmented packet? */
5922 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5926 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5930 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5934 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5935 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5941 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5942 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5954 static struct block
*
5955 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5958 struct block
*b0
, *b1
, *tmp
;
5960 /* link proto ip6 */
5961 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5967 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5971 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5972 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5974 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5986 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
5995 v
= pcap_nametoproto(name
);
5996 if (v
== PROTO_UNDEF
)
5997 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6001 /* XXX should look up h/w protocol type based on cstate->linktype */
6002 v
= pcap_nametoeproto(name
);
6003 if (v
== PROTO_UNDEF
) {
6004 v
= pcap_nametollc(name
);
6005 if (v
== PROTO_UNDEF
)
6006 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6011 if (strcmp(name
, "esis") == 0)
6013 else if (strcmp(name
, "isis") == 0)
6015 else if (strcmp(name
, "clnp") == 0)
6018 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6030 gen_joinsp(struct stmt
**s
, int n
)
6036 static struct block
*
6037 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6039 #ifdef NO_PROTOCHAIN
6040 return gen_proto(cstate
, v
, proto
, dir
);
6042 struct block
*b0
, *b
;
6043 struct slist
*s
[100];
6044 int fix2
, fix3
, fix4
, fix5
;
6045 int ahcheck
, again
, end
;
6047 int reg2
= alloc_reg(cstate
);
6049 memset(s
, 0, sizeof(s
));
6050 fix3
= fix4
= fix5
= 0;
6057 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
6058 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
6062 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6067 * We don't handle variable-length prefixes before the link-layer
6068 * header, or variable-length link-layer headers, here yet.
6069 * We might want to add BPF instructions to do the protochain
6070 * work, to simplify that and, on platforms that have a BPF
6071 * interpreter with the new instructions, let the filtering
6072 * be done in the kernel. (We already require a modified BPF
6073 * engine to do the protochain stuff, to support backward
6074 * branches, and backward branch support is unlikely to appear
6075 * in kernel BPF engines.)
6077 if (cstate
->off_linkpl
.is_variable
)
6078 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6080 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
6083 * s[0] is a dummy entry to protect other BPF insn from damage
6084 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6085 * hard to find interdependency made by jump table fixup.
6088 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6093 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6096 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6097 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6099 /* X = ip->ip_hl << 2 */
6100 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6101 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6106 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6108 /* A = ip6->ip_nxt */
6109 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6110 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6112 /* X = sizeof(struct ip6_hdr) */
6113 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6119 bpf_error(cstate
, "unsupported proto to gen_protochain");
6123 /* again: if (A == v) goto end; else fall through; */
6125 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*/
6132 #ifndef IPPROTO_NONE
6133 #define IPPROTO_NONE 59
6135 /* if (A == IPPROTO_NONE) goto end */
6136 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6137 s
[i
]->s
.jt
= NULL
; /*later*/
6138 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6139 s
[i
]->s
.k
= IPPROTO_NONE
;
6140 s
[fix5
]->s
.jf
= s
[i
];
6144 if (proto
== Q_IPV6
) {
6145 int v6start
, v6end
, v6advance
, j
;
6148 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6149 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6150 s
[i
]->s
.jt
= NULL
; /*later*/
6151 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6152 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6153 s
[fix2
]->s
.jf
= s
[i
];
6155 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6156 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6157 s
[i
]->s
.jt
= NULL
; /*later*/
6158 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6159 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6161 /* if (A == IPPROTO_ROUTING) goto v6advance */
6162 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6163 s
[i
]->s
.jt
= NULL
; /*later*/
6164 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6165 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6167 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6168 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6169 s
[i
]->s
.jt
= NULL
; /*later*/
6170 s
[i
]->s
.jf
= NULL
; /*later*/
6171 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6181 * A = P[X + packet head];
6182 * X = X + (P[X + packet head + 1] + 1) * 8;
6184 /* A = P[X + packet head] */
6185 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6186 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6189 s
[i
] = new_stmt(cstate
, BPF_ST
);
6192 /* A = P[X + packet head + 1]; */
6193 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6194 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6197 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6201 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6205 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6209 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6212 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6216 /* goto again; (must use BPF_JA for backward jump) */
6217 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6218 s
[i
]->s
.k
= again
- i
- 1;
6219 s
[i
- 1]->s
.jf
= s
[i
];
6223 for (j
= v6start
; j
<= v6end
; j
++)
6224 s
[j
]->s
.jt
= s
[v6advance
];
6227 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6229 s
[fix2
]->s
.jf
= s
[i
];
6235 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6236 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6237 s
[i
]->s
.jt
= NULL
; /*later*/
6238 s
[i
]->s
.jf
= NULL
; /*later*/
6239 s
[i
]->s
.k
= IPPROTO_AH
;
6241 s
[fix3
]->s
.jf
= s
[ahcheck
];
6248 * X = X + (P[X + 1] + 2) * 4;
6251 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6253 /* A = P[X + packet head]; */
6254 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6255 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6258 s
[i
] = new_stmt(cstate
, BPF_ST
);
6262 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6265 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6269 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6271 /* A = P[X + packet head] */
6272 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6273 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6276 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6280 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6284 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6287 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6291 /* goto again; (must use BPF_JA for backward jump) */
6292 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6293 s
[i
]->s
.k
= again
- i
- 1;
6298 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6300 s
[fix2
]->s
.jt
= s
[end
];
6301 s
[fix4
]->s
.jf
= s
[end
];
6302 s
[fix5
]->s
.jt
= s
[end
];
6309 for (i
= 0; i
< max
- 1; i
++)
6310 s
[i
]->next
= s
[i
+ 1];
6311 s
[max
- 1]->next
= NULL
;
6316 b
= new_block(cstate
, JMP(BPF_JEQ
));
6317 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6320 free_reg(cstate
, reg2
);
6327 static struct block
*
6328 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6331 struct block
*b0
, *b1
;
6334 * A data frame has the 0x08 bit (b3) in the frame control field set
6335 * and the 0x04 bit (b2) clear.
6337 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6338 b0
= new_block(cstate
, JMP(BPF_JSET
));
6342 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6343 b1
= new_block(cstate
, JMP(BPF_JSET
));
6354 * Generate code that checks whether the packet is a packet for protocol
6355 * <proto> and whether the type field in that protocol's header has
6356 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6357 * IP packet and checks the protocol number in the IP header against <v>.
6359 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6360 * against Q_IP and Q_IPV6.
6362 static struct block
*
6363 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6365 struct block
*b0
, *b1
;
6370 if (dir
!= Q_DEFAULT
)
6371 bpf_error(cstate
, "direction applied to 'proto'");
6375 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6376 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6381 return gen_linktype(cstate
, v
);
6385 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6386 * not LLC encapsulation with LLCSAP_IP.
6388 * For IEEE 802 networks - which includes 802.5 token ring
6389 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6390 * says that SNAP encapsulation is used, not LLC encapsulation
6393 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6394 * RFC 2225 say that SNAP encapsulation is used, not LLC
6395 * encapsulation with LLCSAP_IP.
6397 * So we always check for ETHERTYPE_IP.
6399 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6401 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6403 b1
= gen_protochain(cstate
, v
, Q_IP
);
6409 bpf_error(cstate
, "arp does not encapsulate another protocol");
6413 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6417 bpf_error(cstate
, "'sctp proto' is bogus");
6421 bpf_error(cstate
, "'tcp proto' is bogus");
6425 bpf_error(cstate
, "'udp proto' is bogus");
6429 bpf_error(cstate
, "'icmp proto' is bogus");
6433 bpf_error(cstate
, "'igmp proto' is bogus");
6437 bpf_error(cstate
, "'igrp proto' is bogus");
6441 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6445 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6449 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6453 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6457 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6461 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6465 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6468 * Also check for a fragment header before the final
6471 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6472 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6474 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6477 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6483 bpf_error(cstate
, "'icmp6 proto' is bogus");
6487 bpf_error(cstate
, "'ah proto' is bogus");
6491 bpf_error(cstate
, "'ah proto' is bogus");
6495 bpf_error(cstate
, "'pim proto' is bogus");
6499 bpf_error(cstate
, "'vrrp proto' is bogus");
6503 bpf_error(cstate
, "'aarp proto' is bogus");
6507 switch (cstate
->linktype
) {
6511 * Frame Relay packets typically have an OSI
6512 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6513 * generates code to check for all the OSI
6514 * NLPIDs, so calling it and then adding a check
6515 * for the particular NLPID for which we're
6516 * looking is bogus, as we can just check for
6519 * What we check for is the NLPID and a frame
6520 * control field value of UI, i.e. 0x03 followed
6523 * XXX - assumes a 2-byte Frame Relay header with
6524 * DLCI and flags. What if the address is longer?
6526 * XXX - what about SNAP-encapsulated frames?
6528 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6534 * Cisco uses an Ethertype lookalike - for OSI,
6537 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6538 /* OSI in C-HDLC is stuffed with a fudge byte */
6539 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (bpf_int32
)v
);
6544 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6545 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (bpf_int32
)v
);
6551 bpf_error(cstate
, "'esis proto' is bogus");
6555 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6557 * 4 is the offset of the PDU type relative to the IS-IS
6560 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (bpf_int32
)v
);
6565 bpf_error(cstate
, "'clnp proto' is not supported");
6569 bpf_error(cstate
, "'stp proto' is bogus");
6573 bpf_error(cstate
, "'ipx proto' is bogus");
6577 bpf_error(cstate
, "'netbeui proto' is bogus");
6581 bpf_error(cstate
, "'l1 proto' is bogus");
6585 bpf_error(cstate
, "'l2 proto' is bogus");
6589 bpf_error(cstate
, "'iih proto' is bogus");
6593 bpf_error(cstate
, "'snp proto' is bogus");
6597 bpf_error(cstate
, "'csnp proto' is bogus");
6601 bpf_error(cstate
, "'psnp proto' is bogus");
6605 bpf_error(cstate
, "'lsp proto' is bogus");
6609 bpf_error(cstate
, "'radio proto' is bogus");
6613 bpf_error(cstate
, "'carp proto' is bogus");
6624 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6626 int proto
= q
.proto
;
6630 bpf_u_int32 mask
, addr
;
6631 struct addrinfo
*res
, *res0
;
6632 struct sockaddr_in
*sin4
;
6635 struct sockaddr_in6
*sin6
;
6636 struct in6_addr mask128
;
6638 struct block
*b
, *tmp
;
6639 int port
, real_proto
;
6643 * Catch errors reported by us and routines below us, and return NULL
6646 if (setjmp(cstate
->top_ctx
))
6652 addr
= pcap_nametonetaddr(name
);
6654 bpf_error(cstate
, "unknown network '%s'", name
);
6655 /* Left justify network addr and calculate its network mask */
6657 while (addr
&& (addr
& 0xff000000) == 0) {
6661 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6665 if (proto
== Q_LINK
) {
6666 switch (cstate
->linktype
) {
6669 case DLT_NETANALYZER
:
6670 case DLT_NETANALYZER_TRANSPARENT
:
6671 eaddr
= pcap_ether_hostton(name
);
6674 "unknown ether host '%s'", name
);
6675 tmp
= gen_prevlinkhdr_check(cstate
);
6676 b
= gen_ehostop(cstate
, eaddr
, dir
);
6683 eaddr
= pcap_ether_hostton(name
);
6686 "unknown FDDI host '%s'", name
);
6687 b
= gen_fhostop(cstate
, eaddr
, dir
);
6692 eaddr
= pcap_ether_hostton(name
);
6695 "unknown token ring host '%s'", name
);
6696 b
= gen_thostop(cstate
, eaddr
, dir
);
6700 case DLT_IEEE802_11
:
6701 case DLT_PRISM_HEADER
:
6702 case DLT_IEEE802_11_RADIO_AVS
:
6703 case DLT_IEEE802_11_RADIO
:
6705 eaddr
= pcap_ether_hostton(name
);
6708 "unknown 802.11 host '%s'", name
);
6709 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6713 case DLT_IP_OVER_FC
:
6714 eaddr
= pcap_ether_hostton(name
);
6717 "unknown Fibre Channel host '%s'", name
);
6718 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6723 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6724 } else if (proto
== Q_DECNET
) {
6725 unsigned short dn_addr
;
6727 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6729 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6731 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6736 * I don't think DECNET hosts can be multihomed, so
6737 * there is no need to build up a list of addresses
6739 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6742 memset(&mask128
, 0xff, sizeof(mask128
));
6744 res0
= res
= pcap_nametoaddrinfo(name
);
6746 bpf_error(cstate
, "unknown host '%s'", name
);
6753 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6754 tproto
== Q_DEFAULT
) {
6760 for (res
= res0
; res
; res
= res
->ai_next
) {
6761 switch (res
->ai_family
) {
6764 if (tproto
== Q_IPV6
)
6768 sin4
= (struct sockaddr_in
*)
6770 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6771 0xffffffff, tproto
, dir
, q
.addr
);
6775 if (tproto6
== Q_IP
)
6778 sin6
= (struct sockaddr_in6
*)
6780 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6781 &mask128
, tproto6
, dir
, q
.addr
);
6794 bpf_error(cstate
, "unknown host '%s'%s", name
,
6795 (proto
== Q_DEFAULT
)
6797 : " for specified address family");
6803 if (proto
!= Q_DEFAULT
&&
6804 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6805 bpf_error(cstate
, "illegal qualifier of 'port'");
6806 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6807 bpf_error(cstate
, "unknown port '%s'", name
);
6808 if (proto
== Q_UDP
) {
6809 if (real_proto
== IPPROTO_TCP
)
6810 bpf_error(cstate
, "port '%s' is tcp", name
);
6811 else if (real_proto
== IPPROTO_SCTP
)
6812 bpf_error(cstate
, "port '%s' is sctp", name
);
6814 /* override PROTO_UNDEF */
6815 real_proto
= IPPROTO_UDP
;
6817 if (proto
== Q_TCP
) {
6818 if (real_proto
== IPPROTO_UDP
)
6819 bpf_error(cstate
, "port '%s' is udp", name
);
6821 else if (real_proto
== IPPROTO_SCTP
)
6822 bpf_error(cstate
, "port '%s' is sctp", name
);
6824 /* override PROTO_UNDEF */
6825 real_proto
= IPPROTO_TCP
;
6827 if (proto
== Q_SCTP
) {
6828 if (real_proto
== IPPROTO_UDP
)
6829 bpf_error(cstate
, "port '%s' is udp", name
);
6831 else if (real_proto
== IPPROTO_TCP
)
6832 bpf_error(cstate
, "port '%s' is tcp", name
);
6834 /* override PROTO_UNDEF */
6835 real_proto
= IPPROTO_SCTP
;
6838 bpf_error(cstate
, "illegal port number %d < 0", port
);
6840 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6841 b
= gen_port(cstate
, port
, real_proto
, dir
);
6842 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6846 if (proto
!= Q_DEFAULT
&&
6847 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6848 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6849 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6850 bpf_error(cstate
, "unknown port in range '%s'", name
);
6851 if (proto
== Q_UDP
) {
6852 if (real_proto
== IPPROTO_TCP
)
6853 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6854 else if (real_proto
== IPPROTO_SCTP
)
6855 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6857 /* override PROTO_UNDEF */
6858 real_proto
= IPPROTO_UDP
;
6860 if (proto
== Q_TCP
) {
6861 if (real_proto
== IPPROTO_UDP
)
6862 bpf_error(cstate
, "port in range '%s' is udp", name
);
6863 else if (real_proto
== IPPROTO_SCTP
)
6864 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6866 /* override PROTO_UNDEF */
6867 real_proto
= IPPROTO_TCP
;
6869 if (proto
== Q_SCTP
) {
6870 if (real_proto
== IPPROTO_UDP
)
6871 bpf_error(cstate
, "port in range '%s' is udp", name
);
6872 else if (real_proto
== IPPROTO_TCP
)
6873 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6875 /* override PROTO_UNDEF */
6876 real_proto
= IPPROTO_SCTP
;
6879 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6881 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6883 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6885 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6887 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6888 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6893 eaddr
= pcap_ether_hostton(name
);
6895 bpf_error(cstate
, "unknown ether host: %s", name
);
6897 res
= pcap_nametoaddrinfo(name
);
6900 bpf_error(cstate
, "unknown host '%s'", name
);
6901 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6905 bpf_error(cstate
, "unknown host '%s'", name
);
6908 bpf_error(cstate
, "'gateway' not supported in this configuration");
6912 real_proto
= lookup_proto(cstate
, name
, proto
);
6913 if (real_proto
>= 0)
6914 return gen_proto(cstate
, real_proto
, proto
, dir
);
6916 bpf_error(cstate
, "unknown protocol: %s", name
);
6919 real_proto
= lookup_proto(cstate
, name
, proto
);
6920 if (real_proto
>= 0)
6921 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6923 bpf_error(cstate
, "unknown protocol: %s", name
);
6934 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6935 unsigned int masklen
, struct qual q
)
6937 register int nlen
, mlen
;
6941 * Catch errors reported by us and routines below us, and return NULL
6944 if (setjmp(cstate
->top_ctx
))
6947 nlen
= __pcap_atoin(s1
, &n
);
6948 /* Promote short ipaddr */
6952 mlen
= __pcap_atoin(s2
, &m
);
6953 /* Promote short ipaddr */
6956 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6959 /* Convert mask len to mask */
6961 bpf_error(cstate
, "mask length must be <= 32");
6964 * X << 32 is not guaranteed by C to be 0; it's
6969 m
= 0xffffffff << (32 - masklen
);
6971 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6978 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6981 bpf_error(cstate
, "Mask syntax for networks only");
6988 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
6996 * Catch errors reported by us and routines below us, and return NULL
6999 if (setjmp(cstate
->top_ctx
))
7006 else if (q
.proto
== Q_DECNET
) {
7007 vlen
= __pcap_atodn(s
, &v
);
7009 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7011 vlen
= __pcap_atoin(s
, &v
);
7018 if (proto
== Q_DECNET
)
7019 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7020 else if (proto
== Q_LINK
) {
7021 bpf_error(cstate
, "illegal link layer address");
7024 if (s
== NULL
&& q
.addr
== Q_NET
) {
7025 /* Promote short net number */
7026 while (v
&& (v
& 0xff000000) == 0) {
7031 /* Promote short ipaddr */
7033 mask
<<= 32 - vlen
;
7035 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7040 proto
= IPPROTO_UDP
;
7041 else if (proto
== Q_TCP
)
7042 proto
= IPPROTO_TCP
;
7043 else if (proto
== Q_SCTP
)
7044 proto
= IPPROTO_SCTP
;
7045 else if (proto
== Q_DEFAULT
)
7046 proto
= PROTO_UNDEF
;
7048 bpf_error(cstate
, "illegal qualifier of 'port'");
7051 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7055 b
= gen_port(cstate
, (int)v
, proto
, dir
);
7056 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
7062 proto
= IPPROTO_UDP
;
7063 else if (proto
== Q_TCP
)
7064 proto
= IPPROTO_TCP
;
7065 else if (proto
== Q_SCTP
)
7066 proto
= IPPROTO_SCTP
;
7067 else if (proto
== Q_DEFAULT
)
7068 proto
= PROTO_UNDEF
;
7070 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7073 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7077 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
7078 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
7083 bpf_error(cstate
, "'gateway' requires a name");
7087 return gen_proto(cstate
, (int)v
, proto
, dir
);
7090 return gen_protochain(cstate
, (int)v
, proto
, dir
);
7105 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7106 unsigned int masklen
, struct qual q
)
7108 struct addrinfo
*res
;
7109 struct in6_addr
*addr
;
7110 struct in6_addr mask
;
7115 * Catch errors reported by us and routines below us, and return NULL
7118 if (setjmp(cstate
->top_ctx
))
7122 bpf_error(cstate
, "no mask %s supported", s2
);
7124 res
= pcap_nametoaddrinfo(s1
);
7126 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7129 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7130 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7132 if (sizeof(mask
) * 8 < masklen
)
7133 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
7134 memset(&mask
, 0, sizeof(mask
));
7135 memset(&mask
, 0xff, masklen
/ 8);
7137 mask
.s6_addr
[masklen
/ 8] =
7138 (0xff << (8 - masklen
% 8)) & 0xff;
7141 a
= (uint32_t *)addr
;
7142 m
= (uint32_t *)&mask
;
7143 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7144 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7145 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7153 bpf_error(cstate
, "Mask syntax for networks only");
7157 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7163 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7170 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7172 struct block
*b
, *tmp
;
7175 * Catch errors reported by us and routines below us, and return NULL
7178 if (setjmp(cstate
->top_ctx
))
7181 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7182 cstate
->e
= pcap_ether_aton(s
);
7183 if (cstate
->e
== NULL
)
7184 bpf_error(cstate
, "malloc");
7185 switch (cstate
->linktype
) {
7187 case DLT_NETANALYZER
:
7188 case DLT_NETANALYZER_TRANSPARENT
:
7189 tmp
= gen_prevlinkhdr_check(cstate
);
7190 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7195 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7198 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7200 case DLT_IEEE802_11
:
7201 case DLT_PRISM_HEADER
:
7202 case DLT_IEEE802_11_RADIO_AVS
:
7203 case DLT_IEEE802_11_RADIO
:
7205 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7207 case DLT_IP_OVER_FC
:
7208 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7213 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7221 bpf_error(cstate
, "ethernet address used in non-ether expression");
7226 sappend(struct slist
*s0
, struct slist
*s1
)
7229 * This is definitely not the best way to do this, but the
7230 * lists will rarely get long.
7237 static struct slist
*
7238 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7242 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7247 static struct slist
*
7248 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7252 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7258 * Modify "index" to use the value stored into its register as an
7259 * offset relative to the beginning of the header for the protocol
7260 * "proto", and allocate a register and put an item "size" bytes long
7261 * (1, 2, or 4) at that offset into that register, making it the register
7264 static struct arth
*
7265 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7267 struct slist
*s
, *tmp
;
7269 int regno
= alloc_reg(cstate
);
7271 free_reg(cstate
, inst
->regno
);
7275 bpf_error(cstate
, "data size must be 1, 2, or 4");
7291 bpf_error(cstate
, "unsupported index operation");
7295 * The offset is relative to the beginning of the packet
7296 * data, if we have a radio header. (If we don't, this
7299 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7300 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7301 cstate
->linktype
!= DLT_PRISM_HEADER
)
7302 bpf_error(cstate
, "radio information not present in capture");
7305 * Load into the X register the offset computed into the
7306 * register specified by "index".
7308 s
= xfer_to_x(cstate
, inst
);
7311 * Load the item at that offset.
7313 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7315 sappend(inst
->s
, s
);
7320 * The offset is relative to the beginning of
7321 * the link-layer header.
7323 * XXX - what about ATM LANE? Should the index be
7324 * relative to the beginning of the AAL5 frame, so
7325 * that 0 refers to the beginning of the LE Control
7326 * field, or relative to the beginning of the LAN
7327 * frame, so that 0 refers, for Ethernet LANE, to
7328 * the beginning of the destination address?
7330 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7333 * If "s" is non-null, it has code to arrange that the
7334 * X register contains the length of the prefix preceding
7335 * the link-layer header. Add to it the offset computed
7336 * into the register specified by "index", and move that
7337 * into the X register. Otherwise, just load into the X
7338 * register the offset computed into the register specified
7342 sappend(s
, xfer_to_a(cstate
, inst
));
7343 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7344 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7346 s
= xfer_to_x(cstate
, inst
);
7349 * Load the item at the sum of the offset we've put in the
7350 * X register and the offset of the start of the link
7351 * layer header (which is 0 if the radio header is
7352 * variable-length; that header length is what we put
7353 * into the X register and then added to the index).
7355 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7356 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7358 sappend(inst
->s
, s
);
7372 * The offset is relative to the beginning of
7373 * the network-layer header.
7374 * XXX - are there any cases where we want
7375 * cstate->off_nl_nosnap?
7377 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7380 * If "s" is non-null, it has code to arrange that the
7381 * X register contains the variable part of the offset
7382 * of the link-layer payload. Add to it the offset
7383 * computed into the register specified by "index",
7384 * and move that into the X register. Otherwise, just
7385 * load into the X register the offset computed into
7386 * the register specified by "index".
7389 sappend(s
, xfer_to_a(cstate
, inst
));
7390 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7391 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7393 s
= xfer_to_x(cstate
, inst
);
7396 * Load the item at the sum of the offset we've put in the
7397 * X register, the offset of the start of the network
7398 * layer header from the beginning of the link-layer
7399 * payload, and the constant part of the offset of the
7400 * start of the link-layer payload.
7402 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7403 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7405 sappend(inst
->s
, s
);
7408 * Do the computation only if the packet contains
7409 * the protocol in question.
7411 b
= gen_proto_abbrev_internal(cstate
, proto
);
7413 gen_and(inst
->b
, b
);
7427 * The offset is relative to the beginning of
7428 * the transport-layer header.
7430 * Load the X register with the length of the IPv4 header
7431 * (plus the offset of the link-layer header, if it's
7432 * a variable-length header), in bytes.
7434 * XXX - are there any cases where we want
7435 * cstate->off_nl_nosnap?
7436 * XXX - we should, if we're built with
7437 * IPv6 support, generate code to load either
7438 * IPv4, IPv6, or both, as appropriate.
7440 s
= gen_loadx_iphdrlen(cstate
);
7443 * The X register now contains the sum of the variable
7444 * part of the offset of the link-layer payload and the
7445 * length of the network-layer header.
7447 * Load into the A register the offset relative to
7448 * the beginning of the transport layer header,
7449 * add the X register to that, move that to the
7450 * X register, and load with an offset from the
7451 * X register equal to the sum of the constant part of
7452 * the offset of the link-layer payload and the offset,
7453 * relative to the beginning of the link-layer payload,
7454 * of the network-layer header.
7456 sappend(s
, xfer_to_a(cstate
, inst
));
7457 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7458 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7459 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7460 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7461 sappend(inst
->s
, s
);
7464 * Do the computation only if the packet contains
7465 * the protocol in question - which is true only
7466 * if this is an IP datagram and is the first or
7467 * only fragment of that datagram.
7469 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7471 gen_and(inst
->b
, b
);
7472 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7477 * Do the computation only if the packet contains
7478 * the protocol in question.
7480 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7482 gen_and(inst
->b
, b
);
7487 * Check if we have an icmp6 next header
7489 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7491 gen_and(inst
->b
, b
);
7496 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7498 * If "s" is non-null, it has code to arrange that the
7499 * X register contains the variable part of the offset
7500 * of the link-layer payload. Add to it the offset
7501 * computed into the register specified by "index",
7502 * and move that into the X register. Otherwise, just
7503 * load into the X register the offset computed into
7504 * the register specified by "index".
7507 sappend(s
, xfer_to_a(cstate
, inst
));
7508 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7509 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7511 s
= xfer_to_x(cstate
, inst
);
7515 * Load the item at the sum of the offset we've put in the
7516 * X register, the offset of the start of the network
7517 * layer header from the beginning of the link-layer
7518 * payload, and the constant part of the offset of the
7519 * start of the link-layer payload.
7521 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7522 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7525 sappend(inst
->s
, s
);
7529 inst
->regno
= regno
;
7530 s
= new_stmt(cstate
, BPF_ST
);
7532 sappend(inst
->s
, s
);
7538 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7541 * Catch errors reported by us and routines below us, and return NULL
7544 if (setjmp(cstate
->top_ctx
))
7547 return gen_load_internal(cstate
, proto
, inst
, size
);
7550 static struct block
*
7551 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7552 struct arth
*a1
, int reversed
)
7554 struct slist
*s0
, *s1
, *s2
;
7555 struct block
*b
, *tmp
;
7557 s0
= xfer_to_x(cstate
, a1
);
7558 s1
= xfer_to_a(cstate
, a0
);
7559 if (code
== BPF_JEQ
) {
7560 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7561 b
= new_block(cstate
, JMP(code
));
7565 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7571 sappend(a0
->s
, a1
->s
);
7575 free_reg(cstate
, a0
->regno
);
7576 free_reg(cstate
, a1
->regno
);
7578 /* 'and' together protocol checks */
7581 gen_and(a0
->b
, tmp
= a1
->b
);
7595 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7596 struct arth
*a1
, int reversed
)
7599 * Catch errors reported by us and routines below us, and return NULL
7602 if (setjmp(cstate
->top_ctx
))
7605 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7609 gen_loadlen(compiler_state_t
*cstate
)
7616 * Catch errors reported by us and routines below us, and return NULL
7619 if (setjmp(cstate
->top_ctx
))
7622 regno
= alloc_reg(cstate
);
7623 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7624 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7625 s
->next
= new_stmt(cstate
, BPF_ST
);
7626 s
->next
->s
.k
= regno
;
7633 static struct arth
*
7634 gen_loadi_internal(compiler_state_t
*cstate
, int val
)
7640 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7642 reg
= alloc_reg(cstate
);
7644 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7646 s
->next
= new_stmt(cstate
, BPF_ST
);
7655 gen_loadi(compiler_state_t
*cstate
, int val
)
7658 * Catch errors reported by us and routines below us, and return NULL
7661 if (setjmp(cstate
->top_ctx
))
7664 return gen_loadi_internal(cstate
, val
);
7668 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7673 * Catch errors reported by us and routines below us, and return NULL
7676 if (setjmp(cstate
->top_ctx
))
7679 s
= xfer_to_a(cstate
, a
);
7681 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7684 s
= new_stmt(cstate
, BPF_ST
);
7692 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7695 struct slist
*s0
, *s1
, *s2
;
7698 * Catch errors reported by us and routines below us, and return NULL
7701 if (setjmp(cstate
->top_ctx
))
7705 * Disallow division by, or modulus by, zero; we do this here
7706 * so that it gets done even if the optimizer is disabled.
7708 * Also disallow shifts by a value greater than 31; we do this
7709 * here, for the same reason.
7711 if (code
== BPF_DIV
) {
7712 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7713 bpf_error(cstate
, "division by zero");
7714 } else if (code
== BPF_MOD
) {
7715 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7716 bpf_error(cstate
, "modulus by zero");
7717 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7719 * XXX - we need to make up our minds as to what integers
7720 * are signed and what integers are unsigned in BPF programs
7723 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) &&
7724 (a1
->s
->s
.k
< 0 || a1
->s
->s
.k
> 31))
7725 bpf_error(cstate
, "shift by more than 31 bits");
7727 s0
= xfer_to_x(cstate
, a1
);
7728 s1
= xfer_to_a(cstate
, a0
);
7729 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7734 sappend(a0
->s
, a1
->s
);
7736 free_reg(cstate
, a0
->regno
);
7737 free_reg(cstate
, a1
->regno
);
7739 s0
= new_stmt(cstate
, BPF_ST
);
7740 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7747 * Initialize the table of used registers and the current register.
7750 init_regs(compiler_state_t
*cstate
)
7753 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7757 * Return the next free register.
7760 alloc_reg(compiler_state_t
*cstate
)
7762 int n
= BPF_MEMWORDS
;
7765 if (cstate
->regused
[cstate
->curreg
])
7766 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7768 cstate
->regused
[cstate
->curreg
] = 1;
7769 return cstate
->curreg
;
7772 bpf_error(cstate
, "too many registers needed to evaluate expression");
7777 * Return a register to the table so it can
7781 free_reg(compiler_state_t
*cstate
, int n
)
7783 cstate
->regused
[n
] = 0;
7786 static struct block
*
7787 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7792 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7793 b
= new_block(cstate
, JMP(jmp
));
7801 gen_greater(compiler_state_t
*cstate
, int n
)
7804 * Catch errors reported by us and routines below us, and return NULL
7807 if (setjmp(cstate
->top_ctx
))
7810 return gen_len(cstate
, BPF_JGE
, n
);
7814 * Actually, this is less than or equal.
7817 gen_less(compiler_state_t
*cstate
, int n
)
7822 * Catch errors reported by us and routines below us, and return NULL
7825 if (setjmp(cstate
->top_ctx
))
7828 b
= gen_len(cstate
, BPF_JGT
, n
);
7835 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7836 * the beginning of the link-layer header.
7837 * XXX - that means you can't test values in the radiotap header, but
7838 * as that header is difficult if not impossible to parse generally
7839 * without a loop, that might not be a severe problem. A new keyword
7840 * "radio" could be added for that, although what you'd really want
7841 * would be a way of testing particular radio header values, which
7842 * would generate code appropriate to the radio header in question.
7845 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7851 * Catch errors reported by us and routines below us, and return NULL
7854 if (setjmp(cstate
->top_ctx
))
7862 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7865 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7869 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7873 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7877 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7881 b
= new_block(cstate
, JMP(BPF_JEQ
));
7888 static const u_char abroadcast
[] = { 0x0 };
7891 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7893 bpf_u_int32 hostmask
;
7894 struct block
*b0
, *b1
, *b2
;
7895 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7898 * Catch errors reported by us and routines below us, and return NULL
7901 if (setjmp(cstate
->top_ctx
))
7908 switch (cstate
->linktype
) {
7910 case DLT_ARCNET_LINUX
:
7911 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7913 case DLT_NETANALYZER
:
7914 case DLT_NETANALYZER_TRANSPARENT
:
7915 b1
= gen_prevlinkhdr_check(cstate
);
7916 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7921 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7923 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7924 case DLT_IEEE802_11
:
7925 case DLT_PRISM_HEADER
:
7926 case DLT_IEEE802_11_RADIO_AVS
:
7927 case DLT_IEEE802_11_RADIO
:
7929 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7930 case DLT_IP_OVER_FC
:
7931 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7933 bpf_error(cstate
, "not a broadcast link");
7939 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7940 * as an indication that we don't know the netmask, and fail
7943 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7944 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7945 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7946 hostmask
= ~cstate
->netmask
;
7947 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7948 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7949 (bpf_int32
)(~0 & hostmask
), hostmask
);
7954 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7959 * Generate code to test the low-order bit of a MAC address (that's
7960 * the bottom bit of the *first* byte).
7962 static struct block
*
7963 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7965 register struct block
*b0
;
7966 register struct slist
*s
;
7968 /* link[offset] & 1 != 0 */
7969 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7970 b0
= new_block(cstate
, JMP(BPF_JSET
));
7977 gen_multicast(compiler_state_t
*cstate
, int proto
)
7979 register struct block
*b0
, *b1
, *b2
;
7980 register struct slist
*s
;
7983 * Catch errors reported by us and routines below us, and return NULL
7986 if (setjmp(cstate
->top_ctx
))
7993 switch (cstate
->linktype
) {
7995 case DLT_ARCNET_LINUX
:
7996 /* all ARCnet multicasts use the same address */
7997 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7999 case DLT_NETANALYZER
:
8000 case DLT_NETANALYZER_TRANSPARENT
:
8001 b1
= gen_prevlinkhdr_check(cstate
);
8002 /* ether[0] & 1 != 0 */
8003 b0
= gen_mac_multicast(cstate
, 0);
8009 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8011 * XXX - was that referring to bit-order issues?
8013 /* fddi[1] & 1 != 0 */
8014 return gen_mac_multicast(cstate
, 1);
8016 /* tr[2] & 1 != 0 */
8017 return gen_mac_multicast(cstate
, 2);
8018 case DLT_IEEE802_11
:
8019 case DLT_PRISM_HEADER
:
8020 case DLT_IEEE802_11_RADIO_AVS
:
8021 case DLT_IEEE802_11_RADIO
:
8026 * For control frames, there is no DA.
8028 * For management frames, DA is at an
8029 * offset of 4 from the beginning of
8032 * For data frames, DA is at an offset
8033 * of 4 from the beginning of the packet
8034 * if To DS is clear and at an offset of
8035 * 16 from the beginning of the packet
8040 * Generate the tests to be done for data frames.
8042 * First, check for To DS set, i.e. "link[1] & 0x01".
8044 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8045 b1
= new_block(cstate
, JMP(BPF_JSET
));
8046 b1
->s
.k
= 0x01; /* To DS */
8050 * If To DS is set, the DA is at 16.
8052 b0
= gen_mac_multicast(cstate
, 16);
8056 * Now, check for To DS not set, i.e. check
8057 * "!(link[1] & 0x01)".
8059 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8060 b2
= new_block(cstate
, JMP(BPF_JSET
));
8061 b2
->s
.k
= 0x01; /* To DS */
8066 * If To DS is not set, the DA is at 4.
8068 b1
= gen_mac_multicast(cstate
, 4);
8072 * Now OR together the last two checks. That gives
8073 * the complete set of checks for data frames.
8078 * Now check for a data frame.
8079 * I.e, check "link[0] & 0x08".
8081 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8082 b1
= new_block(cstate
, JMP(BPF_JSET
));
8087 * AND that with the checks done for data frames.
8092 * If the high-order bit of the type value is 0, this
8093 * is a management frame.
8094 * I.e, check "!(link[0] & 0x08)".
8096 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8097 b2
= new_block(cstate
, JMP(BPF_JSET
));
8103 * For management frames, the DA is at 4.
8105 b1
= gen_mac_multicast(cstate
, 4);
8109 * OR that with the checks done for data frames.
8110 * That gives the checks done for management and
8116 * If the low-order bit of the type value is 1,
8117 * this is either a control frame or a frame
8118 * with a reserved type, and thus not a
8121 * I.e., check "!(link[0] & 0x04)".
8123 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8124 b1
= new_block(cstate
, JMP(BPF_JSET
));
8130 * AND that with the checks for data and management
8135 case DLT_IP_OVER_FC
:
8136 b0
= gen_mac_multicast(cstate
, 2);
8141 /* Link not known to support multicasts */
8145 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8146 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
8151 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8152 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
8156 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8161 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8162 * Outbound traffic is sent by this machine, while inbound traffic is
8163 * sent by a remote machine (and may include packets destined for a
8164 * unicast or multicast link-layer address we are not subscribing to).
8165 * These are the same definitions implemented by pcap_setdirection().
8166 * Capturing only unicast traffic destined for this host is probably
8167 * better accomplished using a higher-layer filter.
8170 gen_inbound(compiler_state_t
*cstate
, int dir
)
8172 register struct block
*b0
;
8175 * Catch errors reported by us and routines below us, and return NULL
8178 if (setjmp(cstate
->top_ctx
))
8182 * Only some data link types support inbound/outbound qualifiers.
8184 switch (cstate
->linktype
) {
8186 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8187 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8188 gen_loadi_internal(cstate
, 0),
8194 /* match outgoing packets */
8195 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8197 /* match incoming packets */
8198 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8203 /* match outgoing packets */
8204 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8206 /* to filter on inbound traffic, invert the match */
8211 case DLT_LINUX_SLL2
:
8212 /* match outgoing packets */
8213 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8215 /* to filter on inbound traffic, invert the match */
8220 #ifdef HAVE_NET_PFVAR_H
8222 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8223 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
8229 /* match outgoing packets */
8230 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8232 /* match incoming packets */
8233 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8237 case DLT_JUNIPER_MFR
:
8238 case DLT_JUNIPER_MLFR
:
8239 case DLT_JUNIPER_MLPPP
:
8240 case DLT_JUNIPER_ATM1
:
8241 case DLT_JUNIPER_ATM2
:
8242 case DLT_JUNIPER_PPPOE
:
8243 case DLT_JUNIPER_PPPOE_ATM
:
8244 case DLT_JUNIPER_GGSN
:
8245 case DLT_JUNIPER_ES
:
8246 case DLT_JUNIPER_MONITOR
:
8247 case DLT_JUNIPER_SERVICES
:
8248 case DLT_JUNIPER_ETHER
:
8249 case DLT_JUNIPER_PPP
:
8250 case DLT_JUNIPER_FRELAY
:
8251 case DLT_JUNIPER_CHDLC
:
8252 case DLT_JUNIPER_VP
:
8253 case DLT_JUNIPER_ST
:
8254 case DLT_JUNIPER_ISM
:
8255 case DLT_JUNIPER_VS
:
8256 case DLT_JUNIPER_SRX_E2E
:
8257 case DLT_JUNIPER_FIBRECHANNEL
:
8258 case DLT_JUNIPER_ATM_CEMIC
:
8260 /* juniper flags (including direction) are stored
8261 * the byte after the 3-byte magic number */
8263 /* match outgoing packets */
8264 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8266 /* match incoming packets */
8267 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8273 * If we have packet meta-data indicating a direction,
8274 * and that metadata can be checked by BPF code, check
8275 * it. Otherwise, give up, as this link-layer type has
8276 * nothing in the packet data.
8278 * Currently, the only platform where a BPF filter can
8279 * check that metadata is Linux with the in-kernel
8280 * BPF interpreter. If other packet capture mechanisms
8281 * and BPF filters also supported this, it would be
8282 * nice. It would be even better if they made that
8283 * metadata available so that we could provide it
8284 * with newer capture APIs, allowing it to be saved
8287 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
8289 * This is Linux with PF_PACKET support.
8290 * If this is a *live* capture, we can look at
8291 * special meta-data in the filter expression;
8292 * if it's a savefile, we can't.
8294 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8295 /* We have a FILE *, so this is a savefile */
8296 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
8301 /* match outgoing packets */
8302 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8305 /* to filter on inbound traffic, invert the match */
8308 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8309 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
8312 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8317 #ifdef HAVE_NET_PFVAR_H
8318 /* PF firewall log matched interface */
8320 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8326 * Catch errors reported by us and routines below us, and return NULL
8329 if (setjmp(cstate
->top_ctx
))
8332 if (cstate
->linktype
!= DLT_PFLOG
) {
8333 bpf_error(cstate
, "ifname supported only on PF linktype");
8336 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8337 off
= offsetof(struct pfloghdr
, ifname
);
8338 if (strlen(ifname
) >= len
) {
8339 bpf_error(cstate
, "ifname interface names can only be %d characters",
8343 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
8347 /* PF firewall log ruleset name */
8349 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8354 * Catch errors reported by us and routines below us, and return NULL
8357 if (setjmp(cstate
->top_ctx
))
8360 if (cstate
->linktype
!= DLT_PFLOG
) {
8361 bpf_error(cstate
, "ruleset supported only on PF linktype");
8365 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8366 bpf_error(cstate
, "ruleset names can only be %ld characters",
8367 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8371 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8372 strlen(ruleset
), (const u_char
*)ruleset
);
8376 /* PF firewall log rule number */
8378 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8383 * Catch errors reported by us and routines below us, and return NULL
8386 if (setjmp(cstate
->top_ctx
))
8389 if (cstate
->linktype
!= DLT_PFLOG
) {
8390 bpf_error(cstate
, "rnr supported only on PF linktype");
8394 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8399 /* PF firewall log sub-rule number */
8401 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8406 * Catch errors reported by us and routines below us, and return NULL
8409 if (setjmp(cstate
->top_ctx
))
8412 if (cstate
->linktype
!= DLT_PFLOG
) {
8413 bpf_error(cstate
, "srnr supported only on PF linktype");
8417 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8422 /* PF firewall log reason code */
8424 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8429 * Catch errors reported by us and routines below us, and return NULL
8432 if (setjmp(cstate
->top_ctx
))
8435 if (cstate
->linktype
!= DLT_PFLOG
) {
8436 bpf_error(cstate
, "reason supported only on PF linktype");
8440 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8445 /* PF firewall log action */
8447 gen_pf_action(compiler_state_t
*cstate
, int action
)
8452 * Catch errors reported by us and routines below us, and return NULL
8455 if (setjmp(cstate
->top_ctx
))
8458 if (cstate
->linktype
!= DLT_PFLOG
) {
8459 bpf_error(cstate
, "action supported only on PF linktype");
8463 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8467 #else /* !HAVE_NET_PFVAR_H */
8469 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8472 * Catch errors reported by us and routines below us, and return NULL
8475 if (setjmp(cstate
->top_ctx
))
8478 bpf_error(cstate
, "libpcap was compiled without pf support");
8483 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8486 * Catch errors reported by us and routines below us, and return NULL
8489 if (setjmp(cstate
->top_ctx
))
8492 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8497 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8500 * Catch errors reported by us and routines below us, and return NULL
8503 if (setjmp(cstate
->top_ctx
))
8506 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8511 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8514 * Catch errors reported by us and routines below us, and return NULL
8517 if (setjmp(cstate
->top_ctx
))
8520 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8525 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8528 * Catch errors reported by us and routines below us, and return NULL
8531 if (setjmp(cstate
->top_ctx
))
8534 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8539 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8542 * Catch errors reported by us and routines below us, and return NULL
8545 if (setjmp(cstate
->top_ctx
))
8548 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8551 #endif /* HAVE_NET_PFVAR_H */
8553 /* IEEE 802.11 wireless header */
8555 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
8560 * Catch errors reported by us and routines below us, and return NULL
8563 if (setjmp(cstate
->top_ctx
))
8566 switch (cstate
->linktype
) {
8568 case DLT_IEEE802_11
:
8569 case DLT_PRISM_HEADER
:
8570 case DLT_IEEE802_11_RADIO_AVS
:
8571 case DLT_IEEE802_11_RADIO
:
8572 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8577 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8585 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8590 * Catch errors reported by us and routines below us, and return NULL
8593 if (setjmp(cstate
->top_ctx
))
8596 switch (cstate
->linktype
) {
8598 case DLT_IEEE802_11
:
8599 case DLT_PRISM_HEADER
:
8600 case DLT_IEEE802_11_RADIO_AVS
:
8601 case DLT_IEEE802_11_RADIO
:
8605 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8609 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8610 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8616 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8621 * Catch errors reported by us and routines below us, and return NULL
8624 if (setjmp(cstate
->top_ctx
))
8627 switch (cstate
->linktype
) {
8630 case DLT_ARCNET_LINUX
:
8631 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8632 q
.proto
== Q_LINK
) {
8633 cstate
->e
= pcap_ether_aton(s
);
8634 if (cstate
->e
== NULL
)
8635 bpf_error(cstate
, "malloc");
8636 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8641 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8647 bpf_error(cstate
, "aid supported only on ARCnet");
8652 static struct block
*
8653 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8655 register struct block
*b0
, *b1
;
8658 /* src comes first, different from Ethernet */
8660 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8663 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8666 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8667 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8673 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8674 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8679 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8683 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8687 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8691 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8695 bpf_error(cstate
, "'ra' is only supported on 802.11");
8699 bpf_error(cstate
, "'ta' is only supported on 802.11");
8706 static struct block
*
8707 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8709 struct block
*b0
, *b1
;
8711 /* check for VLAN, including QinQ */
8712 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8713 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8716 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8722 static struct block
*
8723 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8725 if (vlan_num
> 0x0fff) {
8726 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8729 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8732 static struct block
*
8733 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8736 struct block
*b0
, *b1
;
8738 b0
= gen_vlan_tpid_test(cstate
);
8741 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8747 * Both payload and link header type follow the VLAN tags so that
8748 * both need to be updated.
8750 cstate
->off_linkpl
.constant_part
+= 4;
8751 cstate
->off_linktype
.constant_part
+= 4;
8756 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8757 /* add v to variable part of off */
8759 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8763 if (!off
->is_variable
)
8764 off
->is_variable
= 1;
8766 off
->reg
= alloc_reg(cstate
);
8768 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8771 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8774 s2
= new_stmt(cstate
, BPF_ST
);
8780 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8781 * and link type offsets first
8784 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8788 /* offset determined at run time, shift variable part */
8790 cstate
->is_vlan_vloffset
= 1;
8791 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8792 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8794 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8795 sappend(s
.next
, b_tpid
->head
->stmts
);
8796 b_tpid
->head
->stmts
= s
.next
;
8800 * patch block b_vid (VLAN id test) to load VID value either from packet
8801 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8804 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8806 struct slist
*s
, *s2
, *sjeq
;
8809 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8810 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8812 /* true -> next instructions, false -> beginning of b_vid */
8813 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8815 sjeq
->s
.jf
= b_vid
->stmts
;
8818 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8819 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8823 /* Jump to the test in b_vid. We need to jump one instruction before
8824 * the end of the b_vid block so that we only skip loading the TCI
8825 * from packet data and not the 'and' instruction extractging VID.
8828 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8830 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8834 /* insert our statements at the beginning of b_vid */
8835 sappend(s
, b_vid
->stmts
);
8840 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8841 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8842 * tag can be either in metadata or in packet data; therefore if the
8843 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8844 * header for VLAN tag. As the decision is done at run time, we need
8845 * update variable part of the offsets
8847 static struct block
*
8848 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8851 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8854 /* generate new filter code based on extracting packet
8856 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8857 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8859 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8864 * This is tricky. We need to insert the statements updating variable
8865 * parts of offsets before the the traditional TPID and VID tests so
8866 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8867 * we do not want this update to affect those checks. That's why we
8868 * generate both test blocks first and insert the statements updating
8869 * variable parts of both offsets after that. This wouldn't work if
8870 * there already were variable length link header when entering this
8871 * function but gen_vlan_bpf_extensions() isn't called in that case.
8873 b_tpid
= gen_vlan_tpid_test(cstate
);
8875 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8877 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8882 gen_vlan_patch_vid_test(cstate
, b_vid
);
8892 * support IEEE 802.1Q VLAN trunk over ethernet
8895 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8900 * Catch errors reported by us and routines below us, and return NULL
8903 if (setjmp(cstate
->top_ctx
))
8906 /* can't check for VLAN-encapsulated packets inside MPLS */
8907 if (cstate
->label_stack_depth
> 0)
8908 bpf_error(cstate
, "no VLAN match after MPLS");
8911 * Check for a VLAN packet, and then change the offsets to point
8912 * to the type and data fields within the VLAN packet. Just
8913 * increment the offsets, so that we can support a hierarchy, e.g.
8914 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8917 * XXX - this is a bit of a kludge. If we were to split the
8918 * compiler into a parser that parses an expression and
8919 * generates an expression tree, and a code generator that
8920 * takes an expression tree (which could come from our
8921 * parser or from some other parser) and generates BPF code,
8922 * we could perhaps make the offsets parameters of routines
8923 * and, in the handler for an "AND" node, pass to subnodes
8924 * other than the VLAN node the adjusted offsets.
8926 * This would mean that "vlan" would, instead of changing the
8927 * behavior of *all* tests after it, change only the behavior
8928 * of tests ANDed with it. That would change the documented
8929 * semantics of "vlan", which might break some expressions.
8930 * However, it would mean that "(vlan and ip) or ip" would check
8931 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8932 * checking only for VLAN-encapsulated IP, so that could still
8933 * be considered worth doing; it wouldn't break expressions
8934 * that are of the form "vlan and ..." or "vlan N and ...",
8935 * which I suspect are the most common expressions involving
8936 * "vlan". "vlan or ..." doesn't necessarily do what the user
8937 * would really want, now, as all the "or ..." tests would
8938 * be done assuming a VLAN, even though the "or" could be viewed
8939 * as meaning "or, if this isn't a VLAN packet...".
8941 switch (cstate
->linktype
) {
8944 case DLT_NETANALYZER
:
8945 case DLT_NETANALYZER_TRANSPARENT
:
8946 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8947 /* Verify that this is the outer part of the packet and
8948 * not encapsulated somehow. */
8949 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8950 cstate
->off_linkhdr
.constant_part
==
8951 cstate
->off_outermostlinkhdr
.constant_part
) {
8953 * Do we need special VLAN handling?
8955 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8956 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8959 b0
= gen_vlan_no_bpf_extensions(cstate
,
8960 vlan_num
, has_vlan_tag
);
8963 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8967 case DLT_IEEE802_11
:
8968 case DLT_PRISM_HEADER
:
8969 case DLT_IEEE802_11_RADIO_AVS
:
8970 case DLT_IEEE802_11_RADIO
:
8971 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8975 bpf_error(cstate
, "no VLAN support for data link type %d",
8980 cstate
->vlan_stack_depth
++;
8989 gen_mpls(compiler_state_t
*cstate
, bpf_u_int32 label_num
, int has_label_num
)
8991 struct block
*b0
, *b1
;
8994 * Catch errors reported by us and routines below us, and return NULL
8997 if (setjmp(cstate
->top_ctx
))
9000 if (cstate
->label_stack_depth
> 0) {
9001 /* just match the bottom-of-stack bit clear */
9002 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9005 * We're not in an MPLS stack yet, so check the link-layer
9006 * type against MPLS.
9008 switch (cstate
->linktype
) {
9010 case DLT_C_HDLC
: /* fall through */
9012 case DLT_NETANALYZER
:
9013 case DLT_NETANALYZER_TRANSPARENT
:
9014 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9018 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9021 /* FIXME add other DLT_s ...
9022 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9023 * leave it for now */
9026 bpf_error(cstate
, "no MPLS support for data link type %d",
9033 /* If a specific MPLS label is requested, check it */
9034 if (has_label_num
) {
9035 if (label_num
> 0xFFFFF) {
9036 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9037 label_num
, 0xFFFFF);
9039 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9040 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
9041 0xfffff000); /* only compare the first 20 bits */
9047 * Change the offsets to point to the type and data fields within
9048 * the MPLS packet. Just increment the offsets, so that we
9049 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9050 * capture packets with an outer label of 100000 and an inner
9053 * Increment the MPLS stack depth as well; this indicates that
9054 * we're checking MPLS-encapsulated headers, to make sure higher
9055 * level code generators don't try to match against IP-related
9056 * protocols such as Q_ARP, Q_RARP etc.
9058 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9060 cstate
->off_nl_nosnap
+= 4;
9061 cstate
->off_nl
+= 4;
9062 cstate
->label_stack_depth
++;
9067 * Support PPPOE discovery and session.
9070 gen_pppoed(compiler_state_t
*cstate
)
9073 * Catch errors reported by us and routines below us, and return NULL
9076 if (setjmp(cstate
->top_ctx
))
9079 /* check for PPPoE discovery */
9080 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
9084 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9086 struct block
*b0
, *b1
;
9089 * Catch errors reported by us and routines below us, and return NULL
9092 if (setjmp(cstate
->top_ctx
))
9096 * Test against the PPPoE session link-layer type.
9098 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
9100 /* If a specific session is requested, check PPPoE session id */
9102 if (sess_num
> 0x0000ffff) {
9103 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9104 sess_num
, 0x0000ffff);
9106 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
9107 (bpf_int32
)sess_num
, 0x0000ffff);
9113 * Change the offsets to point to the type and data fields within
9114 * the PPP packet, and note that this is PPPoE rather than
9117 * XXX - this is a bit of a kludge. If we were to split the
9118 * compiler into a parser that parses an expression and
9119 * generates an expression tree, and a code generator that
9120 * takes an expression tree (which could come from our
9121 * parser or from some other parser) and generates BPF code,
9122 * we could perhaps make the offsets parameters of routines
9123 * and, in the handler for an "AND" node, pass to subnodes
9124 * other than the PPPoE node the adjusted offsets.
9126 * This would mean that "pppoes" would, instead of changing the
9127 * behavior of *all* tests after it, change only the behavior
9128 * of tests ANDed with it. That would change the documented
9129 * semantics of "pppoes", which might break some expressions.
9130 * However, it would mean that "(pppoes and ip) or ip" would check
9131 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9132 * checking only for VLAN-encapsulated IP, so that could still
9133 * be considered worth doing; it wouldn't break expressions
9134 * that are of the form "pppoes and ..." which I suspect are the
9135 * most common expressions involving "pppoes". "pppoes or ..."
9136 * doesn't necessarily do what the user would really want, now,
9137 * as all the "or ..." tests would be done assuming PPPoE, even
9138 * though the "or" could be viewed as meaning "or, if this isn't
9139 * a PPPoE packet...".
9141 * The "network-layer" protocol is PPPoE, which has a 6-byte
9142 * PPPoE header, followed by a PPP packet.
9144 * There is no HDLC encapsulation for the PPP packet (it's
9145 * encapsulated in PPPoES instead), so the link-layer type
9146 * starts at the first byte of the PPP packet. For PPPoE,
9147 * that offset is relative to the beginning of the total
9148 * link-layer payload, including any 802.2 LLC header, so
9149 * it's 6 bytes past cstate->off_nl.
9151 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9152 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9153 cstate
->off_linkpl
.reg
);
9155 cstate
->off_linktype
= cstate
->off_linkhdr
;
9156 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9159 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9164 /* Check that this is Geneve and the VNI is correct if
9165 * specified. Parameterized to handle both IPv4 and IPv6. */
9166 static struct block
*
9167 gen_geneve_check(compiler_state_t
*cstate
,
9168 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
9169 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9171 struct block
*b0
, *b1
;
9173 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9175 /* Check that we are operating on version 0. Otherwise, we
9176 * can't decode the rest of the fields. The version is 2 bits
9177 * in the first byte of the Geneve header. */
9178 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
9183 if (vni
> 0xffffff) {
9184 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9187 vni
<<= 8; /* VNI is in the upper 3 bytes */
9188 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
9197 /* The IPv4 and IPv6 Geneve checks need to do two things:
9198 * - Verify that this actually is Geneve with the right VNI.
9199 * - Place the IP header length (plus variable link prefix if
9200 * needed) into register A to be used later to compute
9201 * the inner packet offsets. */
9202 static struct block
*
9203 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9205 struct block
*b0
, *b1
;
9206 struct slist
*s
, *s1
;
9208 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9210 /* Load the IP header length into A. */
9211 s
= gen_loadx_iphdrlen(cstate
);
9213 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9216 /* Forcibly append these statements to the true condition
9217 * of the protocol check by creating a new block that is
9218 * always true and ANDing them. */
9219 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9228 static struct block
*
9229 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9231 struct block
*b0
, *b1
;
9232 struct slist
*s
, *s1
;
9234 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9236 /* Load the IP header length. We need to account for a
9237 * variable length link prefix if there is one. */
9238 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9240 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9244 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9248 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9252 /* Forcibly append these statements to the true condition
9253 * of the protocol check by creating a new block that is
9254 * always true and ANDing them. */
9255 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9258 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9267 /* We need to store three values based on the Geneve header::
9268 * - The offset of the linktype.
9269 * - The offset of the end of the Geneve header.
9270 * - The offset of the end of the encapsulated MAC header. */
9271 static struct slist
*
9272 gen_geneve_offsets(compiler_state_t
*cstate
)
9274 struct slist
*s
, *s1
, *s_proto
;
9276 /* First we need to calculate the offset of the Geneve header
9277 * itself. This is composed of the IP header previously calculated
9278 * (include any variable link prefix) and stored in A plus the
9279 * fixed sized headers (fixed link prefix, MAC length, and UDP
9281 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9282 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9284 /* Stash this in X since we'll need it later. */
9285 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9288 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9290 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9294 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9295 cstate
->off_linktype
.is_variable
= 1;
9296 cstate
->off_linktype
.constant_part
= 0;
9298 s1
= new_stmt(cstate
, BPF_ST
);
9299 s1
->s
.k
= cstate
->off_linktype
.reg
;
9302 /* Load the Geneve option length and mask and shift to get the
9303 * number of bytes. It is stored in the first byte of the Geneve
9305 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9309 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9313 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9317 /* Add in the rest of the Geneve base header. */
9318 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9322 /* Add the Geneve header length to its offset and store. */
9323 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9327 /* Set the encapsulated type as Ethernet. Even though we may
9328 * not actually have Ethernet inside there are two reasons this
9330 * - The linktype field is always in EtherType format regardless
9331 * of whether it is in Geneve or an inner Ethernet frame.
9332 * - The only link layer that we have specific support for is
9333 * Ethernet. We will confirm that the packet actually is
9334 * Ethernet at runtime before executing these checks. */
9335 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9337 s1
= new_stmt(cstate
, BPF_ST
);
9338 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9341 /* Calculate whether we have an Ethernet header or just raw IP/
9342 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9343 * and linktype by 14 bytes so that the network header can be found
9344 * seamlessly. Otherwise, keep what we've calculated already. */
9346 /* We have a bare jmp so we can't use the optimizer. */
9347 cstate
->no_optimize
= 1;
9349 /* Load the EtherType in the Geneve header, 2 bytes in. */
9350 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9354 /* Load X with the end of the Geneve header. */
9355 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9356 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9359 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9360 * end of this check, we should have the total length in X. In
9361 * the non-Ethernet case, it's already there. */
9362 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9363 s_proto
->s
.k
= ETHERTYPE_TEB
;
9364 sappend(s
, s_proto
);
9366 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9370 /* Since this is Ethernet, use the EtherType of the payload
9371 * directly as the linktype. Overwrite what we already have. */
9372 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9376 s1
= new_stmt(cstate
, BPF_ST
);
9377 s1
->s
.k
= cstate
->off_linktype
.reg
;
9380 /* Advance two bytes further to get the end of the Ethernet
9382 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9386 /* Move the result to X. */
9387 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9390 /* Store the final result of our linkpl calculation. */
9391 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9392 cstate
->off_linkpl
.is_variable
= 1;
9393 cstate
->off_linkpl
.constant_part
= 0;
9395 s1
= new_stmt(cstate
, BPF_STX
);
9396 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9405 /* Check to see if this is a Geneve packet. */
9407 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9409 struct block
*b0
, *b1
;
9413 * Catch errors reported by us and routines below us, and return NULL
9416 if (setjmp(cstate
->top_ctx
))
9419 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9420 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9425 /* Later filters should act on the payload of the Geneve frame,
9426 * update all of the header pointers. Attach this code so that
9427 * it gets executed in the event that the Geneve filter matches. */
9428 s
= gen_geneve_offsets(cstate
);
9430 b1
= gen_true(cstate
);
9431 sappend(s
, b1
->stmts
);
9436 cstate
->is_geneve
= 1;
9441 /* Check that the encapsulated frame has a link layer header
9442 * for Ethernet filters. */
9443 static struct block
*
9444 gen_geneve_ll_check(compiler_state_t
*cstate
)
9447 struct slist
*s
, *s1
;
9449 /* The easiest way to see if there is a link layer present
9450 * is to check if the link layer header and payload are not
9453 /* Geneve always generates pure variable offsets so we can
9454 * compare only the registers. */
9455 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9456 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9458 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9459 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9462 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9470 static struct block
*
9471 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9472 bpf_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9479 if (!cstate
->is_atm
)
9480 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9481 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9483 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
9488 if (!cstate
->is_atm
)
9489 bpf_error(cstate
, "'vci' supported only on raw ATM");
9490 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9492 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
9497 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9498 abort(); /* XXX - this isn't on FreeBSD */
9499 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
9504 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9506 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9507 0xffffffff, jtype
, reverse
, jvalue
);
9511 if (!cstate
->is_atm
)
9512 bpf_error(cstate
, "'callref' supported only on raw ATM");
9513 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9515 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
9516 jtype
, reverse
, jvalue
);
9525 static struct block
*
9526 gen_atmtype_metac(compiler_state_t
*cstate
)
9528 struct block
*b0
, *b1
;
9530 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9531 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9536 static struct block
*
9537 gen_atmtype_sc(compiler_state_t
*cstate
)
9539 struct block
*b0
, *b1
;
9541 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9542 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9547 static struct block
*
9548 gen_atmtype_llc(compiler_state_t
*cstate
)
9552 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9553 cstate
->linktype
= cstate
->prevlinktype
;
9558 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9559 bpf_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9562 * Catch errors reported by us and routines below us, and return NULL
9565 if (setjmp(cstate
->top_ctx
))
9568 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9573 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9575 struct block
*b0
, *b1
;
9578 * Catch errors reported by us and routines below us, and return NULL
9581 if (setjmp(cstate
->top_ctx
))
9587 /* Get all packets in Meta signalling Circuit */
9588 if (!cstate
->is_atm
)
9589 bpf_error(cstate
, "'metac' supported only on raw ATM");
9590 b1
= gen_atmtype_metac(cstate
);
9594 /* Get all packets in Broadcast Circuit*/
9595 if (!cstate
->is_atm
)
9596 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9597 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9598 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9603 /* Get all cells in Segment OAM F4 circuit*/
9604 if (!cstate
->is_atm
)
9605 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9606 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9607 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9612 /* Get all cells in End-to-End OAM F4 Circuit*/
9613 if (!cstate
->is_atm
)
9614 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9615 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9616 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9621 /* Get all packets in connection Signalling Circuit */
9622 if (!cstate
->is_atm
)
9623 bpf_error(cstate
, "'sc' supported only on raw ATM");
9624 b1
= gen_atmtype_sc(cstate
);
9628 /* Get all packets in ILMI Circuit */
9629 if (!cstate
->is_atm
)
9630 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9631 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9632 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9637 /* Get all LANE packets */
9638 if (!cstate
->is_atm
)
9639 bpf_error(cstate
, "'lane' supported only on raw ATM");
9640 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9643 * Arrange that all subsequent tests assume LANE
9644 * rather than LLC-encapsulated packets, and set
9645 * the offsets appropriately for LANE-encapsulated
9648 * We assume LANE means Ethernet, not Token Ring.
9650 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9651 cstate
->off_payload
+ 2, /* Ethernet header */
9653 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9654 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9655 cstate
->off_nl
= 0; /* Ethernet II */
9656 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9660 /* Get all LLC-encapsulated packets */
9661 if (!cstate
->is_atm
)
9662 bpf_error(cstate
, "'llc' supported only on raw ATM");
9663 b1
= gen_atmtype_llc(cstate
);
9673 * Filtering for MTP2 messages based on li value
9674 * FISU, length is null
9675 * LSSU, length is 1 or 2
9676 * MSU, length is 3 or more
9677 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9680 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9682 struct block
*b0
, *b1
;
9685 * Catch errors reported by us and routines below us, and return NULL
9688 if (setjmp(cstate
->top_ctx
))
9694 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9695 (cstate
->linktype
!= DLT_ERF
) &&
9696 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9697 bpf_error(cstate
, "'fisu' supported only on MTP2");
9698 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9699 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
9703 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9704 (cstate
->linktype
!= DLT_ERF
) &&
9705 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9706 bpf_error(cstate
, "'lssu' supported only on MTP2");
9707 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
9708 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9713 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9714 (cstate
->linktype
!= DLT_ERF
) &&
9715 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9716 bpf_error(cstate
, "'msu' supported only on MTP2");
9717 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9721 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9722 (cstate
->linktype
!= DLT_ERF
) &&
9723 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9724 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9725 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9726 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9730 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9731 (cstate
->linktype
!= DLT_ERF
) &&
9732 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9733 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9734 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9735 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9740 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9741 (cstate
->linktype
!= DLT_ERF
) &&
9742 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9743 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9744 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9754 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
, bpf_u_int32 jvalue
,
9755 bpf_u_int32 jtype
, int reverse
)
9758 bpf_u_int32 val1
, val2
, val3
;
9765 * Catch errors reported by us and routines below us, and return NULL
9768 if (setjmp(cstate
->top_ctx
))
9771 newoff_sio
= cstate
->off_sio
;
9772 newoff_opc
= cstate
->off_opc
;
9773 newoff_dpc
= cstate
->off_dpc
;
9774 newoff_sls
= cstate
->off_sls
;
9775 switch (mtp3field
) {
9778 newoff_sio
+= 3; /* offset for MTP2_HSL */
9782 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9783 bpf_error(cstate
, "'sio' supported only on SS7");
9784 /* sio coded on 1 byte so max value 255 */
9786 bpf_error(cstate
, "sio value %u too big; max value = 255",
9788 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9789 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9795 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9796 bpf_error(cstate
, "'opc' supported only on SS7");
9797 /* opc coded on 14 bits so max value 16383 */
9799 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9801 /* the following instructions are made to convert jvalue
9802 * to the form used to write opc in an ss7 message*/
9803 val1
= jvalue
& 0x00003c00;
9805 val2
= jvalue
& 0x000003fc;
9807 val3
= jvalue
& 0x00000003;
9809 jvalue
= val1
+ val2
+ val3
;
9810 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9811 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9819 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9820 bpf_error(cstate
, "'dpc' supported only on SS7");
9821 /* dpc coded on 14 bits so max value 16383 */
9823 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9825 /* the following instructions are made to convert jvalue
9826 * to the forme used to write dpc in an ss7 message*/
9827 val1
= jvalue
& 0x000000ff;
9829 val2
= jvalue
& 0x00003f00;
9831 jvalue
= val1
+ val2
;
9832 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9833 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9839 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9840 bpf_error(cstate
, "'sls' supported only on SS7");
9841 /* sls coded on 4 bits so max value 15 */
9843 bpf_error(cstate
, "sls value %u too big; max value = 15",
9845 /* the following instruction is made to convert jvalue
9846 * to the forme used to write sls in an ss7 message*/
9847 jvalue
= jvalue
<< 4;
9848 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9849 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9858 static struct block
*
9859 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9864 * Q.2931 signalling protocol messages for handling virtual circuits
9865 * establishment and teardown
9870 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9874 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9878 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9882 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9886 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9889 case A_RELEASE_DONE
:
9890 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9900 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9902 struct block
*b0
, *b1
;
9905 * Catch errors reported by us and routines below us, and return NULL
9908 if (setjmp(cstate
->top_ctx
))
9914 if (!cstate
->is_atm
)
9915 bpf_error(cstate
, "'oam' supported only on raw ATM");
9917 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9918 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9920 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9925 if (!cstate
->is_atm
)
9926 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9928 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9929 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9931 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9937 * Get Q.2931 signalling messages for switched
9938 * virtual connection
9940 if (!cstate
->is_atm
)
9941 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9942 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9943 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9945 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9947 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9949 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9951 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9953 b0
= gen_atmtype_sc(cstate
);
9958 if (!cstate
->is_atm
)
9959 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9960 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9961 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9963 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9965 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9967 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9969 b0
= gen_atmtype_metac(cstate
);