1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 #include <pcap-types.h>
31 #include <sys/socket.h>
34 #include <sys/param.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
55 #include "ethertype.h"
59 #include "ieee80211.h"
61 #include "sunatmpos.h"
64 #include "pcap/ipnet.h"
70 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
71 #include <linux/types.h>
72 #include <linux/if_packet.h>
73 #include <linux/filter.h>
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
89 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
96 uint16_t u6_addr16
[8];
97 uint32_t u6_addr32
[4];
99 #define s6_addr in6_u.u6_addr8
100 #define s6_addr16 in6_u.u6_addr16
101 #define s6_addr32 in6_u.u6_addr32
102 #define s6_addr64 in6_u.u6_addr64
105 typedef unsigned short sa_family_t
;
107 #define __SOCKADDR_COMMON(sa_prefix) \
108 sa_family_t sa_prefix##family
110 /* Ditto, for IPv6. */
113 __SOCKADDR_COMMON (sin6_
);
114 uint16_t sin6_port
; /* Transport layer port # */
115 uint32_t sin6_flowinfo
; /* IPv6 flow information */
116 struct in6_addr sin6_addr
; /* IPv6 address */
119 #ifndef EAI_ADDRFAMILY
121 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
122 int ai_family
; /* PF_xxx */
123 int ai_socktype
; /* SOCK_xxx */
124 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
125 size_t ai_addrlen
; /* length of ai_addr */
126 char *ai_canonname
; /* canonical name for hostname */
127 struct sockaddr
*ai_addr
; /* binary address */
128 struct addrinfo
*ai_next
; /* next structure in linked list */
130 #endif /* EAI_ADDRFAMILY */
131 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
134 #include <netdb.h> /* for "struct addrinfo" */
136 #include <pcap/namedb.h>
138 #include "nametoaddr.h"
140 #define ETHERMTU 1500
142 #ifndef ETHERTYPE_TEB
143 #define ETHERTYPE_TEB 0x6558
146 #ifndef IPPROTO_HOPOPTS
147 #define IPPROTO_HOPOPTS 0
149 #ifndef IPPROTO_ROUTING
150 #define IPPROTO_ROUTING 43
152 #ifndef IPPROTO_FRAGMENT
153 #define IPPROTO_FRAGMENT 44
155 #ifndef IPPROTO_DSTOPTS
156 #define IPPROTO_DSTOPTS 60
159 #define IPPROTO_SCTP 132
162 #define GENEVE_PORT 6081
164 #ifdef HAVE_OS_PROTO_H
165 #include "os-proto.h"
168 #define JMP(c) ((c)|BPF_JMP|BPF_K)
171 * "Push" the current value of the link-layer header type and link-layer
172 * header offset onto a "stack", and set a new value. (It's not a
173 * full-blown stack; we keep only the top two items.)
175 #define PUSH_LINKHDR(cs, new_linktype, new_is_variable, new_constant_part, new_reg) \
177 (cs)->prevlinktype = (cs)->linktype; \
178 (cs)->off_prevlinkhdr = (cs)->off_linkhdr; \
179 (cs)->linktype = (new_linktype); \
180 (cs)->off_linkhdr.is_variable = (new_is_variable); \
181 (cs)->off_linkhdr.constant_part = (new_constant_part); \
182 (cs)->off_linkhdr.reg = (new_reg); \
183 (cs)->is_geneve = 0; \
187 * Offset "not set" value.
189 #define OFFSET_NOT_SET 0xffffffffU
192 * Absolute offsets, which are offsets from the beginning of the raw
193 * packet data, are, in the general case, the sum of a variable value
194 * and a constant value; the variable value may be absent, in which
195 * case the offset is only the constant value, and the constant value
196 * may be zero, in which case the offset is only the variable value.
198 * bpf_abs_offset is a structure containing all that information:
200 * is_variable is 1 if there's a variable part.
202 * constant_part is the constant part of the value, possibly zero;
204 * if is_variable is 1, reg is the register number for a register
205 * containing the variable value if the register has been assigned,
215 * Value passed to gen_load_a() to indicate what the offset argument
216 * is relative to the beginning of.
219 OR_PACKET
, /* full packet data */
220 OR_LINKHDR
, /* link-layer header */
221 OR_PREVLINKHDR
, /* previous link-layer header */
222 OR_LLC
, /* 802.2 LLC header */
223 OR_PREVMPLSHDR
, /* previous MPLS header */
224 OR_LINKTYPE
, /* link-layer type */
225 OR_LINKPL
, /* link-layer payload */
226 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
227 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
228 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
232 * We divy out chunks of memory rather than call malloc each time so
233 * we don't have to worry about leaking memory. It's probably
234 * not a big deal if all this memory was wasted but if this ever
235 * goes into a library that would probably not be a good idea.
237 * XXX - this *is* in a library....
240 #define CHUNK0SIZE 1024
246 /* Code generator state */
248 struct _compiler_state
{
258 int outermostlinktype
;
263 /* Hack for handling VLAN and MPLS stacks. */
264 u_int label_stack_depth
;
265 u_int vlan_stack_depth
;
271 * As errors are handled by a longjmp, anything allocated must
272 * be freed in the longjmp handler, so it must be reachable
275 * One thing that's allocated is the result of pcap_nametoaddrinfo();
276 * it must be freed with freeaddrinfo(). This variable points to
277 * any addrinfo structure that would need to be freed.
282 * Another thing that's allocated is the result of pcap_ether_aton();
283 * it must be freed with free(). This variable points to any
284 * address that would need to be freed.
289 * Various code constructs need to know the layout of the packet.
290 * These values give the necessary offsets from the beginning
291 * of the packet data.
295 * Absolute offset of the beginning of the link-layer header.
297 bpf_abs_offset off_linkhdr
;
300 * If we're checking a link-layer header for a packet encapsulated
301 * in another protocol layer, this is the equivalent information
302 * for the previous layers' link-layer header from the beginning
303 * of the raw packet data.
305 bpf_abs_offset off_prevlinkhdr
;
308 * This is the equivalent information for the outermost layers'
311 bpf_abs_offset off_outermostlinkhdr
;
314 * Absolute offset of the beginning of the link-layer payload.
316 bpf_abs_offset off_linkpl
;
319 * "off_linktype" is the offset to information in the link-layer
320 * header giving the packet type. This is an absolute offset
321 * from the beginning of the packet.
323 * For Ethernet, it's the offset of the Ethernet type field; this
324 * means that it must have a value that skips VLAN tags.
326 * For link-layer types that always use 802.2 headers, it's the
327 * offset of the LLC header; this means that it must have a value
328 * that skips VLAN tags.
330 * For PPP, it's the offset of the PPP type field.
332 * For Cisco HDLC, it's the offset of the CHDLC type field.
334 * For BSD loopback, it's the offset of the AF_ value.
336 * For Linux cooked sockets, it's the offset of the type field.
338 * off_linktype.constant_part is set to OFFSET_NOT_SET for no
339 * encapsulation, in which case, IP is assumed.
341 bpf_abs_offset off_linktype
;
344 * TRUE if the link layer includes an ATM pseudo-header.
349 * TRUE if "geneve" appeared in the filter; it causes us to
350 * generate code that checks for a Geneve header and assume
351 * that later filters apply to the encapsulated payload.
356 * TRUE if we need variable length part of VLAN offset
358 int is_vlan_vloffset
;
361 * These are offsets for the ATM pseudo-header.
368 * These are offsets for the MTP2 fields.
374 * These are offsets for the MTP3 fields.
382 * This is the offset of the first byte after the ATM pseudo_header,
383 * or -1 if there is no ATM pseudo-header.
388 * These are offsets to the beginning of the network-layer header.
389 * They are relative to the beginning of the link-layer payload
390 * (i.e., they don't include off_linkhdr.constant_part or
391 * off_linkpl.constant_part).
393 * If the link layer never uses 802.2 LLC:
395 * "off_nl" and "off_nl_nosnap" are the same.
397 * If the link layer always uses 802.2 LLC:
399 * "off_nl" is the offset if there's a SNAP header following
402 * "off_nl_nosnap" is the offset if there's no SNAP header.
404 * If the link layer is Ethernet:
406 * "off_nl" is the offset if the packet is an Ethernet II packet
407 * (we assume no 802.3+802.2+SNAP);
409 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
410 * with an 802.2 header following it.
416 * Here we handle simple allocation of the scratch registers.
417 * If too many registers are alloc'd, the allocator punts.
419 int regused
[BPF_MEMWORDS
];
425 struct chunk chunks
[NCHUNKS
];
430 * For use by routines outside this file.
434 bpf_set_error(compiler_state_t
*cstate
, const char *fmt
, ...)
439 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
445 * For use *ONLY* in routines in this file.
447 static void PCAP_NORETURN
bpf_error(compiler_state_t
*, const char *, ...)
448 PCAP_PRINTFLIKE(2, 3);
451 static void PCAP_NORETURN
452 bpf_error(compiler_state_t
*cstate
, const char *fmt
, ...)
457 (void)pcap_vsnprintf(cstate
->bpf_pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
460 longjmp(cstate
->top_ctx
, 1);
464 static int init_linktype(compiler_state_t
*, pcap_t
*);
466 static void init_regs(compiler_state_t
*);
467 static int alloc_reg(compiler_state_t
*);
468 static void free_reg(compiler_state_t
*, int);
470 static void initchunks(compiler_state_t
*cstate
);
471 static void *newchunk_nolongjmp(compiler_state_t
*cstate
, size_t);
472 static void *newchunk(compiler_state_t
*cstate
, size_t);
473 static void freechunks(compiler_state_t
*cstate
);
474 static inline struct block
*new_block(compiler_state_t
*cstate
, int);
475 static inline struct slist
*new_stmt(compiler_state_t
*cstate
, int);
476 static struct block
*gen_retblk(compiler_state_t
*cstate
, int);
477 static inline void syntax(compiler_state_t
*cstate
);
479 static void backpatch(struct block
*, struct block
*);
480 static void merge(struct block
*, struct block
*);
481 static struct block
*gen_cmp(compiler_state_t
*, enum e_offrel
, u_int
,
483 static struct block
*gen_cmp_gt(compiler_state_t
*, enum e_offrel
, u_int
,
485 static struct block
*gen_cmp_ge(compiler_state_t
*, enum e_offrel
, u_int
,
487 static struct block
*gen_cmp_lt(compiler_state_t
*, enum e_offrel
, u_int
,
489 static struct block
*gen_cmp_le(compiler_state_t
*, enum e_offrel
, u_int
,
491 static struct block
*gen_mcmp(compiler_state_t
*, enum e_offrel
, u_int
,
492 u_int
, bpf_int32
, bpf_u_int32
);
493 static struct block
*gen_bcmp(compiler_state_t
*, enum e_offrel
, u_int
,
494 u_int
, const u_char
*);
495 static struct block
*gen_ncmp(compiler_state_t
*, enum e_offrel
, bpf_u_int32
,
496 bpf_u_int32
, bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
497 static struct slist
*gen_load_absoffsetrel(compiler_state_t
*, bpf_abs_offset
*,
499 static struct slist
*gen_load_a(compiler_state_t
*, enum e_offrel
, u_int
,
501 static struct slist
*gen_loadx_iphdrlen(compiler_state_t
*);
502 static struct block
*gen_uncond(compiler_state_t
*, int);
503 static inline struct block
*gen_true(compiler_state_t
*);
504 static inline struct block
*gen_false(compiler_state_t
*);
505 static struct block
*gen_ether_linktype(compiler_state_t
*, int);
506 static struct block
*gen_ipnet_linktype(compiler_state_t
*, int);
507 static struct block
*gen_linux_sll_linktype(compiler_state_t
*, int);
508 static struct slist
*gen_load_prism_llprefixlen(compiler_state_t
*);
509 static struct slist
*gen_load_avs_llprefixlen(compiler_state_t
*);
510 static struct slist
*gen_load_radiotap_llprefixlen(compiler_state_t
*);
511 static struct slist
*gen_load_ppi_llprefixlen(compiler_state_t
*);
512 static void insert_compute_vloffsets(compiler_state_t
*, struct block
*);
513 static struct slist
*gen_abs_offset_varpart(compiler_state_t
*,
515 static int ethertype_to_ppptype(int);
516 static struct block
*gen_linktype(compiler_state_t
*, int);
517 static struct block
*gen_snap(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
);
518 static struct block
*gen_llc_linktype(compiler_state_t
*, int);
519 static struct block
*gen_hostop(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
520 int, int, u_int
, u_int
);
522 static struct block
*gen_hostop6(compiler_state_t
*, struct in6_addr
*,
523 struct in6_addr
*, int, int, u_int
, u_int
);
525 static struct block
*gen_ahostop(compiler_state_t
*, const u_char
*, int);
526 static struct block
*gen_ehostop(compiler_state_t
*, const u_char
*, int);
527 static struct block
*gen_fhostop(compiler_state_t
*, const u_char
*, int);
528 static struct block
*gen_thostop(compiler_state_t
*, const u_char
*, int);
529 static struct block
*gen_wlanhostop(compiler_state_t
*, const u_char
*, int);
530 static struct block
*gen_ipfchostop(compiler_state_t
*, const u_char
*, int);
531 static struct block
*gen_dnhostop(compiler_state_t
*, bpf_u_int32
, int);
532 static struct block
*gen_mpls_linktype(compiler_state_t
*, int);
533 static struct block
*gen_host(compiler_state_t
*, bpf_u_int32
, bpf_u_int32
,
536 static struct block
*gen_host6(compiler_state_t
*, struct in6_addr
*,
537 struct in6_addr
*, int, int, int);
540 static struct block
*gen_gateway(compiler_state_t
*, const u_char
*,
541 struct addrinfo
*, int, int);
543 static struct block
*gen_ipfrag(compiler_state_t
*);
544 static struct block
*gen_portatom(compiler_state_t
*, int, bpf_int32
);
545 static struct block
*gen_portrangeatom(compiler_state_t
*, int, bpf_int32
,
547 static struct block
*gen_portatom6(compiler_state_t
*, int, bpf_int32
);
548 static struct block
*gen_portrangeatom6(compiler_state_t
*, int, bpf_int32
,
550 struct block
*gen_portop(compiler_state_t
*, int, int, int);
551 static struct block
*gen_port(compiler_state_t
*, int, int, int);
552 struct block
*gen_portrangeop(compiler_state_t
*, int, int, int, int);
553 static struct block
*gen_portrange(compiler_state_t
*, int, int, int, int);
554 struct block
*gen_portop6(compiler_state_t
*, int, int, int);
555 static struct block
*gen_port6(compiler_state_t
*, int, int, int);
556 struct block
*gen_portrangeop6(compiler_state_t
*, int, int, int, int);
557 static struct block
*gen_portrange6(compiler_state_t
*, int, int, int, int);
558 static int lookup_proto(compiler_state_t
*, const char *, int);
559 static struct block
*gen_protochain(compiler_state_t
*, int, int, int);
560 static struct block
*gen_proto(compiler_state_t
*, int, int, int);
561 static struct slist
*xfer_to_x(compiler_state_t
*, struct arth
*);
562 static struct slist
*xfer_to_a(compiler_state_t
*, struct arth
*);
563 static struct block
*gen_mac_multicast(compiler_state_t
*, int);
564 static struct block
*gen_len(compiler_state_t
*, int, int);
565 static struct block
*gen_check_802_11_data_frame(compiler_state_t
*);
566 static struct block
*gen_geneve_ll_check(compiler_state_t
*cstate
);
568 static struct block
*gen_ppi_dlt_check(compiler_state_t
*);
569 static struct block
*gen_atmfield_code_internal(compiler_state_t
*, int,
570 bpf_int32
, bpf_u_int32
, int);
571 static struct block
*gen_atmtype_llc(compiler_state_t
*);
572 static struct block
*gen_msg_abbrev(compiler_state_t
*, int type
);
575 initchunks(compiler_state_t
*cstate
)
579 for (i
= 0; i
< NCHUNKS
; i
++) {
580 cstate
->chunks
[i
].n_left
= 0;
581 cstate
->chunks
[i
].m
= NULL
;
583 cstate
->cur_chunk
= 0;
587 newchunk_nolongjmp(compiler_state_t
*cstate
, size_t n
)
594 /* XXX Round up to nearest long. */
595 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
597 /* XXX Round up to structure boundary. */
601 cp
= &cstate
->chunks
[cstate
->cur_chunk
];
602 if (n
> cp
->n_left
) {
604 k
= ++cstate
->cur_chunk
;
606 bpf_set_error(cstate
, "out of memory");
609 size
= CHUNK0SIZE
<< k
;
610 cp
->m
= (void *)malloc(size
);
612 bpf_set_error(cstate
, "out of memory");
615 memset((char *)cp
->m
, 0, size
);
618 bpf_set_error(cstate
, "out of memory");
623 return (void *)((char *)cp
->m
+ cp
->n_left
);
627 newchunk(compiler_state_t
*cstate
, size_t n
)
631 p
= newchunk_nolongjmp(cstate
, n
);
633 longjmp(cstate
->top_ctx
, 1);
640 freechunks(compiler_state_t
*cstate
)
644 for (i
= 0; i
< NCHUNKS
; ++i
)
645 if (cstate
->chunks
[i
].m
!= NULL
)
646 free(cstate
->chunks
[i
].m
);
650 * A strdup whose allocations are freed after code generation is over.
651 * This is used by the lexical analyzer, so it can't longjmp; it just
652 * returns NULL on an allocation error, and the callers must check
656 sdup(compiler_state_t
*cstate
, const char *s
)
658 size_t n
= strlen(s
) + 1;
659 char *cp
= newchunk_nolongjmp(cstate
, n
);
663 pcap_strlcpy(cp
, s
, n
);
667 static inline struct block
*
668 new_block(compiler_state_t
*cstate
, int code
)
672 p
= (struct block
*)newchunk(cstate
, sizeof(*p
));
679 static inline struct slist
*
680 new_stmt(compiler_state_t
*cstate
, int code
)
684 p
= (struct slist
*)newchunk(cstate
, sizeof(*p
));
690 static struct block
*
691 gen_retblk(compiler_state_t
*cstate
, int v
)
693 struct block
*b
= new_block(cstate
, BPF_RET
|BPF_K
);
699 static inline PCAP_NORETURN_DEF
void
700 syntax(compiler_state_t
*cstate
)
702 bpf_error(cstate
, "syntax error in filter expression");
706 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
707 const char *buf
, int optimize
, bpf_u_int32 mask
)
712 compiler_state_t cstate
;
713 const char * volatile xbuf
= buf
;
714 yyscan_t scanner
= NULL
;
715 volatile YY_BUFFER_STATE in_buffer
= NULL
;
720 * If this pcap_t hasn't been activated, it doesn't have a
721 * link-layer type, so we can't use it.
724 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
725 "not-yet-activated pcap_t passed to pcap_compile");
737 * If the device on which we're capturing need to be notified
738 * that a new filter is being compiled, do so.
740 * This allows them to save a copy of it, in case, for example,
741 * they're implementing a form of remote packet capture, and
742 * want the remote machine to filter out the packets in which
743 * it's sending the packets it's captured.
745 * XXX - the fact that we happen to be compiling a filter
746 * doesn't necessarily mean we'll be installing it as the
747 * filter for this pcap_t; we might be running it from userland
748 * on captured packets to do packet classification. We really
749 * need a better way of handling this, but this is all that
750 * the WinPcap code did.
752 if (p
->save_current_filter_op
!= NULL
)
753 (p
->save_current_filter_op
)(p
, buf
);
757 cstate
.no_optimize
= 0;
762 cstate
.ic
.root
= NULL
;
763 cstate
.ic
.cur_mark
= 0;
767 cstate
.netmask
= mask
;
769 cstate
.snaplen
= pcap_snapshot(p
);
770 if (cstate
.snaplen
== 0) {
771 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
772 "snaplen of 0 rejects all packets");
777 if (pcap_lex_init(&scanner
) != 0)
778 pcap_fmt_errmsg_for_errno(p
->errbuf
, PCAP_ERRBUF_SIZE
,
779 errno
, "can't initialize scanner");
780 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
783 * Associate the compiler state with the lexical analyzer
786 pcap_set_extra(&cstate
, scanner
);
788 if (init_linktype(&cstate
, p
) == -1) {
792 if (pcap_parse(scanner
, &cstate
) != 0) {
794 if (cstate
.ai
!= NULL
)
795 freeaddrinfo(cstate
.ai
);
797 if (cstate
.e
!= NULL
)
803 if (cstate
.ic
.root
== NULL
) {
805 * Catch errors reported by gen_retblk().
807 if (setjmp(cstate
.top_ctx
)) {
811 cstate
.ic
.root
= gen_retblk(&cstate
, cstate
.snaplen
);
814 if (optimize
&& !cstate
.no_optimize
) {
815 if (bpf_optimize(&cstate
.ic
, p
->errbuf
) == -1) {
820 if (cstate
.ic
.root
== NULL
||
821 (cstate
.ic
.root
->s
.code
== (BPF_RET
|BPF_K
) && cstate
.ic
.root
->s
.k
== 0)) {
822 (void)pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
823 "expression rejects all packets");
828 program
->bf_insns
= icode_to_fcode(&cstate
.ic
,
829 cstate
.ic
.root
, &len
, p
->errbuf
);
830 if (program
->bf_insns
== NULL
) {
835 program
->bf_len
= len
;
837 rc
= 0; /* We're all okay */
841 * Clean up everything for the lexical analyzer.
843 if (in_buffer
!= NULL
)
844 pcap__delete_buffer(in_buffer
, scanner
);
846 pcap_lex_destroy(scanner
);
849 * Clean up our own allocated memory.
857 * entry point for using the compiler with no pcap open
858 * pass in all the stuff that is needed explicitly instead.
861 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
862 struct bpf_program
*program
,
863 const char *buf
, int optimize
, bpf_u_int32 mask
)
868 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
871 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
877 * Clean up a "struct bpf_program" by freeing all the memory allocated
881 pcap_freecode(struct bpf_program
*program
)
884 if (program
->bf_insns
!= NULL
) {
885 free((char *)program
->bf_insns
);
886 program
->bf_insns
= NULL
;
891 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
892 * which of the jt and jf fields has been resolved and which is a pointer
893 * back to another unresolved block (or nil). At least one of the fields
894 * in each block is already resolved.
897 backpatch(struct block
*list
, struct block
*target
)
914 * Merge the lists in b0 and b1, using the 'sense' field to indicate
915 * which of jt and jf is the link.
918 merge(struct block
*b0
, struct block
*b1
)
920 register struct block
**p
= &b0
;
922 /* Find end of list. */
924 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
926 /* Concatenate the lists. */
931 finish_parse(compiler_state_t
*cstate
, struct block
*p
)
933 struct block
*ppi_dlt_check
;
936 * Catch errors reported by us and routines below us, and return -1
939 if (setjmp(cstate
->top_ctx
))
943 * Insert before the statements of the first (root) block any
944 * statements needed to load the lengths of any variable-length
945 * headers into registers.
947 * XXX - a fancier strategy would be to insert those before the
948 * statements of all blocks that use those lengths and that
949 * have no predecessors that use them, so that we only compute
950 * the lengths if we need them. There might be even better
951 * approaches than that.
953 * However, those strategies would be more complicated, and
954 * as we don't generate code to compute a length if the
955 * program has no tests that use the length, and as most
956 * tests will probably use those lengths, we would just
957 * postpone computing the lengths so that it's not done
958 * for tests that fail early, and it's not clear that's
961 insert_compute_vloffsets(cstate
, p
->head
);
964 * For DLT_PPI captures, generate a check of the per-packet
965 * DLT value to make sure it's DLT_IEEE802_11.
967 * XXX - TurboCap cards use DLT_PPI for Ethernet.
968 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
969 * with appropriate Ethernet information and use that rather
970 * than using something such as DLT_PPI where you don't know
971 * the link-layer header type until runtime, which, in the
972 * general case, would force us to generate both Ethernet *and*
973 * 802.11 code (*and* anything else for which PPI is used)
974 * and choose between them early in the BPF program?
976 ppi_dlt_check
= gen_ppi_dlt_check(cstate
);
977 if (ppi_dlt_check
!= NULL
)
978 gen_and(ppi_dlt_check
, p
);
980 backpatch(p
, gen_retblk(cstate
, cstate
->snaplen
));
981 p
->sense
= !p
->sense
;
982 backpatch(p
, gen_retblk(cstate
, 0));
983 cstate
->ic
.root
= p
->head
;
988 gen_and(struct block
*b0
, struct block
*b1
)
990 backpatch(b0
, b1
->head
);
991 b0
->sense
= !b0
->sense
;
992 b1
->sense
= !b1
->sense
;
994 b1
->sense
= !b1
->sense
;
999 gen_or(struct block
*b0
, struct block
*b1
)
1001 b0
->sense
= !b0
->sense
;
1002 backpatch(b0
, b1
->head
);
1003 b0
->sense
= !b0
->sense
;
1005 b1
->head
= b0
->head
;
1009 gen_not(struct block
*b
)
1011 b
->sense
= !b
->sense
;
1014 static struct block
*
1015 gen_cmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1016 u_int size
, bpf_int32 v
)
1018 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
1021 static struct block
*
1022 gen_cmp_gt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1023 u_int size
, bpf_int32 v
)
1025 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
1028 static struct block
*
1029 gen_cmp_ge(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1030 u_int size
, bpf_int32 v
)
1032 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
1035 static struct block
*
1036 gen_cmp_lt(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1037 u_int size
, bpf_int32 v
)
1039 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
1042 static struct block
*
1043 gen_cmp_le(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1044 u_int size
, bpf_int32 v
)
1046 return gen_ncmp(cstate
, offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
1049 static struct block
*
1050 gen_mcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1051 u_int size
, bpf_int32 v
, bpf_u_int32 mask
)
1053 return gen_ncmp(cstate
, offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
1056 static struct block
*
1057 gen_bcmp(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1058 u_int size
, const u_char
*v
)
1060 register struct block
*b
, *tmp
;
1063 * XXX - the actual *instructions* do unsigned comparisons on
1064 * most platforms, and the load instructions don't do sign
1065 * extension, so gen_cmp() should really take an unsigned
1068 * As the load instructons also don't do sign-extension, we
1069 * fetch the values from the byte array as unsigned. We don't
1070 * want to use the signed versions of the extract calls.
1074 register const u_char
*p
= &v
[size
- 4];
1076 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 4, BPF_W
,
1077 (bpf_int32
)EXTRACT_BE_U_4(p
));
1084 register const u_char
*p
= &v
[size
- 2];
1086 tmp
= gen_cmp(cstate
, offrel
, offset
+ size
- 2, BPF_H
,
1087 (bpf_int32
)EXTRACT_BE_U_2(p
));
1094 tmp
= gen_cmp(cstate
, offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
1103 * AND the field of size "size" at offset "offset" relative to the header
1104 * specified by "offrel" with "mask", and compare it with the value "v"
1105 * with the test specified by "jtype"; if "reverse" is true, the test
1106 * should test the opposite of "jtype".
1108 static struct block
*
1109 gen_ncmp(compiler_state_t
*cstate
, enum e_offrel offrel
, bpf_u_int32 offset
,
1110 bpf_u_int32 size
, bpf_u_int32 mask
, bpf_u_int32 jtype
, int reverse
,
1113 struct slist
*s
, *s2
;
1116 s
= gen_load_a(cstate
, offrel
, offset
, size
);
1118 if (mask
!= 0xffffffff) {
1119 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1124 b
= new_block(cstate
, JMP(jtype
));
1127 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
1133 init_linktype(compiler_state_t
*cstate
, pcap_t
*p
)
1135 cstate
->pcap_fddipad
= p
->fddipad
;
1138 * We start out with only one link-layer header.
1140 cstate
->outermostlinktype
= pcap_datalink(p
);
1141 cstate
->off_outermostlinkhdr
.constant_part
= 0;
1142 cstate
->off_outermostlinkhdr
.is_variable
= 0;
1143 cstate
->off_outermostlinkhdr
.reg
= -1;
1145 cstate
->prevlinktype
= cstate
->outermostlinktype
;
1146 cstate
->off_prevlinkhdr
.constant_part
= 0;
1147 cstate
->off_prevlinkhdr
.is_variable
= 0;
1148 cstate
->off_prevlinkhdr
.reg
= -1;
1150 cstate
->linktype
= cstate
->outermostlinktype
;
1151 cstate
->off_linkhdr
.constant_part
= 0;
1152 cstate
->off_linkhdr
.is_variable
= 0;
1153 cstate
->off_linkhdr
.reg
= -1;
1158 cstate
->off_linkpl
.constant_part
= 0;
1159 cstate
->off_linkpl
.is_variable
= 0;
1160 cstate
->off_linkpl
.reg
= -1;
1162 cstate
->off_linktype
.constant_part
= 0;
1163 cstate
->off_linktype
.is_variable
= 0;
1164 cstate
->off_linktype
.reg
= -1;
1167 * Assume it's not raw ATM with a pseudo-header, for now.
1170 cstate
->off_vpi
= OFFSET_NOT_SET
;
1171 cstate
->off_vci
= OFFSET_NOT_SET
;
1172 cstate
->off_proto
= OFFSET_NOT_SET
;
1173 cstate
->off_payload
= OFFSET_NOT_SET
;
1178 cstate
->is_geneve
= 0;
1181 * No variable length VLAN offset by default
1183 cstate
->is_vlan_vloffset
= 0;
1186 * And assume we're not doing SS7.
1188 cstate
->off_li
= OFFSET_NOT_SET
;
1189 cstate
->off_li_hsl
= OFFSET_NOT_SET
;
1190 cstate
->off_sio
= OFFSET_NOT_SET
;
1191 cstate
->off_opc
= OFFSET_NOT_SET
;
1192 cstate
->off_dpc
= OFFSET_NOT_SET
;
1193 cstate
->off_sls
= OFFSET_NOT_SET
;
1195 cstate
->label_stack_depth
= 0;
1196 cstate
->vlan_stack_depth
= 0;
1198 switch (cstate
->linktype
) {
1201 cstate
->off_linktype
.constant_part
= 2;
1202 cstate
->off_linkpl
.constant_part
= 6;
1203 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1204 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1207 case DLT_ARCNET_LINUX
:
1208 cstate
->off_linktype
.constant_part
= 4;
1209 cstate
->off_linkpl
.constant_part
= 8;
1210 cstate
->off_nl
= 0; /* XXX in reality, variable! */
1211 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1215 cstate
->off_linktype
.constant_part
= 12;
1216 cstate
->off_linkpl
.constant_part
= 14; /* Ethernet header length */
1217 cstate
->off_nl
= 0; /* Ethernet II */
1218 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1223 * SLIP doesn't have a link level type. The 16 byte
1224 * header is hacked into our SLIP driver.
1226 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1227 cstate
->off_linkpl
.constant_part
= 16;
1229 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1232 case DLT_SLIP_BSDOS
:
1233 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1234 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1236 cstate
->off_linkpl
.constant_part
= 24;
1238 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1243 cstate
->off_linktype
.constant_part
= 0;
1244 cstate
->off_linkpl
.constant_part
= 4;
1246 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1250 cstate
->off_linktype
.constant_part
= 0;
1251 cstate
->off_linkpl
.constant_part
= 12;
1253 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1258 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1259 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1260 cstate
->off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1261 cstate
->off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1263 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1268 * This does no include the Ethernet header, and
1269 * only covers session state.
1271 cstate
->off_linktype
.constant_part
= 6;
1272 cstate
->off_linkpl
.constant_part
= 8;
1274 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1278 cstate
->off_linktype
.constant_part
= 5;
1279 cstate
->off_linkpl
.constant_part
= 24;
1281 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1286 * FDDI doesn't really have a link-level type field.
1287 * We set "off_linktype" to the offset of the LLC header.
1289 * To check for Ethernet types, we assume that SSAP = SNAP
1290 * is being used and pick out the encapsulated Ethernet type.
1291 * XXX - should we generate code to check for SNAP?
1293 cstate
->off_linktype
.constant_part
= 13;
1294 cstate
->off_linktype
.constant_part
+= cstate
->pcap_fddipad
;
1295 cstate
->off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1296 cstate
->off_linkpl
.constant_part
+= cstate
->pcap_fddipad
;
1297 cstate
->off_nl
= 8; /* 802.2+SNAP */
1298 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1303 * Token Ring doesn't really have a link-level type field.
1304 * We set "off_linktype" to the offset of the LLC header.
1306 * To check for Ethernet types, we assume that SSAP = SNAP
1307 * is being used and pick out the encapsulated Ethernet type.
1308 * XXX - should we generate code to check for SNAP?
1310 * XXX - the header is actually variable-length.
1311 * Some various Linux patched versions gave 38
1312 * as "off_linktype" and 40 as "off_nl"; however,
1313 * if a token ring packet has *no* routing
1314 * information, i.e. is not source-routed, the correct
1315 * values are 20 and 22, as they are in the vanilla code.
1317 * A packet is source-routed iff the uppermost bit
1318 * of the first byte of the source address, at an
1319 * offset of 8, has the uppermost bit set. If the
1320 * packet is source-routed, the total number of bytes
1321 * of routing information is 2 plus bits 0x1F00 of
1322 * the 16-bit value at an offset of 14 (shifted right
1323 * 8 - figure out which byte that is).
1325 cstate
->off_linktype
.constant_part
= 14;
1326 cstate
->off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1327 cstate
->off_nl
= 8; /* 802.2+SNAP */
1328 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1331 case DLT_PRISM_HEADER
:
1332 case DLT_IEEE802_11_RADIO_AVS
:
1333 case DLT_IEEE802_11_RADIO
:
1334 cstate
->off_linkhdr
.is_variable
= 1;
1335 /* Fall through, 802.11 doesn't have a variable link
1336 * prefix but is otherwise the same. */
1338 case DLT_IEEE802_11
:
1340 * 802.11 doesn't really have a link-level type field.
1341 * We set "off_linktype.constant_part" to the offset of
1344 * To check for Ethernet types, we assume that SSAP = SNAP
1345 * is being used and pick out the encapsulated Ethernet type.
1346 * XXX - should we generate code to check for SNAP?
1348 * We also handle variable-length radio headers here.
1349 * The Prism header is in theory variable-length, but in
1350 * practice it's always 144 bytes long. However, some
1351 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1352 * sometimes or always supply an AVS header, so we
1353 * have to check whether the radio header is a Prism
1354 * header or an AVS header, so, in practice, it's
1357 cstate
->off_linktype
.constant_part
= 24;
1358 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1359 cstate
->off_linkpl
.is_variable
= 1;
1360 cstate
->off_nl
= 8; /* 802.2+SNAP */
1361 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1366 * At the moment we treat PPI the same way that we treat
1367 * normal Radiotap encoded packets. The difference is in
1368 * the function that generates the code at the beginning
1369 * to compute the header length. Since this code generator
1370 * of PPI supports bare 802.11 encapsulation only (i.e.
1371 * the encapsulated DLT should be DLT_IEEE802_11) we
1372 * generate code to check for this too.
1374 cstate
->off_linktype
.constant_part
= 24;
1375 cstate
->off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1376 cstate
->off_linkpl
.is_variable
= 1;
1377 cstate
->off_linkhdr
.is_variable
= 1;
1378 cstate
->off_nl
= 8; /* 802.2+SNAP */
1379 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1382 case DLT_ATM_RFC1483
:
1383 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1385 * assume routed, non-ISO PDUs
1386 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1388 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1389 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1390 * latter would presumably be treated the way PPPoE
1391 * should be, so you can do "pppoe and udp port 2049"
1392 * or "pppoa and tcp port 80" and have it check for
1393 * PPPo{A,E} and a PPP protocol of IP and....
1395 cstate
->off_linktype
.constant_part
= 0;
1396 cstate
->off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1397 cstate
->off_nl
= 8; /* 802.2+SNAP */
1398 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1403 * Full Frontal ATM; you get AALn PDUs with an ATM
1407 cstate
->off_vpi
= SUNATM_VPI_POS
;
1408 cstate
->off_vci
= SUNATM_VCI_POS
;
1409 cstate
->off_proto
= PROTO_POS
;
1410 cstate
->off_payload
= SUNATM_PKT_BEGIN_POS
;
1411 cstate
->off_linktype
.constant_part
= cstate
->off_payload
;
1412 cstate
->off_linkpl
.constant_part
= cstate
->off_payload
; /* if LLC-encapsulated */
1413 cstate
->off_nl
= 8; /* 802.2+SNAP */
1414 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1420 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1421 cstate
->off_linkpl
.constant_part
= 0;
1423 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1426 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket v1 */
1427 cstate
->off_linktype
.constant_part
= 14;
1428 cstate
->off_linkpl
.constant_part
= 16;
1430 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1433 case DLT_LINUX_SLL2
: /* fake header for Linux cooked socket v2 */
1434 cstate
->off_linktype
.constant_part
= 0;
1435 cstate
->off_linkpl
.constant_part
= 20;
1437 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1442 * LocalTalk does have a 1-byte type field in the LLAP header,
1443 * but really it just indicates whether there is a "short" or
1444 * "long" DDP packet following.
1446 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1447 cstate
->off_linkpl
.constant_part
= 0;
1449 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1452 case DLT_IP_OVER_FC
:
1454 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1455 * link-level type field. We set "off_linktype" to the
1456 * offset of the LLC header.
1458 * To check for Ethernet types, we assume that SSAP = SNAP
1459 * is being used and pick out the encapsulated Ethernet type.
1460 * XXX - should we generate code to check for SNAP? RFC
1461 * 2625 says SNAP should be used.
1463 cstate
->off_linktype
.constant_part
= 16;
1464 cstate
->off_linkpl
.constant_part
= 16;
1465 cstate
->off_nl
= 8; /* 802.2+SNAP */
1466 cstate
->off_nl_nosnap
= 3; /* 802.2 */
1471 * XXX - we should set this to handle SNAP-encapsulated
1472 * frames (NLPID of 0x80).
1474 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1475 cstate
->off_linkpl
.constant_part
= 0;
1477 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1481 * the only BPF-interesting FRF.16 frames are non-control frames;
1482 * Frame Relay has a variable length link-layer
1483 * so lets start with offset 4 for now and increments later on (FIXME);
1486 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1487 cstate
->off_linkpl
.constant_part
= 0;
1489 cstate
->off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1492 case DLT_APPLE_IP_OVER_IEEE1394
:
1493 cstate
->off_linktype
.constant_part
= 16;
1494 cstate
->off_linkpl
.constant_part
= 18;
1496 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1499 case DLT_SYMANTEC_FIREWALL
:
1500 cstate
->off_linktype
.constant_part
= 6;
1501 cstate
->off_linkpl
.constant_part
= 44;
1502 cstate
->off_nl
= 0; /* Ethernet II */
1503 cstate
->off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1506 #ifdef HAVE_NET_PFVAR_H
1508 cstate
->off_linktype
.constant_part
= 0;
1509 cstate
->off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1511 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
1515 case DLT_JUNIPER_MFR
:
1516 case DLT_JUNIPER_MLFR
:
1517 case DLT_JUNIPER_MLPPP
:
1518 case DLT_JUNIPER_PPP
:
1519 case DLT_JUNIPER_CHDLC
:
1520 case DLT_JUNIPER_FRELAY
:
1521 cstate
->off_linktype
.constant_part
= 4;
1522 cstate
->off_linkpl
.constant_part
= 4;
1524 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1527 case DLT_JUNIPER_ATM1
:
1528 cstate
->off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1529 cstate
->off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1531 cstate
->off_nl_nosnap
= 10;
1534 case DLT_JUNIPER_ATM2
:
1535 cstate
->off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1536 cstate
->off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1538 cstate
->off_nl_nosnap
= 10;
1541 /* frames captured on a Juniper PPPoE service PIC
1542 * contain raw ethernet frames */
1543 case DLT_JUNIPER_PPPOE
:
1544 case DLT_JUNIPER_ETHER
:
1545 cstate
->off_linkpl
.constant_part
= 14;
1546 cstate
->off_linktype
.constant_part
= 16;
1547 cstate
->off_nl
= 18; /* Ethernet II */
1548 cstate
->off_nl_nosnap
= 21; /* 802.3+802.2 */
1551 case DLT_JUNIPER_PPPOE_ATM
:
1552 cstate
->off_linktype
.constant_part
= 4;
1553 cstate
->off_linkpl
.constant_part
= 6;
1555 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1558 case DLT_JUNIPER_GGSN
:
1559 cstate
->off_linktype
.constant_part
= 6;
1560 cstate
->off_linkpl
.constant_part
= 12;
1562 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1565 case DLT_JUNIPER_ES
:
1566 cstate
->off_linktype
.constant_part
= 6;
1567 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1568 cstate
->off_nl
= OFFSET_NOT_SET
; /* not really a network layer but raw IP addresses */
1569 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1572 case DLT_JUNIPER_MONITOR
:
1573 cstate
->off_linktype
.constant_part
= 12;
1574 cstate
->off_linkpl
.constant_part
= 12;
1575 cstate
->off_nl
= 0; /* raw IP/IP6 header */
1576 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1579 case DLT_BACNET_MS_TP
:
1580 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1581 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1582 cstate
->off_nl
= OFFSET_NOT_SET
;
1583 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1586 case DLT_JUNIPER_SERVICES
:
1587 cstate
->off_linktype
.constant_part
= 12;
1588 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1589 cstate
->off_nl
= OFFSET_NOT_SET
; /* L3 proto location dep. on cookie type */
1590 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1593 case DLT_JUNIPER_VP
:
1594 cstate
->off_linktype
.constant_part
= 18;
1595 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1596 cstate
->off_nl
= OFFSET_NOT_SET
;
1597 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1600 case DLT_JUNIPER_ST
:
1601 cstate
->off_linktype
.constant_part
= 18;
1602 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1603 cstate
->off_nl
= OFFSET_NOT_SET
;
1604 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1607 case DLT_JUNIPER_ISM
:
1608 cstate
->off_linktype
.constant_part
= 8;
1609 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1610 cstate
->off_nl
= OFFSET_NOT_SET
;
1611 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1614 case DLT_JUNIPER_VS
:
1615 case DLT_JUNIPER_SRX_E2E
:
1616 case DLT_JUNIPER_FIBRECHANNEL
:
1617 case DLT_JUNIPER_ATM_CEMIC
:
1618 cstate
->off_linktype
.constant_part
= 8;
1619 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1620 cstate
->off_nl
= OFFSET_NOT_SET
;
1621 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1626 cstate
->off_li_hsl
= 4;
1627 cstate
->off_sio
= 3;
1628 cstate
->off_opc
= 4;
1629 cstate
->off_dpc
= 4;
1630 cstate
->off_sls
= 7;
1631 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1632 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1633 cstate
->off_nl
= OFFSET_NOT_SET
;
1634 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1637 case DLT_MTP2_WITH_PHDR
:
1639 cstate
->off_li_hsl
= 8;
1640 cstate
->off_sio
= 7;
1641 cstate
->off_opc
= 8;
1642 cstate
->off_dpc
= 8;
1643 cstate
->off_sls
= 11;
1644 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1645 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1646 cstate
->off_nl
= OFFSET_NOT_SET
;
1647 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1651 cstate
->off_li
= 22;
1652 cstate
->off_li_hsl
= 24;
1653 cstate
->off_sio
= 23;
1654 cstate
->off_opc
= 24;
1655 cstate
->off_dpc
= 24;
1656 cstate
->off_sls
= 27;
1657 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1658 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1659 cstate
->off_nl
= OFFSET_NOT_SET
;
1660 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1664 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1665 cstate
->off_linkpl
.constant_part
= 4;
1667 cstate
->off_nl_nosnap
= 0;
1672 * Currently, only raw "link[N:M]" filtering is supported.
1674 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
; /* variable, min 15, max 71 steps of 7 */
1675 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1676 cstate
->off_nl
= OFFSET_NOT_SET
; /* variable, min 16, max 71 steps of 7 */
1677 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
; /* no 802.2 LLC */
1681 cstate
->off_linktype
.constant_part
= 1;
1682 cstate
->off_linkpl
.constant_part
= 24; /* ipnet header length */
1684 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1687 case DLT_NETANALYZER
:
1688 cstate
->off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1689 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1690 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1691 cstate
->off_nl
= 0; /* Ethernet II */
1692 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1695 case DLT_NETANALYZER_TRANSPARENT
:
1696 cstate
->off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1697 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
1698 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1699 cstate
->off_nl
= 0; /* Ethernet II */
1700 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
1705 * For values in the range in which we've assigned new
1706 * DLT_ values, only raw "link[N:M]" filtering is supported.
1708 if (cstate
->linktype
>= DLT_MATCHING_MIN
&&
1709 cstate
->linktype
<= DLT_MATCHING_MAX
) {
1710 cstate
->off_linktype
.constant_part
= OFFSET_NOT_SET
;
1711 cstate
->off_linkpl
.constant_part
= OFFSET_NOT_SET
;
1712 cstate
->off_nl
= OFFSET_NOT_SET
;
1713 cstate
->off_nl_nosnap
= OFFSET_NOT_SET
;
1715 bpf_set_error(cstate
, "unknown data link type %d", cstate
->linktype
);
1721 cstate
->off_outermostlinkhdr
= cstate
->off_prevlinkhdr
= cstate
->off_linkhdr
;
1726 * Load a value relative to the specified absolute offset.
1728 static struct slist
*
1729 gen_load_absoffsetrel(compiler_state_t
*cstate
, bpf_abs_offset
*abs_offset
,
1730 u_int offset
, u_int size
)
1732 struct slist
*s
, *s2
;
1734 s
= gen_abs_offset_varpart(cstate
, abs_offset
);
1737 * If "s" is non-null, it has code to arrange that the X register
1738 * contains the variable part of the absolute offset, so we
1739 * generate a load relative to that, with an offset of
1740 * abs_offset->constant_part + offset.
1742 * Otherwise, we can do an absolute load with an offset of
1743 * abs_offset->constant_part + offset.
1747 * "s" points to a list of statements that puts the
1748 * variable part of the absolute offset into the X register.
1749 * Do an indirect load, to use the X register as an offset.
1751 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1752 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1756 * There is no variable part of the absolute offset, so
1757 * just do an absolute load.
1759 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1760 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1766 * Load a value relative to the beginning of the specified header.
1768 static struct slist
*
1769 gen_load_a(compiler_state_t
*cstate
, enum e_offrel offrel
, u_int offset
,
1772 struct slist
*s
, *s2
;
1777 s
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|size
);
1782 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkhdr
, offset
, size
);
1785 case OR_PREVLINKHDR
:
1786 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_prevlinkhdr
, offset
, size
);
1790 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, offset
, size
);
1793 case OR_PREVMPLSHDR
:
1794 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
- 4 + offset
, size
);
1798 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ offset
, size
);
1801 case OR_LINKPL_NOSNAP
:
1802 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl_nosnap
+ offset
, size
);
1806 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linktype
, offset
, size
);
1811 * Load the X register with the length of the IPv4 header
1812 * (plus the offset of the link-layer header, if it's
1813 * preceded by a variable-length header such as a radio
1814 * header), in bytes.
1816 s
= gen_loadx_iphdrlen(cstate
);
1819 * Load the item at {offset of the link-layer payload} +
1820 * {offset, relative to the start of the link-layer
1821 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1822 * {specified offset}.
1824 * If the offset of the link-layer payload is variable,
1825 * the variable part of that offset is included in the
1826 * value in the X register, and we include the constant
1827 * part in the offset of the load.
1829 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
1830 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ offset
;
1835 s
= gen_load_absoffsetrel(cstate
, &cstate
->off_linkpl
, cstate
->off_nl
+ 40 + offset
, size
);
1846 * Generate code to load into the X register the sum of the length of
1847 * the IPv4 header and the variable part of the offset of the link-layer
1850 static struct slist
*
1851 gen_loadx_iphdrlen(compiler_state_t
*cstate
)
1853 struct slist
*s
, *s2
;
1855 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
1858 * The offset of the link-layer payload has a variable
1859 * part. "s" points to a list of statements that put
1860 * the variable part of that offset into the X register.
1862 * The 4*([k]&0xf) addressing mode can't be used, as we
1863 * don't have a constant offset, so we have to load the
1864 * value in question into the A register and add to it
1865 * the value from the X register.
1867 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
1868 s2
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1870 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
1873 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
1878 * The A register now contains the length of the IP header.
1879 * We need to add to it the variable part of the offset of
1880 * the link-layer payload, which is still in the X
1881 * register, and move the result into the X register.
1883 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
1884 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
1887 * The offset of the link-layer payload is a constant,
1888 * so no code was generated to load the (non-existent)
1889 * variable part of that offset.
1891 * This means we can use the 4*([k]&0xf) addressing
1892 * mode. Load the length of the IPv4 header, which
1893 * is at an offset of cstate->off_nl from the beginning of
1894 * the link-layer payload, and thus at an offset of
1895 * cstate->off_linkpl.constant_part + cstate->off_nl from the beginning
1896 * of the raw packet data, using that addressing mode.
1898 s
= new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
1899 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
1905 static struct block
*
1906 gen_uncond(compiler_state_t
*cstate
, int rsense
)
1911 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
1913 b
= new_block(cstate
, JMP(BPF_JEQ
));
1919 static inline struct block
*
1920 gen_true(compiler_state_t
*cstate
)
1922 return gen_uncond(cstate
, 1);
1925 static inline struct block
*
1926 gen_false(compiler_state_t
*cstate
)
1928 return gen_uncond(cstate
, 0);
1932 * Byte-swap a 32-bit number.
1933 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1934 * big-endian platforms.)
1936 #define SWAPLONG(y) \
1937 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1940 * Generate code to match a particular packet type.
1942 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1943 * value, if <= ETHERMTU. We use that to determine whether to
1944 * match the type/length field or to check the type/length field for
1945 * a value <= ETHERMTU to see whether it's a type field and then do
1946 * the appropriate test.
1948 static struct block
*
1949 gen_ether_linktype(compiler_state_t
*cstate
, int proto
)
1951 struct block
*b0
, *b1
;
1957 case LLCSAP_NETBEUI
:
1959 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1960 * so we check the DSAP and SSAP.
1962 * LLCSAP_IP checks for IP-over-802.2, rather
1963 * than IP-over-Ethernet or IP-over-SNAP.
1965 * XXX - should we check both the DSAP and the
1966 * SSAP, like this, or should we check just the
1967 * DSAP, as we do for other types <= ETHERMTU
1968 * (i.e., other SAP values)?
1970 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1972 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
1973 ((proto
<< 8) | proto
));
1981 * Ethernet_II frames, which are Ethernet
1982 * frames with a frame type of ETHERTYPE_IPX;
1984 * Ethernet_802.3 frames, which are 802.3
1985 * frames (i.e., the type/length field is
1986 * a length field, <= ETHERMTU, rather than
1987 * a type field) with the first two bytes
1988 * after the Ethernet/802.3 header being
1991 * Ethernet_802.2 frames, which are 802.3
1992 * frames with an 802.2 LLC header and
1993 * with the IPX LSAP as the DSAP in the LLC
1996 * Ethernet_SNAP frames, which are 802.3
1997 * frames with an LLC header and a SNAP
1998 * header and with an OUI of 0x000000
1999 * (encapsulated Ethernet) and a protocol
2000 * ID of ETHERTYPE_IPX in the SNAP header.
2002 * XXX - should we generate the same code both
2003 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
2007 * This generates code to check both for the
2008 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
2010 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2011 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
2015 * Now we add code to check for SNAP frames with
2016 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
2018 b0
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2022 * Now we generate code to check for 802.3
2023 * frames in general.
2025 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2029 * Now add the check for 802.3 frames before the
2030 * check for Ethernet_802.2 and Ethernet_802.3,
2031 * as those checks should only be done on 802.3
2032 * frames, not on Ethernet frames.
2037 * Now add the check for Ethernet_II frames, and
2038 * do that before checking for the other frame
2041 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2045 case ETHERTYPE_ATALK
:
2046 case ETHERTYPE_AARP
:
2048 * EtherTalk (AppleTalk protocols on Ethernet link
2049 * layer) may use 802.2 encapsulation.
2053 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2054 * we check for an Ethernet type field less than
2055 * 1500, which means it's an 802.3 length field.
2057 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2061 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2062 * SNAP packets with an organization code of
2063 * 0x080007 (Apple, for Appletalk) and a protocol
2064 * type of ETHERTYPE_ATALK (Appletalk).
2066 * 802.2-encapsulated ETHERTYPE_AARP packets are
2067 * SNAP packets with an organization code of
2068 * 0x000000 (encapsulated Ethernet) and a protocol
2069 * type of ETHERTYPE_AARP (Appletalk ARP).
2071 if (proto
== ETHERTYPE_ATALK
)
2072 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2073 else /* proto == ETHERTYPE_AARP */
2074 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2078 * Check for Ethernet encapsulation (Ethertalk
2079 * phase 1?); we just check for the Ethernet
2082 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2088 if (proto
<= ETHERMTU
) {
2090 * This is an LLC SAP value, so the frames
2091 * that match would be 802.2 frames.
2092 * Check that the frame is an 802.2 frame
2093 * (i.e., that the length/type field is
2094 * a length field, <= ETHERMTU) and
2095 * then check the DSAP.
2097 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
2099 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
2104 * This is an Ethernet type, so compare
2105 * the length/type field with it (if
2106 * the frame is an 802.2 frame, the length
2107 * field will be <= ETHERMTU, and, as
2108 * "proto" is > ETHERMTU, this test
2109 * will fail and the frame won't match,
2110 * which is what we want).
2112 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
2118 static struct block
*
2119 gen_loopback_linktype(compiler_state_t
*cstate
, int proto
)
2122 * For DLT_NULL, the link-layer header is a 32-bit word
2123 * containing an AF_ value in *host* byte order, and for
2124 * DLT_ENC, the link-layer header begins with a 32-bit
2125 * word containing an AF_ value in host byte order.
2127 * In addition, if we're reading a saved capture file,
2128 * the host byte order in the capture may not be the
2129 * same as the host byte order on this machine.
2131 * For DLT_LOOP, the link-layer header is a 32-bit
2132 * word containing an AF_ value in *network* byte order.
2134 if (cstate
->linktype
== DLT_NULL
|| cstate
->linktype
== DLT_ENC
) {
2136 * The AF_ value is in host byte order, but the BPF
2137 * interpreter will convert it to network byte order.
2139 * If this is a save file, and it's from a machine
2140 * with the opposite byte order to ours, we byte-swap
2143 * Then we run it through "htonl()", and generate
2144 * code to compare against the result.
2146 if (cstate
->bpf_pcap
->rfile
!= NULL
&& cstate
->bpf_pcap
->swapped
)
2147 proto
= SWAPLONG(proto
);
2148 proto
= htonl(proto
);
2150 return (gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
2154 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
2155 * or IPv6 then we have an error.
2157 static struct block
*
2158 gen_ipnet_linktype(compiler_state_t
*cstate
, int proto
)
2163 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
2166 case ETHERTYPE_IPV6
:
2167 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
2168 (bpf_int32
)IPH_AF_INET6
);
2175 return gen_false(cstate
);
2179 * Generate code to match a particular packet type.
2181 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2182 * value, if <= ETHERMTU. We use that to determine whether to
2183 * match the type field or to check the type field for the special
2184 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2186 static struct block
*
2187 gen_linux_sll_linktype(compiler_state_t
*cstate
, int proto
)
2189 struct block
*b0
, *b1
;
2195 case LLCSAP_NETBEUI
:
2197 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2198 * so we check the DSAP and SSAP.
2200 * LLCSAP_IP checks for IP-over-802.2, rather
2201 * than IP-over-Ethernet or IP-over-SNAP.
2203 * XXX - should we check both the DSAP and the
2204 * SSAP, like this, or should we check just the
2205 * DSAP, as we do for other types <= ETHERMTU
2206 * (i.e., other SAP values)?
2208 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2209 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)
2210 ((proto
<< 8) | proto
));
2216 * Ethernet_II frames, which are Ethernet
2217 * frames with a frame type of ETHERTYPE_IPX;
2219 * Ethernet_802.3 frames, which have a frame
2220 * type of LINUX_SLL_P_802_3;
2222 * Ethernet_802.2 frames, which are 802.3
2223 * frames with an 802.2 LLC header (i.e, have
2224 * a frame type of LINUX_SLL_P_802_2) and
2225 * with the IPX LSAP as the DSAP in the LLC
2228 * Ethernet_SNAP frames, which are 802.3
2229 * frames with an LLC header and a SNAP
2230 * header and with an OUI of 0x000000
2231 * (encapsulated Ethernet) and a protocol
2232 * ID of ETHERTYPE_IPX in the SNAP header.
2234 * First, do the checks on LINUX_SLL_P_802_2
2235 * frames; generate the check for either
2236 * Ethernet_802.2 or Ethernet_SNAP frames, and
2237 * then put a check for LINUX_SLL_P_802_2 frames
2240 b0
= gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2241 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_IPX
);
2243 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2247 * Now check for 802.3 frames and OR that with
2248 * the previous test.
2250 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2254 * Now add the check for Ethernet_II frames, and
2255 * do that before checking for the other frame
2258 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2262 case ETHERTYPE_ATALK
:
2263 case ETHERTYPE_AARP
:
2265 * EtherTalk (AppleTalk protocols on Ethernet link
2266 * layer) may use 802.2 encapsulation.
2270 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2271 * we check for the 802.2 protocol type in the
2272 * "Ethernet type" field.
2274 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2277 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2278 * SNAP packets with an organization code of
2279 * 0x080007 (Apple, for Appletalk) and a protocol
2280 * type of ETHERTYPE_ATALK (Appletalk).
2282 * 802.2-encapsulated ETHERTYPE_AARP packets are
2283 * SNAP packets with an organization code of
2284 * 0x000000 (encapsulated Ethernet) and a protocol
2285 * type of ETHERTYPE_AARP (Appletalk ARP).
2287 if (proto
== ETHERTYPE_ATALK
)
2288 b1
= gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
2289 else /* proto == ETHERTYPE_AARP */
2290 b1
= gen_snap(cstate
, 0x000000, ETHERTYPE_AARP
);
2294 * Check for Ethernet encapsulation (Ethertalk
2295 * phase 1?); we just check for the Ethernet
2298 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2304 if (proto
<= ETHERMTU
) {
2306 * This is an LLC SAP value, so the frames
2307 * that match would be 802.2 frames.
2308 * Check for the 802.2 protocol type
2309 * in the "Ethernet type" field, and
2310 * then check the DSAP.
2312 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2313 b1
= gen_cmp(cstate
, OR_LINKHDR
, cstate
->off_linkpl
.constant_part
, BPF_B
,
2319 * This is an Ethernet type, so compare
2320 * the length/type field with it (if
2321 * the frame is an 802.2 frame, the length
2322 * field will be <= ETHERMTU, and, as
2323 * "proto" is > ETHERMTU, this test
2324 * will fail and the frame won't match,
2325 * which is what we want).
2327 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2332 static struct slist
*
2333 gen_load_prism_llprefixlen(compiler_state_t
*cstate
)
2335 struct slist
*s1
, *s2
;
2336 struct slist
*sjeq_avs_cookie
;
2337 struct slist
*sjcommon
;
2340 * This code is not compatible with the optimizer, as
2341 * we are generating jmp instructions within a normal
2342 * slist of instructions
2344 cstate
->no_optimize
= 1;
2347 * Generate code to load the length of the radio header into
2348 * the register assigned to hold that length, if one has been
2349 * assigned. (If one hasn't been assigned, no code we've
2350 * generated uses that prefix, so we don't need to generate any
2353 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2354 * or always use the AVS header rather than the Prism header.
2355 * We load a 4-byte big-endian value at the beginning of the
2356 * raw packet data, and see whether, when masked with 0xFFFFF000,
2357 * it's equal to 0x80211000. If so, that indicates that it's
2358 * an AVS header (the masked-out bits are the version number).
2359 * Otherwise, it's a Prism header.
2361 * XXX - the Prism header is also, in theory, variable-length,
2362 * but no known software generates headers that aren't 144
2365 if (cstate
->off_linkhdr
.reg
!= -1) {
2369 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2373 * AND it with 0xFFFFF000.
2375 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
2376 s2
->s
.k
= 0xFFFFF000;
2380 * Compare with 0x80211000.
2382 sjeq_avs_cookie
= new_stmt(cstate
, JMP(BPF_JEQ
));
2383 sjeq_avs_cookie
->s
.k
= 0x80211000;
2384 sappend(s1
, sjeq_avs_cookie
);
2389 * The 4 bytes at an offset of 4 from the beginning of
2390 * the AVS header are the length of the AVS header.
2391 * That field is big-endian.
2393 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2396 sjeq_avs_cookie
->s
.jt
= s2
;
2399 * Now jump to the code to allocate a register
2400 * into which to save the header length and
2401 * store the length there. (The "jump always"
2402 * instruction needs to have the k field set;
2403 * it's added to the PC, so, as we're jumping
2404 * over a single instruction, it should be 1.)
2406 sjcommon
= new_stmt(cstate
, JMP(BPF_JA
));
2408 sappend(s1
, sjcommon
);
2411 * Now for the code that handles the Prism header.
2412 * Just load the length of the Prism header (144)
2413 * into the A register. Have the test for an AVS
2414 * header branch here if we don't have an AVS header.
2416 s2
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2419 sjeq_avs_cookie
->s
.jf
= s2
;
2422 * Now allocate a register to hold that value and store
2423 * it. The code for the AVS header will jump here after
2424 * loading the length of the AVS header.
2426 s2
= new_stmt(cstate
, BPF_ST
);
2427 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2429 sjcommon
->s
.jf
= s2
;
2432 * Now move it into the X register.
2434 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2442 static struct slist
*
2443 gen_load_avs_llprefixlen(compiler_state_t
*cstate
)
2445 struct slist
*s1
, *s2
;
2448 * Generate code to load the length of the AVS header into
2449 * the register assigned to hold that length, if one has been
2450 * assigned. (If one hasn't been assigned, no code we've
2451 * generated uses that prefix, so we don't need to generate any
2454 if (cstate
->off_linkhdr
.reg
!= -1) {
2456 * The 4 bytes at an offset of 4 from the beginning of
2457 * the AVS header are the length of the AVS header.
2458 * That field is big-endian.
2460 s1
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2464 * Now allocate a register to hold that value and store
2467 s2
= new_stmt(cstate
, BPF_ST
);
2468 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2472 * Now move it into the X register.
2474 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2482 static struct slist
*
2483 gen_load_radiotap_llprefixlen(compiler_state_t
*cstate
)
2485 struct slist
*s1
, *s2
;
2488 * Generate code to load the length of the radiotap header into
2489 * the register assigned to hold that length, if one has been
2490 * assigned. (If one hasn't been assigned, no code we've
2491 * generated uses that prefix, so we don't need to generate any
2494 if (cstate
->off_linkhdr
.reg
!= -1) {
2496 * The 2 bytes at offsets of 2 and 3 from the beginning
2497 * of the radiotap header are the length of the radiotap
2498 * header; unfortunately, it's little-endian, so we have
2499 * to load it a byte at a time and construct the value.
2503 * Load the high-order byte, at an offset of 3, shift it
2504 * left a byte, and put the result in the X register.
2506 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2508 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2511 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2515 * Load the next byte, at an offset of 2, and OR the
2516 * value from the X register into it.
2518 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2521 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2525 * Now allocate a register to hold that value and store
2528 s2
= new_stmt(cstate
, BPF_ST
);
2529 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2533 * Now move it into the X register.
2535 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2544 * At the moment we treat PPI as normal Radiotap encoded
2545 * packets. The difference is in the function that generates
2546 * the code at the beginning to compute the header length.
2547 * Since this code generator of PPI supports bare 802.11
2548 * encapsulation only (i.e. the encapsulated DLT should be
2549 * DLT_IEEE802_11) we generate code to check for this too;
2550 * that's done in finish_parse().
2552 static struct slist
*
2553 gen_load_ppi_llprefixlen(compiler_state_t
*cstate
)
2555 struct slist
*s1
, *s2
;
2558 * Generate code to load the length of the radiotap header
2559 * into the register assigned to hold that length, if one has
2562 if (cstate
->off_linkhdr
.reg
!= -1) {
2564 * The 2 bytes at offsets of 2 and 3 from the beginning
2565 * of the radiotap header are the length of the radiotap
2566 * header; unfortunately, it's little-endian, so we have
2567 * to load it a byte at a time and construct the value.
2571 * Load the high-order byte, at an offset of 3, shift it
2572 * left a byte, and put the result in the X register.
2574 s1
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2576 s2
= new_stmt(cstate
, BPF_ALU
|BPF_LSH
|BPF_K
);
2579 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2583 * Load the next byte, at an offset of 2, and OR the
2584 * value from the X register into it.
2586 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
2589 s2
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_X
);
2593 * Now allocate a register to hold that value and store
2596 s2
= new_stmt(cstate
, BPF_ST
);
2597 s2
->s
.k
= cstate
->off_linkhdr
.reg
;
2601 * Now move it into the X register.
2603 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
2612 * Load a value relative to the beginning of the link-layer header after the 802.11
2613 * header, i.e. LLC_SNAP.
2614 * The link-layer header doesn't necessarily begin at the beginning
2615 * of the packet data; there might be a variable-length prefix containing
2616 * radio information.
2618 static struct slist
*
2619 gen_load_802_11_header_len(compiler_state_t
*cstate
, struct slist
*s
, struct slist
*snext
)
2622 struct slist
*sjset_data_frame_1
;
2623 struct slist
*sjset_data_frame_2
;
2624 struct slist
*sjset_qos
;
2625 struct slist
*sjset_radiotap_flags_present
;
2626 struct slist
*sjset_radiotap_ext_present
;
2627 struct slist
*sjset_radiotap_tsft_present
;
2628 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2629 struct slist
*s_roundup
;
2631 if (cstate
->off_linkpl
.reg
== -1) {
2633 * No register has been assigned to the offset of
2634 * the link-layer payload, which means nobody needs
2635 * it; don't bother computing it - just return
2636 * what we already have.
2642 * This code is not compatible with the optimizer, as
2643 * we are generating jmp instructions within a normal
2644 * slist of instructions
2646 cstate
->no_optimize
= 1;
2649 * If "s" is non-null, it has code to arrange that the X register
2650 * contains the length of the prefix preceding the link-layer
2653 * Otherwise, the length of the prefix preceding the link-layer
2654 * header is "off_outermostlinkhdr.constant_part".
2658 * There is no variable-length header preceding the
2659 * link-layer header.
2661 * Load the length of the fixed-length prefix preceding
2662 * the link-layer header (if any) into the X register,
2663 * and store it in the cstate->off_linkpl.reg register.
2664 * That length is off_outermostlinkhdr.constant_part.
2666 s
= new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
2667 s
->s
.k
= cstate
->off_outermostlinkhdr
.constant_part
;
2671 * The X register contains the offset of the beginning of the
2672 * link-layer header; add 24, which is the minimum length
2673 * of the MAC header for a data frame, to that, and store it
2674 * in cstate->off_linkpl.reg, and then load the Frame Control field,
2675 * which is at the offset in the X register, with an indexed load.
2677 s2
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
2679 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
2682 s2
= new_stmt(cstate
, BPF_ST
);
2683 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2686 s2
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
2691 * Check the Frame Control field to see if this is a data frame;
2692 * a data frame has the 0x08 bit (b3) in that field set and the
2693 * 0x04 bit (b2) clear.
2695 sjset_data_frame_1
= new_stmt(cstate
, JMP(BPF_JSET
));
2696 sjset_data_frame_1
->s
.k
= 0x08;
2697 sappend(s
, sjset_data_frame_1
);
2700 * If b3 is set, test b2, otherwise go to the first statement of
2701 * the rest of the program.
2703 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(cstate
, JMP(BPF_JSET
));
2704 sjset_data_frame_2
->s
.k
= 0x04;
2705 sappend(s
, sjset_data_frame_2
);
2706 sjset_data_frame_1
->s
.jf
= snext
;
2709 * If b2 is not set, this is a data frame; test the QoS bit.
2710 * Otherwise, go to the first statement of the rest of the
2713 sjset_data_frame_2
->s
.jt
= snext
;
2714 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(cstate
, JMP(BPF_JSET
));
2715 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2716 sappend(s
, sjset_qos
);
2719 * If it's set, add 2 to cstate->off_linkpl.reg, to skip the QoS
2721 * Otherwise, go to the first statement of the rest of the
2724 sjset_qos
->s
.jt
= s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2725 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2727 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2730 s2
= new_stmt(cstate
, BPF_ST
);
2731 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2735 * If we have a radiotap header, look at it to see whether
2736 * there's Atheros padding between the MAC-layer header
2739 * Note: all of the fields in the radiotap header are
2740 * little-endian, so we byte-swap all of the values
2741 * we test against, as they will be loaded as big-endian
2744 * XXX - in the general case, we would have to scan through
2745 * *all* the presence bits, if there's more than one word of
2746 * presence bits. That would require a loop, meaning that
2747 * we wouldn't be able to run the filter in the kernel.
2749 * We assume here that the Atheros adapters that insert the
2750 * annoying padding don't have multiple antennae and therefore
2751 * do not generate radiotap headers with multiple presence words.
2753 if (cstate
->linktype
== DLT_IEEE802_11_RADIO
) {
2755 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2756 * in the first presence flag word?
2758 sjset_qos
->s
.jf
= s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_W
);
2762 sjset_radiotap_flags_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2763 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2764 sappend(s
, sjset_radiotap_flags_present
);
2767 * If not, skip all of this.
2769 sjset_radiotap_flags_present
->s
.jf
= snext
;
2772 * Otherwise, is the "extension" bit set in that word?
2774 sjset_radiotap_ext_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2775 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2776 sappend(s
, sjset_radiotap_ext_present
);
2777 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2780 * If so, skip all of this.
2782 sjset_radiotap_ext_present
->s
.jt
= snext
;
2785 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2787 sjset_radiotap_tsft_present
= new_stmt(cstate
, JMP(BPF_JSET
));
2788 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2789 sappend(s
, sjset_radiotap_tsft_present
);
2790 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2793 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2794 * at an offset of 16 from the beginning of the raw packet
2795 * data (8 bytes for the radiotap header and 8 bytes for
2798 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2801 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2804 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2806 sjset_tsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2807 sjset_tsft_datapad
->s
.k
= 0x20;
2808 sappend(s
, sjset_tsft_datapad
);
2811 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2812 * at an offset of 8 from the beginning of the raw packet
2813 * data (8 bytes for the radiotap header).
2815 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2818 s2
= new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
2821 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2823 sjset_notsft_datapad
= new_stmt(cstate
, JMP(BPF_JSET
));
2824 sjset_notsft_datapad
->s
.k
= 0x20;
2825 sappend(s
, sjset_notsft_datapad
);
2828 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2829 * set, round the length of the 802.11 header to
2830 * a multiple of 4. Do that by adding 3 and then
2831 * dividing by and multiplying by 4, which we do by
2834 s_roundup
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
2835 s_roundup
->s
.k
= cstate
->off_linkpl
.reg
;
2836 sappend(s
, s_roundup
);
2837 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
2840 s2
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_IMM
);
2843 s2
= new_stmt(cstate
, BPF_ST
);
2844 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2847 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2848 sjset_tsft_datapad
->s
.jf
= snext
;
2849 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2850 sjset_notsft_datapad
->s
.jf
= snext
;
2852 sjset_qos
->s
.jf
= snext
;
2858 insert_compute_vloffsets(compiler_state_t
*cstate
, struct block
*b
)
2862 /* There is an implicit dependency between the link
2863 * payload and link header since the payload computation
2864 * includes the variable part of the header. Therefore,
2865 * if nobody else has allocated a register for the link
2866 * header and we need it, do it now. */
2867 if (cstate
->off_linkpl
.reg
!= -1 && cstate
->off_linkhdr
.is_variable
&&
2868 cstate
->off_linkhdr
.reg
== -1)
2869 cstate
->off_linkhdr
.reg
= alloc_reg(cstate
);
2872 * For link-layer types that have a variable-length header
2873 * preceding the link-layer header, generate code to load
2874 * the offset of the link-layer header into the register
2875 * assigned to that offset, if any.
2877 * XXX - this, and the next switch statement, won't handle
2878 * encapsulation of 802.11 or 802.11+radio information in
2879 * some other protocol stack. That's significantly more
2882 switch (cstate
->outermostlinktype
) {
2884 case DLT_PRISM_HEADER
:
2885 s
= gen_load_prism_llprefixlen(cstate
);
2888 case DLT_IEEE802_11_RADIO_AVS
:
2889 s
= gen_load_avs_llprefixlen(cstate
);
2892 case DLT_IEEE802_11_RADIO
:
2893 s
= gen_load_radiotap_llprefixlen(cstate
);
2897 s
= gen_load_ppi_llprefixlen(cstate
);
2906 * For link-layer types that have a variable-length link-layer
2907 * header, generate code to load the offset of the link-layer
2908 * payload into the register assigned to that offset, if any.
2910 switch (cstate
->outermostlinktype
) {
2912 case DLT_IEEE802_11
:
2913 case DLT_PRISM_HEADER
:
2914 case DLT_IEEE802_11_RADIO_AVS
:
2915 case DLT_IEEE802_11_RADIO
:
2917 s
= gen_load_802_11_header_len(cstate
, s
, b
->stmts
);
2922 * If there there is no initialization yet and we need variable
2923 * length offsets for VLAN, initialize them to zero
2925 if (s
== NULL
&& cstate
->is_vlan_vloffset
) {
2928 if (cstate
->off_linkpl
.reg
== -1)
2929 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
2930 if (cstate
->off_linktype
.reg
== -1)
2931 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
2933 s
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_IMM
);
2935 s2
= new_stmt(cstate
, BPF_ST
);
2936 s2
->s
.k
= cstate
->off_linkpl
.reg
;
2938 s2
= new_stmt(cstate
, BPF_ST
);
2939 s2
->s
.k
= cstate
->off_linktype
.reg
;
2944 * If we have any offset-loading code, append all the
2945 * existing statements in the block to those statements,
2946 * and make the resulting list the list of statements
2950 sappend(s
, b
->stmts
);
2955 static struct block
*
2956 gen_ppi_dlt_check(compiler_state_t
*cstate
)
2958 struct slist
*s_load_dlt
;
2961 if (cstate
->linktype
== DLT_PPI
)
2963 /* Create the statements that check for the DLT
2965 s_load_dlt
= new_stmt(cstate
, BPF_LD
|BPF_W
|BPF_ABS
);
2966 s_load_dlt
->s
.k
= 4;
2968 b
= new_block(cstate
, JMP(BPF_JEQ
));
2970 b
->stmts
= s_load_dlt
;
2971 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2982 * Take an absolute offset, and:
2984 * if it has no variable part, return NULL;
2986 * if it has a variable part, generate code to load the register
2987 * containing that variable part into the X register, returning
2988 * a pointer to that code - if no register for that offset has
2989 * been allocated, allocate it first.
2991 * (The code to set that register will be generated later, but will
2992 * be placed earlier in the code sequence.)
2994 static struct slist
*
2995 gen_abs_offset_varpart(compiler_state_t
*cstate
, bpf_abs_offset
*off
)
2999 if (off
->is_variable
) {
3000 if (off
->reg
== -1) {
3002 * We haven't yet assigned a register for the
3003 * variable part of the offset of the link-layer
3004 * header; allocate one.
3006 off
->reg
= alloc_reg(cstate
);
3010 * Load the register containing the variable part of the
3011 * offset of the link-layer header into the X register.
3013 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
3018 * That offset isn't variable, there's no variable part,
3019 * so we don't need to generate any code.
3026 * Map an Ethernet type to the equivalent PPP type.
3029 ethertype_to_ppptype(int proto
)
3037 case ETHERTYPE_IPV6
:
3045 case ETHERTYPE_ATALK
:
3059 * I'm assuming the "Bridging PDU"s that go
3060 * over PPP are Spanning Tree Protocol
3074 * Generate any tests that, for encapsulation of a link-layer packet
3075 * inside another protocol stack, need to be done to check for those
3076 * link-layer packets (and that haven't already been done by a check
3077 * for that encapsulation).
3079 static struct block
*
3080 gen_prevlinkhdr_check(compiler_state_t
*cstate
)
3084 if (cstate
->is_geneve
)
3085 return gen_geneve_ll_check(cstate
);
3087 switch (cstate
->prevlinktype
) {
3091 * This is LANE-encapsulated Ethernet; check that the LANE
3092 * packet doesn't begin with an LE Control marker, i.e.
3093 * that it's data, not a control message.
3095 * (We've already generated a test for LANE.)
3097 b0
= gen_cmp(cstate
, OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
3103 * No such tests are necessary.
3111 * The three different values we should check for when checking for an
3112 * IPv6 packet with DLT_NULL.
3114 #define BSD_AFNUM_INET6_BSD 24 /* NetBSD, OpenBSD, BSD/OS, Npcap */
3115 #define BSD_AFNUM_INET6_FREEBSD 28 /* FreeBSD */
3116 #define BSD_AFNUM_INET6_DARWIN 30 /* macOS, iOS, other Darwin-based OSes */
3119 * Generate code to match a particular packet type by matching the
3120 * link-layer type field or fields in the 802.2 LLC header.
3122 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3123 * value, if <= ETHERMTU.
3125 static struct block
*
3126 gen_linktype(compiler_state_t
*cstate
, int proto
)
3128 struct block
*b0
, *b1
, *b2
;
3129 const char *description
;
3131 /* are we checking MPLS-encapsulated packets? */
3132 if (cstate
->label_stack_depth
> 0) {
3136 /* FIXME add other L3 proto IDs */
3137 return gen_mpls_linktype(cstate
, Q_IP
);
3139 case ETHERTYPE_IPV6
:
3141 /* FIXME add other L3 proto IDs */
3142 return gen_mpls_linktype(cstate
, Q_IPV6
);
3145 bpf_error(cstate
, "unsupported protocol over mpls");
3150 switch (cstate
->linktype
) {
3153 case DLT_NETANALYZER
:
3154 case DLT_NETANALYZER_TRANSPARENT
:
3155 /* Geneve has an EtherType regardless of whether there is an
3157 if (!cstate
->is_geneve
)
3158 b0
= gen_prevlinkhdr_check(cstate
);
3162 b1
= gen_ether_linktype(cstate
, proto
);
3173 proto
= (proto
<< 8 | LLCSAP_ISONS
);
3177 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3183 case DLT_IEEE802_11
:
3184 case DLT_PRISM_HEADER
:
3185 case DLT_IEEE802_11_RADIO_AVS
:
3186 case DLT_IEEE802_11_RADIO
:
3189 * Check that we have a data frame.
3191 b0
= gen_check_802_11_data_frame(cstate
);
3194 * Now check for the specified link-layer type.
3196 b1
= gen_llc_linktype(cstate
, proto
);
3204 * XXX - check for LLC frames.
3206 return gen_llc_linktype(cstate
, proto
);
3212 * XXX - check for LLC PDUs, as per IEEE 802.5.
3214 return gen_llc_linktype(cstate
, proto
);
3218 case DLT_ATM_RFC1483
:
3220 case DLT_IP_OVER_FC
:
3221 return gen_llc_linktype(cstate
, proto
);
3227 * Check for an LLC-encapsulated version of this protocol;
3228 * if we were checking for LANE, linktype would no longer
3231 * Check for LLC encapsulation and then check the protocol.
3233 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3234 b1
= gen_llc_linktype(cstate
, proto
);
3241 return gen_linux_sll_linktype(cstate
, proto
);
3246 case DLT_SLIP_BSDOS
:
3249 * These types don't provide any type field; packets
3250 * are always IPv4 or IPv6.
3252 * XXX - for IPv4, check for a version number of 4, and,
3253 * for IPv6, check for a version number of 6?
3258 /* Check for a version number of 4. */
3259 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3261 case ETHERTYPE_IPV6
:
3262 /* Check for a version number of 6. */
3263 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3266 return gen_false(cstate
); /* always false */
3273 * Raw IPv4, so no type field.
3275 if (proto
== ETHERTYPE_IP
)
3276 return gen_true(cstate
); /* always true */
3278 /* Checking for something other than IPv4; always false */
3279 return gen_false(cstate
);
3285 * Raw IPv6, so no type field.
3287 if (proto
== ETHERTYPE_IPV6
)
3288 return gen_true(cstate
); /* always true */
3290 /* Checking for something other than IPv6; always false */
3291 return gen_false(cstate
);
3297 case DLT_PPP_SERIAL
:
3300 * We use Ethernet protocol types inside libpcap;
3301 * map them to the corresponding PPP protocol types.
3303 proto
= ethertype_to_ppptype(proto
);
3304 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3310 * We use Ethernet protocol types inside libpcap;
3311 * map them to the corresponding PPP protocol types.
3317 * Also check for Van Jacobson-compressed IP.
3318 * XXX - do this for other forms of PPP?
3320 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3321 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3323 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3328 proto
= ethertype_to_ppptype(proto
);
3329 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
,
3341 return (gen_loopback_linktype(cstate
, AF_INET
));
3343 case ETHERTYPE_IPV6
:
3345 * AF_ values may, unfortunately, be platform-
3346 * dependent; AF_INET isn't, because everybody
3347 * used 4.2BSD's value, but AF_INET6 is, because
3348 * 4.2BSD didn't have a value for it (given that
3349 * IPv6 didn't exist back in the early 1980's),
3350 * and they all picked their own values.
3352 * This means that, if we're reading from a
3353 * savefile, we need to check for all the
3356 * If we're doing a live capture, we only need
3357 * to check for this platform's value; however,
3358 * Npcap uses 24, which isn't Windows's AF_INET6
3359 * value. (Given the multiple different values,
3360 * programs that read pcap files shouldn't be
3361 * checking for their platform's AF_INET6 value
3362 * anyway, they should check for all of the
3363 * possible values. and they might as well do
3364 * that even for live captures.)
3366 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
3368 * Savefile - check for all three
3369 * possible IPv6 values.
3371 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_BSD
);
3372 b1
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_FREEBSD
);
3374 b0
= gen_loopback_linktype(cstate
, BSD_AFNUM_INET6_DARWIN
);
3379 * Live capture, so we only need to
3380 * check for the value used on this
3385 * Npcap doesn't use Windows's AF_INET6,
3386 * as that collides with AF_IPX on
3387 * some BSDs (both have the value 23).
3388 * Instead, it uses 24.
3390 return (gen_loopback_linktype(cstate
, 24));
3393 return (gen_loopback_linktype(cstate
, AF_INET6
));
3394 #else /* AF_INET6 */
3396 * I guess this platform doesn't support
3397 * IPv6, so we just reject all packets.
3399 return gen_false(cstate
);
3400 #endif /* AF_INET6 */
3406 * Not a type on which we support filtering.
3407 * XXX - support those that have AF_ values
3408 * #defined on this platform, at least?
3410 return gen_false(cstate
);
3413 #ifdef HAVE_NET_PFVAR_H
3416 * af field is host byte order in contrast to the rest of
3419 if (proto
== ETHERTYPE_IP
)
3420 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3421 BPF_B
, (bpf_int32
)AF_INET
));
3422 else if (proto
== ETHERTYPE_IPV6
)
3423 return (gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3424 BPF_B
, (bpf_int32
)AF_INET6
));
3426 return gen_false(cstate
);
3429 #endif /* HAVE_NET_PFVAR_H */
3432 case DLT_ARCNET_LINUX
:
3434 * XXX should we check for first fragment if the protocol
3440 return gen_false(cstate
);
3442 case ETHERTYPE_IPV6
:
3443 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3444 (bpf_int32
)ARCTYPE_INET6
));
3447 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3448 (bpf_int32
)ARCTYPE_IP
);
3449 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3450 (bpf_int32
)ARCTYPE_IP_OLD
);
3455 b0
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3456 (bpf_int32
)ARCTYPE_ARP
);
3457 b1
= gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3458 (bpf_int32
)ARCTYPE_ARP_OLD
);
3462 case ETHERTYPE_REVARP
:
3463 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3464 (bpf_int32
)ARCTYPE_REVARP
));
3466 case ETHERTYPE_ATALK
:
3467 return (gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_B
,
3468 (bpf_int32
)ARCTYPE_ATALK
));
3475 case ETHERTYPE_ATALK
:
3476 return gen_true(cstate
);
3478 return gen_false(cstate
);
3485 * XXX - assumes a 2-byte Frame Relay header with
3486 * DLCI and flags. What if the address is longer?
3492 * Check for the special NLPID for IP.
3494 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3496 case ETHERTYPE_IPV6
:
3498 * Check for the special NLPID for IPv6.
3500 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3504 * Check for several OSI protocols.
3506 * Frame Relay packets typically have an OSI
3507 * NLPID at the beginning; we check for each
3510 * What we check for is the NLPID and a frame
3511 * control field of UI, i.e. 0x03 followed
3514 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3515 b1
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3516 b2
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3522 return gen_false(cstate
);
3528 bpf_error(cstate
, "Multi-link Frame Relay link-layer type filtering not implemented");
3530 case DLT_JUNIPER_MFR
:
3531 case DLT_JUNIPER_MLFR
:
3532 case DLT_JUNIPER_MLPPP
:
3533 case DLT_JUNIPER_ATM1
:
3534 case DLT_JUNIPER_ATM2
:
3535 case DLT_JUNIPER_PPPOE
:
3536 case DLT_JUNIPER_PPPOE_ATM
:
3537 case DLT_JUNIPER_GGSN
:
3538 case DLT_JUNIPER_ES
:
3539 case DLT_JUNIPER_MONITOR
:
3540 case DLT_JUNIPER_SERVICES
:
3541 case DLT_JUNIPER_ETHER
:
3542 case DLT_JUNIPER_PPP
:
3543 case DLT_JUNIPER_FRELAY
:
3544 case DLT_JUNIPER_CHDLC
:
3545 case DLT_JUNIPER_VP
:
3546 case DLT_JUNIPER_ST
:
3547 case DLT_JUNIPER_ISM
:
3548 case DLT_JUNIPER_VS
:
3549 case DLT_JUNIPER_SRX_E2E
:
3550 case DLT_JUNIPER_FIBRECHANNEL
:
3551 case DLT_JUNIPER_ATM_CEMIC
:
3553 /* just lets verify the magic number for now -
3554 * on ATM we may have up to 6 different encapsulations on the wire
3555 * and need a lot of heuristics to figure out that the payload
3558 * FIXME encapsulation specific BPF_ filters
3560 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3562 case DLT_BACNET_MS_TP
:
3563 return gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3566 return gen_ipnet_linktype(cstate
, proto
);
3568 case DLT_LINUX_IRDA
:
3569 bpf_error(cstate
, "IrDA link-layer type filtering not implemented");
3572 bpf_error(cstate
, "DOCSIS link-layer type filtering not implemented");
3575 case DLT_MTP2_WITH_PHDR
:
3576 bpf_error(cstate
, "MTP2 link-layer type filtering not implemented");
3579 bpf_error(cstate
, "ERF link-layer type filtering not implemented");
3582 bpf_error(cstate
, "PFSYNC link-layer type filtering not implemented");
3584 case DLT_LINUX_LAPD
:
3585 bpf_error(cstate
, "LAPD link-layer type filtering not implemented");
3587 case DLT_USB_FREEBSD
:
3589 case DLT_USB_LINUX_MMAPPED
:
3591 bpf_error(cstate
, "USB link-layer type filtering not implemented");
3593 case DLT_BLUETOOTH_HCI_H4
:
3594 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3595 bpf_error(cstate
, "Bluetooth link-layer type filtering not implemented");
3598 case DLT_CAN_SOCKETCAN
:
3599 bpf_error(cstate
, "CAN link-layer type filtering not implemented");
3601 case DLT_IEEE802_15_4
:
3602 case DLT_IEEE802_15_4_LINUX
:
3603 case DLT_IEEE802_15_4_NONASK_PHY
:
3604 case DLT_IEEE802_15_4_NOFCS
:
3605 bpf_error(cstate
, "IEEE 802.15.4 link-layer type filtering not implemented");
3607 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3608 bpf_error(cstate
, "IEEE 802.16 link-layer type filtering not implemented");
3611 bpf_error(cstate
, "SITA link-layer type filtering not implemented");
3614 bpf_error(cstate
, "RAIF1 link-layer type filtering not implemented");
3617 bpf_error(cstate
, "IPMB link-layer type filtering not implemented");
3620 bpf_error(cstate
, "AX.25 link-layer type filtering not implemented");
3623 /* Using the fixed-size NFLOG header it is possible to tell only
3624 * the address family of the packet, other meaningful data is
3625 * either missing or behind TLVs.
3627 bpf_error(cstate
, "NFLOG link-layer type filtering not implemented");
3631 * Does this link-layer header type have a field
3632 * indicating the type of the next protocol? If
3633 * so, off_linktype.constant_part will be the offset of that
3634 * field in the packet; if not, it will be OFFSET_NOT_SET.
3636 if (cstate
->off_linktype
.constant_part
!= OFFSET_NOT_SET
) {
3638 * Yes; assume it's an Ethernet type. (If
3639 * it's not, it needs to be handled specially
3642 return gen_cmp(cstate
, OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3645 * No; report an error.
3647 description
= pcap_datalink_val_to_description(cstate
->linktype
);
3648 if (description
!= NULL
) {
3649 bpf_error(cstate
, "%s link-layer type filtering not implemented",
3652 bpf_error(cstate
, "DLT %u link-layer type filtering not implemented",
3661 * Check for an LLC SNAP packet with a given organization code and
3662 * protocol type; we check the entire contents of the 802.2 LLC and
3663 * snap headers, checking for DSAP and SSAP of SNAP and a control
3664 * field of 0x03 in the LLC header, and for the specified organization
3665 * code and protocol type in the SNAP header.
3667 static struct block
*
3668 gen_snap(compiler_state_t
*cstate
, bpf_u_int32 orgcode
, bpf_u_int32 ptype
)
3670 u_char snapblock
[8];
3672 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3673 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3674 snapblock
[2] = 0x03; /* control = UI */
3675 snapblock
[3] = (u_char
)(orgcode
>> 16); /* upper 8 bits of organization code */
3676 snapblock
[4] = (u_char
)(orgcode
>> 8); /* middle 8 bits of organization code */
3677 snapblock
[5] = (u_char
)(orgcode
>> 0); /* lower 8 bits of organization code */
3678 snapblock
[6] = (u_char
)(ptype
>> 8); /* upper 8 bits of protocol type */
3679 snapblock
[7] = (u_char
)(ptype
>> 0); /* lower 8 bits of protocol type */
3680 return gen_bcmp(cstate
, OR_LLC
, 0, 8, snapblock
);
3684 * Generate code to match frames with an LLC header.
3686 static struct block
*
3687 gen_llc_internal(compiler_state_t
*cstate
)
3689 struct block
*b0
, *b1
;
3691 switch (cstate
->linktype
) {
3695 * We check for an Ethernet type field less than
3696 * 1500, which means it's an 802.3 length field.
3698 b0
= gen_cmp_gt(cstate
, OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3702 * Now check for the purported DSAP and SSAP not being
3703 * 0xFF, to rule out NetWare-over-802.3.
3705 b1
= gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3712 * We check for LLC traffic.
3714 b0
= gen_atmtype_llc(cstate
);
3717 case DLT_IEEE802
: /* Token Ring */
3719 * XXX - check for LLC frames.
3721 return gen_true(cstate
);
3725 * XXX - check for LLC frames.
3727 return gen_true(cstate
);
3729 case DLT_ATM_RFC1483
:
3731 * For LLC encapsulation, these are defined to have an
3734 * For VC encapsulation, they don't, but there's no
3735 * way to check for that; the protocol used on the VC
3736 * is negotiated out of band.
3738 return gen_true(cstate
);
3740 case DLT_IEEE802_11
:
3741 case DLT_PRISM_HEADER
:
3742 case DLT_IEEE802_11_RADIO
:
3743 case DLT_IEEE802_11_RADIO_AVS
:
3746 * Check that we have a data frame.
3748 b0
= gen_check_802_11_data_frame(cstate
);
3752 bpf_error(cstate
, "'llc' not supported for linktype %d", cstate
->linktype
);
3758 gen_llc(compiler_state_t
*cstate
)
3761 * Catch errors reported by us and routines below us, and return NULL
3764 if (setjmp(cstate
->top_ctx
))
3767 return gen_llc_internal(cstate
);
3771 gen_llc_i(compiler_state_t
*cstate
)
3773 struct block
*b0
, *b1
;
3777 * Catch errors reported by us and routines below us, and return NULL
3780 if (setjmp(cstate
->top_ctx
))
3784 * Check whether this is an LLC frame.
3786 b0
= gen_llc_internal(cstate
);
3789 * Load the control byte and test the low-order bit; it must
3790 * be clear for I frames.
3792 s
= gen_load_a(cstate
, OR_LLC
, 2, BPF_B
);
3793 b1
= new_block(cstate
, JMP(BPF_JSET
));
3802 gen_llc_s(compiler_state_t
*cstate
)
3804 struct block
*b0
, *b1
;
3807 * Catch errors reported by us and routines below us, and return NULL
3810 if (setjmp(cstate
->top_ctx
))
3814 * Check whether this is an LLC frame.
3816 b0
= gen_llc_internal(cstate
);
3819 * Now compare the low-order 2 bit of the control byte against
3820 * the appropriate value for S frames.
3822 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3828 gen_llc_u(compiler_state_t
*cstate
)
3830 struct block
*b0
, *b1
;
3833 * Catch errors reported by us and routines below us, and return NULL
3836 if (setjmp(cstate
->top_ctx
))
3840 * Check whether this is an LLC frame.
3842 b0
= gen_llc_internal(cstate
);
3845 * Now compare the low-order 2 bit of the control byte against
3846 * the appropriate value for U frames.
3848 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3854 gen_llc_s_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3856 struct block
*b0
, *b1
;
3859 * Catch errors reported by us and routines below us, and return NULL
3862 if (setjmp(cstate
->top_ctx
))
3866 * Check whether this is an LLC frame.
3868 b0
= gen_llc_internal(cstate
);
3871 * Now check for an S frame with the appropriate type.
3873 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3879 gen_llc_u_subtype(compiler_state_t
*cstate
, bpf_u_int32 subtype
)
3881 struct block
*b0
, *b1
;
3884 * Catch errors reported by us and routines below us, and return NULL
3887 if (setjmp(cstate
->top_ctx
))
3891 * Check whether this is an LLC frame.
3893 b0
= gen_llc_internal(cstate
);
3896 * Now check for a U frame with the appropriate type.
3898 b1
= gen_mcmp(cstate
, OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3904 * Generate code to match a particular packet type, for link-layer types
3905 * using 802.2 LLC headers.
3907 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3908 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3910 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3911 * value, if <= ETHERMTU. We use that to determine whether to
3912 * match the DSAP or both DSAP and LSAP or to check the OUI and
3913 * protocol ID in a SNAP header.
3915 static struct block
*
3916 gen_llc_linktype(compiler_state_t
*cstate
, int proto
)
3919 * XXX - handle token-ring variable-length header.
3925 case LLCSAP_NETBEUI
:
3927 * XXX - should we check both the DSAP and the
3928 * SSAP, like this, or should we check just the
3929 * DSAP, as we do for other SAP values?
3931 return gen_cmp(cstate
, OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3932 ((proto
<< 8) | proto
));
3936 * XXX - are there ever SNAP frames for IPX on
3937 * non-Ethernet 802.x networks?
3939 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
,
3940 (bpf_int32
)LLCSAP_IPX
);
3942 case ETHERTYPE_ATALK
:
3944 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3945 * SNAP packets with an organization code of
3946 * 0x080007 (Apple, for Appletalk) and a protocol
3947 * type of ETHERTYPE_ATALK (Appletalk).
3949 * XXX - check for an organization code of
3950 * encapsulated Ethernet as well?
3952 return gen_snap(cstate
, 0x080007, ETHERTYPE_ATALK
);
3956 * XXX - we don't have to check for IPX 802.3
3957 * here, but should we check for the IPX Ethertype?
3959 if (proto
<= ETHERMTU
) {
3961 * This is an LLC SAP value, so check
3964 return gen_cmp(cstate
, OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3967 * This is an Ethernet type; we assume that it's
3968 * unlikely that it'll appear in the right place
3969 * at random, and therefore check only the
3970 * location that would hold the Ethernet type
3971 * in a SNAP frame with an organization code of
3972 * 0x000000 (encapsulated Ethernet).
3974 * XXX - if we were to check for the SNAP DSAP and
3975 * LSAP, as per XXX, and were also to check for an
3976 * organization code of 0x000000 (encapsulated
3977 * Ethernet), we'd do
3979 * return gen_snap(cstate, 0x000000, proto);
3981 * here; for now, we don't, as per the above.
3982 * I don't know whether it's worth the extra CPU
3983 * time to do the right check or not.
3985 return gen_cmp(cstate
, OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3990 static struct block
*
3991 gen_hostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
3992 int dir
, int proto
, u_int src_off
, u_int dst_off
)
3994 struct block
*b0
, *b1
;
4008 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4009 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4015 b0
= gen_hostop(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4016 b1
= gen_hostop(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4021 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4025 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4029 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4033 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4037 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4041 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4047 b0
= gen_linktype(cstate
, proto
);
4048 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
4054 static struct block
*
4055 gen_hostop6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
4056 struct in6_addr
*mask
, int dir
, int proto
, u_int src_off
, u_int dst_off
)
4058 struct block
*b0
, *b1
;
4073 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4074 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4080 b0
= gen_hostop6(cstate
, addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
4081 b1
= gen_hostop6(cstate
, addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
4086 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4090 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4094 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4098 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4102 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4106 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4112 /* this order is important */
4113 a
= (uint32_t *)addr
;
4114 m
= (uint32_t *)mask
;
4115 b1
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
4116 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
4118 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
4120 b0
= gen_mcmp(cstate
, OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
4122 b0
= gen_linktype(cstate
, proto
);
4128 static struct block
*
4129 gen_ehostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4131 register struct block
*b0
, *b1
;
4135 return gen_bcmp(cstate
, OR_LINKHDR
, 6, 6, eaddr
);
4138 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 6, eaddr
);
4141 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4142 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4148 b0
= gen_ehostop(cstate
, eaddr
, Q_SRC
);
4149 b1
= gen_ehostop(cstate
, eaddr
, Q_DST
);
4154 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11 with 802.11 headers");
4158 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11 with 802.11 headers");
4162 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11 with 802.11 headers");
4166 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11 with 802.11 headers");
4170 bpf_error(cstate
, "'ra' is only supported on 802.11 with 802.11 headers");
4174 bpf_error(cstate
, "'ta' is only supported on 802.11 with 802.11 headers");
4182 * Like gen_ehostop, but for DLT_FDDI
4184 static struct block
*
4185 gen_fhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4187 struct block
*b0
, *b1
;
4191 return gen_bcmp(cstate
, OR_LINKHDR
, 6 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4194 return gen_bcmp(cstate
, OR_LINKHDR
, 0 + 1 + cstate
->pcap_fddipad
, 6, eaddr
);
4197 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4198 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4204 b0
= gen_fhostop(cstate
, eaddr
, Q_SRC
);
4205 b1
= gen_fhostop(cstate
, eaddr
, Q_DST
);
4210 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4214 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4218 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4222 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4226 bpf_error(cstate
, "'ra' is only supported on 802.11");
4230 bpf_error(cstate
, "'ta' is only supported on 802.11");
4238 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
4240 static struct block
*
4241 gen_thostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4243 register struct block
*b0
, *b1
;
4247 return gen_bcmp(cstate
, OR_LINKHDR
, 8, 6, eaddr
);
4250 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4253 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4254 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4260 b0
= gen_thostop(cstate
, eaddr
, Q_SRC
);
4261 b1
= gen_thostop(cstate
, eaddr
, Q_DST
);
4266 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4270 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4274 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4278 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4282 bpf_error(cstate
, "'ra' is only supported on 802.11");
4286 bpf_error(cstate
, "'ta' is only supported on 802.11");
4294 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
4295 * various 802.11 + radio headers.
4297 static struct block
*
4298 gen_wlanhostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4300 register struct block
*b0
, *b1
, *b2
;
4301 register struct slist
*s
;
4303 #ifdef ENABLE_WLAN_FILTERING_PATCH
4306 * We need to disable the optimizer because the optimizer is buggy
4307 * and wipes out some LD instructions generated by the below
4308 * code to validate the Frame Control bits
4310 cstate
->no_optimize
= 1;
4311 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4318 * For control frames, there is no SA.
4320 * For management frames, SA is at an
4321 * offset of 10 from the beginning of
4324 * For data frames, SA is at an offset
4325 * of 10 from the beginning of the packet
4326 * if From DS is clear, at an offset of
4327 * 16 from the beginning of the packet
4328 * if From DS is set and To DS is clear,
4329 * and an offset of 24 from the beginning
4330 * of the packet if From DS is set and To DS
4335 * Generate the tests to be done for data frames
4338 * First, check for To DS set, i.e. check "link[1] & 0x01".
4340 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4341 b1
= new_block(cstate
, JMP(BPF_JSET
));
4342 b1
->s
.k
= 0x01; /* To DS */
4346 * If To DS is set, the SA is at 24.
4348 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4352 * Now, check for To DS not set, i.e. check
4353 * "!(link[1] & 0x01)".
4355 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4356 b2
= new_block(cstate
, JMP(BPF_JSET
));
4357 b2
->s
.k
= 0x01; /* To DS */
4362 * If To DS is not set, the SA is at 16.
4364 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4368 * Now OR together the last two checks. That gives
4369 * the complete set of checks for data frames with
4375 * Now check for From DS being set, and AND that with
4376 * the ORed-together checks.
4378 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4379 b1
= new_block(cstate
, JMP(BPF_JSET
));
4380 b1
->s
.k
= 0x02; /* From DS */
4385 * Now check for data frames with From DS not set.
4387 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4388 b2
= new_block(cstate
, JMP(BPF_JSET
));
4389 b2
->s
.k
= 0x02; /* From DS */
4394 * If From DS isn't set, the SA is at 10.
4396 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4400 * Now OR together the checks for data frames with
4401 * From DS not set and for data frames with From DS
4402 * set; that gives the checks done for data frames.
4407 * Now check for a data frame.
4408 * I.e, check "link[0] & 0x08".
4410 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4411 b1
= new_block(cstate
, JMP(BPF_JSET
));
4416 * AND that with the checks done for data frames.
4421 * If the high-order bit of the type value is 0, this
4422 * is a management frame.
4423 * I.e, check "!(link[0] & 0x08)".
4425 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4426 b2
= new_block(cstate
, JMP(BPF_JSET
));
4432 * For management frames, the SA is at 10.
4434 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4438 * OR that with the checks done for data frames.
4439 * That gives the checks done for management and
4445 * If the low-order bit of the type value is 1,
4446 * this is either a control frame or a frame
4447 * with a reserved type, and thus not a
4450 * I.e., check "!(link[0] & 0x04)".
4452 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4453 b1
= new_block(cstate
, JMP(BPF_JSET
));
4459 * AND that with the checks for data and management
4469 * For control frames, there is no DA.
4471 * For management frames, DA is at an
4472 * offset of 4 from the beginning of
4475 * For data frames, DA is at an offset
4476 * of 4 from the beginning of the packet
4477 * if To DS is clear and at an offset of
4478 * 16 from the beginning of the packet
4483 * Generate the tests to be done for data frames.
4485 * First, check for To DS set, i.e. "link[1] & 0x01".
4487 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4488 b1
= new_block(cstate
, JMP(BPF_JSET
));
4489 b1
->s
.k
= 0x01; /* To DS */
4493 * If To DS is set, the DA is at 16.
4495 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4499 * Now, check for To DS not set, i.e. check
4500 * "!(link[1] & 0x01)".
4502 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
4503 b2
= new_block(cstate
, JMP(BPF_JSET
));
4504 b2
->s
.k
= 0x01; /* To DS */
4509 * If To DS is not set, the DA is at 4.
4511 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4515 * Now OR together the last two checks. That gives
4516 * the complete set of checks for data frames.
4521 * Now check for a data frame.
4522 * I.e, check "link[0] & 0x08".
4524 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4525 b1
= new_block(cstate
, JMP(BPF_JSET
));
4530 * AND that with the checks done for data frames.
4535 * If the high-order bit of the type value is 0, this
4536 * is a management frame.
4537 * I.e, check "!(link[0] & 0x08)".
4539 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4540 b2
= new_block(cstate
, JMP(BPF_JSET
));
4546 * For management frames, the DA is at 4.
4548 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4552 * OR that with the checks done for data frames.
4553 * That gives the checks done for management and
4559 * If the low-order bit of the type value is 1,
4560 * this is either a control frame or a frame
4561 * with a reserved type, and thus not a
4564 * I.e., check "!(link[0] & 0x04)".
4566 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4567 b1
= new_block(cstate
, JMP(BPF_JSET
));
4573 * AND that with the checks for data and management
4580 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4581 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4587 b0
= gen_wlanhostop(cstate
, eaddr
, Q_SRC
);
4588 b1
= gen_wlanhostop(cstate
, eaddr
, Q_DST
);
4593 * XXX - add BSSID keyword?
4596 return (gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
));
4600 * Not present in CTS or ACK control frames.
4602 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4603 IEEE80211_FC0_TYPE_MASK
);
4605 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4606 IEEE80211_FC0_SUBTYPE_MASK
);
4608 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4609 IEEE80211_FC0_SUBTYPE_MASK
);
4613 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4619 * Not present in control frames.
4621 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4622 IEEE80211_FC0_TYPE_MASK
);
4624 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 16, 6, eaddr
);
4630 * Present only if the direction mask has both "From DS"
4631 * and "To DS" set. Neither control frames nor management
4632 * frames should have both of those set, so we don't
4633 * check the frame type.
4635 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
,
4636 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4637 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 24, 6, eaddr
);
4643 * Not present in management frames; addr1 in other
4648 * If the high-order bit of the type value is 0, this
4649 * is a management frame.
4650 * I.e, check "(link[0] & 0x08)".
4652 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4653 b1
= new_block(cstate
, JMP(BPF_JSET
));
4660 b0
= gen_bcmp(cstate
, OR_LINKHDR
, 4, 6, eaddr
);
4663 * AND that with the check of addr1.
4670 * Not present in management frames; addr2, if present,
4675 * Not present in CTS or ACK control frames.
4677 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4678 IEEE80211_FC0_TYPE_MASK
);
4680 b1
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4681 IEEE80211_FC0_SUBTYPE_MASK
);
4683 b2
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4684 IEEE80211_FC0_SUBTYPE_MASK
);
4690 * If the high-order bit of the type value is 0, this
4691 * is a management frame.
4692 * I.e, check "(link[0] & 0x08)".
4694 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
4695 b1
= new_block(cstate
, JMP(BPF_JSET
));
4700 * AND that with the check for frames other than
4701 * CTS and ACK frames.
4708 b1
= gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4717 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4718 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4719 * as the RFC states.)
4721 static struct block
*
4722 gen_ipfchostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
4724 register struct block
*b0
, *b1
;
4728 return gen_bcmp(cstate
, OR_LINKHDR
, 10, 6, eaddr
);
4731 return gen_bcmp(cstate
, OR_LINKHDR
, 2, 6, eaddr
);
4734 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4735 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4741 b0
= gen_ipfchostop(cstate
, eaddr
, Q_SRC
);
4742 b1
= gen_ipfchostop(cstate
, eaddr
, Q_DST
);
4747 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
4751 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
4755 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
4759 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
4763 bpf_error(cstate
, "'ra' is only supported on 802.11");
4767 bpf_error(cstate
, "'ta' is only supported on 802.11");
4775 * This is quite tricky because there may be pad bytes in front of the
4776 * DECNET header, and then there are two possible data packet formats that
4777 * carry both src and dst addresses, plus 5 packet types in a format that
4778 * carries only the src node, plus 2 types that use a different format and
4779 * also carry just the src node.
4783 * Instead of doing those all right, we just look for data packets with
4784 * 0 or 1 bytes of padding. If you want to look at other packets, that
4785 * will require a lot more hacking.
4787 * To add support for filtering on DECNET "areas" (network numbers)
4788 * one would want to add a "mask" argument to this routine. That would
4789 * make the filter even more inefficient, although one could be clever
4790 * and not generate masking instructions if the mask is 0xFFFF.
4792 static struct block
*
4793 gen_dnhostop(compiler_state_t
*cstate
, bpf_u_int32 addr
, int dir
)
4795 struct block
*b0
, *b1
, *b2
, *tmp
;
4796 u_int offset_lh
; /* offset if long header is received */
4797 u_int offset_sh
; /* offset if short header is received */
4802 offset_sh
= 1; /* follows flags */
4803 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4807 offset_sh
= 3; /* follows flags, dstnode */
4808 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4812 /* Inefficient because we do our Calvinball dance twice */
4813 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4814 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4820 /* Inefficient because we do our Calvinball dance twice */
4821 b0
= gen_dnhostop(cstate
, addr
, Q_SRC
);
4822 b1
= gen_dnhostop(cstate
, addr
, Q_DST
);
4827 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4831 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4835 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4839 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for addresses other than 802.11 MAC addresses");
4843 bpf_error(cstate
, "'ra' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4847 bpf_error(cstate
, "'ta' is not a valid qualifier for addresses other than 802.11 MAC addresses");
4853 b0
= gen_linktype(cstate
, ETHERTYPE_DN
);
4854 /* Check for pad = 1, long header case */
4855 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4856 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4857 b1
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_lh
,
4858 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4860 /* Check for pad = 0, long header case */
4861 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4862 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4865 /* Check for pad = 1, short header case */
4866 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_H
,
4867 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4868 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4871 /* Check for pad = 0, short header case */
4872 tmp
= gen_mcmp(cstate
, OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4873 b2
= gen_cmp(cstate
, OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4877 /* Combine with test for cstate->linktype */
4883 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4884 * test the bottom-of-stack bit, and then check the version number
4885 * field in the IP header.
4887 static struct block
*
4888 gen_mpls_linktype(compiler_state_t
*cstate
, int proto
)
4890 struct block
*b0
, *b1
;
4895 /* match the bottom-of-stack bit */
4896 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4897 /* match the IPv4 version number */
4898 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4903 /* match the bottom-of-stack bit */
4904 b0
= gen_mcmp(cstate
, OR_LINKPL
, (u_int
)-2, BPF_B
, 0x01, 0x01);
4905 /* match the IPv4 version number */
4906 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4915 static struct block
*
4916 gen_host(compiler_state_t
*cstate
, bpf_u_int32 addr
, bpf_u_int32 mask
,
4917 int proto
, int dir
, int type
)
4919 struct block
*b0
, *b1
;
4920 const char *typestr
;
4930 b0
= gen_host(cstate
, addr
, mask
, Q_IP
, dir
, type
);
4932 * Only check for non-IPv4 addresses if we're not
4933 * checking MPLS-encapsulated packets.
4935 if (cstate
->label_stack_depth
== 0) {
4936 b1
= gen_host(cstate
, addr
, mask
, Q_ARP
, dir
, type
);
4938 b0
= gen_host(cstate
, addr
, mask
, Q_RARP
, dir
, type
);
4944 bpf_error(cstate
, "link-layer modifier applied to %s", typestr
);
4947 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4950 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4953 return gen_hostop(cstate
, addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4956 bpf_error(cstate
, "'sctp' modifier applied to %s", typestr
);
4959 bpf_error(cstate
, "'tcp' modifier applied to %s", typestr
);
4962 bpf_error(cstate
, "'udp' modifier applied to %s", typestr
);
4965 bpf_error(cstate
, "'icmp' modifier applied to %s", typestr
);
4968 bpf_error(cstate
, "'igmp' modifier applied to %s", typestr
);
4971 bpf_error(cstate
, "'igrp' modifier applied to %s", typestr
);
4974 bpf_error(cstate
, "AppleTalk host filtering not implemented");
4977 return gen_dnhostop(cstate
, addr
, dir
);
4980 bpf_error(cstate
, "LAT host filtering not implemented");
4983 bpf_error(cstate
, "SCA host filtering not implemented");
4986 bpf_error(cstate
, "MOPRC host filtering not implemented");
4989 bpf_error(cstate
, "MOPDL host filtering not implemented");
4992 bpf_error(cstate
, "'ip6' modifier applied to ip host");
4995 bpf_error(cstate
, "'icmp6' modifier applied to %s", typestr
);
4998 bpf_error(cstate
, "'ah' modifier applied to %s", typestr
);
5001 bpf_error(cstate
, "'esp' modifier applied to %s", typestr
);
5004 bpf_error(cstate
, "'pim' modifier applied to %s", typestr
);
5007 bpf_error(cstate
, "'vrrp' modifier applied to %s", typestr
);
5010 bpf_error(cstate
, "AARP host filtering not implemented");
5013 bpf_error(cstate
, "ISO host filtering not implemented");
5016 bpf_error(cstate
, "'esis' modifier applied to %s", typestr
);
5019 bpf_error(cstate
, "'isis' modifier applied to %s", typestr
);
5022 bpf_error(cstate
, "'clnp' modifier applied to %s", typestr
);
5025 bpf_error(cstate
, "'stp' modifier applied to %s", typestr
);
5028 bpf_error(cstate
, "IPX host filtering not implemented");
5031 bpf_error(cstate
, "'netbeui' modifier applied to %s", typestr
);
5034 bpf_error(cstate
, "'l1' modifier applied to %s", typestr
);
5037 bpf_error(cstate
, "'l2' modifier applied to %s", typestr
);
5040 bpf_error(cstate
, "'iih' modifier applied to %s", typestr
);
5043 bpf_error(cstate
, "'snp' modifier applied to %s", typestr
);
5046 bpf_error(cstate
, "'csnp' modifier applied to %s", typestr
);
5049 bpf_error(cstate
, "'psnp' modifier applied to %s", typestr
);
5052 bpf_error(cstate
, "'lsp' modifier applied to %s", typestr
);
5055 bpf_error(cstate
, "'radio' modifier applied to %s", typestr
);
5058 bpf_error(cstate
, "'carp' modifier applied to %s", typestr
);
5067 static struct block
*
5068 gen_host6(compiler_state_t
*cstate
, struct in6_addr
*addr
,
5069 struct in6_addr
*mask
, int proto
, int dir
, int type
)
5071 const char *typestr
;
5081 return gen_host6(cstate
, addr
, mask
, Q_IPV6
, dir
, type
);
5084 bpf_error(cstate
, "link-layer modifier applied to ip6 %s", typestr
);
5087 bpf_error(cstate
, "'ip' modifier applied to ip6 %s", typestr
);
5090 bpf_error(cstate
, "'rarp' modifier applied to ip6 %s", typestr
);
5093 bpf_error(cstate
, "'arp' modifier applied to ip6 %s", typestr
);
5096 bpf_error(cstate
, "'sctp' modifier applied to ip6 %s", typestr
);
5099 bpf_error(cstate
, "'tcp' modifier applied to ip6 %s", typestr
);
5102 bpf_error(cstate
, "'udp' modifier applied to ip6 %s", typestr
);
5105 bpf_error(cstate
, "'icmp' modifier applied to ip6 %s", typestr
);
5108 bpf_error(cstate
, "'igmp' modifier applied to ip6 %s", typestr
);
5111 bpf_error(cstate
, "'igrp' modifier applied to ip6 %s", typestr
);
5114 bpf_error(cstate
, "AppleTalk modifier applied to ip6 %s", typestr
);
5117 bpf_error(cstate
, "'decnet' modifier applied to ip6 %s", typestr
);
5120 bpf_error(cstate
, "'lat' modifier applied to ip6 %s", typestr
);
5123 bpf_error(cstate
, "'sca' modifier applied to ip6 %s", typestr
);
5126 bpf_error(cstate
, "'moprc' modifier applied to ip6 %s", typestr
);
5129 bpf_error(cstate
, "'mopdl' modifier applied to ip6 %s", typestr
);
5132 return gen_hostop6(cstate
, addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
5135 bpf_error(cstate
, "'icmp6' modifier applied to ip6 %s", typestr
);
5138 bpf_error(cstate
, "'ah' modifier applied to ip6 %s", typestr
);
5141 bpf_error(cstate
, "'esp' modifier applied to ip6 %s", typestr
);
5144 bpf_error(cstate
, "'pim' modifier applied to ip6 %s", typestr
);
5147 bpf_error(cstate
, "'vrrp' modifier applied to ip6 %s", typestr
);
5150 bpf_error(cstate
, "'aarp' modifier applied to ip6 %s", typestr
);
5153 bpf_error(cstate
, "'iso' modifier applied to ip6 %s", typestr
);
5156 bpf_error(cstate
, "'esis' modifier applied to ip6 %s", typestr
);
5159 bpf_error(cstate
, "'isis' modifier applied to ip6 %s", typestr
);
5162 bpf_error(cstate
, "'clnp' modifier applied to ip6 %s", typestr
);
5165 bpf_error(cstate
, "'stp' modifier applied to ip6 %s", typestr
);
5168 bpf_error(cstate
, "'ipx' modifier applied to ip6 %s", typestr
);
5171 bpf_error(cstate
, "'netbeui' modifier applied to ip6 %s", typestr
);
5174 bpf_error(cstate
, "'l1' modifier applied to ip6 %s", typestr
);
5177 bpf_error(cstate
, "'l2' modifier applied to ip6 %s", typestr
);
5180 bpf_error(cstate
, "'iih' modifier applied to ip6 %s", typestr
);
5183 bpf_error(cstate
, "'snp' modifier applied to ip6 %s", typestr
);
5186 bpf_error(cstate
, "'csnp' modifier applied to ip6 %s", typestr
);
5189 bpf_error(cstate
, "'psnp' modifier applied to ip6 %s", typestr
);
5192 bpf_error(cstate
, "'lsp' modifier applied to ip6 %s", typestr
);
5195 bpf_error(cstate
, "'radio' modifier applied to ip6 %s", typestr
);
5198 bpf_error(cstate
, "'carp' modifier applied to ip6 %s", typestr
);
5208 static struct block
*
5209 gen_gateway(compiler_state_t
*cstate
, const u_char
*eaddr
,
5210 struct addrinfo
*alist
, int proto
, int dir
)
5212 struct block
*b0
, *b1
, *tmp
;
5213 struct addrinfo
*ai
;
5214 struct sockaddr_in
*sin
;
5217 bpf_error(cstate
, "direction applied to 'gateway'");
5224 switch (cstate
->linktype
) {
5226 case DLT_NETANALYZER
:
5227 case DLT_NETANALYZER_TRANSPARENT
:
5228 b1
= gen_prevlinkhdr_check(cstate
);
5229 b0
= gen_ehostop(cstate
, eaddr
, Q_OR
);
5234 b0
= gen_fhostop(cstate
, eaddr
, Q_OR
);
5237 b0
= gen_thostop(cstate
, eaddr
, Q_OR
);
5239 case DLT_IEEE802_11
:
5240 case DLT_PRISM_HEADER
:
5241 case DLT_IEEE802_11_RADIO_AVS
:
5242 case DLT_IEEE802_11_RADIO
:
5244 b0
= gen_wlanhostop(cstate
, eaddr
, Q_OR
);
5248 * This is LLC-multiplexed traffic; if it were
5249 * LANE, cstate->linktype would have been set to
5253 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5255 case DLT_IP_OVER_FC
:
5256 b0
= gen_ipfchostop(cstate
, eaddr
, Q_OR
);
5260 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5263 for (ai
= alist
; ai
!= NULL
; ai
= ai
->ai_next
) {
5265 * Does it have an address?
5267 if (ai
->ai_addr
!= NULL
) {
5269 * Yes. Is it an IPv4 address?
5271 if (ai
->ai_addr
->sa_family
== AF_INET
) {
5273 * Generate an entry for it.
5275 sin
= (struct sockaddr_in
*)ai
->ai_addr
;
5276 tmp
= gen_host(cstate
,
5277 ntohl(sin
->sin_addr
.s_addr
),
5278 0xffffffff, proto
, Q_OR
, Q_HOST
);
5280 * Is it the *first* IPv4 address?
5284 * Yes, so start with it.
5289 * No, so OR it into the
5301 * No IPv4 addresses found.
5309 bpf_error(cstate
, "illegal modifier of 'gateway'");
5314 static struct block
*
5315 gen_proto_abbrev_internal(compiler_state_t
*cstate
, int proto
)
5323 b1
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
5324 b0
= gen_proto(cstate
, IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
5329 b1
= gen_proto(cstate
, IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
5330 b0
= gen_proto(cstate
, IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
5335 b1
= gen_proto(cstate
, IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
5336 b0
= gen_proto(cstate
, IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
5341 b1
= gen_proto(cstate
, IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
5344 #ifndef IPPROTO_IGMP
5345 #define IPPROTO_IGMP 2
5349 b1
= gen_proto(cstate
, IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
5352 #ifndef IPPROTO_IGRP
5353 #define IPPROTO_IGRP 9
5356 b1
= gen_proto(cstate
, IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
5360 #define IPPROTO_PIM 103
5364 b1
= gen_proto(cstate
, IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
5365 b0
= gen_proto(cstate
, IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
5369 #ifndef IPPROTO_VRRP
5370 #define IPPROTO_VRRP 112
5374 b1
= gen_proto(cstate
, IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
5377 #ifndef IPPROTO_CARP
5378 #define IPPROTO_CARP 112
5382 b1
= gen_proto(cstate
, IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5386 b1
= gen_linktype(cstate
, ETHERTYPE_IP
);
5390 b1
= gen_linktype(cstate
, ETHERTYPE_ARP
);
5394 b1
= gen_linktype(cstate
, ETHERTYPE_REVARP
);
5398 bpf_error(cstate
, "link layer applied in wrong context");
5401 b1
= gen_linktype(cstate
, ETHERTYPE_ATALK
);
5405 b1
= gen_linktype(cstate
, ETHERTYPE_AARP
);
5409 b1
= gen_linktype(cstate
, ETHERTYPE_DN
);
5413 b1
= gen_linktype(cstate
, ETHERTYPE_SCA
);
5417 b1
= gen_linktype(cstate
, ETHERTYPE_LAT
);
5421 b1
= gen_linktype(cstate
, ETHERTYPE_MOPDL
);
5425 b1
= gen_linktype(cstate
, ETHERTYPE_MOPRC
);
5429 b1
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5432 #ifndef IPPROTO_ICMPV6
5433 #define IPPROTO_ICMPV6 58
5436 b1
= gen_proto(cstate
, IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5440 #define IPPROTO_AH 51
5443 b1
= gen_proto(cstate
, IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5444 b0
= gen_proto(cstate
, IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5449 #define IPPROTO_ESP 50
5452 b1
= gen_proto(cstate
, IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5453 b0
= gen_proto(cstate
, IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5458 b1
= gen_linktype(cstate
, LLCSAP_ISONS
);
5462 b1
= gen_proto(cstate
, ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5466 b1
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5469 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5470 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5471 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5473 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5475 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5477 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5481 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5482 b0
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5483 b1
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5485 b0
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5487 b0
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5489 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5493 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5494 b0
= gen_proto(cstate
, ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5495 b1
= gen_proto(cstate
, ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5497 b0
= gen_proto(cstate
, ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5502 b0
= gen_proto(cstate
, ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5503 b1
= gen_proto(cstate
, ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5508 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5509 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5511 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5513 b0
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5518 b0
= gen_proto(cstate
, ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5519 b1
= gen_proto(cstate
, ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5524 b0
= gen_proto(cstate
, ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5525 b1
= gen_proto(cstate
, ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5530 b1
= gen_proto(cstate
, ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5534 b1
= gen_linktype(cstate
, LLCSAP_8021D
);
5538 b1
= gen_linktype(cstate
, LLCSAP_IPX
);
5542 b1
= gen_linktype(cstate
, LLCSAP_NETBEUI
);
5546 bpf_error(cstate
, "'radio' is not a valid protocol type");
5555 gen_proto_abbrev(compiler_state_t
*cstate
, int proto
)
5558 * Catch errors reported by us and routines below us, and return NULL
5561 if (setjmp(cstate
->top_ctx
))
5564 return gen_proto_abbrev_internal(cstate
, proto
);
5567 static struct block
*
5568 gen_ipfrag(compiler_state_t
*cstate
)
5573 /* not IPv4 frag other than the first frag */
5574 s
= gen_load_a(cstate
, OR_LINKPL
, 6, BPF_H
);
5575 b
= new_block(cstate
, JMP(BPF_JSET
));
5584 * Generate a comparison to a port value in the transport-layer header
5585 * at the specified offset from the beginning of that header.
5587 * XXX - this handles a variable-length prefix preceding the link-layer
5588 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5589 * variable-length link-layer headers (such as Token Ring or 802.11
5592 static struct block
*
5593 gen_portatom(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5595 return gen_cmp(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v
);
5598 static struct block
*
5599 gen_portatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v
)
5601 return gen_cmp(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v
);
5605 gen_portop(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5607 struct block
*b0
, *b1
, *tmp
;
5609 /* ip proto 'proto' and not a fragment other than the first fragment */
5610 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5611 b0
= gen_ipfrag(cstate
);
5616 b1
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5620 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5624 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5625 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5631 tmp
= gen_portatom(cstate
, 0, (bpf_int32
)port
);
5632 b1
= gen_portatom(cstate
, 2, (bpf_int32
)port
);
5637 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for ports");
5641 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for ports");
5645 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for ports");
5649 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for ports");
5653 bpf_error(cstate
, "'ra' is not a valid qualifier for ports");
5657 bpf_error(cstate
, "'ta' is not a valid qualifier for ports");
5668 static struct block
*
5669 gen_port(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5671 struct block
*b0
, *b1
, *tmp
;
5676 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5677 * not LLC encapsulation with LLCSAP_IP.
5679 * For IEEE 802 networks - which includes 802.5 token ring
5680 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5681 * says that SNAP encapsulation is used, not LLC encapsulation
5684 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5685 * RFC 2225 say that SNAP encapsulation is used, not LLC
5686 * encapsulation with LLCSAP_IP.
5688 * So we always check for ETHERTYPE_IP.
5690 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5696 b1
= gen_portop(cstate
, port
, ip_proto
, dir
);
5700 tmp
= gen_portop(cstate
, port
, IPPROTO_TCP
, dir
);
5701 b1
= gen_portop(cstate
, port
, IPPROTO_UDP
, dir
);
5703 tmp
= gen_portop(cstate
, port
, IPPROTO_SCTP
, dir
);
5715 gen_portop6(compiler_state_t
*cstate
, int port
, int proto
, int dir
)
5717 struct block
*b0
, *b1
, *tmp
;
5719 /* ip6 proto 'proto' */
5720 /* XXX - catch the first fragment of a fragmented packet? */
5721 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5725 b1
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5729 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5733 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5734 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5740 tmp
= gen_portatom6(cstate
, 0, (bpf_int32
)port
);
5741 b1
= gen_portatom6(cstate
, 2, (bpf_int32
)port
);
5753 static struct block
*
5754 gen_port6(compiler_state_t
*cstate
, int port
, int ip_proto
, int dir
)
5756 struct block
*b0
, *b1
, *tmp
;
5758 /* link proto ip6 */
5759 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5765 b1
= gen_portop6(cstate
, port
, ip_proto
, dir
);
5769 tmp
= gen_portop6(cstate
, port
, IPPROTO_TCP
, dir
);
5770 b1
= gen_portop6(cstate
, port
, IPPROTO_UDP
, dir
);
5772 tmp
= gen_portop6(cstate
, port
, IPPROTO_SCTP
, dir
);
5783 /* gen_portrange code */
5784 static struct block
*
5785 gen_portrangeatom(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5788 struct block
*b1
, *b2
;
5792 * Reverse the order of the ports, so v1 is the lower one.
5801 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5802 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5810 gen_portrangeop(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5813 struct block
*b0
, *b1
, *tmp
;
5815 /* ip proto 'proto' and not a fragment other than the first fragment */
5816 tmp
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5817 b0
= gen_ipfrag(cstate
);
5822 b1
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5826 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5830 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5831 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5837 tmp
= gen_portrangeatom(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5838 b1
= gen_portrangeatom(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5843 bpf_error(cstate
, "'addr1' and 'address1' are not valid qualifiers for port ranges");
5847 bpf_error(cstate
, "'addr2' and 'address2' are not valid qualifiers for port ranges");
5851 bpf_error(cstate
, "'addr3' and 'address3' are not valid qualifiers for port ranges");
5855 bpf_error(cstate
, "'addr4' and 'address4' are not valid qualifiers for port ranges");
5859 bpf_error(cstate
, "'ra' is not a valid qualifier for port ranges");
5863 bpf_error(cstate
, "'ta' is not a valid qualifier for port ranges");
5874 static struct block
*
5875 gen_portrange(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5878 struct block
*b0
, *b1
, *tmp
;
5881 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
5887 b1
= gen_portrangeop(cstate
, port1
, port2
, ip_proto
, dir
);
5891 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5892 b1
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5894 tmp
= gen_portrangeop(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
5905 static struct block
*
5906 gen_portrangeatom6(compiler_state_t
*cstate
, int off
, bpf_int32 v1
,
5909 struct block
*b1
, *b2
;
5913 * Reverse the order of the ports, so v1 is the lower one.
5922 b1
= gen_cmp_ge(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5923 b2
= gen_cmp_le(cstate
, OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5931 gen_portrangeop6(compiler_state_t
*cstate
, int port1
, int port2
, int proto
,
5934 struct block
*b0
, *b1
, *tmp
;
5936 /* ip6 proto 'proto' */
5937 /* XXX - catch the first fragment of a fragmented packet? */
5938 b0
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5942 b1
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5946 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5950 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5951 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5957 tmp
= gen_portrangeatom6(cstate
, 0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5958 b1
= gen_portrangeatom6(cstate
, 2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5970 static struct block
*
5971 gen_portrange6(compiler_state_t
*cstate
, int port1
, int port2
, int ip_proto
,
5974 struct block
*b0
, *b1
, *tmp
;
5976 /* link proto ip6 */
5977 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
5983 b1
= gen_portrangeop6(cstate
, port1
, port2
, ip_proto
, dir
);
5987 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_TCP
, dir
);
5988 b1
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_UDP
, dir
);
5990 tmp
= gen_portrangeop6(cstate
, port1
, port2
, IPPROTO_SCTP
, dir
);
6002 lookup_proto(compiler_state_t
*cstate
, const char *name
, int proto
)
6011 v
= pcap_nametoproto(name
);
6012 if (v
== PROTO_UNDEF
)
6013 bpf_error(cstate
, "unknown ip proto '%s'", name
);
6017 /* XXX should look up h/w protocol type based on cstate->linktype */
6018 v
= pcap_nametoeproto(name
);
6019 if (v
== PROTO_UNDEF
) {
6020 v
= pcap_nametollc(name
);
6021 if (v
== PROTO_UNDEF
)
6022 bpf_error(cstate
, "unknown ether proto '%s'", name
);
6027 if (strcmp(name
, "esis") == 0)
6029 else if (strcmp(name
, "isis") == 0)
6031 else if (strcmp(name
, "clnp") == 0)
6034 bpf_error(cstate
, "unknown osi proto '%s'", name
);
6046 gen_joinsp(struct stmt
**s
, int n
)
6052 static struct block
*
6053 gen_protochain(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6055 #ifdef NO_PROTOCHAIN
6056 return gen_proto(cstate
, v
, proto
, dir
);
6058 struct block
*b0
, *b
;
6059 struct slist
*s
[100];
6060 int fix2
, fix3
, fix4
, fix5
;
6061 int ahcheck
, again
, end
;
6063 int reg2
= alloc_reg(cstate
);
6065 memset(s
, 0, sizeof(s
));
6066 fix3
= fix4
= fix5
= 0;
6073 b0
= gen_protochain(cstate
, v
, Q_IP
, dir
);
6074 b
= gen_protochain(cstate
, v
, Q_IPV6
, dir
);
6078 bpf_error(cstate
, "bad protocol applied for 'protochain'");
6083 * We don't handle variable-length prefixes before the link-layer
6084 * header, or variable-length link-layer headers, here yet.
6085 * We might want to add BPF instructions to do the protochain
6086 * work, to simplify that and, on platforms that have a BPF
6087 * interpreter with the new instructions, let the filtering
6088 * be done in the kernel. (We already require a modified BPF
6089 * engine to do the protochain stuff, to support backward
6090 * branches, and backward branch support is unlikely to appear
6091 * in kernel BPF engines.)
6093 if (cstate
->off_linkpl
.is_variable
)
6094 bpf_error(cstate
, "'protochain' not supported with variable length headers");
6096 cstate
->no_optimize
= 1; /* this code is not compatible with optimizer yet */
6099 * s[0] is a dummy entry to protect other BPF insn from damage
6100 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
6101 * hard to find interdependency made by jump table fixup.
6104 s
[i
] = new_stmt(cstate
, 0); /*dummy*/
6109 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6112 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6113 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 9;
6115 /* X = ip->ip_hl << 2 */
6116 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_MSH
|BPF_B
);
6117 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6122 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6124 /* A = ip6->ip_nxt */
6125 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_ABS
|BPF_B
);
6126 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6;
6128 /* X = sizeof(struct ip6_hdr) */
6129 s
[i
] = new_stmt(cstate
, BPF_LDX
|BPF_IMM
);
6135 bpf_error(cstate
, "unsupported proto to gen_protochain");
6139 /* again: if (A == v) goto end; else fall through; */
6141 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6143 s
[i
]->s
.jt
= NULL
; /*later*/
6144 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6148 #ifndef IPPROTO_NONE
6149 #define IPPROTO_NONE 59
6151 /* if (A == IPPROTO_NONE) goto end */
6152 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6153 s
[i
]->s
.jt
= NULL
; /*later*/
6154 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6155 s
[i
]->s
.k
= IPPROTO_NONE
;
6156 s
[fix5
]->s
.jf
= s
[i
];
6160 if (proto
== Q_IPV6
) {
6161 int v6start
, v6end
, v6advance
, j
;
6164 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
6165 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6166 s
[i
]->s
.jt
= NULL
; /*later*/
6167 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6168 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
6169 s
[fix2
]->s
.jf
= s
[i
];
6171 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
6172 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6173 s
[i
]->s
.jt
= NULL
; /*later*/
6174 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6175 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
6177 /* if (A == IPPROTO_ROUTING) goto v6advance */
6178 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6179 s
[i
]->s
.jt
= NULL
; /*later*/
6180 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
6181 s
[i
]->s
.k
= IPPROTO_ROUTING
;
6183 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
6184 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6185 s
[i
]->s
.jt
= NULL
; /*later*/
6186 s
[i
]->s
.jf
= NULL
; /*later*/
6187 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
6197 * A = P[X + packet head];
6198 * X = X + (P[X + packet head + 1] + 1) * 8;
6200 /* A = P[X + packet head] */
6201 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6202 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6205 s
[i
] = new_stmt(cstate
, BPF_ST
);
6208 /* A = P[X + packet head + 1]; */
6209 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6210 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 1;
6213 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6217 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6221 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
6225 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6228 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6232 /* goto again; (must use BPF_JA for backward jump) */
6233 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6234 s
[i
]->s
.k
= again
- i
- 1;
6235 s
[i
- 1]->s
.jf
= s
[i
];
6239 for (j
= v6start
; j
<= v6end
; j
++)
6240 s
[j
]->s
.jt
= s
[v6advance
];
6243 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6245 s
[fix2
]->s
.jf
= s
[i
];
6251 /* if (A == IPPROTO_AH) then fall through; else goto end; */
6252 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JEQ
|BPF_K
);
6253 s
[i
]->s
.jt
= NULL
; /*later*/
6254 s
[i
]->s
.jf
= NULL
; /*later*/
6255 s
[i
]->s
.k
= IPPROTO_AH
;
6257 s
[fix3
]->s
.jf
= s
[ahcheck
];
6264 * X = X + (P[X + 1] + 2) * 4;
6267 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6269 /* A = P[X + packet head]; */
6270 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6271 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6274 s
[i
] = new_stmt(cstate
, BPF_ST
);
6278 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
6281 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6285 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6287 /* A = P[X + packet head] */
6288 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
6289 s
[i
]->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
6292 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6296 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
6300 s
[i
] = new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
6303 s
[i
] = new_stmt(cstate
, BPF_LD
|BPF_MEM
);
6307 /* goto again; (must use BPF_JA for backward jump) */
6308 s
[i
] = new_stmt(cstate
, BPF_JMP
|BPF_JA
);
6309 s
[i
]->s
.k
= again
- i
- 1;
6314 s
[i
] = new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
6316 s
[fix2
]->s
.jt
= s
[end
];
6317 s
[fix4
]->s
.jf
= s
[end
];
6318 s
[fix5
]->s
.jt
= s
[end
];
6325 for (i
= 0; i
< max
- 1; i
++)
6326 s
[i
]->next
= s
[i
+ 1];
6327 s
[max
- 1]->next
= NULL
;
6332 b
= new_block(cstate
, JMP(BPF_JEQ
));
6333 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
6336 free_reg(cstate
, reg2
);
6343 static struct block
*
6344 gen_check_802_11_data_frame(compiler_state_t
*cstate
)
6347 struct block
*b0
, *b1
;
6350 * A data frame has the 0x08 bit (b3) in the frame control field set
6351 * and the 0x04 bit (b2) clear.
6353 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6354 b0
= new_block(cstate
, JMP(BPF_JSET
));
6358 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
6359 b1
= new_block(cstate
, JMP(BPF_JSET
));
6370 * Generate code that checks whether the packet is a packet for protocol
6371 * <proto> and whether the type field in that protocol's header has
6372 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
6373 * IP packet and checks the protocol number in the IP header against <v>.
6375 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
6376 * against Q_IP and Q_IPV6.
6378 static struct block
*
6379 gen_proto(compiler_state_t
*cstate
, int v
, int proto
, int dir
)
6381 struct block
*b0
, *b1
;
6386 if (dir
!= Q_DEFAULT
)
6387 bpf_error(cstate
, "direction applied to 'proto'");
6391 b0
= gen_proto(cstate
, v
, Q_IP
, dir
);
6392 b1
= gen_proto(cstate
, v
, Q_IPV6
, dir
);
6397 return gen_linktype(cstate
, v
);
6401 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
6402 * not LLC encapsulation with LLCSAP_IP.
6404 * For IEEE 802 networks - which includes 802.5 token ring
6405 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
6406 * says that SNAP encapsulation is used, not LLC encapsulation
6409 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
6410 * RFC 2225 say that SNAP encapsulation is used, not LLC
6411 * encapsulation with LLCSAP_IP.
6413 * So we always check for ETHERTYPE_IP.
6415 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
6417 b1
= gen_cmp(cstate
, OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6419 b1
= gen_protochain(cstate
, v
, Q_IP
);
6425 bpf_error(cstate
, "arp does not encapsulate another protocol");
6429 bpf_error(cstate
, "rarp does not encapsulate another protocol");
6433 bpf_error(cstate
, "'sctp proto' is bogus");
6437 bpf_error(cstate
, "'tcp proto' is bogus");
6441 bpf_error(cstate
, "'udp proto' is bogus");
6445 bpf_error(cstate
, "'icmp proto' is bogus");
6449 bpf_error(cstate
, "'igmp proto' is bogus");
6453 bpf_error(cstate
, "'igrp proto' is bogus");
6457 bpf_error(cstate
, "AppleTalk encapsulation is not specifiable");
6461 bpf_error(cstate
, "DECNET encapsulation is not specifiable");
6465 bpf_error(cstate
, "LAT does not encapsulate another protocol");
6469 bpf_error(cstate
, "SCA does not encapsulate another protocol");
6473 bpf_error(cstate
, "MOPRC does not encapsulate another protocol");
6477 bpf_error(cstate
, "MOPDL does not encapsulate another protocol");
6481 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
6484 * Also check for a fragment header before the final
6487 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6488 b1
= gen_cmp(cstate
, OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6490 b2
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6493 b1
= gen_protochain(cstate
, v
, Q_IPV6
);
6499 bpf_error(cstate
, "'icmp6 proto' is bogus");
6503 bpf_error(cstate
, "'ah proto' is bogus");
6507 bpf_error(cstate
, "'ah proto' is bogus");
6511 bpf_error(cstate
, "'pim proto' is bogus");
6515 bpf_error(cstate
, "'vrrp proto' is bogus");
6519 bpf_error(cstate
, "'aarp proto' is bogus");
6523 switch (cstate
->linktype
) {
6527 * Frame Relay packets typically have an OSI
6528 * NLPID at the beginning; "gen_linktype(cstate, LLCSAP_ISONS)"
6529 * generates code to check for all the OSI
6530 * NLPIDs, so calling it and then adding a check
6531 * for the particular NLPID for which we're
6532 * looking is bogus, as we can just check for
6535 * What we check for is the NLPID and a frame
6536 * control field value of UI, i.e. 0x03 followed
6539 * XXX - assumes a 2-byte Frame Relay header with
6540 * DLCI and flags. What if the address is longer?
6542 * XXX - what about SNAP-encapsulated frames?
6544 return gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6550 * Cisco uses an Ethertype lookalike - for OSI,
6553 b0
= gen_linktype(cstate
, LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6554 /* OSI in C-HDLC is stuffed with a fudge byte */
6555 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 1, BPF_B
, (bpf_int32
)v
);
6560 b0
= gen_linktype(cstate
, LLCSAP_ISONS
);
6561 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 0, BPF_B
, (bpf_int32
)v
);
6567 bpf_error(cstate
, "'esis proto' is bogus");
6571 b0
= gen_proto(cstate
, ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6573 * 4 is the offset of the PDU type relative to the IS-IS
6576 b1
= gen_cmp(cstate
, OR_LINKPL_NOSNAP
, 4, BPF_B
, (bpf_int32
)v
);
6581 bpf_error(cstate
, "'clnp proto' is not supported");
6585 bpf_error(cstate
, "'stp proto' is bogus");
6589 bpf_error(cstate
, "'ipx proto' is bogus");
6593 bpf_error(cstate
, "'netbeui proto' is bogus");
6597 bpf_error(cstate
, "'l1 proto' is bogus");
6601 bpf_error(cstate
, "'l2 proto' is bogus");
6605 bpf_error(cstate
, "'iih proto' is bogus");
6609 bpf_error(cstate
, "'snp proto' is bogus");
6613 bpf_error(cstate
, "'csnp proto' is bogus");
6617 bpf_error(cstate
, "'psnp proto' is bogus");
6621 bpf_error(cstate
, "'lsp proto' is bogus");
6625 bpf_error(cstate
, "'radio proto' is bogus");
6629 bpf_error(cstate
, "'carp proto' is bogus");
6640 gen_scode(compiler_state_t
*cstate
, const char *name
, struct qual q
)
6642 int proto
= q
.proto
;
6646 bpf_u_int32 mask
, addr
;
6647 struct addrinfo
*res
, *res0
;
6648 struct sockaddr_in
*sin4
;
6651 struct sockaddr_in6
*sin6
;
6652 struct in6_addr mask128
;
6654 struct block
*b
, *tmp
;
6655 int port
, real_proto
;
6659 * Catch errors reported by us and routines below us, and return NULL
6662 if (setjmp(cstate
->top_ctx
))
6668 addr
= pcap_nametonetaddr(name
);
6670 bpf_error(cstate
, "unknown network '%s'", name
);
6671 /* Left justify network addr and calculate its network mask */
6673 while (addr
&& (addr
& 0xff000000) == 0) {
6677 return gen_host(cstate
, addr
, mask
, proto
, dir
, q
.addr
);
6681 if (proto
== Q_LINK
) {
6682 switch (cstate
->linktype
) {
6685 case DLT_NETANALYZER
:
6686 case DLT_NETANALYZER_TRANSPARENT
:
6687 eaddr
= pcap_ether_hostton(name
);
6690 "unknown ether host '%s'", name
);
6691 tmp
= gen_prevlinkhdr_check(cstate
);
6692 b
= gen_ehostop(cstate
, eaddr
, dir
);
6699 eaddr
= pcap_ether_hostton(name
);
6702 "unknown FDDI host '%s'", name
);
6703 b
= gen_fhostop(cstate
, eaddr
, dir
);
6708 eaddr
= pcap_ether_hostton(name
);
6711 "unknown token ring host '%s'", name
);
6712 b
= gen_thostop(cstate
, eaddr
, dir
);
6716 case DLT_IEEE802_11
:
6717 case DLT_PRISM_HEADER
:
6718 case DLT_IEEE802_11_RADIO_AVS
:
6719 case DLT_IEEE802_11_RADIO
:
6721 eaddr
= pcap_ether_hostton(name
);
6724 "unknown 802.11 host '%s'", name
);
6725 b
= gen_wlanhostop(cstate
, eaddr
, dir
);
6729 case DLT_IP_OVER_FC
:
6730 eaddr
= pcap_ether_hostton(name
);
6733 "unknown Fibre Channel host '%s'", name
);
6734 b
= gen_ipfchostop(cstate
, eaddr
, dir
);
6739 bpf_error(cstate
, "only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6740 } else if (proto
== Q_DECNET
) {
6741 unsigned short dn_addr
;
6743 if (!__pcap_nametodnaddr(name
, &dn_addr
)) {
6745 bpf_error(cstate
, "unknown decnet host name '%s'\n", name
);
6747 bpf_error(cstate
, "decnet name support not included, '%s' cannot be translated\n",
6752 * I don't think DECNET hosts can be multihomed, so
6753 * there is no need to build up a list of addresses
6755 return (gen_host(cstate
, dn_addr
, 0, proto
, dir
, q
.addr
));
6758 memset(&mask128
, 0xff, sizeof(mask128
));
6760 res0
= res
= pcap_nametoaddrinfo(name
);
6762 bpf_error(cstate
, "unknown host '%s'", name
);
6769 if (cstate
->off_linktype
.constant_part
== OFFSET_NOT_SET
&&
6770 tproto
== Q_DEFAULT
) {
6776 for (res
= res0
; res
; res
= res
->ai_next
) {
6777 switch (res
->ai_family
) {
6780 if (tproto
== Q_IPV6
)
6784 sin4
= (struct sockaddr_in
*)
6786 tmp
= gen_host(cstate
, ntohl(sin4
->sin_addr
.s_addr
),
6787 0xffffffff, tproto
, dir
, q
.addr
);
6791 if (tproto6
== Q_IP
)
6794 sin6
= (struct sockaddr_in6
*)
6796 tmp
= gen_host6(cstate
, &sin6
->sin6_addr
,
6797 &mask128
, tproto6
, dir
, q
.addr
);
6810 bpf_error(cstate
, "unknown host '%s'%s", name
,
6811 (proto
== Q_DEFAULT
)
6813 : " for specified address family");
6819 if (proto
!= Q_DEFAULT
&&
6820 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6821 bpf_error(cstate
, "illegal qualifier of 'port'");
6822 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6823 bpf_error(cstate
, "unknown port '%s'", name
);
6824 if (proto
== Q_UDP
) {
6825 if (real_proto
== IPPROTO_TCP
)
6826 bpf_error(cstate
, "port '%s' is tcp", name
);
6827 else if (real_proto
== IPPROTO_SCTP
)
6828 bpf_error(cstate
, "port '%s' is sctp", name
);
6830 /* override PROTO_UNDEF */
6831 real_proto
= IPPROTO_UDP
;
6833 if (proto
== Q_TCP
) {
6834 if (real_proto
== IPPROTO_UDP
)
6835 bpf_error(cstate
, "port '%s' is udp", name
);
6837 else if (real_proto
== IPPROTO_SCTP
)
6838 bpf_error(cstate
, "port '%s' is sctp", name
);
6840 /* override PROTO_UNDEF */
6841 real_proto
= IPPROTO_TCP
;
6843 if (proto
== Q_SCTP
) {
6844 if (real_proto
== IPPROTO_UDP
)
6845 bpf_error(cstate
, "port '%s' is udp", name
);
6847 else if (real_proto
== IPPROTO_TCP
)
6848 bpf_error(cstate
, "port '%s' is tcp", name
);
6850 /* override PROTO_UNDEF */
6851 real_proto
= IPPROTO_SCTP
;
6854 bpf_error(cstate
, "illegal port number %d < 0", port
);
6856 bpf_error(cstate
, "illegal port number %d > 65535", port
);
6857 b
= gen_port(cstate
, port
, real_proto
, dir
);
6858 gen_or(gen_port6(cstate
, port
, real_proto
, dir
), b
);
6862 if (proto
!= Q_DEFAULT
&&
6863 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6864 bpf_error(cstate
, "illegal qualifier of 'portrange'");
6865 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6866 bpf_error(cstate
, "unknown port in range '%s'", name
);
6867 if (proto
== Q_UDP
) {
6868 if (real_proto
== IPPROTO_TCP
)
6869 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6870 else if (real_proto
== IPPROTO_SCTP
)
6871 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6873 /* override PROTO_UNDEF */
6874 real_proto
= IPPROTO_UDP
;
6876 if (proto
== Q_TCP
) {
6877 if (real_proto
== IPPROTO_UDP
)
6878 bpf_error(cstate
, "port in range '%s' is udp", name
);
6879 else if (real_proto
== IPPROTO_SCTP
)
6880 bpf_error(cstate
, "port in range '%s' is sctp", name
);
6882 /* override PROTO_UNDEF */
6883 real_proto
= IPPROTO_TCP
;
6885 if (proto
== Q_SCTP
) {
6886 if (real_proto
== IPPROTO_UDP
)
6887 bpf_error(cstate
, "port in range '%s' is udp", name
);
6888 else if (real_proto
== IPPROTO_TCP
)
6889 bpf_error(cstate
, "port in range '%s' is tcp", name
);
6891 /* override PROTO_UNDEF */
6892 real_proto
= IPPROTO_SCTP
;
6895 bpf_error(cstate
, "illegal port number %d < 0", port1
);
6897 bpf_error(cstate
, "illegal port number %d > 65535", port1
);
6899 bpf_error(cstate
, "illegal port number %d < 0", port2
);
6901 bpf_error(cstate
, "illegal port number %d > 65535", port2
);
6903 b
= gen_portrange(cstate
, port1
, port2
, real_proto
, dir
);
6904 gen_or(gen_portrange6(cstate
, port1
, port2
, real_proto
, dir
), b
);
6909 eaddr
= pcap_ether_hostton(name
);
6911 bpf_error(cstate
, "unknown ether host: %s", name
);
6913 res
= pcap_nametoaddrinfo(name
);
6916 bpf_error(cstate
, "unknown host '%s'", name
);
6917 b
= gen_gateway(cstate
, eaddr
, res
, proto
, dir
);
6921 bpf_error(cstate
, "unknown host '%s'", name
);
6924 bpf_error(cstate
, "'gateway' not supported in this configuration");
6928 real_proto
= lookup_proto(cstate
, name
, proto
);
6929 if (real_proto
>= 0)
6930 return gen_proto(cstate
, real_proto
, proto
, dir
);
6932 bpf_error(cstate
, "unknown protocol: %s", name
);
6935 real_proto
= lookup_proto(cstate
, name
, proto
);
6936 if (real_proto
>= 0)
6937 return gen_protochain(cstate
, real_proto
, proto
, dir
);
6939 bpf_error(cstate
, "unknown protocol: %s", name
);
6950 gen_mcode(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
6951 unsigned int masklen
, struct qual q
)
6953 register int nlen
, mlen
;
6957 * Catch errors reported by us and routines below us, and return NULL
6960 if (setjmp(cstate
->top_ctx
))
6963 nlen
= __pcap_atoin(s1
, &n
);
6964 /* Promote short ipaddr */
6968 mlen
= __pcap_atoin(s2
, &m
);
6969 /* Promote short ipaddr */
6972 bpf_error(cstate
, "non-network bits set in \"%s mask %s\"",
6975 /* Convert mask len to mask */
6977 bpf_error(cstate
, "mask length must be <= 32");
6980 * X << 32 is not guaranteed by C to be 0; it's
6985 m
= 0xffffffff << (32 - masklen
);
6987 bpf_error(cstate
, "non-network bits set in \"%s/%d\"",
6994 return gen_host(cstate
, n
, m
, q
.proto
, q
.dir
, q
.addr
);
6997 bpf_error(cstate
, "Mask syntax for networks only");
7004 gen_ncode(compiler_state_t
*cstate
, const char *s
, bpf_u_int32 v
, struct qual q
)
7012 * Catch errors reported by us and routines below us, and return NULL
7015 if (setjmp(cstate
->top_ctx
))
7022 else if (q
.proto
== Q_DECNET
) {
7023 vlen
= __pcap_atodn(s
, &v
);
7025 bpf_error(cstate
, "malformed decnet address '%s'", s
);
7027 vlen
= __pcap_atoin(s
, &v
);
7034 if (proto
== Q_DECNET
)
7035 return gen_host(cstate
, v
, 0, proto
, dir
, q
.addr
);
7036 else if (proto
== Q_LINK
) {
7037 bpf_error(cstate
, "illegal link layer address");
7040 if (s
== NULL
&& q
.addr
== Q_NET
) {
7041 /* Promote short net number */
7042 while (v
&& (v
& 0xff000000) == 0) {
7047 /* Promote short ipaddr */
7049 mask
<<= 32 - vlen
;
7051 return gen_host(cstate
, v
, mask
, proto
, dir
, q
.addr
);
7056 proto
= IPPROTO_UDP
;
7057 else if (proto
== Q_TCP
)
7058 proto
= IPPROTO_TCP
;
7059 else if (proto
== Q_SCTP
)
7060 proto
= IPPROTO_SCTP
;
7061 else if (proto
== Q_DEFAULT
)
7062 proto
= PROTO_UNDEF
;
7064 bpf_error(cstate
, "illegal qualifier of 'port'");
7067 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7071 b
= gen_port(cstate
, (int)v
, proto
, dir
);
7072 gen_or(gen_port6(cstate
, (int)v
, proto
, dir
), b
);
7078 proto
= IPPROTO_UDP
;
7079 else if (proto
== Q_TCP
)
7080 proto
= IPPROTO_TCP
;
7081 else if (proto
== Q_SCTP
)
7082 proto
= IPPROTO_SCTP
;
7083 else if (proto
== Q_DEFAULT
)
7084 proto
= PROTO_UNDEF
;
7086 bpf_error(cstate
, "illegal qualifier of 'portrange'");
7089 bpf_error(cstate
, "illegal port number %u > 65535", v
);
7093 b
= gen_portrange(cstate
, (int)v
, (int)v
, proto
, dir
);
7094 gen_or(gen_portrange6(cstate
, (int)v
, (int)v
, proto
, dir
), b
);
7099 bpf_error(cstate
, "'gateway' requires a name");
7103 return gen_proto(cstate
, (int)v
, proto
, dir
);
7106 return gen_protochain(cstate
, (int)v
, proto
, dir
);
7121 gen_mcode6(compiler_state_t
*cstate
, const char *s1
, const char *s2
,
7122 unsigned int masklen
, struct qual q
)
7124 struct addrinfo
*res
;
7125 struct in6_addr
*addr
;
7126 struct in6_addr mask
;
7131 * Catch errors reported by us and routines below us, and return NULL
7134 if (setjmp(cstate
->top_ctx
))
7138 bpf_error(cstate
, "no mask %s supported", s2
);
7140 res
= pcap_nametoaddrinfo(s1
);
7142 bpf_error(cstate
, "invalid ip6 address %s", s1
);
7145 bpf_error(cstate
, "%s resolved to multiple address", s1
);
7146 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
7148 if (sizeof(mask
) * 8 < masklen
)
7149 bpf_error(cstate
, "mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
7150 memset(&mask
, 0, sizeof(mask
));
7151 memset(&mask
, 0xff, masklen
/ 8);
7153 mask
.s6_addr
[masklen
/ 8] =
7154 (0xff << (8 - masklen
% 8)) & 0xff;
7157 a
= (uint32_t *)addr
;
7158 m
= (uint32_t *)&mask
;
7159 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
7160 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
7161 bpf_error(cstate
, "non-network bits set in \"%s/%d\"", s1
, masklen
);
7169 bpf_error(cstate
, "Mask syntax for networks only");
7173 b
= gen_host6(cstate
, addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
7179 bpf_error(cstate
, "invalid qualifier against IPv6 address");
7186 gen_ecode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
7188 struct block
*b
, *tmp
;
7191 * Catch errors reported by us and routines below us, and return NULL
7194 if (setjmp(cstate
->top_ctx
))
7197 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
7198 cstate
->e
= pcap_ether_aton(s
);
7199 if (cstate
->e
== NULL
)
7200 bpf_error(cstate
, "malloc");
7201 switch (cstate
->linktype
) {
7203 case DLT_NETANALYZER
:
7204 case DLT_NETANALYZER_TRANSPARENT
:
7205 tmp
= gen_prevlinkhdr_check(cstate
);
7206 b
= gen_ehostop(cstate
, cstate
->e
, (int)q
.dir
);
7211 b
= gen_fhostop(cstate
, cstate
->e
, (int)q
.dir
);
7214 b
= gen_thostop(cstate
, cstate
->e
, (int)q
.dir
);
7216 case DLT_IEEE802_11
:
7217 case DLT_PRISM_HEADER
:
7218 case DLT_IEEE802_11_RADIO_AVS
:
7219 case DLT_IEEE802_11_RADIO
:
7221 b
= gen_wlanhostop(cstate
, cstate
->e
, (int)q
.dir
);
7223 case DLT_IP_OVER_FC
:
7224 b
= gen_ipfchostop(cstate
, cstate
->e
, (int)q
.dir
);
7229 bpf_error(cstate
, "ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
7237 bpf_error(cstate
, "ethernet address used in non-ether expression");
7242 sappend(struct slist
*s0
, struct slist
*s1
)
7245 * This is definitely not the best way to do this, but the
7246 * lists will rarely get long.
7253 static struct slist
*
7254 xfer_to_x(compiler_state_t
*cstate
, struct arth
*a
)
7258 s
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
7263 static struct slist
*
7264 xfer_to_a(compiler_state_t
*cstate
, struct arth
*a
)
7268 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
7274 * Modify "index" to use the value stored into its register as an
7275 * offset relative to the beginning of the header for the protocol
7276 * "proto", and allocate a register and put an item "size" bytes long
7277 * (1, 2, or 4) at that offset into that register, making it the register
7280 static struct arth
*
7281 gen_load_internal(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7283 struct slist
*s
, *tmp
;
7285 int regno
= alloc_reg(cstate
);
7287 free_reg(cstate
, inst
->regno
);
7291 bpf_error(cstate
, "data size must be 1, 2, or 4");
7307 bpf_error(cstate
, "unsupported index operation");
7311 * The offset is relative to the beginning of the packet
7312 * data, if we have a radio header. (If we don't, this
7315 if (cstate
->linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
7316 cstate
->linktype
!= DLT_IEEE802_11_RADIO
&&
7317 cstate
->linktype
!= DLT_PRISM_HEADER
)
7318 bpf_error(cstate
, "radio information not present in capture");
7321 * Load into the X register the offset computed into the
7322 * register specified by "index".
7324 s
= xfer_to_x(cstate
, inst
);
7327 * Load the item at that offset.
7329 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7331 sappend(inst
->s
, s
);
7336 * The offset is relative to the beginning of
7337 * the link-layer header.
7339 * XXX - what about ATM LANE? Should the index be
7340 * relative to the beginning of the AAL5 frame, so
7341 * that 0 refers to the beginning of the LE Control
7342 * field, or relative to the beginning of the LAN
7343 * frame, so that 0 refers, for Ethernet LANE, to
7344 * the beginning of the destination address?
7346 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkhdr
);
7349 * If "s" is non-null, it has code to arrange that the
7350 * X register contains the length of the prefix preceding
7351 * the link-layer header. Add to it the offset computed
7352 * into the register specified by "index", and move that
7353 * into the X register. Otherwise, just load into the X
7354 * register the offset computed into the register specified
7358 sappend(s
, xfer_to_a(cstate
, inst
));
7359 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7360 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7362 s
= xfer_to_x(cstate
, inst
);
7365 * Load the item at the sum of the offset we've put in the
7366 * X register and the offset of the start of the link
7367 * layer header (which is 0 if the radio header is
7368 * variable-length; that header length is what we put
7369 * into the X register and then added to the index).
7371 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7372 tmp
->s
.k
= cstate
->off_linkhdr
.constant_part
;
7374 sappend(inst
->s
, s
);
7388 * The offset is relative to the beginning of
7389 * the network-layer header.
7390 * XXX - are there any cases where we want
7391 * cstate->off_nl_nosnap?
7393 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7396 * If "s" is non-null, it has code to arrange that the
7397 * X register contains the variable part of the offset
7398 * of the link-layer payload. Add to it the offset
7399 * computed into the register specified by "index",
7400 * and move that into the X register. Otherwise, just
7401 * load into the X register the offset computed into
7402 * the register specified by "index".
7405 sappend(s
, xfer_to_a(cstate
, inst
));
7406 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7407 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7409 s
= xfer_to_x(cstate
, inst
);
7412 * Load the item at the sum of the offset we've put in the
7413 * X register, the offset of the start of the network
7414 * layer header from the beginning of the link-layer
7415 * payload, and the constant part of the offset of the
7416 * start of the link-layer payload.
7418 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7419 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7421 sappend(inst
->s
, s
);
7424 * Do the computation only if the packet contains
7425 * the protocol in question.
7427 b
= gen_proto_abbrev_internal(cstate
, proto
);
7429 gen_and(inst
->b
, b
);
7443 * The offset is relative to the beginning of
7444 * the transport-layer header.
7446 * Load the X register with the length of the IPv4 header
7447 * (plus the offset of the link-layer header, if it's
7448 * a variable-length header), in bytes.
7450 * XXX - are there any cases where we want
7451 * cstate->off_nl_nosnap?
7452 * XXX - we should, if we're built with
7453 * IPv6 support, generate code to load either
7454 * IPv4, IPv6, or both, as appropriate.
7456 s
= gen_loadx_iphdrlen(cstate
);
7459 * The X register now contains the sum of the variable
7460 * part of the offset of the link-layer payload and the
7461 * length of the network-layer header.
7463 * Load into the A register the offset relative to
7464 * the beginning of the transport layer header,
7465 * add the X register to that, move that to the
7466 * X register, and load with an offset from the
7467 * X register equal to the sum of the constant part of
7468 * the offset of the link-layer payload and the offset,
7469 * relative to the beginning of the link-layer payload,
7470 * of the network-layer header.
7472 sappend(s
, xfer_to_a(cstate
, inst
));
7473 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7474 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7475 sappend(s
, tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
));
7476 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
;
7477 sappend(inst
->s
, s
);
7480 * Do the computation only if the packet contains
7481 * the protocol in question - which is true only
7482 * if this is an IP datagram and is the first or
7483 * only fragment of that datagram.
7485 gen_and(gen_proto_abbrev_internal(cstate
, proto
), b
= gen_ipfrag(cstate
));
7487 gen_and(inst
->b
, b
);
7488 gen_and(gen_proto_abbrev_internal(cstate
, Q_IP
), b
);
7493 * Do the computation only if the packet contains
7494 * the protocol in question.
7496 b
= gen_proto_abbrev_internal(cstate
, Q_IPV6
);
7498 gen_and(inst
->b
, b
);
7503 * Check if we have an icmp6 next header
7505 b
= gen_cmp(cstate
, OR_LINKPL
, 6, BPF_B
, 58);
7507 gen_and(inst
->b
, b
);
7512 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
7514 * If "s" is non-null, it has code to arrange that the
7515 * X register contains the variable part of the offset
7516 * of the link-layer payload. Add to it the offset
7517 * computed into the register specified by "index",
7518 * and move that into the X register. Otherwise, just
7519 * load into the X register the offset computed into
7520 * the register specified by "index".
7523 sappend(s
, xfer_to_a(cstate
, inst
));
7524 sappend(s
, new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
));
7525 sappend(s
, new_stmt(cstate
, BPF_MISC
|BPF_TAX
));
7527 s
= xfer_to_x(cstate
, inst
);
7531 * Load the item at the sum of the offset we've put in the
7532 * X register, the offset of the start of the network
7533 * layer header from the beginning of the link-layer
7534 * payload, and the constant part of the offset of the
7535 * start of the link-layer payload.
7537 tmp
= new_stmt(cstate
, BPF_LD
|BPF_IND
|size
);
7538 tmp
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 40;
7541 sappend(inst
->s
, s
);
7545 inst
->regno
= regno
;
7546 s
= new_stmt(cstate
, BPF_ST
);
7548 sappend(inst
->s
, s
);
7554 gen_load(compiler_state_t
*cstate
, int proto
, struct arth
*inst
, int size
)
7557 * Catch errors reported by us and routines below us, and return NULL
7560 if (setjmp(cstate
->top_ctx
))
7563 return gen_load_internal(cstate
, proto
, inst
, size
);
7566 static struct block
*
7567 gen_relation_internal(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7568 struct arth
*a1
, int reversed
)
7570 struct slist
*s0
, *s1
, *s2
;
7571 struct block
*b
, *tmp
;
7573 s0
= xfer_to_x(cstate
, a1
);
7574 s1
= xfer_to_a(cstate
, a0
);
7575 if (code
== BPF_JEQ
) {
7576 s2
= new_stmt(cstate
, BPF_ALU
|BPF_SUB
|BPF_X
);
7577 b
= new_block(cstate
, JMP(code
));
7581 b
= new_block(cstate
, BPF_JMP
|code
|BPF_X
);
7587 sappend(a0
->s
, a1
->s
);
7591 free_reg(cstate
, a0
->regno
);
7592 free_reg(cstate
, a1
->regno
);
7594 /* 'and' together protocol checks */
7597 gen_and(a0
->b
, tmp
= a1
->b
);
7611 gen_relation(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7612 struct arth
*a1
, int reversed
)
7615 * Catch errors reported by us and routines below us, and return NULL
7618 if (setjmp(cstate
->top_ctx
))
7621 return gen_relation_internal(cstate
, code
, a0
, a1
, reversed
);
7625 gen_loadlen(compiler_state_t
*cstate
)
7632 * Catch errors reported by us and routines below us, and return NULL
7635 if (setjmp(cstate
->top_ctx
))
7638 regno
= alloc_reg(cstate
);
7639 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7640 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7641 s
->next
= new_stmt(cstate
, BPF_ST
);
7642 s
->next
->s
.k
= regno
;
7649 static struct arth
*
7650 gen_loadi_internal(compiler_state_t
*cstate
, int val
)
7656 a
= (struct arth
*)newchunk(cstate
, sizeof(*a
));
7658 reg
= alloc_reg(cstate
);
7660 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
7662 s
->next
= new_stmt(cstate
, BPF_ST
);
7671 gen_loadi(compiler_state_t
*cstate
, int val
)
7674 * Catch errors reported by us and routines below us, and return NULL
7677 if (setjmp(cstate
->top_ctx
))
7680 return gen_loadi_internal(cstate
, val
);
7684 gen_neg(compiler_state_t
*cstate
, struct arth
*a
)
7689 * Catch errors reported by us and routines below us, and return NULL
7692 if (setjmp(cstate
->top_ctx
))
7695 s
= xfer_to_a(cstate
, a
);
7697 s
= new_stmt(cstate
, BPF_ALU
|BPF_NEG
);
7700 s
= new_stmt(cstate
, BPF_ST
);
7708 gen_arth(compiler_state_t
*cstate
, int code
, struct arth
*a0
,
7711 struct slist
*s0
, *s1
, *s2
;
7714 * Catch errors reported by us and routines below us, and return NULL
7717 if (setjmp(cstate
->top_ctx
))
7721 * Disallow division by, or modulus by, zero; we do this here
7722 * so that it gets done even if the optimizer is disabled.
7724 * Also disallow shifts by a value greater than 31; we do this
7725 * here, for the same reason.
7727 if (code
== BPF_DIV
) {
7728 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7729 bpf_error(cstate
, "division by zero");
7730 } else if (code
== BPF_MOD
) {
7731 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7732 bpf_error(cstate
, "modulus by zero");
7733 } else if (code
== BPF_LSH
|| code
== BPF_RSH
) {
7735 * XXX - we need to make up our minds as to what integers
7736 * are signed and what integers are unsigned in BPF programs
7739 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) &&
7740 (a1
->s
->s
.k
< 0 || a1
->s
->s
.k
> 31))
7741 bpf_error(cstate
, "shift by more than 31 bits");
7743 s0
= xfer_to_x(cstate
, a1
);
7744 s1
= xfer_to_a(cstate
, a0
);
7745 s2
= new_stmt(cstate
, BPF_ALU
|BPF_X
|code
);
7750 sappend(a0
->s
, a1
->s
);
7752 free_reg(cstate
, a0
->regno
);
7753 free_reg(cstate
, a1
->regno
);
7755 s0
= new_stmt(cstate
, BPF_ST
);
7756 a0
->regno
= s0
->s
.k
= alloc_reg(cstate
);
7763 * Initialize the table of used registers and the current register.
7766 init_regs(compiler_state_t
*cstate
)
7769 memset(cstate
->regused
, 0, sizeof cstate
->regused
);
7773 * Return the next free register.
7776 alloc_reg(compiler_state_t
*cstate
)
7778 int n
= BPF_MEMWORDS
;
7781 if (cstate
->regused
[cstate
->curreg
])
7782 cstate
->curreg
= (cstate
->curreg
+ 1) % BPF_MEMWORDS
;
7784 cstate
->regused
[cstate
->curreg
] = 1;
7785 return cstate
->curreg
;
7788 bpf_error(cstate
, "too many registers needed to evaluate expression");
7793 * Return a register to the table so it can
7797 free_reg(compiler_state_t
*cstate
, int n
)
7799 cstate
->regused
[n
] = 0;
7802 static struct block
*
7803 gen_len(compiler_state_t
*cstate
, int jmp
, int n
)
7808 s
= new_stmt(cstate
, BPF_LD
|BPF_LEN
);
7809 b
= new_block(cstate
, JMP(jmp
));
7817 gen_greater(compiler_state_t
*cstate
, int n
)
7820 * Catch errors reported by us and routines below us, and return NULL
7823 if (setjmp(cstate
->top_ctx
))
7826 return gen_len(cstate
, BPF_JGE
, n
);
7830 * Actually, this is less than or equal.
7833 gen_less(compiler_state_t
*cstate
, int n
)
7838 * Catch errors reported by us and routines below us, and return NULL
7841 if (setjmp(cstate
->top_ctx
))
7844 b
= gen_len(cstate
, BPF_JGT
, n
);
7851 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7852 * the beginning of the link-layer header.
7853 * XXX - that means you can't test values in the radiotap header, but
7854 * as that header is difficult if not impossible to parse generally
7855 * without a loop, that might not be a severe problem. A new keyword
7856 * "radio" could be added for that, although what you'd really want
7857 * would be a way of testing particular radio header values, which
7858 * would generate code appropriate to the radio header in question.
7861 gen_byteop(compiler_state_t
*cstate
, int op
, int idx
, int val
)
7867 * Catch errors reported by us and routines below us, and return NULL
7870 if (setjmp(cstate
->top_ctx
))
7878 return gen_cmp(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7881 b
= gen_cmp_lt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7885 b
= gen_cmp_gt(cstate
, OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7889 s
= new_stmt(cstate
, BPF_ALU
|BPF_OR
|BPF_K
);
7893 s
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
7897 b
= new_block(cstate
, JMP(BPF_JEQ
));
7904 static const u_char abroadcast
[] = { 0x0 };
7907 gen_broadcast(compiler_state_t
*cstate
, int proto
)
7909 bpf_u_int32 hostmask
;
7910 struct block
*b0
, *b1
, *b2
;
7911 static const u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7914 * Catch errors reported by us and routines below us, and return NULL
7917 if (setjmp(cstate
->top_ctx
))
7924 switch (cstate
->linktype
) {
7926 case DLT_ARCNET_LINUX
:
7927 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
7929 case DLT_NETANALYZER
:
7930 case DLT_NETANALYZER_TRANSPARENT
:
7931 b1
= gen_prevlinkhdr_check(cstate
);
7932 b0
= gen_ehostop(cstate
, ebroadcast
, Q_DST
);
7937 return gen_fhostop(cstate
, ebroadcast
, Q_DST
);
7939 return gen_thostop(cstate
, ebroadcast
, Q_DST
);
7940 case DLT_IEEE802_11
:
7941 case DLT_PRISM_HEADER
:
7942 case DLT_IEEE802_11_RADIO_AVS
:
7943 case DLT_IEEE802_11_RADIO
:
7945 return gen_wlanhostop(cstate
, ebroadcast
, Q_DST
);
7946 case DLT_IP_OVER_FC
:
7947 return gen_ipfchostop(cstate
, ebroadcast
, Q_DST
);
7949 bpf_error(cstate
, "not a broadcast link");
7955 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7956 * as an indication that we don't know the netmask, and fail
7959 if (cstate
->netmask
== PCAP_NETMASK_UNKNOWN
)
7960 bpf_error(cstate
, "netmask not known, so 'ip broadcast' not supported");
7961 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
7962 hostmask
= ~cstate
->netmask
;
7963 b1
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7964 b2
= gen_mcmp(cstate
, OR_LINKPL
, 16, BPF_W
,
7965 (bpf_int32
)(~0 & hostmask
), hostmask
);
7970 bpf_error(cstate
, "only link-layer/IP broadcast filters supported");
7975 * Generate code to test the low-order bit of a MAC address (that's
7976 * the bottom bit of the *first* byte).
7978 static struct block
*
7979 gen_mac_multicast(compiler_state_t
*cstate
, int offset
)
7981 register struct block
*b0
;
7982 register struct slist
*s
;
7984 /* link[offset] & 1 != 0 */
7985 s
= gen_load_a(cstate
, OR_LINKHDR
, offset
, BPF_B
);
7986 b0
= new_block(cstate
, JMP(BPF_JSET
));
7993 gen_multicast(compiler_state_t
*cstate
, int proto
)
7995 register struct block
*b0
, *b1
, *b2
;
7996 register struct slist
*s
;
7999 * Catch errors reported by us and routines below us, and return NULL
8002 if (setjmp(cstate
->top_ctx
))
8009 switch (cstate
->linktype
) {
8011 case DLT_ARCNET_LINUX
:
8012 /* all ARCnet multicasts use the same address */
8013 return gen_ahostop(cstate
, abroadcast
, Q_DST
);
8015 case DLT_NETANALYZER
:
8016 case DLT_NETANALYZER_TRANSPARENT
:
8017 b1
= gen_prevlinkhdr_check(cstate
);
8018 /* ether[0] & 1 != 0 */
8019 b0
= gen_mac_multicast(cstate
, 0);
8025 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
8027 * XXX - was that referring to bit-order issues?
8029 /* fddi[1] & 1 != 0 */
8030 return gen_mac_multicast(cstate
, 1);
8032 /* tr[2] & 1 != 0 */
8033 return gen_mac_multicast(cstate
, 2);
8034 case DLT_IEEE802_11
:
8035 case DLT_PRISM_HEADER
:
8036 case DLT_IEEE802_11_RADIO_AVS
:
8037 case DLT_IEEE802_11_RADIO
:
8042 * For control frames, there is no DA.
8044 * For management frames, DA is at an
8045 * offset of 4 from the beginning of
8048 * For data frames, DA is at an offset
8049 * of 4 from the beginning of the packet
8050 * if To DS is clear and at an offset of
8051 * 16 from the beginning of the packet
8056 * Generate the tests to be done for data frames.
8058 * First, check for To DS set, i.e. "link[1] & 0x01".
8060 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8061 b1
= new_block(cstate
, JMP(BPF_JSET
));
8062 b1
->s
.k
= 0x01; /* To DS */
8066 * If To DS is set, the DA is at 16.
8068 b0
= gen_mac_multicast(cstate
, 16);
8072 * Now, check for To DS not set, i.e. check
8073 * "!(link[1] & 0x01)".
8075 s
= gen_load_a(cstate
, OR_LINKHDR
, 1, BPF_B
);
8076 b2
= new_block(cstate
, JMP(BPF_JSET
));
8077 b2
->s
.k
= 0x01; /* To DS */
8082 * If To DS is not set, the DA is at 4.
8084 b1
= gen_mac_multicast(cstate
, 4);
8088 * Now OR together the last two checks. That gives
8089 * the complete set of checks for data frames.
8094 * Now check for a data frame.
8095 * I.e, check "link[0] & 0x08".
8097 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8098 b1
= new_block(cstate
, JMP(BPF_JSET
));
8103 * AND that with the checks done for data frames.
8108 * If the high-order bit of the type value is 0, this
8109 * is a management frame.
8110 * I.e, check "!(link[0] & 0x08)".
8112 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8113 b2
= new_block(cstate
, JMP(BPF_JSET
));
8119 * For management frames, the DA is at 4.
8121 b1
= gen_mac_multicast(cstate
, 4);
8125 * OR that with the checks done for data frames.
8126 * That gives the checks done for management and
8132 * If the low-order bit of the type value is 1,
8133 * this is either a control frame or a frame
8134 * with a reserved type, and thus not a
8137 * I.e., check "!(link[0] & 0x04)".
8139 s
= gen_load_a(cstate
, OR_LINKHDR
, 0, BPF_B
);
8140 b1
= new_block(cstate
, JMP(BPF_JSET
));
8146 * AND that with the checks for data and management
8151 case DLT_IP_OVER_FC
:
8152 b0
= gen_mac_multicast(cstate
, 2);
8157 /* Link not known to support multicasts */
8161 b0
= gen_linktype(cstate
, ETHERTYPE_IP
);
8162 b1
= gen_cmp_ge(cstate
, OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
8167 b0
= gen_linktype(cstate
, ETHERTYPE_IPV6
);
8168 b1
= gen_cmp(cstate
, OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
8172 bpf_error(cstate
, "link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
8177 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
8178 * Outbound traffic is sent by this machine, while inbound traffic is
8179 * sent by a remote machine (and may include packets destined for a
8180 * unicast or multicast link-layer address we are not subscribing to).
8181 * These are the same definitions implemented by pcap_setdirection().
8182 * Capturing only unicast traffic destined for this host is probably
8183 * better accomplished using a higher-layer filter.
8186 gen_inbound(compiler_state_t
*cstate
, int dir
)
8188 register struct block
*b0
;
8191 * Catch errors reported by us and routines below us, and return NULL
8194 if (setjmp(cstate
->top_ctx
))
8198 * Only some data link types support inbound/outbound qualifiers.
8200 switch (cstate
->linktype
) {
8202 b0
= gen_relation_internal(cstate
, BPF_JEQ
,
8203 gen_load_internal(cstate
, Q_LINK
, gen_loadi_internal(cstate
, 0), 1),
8204 gen_loadi_internal(cstate
, 0),
8210 /* match outgoing packets */
8211 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
8213 /* match incoming packets */
8214 b0
= gen_cmp(cstate
, OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
8219 /* match outgoing packets */
8220 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
8222 /* to filter on inbound traffic, invert the match */
8227 case DLT_LINUX_SLL2
:
8228 /* match outgoing packets */
8229 b0
= gen_cmp(cstate
, OR_LINKHDR
, 10, BPF_B
, LINUX_SLL_OUTGOING
);
8231 /* to filter on inbound traffic, invert the match */
8236 #ifdef HAVE_NET_PFVAR_H
8238 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
8239 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
8245 /* match outgoing packets */
8246 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
8248 /* match incoming packets */
8249 b0
= gen_cmp(cstate
, OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
8253 case DLT_JUNIPER_MFR
:
8254 case DLT_JUNIPER_MLFR
:
8255 case DLT_JUNIPER_MLPPP
:
8256 case DLT_JUNIPER_ATM1
:
8257 case DLT_JUNIPER_ATM2
:
8258 case DLT_JUNIPER_PPPOE
:
8259 case DLT_JUNIPER_PPPOE_ATM
:
8260 case DLT_JUNIPER_GGSN
:
8261 case DLT_JUNIPER_ES
:
8262 case DLT_JUNIPER_MONITOR
:
8263 case DLT_JUNIPER_SERVICES
:
8264 case DLT_JUNIPER_ETHER
:
8265 case DLT_JUNIPER_PPP
:
8266 case DLT_JUNIPER_FRELAY
:
8267 case DLT_JUNIPER_CHDLC
:
8268 case DLT_JUNIPER_VP
:
8269 case DLT_JUNIPER_ST
:
8270 case DLT_JUNIPER_ISM
:
8271 case DLT_JUNIPER_VS
:
8272 case DLT_JUNIPER_SRX_E2E
:
8273 case DLT_JUNIPER_FIBRECHANNEL
:
8274 case DLT_JUNIPER_ATM_CEMIC
:
8276 /* juniper flags (including direction) are stored
8277 * the byte after the 3-byte magic number */
8279 /* match outgoing packets */
8280 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
8282 /* match incoming packets */
8283 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
8289 * If we have packet meta-data indicating a direction,
8290 * and that metadata can be checked by BPF code, check
8291 * it. Otherwise, give up, as this link-layer type has
8292 * nothing in the packet data.
8294 * Currently, the only platform where a BPF filter can
8295 * check that metadata is Linux with the in-kernel
8296 * BPF interpreter. If other packet capture mechanisms
8297 * and BPF filters also supported this, it would be
8298 * nice. It would be even better if they made that
8299 * metadata available so that we could provide it
8300 * with newer capture APIs, allowing it to be saved
8303 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
8305 * This is Linux with PF_PACKET support.
8306 * If this is a *live* capture, we can look at
8307 * special meta-data in the filter expression;
8308 * if it's a savefile, we can't.
8310 if (cstate
->bpf_pcap
->rfile
!= NULL
) {
8311 /* We have a FILE *, so this is a savefile */
8312 bpf_error(cstate
, "inbound/outbound not supported on linktype %d when reading savefiles",
8317 /* match outgoing packets */
8318 b0
= gen_cmp(cstate
, OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
8321 /* to filter on inbound traffic, invert the match */
8324 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8325 bpf_error(cstate
, "inbound/outbound not supported on linktype %d",
8328 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
8333 #ifdef HAVE_NET_PFVAR_H
8334 /* PF firewall log matched interface */
8336 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname
)
8342 * Catch errors reported by us and routines below us, and return NULL
8345 if (setjmp(cstate
->top_ctx
))
8348 if (cstate
->linktype
!= DLT_PFLOG
) {
8349 bpf_error(cstate
, "ifname supported only on PF linktype");
8352 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
8353 off
= offsetof(struct pfloghdr
, ifname
);
8354 if (strlen(ifname
) >= len
) {
8355 bpf_error(cstate
, "ifname interface names can only be %d characters",
8359 b0
= gen_bcmp(cstate
, OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
8363 /* PF firewall log ruleset name */
8365 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset
)
8370 * Catch errors reported by us and routines below us, and return NULL
8373 if (setjmp(cstate
->top_ctx
))
8376 if (cstate
->linktype
!= DLT_PFLOG
) {
8377 bpf_error(cstate
, "ruleset supported only on PF linktype");
8381 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
8382 bpf_error(cstate
, "ruleset names can only be %ld characters",
8383 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
8387 b0
= gen_bcmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
8388 strlen(ruleset
), (const u_char
*)ruleset
);
8392 /* PF firewall log rule number */
8394 gen_pf_rnr(compiler_state_t
*cstate
, int rnr
)
8399 * Catch errors reported by us and routines below us, and return NULL
8402 if (setjmp(cstate
->top_ctx
))
8405 if (cstate
->linktype
!= DLT_PFLOG
) {
8406 bpf_error(cstate
, "rnr supported only on PF linktype");
8410 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
8415 /* PF firewall log sub-rule number */
8417 gen_pf_srnr(compiler_state_t
*cstate
, int srnr
)
8422 * Catch errors reported by us and routines below us, and return NULL
8425 if (setjmp(cstate
->top_ctx
))
8428 if (cstate
->linktype
!= DLT_PFLOG
) {
8429 bpf_error(cstate
, "srnr supported only on PF linktype");
8433 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
8438 /* PF firewall log reason code */
8440 gen_pf_reason(compiler_state_t
*cstate
, int reason
)
8445 * Catch errors reported by us and routines below us, and return NULL
8448 if (setjmp(cstate
->top_ctx
))
8451 if (cstate
->linktype
!= DLT_PFLOG
) {
8452 bpf_error(cstate
, "reason supported only on PF linktype");
8456 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
8461 /* PF firewall log action */
8463 gen_pf_action(compiler_state_t
*cstate
, int action
)
8468 * Catch errors reported by us and routines below us, and return NULL
8471 if (setjmp(cstate
->top_ctx
))
8474 if (cstate
->linktype
!= DLT_PFLOG
) {
8475 bpf_error(cstate
, "action supported only on PF linktype");
8479 b0
= gen_cmp(cstate
, OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
8483 #else /* !HAVE_NET_PFVAR_H */
8485 gen_pf_ifname(compiler_state_t
*cstate
, const char *ifname _U_
)
8488 * Catch errors reported by us and routines below us, and return NULL
8491 if (setjmp(cstate
->top_ctx
))
8494 bpf_error(cstate
, "libpcap was compiled without pf support");
8499 gen_pf_ruleset(compiler_state_t
*cstate
, char *ruleset _U_
)
8502 * Catch errors reported by us and routines below us, and return NULL
8505 if (setjmp(cstate
->top_ctx
))
8508 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8513 gen_pf_rnr(compiler_state_t
*cstate
, int rnr _U_
)
8516 * Catch errors reported by us and routines below us, and return NULL
8519 if (setjmp(cstate
->top_ctx
))
8522 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8527 gen_pf_srnr(compiler_state_t
*cstate
, int srnr _U_
)
8530 * Catch errors reported by us and routines below us, and return NULL
8533 if (setjmp(cstate
->top_ctx
))
8536 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8541 gen_pf_reason(compiler_state_t
*cstate
, int reason _U_
)
8544 * Catch errors reported by us and routines below us, and return NULL
8547 if (setjmp(cstate
->top_ctx
))
8550 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8555 gen_pf_action(compiler_state_t
*cstate
, int action _U_
)
8558 * Catch errors reported by us and routines below us, and return NULL
8561 if (setjmp(cstate
->top_ctx
))
8564 bpf_error(cstate
, "libpcap was compiled on a machine without pf support");
8567 #endif /* HAVE_NET_PFVAR_H */
8569 /* IEEE 802.11 wireless header */
8571 gen_p80211_type(compiler_state_t
*cstate
, int type
, int mask
)
8576 * Catch errors reported by us and routines below us, and return NULL
8579 if (setjmp(cstate
->top_ctx
))
8582 switch (cstate
->linktype
) {
8584 case DLT_IEEE802_11
:
8585 case DLT_PRISM_HEADER
:
8586 case DLT_IEEE802_11_RADIO_AVS
:
8587 case DLT_IEEE802_11_RADIO
:
8588 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
8593 bpf_error(cstate
, "802.11 link-layer types supported only on 802.11");
8601 gen_p80211_fcdir(compiler_state_t
*cstate
, int fcdir
)
8606 * Catch errors reported by us and routines below us, and return NULL
8609 if (setjmp(cstate
->top_ctx
))
8612 switch (cstate
->linktype
) {
8614 case DLT_IEEE802_11
:
8615 case DLT_PRISM_HEADER
:
8616 case DLT_IEEE802_11_RADIO_AVS
:
8617 case DLT_IEEE802_11_RADIO
:
8621 bpf_error(cstate
, "frame direction supported only with 802.11 headers");
8625 b0
= gen_mcmp(cstate
, OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
8626 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
8632 gen_acode(compiler_state_t
*cstate
, const char *s
, struct qual q
)
8637 * Catch errors reported by us and routines below us, and return NULL
8640 if (setjmp(cstate
->top_ctx
))
8643 switch (cstate
->linktype
) {
8646 case DLT_ARCNET_LINUX
:
8647 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
8648 q
.proto
== Q_LINK
) {
8649 cstate
->e
= pcap_ether_aton(s
);
8650 if (cstate
->e
== NULL
)
8651 bpf_error(cstate
, "malloc");
8652 b
= gen_ahostop(cstate
, cstate
->e
, (int)q
.dir
);
8657 bpf_error(cstate
, "ARCnet address used in non-arc expression");
8663 bpf_error(cstate
, "aid supported only on ARCnet");
8668 static struct block
*
8669 gen_ahostop(compiler_state_t
*cstate
, const u_char
*eaddr
, int dir
)
8671 register struct block
*b0
, *b1
;
8674 /* src comes first, different from Ethernet */
8676 return gen_bcmp(cstate
, OR_LINKHDR
, 0, 1, eaddr
);
8679 return gen_bcmp(cstate
, OR_LINKHDR
, 1, 1, eaddr
);
8682 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8683 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8689 b0
= gen_ahostop(cstate
, eaddr
, Q_SRC
);
8690 b1
= gen_ahostop(cstate
, eaddr
, Q_DST
);
8695 bpf_error(cstate
, "'addr1' and 'address1' are only supported on 802.11");
8699 bpf_error(cstate
, "'addr2' and 'address2' are only supported on 802.11");
8703 bpf_error(cstate
, "'addr3' and 'address3' are only supported on 802.11");
8707 bpf_error(cstate
, "'addr4' and 'address4' are only supported on 802.11");
8711 bpf_error(cstate
, "'ra' is only supported on 802.11");
8715 bpf_error(cstate
, "'ta' is only supported on 802.11");
8722 static struct block
*
8723 gen_vlan_tpid_test(compiler_state_t
*cstate
)
8725 struct block
*b0
, *b1
;
8727 /* check for VLAN, including QinQ */
8728 b0
= gen_linktype(cstate
, ETHERTYPE_8021Q
);
8729 b1
= gen_linktype(cstate
, ETHERTYPE_8021AD
);
8732 b1
= gen_linktype(cstate
, ETHERTYPE_8021QINQ
);
8738 static struct block
*
8739 gen_vlan_vid_test(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
)
8741 if (vlan_num
> 0x0fff) {
8742 bpf_error(cstate
, "VLAN tag %u greater than maximum %u",
8745 return gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
8748 static struct block
*
8749 gen_vlan_no_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8752 struct block
*b0
, *b1
;
8754 b0
= gen_vlan_tpid_test(cstate
);
8757 b1
= gen_vlan_vid_test(cstate
, vlan_num
);
8763 * Both payload and link header type follow the VLAN tags so that
8764 * both need to be updated.
8766 cstate
->off_linkpl
.constant_part
+= 4;
8767 cstate
->off_linktype
.constant_part
+= 4;
8772 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8773 /* add v to variable part of off */
8775 gen_vlan_vloffset_add(compiler_state_t
*cstate
, bpf_abs_offset
*off
, int v
, struct slist
*s
)
8779 if (!off
->is_variable
)
8780 off
->is_variable
= 1;
8782 off
->reg
= alloc_reg(cstate
);
8784 s2
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
8787 s2
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_IMM
);
8790 s2
= new_stmt(cstate
, BPF_ST
);
8796 * patch block b_tpid (VLAN TPID test) to update variable parts of link payload
8797 * and link type offsets first
8800 gen_vlan_patch_tpid_test(compiler_state_t
*cstate
, struct block
*b_tpid
)
8804 /* offset determined at run time, shift variable part */
8806 cstate
->is_vlan_vloffset
= 1;
8807 gen_vlan_vloffset_add(cstate
, &cstate
->off_linkpl
, 4, &s
);
8808 gen_vlan_vloffset_add(cstate
, &cstate
->off_linktype
, 4, &s
);
8810 /* we get a pointer to a chain of or-ed blocks, patch first of them */
8811 sappend(s
.next
, b_tpid
->head
->stmts
);
8812 b_tpid
->head
->stmts
= s
.next
;
8816 * patch block b_vid (VLAN id test) to load VID value either from packet
8817 * metadata (using BPF extensions) if SKF_AD_VLAN_TAG_PRESENT is true
8820 gen_vlan_patch_vid_test(compiler_state_t
*cstate
, struct block
*b_vid
)
8822 struct slist
*s
, *s2
, *sjeq
;
8825 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8826 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8828 /* true -> next instructions, false -> beginning of b_vid */
8829 sjeq
= new_stmt(cstate
, JMP(BPF_JEQ
));
8831 sjeq
->s
.jf
= b_vid
->stmts
;
8834 s2
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8835 s2
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
8839 /* Jump to the test in b_vid. We need to jump one instruction before
8840 * the end of the b_vid block so that we only skip loading the TCI
8841 * from packet data and not the 'and' instruction extractging VID.
8844 for (s2
= b_vid
->stmts
; s2
; s2
= s2
->next
)
8846 s2
= new_stmt(cstate
, JMP(BPF_JA
));
8850 /* insert our statements at the beginning of b_vid */
8851 sappend(s
, b_vid
->stmts
);
8856 * Generate check for "vlan" or "vlan <id>" on systems with support for BPF
8857 * extensions. Even if kernel supports VLAN BPF extensions, (outermost) VLAN
8858 * tag can be either in metadata or in packet data; therefore if the
8859 * SKF_AD_VLAN_TAG_PRESENT test is negative, we need to check link
8860 * header for VLAN tag. As the decision is done at run time, we need
8861 * update variable part of the offsets
8863 static struct block
*
8864 gen_vlan_bpf_extensions(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
,
8867 struct block
*b0
, *b_tpid
, *b_vid
= NULL
;
8870 /* generate new filter code based on extracting packet
8872 s
= new_stmt(cstate
, BPF_LD
|BPF_B
|BPF_ABS
);
8873 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
8875 b0
= new_block(cstate
, JMP(BPF_JEQ
));
8880 * This is tricky. We need to insert the statements updating variable
8881 * parts of offsets before the the traditional TPID and VID tests so
8882 * that they are called whenever SKF_AD_VLAN_TAG_PRESENT fails but
8883 * we do not want this update to affect those checks. That's why we
8884 * generate both test blocks first and insert the statements updating
8885 * variable parts of both offsets after that. This wouldn't work if
8886 * there already were variable length link header when entering this
8887 * function but gen_vlan_bpf_extensions() isn't called in that case.
8889 b_tpid
= gen_vlan_tpid_test(cstate
);
8891 b_vid
= gen_vlan_vid_test(cstate
, vlan_num
);
8893 gen_vlan_patch_tpid_test(cstate
, b_tpid
);
8898 gen_vlan_patch_vid_test(cstate
, b_vid
);
8908 * support IEEE 802.1Q VLAN trunk over ethernet
8911 gen_vlan(compiler_state_t
*cstate
, bpf_u_int32 vlan_num
, int has_vlan_tag
)
8916 * Catch errors reported by us and routines below us, and return NULL
8919 if (setjmp(cstate
->top_ctx
))
8922 /* can't check for VLAN-encapsulated packets inside MPLS */
8923 if (cstate
->label_stack_depth
> 0)
8924 bpf_error(cstate
, "no VLAN match after MPLS");
8927 * Check for a VLAN packet, and then change the offsets to point
8928 * to the type and data fields within the VLAN packet. Just
8929 * increment the offsets, so that we can support a hierarchy, e.g.
8930 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8933 * XXX - this is a bit of a kludge. If we were to split the
8934 * compiler into a parser that parses an expression and
8935 * generates an expression tree, and a code generator that
8936 * takes an expression tree (which could come from our
8937 * parser or from some other parser) and generates BPF code,
8938 * we could perhaps make the offsets parameters of routines
8939 * and, in the handler for an "AND" node, pass to subnodes
8940 * other than the VLAN node the adjusted offsets.
8942 * This would mean that "vlan" would, instead of changing the
8943 * behavior of *all* tests after it, change only the behavior
8944 * of tests ANDed with it. That would change the documented
8945 * semantics of "vlan", which might break some expressions.
8946 * However, it would mean that "(vlan and ip) or ip" would check
8947 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8948 * checking only for VLAN-encapsulated IP, so that could still
8949 * be considered worth doing; it wouldn't break expressions
8950 * that are of the form "vlan and ..." or "vlan N and ...",
8951 * which I suspect are the most common expressions involving
8952 * "vlan". "vlan or ..." doesn't necessarily do what the user
8953 * would really want, now, as all the "or ..." tests would
8954 * be done assuming a VLAN, even though the "or" could be viewed
8955 * as meaning "or, if this isn't a VLAN packet...".
8957 switch (cstate
->linktype
) {
8960 case DLT_NETANALYZER
:
8961 case DLT_NETANALYZER_TRANSPARENT
:
8962 #if defined(SKF_AD_VLAN_TAG_PRESENT)
8963 /* Verify that this is the outer part of the packet and
8964 * not encapsulated somehow. */
8965 if (cstate
->vlan_stack_depth
== 0 && !cstate
->off_linkhdr
.is_variable
&&
8966 cstate
->off_linkhdr
.constant_part
==
8967 cstate
->off_outermostlinkhdr
.constant_part
) {
8969 * Do we need special VLAN handling?
8971 if (cstate
->bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8972 b0
= gen_vlan_bpf_extensions(cstate
, vlan_num
,
8975 b0
= gen_vlan_no_bpf_extensions(cstate
,
8976 vlan_num
, has_vlan_tag
);
8979 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
,
8983 case DLT_IEEE802_11
:
8984 case DLT_PRISM_HEADER
:
8985 case DLT_IEEE802_11_RADIO_AVS
:
8986 case DLT_IEEE802_11_RADIO
:
8987 b0
= gen_vlan_no_bpf_extensions(cstate
, vlan_num
, has_vlan_tag
);
8991 bpf_error(cstate
, "no VLAN support for data link type %d",
8996 cstate
->vlan_stack_depth
++;
9005 gen_mpls(compiler_state_t
*cstate
, volatile bpf_u_int32 label_num
,
9008 struct block
*b0
, *b1
;
9011 * Catch errors reported by us and routines below us, and return NULL
9014 if (setjmp(cstate
->top_ctx
))
9017 if (cstate
->label_stack_depth
> 0) {
9018 /* just match the bottom-of-stack bit clear */
9019 b0
= gen_mcmp(cstate
, OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
9022 * We're not in an MPLS stack yet, so check the link-layer
9023 * type against MPLS.
9025 switch (cstate
->linktype
) {
9027 case DLT_C_HDLC
: /* fall through */
9029 case DLT_NETANALYZER
:
9030 case DLT_NETANALYZER_TRANSPARENT
:
9031 b0
= gen_linktype(cstate
, ETHERTYPE_MPLS
);
9035 b0
= gen_linktype(cstate
, PPP_MPLS_UCAST
);
9038 /* FIXME add other DLT_s ...
9039 * for Frame-Relay/and ATM this may get messy due to SNAP headers
9040 * leave it for now */
9043 bpf_error(cstate
, "no MPLS support for data link type %d",
9050 /* If a specific MPLS label is requested, check it */
9051 if (has_label_num
) {
9052 if (label_num
> 0xFFFFF) {
9053 bpf_error(cstate
, "MPLS label %u greater than maximum %u",
9054 label_num
, 0xFFFFF);
9056 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
9057 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
9058 0xfffff000); /* only compare the first 20 bits */
9064 * Change the offsets to point to the type and data fields within
9065 * the MPLS packet. Just increment the offsets, so that we
9066 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
9067 * capture packets with an outer label of 100000 and an inner
9070 * Increment the MPLS stack depth as well; this indicates that
9071 * we're checking MPLS-encapsulated headers, to make sure higher
9072 * level code generators don't try to match against IP-related
9073 * protocols such as Q_ARP, Q_RARP etc.
9075 * XXX - this is a bit of a kludge. See comments in gen_vlan().
9077 cstate
->off_nl_nosnap
+= 4;
9078 cstate
->off_nl
+= 4;
9079 cstate
->label_stack_depth
++;
9084 * Support PPPOE discovery and session.
9087 gen_pppoed(compiler_state_t
*cstate
)
9090 * Catch errors reported by us and routines below us, and return NULL
9093 if (setjmp(cstate
->top_ctx
))
9096 /* check for PPPoE discovery */
9097 return gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOED
);
9101 gen_pppoes(compiler_state_t
*cstate
, bpf_u_int32 sess_num
, int has_sess_num
)
9103 struct block
*b0
, *b1
;
9106 * Catch errors reported by us and routines below us, and return NULL
9109 if (setjmp(cstate
->top_ctx
))
9113 * Test against the PPPoE session link-layer type.
9115 b0
= gen_linktype(cstate
, (bpf_int32
)ETHERTYPE_PPPOES
);
9117 /* If a specific session is requested, check PPPoE session id */
9119 if (sess_num
> 0x0000ffff) {
9120 bpf_error(cstate
, "PPPoE session number %u greater than maximum %u",
9121 sess_num
, 0x0000ffff);
9123 b1
= gen_mcmp(cstate
, OR_LINKPL
, 0, BPF_W
,
9124 (bpf_int32
)sess_num
, 0x0000ffff);
9130 * Change the offsets to point to the type and data fields within
9131 * the PPP packet, and note that this is PPPoE rather than
9134 * XXX - this is a bit of a kludge. If we were to split the
9135 * compiler into a parser that parses an expression and
9136 * generates an expression tree, and a code generator that
9137 * takes an expression tree (which could come from our
9138 * parser or from some other parser) and generates BPF code,
9139 * we could perhaps make the offsets parameters of routines
9140 * and, in the handler for an "AND" node, pass to subnodes
9141 * other than the PPPoE node the adjusted offsets.
9143 * This would mean that "pppoes" would, instead of changing the
9144 * behavior of *all* tests after it, change only the behavior
9145 * of tests ANDed with it. That would change the documented
9146 * semantics of "pppoes", which might break some expressions.
9147 * However, it would mean that "(pppoes and ip) or ip" would check
9148 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
9149 * checking only for VLAN-encapsulated IP, so that could still
9150 * be considered worth doing; it wouldn't break expressions
9151 * that are of the form "pppoes and ..." which I suspect are the
9152 * most common expressions involving "pppoes". "pppoes or ..."
9153 * doesn't necessarily do what the user would really want, now,
9154 * as all the "or ..." tests would be done assuming PPPoE, even
9155 * though the "or" could be viewed as meaning "or, if this isn't
9156 * a PPPoE packet...".
9158 * The "network-layer" protocol is PPPoE, which has a 6-byte
9159 * PPPoE header, followed by a PPP packet.
9161 * There is no HDLC encapsulation for the PPP packet (it's
9162 * encapsulated in PPPoES instead), so the link-layer type
9163 * starts at the first byte of the PPP packet. For PPPoE,
9164 * that offset is relative to the beginning of the total
9165 * link-layer payload, including any 802.2 LLC header, so
9166 * it's 6 bytes past cstate->off_nl.
9168 PUSH_LINKHDR(cstate
, DLT_PPP
, cstate
->off_linkpl
.is_variable
,
9169 cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 6, /* 6 bytes past the PPPoE header */
9170 cstate
->off_linkpl
.reg
);
9172 cstate
->off_linktype
= cstate
->off_linkhdr
;
9173 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 2;
9176 cstate
->off_nl_nosnap
= 0; /* no 802.2 LLC */
9181 /* Check that this is Geneve and the VNI is correct if
9182 * specified. Parameterized to handle both IPv4 and IPv6. */
9183 static struct block
*
9184 gen_geneve_check(compiler_state_t
*cstate
,
9185 struct block
*(*gen_portfn
)(compiler_state_t
*, int, int, int),
9186 enum e_offrel offrel
, bpf_u_int32 vni
, int has_vni
)
9188 struct block
*b0
, *b1
;
9190 b0
= gen_portfn(cstate
, GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
9192 /* Check that we are operating on version 0. Otherwise, we
9193 * can't decode the rest of the fields. The version is 2 bits
9194 * in the first byte of the Geneve header. */
9195 b1
= gen_mcmp(cstate
, offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
9200 if (vni
> 0xffffff) {
9201 bpf_error(cstate
, "Geneve VNI %u greater than maximum %u",
9204 vni
<<= 8; /* VNI is in the upper 3 bytes */
9205 b1
= gen_mcmp(cstate
, offrel
, 12, BPF_W
, (bpf_int32
)vni
,
9214 /* The IPv4 and IPv6 Geneve checks need to do two things:
9215 * - Verify that this actually is Geneve with the right VNI.
9216 * - Place the IP header length (plus variable link prefix if
9217 * needed) into register A to be used later to compute
9218 * the inner packet offsets. */
9219 static struct block
*
9220 gen_geneve4(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9222 struct block
*b0
, *b1
;
9223 struct slist
*s
, *s1
;
9225 b0
= gen_geneve_check(cstate
, gen_port
, OR_TRAN_IPV4
, vni
, has_vni
);
9227 /* Load the IP header length into A. */
9228 s
= gen_loadx_iphdrlen(cstate
);
9230 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9233 /* Forcibly append these statements to the true condition
9234 * of the protocol check by creating a new block that is
9235 * always true and ANDing them. */
9236 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9245 static struct block
*
9246 gen_geneve6(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9248 struct block
*b0
, *b1
;
9249 struct slist
*s
, *s1
;
9251 b0
= gen_geneve_check(cstate
, gen_port6
, OR_TRAN_IPV6
, vni
, has_vni
);
9253 /* Load the IP header length. We need to account for a
9254 * variable length link prefix if there is one. */
9255 s
= gen_abs_offset_varpart(cstate
, &cstate
->off_linkpl
);
9257 s1
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9261 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9265 s
= new_stmt(cstate
, BPF_LD
|BPF_IMM
);
9269 /* Forcibly append these statements to the true condition
9270 * of the protocol check by creating a new block that is
9271 * always true and ANDing them. */
9272 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9275 b1
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9284 /* We need to store three values based on the Geneve header::
9285 * - The offset of the linktype.
9286 * - The offset of the end of the Geneve header.
9287 * - The offset of the end of the encapsulated MAC header. */
9288 static struct slist
*
9289 gen_geneve_offsets(compiler_state_t
*cstate
)
9291 struct slist
*s
, *s1
, *s_proto
;
9293 /* First we need to calculate the offset of the Geneve header
9294 * itself. This is composed of the IP header previously calculated
9295 * (include any variable link prefix) and stored in A plus the
9296 * fixed sized headers (fixed link prefix, MAC length, and UDP
9298 s
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9299 s
->s
.k
= cstate
->off_linkpl
.constant_part
+ cstate
->off_nl
+ 8;
9301 /* Stash this in X since we'll need it later. */
9302 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9305 /* The EtherType in Geneve is 2 bytes in. Calculate this and
9307 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9311 cstate
->off_linktype
.reg
= alloc_reg(cstate
);
9312 cstate
->off_linktype
.is_variable
= 1;
9313 cstate
->off_linktype
.constant_part
= 0;
9315 s1
= new_stmt(cstate
, BPF_ST
);
9316 s1
->s
.k
= cstate
->off_linktype
.reg
;
9319 /* Load the Geneve option length and mask and shift to get the
9320 * number of bytes. It is stored in the first byte of the Geneve
9322 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_B
);
9326 s1
= new_stmt(cstate
, BPF_ALU
|BPF_AND
|BPF_K
);
9330 s1
= new_stmt(cstate
, BPF_ALU
|BPF_MUL
|BPF_K
);
9334 /* Add in the rest of the Geneve base header. */
9335 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9339 /* Add the Geneve header length to its offset and store. */
9340 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_X
);
9344 /* Set the encapsulated type as Ethernet. Even though we may
9345 * not actually have Ethernet inside there are two reasons this
9347 * - The linktype field is always in EtherType format regardless
9348 * of whether it is in Geneve or an inner Ethernet frame.
9349 * - The only link layer that we have specific support for is
9350 * Ethernet. We will confirm that the packet actually is
9351 * Ethernet at runtime before executing these checks. */
9352 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 1, 0, alloc_reg(cstate
));
9354 s1
= new_stmt(cstate
, BPF_ST
);
9355 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9358 /* Calculate whether we have an Ethernet header or just raw IP/
9359 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
9360 * and linktype by 14 bytes so that the network header can be found
9361 * seamlessly. Otherwise, keep what we've calculated already. */
9363 /* We have a bare jmp so we can't use the optimizer. */
9364 cstate
->no_optimize
= 1;
9366 /* Load the EtherType in the Geneve header, 2 bytes in. */
9367 s1
= new_stmt(cstate
, BPF_LD
|BPF_IND
|BPF_H
);
9371 /* Load X with the end of the Geneve header. */
9372 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9373 s1
->s
.k
= cstate
->off_linkhdr
.reg
;
9376 /* Check if the EtherType is Transparent Ethernet Bridging. At the
9377 * end of this check, we should have the total length in X. In
9378 * the non-Ethernet case, it's already there. */
9379 s_proto
= new_stmt(cstate
, JMP(BPF_JEQ
));
9380 s_proto
->s
.k
= ETHERTYPE_TEB
;
9381 sappend(s
, s_proto
);
9383 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TXA
);
9387 /* Since this is Ethernet, use the EtherType of the payload
9388 * directly as the linktype. Overwrite what we already have. */
9389 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9393 s1
= new_stmt(cstate
, BPF_ST
);
9394 s1
->s
.k
= cstate
->off_linktype
.reg
;
9397 /* Advance two bytes further to get the end of the Ethernet
9399 s1
= new_stmt(cstate
, BPF_ALU
|BPF_ADD
|BPF_K
);
9403 /* Move the result to X. */
9404 s1
= new_stmt(cstate
, BPF_MISC
|BPF_TAX
);
9407 /* Store the final result of our linkpl calculation. */
9408 cstate
->off_linkpl
.reg
= alloc_reg(cstate
);
9409 cstate
->off_linkpl
.is_variable
= 1;
9410 cstate
->off_linkpl
.constant_part
= 0;
9412 s1
= new_stmt(cstate
, BPF_STX
);
9413 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9422 /* Check to see if this is a Geneve packet. */
9424 gen_geneve(compiler_state_t
*cstate
, bpf_u_int32 vni
, int has_vni
)
9426 struct block
*b0
, *b1
;
9430 * Catch errors reported by us and routines below us, and return NULL
9433 if (setjmp(cstate
->top_ctx
))
9436 b0
= gen_geneve4(cstate
, vni
, has_vni
);
9437 b1
= gen_geneve6(cstate
, vni
, has_vni
);
9442 /* Later filters should act on the payload of the Geneve frame,
9443 * update all of the header pointers. Attach this code so that
9444 * it gets executed in the event that the Geneve filter matches. */
9445 s
= gen_geneve_offsets(cstate
);
9447 b1
= gen_true(cstate
);
9448 sappend(s
, b1
->stmts
);
9453 cstate
->is_geneve
= 1;
9458 /* Check that the encapsulated frame has a link layer header
9459 * for Ethernet filters. */
9460 static struct block
*
9461 gen_geneve_ll_check(compiler_state_t
*cstate
)
9464 struct slist
*s
, *s1
;
9466 /* The easiest way to see if there is a link layer present
9467 * is to check if the link layer header and payload are not
9470 /* Geneve always generates pure variable offsets so we can
9471 * compare only the registers. */
9472 s
= new_stmt(cstate
, BPF_LD
|BPF_MEM
);
9473 s
->s
.k
= cstate
->off_linkhdr
.reg
;
9475 s1
= new_stmt(cstate
, BPF_LDX
|BPF_MEM
);
9476 s1
->s
.k
= cstate
->off_linkpl
.reg
;
9479 b0
= new_block(cstate
, BPF_JMP
|BPF_JEQ
|BPF_X
);
9487 static struct block
*
9488 gen_atmfield_code_internal(compiler_state_t
*cstate
, int atmfield
,
9489 bpf_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9496 if (!cstate
->is_atm
)
9497 bpf_error(cstate
, "'vpi' supported only on raw ATM");
9498 if (cstate
->off_vpi
== OFFSET_NOT_SET
)
9500 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vpi
, BPF_B
, 0xffffffff, jtype
,
9505 if (!cstate
->is_atm
)
9506 bpf_error(cstate
, "'vci' supported only on raw ATM");
9507 if (cstate
->off_vci
== OFFSET_NOT_SET
)
9509 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_vci
, BPF_H
, 0xffffffff, jtype
,
9514 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9515 abort(); /* XXX - this isn't on FreeBSD */
9516 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0x0f, jtype
,
9521 if (cstate
->off_payload
== OFFSET_NOT_SET
)
9523 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_payload
+ MSG_TYPE_POS
, BPF_B
,
9524 0xffffffff, jtype
, reverse
, jvalue
);
9528 if (!cstate
->is_atm
)
9529 bpf_error(cstate
, "'callref' supported only on raw ATM");
9530 if (cstate
->off_proto
== OFFSET_NOT_SET
)
9532 b0
= gen_ncmp(cstate
, OR_LINKHDR
, cstate
->off_proto
, BPF_B
, 0xffffffff,
9533 jtype
, reverse
, jvalue
);
9542 static struct block
*
9543 gen_atmtype_metac(compiler_state_t
*cstate
)
9545 struct block
*b0
, *b1
;
9547 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9548 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 1, BPF_JEQ
, 0);
9553 static struct block
*
9554 gen_atmtype_sc(compiler_state_t
*cstate
)
9556 struct block
*b0
, *b1
;
9558 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9559 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 5, BPF_JEQ
, 0);
9564 static struct block
*
9565 gen_atmtype_llc(compiler_state_t
*cstate
)
9569 b0
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
9570 cstate
->linktype
= cstate
->prevlinktype
;
9575 gen_atmfield_code(compiler_state_t
*cstate
, int atmfield
,
9576 bpf_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9579 * Catch errors reported by us and routines below us, and return NULL
9582 if (setjmp(cstate
->top_ctx
))
9585 return gen_atmfield_code_internal(cstate
, atmfield
, jvalue
, jtype
,
9590 gen_atmtype_abbrev(compiler_state_t
*cstate
, int type
)
9592 struct block
*b0
, *b1
;
9595 * Catch errors reported by us and routines below us, and return NULL
9598 if (setjmp(cstate
->top_ctx
))
9604 /* Get all packets in Meta signalling Circuit */
9605 if (!cstate
->is_atm
)
9606 bpf_error(cstate
, "'metac' supported only on raw ATM");
9607 b1
= gen_atmtype_metac(cstate
);
9611 /* Get all packets in Broadcast Circuit*/
9612 if (!cstate
->is_atm
)
9613 bpf_error(cstate
, "'bcc' supported only on raw ATM");
9614 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9615 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 2, BPF_JEQ
, 0);
9620 /* Get all cells in Segment OAM F4 circuit*/
9621 if (!cstate
->is_atm
)
9622 bpf_error(cstate
, "'oam4sc' supported only on raw ATM");
9623 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9624 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9629 /* Get all cells in End-to-End OAM F4 Circuit*/
9630 if (!cstate
->is_atm
)
9631 bpf_error(cstate
, "'oam4ec' supported only on raw ATM");
9632 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9633 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9638 /* Get all packets in connection Signalling Circuit */
9639 if (!cstate
->is_atm
)
9640 bpf_error(cstate
, "'sc' supported only on raw ATM");
9641 b1
= gen_atmtype_sc(cstate
);
9645 /* Get all packets in ILMI Circuit */
9646 if (!cstate
->is_atm
)
9647 bpf_error(cstate
, "'ilmic' supported only on raw ATM");
9648 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9649 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 16, BPF_JEQ
, 0);
9654 /* Get all LANE packets */
9655 if (!cstate
->is_atm
)
9656 bpf_error(cstate
, "'lane' supported only on raw ATM");
9657 b1
= gen_atmfield_code_internal(cstate
, A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
9660 * Arrange that all subsequent tests assume LANE
9661 * rather than LLC-encapsulated packets, and set
9662 * the offsets appropriately for LANE-encapsulated
9665 * We assume LANE means Ethernet, not Token Ring.
9667 PUSH_LINKHDR(cstate
, DLT_EN10MB
, 0,
9668 cstate
->off_payload
+ 2, /* Ethernet header */
9670 cstate
->off_linktype
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 12;
9671 cstate
->off_linkpl
.constant_part
= cstate
->off_linkhdr
.constant_part
+ 14; /* Ethernet */
9672 cstate
->off_nl
= 0; /* Ethernet II */
9673 cstate
->off_nl_nosnap
= 3; /* 802.3+802.2 */
9677 /* Get all LLC-encapsulated packets */
9678 if (!cstate
->is_atm
)
9679 bpf_error(cstate
, "'llc' supported only on raw ATM");
9680 b1
= gen_atmtype_llc(cstate
);
9690 * Filtering for MTP2 messages based on li value
9691 * FISU, length is null
9692 * LSSU, length is 1 or 2
9693 * MSU, length is 3 or more
9694 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
9697 gen_mtp2type_abbrev(compiler_state_t
*cstate
, int type
)
9699 struct block
*b0
, *b1
;
9702 * Catch errors reported by us and routines below us, and return NULL
9705 if (setjmp(cstate
->top_ctx
))
9711 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9712 (cstate
->linktype
!= DLT_ERF
) &&
9713 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9714 bpf_error(cstate
, "'fisu' supported only on MTP2");
9715 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9716 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
9720 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9721 (cstate
->linktype
!= DLT_ERF
) &&
9722 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9723 bpf_error(cstate
, "'lssu' supported only on MTP2");
9724 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
9725 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
9730 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9731 (cstate
->linktype
!= DLT_ERF
) &&
9732 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9733 bpf_error(cstate
, "'msu' supported only on MTP2");
9734 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
9738 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9739 (cstate
->linktype
!= DLT_ERF
) &&
9740 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9741 bpf_error(cstate
, "'hfisu' supported only on MTP2_HSL");
9742 /* gen_ncmp(cstate, offrel, offset, size, mask, jtype, reverse, value) */
9743 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
9747 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9748 (cstate
->linktype
!= DLT_ERF
) &&
9749 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9750 bpf_error(cstate
, "'hlssu' supported only on MTP2_HSL");
9751 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
9752 b1
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
9757 if ( (cstate
->linktype
!= DLT_MTP2
) &&
9758 (cstate
->linktype
!= DLT_ERF
) &&
9759 (cstate
->linktype
!= DLT_MTP2_WITH_PHDR
) )
9760 bpf_error(cstate
, "'hmsu' supported only on MTP2_HSL");
9761 b0
= gen_ncmp(cstate
, OR_PACKET
, cstate
->off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
9771 gen_mtp3field_code(compiler_state_t
*cstate
, int mtp3field
,
9772 volatile bpf_u_int32 jvalue
, bpf_u_int32 jtype
, int reverse
)
9775 bpf_u_int32 val1
, val2
, val3
;
9782 * Catch errors reported by us and routines below us, and return NULL
9785 if (setjmp(cstate
->top_ctx
))
9788 newoff_sio
= cstate
->off_sio
;
9789 newoff_opc
= cstate
->off_opc
;
9790 newoff_dpc
= cstate
->off_dpc
;
9791 newoff_sls
= cstate
->off_sls
;
9792 switch (mtp3field
) {
9795 newoff_sio
+= 3; /* offset for MTP2_HSL */
9799 if (cstate
->off_sio
== OFFSET_NOT_SET
)
9800 bpf_error(cstate
, "'sio' supported only on SS7");
9801 /* sio coded on 1 byte so max value 255 */
9803 bpf_error(cstate
, "sio value %u too big; max value = 255",
9805 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
9806 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9812 if (cstate
->off_opc
== OFFSET_NOT_SET
)
9813 bpf_error(cstate
, "'opc' supported only on SS7");
9814 /* opc coded on 14 bits so max value 16383 */
9816 bpf_error(cstate
, "opc value %u too big; max value = 16383",
9818 /* the following instructions are made to convert jvalue
9819 * to the form used to write opc in an ss7 message*/
9820 val1
= jvalue
& 0x00003c00;
9822 val2
= jvalue
& 0x000003fc;
9824 val3
= jvalue
& 0x00000003;
9826 jvalue
= val1
+ val2
+ val3
;
9827 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
9828 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9836 if (cstate
->off_dpc
== OFFSET_NOT_SET
)
9837 bpf_error(cstate
, "'dpc' supported only on SS7");
9838 /* dpc coded on 14 bits so max value 16383 */
9840 bpf_error(cstate
, "dpc value %u too big; max value = 16383",
9842 /* the following instructions are made to convert jvalue
9843 * to the forme used to write dpc in an ss7 message*/
9844 val1
= jvalue
& 0x000000ff;
9846 val2
= jvalue
& 0x00003f00;
9848 jvalue
= val1
+ val2
;
9849 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
9850 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
9856 if (cstate
->off_sls
== OFFSET_NOT_SET
)
9857 bpf_error(cstate
, "'sls' supported only on SS7");
9858 /* sls coded on 4 bits so max value 15 */
9860 bpf_error(cstate
, "sls value %u too big; max value = 15",
9862 /* the following instruction is made to convert jvalue
9863 * to the forme used to write sls in an ss7 message*/
9864 jvalue
= jvalue
<< 4;
9865 b0
= gen_ncmp(cstate
, OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
9866 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
9875 static struct block
*
9876 gen_msg_abbrev(compiler_state_t
*cstate
, int type
)
9881 * Q.2931 signalling protocol messages for handling virtual circuits
9882 * establishment and teardown
9887 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
9891 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
9895 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
9899 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
9903 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
9906 case A_RELEASE_DONE
:
9907 b1
= gen_atmfield_code_internal(cstate
, A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
9917 gen_atmmulti_abbrev(compiler_state_t
*cstate
, int type
)
9919 struct block
*b0
, *b1
;
9922 * Catch errors reported by us and routines below us, and return NULL
9925 if (setjmp(cstate
->top_ctx
))
9931 if (!cstate
->is_atm
)
9932 bpf_error(cstate
, "'oam' supported only on raw ATM");
9934 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9935 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9937 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9942 if (!cstate
->is_atm
)
9943 bpf_error(cstate
, "'oamf4' supported only on raw ATM");
9945 b0
= gen_atmfield_code_internal(cstate
, A_VCI
, 3, BPF_JEQ
, 0);
9946 b1
= gen_atmfield_code_internal(cstate
, A_VCI
, 4, BPF_JEQ
, 0);
9948 b0
= gen_atmfield_code_internal(cstate
, A_VPI
, 0, BPF_JEQ
, 0);
9954 * Get Q.2931 signalling messages for switched
9955 * virtual connection
9957 if (!cstate
->is_atm
)
9958 bpf_error(cstate
, "'connectmsg' supported only on raw ATM");
9959 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9960 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9962 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9964 b0
= gen_msg_abbrev(cstate
, A_CONNECTACK
);
9966 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9968 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9970 b0
= gen_atmtype_sc(cstate
);
9975 if (!cstate
->is_atm
)
9976 bpf_error(cstate
, "'metaconnect' supported only on raw ATM");
9977 b0
= gen_msg_abbrev(cstate
, A_SETUP
);
9978 b1
= gen_msg_abbrev(cstate
, A_CALLPROCEED
);
9980 b0
= gen_msg_abbrev(cstate
, A_CONNECT
);
9982 b0
= gen_msg_abbrev(cstate
, A_RELEASE
);
9984 b0
= gen_msg_abbrev(cstate
, A_RELEASE_DONE
);
9986 b0
= gen_atmtype_metac(cstate
);